Pysam get query sequence range without loop
0
0
Entering edit mode
17 months ago
yryan ▴ 10

Hi folks,

I'm trying to optimise my code here. Esentially I'm trying to use pysam to get a list of bases of reads around a base of interest as shown in the code below. However my code is quite inefficient as the only way I've managed to get it working is by looping through each column in the pileup instead of just going directly to my location of interest. Could anyone help me optimise this?

def location_extractor(input_file, location, save_name):
base_0 = []
full_sequence = []
for pileupcolumn in samfile.pileup("ref", location, (location+1)):
# if lower error rate between control and exposed v different, higher = loss
pileupcolumn.set_min_base_quality(4)
for pileupread in pileupcolumn.pileups:  # very inefficient, getting to location first would speed up vs looping
if pileupcolumn.pos != location:
continue
else:

location_extractor("exposed_5h_sorted.sam", 4604, "exposed_5.fasta")


Cheers!

pysam python • 644 views