Hi,
I am trying to write a python script that finds bases at specific sites in each read (aligning to the site). I am using pysam pileup, but cannot figure out how to use it properly. Could someone help me out with this?
the input data is a list of coordinates and a samfile = pysam.AlignmentFile("bam", "rb")
I just want to iterate through each read in a bam aligning to a single base site, and extract bases at each of the sites.
I tried doing this, but it's outputting each base multiple times.
for pileupcolumn in samfile.pileup('1', 14693, 14694):
for pileupread in pileupcolumn.pileups:
print pileupread.alignment.query_sequence[pileupread.query_position]
Thank you!
Kristina
Can you post entire script?
My initial attempt is posted in my first message, and now I just directly plugged in what you posted earlier. when I got an "attribute is not present" I changed it to
but that was a useless attempt
The reason I asked for entire script is to see how you are reading the bam file.
pysam.AlignmentFile()
orpysam.Samfile()
? If you are usingpysam.AlignmentFile()
, then try something like this:So I am using
pysam.AlignmentFile()
, and tried what you suggest, but now I get:Hi kris_A, Did you solve the problem? As far as I know, if I want the retrieve the base at a specific position of the read by something like the following code. The read.pos is supposed to be the order of the specific read instead of the position of reference.