Hello,

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[0]
        pos=start[1]
        chromreads=[]
        for read in samfile:
        if int(chr) == int(read.tid):
            chromreads.append(read)
        print(len(chromreads))

Output I'm getting:

    2525527
    0

Output I expect:

    2525527
    2525527



It seems like it's not even looping through the second position.  What am I doing wrong?

Thank you.
Answered my own question [from StackOverflow][1] 

 [1]: http://stackoverflow.com/questions/10255273/iterating-on-a-file-using-python