-
Notifications
You must be signed in to change notification settings - Fork 0
/
bestref.py
159 lines (97 loc) · 3.77 KB
/
bestref.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
#!/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 = "bestref.py -i pangenome.fa -hap goin.hapc -g guide.goc -l location.lg -o output "
description = "Create a optimal reference genome based on short reads from pan-genome: " + example
parser = argparse.ArgumentParser(description = description,prog = 'bestref.py')
parser.add_argument('-i', '--inpan', action='store',type=str,help='input your reference .fasta')
parser.add_argument('-o', '--output', action='store',type=str,
help='name of the pan-genome coverage output',default="output")
parser.add_argument('-g', '--gocguide', action='store',type=str,help='input your .goc file generated by mappingtools.py')
parser.add_argument('-hap', '--hapcguide', action='store',type=str,help='input your .hapc file generated by mappingtools.py')
return parser.parse_args()
def seqfileread(file):#readfa
file = open(file,"r")
lines = list(file.readlines())
dic={}
temp = ""
for i in lines:
if i.startswith(">"):
temp = i.strip()[1:]
dic[temp] = ""
#print(temp)
else:
i = i.strip()
dic[temp] = dic[temp] + i
file.close()
return dic
gosome = get_options()
seq = gosome.inpan
goc = gosome.gocguide
hap = gosome.hapcguide
seqdic = seqfileread(seq)
gocf = open(goc,"r")
gocl = list(gocf.readlines())
gocf.close()
dicgoc = {}
for i in gocl:
if i.find("Start") == -1:
i = i.split()
dicgoc[i[0].strip()] = []
if "more" in i:
lc = i.index("more")
locstart = i[2]
locend = i[lc-2]
else:
locstart = i[2]
locend = i[5]
lc = 7
maplc = i[2:lc-1]
#print(maplc)
mapc = 2
c = 0
for k in range(len(maplc)): #build the [[xxx,xxx],[xxx,xxx]] .goc dic
if mapc%2 == 0:
#print(mapc)
loc1 = maplc[mapc-2]
#print(loc1)
loc2 = maplc[mapc-1]
#print(loc1,loc2)
dicgoc[i[0]].append([loc1,loc2])
mapc += 1
print(dicgoc)
hapf = open(hap,"r")
hapl = list(hapf.readlines())
hapf.close()
cumlength = 0
temp = ""
for i in hapl[1:]:
i = i.split()
#dicgoc[i[0]] = []
mappc = i[3:-1]
#print(mappc)
name = i[0].strip()
seqname = i[0].split("-")[2]
if seqname != temp:
cumlength = 0
for j in range(len(mappc)):
if mappc[j] == "0":
print(dicgoc[name])
#print(name.strip())
#print(dicgoc["1-1-chr10"])
a = int(dicgoc[name][j][0])-cumlength
b = int(dicgoc[name][j][1])-cumlength
seqdic[seqname] = seqdic[seqname][:a] + seqdic[seqname][b:]
cumlength += b - a
temp = seqname
outputfile = open(gosome.output+".fasta","w")
for i in seqdic.keys():
print(">"+i,file = outputfile)
print(seqdic[i], file = outputfile)
outputfile.close()