Skip to content

Instantly share code, notes, and snippets.

@seandavi
Created September 12, 2011 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seandavi/1212196 to your computer and use it in GitHub Desktop.
Save seandavi/1212196 to your computer and use it in GitHub Desktop.
Given an bam file, find all potential introns, optionally within a given region.
#!/usr/bin/env python
import pysam
import argparse
import collections
parser = argparse.ArgumentParser()
parser.add_argument('bamfile',
help="bamfile for analysis")
parser.add_argument('-r','--region',
help="Limit to region chrN:XXX-YYY")
opts=parser.parse_args()
samfile = pysam.Samfile(opts.bamfile)
reads = samfile.fetch()
print opts.region
if(opts.region):
reads = samfile.fetch(region=opts.region)
introns=collections.defaultdict(int)
for read in reads:
currentloc = read.pos
for cigarop in read.cigar:
if(cigarop[0]==3):
introns[(samfile.getrname(read.tid),currentloc,currentloc+cigarop[1])]+=1
currentloc=currentloc+cigarop[1]
for i in introns:
print i,introns[i]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment