Is there a standard method in Python to extract a CIGAR string from the BAM?
There are great libraries which parse the CIGAR, e.g. https://pypi.python.org/pypi/cigar/0.1
>>> c = Cigar('10M20S10M')
>>> c.mask_left(10).cigar
'10S20S10M'
>>> c.mask_left(9).cigar
'9S1M20S10M'
>>> Cigar('10S').mask_left(10).cigar
'10S'
>>> Cigar('10H').mask_left(10).cigar
'10H'
>>> Cigar('10H').mask_left(11).cigar
'10H'
>>> Cigar('10H').mask_left(9).cigar
'10H'
It looks like pysam already parses the cigar string, if I'm not mistaken:
import pysam
bam = 'myfile.bam'
bamfile = pysam.AlignmentFile(bam, 'rb')
for read in bamfile:
if not read.is_unmapped:
cigar = read.cigar
print(cigar)
This outputs lists of tuples of the parse output:
[(5, 61), (0, 30), (5, 198)]
[(4, 11), (0, 30), (4, 248)]
[(4, 11), (0, 30), (4, 248)]
....
How could I simply output the CIGAR from read
? I would prefer to create a column in a pandas DataFrame