This is a test version of Biostars. For the public version, visit https://www.biostars.org.
samtools mpileup for multiple regions output in different files

I am using samtools mpileup over different regions:

samtools mpileup -Q 0 -l regions.bed reads.sort.bam > counts.txt

I would like to output for each region (in regions.bed) a separated file, so that results from different regions will be in different output files. Is there any possibility to do so without running a samtools command for each region separately?

Or is it possible to output results in different files for different .bam files without again running a loop for each file?

samtools mpileup -Q 0 -l chr1:1222454-1342444 reads1.sort.bam reads2.sort.bam > counts.txt​
sequencing

1 answer

For the first method (1 BAM file, multiple regions), you can probably pipe the output to bedtools intersect (you'll probably need to munge things with awk) and then pipe that to awk, where you could reformat the region columns into a file name and redirect to it (something like ... | awk 'BEGIN{OFS="\t"}{ofile=some_manipulation; print $1,$2,$3 > ofile}').

For the second method (multiple BAM files, one region), you would just pipe to awk and write the appropriate columns to different files. The only downside to this is that you probably need to tell awk the region, so it can write everything to a new file.

Personally, I'd just write a little script using pysam to do this, but to each his/her own.

Log in to answer this question.