Tuesday, September 22, 2009

Simple pysam example

This python code uses pysam to parse a bam file and print out all the alignment contained therein.

import pysam

# Open our sam file
samfile = pysam.Samfile()
samfile.open( "sim.bam", "rb" )

# Grab an alignment from the sam file

def m_fetch_callback( alignment ):
print str(alignment)

num_targets = samfile.getNumTargets()

for n in xrange(num_targets):
samfile.fetch(samfile.getTarget(n),m_fetch_callback)


No comments: