Skip to content

Instantly share code, notes, and snippets.

@wdecoster
Created March 16, 2018 12:19
Show Gist options
  • Select an option

  • Save wdecoster/a2be73a676b68a9f92d07509e30d37a1 to your computer and use it in GitHub Desktop.

Select an option

Save wdecoster/a2be73a676b68a9f92d07509e30d37a1 to your computer and use it in GitHub Desktop.
import pysam
import sys
import matplotlib.pyplot as plt
import numpy as np
def main(bam):
samfile = pysam.AlignmentFile(bam)
ratios = np.array([indelratio(read.cigartuples) for read in samfile.fetch()])
n, bins, patches = plt.hist(ratios, bins=[i/10 for i in range(1,50, 1)])
plt.xlabel('indel ratio')
plt.ylabel('reads')
plt.savefig("Indelratio.png")
print(np.median(ratios))
def indelratio(cigartuples):
ins = sum([oplen for op, oplen in cigartuples if op == 1])
dels = sum([oplen for op, oplen in cigartuples if op == 2]) or 1
return ins/dels
if __name__ == "__main__":
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment