This is a test version of Biostars. For the public version, visit https://www.biostars.org.
remove unmapped reads on a specific chromosome only

I looked around and couldn't find an example that meets my needs. I have a bam file with all the hg19 chromosomes and I want to remove the unmapped reads that are located at chrM specifically rather than an entire bam operation. I can think of splitting the bam into chromosomes and treating individually but is there a way to do this without extra steps? It is samtools view -F 4 or samtools view -F 12 ONLY for chrM yet the input is file is still the entire bam file and the output file is still the entire bam file with only the unwanted reads on chrM filtered out.

Thanks

bam remove unmapped

In technical terms @prasundutta87's answer below should would. But your question is a little odd. If the reads were unmapped, how would the be on chrM? By definition an unmapped read is not on any chromosome.

i understand what you mean. in my case i have some reads whose mates are unmapped and they are causing some issues, they are only on chrM. therefore i want to get rid of all the reads with either mate unmapped on chrM only without touching the other chrs.

therefore i want to get rid of all the reads with either mate unmapped on chrM only

It would be good to add this clarification to original post.

Reads that are mapped, but whose mate is unmapped are mapped, and therefore will not be caught by -F 4, the flag you want is 8, not 4. The unmapped reads themselves (which are the mates to the reads mapped to chrM) won't be located on any chromosome, so any command that only removes unmapped reads on chrM won't remove them.

As far as i'm aware the only what to do this would be to divide the BAM file in two (reads on chrM, and reads not on chrM) and then filter only those on chrM, before recombining.

1 answer

You can specify the region of the BAM file you want in your samtools view command. In your case, it should be your chromosome name (without start and end position information). You can find the information in the manual/help file of the command (http://www.htslib.org/doc/samtools-view.html):

REGIONS: Regions can be specified as: RNAME[:STARTPOS[-ENDPOS]] and all position coordinates are 1-based

You would of course need to index your BAM file first.

could you give an example? when I do samtools view -F 12 my.bam chrM this will give me only chrM but not the other chromosomes if this is what you meant.

Log in to answer this question.