-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapStat.sh
executable file
·342 lines (299 loc) · 17.9 KB
/
mapStat.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
#PBS -l nodes=1:ppn=4
declare -A GENOME
#### usage ####
usage() {
echo Program: "mapStat (tabulate mapping statistics)"
echo Author: BRIC, University of Copenhagen, Denmark
echo Version: 1.0
echo Contact: [email protected]
echo "Usage: mapStat.sh -s <file>"
echo " -s <file> [input mapStat file from map[S|P]EReads.sh (required)]"
echo "[Options]"
echo " -m <file> [input BAM file containing mapped reads (optional)]"
echo " [will be used to compute additional mapping statistics]"
#echo " -q <file> [input FASTQ (raw reads) file (optional)]"
#echo " -f <file> [input FASTQ (clipped read) file (optional)]"
echo " -N [also compute signal to noise ratio]"
echo " -S [compute mapping statistics for STAR alignment results]"
echo " -h [help]"
echo "[NOTE]"
echo " [mapping statistics are computed using:]"
echo " [1. bowtie2 (single-end) -> id; #reads (for mapping); #reads (unpaired); #reads (unmapped); #reads (aligned 1 time); #reads (aligned >1 time); alignment rate]"
echo " [ OR ]"
echo " [1. bowtie2 (paired-end) -> id; #reads (for mapping); #reads (paired); #reads (unmapped); #reads (aligned 1 time); #reads (aligned >1 time); alignment rate]"
echo " [2. samtools idxstats -> mm9_mapped (%proper pairs); dm6_mapped (%proper pairs) (spike-in)]"
echo " [3. samtools flagstat -> #reads (QC-passed); #reads (mapped); #reads (paired); #reads (singleton); #reads (PCR duplicates)]"
echo " [4. bam2spikeInScale -> spikeInScale]"
echo
exit 0
}
MAPPING_FREQUENCY=1
#### parse options ####
while getopts s:m:q:f:NSh ARG; do
case "$ARG" in
s) MAPSTATFILE=$OPTARG;;
m) BAMFILE=$OPTARG;;
q) RAWFASTQFILE=$OPTARG;;
f) CLIPPEDFASTQFILE=$OPTARG;;
N) SNR=1;;
S) STAR=1;;
h) HELP=1;;
esac
done
## usage, if necessary file and directories are given/exist
if [ ! -f "$MAPSTATFILE" -o "$HELP" ]; then
usage
fi
## create index of input bam file, if does not exist
if [ ! -z "$BAMFILE" ]; then
if [ ! -f "$BAMFILE.bai" ]; then
samtools index $BAMFILE
fi
BAMID=$(echo $BAMFILE | sed 's/\.bam$//g')
## determine reference and spike-in genome
if [ "$(samtools idxstats $BAMFILE 2>/dev/null | cut -f 1 | grep "_" | cut -f 2 -d "_" | sort | uniq | wc -l)" -eq 2 ]; then
GENOME['ref']=$(samtools idxstats $BAMFILE | perl -ane 'if($F[0]!~/\*/) { $F[0]=~s/^.*\_//g; $seen{$F[0]}+=$F[2]; } END { foreach(keys(%seen)) { print "$_\t$seen{$_}\n"; } }' | sort -k 2rn,2 | head -n 1 | tail -n 1 | cut -f 1);
GENOME['spike']=$(samtools idxstats $BAMFILE | perl -ane 'if($F[0]!~/\*/) { $F[0]=~s/^.*\_//g; $seen{$F[0]}+=$F[2]; } END { foreach(keys(%seen)) { print "$_\t$seen{$_}\n"; } }' | sort -k 2rn,2 | head -n 2 | tail -n 1 | cut -f 1);
elif [ "$(samtools idxstats $BAMFILE 2>/dev/null | cut -f 1 | grep "_" | cut -f 2 -d "_" | sort | uniq | wc -l)" -eq 1 ]; then
GENOME['ref']=$(samtools idxstats $BAMFILE | perl -ane 'if($F[0]!~/\*/) { $F[0]=~s/^.*\_//g; $seen{$F[0]}+=$F[2]; } END { foreach(keys(%seen)) { print "$_\t$seen{$_}\n"; } }' | sort -k 2rn,2 | head -n 1 | tail -n 1 | cut -f 1);
fi
fi
## bowtie2-based results
if [ -z "$STAR" ]; then
## compute mapping statistics for single-end data
if [ "$(zless $MAPSTATFILE| grep -w "paired" | wc -l)" -eq 0 ]; then
## count total number of raw reads
RAW_READS_COUNT="NA"
if [ ! -z "$RAWFASTQFILE" -a -f "$RAWFASTQFILE" ]; then
RAW_READS_COUNT=$(zless $RAWFASTQFILE | grep "@" | wc -l)
fi
## count total number of reads left after quality check
CLIPPED_READS_COUNT="NA"
PER="NA"
if [ ! -z "$CLIPPED_READS_COUNT" -a -f "$CLIPPEDFASTQFILE" ]; then
CLIPPED_READS_COUNT=$(zless $CLIPPEDFASTQFILE | grep "@" | wc -l)
if [ ! -z "$RAWFASTQFILE" -a -f "$RAWFASTQFILE" ]; then
PER=$(echo $CLIPPED_READS_COUNT | perl -ane 'chomp($_); $per=($_*100)/'$RAW_READS_COUNT'; printf("%0.2f%", $per);')
fi
fi
## determine unique id
ID=$(zless $MAPSTATFILE | grep "Map for" | sed 's/Map for //g' | sed 's/\..*//g')
##----------------------------------------##
## tabulate mapping statistics (HEADER - SINGLE-END)
##----------------------------------------##
#echo -ne "id\t#reads (raw)\t#reads (after qualityCheck)\t#reads (for mapping)\t#reads (unpaired)\t#reads (unmapped)\t#reads (aligned 1 time)\t#reads (aligned >1 time)\talignment rate"
echo -ne "id\t#reads (for mapping)\t#reads (unpaired)\t#reads (unmapped)\t#reads (aligned 1 time)\t#reads (aligned >1 time)\talignment rate"
if [ ! -z "$BAMFILE" ]; then
if [ "${#GENOME[@]}" -eq 2 ]; then
echo -ne "\t${GENOME['ref']}_mapped\t${GENOME['spike']}_mapped"
elif [ "${#GENOME[@]}" -eq 1 ]; then
echo -ne "\t${GENOME['ref']}_mapped"
else
echo -ne "\tmapped"
fi
echo -ne "\t#reads (QC-passed)\t#reads (mapped)\t#reads (PCR duplicates)"
## add signal to noise ratio
if [ "$SNR" ]; then
echo -ne "\tSNR"
fi
if [ "$(ls $ID* | grep "_dupRemoved_" | grep "bam$" | wc -l)" -gt 0 ]; then
echo $(ls $ID* | grep "_dupRemoved_" | grep "bam$") | perl -ane 'foreach(@F) { $_=~s/^.*\_//g; $_=~s/\..*//g; print "\t$_ (final mapped, dupRemoved)"; }'
BAMID="${BAMID}_dupRemoved"
elif [ "$(ls $ID* | grep "_dupRemoved" | grep "bam$" | wc -l)" -gt 0 ]; then
echo $(ls $ID* | grep "_dupRemoved" | grep "bam$") | perl -ane 'foreach(@F) { $_=~s/^.*\_//g; $_=~s/\..*//g; print "\t$_ (final mapped, dupRemoved)"; }'
BAMID="${BAMID}_dupRemoved"
fi
fi
## add spike-in scale
if [ "${#GENOME[@]}" -eq 2 ]; then
echo -ne "\tspikeInScale"
fi
echo
echo -ne "$ID"
##----------------------------------------##
## tabulate mapping statistics (VALUES - SINGLE-END)
##----------------------------------------##
#zless $MAPSTATFILE | perl -ane 'BEGIN { print "\t'$RAW_READS_COUNT'\t'$CLIPPED_READS_COUNT' ('$PER')"; } if($_=~/^[0-9\s]+/) { $_=~s/\;.*//g; chomp($_); $_=~s/\s+[a-zA-Z]+.*//g; print "\t$_"; }'
zless $MAPSTATFILE | perl -ane 'BEGIN { print "\t"; } if($_=~/^[0-9\s]+/) { $_=~s/\;.*//g; chomp($_); $_=~s/\s+[a-zA-Z]+.*//g; print "\t$_"; }'
TOTAL_READS=$(zless $MAPSTATFILE | grep "reads; of these:" | cut -f 1 -d " ")
if [ ! -z "$BAMFILE" ]; then
if [ "${#GENOME[@]}" -eq 2 ]; then
samtools idxstats $BAMFILE 2>/dev/null | perl -ane '
if($F[0]!~/\*/) {
$F[0]=~s/^.*\_//g; $count{$F[0]}+=$F[2];
}
END {
print "\t$count{'${GENOME[ref]}'}\t$count{'${GENOME[spike]}'}";
}'
elif [ "${#GENOME[@]}" -eq 1 ]; then
samtools idxstats $BAMFILE 2>/dev/null | perl -ane '
if($F[0]!~/\*/) {
$F[0]=~s/^.*\_//g; $count{$F[0]}+=$F[2];
}
END {
print "\t$count{'${GENOME[ref]}'}";
}'
else
samtools idxstats $BAMFILE 2>/dev/null | perl -ane '
if($F[0]!~/\*/) {
$count+=$F[2];
}
END {
print "\t$count";
}'
fi
samtools flagstat $BAMFILE | perl -ane '
if($_=~/QC-passed/) { $_=~s/\s+.*//g; $qcPassed_reads=$_; }
elsif($_=~/mapped\s+\(/) { $_=~s/\s+.*//g; $mapped_reads=$_; }
elsif($_=~/duplicates/) { $_=~s/\s+.*//g; $duplicate_reads=$_; }
END {
printf("\t%s\t%s (%0.2f)\t%s (%0.2f)", $qcPassed_reads, $mapped_reads, ($mapped_reads*100)/$qcPassed_reads, $duplicate_reads, ($duplicate_reads*100)/$qcPassed_reads);
}'
## compute signal to noise ratio
if [ "${#GENOME[@]}" -eq 2 -a "$SNR" ]; then
SNRatio=$(bam2signalVsNoise -i ${BAMID}_${GENOME['ref']}.bam -g ${GENOME['ref']} | cut -f 3)
echo -ne "\t$SNRatio"
elif [ "${#GENOME[@]}" -eq 1 -a "$SNR" ]; then
SNRatio=$(bam2signalVsNoise -i ${BAMFILE} -g ${GENOME['ref']} | cut -f 3)
echo -ne "\t$SNRatio"
elif [ "$SNR" ]; then
SNRatio=$(bam2signalVsNoise -i ${BAMFILE} -g ${GENOME['ref']} | cut -f 3)
echo -ne "\t$SNRatio"
fi
if [ "$(ls $BAMID* | grep "_dupRemoved_" | grep "bam$" | wc -l)" -gt 0 ]; then
for i in $(ls $BAMID* | grep "_dupRemoved_" | grep "bam$"); do
samtools flagstat $i | perl -ane 'if($_=~/mapped\s+\(/) { $_=~s/\s+.*//g; print "\t$_"; } '
done
elif [ "$(ls $BAMID* | grep "_dupRemoved" | grep "bam$" | wc -l)" -gt 0 ]; then
for i in $(ls $BAMID* | grep "_dupRemoved" | grep "bam$"); do
samtools flagstat $i | perl -ane 'if($_=~/mapped\s+\(/) { $_=~s/\s+.*//g; print "\t$_"; } '
done
fi
#samtools view -f 4 $BAMFILE | cut -f 10 | sort | uniq -c | sort -nr > $ID.unmapped;
fi
## add spike-in scale
if [ "${#GENOME[@]}" -eq 2 ]; then
echo -ne "\t$(bam2spikeInScale -i ${BAMID}_${GENOME[spike]}.bam)"
fi
echo
else
## determine unique id
ID=$(zless $MAPSTATFILE | grep "Map for" | sed 's/Map for //g' | sed 's/\..*//g')
##----------------------------------------##
## tabulate mapping statistics (HEADER - PAIRED-END)
##----------------------------------------##
echo -ne "id\t#reads (for mapping)\t#reads (paired)\t#reads (unmapped)\t#reads (aligned 1 time)\t#reads (aligned >1 time)\talignment rate"
if [ ! -z "$BAMFILE" ]; then
if [ "${#GENOME[@]}" -eq 2 ]; then
echo -ne "\t${GENOME['ref']}_mapped (proper_pairs)\t${GENOME['spike']}_mapped (proper_pairs)"
elif [ "${#GENOME[@]}" -eq 1 ]; then
echo -ne "\t${GENOME['ref']}_mapped (proper_pairs)"
else
echo -ne "\tmapped (proper_pairs)"
fi
echo -ne "\t#reads (QC-passed)\t#reads (mapped)\t#reads (paired)\t#reads (singleton)\t#reads (PCR duplicates)"
## add signal to noise ratio
if [ "$SNR" ]; then
echo -ne "\tSNR"
fi
if [ "$(ls $BAMID* | grep "_dupRemoved_" | grep "bam$" | wc -l)" -gt 0 ]; then
echo $(ls $BAMID* | grep "_dupRemoved_" | grep "bam$") | perl -ane 'foreach(@F) { $_=~s/^.*\_//g; $_=~s/\..*//g; print "\t$_ (final mapped, dupRemoved)"; }'
BAMID="${BAMID}_dupRemoved"
elif [ "$(ls $BAMID* | grep "_dupRemoved" | grep "bam$" | wc -l)" -gt 0 ]; then
echo $(ls $BAMID* | grep "_dupRemoved" | grep "bam$") | perl -ane 'foreach(@F) { print "\tgenome (final mapped, dupRemoved)"; }'
BAMID="${BAMID}_dupRemoved"
fi
fi
## add spike-in scale
if [ "${#GENOME[@]}" -eq 2 ]; then
echo -ne "\tspikeInScale"
fi
echo
echo -ne "$ID"
##----------------------------------------##
## tabulate mapping statistics (VALUES - PAIRED-END)
##----------------------------------------##
cat <(head -n 7 $MAPSTATFILE) <(grep "overall alignment rate" $MAPSTATFILE) | perl -ane 'if($_=~/\----/) { last; } if($_=~/^[0-9\s]+/) { $_=~s/\;.*//g; chomp($_); $_=~s/\s+[a-zA-Z]+.*//g; print "\t$_"; }'
TOTAL_READS=$(zless $MAPSTATFILE | grep "reads; of these:" | cut -f 1 -d " ")
if [ ! -z "$BAMFILE" ]; then
if [ "${#GENOME[@]}" -eq 2 ]; then
PER_REF=$(samtools flagstat ${BAMID}_${GENOME['ref']}.bam | perl -ane 'if($_=~/mapped \(/) { $_=~s/\s+.*//g; $mapped=$_/2; } elsif($_=~/properly paired/) { $_=~s/\s+.*//g; $properly_paired=$_/2; } END { $per=sprintf("%0.2f", ($properly_paired*100)/$mapped); print "$properly_paired\t$mapped\t$per\n"; }' | cut -f 3)
PER_SPIKE=$(samtools flagstat ${BAMID}_${GENOME['spike']}.bam | perl -ane 'if($_=~/mapped \(/) { $_=~s/\s+.*//g; $mapped=$_/2; } elsif($_=~/properly paired/) { $_=~s/\s+.*//g; $properly_paired=$_/2; } END { $per=sprintf("%0.2f", ($properly_paired*100)/$mapped); print "$properly_paired\t$mapped\t$per\n"; }' | cut -f 3)
samtools idxstats $BAMFILE 2>/dev/null | perl -ane '
if($F[0]!~/\*/) {
$F[0]=~s/^.*\_//g; $count{$F[0]}+=$F[2];
}
END {
printf("\t%0.0f (%s)\t%0.0f (%s)", $count{'${GENOME[ref]}'}/2, '${PER_REF}', $count{'${GENOME[spike]}'}/2, '${PER_SPIKE}');
}'
elif [ "${#GENOME[@]}" -eq 1 ]; then
PER_REF=$(samtools flagstat ${BAMID}_${GENOME['ref']}.bam | perl -ane 'if($_=~/mapped \(/) { $_=~s/\s+.*//g; $mapped=$_/2; } elsif($_=~/properly paired/) { $_=~s/\s+.*//g; $properly_paired=$_/2; } END { $per=sprintf("%0.2f", ($properly_paired*100)/$mapped); print "$properly_paired\t$mapped\t$per\n"; }' | cut -f 3)
samtools idxstats $BAMFILE 2>/dev/null | perl -ane '
if($F[0]!~/\*/) {
$F[0]=~s/^.*\_//g; $count{$F[0]}+=$F[2];
}
END {
printf("\t%0.0f (%s)", $count{'${GENOME[ref]}'}/2, '${PER_REF}');
}'
else
PER_REF=$(samtools flagstat ${BAMFILE} | perl -ane 'if($_=~/mapped \(/) { $_=~s/\s+.*//g; $mapped=$_/2; } elsif($_=~/properly paired/) { $_=~s/\s+.*//g; $properly_paired=$_/2; } END { $per=sprintf("%0.2f", ($properly_paired*100)/$mapped); print "$properly_paired\t$mapped\t$per\n"; }' | cut -f 3)
samtools idxstats $BAMFILE 2>/dev/null | perl -ane '
if($F[0]!~/\*/) {
$count+=$F[2];
}
END {
printf("\t%0.0f (%s)", $count/2, '${PER_REF}');
}'
fi
samtools flagstat $BAMFILE | perl -ane '
if($_=~/QC-passed/) { $_=~s/\s+.*//g; $qcPassed_reads=sprintf("%0.0f", $_/2); }
elsif($_=~/paired in sequencing/) { $_=~s/\s+.*//g; $mapped_reads=sprintf("%0.0f", $_/2); }
elsif($_=~/properly paired/) { $_=~s/\s+.*//g; $paired_reads=sprintf("%0.0f", $_/2); }
elsif($_=~/singletons/) { $_=~s/\s+.*//g; $singleton_reads=sprintf("%0.0f", $_/2); }
elsif($_=~/duplicates/) { $_=~s/\s+.*//g; $duplicate_reads=sprintf("%0.0f", $_/2); }
END {
printf("\t%s\t%s (%0.2f)\t%s (%0.2f)\t%s (%0.2f)\t %s (%0.2f)", $qcPassed_reads, $mapped_reads, ($mapped_reads*100)/$qcPassed_reads, $paired_reads, ($paired_reads*100)/$qcPassed_reads, $singleton_reads, ($singleton_reads*100)/$qcPassed_reads, $duplicate_reads, ($duplicate_reads*100)/$qcPassed_reads);
}'
## compute signal to noise ratio
if [ "${#GENOME[@]}" -eq 2 -a "$SNR" ]; then
SNRatio=$(bam2signalVsNoise -i ${BAMID}_${GENOME['ref']}.bam -g ${GENOME['ref']} -P | cut -f 3)
echo -ne "\t$SNRatio"
elif [ "${#GENOME[@]}" -eq 1 -a "$SNR" ]; then
SNRatio=$(bam2signalVsNoise -i ${BAMFILE} -g ${GENOME['ref']} -P | cut -f 3)
echo -ne "\t$SNRatio"
elif [ "$SNR" ]; then
GN=$(grep bowtie2 ${MAPSTATFILE} | sed -E 's/\/bowtie2.*//g' | sed -E 's/^.*\///g')
SNRatio=$(bam2signalVsNoise -i ${BAMFILE} -g ${GN} -P | cut -f 3)
echo -ne "\t$SNRatio"
fi
if [ "$(ls $BAMID* | grep "_dupRemoved_" | grep "bam$" | wc -l)" -gt 0 ]; then
for i in $(ls $BAMID* | grep "_dupRemoved_" | grep "bam$"); do
samtools flagstat $i | perl -ane 'if($_=~/properly paired/) { $_=~s/\s+.*//g; printf("\t%0.0f", $_/2); } '
done
elif [ "$(ls $BAMID* | grep "_dupRemoved" | grep "bam$" | wc -l)" -gt 0 ]; then
for i in $(ls $BAMID* | grep "_dupRemoved" | grep "bam$"); do
samtools flagstat $i | perl -ane 'if($_=~/properly paired/) { $_=~s/\s+.*//g; printf("\t%0.0f", $_/2); } '
done
fi
fi
## add spike-in scale
if [ "${#GENOME[@]}" -eq 2 ]; then
echo -ne "\t$(bam2spikeInScale -i ${BAMID}_${GENOME[spike]}.bam)"
fi
echo
fi
else
## determine unique id
ID=$(zless $MAPSTATFILE | grep "Map for" | sed 's/Map for //g' | sed 's/\..*//g')
## tabulate mapping statistics (header)
echo -e "id\t#reads (raw)\t#reads (aligned 1 time)\t#reads (aligned >1 time)"
## tabulate mapping statistics (values)
TOTAL_READS=$(zless $MAPSTATFILE | grep "Number of input reads" | perl -ane 'print $F[scalar(@F)-1]."\n";')
UNIQUELY_MAPPED=$(zless $MAPSTATFILE | grep "Uniquely mapped reads number" | perl -ane 'print $F[scalar(@F)-1]."\n";')
UNIQUELY_MAPPED_PER=$(zless $MAPSTATFILE | grep "Uniquely mapped reads %" | perl -ane 'print $F[scalar(@F)-1]."\n";')
MULTI_MAPPED=$(zless $MAPSTATFILE | grep 'Number of reads mapped to multiple loci\|Number of reads mapped to too many loci' | perl -ane '$sum+=$F[scalar(@F)-1]; END { print "$sum\n"; }')
MULTI_MAPPED_PER=$(zless $MAPSTATFILE | grep '% of reads mapped to multiple loci\|% of reads mapped to too many loci' | perl -ane '$per=$F[scalar(@F)-1]; $per=~s/\%//g; $sum+=$per; END { print "$sum%\n"; }')
echo -e "$ID\t$TOTAL_READS\t$UNIQUELY_MAPPED ($UNIQUELY_MAPPED_PER)\t$MULTI_MAPPED ($MULTI_MAPPED_PER)"
fi
exit