Java Samtools Api: Write Bam To Stdout
1
0
Entering edit mode
10.6 years ago

Hello,

I'm trying to use the Java API for picard/samtools to write a BAM file to standard output.

With the code below I can write SAM to stdout. However, I would like to write directly as BAM to stdout. Is it possible?

SAMFileWriterFactory sf= new SAMFileWriterFactory();
SAMFileWriter sw= sf.makeSAMWriter(mySamFileHeader, false, System.out);
sw.addAlignment(mySamRecord);

Many thanks

Dario

java picard samtools • 2.9k views
ADD COMMENT
0
Entering edit mode
10.6 years ago

try to use:

makeBAMWriter(mySamFileHeader,false,new File("/dev/stdout"));

or try to create a BAMFileWriter with file=null (not tested, but it should work (?))

   return new  BAMFileWriter(System.out, (File)null)
ADD COMMENT
0
Entering edit mode

Thanks Pierre! Your first option would work but I'm a bit reluctant to hard-coding "/dev/stdout". Your second option however put me in the right direction. This seems to work:

SAMFileHeader samFileHeader= sam.getFileHeader();                
SAMFileWriterFactory sf= new SAMFileWriterFactory();
BAMFileWriter sw= new BAMFileWriter((OutputStream)System.out, (File)null);
sw.setHeader(samFileHeader);
sw.addAlignment(rec);
ADD REPLY

Login before adding your answer.

Traffic: 1009 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