I am trying to use a for loop in python to loop through a bam file and take out reads that map to certain chromosomes, for further downstream analysis. I am having problems in the beginning, seemingly easiest part of my code.
samfile = pysam.Samfile("mybam.bam", "rb") positions=["7:151970856", "7:151970856"] for p in positions: start = p.split(":") chr=start pos=start chromreads= for read in samfile: if int(chr) == int(read.tid): chromreads.append(read) print(len(chromreads))
Output I'm getting:
Output I expect:
It seems like it's not even looping through the second position. What am I doing wrong?