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")
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 :)