Output the results of samtools flagstat of 180 bam files in one text file
2
0
Entering edit mode
18 months ago
Hann ▴ 80

Hello,

I have 180 bam files that I wanna see the mapping rate. I am running a for loop in files, and using samtools flagstat to get the stats, I want to have the output results for all the files in one text file with the file name so it would be something like this:

>M883.rgroup.bam
1695365 + 0 secondary
0 + 0 supplementary
55252515 + 0 duplicates
88168368 + 0 mapped (100.00% : N/A)
86473003 + 0 paired in sequencing
79991062 + 0 properly paired (92.50% : N/A)
85979751 + 0 with itself and mate mapped
493252 + 0 singletons (0.57% : N/A)
5058236 + 0 with mate mapped to a different chr
5058236 + 0 with mate mapped to a different chr (mapQ>=5)
>L33r4.rgroup.bam
.......
.....etc


I am running a for loop as the following:

    for i in $(ls bam_files/*.bam); do temp=${i/.bam/.txt}; out=${temp/bam_files/stat}; samtools flagstat$i > out & done  But this give the output for each bam file in a text file, therefore I will have 180 text files Thanks for loop shell alignment genome • 631 views ADD COMMENT 1 Entering edit mode 18 months ago ( for i in bam_files/*.bam ; do samtools flagstati ; done) > out.txt

0
Entering edit mode
18 months ago
Hann ▴ 80

One option is to concatenate all files together after the for loop finishes.

grep "" *.txt > all_files


This won't give a 'header', but it prefixes every line with the filename.