This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Output the results of samtools flagstat of 180 bam files in one text file

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
88168368 + 0 in total (QC-passed reads + QC-failed reads)
1695365 + 0 secondary
0 + 0 supplementary
55252515 + 0 duplicates
88168368 + 0 mapped (100.00% : N/A)
86473003 + 0 paired in sequencing
43227447 + 0 read1
43245556 + 0 read2
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

2 answers

( for i in bam_files/*.bam ; do samtools flagstat $i ; done) > out.txt

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.

Log in to answer this question.