-
Notifications
You must be signed in to change notification settings - Fork 11
/
ngs_BOWTIE.sh
executable file
·314 lines (267 loc) · 14 KB
/
ngs_BOWTIE.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/bin/bash
# Copyright (c) 2012-2014, Stephen Fisher, Hoa Giang, and Junhyong Kim, University of
# Pennsylvania. All Rights Reserved.
#
# You may not use this file except in compliance with the Kim Lab License
# located at
#
# http://kim.bio.upenn.edu/software/LICENSE
#
# Unless required by applicable law or agreed to in writing, this
# software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License
# for the specific language governing permissions and limitations
# under the License.
##########################################################################################
# SINGLE-END READS:
# INPUT: $SAMPLE/trim/unaligned_1.fq
# OUTPUT: $SAMPLE/bowtie/$SAMPLE.bowtie.sorted.bam
#
# PAIRED-END READS:
# INPUT: $SAMPLE/trim/unaligned_1.fq and $SAMPLE/trim/unaligned_2.fq
# OUTPUT: $SAMPLE/bowtie/$SAMPLE.bowtie.sorted.bam and $SAMPLE/bowtie/SE_mapping
#
# REQUIRES: bowtie, samtools
##########################################################################################
##########################################################################################
# USAGE
##########################################################################################
NGS_USAGE+="Usage: `basename $0` bowtie OPTIONS sampleID -- run bowtie on trimmed reads\n"
##########################################################################################
# HELP TEXT
##########################################################################################
ngsHelp_BOWTIE() {
echo -e "Usage:\n\t`basename $0` bowtie [-i inputDir] [-v mismatches] [-m maxMulti] [-minins minInsertSize] [-maxins maxInsertSize] -p numProc -s species [-se] sampleID"
echo -e "Input:\n\tsampleID/inputDir/unaligned_1.fq\n\tsampleID/inputDir/unaligned_2.fq (paired-end reads)"
echo -e "Output:\n\tsampleID/bowtie/sampleID.bowtie.sorted.bam\n\tsampleID/bowtie/sampleID.suppressed.sorted.bam\n\tsampleID/bowtie/sampleID.stats.txt"
echo -e "Requires:\n\tbowtie ( http://bowtie-bio.sourceforge.net/index.shtml )\n\tsamtools ( http://samtools.sourceforge.net/ )"
echo -e "Options:"
echo -e "\t-i inputDir - directory with unaligned reads (default: trim)"
echo -e "\t-v mismatches - maximum mismatches allowed per read length (default: 3)"
echo -e "\t-m maxMulti - suppress all alignments if > m alignments (default: 1)"
echo -e "\t-minins minInsertSize - minimum insert size for PE alignment (default: 250bp)"
echo -e "\t-maxins maxInsertSize - maximum insert size for PE alignment (default: 450bp)"
echo -e "\t-p numProc - number of cpu to use"
echo -e "\t-s species - species from repository: $BOWTIE_REPO"
echo -e "\t-se - single-end reads (default: paired-end)\n"
echo -e "Run bowtie on the unaligned reads (ie sampleID/inputDir). The arguments used assume Bowtie version 1. Output is placed in the directory sampleID/bowtie. Multimapping reads that exceed the maxMulti count are output to the sampleID.suppressed.sorted.bam file (ie the Bowtie -max flag is used to direct the reads to this file). For paired-end samples, after the paired mapping is complete then all unmapped reads are aligned as if they were single-end with the alignments stored in the sampleID/bowtie/SE_mapping directory. The alignment stats for the single-end and paired-end mappings are reported in the stats file."
}
##########################################################################################
# LOCAL VARIABLES WITH DEFAULT VALUES. Using the naming convention to
# make sure these variables don't collide with the other modules.
##########################################################################################
ngsLocal_BOWTIE_INP_DIR="trim"
ngsLocal_BOWTIE_MISMATCHES=3
ngsLocal_BOWTIE_MAXMULTI=1
ngsLocal_BOWTIE_MININS=250
ngsLocal_BOWTIE_MAXINS=450
##########################################################################################
# PROCESSING COMMAND LINE ARGUMENTS
# BOWTIE args: -p value, -s value, -se (optional), sampleID
##########################################################################################
ngsArgs_BOWTIE() {
if [ $# -lt 5 ]; then printHelp "BOWTIE"; fi
# getopts doesn't allow for optional arguments so handle them manually
while true; do
case $1 in
-i) ngsLocal_BOWTIE_INP_DIR=$2
shift; shift;
;;
-v) ngsLocal_BOWTIE_MISMATCHES=$2
shift; shift;
;;
-m) ngsLocal_BOWTIE_MAXMULTI=$2
shift; shift;
;;
-minins) ngsLocal_BOWTIE_MININS=$2
shift; shift;
;;
-maxins) ngsLocal_BOWTIE_MAXINS=$2
shift; shift;
;;
-p) NUMCPU=$2
shift; shift;
;;
-s) SPECIES=$2
shift; shift;
;;
-se) SE=true
shift;
;;
-*) printf "Illegal option: '%s'\n" "$1"
printHelp $COMMAND
exit 0
;;
*) break ;;
esac
done
SAMPLE=$1
}
##########################################################################################
# RUNNING COMMAND ACTION
# Run BOWTIE. Run BOWTIE on untrimmed data.
##########################################################################################
ngsCmd_BOWTIE() {
if $SE; then prnCmd "# BEGIN: BOWTIE SINGLE-END ALIGNMENT"
else prnCmd "# BEGIN: BOWTIE PAIRED-END ALIGNMENT"; fi
# make relevant directory
if [ ! -d $SAMPLE/bowtie ]; then
prnCmd "mkdir $SAMPLE/bowtie"
if ! $DEBUG; then mkdir $SAMPLE/bowtie; fi
if ! $SE; then
prnCmd "mkdir $SAMPLE/bowtie/SE_mapping"
if ! $DEBUG; then mkdir $SAMPLE/bowtie/SE_mapping; fi
fi
fi
# print version info in $SAMPLE directory
prnCmd "# bowtie version: bowtie --version | head -1 | awk '{print \$3}'"
prnCmd "# samtools version: samtools 2>&1 | grep 'Version:' | awk '{print \$2}'"
if ! $DEBUG; then
# gets this: "bowtie version 0.12.7"
# returns this: "0.12.7"
ver=$(bowtie --version | head -1 | awk '{print $3}')
sver=$(samtools 2>&1 | grep 'Version:' | awk '{print $2}')
local prnSE=0
if $SE; then prnSE=1; fi
prnVersion "bowtie" \
"program\tversion\tprogram\tversion\tspecies\tnMismatch\tmaxMulti\tminIns\tmaxIns\tSE" \
"bowtie\t$ver\tsamtools\t$sver\t$SPECIES\t$ngsLocal_BOWTIE_MISMATCHES\t$ngsLocal_BOWTIE_MAXMULTI\t$ngsLocal_BOWTIE_MININS\t$ngsLocal_BOWTIE_MAXINS\t$SE"
fi
if $SE; then
# single-end
prnCmd "bowtie -t -v $ngsLocal_BOWTIE_MISMATCHES -a -m $ngsLocal_BOWTIE_MAXMULTI --best --sam -p $NUMCPU $BOWTIE_REPO/$SPECIES $SAMPLE/$ngsLocal_BOWTIE_INP_DIR/unaligned_1.fq $SAMPLE/bowtie/output_p.sam --max $SAMPLE/bowtie/suppressed.fq --un $SAMPLE/bowtie/notMapped_1.fq > $SAMPLE/bowtie/$SAMPLE.stats.txt 2>&1"
if ! $DEBUG; then
bowtie -t -v $ngsLocal_BOWTIE_MISMATCHES -a -m $ngsLocal_BOWTIE_MAXMULTI --best --sam -p $NUMCPU $BOWTIE_REPO/$SPECIES $SAMPLE/$ngsLocal_BOWTIE_INP_DIR/unaligned_1.fq $SAMPLE/bowtie/output_p.sam --max $SAMPLE/bowtie/suppressed.fq --un $SAMPLE/bowtie/notMapped_1.fq > $SAMPLE/bowtie/$SAMPLE.stats.txt 2>&1
fi
else
# paired-end
prnCmd "bowtie -t -v $ngsLocal_BOWTIE_MISMATCHES --minins $ngsLocal_BOWTIE_MININS --maxins $ngsLocal_BOWTIE_MAXINS -a -m $ngsLocal_BOWTIE_MAXMULTI --best --sam -p $NUMCPU $BOWTIE_REPO/$SPECIES -1 $SAMPLE/$ngsLocal_BOWTIE_INP_DIR/unaligned_1.fq -2 $SAMPLE/$ngsLocal_BOWTIE_INP_DIR/unaligned_2.fq $SAMPLE/bowtie/output_p.sam --max $SAMPLE/bowtie/suppressed.sam --un $SAMPLE/bowtie/unmapped.fq > $SAMPLE/bowtie/$SAMPLE.stats.txt 2>&1"
if ! $DEBUG; then
bowtie -t -v $ngsLocal_BOWTIE_MISMATCHES --minins $ngsLocal_BOWTIE_MININS --maxins $ngsLocal_BOWTIE_MAXINS -a -m $ngsLocal_BOWTIE_MAXMULTI --best --sam -p $NUMCPU $BOWTIE_REPO/$SPECIES -1 $SAMPLE/$ngsLocal_BOWTIE_INP_DIR/unaligned_1.fq -2 $SAMPLE/$ngsLocal_BOWTIE_INP_DIR/unaligned_2.fq $SAMPLE/bowtie/output_p.sam --max $SAMPLE/bowtie/suppressed.sam --un $SAMPLE/bowtie/notMapped.fq > $SAMPLE/bowtie/$SAMPLE.stats.txt 2>&1
fi
# mate 1 as single-end reads
prnCmd "bowtie -t -v $ngsLocal_BOWTIE_MISMATCHES -a -m $ngsLocal_BOWTIE_MAXMULTI --best --sam -p $NUMCPU $BOWTIE_REPO/$SPECIES $SAMPLE/bowtie/notMapped_1.fq $SAMPLE/bowtie/SE_mapping/output_se1.sam --un $SAMPLE/bowtie/SE_mapping/notMapped_1.fq >> $SAMPLE/bowtie/$SAMPLE.stats.txt 2>&1"
if ! $DEBUG; then
bowtie -t -v $ngsLocal_BOWTIE_MISMATCHES -a -m $ngsLocal_BOWTIE_MAXMULTI --best --sam -p $NUMCPU $BOWTIE_REPO/$SPECIES $SAMPLE/bowtie/notMapped_1.fq $SAMPLE/bowtie/SE_mapping/output_se1.sam --un $SAMPLE/bowtie/SE_mapping/notMapped_1.fq >> $SAMPLE/bowtie/$SAMPLE.stats.txt 2>&1
fi
# mate 2 as single-end reads
prnCmd "bowtie -t -v $ngsLocal_BOWTIE_MISMATCHES -a -m $ngsLocal_BOWTIE_MAXMULTI --best --sam -p $NUMCPU $BOWTIE_REPO/$SPECIES $SAMPLE/bowtie/notMapped_2.fq $SAMPLE/bowtie/SE_mapping/output_se2.sam --un $SAMPLE/bowtie/SE_mapping/notMapped_2.fq >> $SAMPLE/bowtie/$SAMPLE.stats.txt 2>&1"
if ! $DEBUG; then
bowtie -t -v $ngsLocal_BOWTIE_MISMATCHES -a -m $ngsLocal_BOWTIE_MAXMULTI --best --sam -p $NUMCPU $BOWTIE_REPO/$SPECIES $SAMPLE/bowtie/notMapped_2.fq $SAMPLE/bowtie/SE_mapping/output_se2.sam --un $SAMPLE/bowtie/SE_mapping/notMapped_2.fq >> $SAMPLE/bowtie/$SAMPLE.stats.txt 2>&1
fi
fi
# need to save current directory so we can return here. We also
# need to adjust $JOURNAL so prnCmd() still works when we change
# directories
prnCmd "CUR_DIR=`pwd`"
CUR_DIR=`pwd`
prnCmd "JOURNAL_SAV=$JOURNAL"
JOURNAL_SAV=$JOURNAL
prnCmd "cd $SAMPLE/bowtie"
if ! $DEBUG; then
cd $SAMPLE/bowtie
JOURNAL=../../$JOURNAL
prnCmd "JOURNAL=../../$JOURNAL"
fi
# conver SAM output into a sorted BAM file.
prnCmd "samtools view -h -b -S -o output_p.bam output_p.sam"
if ! $DEBUG; then samtools view -h -b -S -o output_p.bam output_p.sam; fi
prnCmd "samtools sort output_p.bam $SAMPLE.bowtie.sorted"
if ! $DEBUG; then samtools sort output_p.bam $SAMPLE.bowtie.sorted; fi
prnCmd "samtools index $SAMPLE.bowtie.sorted.bam"
if ! $DEBUG; then samtools index $SAMPLE.bowtie.sorted.bam; fi
prnCmd "rm output_p.sam output_p.bam"
if ! $DEBUG; then rm output_p.sam output_p.bam; fi
# the suppressed file contains multimapping that were not included
# in the sorted output (ie too many multimappings for a single
# read). This file won't always exist, so we need to make sure it
# does exist before trying to convert it into a sorted BAM file.
if [[ -s $SAMPLE/bowtie/suppressed.sam ]]; then
prnCmd "samtools view -h -b -S -o suppressed.bam suppressed.sam"
if ! $DEBUG; then samtools view -h -b -S -o suppressed.bam suppressed.sam; fi
prnCmd "samtools sort suppressed.bam $SAMPLE.suppressed.sorted"
if ! $DEBUG; then samtools sort suppressed.bam $SAMPLE.suppressed.sorted; fi
prnCmd "samtools index $SAMPLE.suppressed.sorted.bam"
if ! $DEBUG; then samtools index $SAMPLE.suppressed.sorted.bam; fi
prnCmd "rm suppressed.sam suppressed.bam"
if ! $DEBUG; then rm suppressed.sam suppressed.bam; fi
fi
if ! $SE; then
# if paired-end then we need to also compress the aligning of
# the unpaired mates that were mapped.
prnCmd "samtools view -h -b -S -o SE_mapping/$SAMPLE.mate1.bam SE_mapping/output_se1.sam"
if ! $DEBUG; then samtools view -h -b -S -o SE_mapping/$SAMPLE.mate1.bam SE_mapping/output_se1.sam; fi
prnCmd "samtools view -h -b -S -o SE_mapping/$SAMPLE.mate2.bam SE_mapping/output_se2.sam"
if ! $DEBUG; then samtools view -h -b -S -o SE_mapping/$SAMPLE.mate2.bam SE_mapping/output_se2.sam; fi
prnCmd "rm SE_mapping/output_se1.sam SE_mapping/output_se2.sam"
if ! $DEBUG; then rm SE_mapping/output_se1.sam SE_mapping/output_se2.sam; fi
fi
# return to proper directory and restore $JOURNAL
prnCmd "cd $CUR_DIR"
if ! $DEBUG; then
cd $CUR_DIR
JOURNAL=$JOURNAL_SAV
prnCmd "JOURNAL=$JOURNAL_SAV"
fi
# run error checking
if ! $DEBUG; then ngsErrorChk_BOWTIE $@; fi
if $SE; then prnCmd "# FINISHED: BOWTIE SINGLE-END ALIGNMENT"
else prnCmd "# FINISHED: BOWTIE PAIRED-END ALIGNMENT"; fi
}
##########################################################################################
# ERROR CHECKING. Make sure output file exists and is not empty.
##########################################################################################
ngsErrorChk_BOWTIE() {
prnCmd "# BOWTIE ERROR CHECKING: RUNNING"
inputFile_1="$SAMPLE/$ngsLocal_BOWTIE_INP_DIR/unaligned_1.fq"
inputFile_2="$SAMPLE/$ngsLocal_BOWTIE_INP_DIR/unaligned_2.fq"
outputFile="$SAMPLE/bowtie/$SAMPLE.bowtie.sorted.bam"
# make sure expected output file exists
if [[ ! -s $outputFile ]]; then
errorMsg="Output file doesn't exist or is empty.\n"
errorMsg+="\tinput file: $inputFile_1\n"
if ! $SE; then errorMsg+="\tinput file: $inputFile_2\n"; fi
errorMsg+="\toutput file: $outputFile\n"
prnError "$errorMsg"
fi
prnCmd "# BOWTIE ERROR CHECKING: DONE"
}
ngsStats_BOWTIE() {
if [ $# -ne 1 ]; then
prnError "Incorrect number of parameters for ngsStats_STAR()."
fi
statsFile="$SAMPLE.stats.txt"
genome=`tail -n1 $SAMPLE/bowtie/$SAMPLE.versions | awk '{print($6)}'`
BOWTIE_HEADER="Genome"
BOWTIE_VALUES="$genome"
allNumReads=`grep "processed" $SAMPLE/bowtie/$SAMPLE.stats.txt | awk -F $' ' '{print $4}'`
PENumReads=`echo $allNumReads | awk -F $' ' '{if(NR==1){print $1}}'`
BOWTIE_HEADER="$BOWTIE_HEADER\tNumber of PE reads"
BOWTIE_VALUES="$BOWTIE_VALUES\t$PENumReads"
uniqMap=`grep "reported" $SAMPLE/bowtie/$SAMPLE.stats.txt | awk -F $' ' '{print $9}'`
uniqPEmap=`echo $uniqMap | awk -F $' ' '{if(NR==1){print $1}}'`
BOWTIE_HEADER="$BOWTIE_HEADER\tUniq PE Map"
BOWTIE_VALUES="$BOWTIE_VALUES\t$uniqPEmap"
uniqSEmap1=`echo $uniqMap | awk '{if(NR==2){print $1}}'`
uniqSEmap2=`echo $uniqMap | awk '{if(NR==3){print $1}}'`
uniqSEmap=$((uniqSEmap1+uniqSEmap2))
BOWTIE_HEADER="$BOWTIE_HEADER\tUniq SE Map"
BOWTIE_VALUES="$BOWTIE_VALUES\t$uniqSEmap"
percentMap=$(bc <<<"scale=5;($uniqPEmap+($uniqSEmap/2))/$PENumReads")
BOWTIE_HEADER="$BOWTIE_HEADER\tPercent Mapped"
BOWTIE_VALUES="$BOWTIE_VALUES\t$percentMap"
case $1 in
header)
# the second to the last line of the stats.txt file is a tab-delimited lists of headers
echo "$BOWTIE_HEADER"
;;
values)
# the last line of the stats.txt file is a tab-delimited lists of values
echo "$BOWTIE_VALUES"
;;
*)
# incorrect argument
prnError "Invalid parameter for ngsStats_BOWTIE() (got $1, expected: 'header|values')."
;;
esac
}