-
Notifications
You must be signed in to change notification settings - Fork 0
/
promoterAligner.py
233 lines (167 loc) · 6.54 KB
/
promoterAligner.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
import os
import re
here = os.path.dirname(os.path.abspath(__file__))
files = []
for file in os.listdir(here):
if file.endswith(".gbk"):
files.append(file)
def searchFile(lines, gene):
#search for gene
complement = False
endLine = len(lines)
pattern = 'gene="'+gene+'"'
for i in range (0, endLine):
if (re.search(pattern, lines[i]) != None):
# gene found
found = True
# check for complement
complement = (re.search('complement', lines[i-1]) != None)
# get sequence boudaries
pattern = r'(\d+)\.\.(\d+)'
match = re.search(pattern, lines[i-1])
if match != None:
lower = match.group(1)
upper = match.group(2)
for j in range (i, endLine):
match = re.search(r'/product="([^"]+)"', lines[j])
if (match!= None):
product = match.group(1)
return (found, lower, upper, complement, i, product)
return (found, lower, upper, complement, i, '')
return (False, 0, 0, 0, 0, 0)
def getGeneSeq(fileLine, flag, lower, upper):
# find start line
startLine = 1
while startLine < lower and startLine+60 <= lower:
startLine+=60
lowBuffer = lower - startLine
endLine = startLine
# find end line
while endLine < upper and endLine+60 < upper:
endLine+= 60
uppBuffer = upper - endLine+1
startLine = ' '+str(startLine)+' '
endLine = ' '+str(endLine)+' '
# find beginning
for i in range (fileLine, len(lines)):
#search for the lower limit of the sequence
if (re.search(startLine, lines[i]) != None):
#lower limit found, copy cleaned up line
fileLine = i # keep copy of line where beginning was found
# Regular expression to match the letters while ignoring numbers and spaces
match = re.search(r'\b\d+\s*([a-z]+(?:\s+[a-z]+)*)', lines[i])
# Extract the letters if a match is found
if match:
sequence = match.group(1).replace(" ", "")[lowBuffer:]
# capitalize start codon when not complement (first letters)
if not isComplement and not flag :
startCodon = sequence[0:3].upper()
sequence = startCodon + sequence[3:]
break
# middle to end
for i in range (fileLine+1, len(lines)):
if (re.search(endLine, lines[i],) == None):
# before reaching end, cleanup and copy all lines to sequence
match = re.search(r'\b\d+\s*([a-z]+(?:\s+[a-z]+)*)', lines[i])
if match:
sequence+= match.group(1).replace(" ", "")
else :
# last line
match = re.search(r'\b\d+\s*([a-z]+(?:\s+[a-z]+)*)', lines[i])
if match:
sequence+= match.group(1).replace(" ", "")[0:uppBuffer]
# capitalize start codon if complement (last letters)
if isComplement and not flag:
startCodon = sequence[-3:].upper()
sequence+= sequence[:-3]+startCodon
break
return sequence
def complementSeq(sequence):
seqList = list(sequence)
for i in range (0, len(seqList)):
if seqList[i] == 'a':
seqList[i] = "t"
elif seqList[i] == 't':
seqList[i] = "a"
elif seqList[i] == 'g':
seqList[i] = "c"
elif seqList[i] == 'c':
seqList[i] = "g"
elif seqList[i] == 'A':
seqList[i] = "T"
elif seqList[i] == 'T':
seqList[i] = "A"
elif seqList[i] == 'G':
seqList[i] = "C"
elif seqList[i] == 'C':
seqList[i] = "G"
sequence = ''.join(seqList)
return sequence
def reverseSeq(sequence):
seqList = list(sequence)
sequence = ''.join(reversed(seqList))
return sequence
def main(lower, upper):
lower = int(lower)
upper = int(upper)
flag = False
# get gene sequence from boudaries
sequence = getGeneSeq(fileLine, flag, lower, upper)
flag = True
# change boundaries to get 100 bases pre- start codon
if isComplement:
lower = upper + 1
upper = upper + 100
sequence = sequence + getGeneSeq(fileLine, flag, lower, upper)
else:
upper = lower -1
lower = lower - 100
sequence = getGeneSeq(fileLine, flag, lower, upper) + sequence
# if is complement, complement and reverse sequence
if isComplement:
sequence = complementSeq(sequence)
sequence = reverseSeq(sequence)
return sequence
gene = input("Input gene name (case sensitive): ")
while (gene != 'exit'):
filesSearched = 0
fileNum = len(files)
sequence = ''
product = ''
for file in files:
percentage = int((filesSearched/fileNum)*100)
print(str(percentage)+"% " + '\t|\tFiles searched: ' + str(filesSearched)+'/'+str(fileNum), end="\r")
filesSearched+=1
TXT = gene + ".txt"
found = False
nameFlag = False
#open file
file = os.path.join(here, file)
GBK = open(file, 'r')
lines = GBK.readlines()
# search file
found, lower, upper, isComplement, fileLine, product= searchFile(lines, gene)
if found:
# gene found, open file to write in
f = open(TXT ,"a")
f.write('>'+ os.path.basename(file).split('/')[-1]+ '-' + product+ '\n')
# get sequence
sequence = main(lower, upper)
f.write(sequence+'\n'+'\n')
f.close()
else:
variations = 1
found = True
while (found):
nextGene = str(f"{gene}_{variations}")
found, lower, upper, isComplement, fileLine , product= searchFile(lines, nextGene)
if found:
# gene found, open file to write in
f = open(TXT ,"a")
f.write('>'+os.path.basename(file).split('/')[-1]+'_'+str(variations)+'-'+product+'\n')
# get sequence
sequence = main(lower, upper)
f.write(sequence+'\n'+'\n')
f.close()
variations = variations+1
gene = input("Enter 'exit' to exit program OR input another gene name (case sensitive) : ")