Optional SAM headers and writing to file

822 views
Skip to first unread message

Adam

unread,
Jun 7, 2011, 5:20:56 PM6/7/11
to Pysam User group
Great software and docs, thanks for your work.

I apologize if I missed this in the docs, but is there a way to tell a
pysam.Samfile to also write out the header when specified with the
template or header constructor arguments? Though the SAM spec says
the header lines are optional, Samfile seems to require it (which I
think is a reasonable thing to enforce). However, unless I'm missing
something, since the Samfile output doesn't include header info, pysam
can't parse the same SAM files it produces. I just installed
pysam-0.5 and the issue is the same as previous versions. Thanks.

Ben Schiller

unread,
Jun 7, 2011, 5:27:18 PM6/7/11
to pysam-us...@googlegroups.com
Use the template keyword argument in Samfile

Example (copies one samfile to a new BAM file):

original_file = pysam.Samfile('foo')
new_file = pysam.Samfile('foo.bam', mode='wb', template=orginal_file)
for read in original_file: new_file.write(read)

Ben

Adam

unread,
Jun 7, 2011, 6:52:02 PM6/7/11
to Pysam User group
Thanks for the reply, but my current situation requires SAM format,
not BAM. I tried the template and header keywords and a mode of 'w'
to produce SAM and there is no header. Right now I use a standard
python file object and write sam_file.text to it before writing new
lines. I may eventually convert my stuff to BAM but I'm under a time
deadline right now and the amount of work it would take is non-
trivial. Was just wondering if I wasn't using the software correctly
- are there plans to add header to SAM output?

Ben Schiller

unread,
Jun 7, 2011, 7:14:57 PM6/7/11
to pysam-us...@googlegroups.com
Hm that's not quite right

Use mode='wh' instead, that will work correctly ('h' is for header)

Do you want to submit a bug report so we don't loose track of this
one? <http://code.google.com/p/pysam/issues/list>

Thanks
Ben

Adam

unread,
Jun 7, 2011, 9:25:04 PM6/7/11
to Pysam User group
Yeah, the 'h' works great. I'll file a bug. Thanks.
Reply all
Reply to author
Forward
0 new messages