-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmean_coverage_plot.py
69 lines (51 loc) · 1.32 KB
/
mean_coverage_plot.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
import pysam
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import re
import pylab
from collections import defaultdict
samfile = pysam.Samfile( "LM2m1.bam", "rb" )
region = samfile.fetch('chrM')
list_of_lists = []
for read in region:
extracted = str(read)
AS = extracted.find('AS')
a_flag_string = extracted[AS:AS+9]
a_flag_number = re.findall(r'\d+', a_flag_string)
a_flag_number = int (a_flag_number[0])
regionpos = read.pos
list_for_list = []
list_for_list.append(regionpos)
list_for_list.append(a_flag_number)
list_of_lists.append(list_for_list)
list_of_lists.sort(key=lambda x:x)
# List I am appending to
newlist=[]
counter = 1
for ls in list_of_lists:
newlist.append(ls)
sums = defaultdict(int)
counts = defaultdict(int)
xaxis = []
yaxis = []
#zaxis = []
for lis in newlist:
xaxis.append(lis[0])
yaxis.append(lis[1])
xmax = max(xaxis)
ymax = max(yaxis)
print(newlist)
for k in newlist:
sums[k[0]] += k[1]
counts[k[0]] += 1
positions= []
average_As = []
for x in sums.keys():
print "average " + str(x) + ": " + str(float(sums[x])/counts[x])
positions.append(int(x))
average_As.append(float(sums[x])/counts[x])
print(positions)
print(average_As)
plt.plot(positions, average_As)
plt.show()