Hi everyone,
I am trying to pipe pysam.view() into pysam.sort() in a program. Since both are wrappers for samtools, it is quite easy to read a bam file and generate a new (filtered or sorted) bam file from them.
My aim, however, is to filter reads by certain criteria and pipe the filtered reads output directly to the sorting function without passing through an intermediate filtered file. This is because the bam file intermediate would take a lot of space on our hard drive, while RAM is not a problem.
Is there a way to do so?
My code at the moment:
import pysam
infile = "test.bam"
outfile = "test.f.bam"
open(outfile, "w").close()
pysam.view("-F", "0x0100", "-F", "0x4", "-b", "-h", "-o", outfile, infile, catch_stdout=False)
pysam.sort("-n", "-m", "4G", "-@", "20", "-T", "test", "--output-fmt", "bam", "-o", "test.fs.bam", "test.f.bam")
1 answer
I would say it is not possible
http://samtools.sourceforge.net/pipe.shtml
in the examples above, we only see how to combine the 'view' and 'pileup' commands. In fact, most of samtools commands, except indexing and external sorting, recognize an input file '-' as stdin and an output file '-' as stdout.
Also I can imagine that for sorting you need to have all the data, otherwise the program need to sort every time again if new information enters the sorting function(?)
EDIT:
Maybe this is an option? (working with file objects)
https://stackoverflow.com/questions/18550127/how-to-do-virtual-file-processing/18550652
https://stackoverflow.com/questions/44672524/how-to-create-in-memory-file-object
EDIT: If this should be a comment let me know, still getting familiar with this forum =)
Log in to answer this question.
Might be easier to use subprocess with PIPEs and call your system samtools, but that's not very clean maybe
That's what I'm already doing, but I am trying to get rid of :)