I'll preface this with acknowledging all the help from various people who have replied to this thread and I'll promise to try and figure out perl over shell scripts, but two weeks ago the "Hello World!" script in any programing language was "Greek" to me so bare with my ignorance for a week or two more. But in the interest of sharing here is my current bwa samse pipeline. I'm stuck at not being able to merge the sorted bam files but at least this lets me get back to the lab and opens up some of my time to write the back log of papers I need to polish off so I can get my own lab and make someone else deal with these issues

**Currently, this uses all default settings so modify commands as you see fit***
Code:
#!/bin/sh
# Batch_BWAsamse_V1.sh
# Created by Jonathan Keats on 04/05/10.
# This script is designed to take a batch of raw Illumina 1.3+ reads to sorted BAM files.
# It is designed to be initiated from a folder called "NGS" with a specific subdirectory structure
# Use the script called "Create_NGS_DirectoryStructureV1.sh" or the other variations (see Jon_Keats SeqAnswers Introduction Thread) to create the analysis directory structure
# To run this script you MUST first place your reference file in "NGS/RefGenomes/BWA_Indexed" and have run the "bwa index" command to create the BWT index files
# If you are NOT using a reference genome called "hg18.fa" you will NEED to edit lines 124 and 150 accordingly!
# The script is based on having ***RENAMED*** Illumina files in NGS/InputSequence/Illumina/Read1 and if available NGS/InputSequence/Illumina/Read2 if paired end runs
# The renamed format MUST be "YourSampleName_RunX_LaneX_R1.txt" and "YourSampleName_RunX_LaneX_R2.txt" otherwise files will be overwritten, paired-end analysis will not be possible, and lane merging will not be possible
# At each step it queries specific folders for available files and passes them to the next analysis module
# After each step the filename extension of the output files are corrected. (ie. "MySequenceFile.txt.fastq" to "MySequenceFile.fastq")
# Order of Embedded Steps - Converts Illumina 1.3+ fastq files "s_1_sequence.txt" to Sanger fastq using "maq ill2sanger" command
# - Aligns created fastq files to reference genome using "bwa aln" command
# - Generates SAM files from alignment files using "bwa samse" command
# - Converts SAM files to BAM files using "samtools view" command
# - Sorts BAM files using "samtools sort" command
# - Final output files are archived then the input and analysis directories are cleaned-up and readied for the next analysis batch
# ***DURING CLEAN-UP THE ENTIRE CONTENTS OF SEVERAL FOLDERS ARE DELETED, SO ONLY PLACE ADDITIONAL FILES IN THE "NGS/FINALOUTPUTS" DIRECTORY OR OUTSIDE THE NGS FOLDER SUBDIRECTORIES***
# The script creates a log file in "NGS/AnalysisNotes" to track the steps completed and the time each step started and finished
# Some of the log events will print to both the terminal screen and the log file so you can see what is going on
# On our workstation this will process two lanes of PE data overnight or a full run over the weekend
# Sould you merge files (lane 1 and 2) before analysis as illumina fastq files or after sorting BAM files?
# If you merge before analysis does this mess up the a paired-end analysis?
# Carabelle - Can you have a fail safe start query to kill the script if it is not initiated from the "NGS" folder? Probably doesn't run anyways
# Carabelle - Also can you check if destination folders are empty and if not kill the script?
# Carabelle - Such as if directory NGS/SAMfiles is NOT empty kill script and print "The NGS/SAMfiles directory is not empty please remove files otherwise they will be deleted"
# Ligne=Line en francais (I had a significant amount of help from our French post-docs husband who is a unix programer in Paris)
#Starting directory = NGS/
echo ***Starting Analysis Batch***
date '+%m/%d/%y %H:%M:%S'
#The following step creates the log file in the AnalysisNotes subdirectory the first time the script is run
#On subsequent runs the results are printed at the bottom of the pre-existing log file
echo ***Starting Analysis Batch*** >> AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> AnalysisNotes/Analysis.log
#In this step we will converted the original Illumina 1.3+ fastq format files to Sanger fastq format
#Input files from NGS/InputSequences/Illumina/Read1 and NGS/InputSequences/Illumina/Read2
#Output files to NGS/InputSequences/SangerFastq
echo Starting Step1a - Read1 Fastq Conversion with maq ill2sanger
date '+%m/%d/%y %H:%M:%S'
echo Starting Step1a - Read1 Fastq Conversion with maq ill2sanger >> AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> AnalysisNotes/Analysis.log
cd InputSequence/Illumina/Read1
#Current directory = NGS/InputSequence/Illumina/Read1
echo Converting the following Illumina files from the Read1 folder:
for ligne in `ls *.txt`
do
echo $ligne
done
echo Converting the following Illumina files from the Read1 folder: >> ../../../AnalysisNotes/Analysis.log
for ligne in `ls *.txt`
do
echo $ligne >> ../../../AnalysisNotes/Analysis.log
done
for ligne in `ls *.txt`
do
maq ill2sanger $ligne ../../SangerFastq/$ligne.fastq
done
echo Finished Step1a - Read1 Fastq Conversion
date '+%m/%d/%y %H:%M:%S'
echo Finished Step1a - Read1 Fastq Conversion >> ../../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../../AnalysisNotes/Analysis.log
cd ../Read2
#Current directory = NGS/InputSequence/Illumina/Read2
echo Starting Step1b - Read2 Fastq Conversion with maq ill2sanger
date '+%m/%d/%y %H:%M:%S'
echo Starting Step1b - Read2 Fastq Conversion with maq ill2sanger >> ../../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../../AnalysisNotes/Analysis.log
echo Converting the following Illumina files from the Read2 folder:
for ligne in `ls *.txt`
do
echo $ligne
done
echo Converting the following Illumina files from the Read2 folder: >> ../../../AnalysisNotes/Analysis.log
for ligne in `ls *.txt`
do
echo $ligne >> ../../../AnalysisNotes/Analysis.log
done
for ligne in `ls *.txt`
do
maq ill2sanger $ligne ../../SangerFastq/$ligne.fastq
done
cd ../../SangerFastq/
#Current directory = NGS/InputSequence/SangerFastq
#The next step will change the file extensions of ths Sanger Fastq outputs from "File_Run1_Lane1_R1.txt.fastq" to "File_Run1_Lane1_R1.fastq"
old_ext=txt.fastq
new_ext=fastq
find . -type f -name "*.$old_ext" -print | while read file
do
mv $file ${file%${old_ext}}${new_ext}
done
echo Finished Step1b - Read2 Fastq Conversion
date '+%m/%d/%y %H:%M:%S'
echo Finished Step1b - Read2 Fastq Conversion >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
#In the next step we will align the converted sanger fastq format files to the reference genome (hg18.fa)
echo Starting Step2 - bwa aln process
date '+%m/%d/%y %H:%M:%S'
echo Starting Step2 - bwa aln process >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
echo The following fastq files will be aligned:
for ligne in `ls *.fastq`
do
echo $ligne
done
echo The following fastq files will be aligned: >> ../../AnalysisNotes/Analysis.log
for ligne in `ls *.fastq`
do
echo $ligne >> ../../AnalysisNotes/Analysis.log
done
for ligne in `ls *.fastq`
do
bwa aln ../../RefGenomes/BWA_Indexed/hg18.fa $ligne > $ligne.sai
done
echo Finished Step2 - bwa aln process
date '+%m/%d/%y %H:%M:%S'
echo Finished Step2 - bwa aln process >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
#In the next step we will generate SAM files for the alignments using bwa samse
echo Starting Step3 - bwa samse process
date '+%m/%d/%y %H:%M:%S'
echo Starting Step3 - bwa samse process >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
echo The following alignment files will be converted to SAM files:
for ligne in `ls *.sai`
do
echo $ligne
done
echo The following alignment files will be converted to SAM files: >> ../../AnalysisNotes/Analysis.log
for ligne in `ls *.sai`
do
echo $ligne >> ../../AnalysisNotes/Analysis.log
done
#To get this step to work I had to trick the system to get the conventional (bwa samse <database.fasta> <aln.sai> <input.fastq> > aln.sam) command as I can only pull up one list at a time to automatically feed files into the command
for ligne in `ls *.fastq`
do
bwa samse ../../RefGenomes/BWA_Indexed/hg18.fa $ligne.sai $ligne > ../../SAMfiles/$ligne.sam
done
#This step renames the bwa aln output files from "File_Run1_Lane1_R1.fastq.sai" to "File_Run1_Lane1_R1.sai"
#This is done on purpuse at this point to allow the preceeding step to work
old_ext=fastq.sai
new_ext=sai
find . -type f -name "*.$old_ext" -print | while read file
do
mv $file ${file%${old_ext}}${new_ext}
done
cd ../../SAMfiles/
#Current directory = NGS/SAMfiles
#This step renames the bwa samse output files from "File_Run1_Lane1_R1.fastq.sam" to "File_Run1_Lane1_R1_bwase.sam"
#We up in the "_bwase" to indicate the files are bwa alignments converted to SAM files with the bwa samse command
old_ext=.fastq.sam
new_ext=_bwase.sam
find . -type f -name "*$old_ext" -print | while read file
do
mv $file ${file%${old_ext}}${new_ext}
done
echo Finished Step3 - bwa samse process
date '+%m/%d/%y %H:%M:%S'
echo Finished Step3 - bwa samse process >> ../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../AnalysisNotes/Analysis.log
#In the next step we will convert each SAM file to a BAM file
echo Starting Step4 - samtools SAM to BAM conversion
date '+%m/%d/%y %H:%M:%S'
echo Starting Step4 - samtools SAM to BAM conversion >> ../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../AnalysisNotes/Analysis.log
echo The following SAM files will be converted to BAM files:
for ligne in `ls *.sam`
do
echo $ligne
done
echo The following SAM files will be converted to BAM files: >> ../AnalysisNotes/Analysis.log
for ligne in `ls *.sam`
do
echo $ligne >> ../AnalysisNotes/Analysis.log
done
for ligne in `ls *.sam`
do
samtools view -bS -o ../BAMfiles/Original/$ligne.bam $ligne
done
cd ../BAMfiles/Original
#Current directory = NGS/BAMfiles/Original
#This step renames the samtools view output files from "File_Run1_Lane1_R1_bwase.sam.bam" to "File_Run1_Lane1_R1_bwase.bam"
old_ext=sam.bam
new_ext=bam
find . -type f -name "*.$old_ext" -print | while read file
do
mv $file ${file%${old_ext}}${new_ext}
done
echo Finished Step4 - samtools SAM to BAM conversion
date '+%m/%d/%y %H:%M:%S'
echo Finished Step4 - samtools SAM to BAM conversion >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
#In the next step we will sort the BAM file by chromosome coordinate
echo Starting Step4 - samtools BAM sorting process
date '+%m/%d/%y %H:%M:%S'
echo Starting Step4 - samtools BAM sorting process >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
echo The following BAM files will be sorted:
for ligne in `ls *.bam`
do
echo $ligne
done
echo The following BAM files will be sorted: >> ../../AnalysisNotes/Analysis.log
for ligne in `ls *.bam`
do
echo $ligne >> ../../AnalysisNotes/Analysis.log
done
for ligne in `ls *.bam`
do
samtools sort $ligne ../Sorted/$ligne
done
cd ../Sorted
#Current directory = NGS/BAMfiles/Sorted
#This step renames the samtools sort output files from "File_Run1_Lane1_R1_bwase.bam.bam" to "File_Run1_Lane1_R1_bwase_sorted.bam
old_ext=.bam.bam
new_ext=_sorted.bam
find . -type f -name "*$old_ext" -print | while read file
do
mv $file ${file%${old_ext}}${new_ext}
done
echo Finished Step4 - samtools BAM sorting process
date '+%m/%d/%y %H:%M:%S'
echo Finished Step4 - samtools BAM sorting process >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
cd ../..
#Current directory = NGS/
#In the next step we will move files from the input and analysis directories to their respective "NGS/FinalOutputs" subdirectory so the pipeline can be used another time.
echo Cleaning up Input and Analysis Directories
date '+%m/%d/%y %H:%M:%S'
echo Cleaning up Input and Analysis Directories >> AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> AnalysisNotes/Analysis.log
cd InputSequence/Illumina/Read1
#Current directory = NGS/InputSequence/Illumina/Read1
echo Moving the following Illumina Fastq Files from NGS/InputSequence/Illumina/Read1 to NGS/FinalOutputs/Illumina/Read1:
for ligne in `ls *.txt`
do
echo $ligne
done
echo Moving Illumina Fastq Files from NGS/InputSequence/Illumina/Read1 to NGS/FinalOutputs/Illumina/Read1 >> ../../../AnalysisNotes/Analysis.log
for ligne in `ls *.txt`
do
echo $ligne >> ../../../AnalysisNotes/Analysis.log
done
for ligne in `ls *.txt`
do
mv $ligne ../../../FinalOutputs/Illumina/Read1/
done
cd ../Read2
#Current directory = NGS/InputSequence/Illumina/Read2
echo Moving the following Illumina Fastq Files from NGS/InputSequence/Illumina/Read2 to NGS/FinalOutputs/Illumina/Read2:
for ligne in `ls *.txt`
do
echo $ligne
done
echo Moving Illumina Fastq Files from NGS/InputSequence/Illumina/Read2 to NGS/FinalOutputs/Illumina/Read2 >> ../../../AnalysisNotes/Analysis.log
for ligne in `ls *.txt`
do
echo $ligne >> ../../../AnalysisNotes/Analysis.log
done
for ligne in `ls *.txt`
do
mv $ligne ../../../FinalOutputs/Illumina/Read2/
done
echo Moving Illumina Input Files Complete
date '+%m/%d/%y %H:%M:%S'
echo Moving Illumina Input Files Complete >> ../../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../../AnalysisNotes/Analysis.log
cd ../../SangerFastq
#Current directory = NGS/InputSequence/SangerFastq
echo Moving the following Sanger Format Fastq Files from NGS/InputSequence/SangerFastq to NGS/FinalOutputs/SangerFastq:
for ligne in `ls *.fastq`
do
echo $ligne
done
echo Moving the following Sanger Format Fastq Files from NGS/InputSequence/SangerFastq to NGS/FinalOutputs/SangerFastq: >> ../../AnalysisNotes/Analysis.log
for ligne in `ls *.fastq`
do
echo $ligne >> ../../AnalysisNotes/Analysis.log
done
for ligne in `ls *.fastq`
do
mv $ligne ../../FinalOutputs/SangerFastq/
done
echo Moving Sanger Format Fastq Files Complete
date '+%m/%d/%y %H:%M:%S'
echo Moving Sanger Format Fastq Files Complete >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
echo Moving the following Alignment .sai Files from NGS/InputSequence/SangerFastq to NGS/FinalOutputs/AlignmentResults:
for ligne in `ls *.sai`
do
echo $ligne
done
echo Moving the following Alignment .sai Files from NGS/InputSequence/SangerFastq to NGS/FinalOutputs/AlignmentResults: >> ../../AnalysisNotes/Analysis.log
for ligne in `ls *.sai`
do
echo $ligne >> ../../AnalysisNotes/Analysis.log
done
for ligne in `ls *.sai`
do
mv $ligne ../../FinalOutputs/AlignmentResults/
done
echo Moving Alignment Results Files Complete
date '+%m/%d/%y %H:%M:%S'
echo Moving Alignment Results Files Complete >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
cd ../../BAMfiles/Sorted/
#Current directory = NGS/BAMfiles/Sorted/
echo Moving the following Sorted BAM files from NGS/BAMfiles/Sorted/ to NGS/FinalOutputs/SortedBAMfiles:
for ligne in `ls *.bam`
do
echo $ligne
done
echo Moving the following Sorted BAM files from NGS/BAMfiles/Sorted/ to NGS/FinalOutputs/SortedBAMfiles: >> ../../AnalysisNotes/Analysis.log
for ligne in `ls *.bam`
do
echo $ligne >> ../../AnalysisNotes/Analysis.log
done
for ligne in `ls *.bam`
do
mv $ligne ../../FinalOutputs/SortedBAMfiles/
done
echo Moving Sorted BAM Files Complete
date '+%m/%d/%y %H:%M:%S'
echo Moving Alignment Results Files Complete >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
cd ../../Samfiles
#Current directory = NGS/SAMfiles
echo Deleting the following SAM Files from NGS/SAMfiles:
for ligne in `ls *.sam`
do
echo $ligne
done
echo Deleting the following SAM Files from NGS/SAMfiles: >> ../AnalysisNotes/Analysis.log
for ligne in `ls *.sam`
do
echo $ligne >> ../AnalysisNotes/Analysis.log
done
for ligne in `ls *.sam`
do
rm $ligne
done
echo Deleting SAM Files Complete
date '+%m/%d/%y %H:%M:%S'
echo Deleting SAM Files Complete >> ../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../AnalysisNotes/Analysis.log
cd ../BAMfiles/Original/
#Current directory = NGS/BAMfiles/Original
echo Deleting the following BAM Files from NGS/BAMfiles/Original:
for ligne in `ls *.bam`
do
echo $ligne
done
echo Deleting the following BAM Files from NGS/BAMfiles/Original: >> ../../AnalysisNotes/Analysis.log
for ligne in `ls *.bam`
do
echo $ligne >> ../../AnalysisNotes/Analysis.log
done
for ligne in `ls *.bam`
do
rm $ligne
done
echo Deleting BAM Files from NGS/BAMfiles/Original Complete
date '+%m/%d/%y %H:%M:%S'
echo Deleting BAM Files from NGS/BAMfiles/Original Complete >> ../../AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> ../../AnalysisNotes/Analysis.log
cd ../..
#Current directory = NGS/
echo Cleaning up Input and Analysis Directories Complete
date '+%m/%d/%y %H:%M:%S'
echo Cleaning up Input and Analysis Directories Complete >> AnalysisNotes/Analysis.log
date '+%m/%d/%y %H:%M:%S' >> AnalysisNotes/Analysis.log
echo ***Analysis Batch Complete***
echo ***Analysis Batch Complete*** >> AnalysisNotes/Analysis.log
# Future development: Merge BAM files based on filename structure "RunX_LaneX_RX", Index files for IGV viewing
# Figure out Perl or Python so I don't look like such an amateur :)

Leave a comment: