-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract.py
187 lines (149 loc) · 4.46 KB
/
extract.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
import os
from Bio import Seq
class extractApp():
def __init__(self):
self.verbose = False
def start(self, database, expanded, to_examine, working=''):
if working!='':
if working[-1]!='/':
working+='/'
if '../' in working:
dir = os.path.dirname(__file__)
working = os.path.join(dir, working)
os.chdir(working)
else:
working=os.path.dirname(__file__)
if '../' in database or database[0]!='/':
dir = os.path.dirname(working)
database = os.path.join(dir, database)
if '../' in expanded or expanded[0]!='/':
dir = os.path.dirname(working)
expanded = os.path.join(dir, expanded)
if '../' in to_examine or to_examine[0]!='/':
dir = os.path.dirname(working)
to_examine = os.path.join(dir, to_examine)
infile_filename = expanded.split('/')[-1]
infiledir = expanded.split(infile_filename)[0]
extracted_path=infiledir+'extracted_genes/'
if not os.path.exists(extracted_path):
os.makedirs(extracted_path)
#print extracted_path, "test'"
with open(to_examine, 'r') as file:
all_examine=file.readlines()
file.close()
#print all_examine[:4]
examin_headers=[]
for line in all_examine:
if '>' in line:
examin_headers.append(line.split('>')[1])
elif '\n'!=line:
examin_headers.append(line)
#print len(examin_headers)
with open(expanded, 'r') as file:
all_exapnded =file.readlines()
file.close()
#print all_exapnded[:4]
x=0
while x <len(all_examine):
#print [all_examine[x]]
all_examine[x]=str(all_examine[x]).replace('\t','').replace('\n','')
x+=1
#print all_exapnded[0:3]
clustered_headers=[]
clusters=[]
record=False
y=0
t=True
#print examin_headers[:4]
for header in examin_headers:
for line in all_exapnded:
#if t==True:
# print 'x'
if record==True:
if '--------' in line:
record=False
clustered_headers.append(clusters)
clusters=[]
else:
clusters.append(line)
if ' 'in header:
#if t==True:
# t=False
# print '-'+header.split(' ')[0].replace('\n','')
if '-'+header.split(' ')[0].replace('\n','') in line:
clusters.append(header.split(' ')[0])
record=True
else:
#if t==True:
# t=False
# print '-'+header.split(' ')[0].replace('\n','')
if '-'+header.replace('\n','') in line:
clusters.append(header.replace("signalal","signal"))
#print "test"
record=True
clustered_headers.append(clusters)
#print clustered_headers
#print len(clustered_headers)
#print len(clustered_headers[0])
with open(database,'r') as file:
all_genes=file.readlines()
file.close()
record=False
#print clustered_headers[:2]
#print len(clustered_headers)
to_edit = all_genes
all_genes=[]
for gene in to_edit:
if ' 'in gene:
all_genes.append(gene.split(' ')[0]) #turn this into a \n replace as well as a " " replace with _. problem is signal peptide spelled signalal
else:
all_genes.append(gene)
#print [clustered_headers[-1][0]]
#print [all_genes[0]]
#print len(all_genes)
reverse=False
check=True
for cluster in clustered_headers:
z=0
to_write=''
while z<len(cluster):
x=0
if '63_fo_mel_Fom013_contig_1806:3631-3882' in cluster[z]:
print("ahhhhhh")
print(cluster[z])
if '_Reversed' in cluster[z].replace('\n',''):
reverse=True
to_reverse=''
while x<len(all_genes):
if record==True and reverse==False:
if '63_fo_mel_Fom013_contig_1806:3631-3882' in cluster[z]:
print("ahhhhhh")
if '>' in all_genes[x]:
record=False
to_write+='\n'
else:
to_write+=all_genes[x]
elif record==True and reverse ==True:
if '>' in all_genes[x]:
record=False
reverse=False
my_seq=Seq.Seq(str(to_reverse))
rrr=my_seq.reverse_complement()
to_write+=str(rrr).replace('\n','')+'\n'
else:
to_reverse+=all_genes[x].replace('\n','')
if reverse==True:
if ">"+cluster[z].split('_Reversed')[0] == all_genes[x].split(" ")[0].replace('\n','') and cluster[z]!='\n':
to_write+='>'+cluster[z]
record=True
else:
if ">"+cluster[z].replace('\n',"") == all_genes[x].split(" ")[0].replace('\n','') and cluster[z]!='\n':
to_write+='>'+cluster[z]
record=True
x+=1
z+=1
if cluster != []:
#print extracted_path
with open(extracted_path+cluster[0].replace('\n','')+'_extracted.fasta','w') as file:
file.write(to_write)
file.close