-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path09-bamstats.sh
executable file
·86 lines (68 loc) · 2.85 KB
/
09-bamstats.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
#!/bin/bash
CAPTURE_KIT=$RESOURCES_PATH/panel/Agilent_SureSelect_All_Exon_V2.bed
BAM_PATH=$HOME_PATH/bam
REPORT=$HOME_PATH/reports/finalbamstats.txt
mkdir $HOME_PATH/reports
printf "%s\t%s\t%s\t%s\t%s\t%s%s\t%s\t%s\t%s\t%s\t%s\t%s\n" "name" \
"total reads" "total reads pairs" "aligned reads" \
"properly paired aligned pairs" "uniquely aligned reads (q>20)" \
"properly paired uniquely aligned reads" "chimeric reads" \
"reads overlapping targets" "total bases" "aligned bases" \
"uniquely aligned bases" "bases overlapping targets" > $REPORT
for FILE in `ls $BAM_PATH/*_fixmate.bam`
do
SAMPLE=`basename $FILE | sed s/_fixmate\.bam//`
echo "Processing $SAMPLE"
BAM=$BAM_PATH/$SAMPLE".bam"
printf "%s\t" $SAMPLE >> $REPORT
echo " total reads"
printf "%d\t" `$SAMTOOLS_PATH/samtools view -c -F2048 $BAM` >> $REPORT
echo " total read pairs"
printf "%d\t" `$SAMTOOLS_PATH/samtools view -c -F2048 $BAM | awk '{print $1/2}'` \
>> $REPORT
echo " aligned reads"
printf "%d\t" `$SAMTOOLS_PATH/samtools view -c -F2052 $BAM` >> $REPORT
echo " properly paired aligned pairs"
printf "%d\t" `$SAMTOOLS_PATH/samtools view -c -f66 -F2048 $BAM` \
>> $REPORT
echo " uniquely aligned reads (q>20)"
printf "%d\t" `$SAMTOOLS_PATH/samtools view -c -F2052 -q20 $BAM` >> \
$REPORT
echo " properly paired uniquely aligned reads"
printf "%d\t" `$SAMTOOLS_PATH/samtools view -c -f66 -F2048 -q20 $BAM` \
>> $REPORT
echo " chimeric reads"
printf "%d\t" `
$SAMTOOLS_PATH/samtools flagstat $BAM | \
perl -e 'my @in;' \
-e 'while(<>) { chomp $_; push(@in,$_); }' \
-e 'my @tmp = split("\\\+",pop(@in));' \
-e '$tmp[0] =~ s/\s+$//;' \
-e 'print STDOUT $tmp[0];'
` >> $REPORT
echo " reads overlapping targets"
printf "%d\t" `
$BEDTOOLS_PATH/bedtools intersect -a $CAPTURE_KIT -b $BAM -c | \
awk 'BEGIN {tot=0}{tot+=$4} END {print tot}'
` >> $REPORT
echo " total bases"
printf "%d\t" `
$SAMTOOLS_PATH/samtools view $BAM | cut -f10 | \
awk 'BEGIN {tr=0}{tr+=length($0)} END {print tr}'
` >> $REPORT
echo " aligned bases"
printf "%d\t" `
$SAMTOOLS_PATH/samtools view -F2052 $BAM | cut -f10 | \
awk 'BEGIN {tr=0}{tr+=length($0)} END {print tr}'
` >> $REPORT
echo " uniquely aligned bases"
printf "%d\t" `
$SAMTOOLS_PATH/samtools view -F2052 -q20 $BAM | cut -f10 | \
awk 'BEGIN {tr=0}{tr+=length($0)} END {print tr}'
` >> $REPORT
echo " bases overlapping targets"
printf "%d\n" `
$BEDTOOLS_PATH/bedtools coverage -a $CAPTURE_KIT -b $BAM -d | \
awk 'BEGIN {tr=0} {tr+=$5} END {print tr}'
` >> $REPORT
done