Download this file

57 lines (48 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from singlecellmultiomics.bamProcessing.pileup import pileup_truncated
def get_pileup_vect(alignments, contig, pos, ref, alt):
"""Create feature vector for selected variant
Args:
alignments(pysam.AlignmentFile) : Handle to alignmentfile
contig(str) : contig to perform pileup
pos(int) : zeros based position of variant to pileup
ref(str) : reference base
alt(str) : alternative base
Returns
total(int) : Total amount of bases overlapping with selected location
ref_calls : Total amount of bases matching ref
alt_calls : Total amount of bases matching alt
other_calls : Total amount of bases matching neither ref nor alt
"""
total = 0
ref_calls = 0
alt_calls = 0
other_calls = 0
start=pos
stop = pos+1
for pileupcolumn in pileup_truncated(alignments,contig,start,stop,stepper='all'):
for i,pileupread in enumerate(pileupcolumn.pileups):
if not pileupread.is_del and not pileupread.is_refskip:
call = pileupread.alignment.query_sequence[pileupread.query_position]
if call==ref:
ref_calls += 1
elif call==alt:
alt_calls += 1
else:
other_calls += 1
other_calls+=1
return total, ref_calls, alt_calls, other_calls
# Create mapping quality feature vector:
def get_mapping_q_vect(alignments_handle, contig, pos, radius=150):
"""Obtain histogram of mapping qualties, clipped at 60
Args:
alignments(pysam.AlignmentFile) : Handle to alignmentfile
contig(str) : contig
pos(int) : zeros based position of location to check mapping qualties
radius(int) : radius to check around selected location
Returns:
mapping_qualities(list) : Histogram with 7 bins (0 to highest mapping quality)
"""
mapping_qualities = [0]*7
for read in alignments_handle.fetch(contig, pos-radius, pos+radius):
mapping_qualities[min(60,int(read.mapping_quality/10))]+=1
return mapping_qualities