-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatpangenome.py
168 lines (143 loc) · 7.72 KB
/
creatpangenome.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
#!/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
def get_options():
description = "Create lineal pangenome"
parser = argparse.ArgumentParser(description = description,prog = 'creatpangenome.py')
parser.add_argument('--version', action='version',version='%(prog)s '+__version__+" "+__author__)
parser.add_argument('-1', '--reference', action='store',type=str,help='input your reference .fasta')
parser.add_argument('-2', '--query', action='store',type=str,help='input your query .fasta')
parser.add_argument('-g', '--svguide', action='store',type=str,help='input your sv file generated by svmu')
parser.add_argument('-r', '--rangefliter', action='store',type=int,help='SVs distance between red and query',default=1000000)
parser.add_argument('-o', '--output', action='store',type=str,help='name of the pan-genome output: <-o>.fasta;<-o>.fasta.goc',default="output")
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()
seq1 = gosome.reference#sys.argv[1] #Firstgenome
seq2 = gosome.query#sys.argv[2] #Secondgenome
seqdic1 = seqfileread(seq1)
seqdic2 = seqfileread(seq2)
rangefliter = gosome.rangefliter
svfile = open(gosome.svguide,"r")#sys.argv[3]#svguidefile
svfileline = list(svfile.readlines())
svfile.close()
cumlength = 0
ids = 0
outputloc = open(gosome.output+".goc","w")
roundcount = open("roundcount","r")
roundcountn = int(list(roundcount.readlines())[0])
print(roundcountn)
roundcount.close()
if roundcountn == 1:
for i in svfileline:
j = i.split()
a = int(j[1]) + cumlength
b = int(j[2]) + cumlength
c = int(j[5])-1
d = int(j[6])-1
if abs(int(j[5]) - int(j[1])) < rangefliter:
seqdic1[j[0]] = seqdic1[j[0]][:b] + seqdic2[j[4]][c:d+1] + seqdic1[j[0]][b:]
cumlength += int(j[6])-int(j[5])
orglength = int(j[6])-int(j[5])
ids += 1
idk = str(roundcountn)+"-"+str(ids)+"-"+ j[0]
locin = str(idk)+ " " + j[0] + " " +str(a) + " " + str(b) + " " + str(b+1) + " " +str(b+orglength) + " " +str(orglength)
locout= str("Org"+str(idk))+ " "+ j[0] + " " +j[1] + " " + j[2] + " " + j[5] + " " +j[6] + " " +j[8]
print(locin,file = outputloc)
print(locout,file = outputloc)
if roundcountn > 1:
for i in svfileline:
j = i.split()
if int(j[5]) - int(j[1]) < rangefliter:#SVs range fliter
a = int(j[1]) + cumlength
b = int(j[2]) + cumlength
m = int(j[1])
n = int(j[2])
c = int(j[5])-1
d = int(j[6])-1
orglength = int(j[6])-int(j[5])
tempgocf = open("temp"+str(roundcountn-1)+".fasta.goc","r")
tempgoc = list(tempgocf.readlines())
tempgoclenc = 0
for k in tempgoc:
if k.find("Org")== -1:
tempgoclenc += 1
k = k.split()
if "more" in k:
if j[0] == k[1]:
if int(m)+ orglength >int(k[2]) and int(k[k.index("more")-2]) >int(n)+ orglength:
print(m,int(k[2]),k[5],n)
g = int(k[2]) + cumlength
f = int(k[5]) + cumlength
seqdic1[j[0]] = seqdic1[j[0]][:f] + seqdic2[j[4]][c:d+1] + seqdic1[j[0]][f:]
ids += 1
idk = str(roundcountn)+"-"+str(ids)+"-"+ j[0]
print(idk)
locin = str(idk)+ " " + j[0] + " " +str(g) + " " + str(f) + " " + str(f+1) + " " +str(f+orglength) + " " +str(orglength)+" "+"more"+" "+k[0]
locout= str("Org"+str(idk))+ " "+ j[0] + " " +j[1] + " " + j[2] + " " + j[5] + " " +j[6] + " " +j[8]
print(locin,file = outputloc)
print(locout,file = outputloc)
cumlength += int(j[6])-int(j[5])
break
if tempgoclenc == len(tempgoc)/2:#weather the cycle will be end means not in same position
seqdic1[j[0]] = seqdic1[j[0]][:b] + seqdic2[j[4]][c:d+1] + seqdic1[j[0]][b:]
orglength = int(j[6])-int(j[5])
ids += 1
idk = str(roundcountn)+"-"+str(ids)+"-"+ j[0]
print(idk)
locin = str(idk)+ " " + j[0] + " " +str(a) + " " + str(b) + " " + str(b+1) + " " +str(b+orglength) + " " +str(orglength)
locout= str("Org"+str(idk))+ " "+ j[0] + " " +j[1] + " " + j[2] + " " + j[5] + " " +j[6] + " " +j[8]
print(locin,file = outputloc)
print(locout,file = outputloc)
cumlength += int(j[6])-int(j[5])
else:
if j[0] == k[1]:
if int(m)+ orglength >int(k[2]) and int(k[5]) >int(n)+ orglength:
print(m,int(k[2]),k[5],n)
g = int(k[2]) + cumlength
f = int(k[5]) + cumlength
seqdic1[j[0]] = seqdic1[j[0]][:f] + seqdic2[j[4]][c:d+1] + seqdic1[j[0]][f:]
ids += 1
idk = str(roundcountn)+"-"+str(ids)+"-"+ j[0]
print(idk)
locin = str(idk)+ " " + j[0] + " " +str(g) + " " + str(f) + " " + str(f+1) + " " +str(f+orglength) + " " +str(orglength)+" "+"more"+" "+k[0]
locout= str("Org"+str(idk))+ " "+ j[0] + " " +j[1] + " " + j[2] + " " + j[5] + " " +j[6] + " " +j[8]
print(locin,file = outputloc)
print(locout,file = outputloc)
cumlength += int(j[6])-int(j[5])
break
if tempgoclenc == len(tempgoc)/2:#weather the cycle will be end means not in same position
seqdic1[j[0]] = seqdic1[j[0]][:b] + seqdic2[j[4]][c:d+1] + seqdic1[j[0]][b:]
orglength = int(j[6])-int(j[5])
ids += 1
idk = str(roundcountn)+"-"+str(ids)+"-"+ j[0]
print(idk)
locin = str(idk)+ " " + j[0] + " " +str(a) + " " + str(b) + " " + str(b+1) + " " +str(b+orglength) + " " +str(orglength)
locout= str("Org"+str(idk))+ " "+ j[0] + " " +j[1] + " " + j[2] + " " + j[5] + " " +j[6] + " " +j[8]
print(locin,file = outputloc)
print(locout,file = outputloc)
cumlength += int(j[6])-int(j[5])
outputloc.close()
outputfile = open(gosome.output+".fasta","w")
for i in seqdic1.keys():
print(">"+i,file = outputfile)
print(seqdic1[i], file = outputfile)
outputfile.close()