forked from Maggi-Chen/Inspector
-
Notifications
You must be signed in to change notification settings - Fork 4
/
denovo_plot.py
182 lines (152 loc) · 3.96 KB
/
denovo_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import matplotlib
matplotlib.use('Agg')
import pysam
import matplotlib.pyplot as plt
def plot_n100(outpath,minlen):
ctglen=open(outpath+'contig_length_info','r').read().split('\n')[:-1]
ctglen=[int(c.split('\t')[1]) for c in ctglen if int(c.split('\t')[1]) >= minlen]
n100=[]
x100=[]
ctglen.sort(reverse=True)
totallen=sum(ctglen)
addlen=0
lastlen=0
for i in range(100):
x100+=[i+1]
while addlen < (i+1)/100.0*totallen:
try:
lastlen=ctglen.pop(1)
addlen+=lastlen
except:
break
n100+=[lastlen]
plt.plot(x100,n100,linewidth=2)
plt.xlabel('N1-N100')
plt.ylabel('Contig Length /bp')
plt.savefig(outpath+'plot_n1n100.pdf')
print ('end n100')
return 0
def plot_na100(outpath):
samfile=pysam.AlignmentFile(outpath+'contig_to_ref.sam','r')
allread=samfile.fetch()
alignlen=[]
for align in allread:
if align.flag==4:
continue
alignlen+=[align.query_alignment_length]
n100=[]
x100=[]
alignlen.sort(reverse=True)
totallen=sum(alignlen)
addlen=0
lastlen=0
for i in range(100):
x100+=[i+1]
while addlen < (i+1)/100.0*totallen:
try:
lastlen=alignlen.pop(1)
addlen+=lastlen
except:
break
n100+=[lastlen]
plt.plot(x100,n100,linewidth=2)
plt.xlabel('NA1-NA100')
plt.ylabel('Contig Length /bp')
plt.savefig(outpath+'plot_na1na100.pdf')
print ('end na100')
return 0
def findpos(c,ctglength,step,startrefpos,ctgstartpos):
temppos=[]
ctgname=c.query_name
refpos=c.reference_start
cigar=c.cigarstring
if 'S' not in cigar.split('M')[0].split('=')[0] and 'H' not in cigar.split('M')[0].split('=')[0]:
ctgpos=0
else:
ctgpos=int(cigar.split('M')[0].split('=')[0].split('S')[0].split('H')[0])
currpos=ctgpos
num=''
for m in cigar:
num+=m; continue
if m in 'M=X':
ctgpos+=int(num); refpos+=int(num); num=''
if m=='I':
ctgpos+=int(num);num=''
if m =='D':
refpos+=int(num); num='';continue
if m in 'SH':
if refpos>c.reference_start:
break
else:
num=''
while ctgpos>=currpos+step:
if ctgpos>=currpos+step*2:
temppos+=[[currpos+step,refpos+startrefpos,ctgname]]
else:
temppos+=[[ctgpos,refpos+startrefpos,ctgname]]
currpos+=step
if c.flag in [16,2064]:
temppos=[ [ctglength-mm[0],mm[1],mm[2]] for mm in temppos]
updatestart=[]
for mm in temppos :
updatestart+=[[mm[0]+ctgstartpos,mm[1],mm[2]]]
return updatestart
def plot_dotplot(outpath):
print ('start dot plot')
samfile=pysam.AlignmentFile(outpath+'contig_to_ref.bam','rb')
allchrom=samfile.references
allchromlen=samfile.lengths
maxreflen=max(allchromlen)
idex=allchromlen.index(maxreflen)
maxchrom=allchrom[idex]
print (maxchrom)
allread=samfile.fetch(maxchrom)
if maxreflen >= 10000000:
step=10000
elif maxreflen>=1000000:
step=1000
else:
step=100
ctgleninfo=open(outpath+'contig_length_info','r').read().split('\n')[:-1]
ctglen={}
for c in ctgleninfo:
ctglen[c.split('\t')[0]]=int(c.split('\t')[1])
alignedctg={}
for align in allread:
if align.query_name not in alignedctg:
alignedctg[align.query_name]=align.query_alignment_length
else:
alignedctg[align.query_name]+=align.query_alignment_length
longalignctg=[c for c in alignedctg if alignedctg[c] >= maxreflen/100.0]
print (len(longalignctg))
startpos=0
contig_startpos={}
for c in longalignctg:
contig_startpos[c]=startpos
startpos+=ctglen[c]
allpos=[]
allread=samfile.fetch(maxchrom)
for align in allread:
if align.query_name not in longalignctg:
continue
temppos=[]
ctglength=ctglen[align.query_name]
ctgstart=contig_startpos[align.query_name]
temppos=findpos(align,ctglength,step,0,ctgstart)
allpos+=temppos
plotx=[c[0] for c in allpos]
ploty=[c[1] for c in allpos]
plotcolor=[c[2] for c in allpos]
allcolor=set(plotcolor)
colors={}
i=1
for c in allcolor:
colors[c]=i
i+=10
plotcolor2=[colors[c] for c in plotcolor]
size=[1]*len(plotcolor2)
plt.scatter(plotx,ploty,s=size,c=plotcolor2)
plt.xlabel('Reference Position')
plt.ylabel('Contig Position')
plt.savefig(outpath+'plot_synteny.pdf')
return 0