-
Notifications
You must be signed in to change notification settings - Fork 0
/
mappingtools.py
249 lines (193 loc) · 8.06 KB
/
mappingtools.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python
# Copyright (C) <2020> PMBL;South China Agricultural University. All rights reserved
__author__ = "Write by Fangping Li"
__version__ = '0.1.0'
import argparse
import sys
import os
import collections
def get_options():
example = "mappingtools.py -i pangenome.fa -1 illnumina_R1.fq.gz -2 illnumina_R3.fq.gz -g guide.goc -l location.lg -o outputcov -c 5 "
description = "Create one-by-one mapping and pav example: " + example
parser = argparse.ArgumentParser(description = description,prog = 'mappingtools.py')
parser.add_argument('-i', '--inpan', action='store',type=str,help='input your reference .fasta')
parser.add_argument('-t', '--threads', action='store',type=int,default=1,help='how many thread do you want to use')
parser.add_argument('-b', '--inbase', action='store',type=int,help='how many base-pair will you consider it as a total insertion rather than a replace; if these PAVs are considered as insertion, the coverage of this locus will be -1',default=20)
parser.add_argument('-1', '--pairend1', action='store',type=str,help='input your pairend1 .fastq')
parser.add_argument('-2', '--pairend2', action='store',type=str,help='input your pairend2 .fastq')
parser.add_argument('-l', '--location', action='store',type=str,
help='location of software "mummer" "lastz" and "svmu","samtools","bowtie"',default="location.lg")
parser.add_argument('-g', '--gocguide', action='store',type=str,help='input your .goc file generated by multiple.py')
parser.add_argument('-o', '--output', action='store',type=str,
help='name of the pan-genome coverage output',default="output")
parser.add_argument('-c', '--covfliter', action='store',type=int,help='coverage fliter',default=5)
parser.add_argument('-clean', '--clean', action='store',type=str,choices=('yes','no'),
help='Clean all of the middle file!"',default="no")
parser.add_argument('-create', '--createfasta', action='store',type=str,choices=('yes','no'),
help='Create a optimal reference genome for short read"',default="no")
return parser.parse_args()
def acm(a,b):
b=b
sum=0
for i in a:
sum=sum+i
mn = sum/b
return mn
gosome = get_options()
location = gosome.location
parame = open(location,"r") #mum lastz svmu samtools bowtie2 location
parameline = list(parame.readlines())
mum = parameline[0].split("=")[1].strip()
lastz = parameline[1].split("=")[1].strip()
svmu = parameline[2].split("=")[1].strip()
bowtie2 = parameline[3].split("=")[1].strip()
samtools = parameline[4].split("=")[1].strip()
p1=gosome.pairend1
p2=gosome.pairend2
parame.close()
panin = gosome.inpan
panout = gosome.output
print(bowtie2+"bowtie2-build "+"--threads "+str(gosome.threads)+" "+panin+" "+panout)
os.system(bowtie2+"bowtie2-build "+"--threads "+str(gosome.threads)+" "+panin+" "+panout)
print("begin mapping")
na1 = os.system("bosm.sh "+bowtie2+" "+panout+" "+samtools+" "+p1+" "+p2+" "+panout+" "+str(gosome.threads))
if na1 ==0:
print("mapping done")
mapout=panout+".sort.bam"
else:
print("mapping error!")
quit()
gocguide = gosome.gocguide
goc = open(gocguide,"r")
gocl = list(goc.readlines())
goc.close()
golist2 = open("golist2","w")
for i in gocl:
if i.find("Org")==-1:
i = i.split()
ch = i[1]
if "more" in i:
lc = i.index("more")
loc1 = i[2]
loc2 = i[lc-2]
else:
loc1 = i[2]
loc2 = i[5]
print("cadepth.sh "+samtools+" "+mapout+" "+ch+" "+loc1+" "+loc2,file = golist2)
golist2.close()
na2 = os.system("parallel --jobs "+str(gosome.threads)+" < golist2")
if na2 == 0:
print("Cov acquire done!")
else:
print("Cov acquire error!")
finaloutput = open(gosome.output+".cov","w")#open the .goc file again and let the .goc file guide the acquire of coverage
print("ID PanPosition RefPosition coverage",file = finaloutput)
for i in gocl:#cov acquire go!
ref = []
if i.find("Org")==-1:
i = i.split()
ch = i[1]
loc1 = i[1]
loc2 = i[2]
if "more" in i:
lc = i.index("more")
locstart = i[2]
locend = i[lc-2]
else:
locstart = i[2]
locend = i[5]
lc = 7
filename = mapout+"_"+ch+"_"+locstart+"_"+locend+".bam.depth"#open the .depth file by the guide of coverage
#print(filename)
filename = open(filename,"r")
filenamego = list(filename.readlines())
filename.close()
maplc = i[2:lc-1]
print(maplc)
loc0 = str(int(maplc[0]))
loc0c = int(loc0) - int(loc00)
print(i[0],file = finaloutput,end = " ")
print(loc0, file = finaloutput,end = " ")
print(loc0c, file = finaloutput,end = " ")
if "more" in i: #all cummlation count!
loc0f =i.index("more")-1
loc00 +=int(i[loc0f])
else:
loc00 +=int(i[6])
mapc = 2
for k in range(len(maplc)):
que =[]
if mapc%2 == 0:
#print(mapc)
loc1 = maplc[mapc-2]
#print(loc1)
loc2 = maplc[mapc-1]
#print(loc2)
if int(loc2) - int(loc1) > gosome.inbase:
print(loc1)
print(loc2)#how many base-pair will you consider it as a total insertion rather than a replace
for j in filenamego:#judge the location of depth
j = j.split()
if int(j[1])>= int(loc1) and int(j[1])< int(loc2):
que.append(int(j[2]))
#print(que)
try:
quem = str(acm(que,int(loc2)-int(loc1)))[:6]#mean caculate
except:
quem = -1
print(quem,end = " ",file = finaloutput)
else:
quem = -1
print(quem,file = finaloutput,end = " ")
mapc += 1
print("",file = finaloutput)
else:
loc00 = 0 #all cummlation count!
finaloutput.close()
if gosome.clean == "yes":
os.system("rm *depth")
os.system("rm *chr*_*_*.bam")
print("mapping coverage caculation done!")
print("Your ouput is "+ gosome.output+".cov")
covfliter = int(gosome.covfliter)
goin = open(gosome.output+".cov","r")
goinf = list(goin.readlines())
goin.close()
goout = open(gosome.output+".cov"+".hapc","w")
dicg = collections.OrderedDict()
print("ID PanPosition RefPosition HaplotypeComposition Haplotype",file = goout)
for i in goinf:
#print(i)
if i.find("Pan") == -1:
i = i.split()
#print(i)
dicname = i[0]
dicg[dicname] = []
dicg[dicname].append(i[1])
dicg[dicname].append(i[2])
for j in i[3:]:
if float(j) > covfliter:
dicg[dicname].append("1")
else:
dicg[dicname].append("0")
#print(dicg[dicname])
for i in dicg.keys():
hap = i
hapc =""
print(hap,end = " ",file = goout)
print(dicg[i][0],end = " ",file = goout)
print(dicg[i][1],end = " ",file = goout)
for j in dicg[i][2:]:
hapc += j
print(j,end = " ",file = goout)
print("Hap"+hapc, file = goout)
#print("",file = goout)
goout.close()
print("Hap caculation done!")
print("Your ouput is "+gosome.output+".cov"+".hapc")
if gosome.createfasta == "yes":
print("Go to create a optimal reference genome for short read")
naa = os.system("bestred.py -i "+panin+" -g "+gocguide+" -hap "+gosome.output+".cov"+".hapc"+" -o "+ gosome.output+".cov"+".hapc.fasta" )
if naa == 0:
print("Success!")
print("Your optimal reference ouput is "+gosome.output+".cov"+".hapc.fasta")