This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Get unmapped reads from cram/bam file

I am extracting unmapped reads from a cram file using the command:

   samtools  view -b -f 4 in.cram > out.cram

However, I wonder if there is a faster way of doing this. Either another program or using more threads.

alignment
   samtools  view --threads 10 --reference /path/to/reference.fa -O CRAM -f 4 in.cram > out.cram

1 answer

-@ parameter for more threads, beyond that probably no, samtools is top of the pops for these kinds of things. Be sure that the reference file you used for CRAM generation is available or explicitely specify it with -T as suggested by Pierre Lindenbaum

CRAM files rely on the reference file to decode the sequences.

Be sure that the reference file you used for CRAM generation is available

well samtools is able to find the reference if it can find it in the SAM header or if some env variables are set. ${REF_PATH} ....

Yes, that is what I mean. The file must be available on the machine/network you work on/in. If it is then samtools may even find it implicitely.

But I don't need the reference file to extract the unmapped reads using my command?

Don't you? I mean, technically (from my understanding) the unmapped reads are not compressed against the reference, but doesn't samtools throw an error if there is no reference for a CRAM that contains at least some mapped reads? Not sure, maybe you're right and you do not need it at all. As said samtools can implicitely search for the reference on your disk so maybe it uses it without that you note it.

I have a crai file for each cram file is this enough, or do I need the references as well? I did not get an error, but its running for several hours for a 100GB file and so far the unmapped reads are about 800MB.

do I need the reference when I have the crai-file? It seems to run without references with my original command

do I need the reference when I have the crai-file? It seems to run without references with my original command

yes, unless samtools is able to find the REF by itself.

Why is it then running without specifying it? What will the output be?

its been running for 24h using a 100GB cram as input. Not sure if its supposed to take that long. Now it has created a 1GB unmapped file. But don't finished.

instead of redirecting it to a CRAM file, just keep the SAM format and pipe the output in more

Log in to answer this question.