Pipe pysam.view() output to pysam.sort()
1
1
Entering edit mode
22 months ago
Macspider ★ 3.4k

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")
pysam sam alignment python sort • 605 views
ADD COMMENT
0
Entering edit mode

Might be easier to use subprocess with PIPEs and call your system samtools, but that's not very clean maybe

ADD REPLY
0
Entering edit mode

That's what I'm already doing, but I am trying to get rid of :)

ADD REPLY
0
Entering edit mode
22 months ago
gb ★ 2.0k

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 =)

ADD COMMENT

Login before adding your answer.

Traffic: 1440 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6