forked from sd17fall/GeneFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgene_finder.py
303 lines (221 loc) · 9.09 KB
/
gene_finder.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# -*- coding: utf-8 -*-
"""
Finds amino acid sequences coded by DNA input
@author: Vivien Chen
"""
import random
from amino_acids import aa, codons, aa_table # you may find these useful
from load import load_seq
dna = load_seq("./data/X73525.fa")
import doctest
from pickle import dump, load
def shuffle_string(s):
"""Shuffles the characters in the input string
NOTE: this is a helper function, you do not
have to modify this in any way """
return ''.join(random.sample(s, len(s)))
# YOU WILL START YOUR IMPLEMENTATION FROM HERE DOWN ###
def get_complement(nucleotide):
""" Returns the complementary nucleotide
nucleotide: a nucleotide (A, C, G, or T) represented as a string
returns: the complementary nucleotide
I added two more doctests because each complementary nucleotide is in
its own if/else if branch. If one branch doesn't work and there is no
doctest for that branch, the mistake will not be caught. In fact, I
found out that I spelled nucleotide wrong in the G branch.
>>> get_complement('A')
'T'
>>> get_complement('C')
'G'
>>> get_complement('G')
'C'
>>> get_complement('T')
'A'
"""
if nucleotide == 'A':
return 'T'
elif nucleotide == 'C':
return 'G'
elif nucleotide == 'G':
return 'C'
elif nucleotide == 'T':
return 'A'
#doctest.run_docstring_examples(get_complement, globals(), verbose=True)
def get_reverse_complement(dna):
""" Computes the reverse complementary sequence of DNA for the specfied DNA
sequence
dna: a DNA sequence represented as a string
returns: the reverse complementary DNA sequence represented as a string
I did not add any more doctests because each string contains all the
nucleotides, so two doctests with random strings is sufficient.
>>> get_reverse_complement("ATGCCCGCTTT")
'AAAGCGGGCAT'
>>> get_reverse_complement("CCGCGTTCA")
'TGAACGCGG'
"""
reversed_dna = dna[::-1]
comp_list = []
for nuc in range(0, len(reversed_dna)):
comp_nuc = get_complement(reversed_dna[nuc])
comp_list.append(comp_nuc)
comp_str = ''.join(comp_list)
return comp_str
#doctest.run_docstring_examples(get_reverse_complement, globals(), verbose=True)
def rest_of_ORF(dna):
""" Takes a DNA sequence that is assumed to begin with a start
codon and returns the sequence up to but not including the
first in frame stop codon. If there is no in frame stop codon,
returns the whole string.
dna: a DNA sequence
returns: the open reading frame represented as a string
I added two more doctests: one in which the frame stop codon is TAA and
another in which there is no frame stop codon. This way, all the
branches are tested for error.
>>> rest_of_ORF("ATGTGAA")
'ATG'
>>> rest_of_ORF("ATGAGATAGG")
'ATGAGA'
>>> rest_of_ORF("ATGGAATAAGTA")
'ATGGAA'
>>> rest_of_ORF("ATGAGAGA")
'ATGAGAGA'
"""
stop_codons = ['TAA', 'TAG', 'TGA']
for i in range(3, len(dna)-2, 3):
if dna[i:i+3] in stop_codons:
return dna[:i]
return dna
#doctest.run_docstring_examples(rest_of_ORF, globals(), verbose=True)
def find_all_ORFs_oneframe(dna):
""" Finds all non-nested open reading frames in the given DNA
sequence and returns them as a list. This function should
only find ORFs that are in the default frame of the sequence
(i.e. they start on indices that are multiples of 3).
By non-nested we mean that if an ORF occurs entirely within
another ORF, it should not be included in the returned list of ORFs.
dna: a DNA sequence
returns: a list of non-nested ORFs
I added another doctest that includes three nested ATGs in the default
frame of the sequence in order to make sure that the function does not
return nested ORFs in that frame.
>>> find_all_ORFs_oneframe("ATGCATGAATGTAGATAGATGTGCCC")
['ATGCATGAATGTAGA', 'ATGTGCCC']
>>> find_all_ORFs_oneframe('ATGATGATGTAAC')
['ATGATGATG']
"""
all_ORFs = []
stopped = True
stop_codons = ['TAA', 'TAG', 'TGA']
for i in range(0, len(dna)-2, 3):
if dna[i:i+3] == 'ATG' and stopped:
all_ORFs.append(rest_of_ORF(dna[i:]))
stopped = False
elif dna[i:i+3] in stop_codons:
stopped = True
return all_ORFs
#doctest.run_docstring_examples(find_all_ORFs_oneframe, globals(), verbose=True)
def find_all_ORFs(dna):
""" Finds all non-nested open reading frames in the given DNA sequence in
all 3 possible frames and returns them as a list. By non-nested we
mean that if an ORF occurs entirely within another ORF and they are
both in the same frame, it should not be included in the returned list
of ORFs.
dna: a DNA sequence
returns: a list of non-nested ORFs
I did not add any more doctests because this function depends on the
previous functions, so assuming the doctests of those were fruitful,
one doctest is sufficient for this function. The doctest includes one
ORF in each frame, so the function works if the doctest yields true.
>>> find_all_ORFs("ATGCATGAATGTAG")
['ATGCATGAATGTAG', 'ATGAATGTAG', 'ATG']
"""
frame1 = find_all_ORFs_oneframe(dna)
frame2 = find_all_ORFs_oneframe(dna[1:])
frame3 = find_all_ORFs_oneframe(dna[2:])
return frame1 + frame2 + frame3
#doctest.run_docstring_examples(find_all_ORFs, globals(), verbose=True)
def find_all_ORFs_both_strands(dna):
""" Finds all non-nested open reading frames in the given DNA sequence on both
strands.
dna: a DNA sequence
returns: a list of non-nested ORFs
I did not add any more doctests because this function depends on the
previous functions, so assuming the doctests of those were fruitful,
one doctest is sufficient for this function. The doctest includes one
ORF in each strand, so the function works if the doctest yields true.
>>> find_all_ORFs_both_strands("ATGCGAATGTAGCATCAAA")
['ATGCGAATG', 'ATGCTACATTCGCAT']
"""
strand1 = find_all_ORFs(dna)
strand2 = find_all_ORFs(get_reverse_complement(dna))
return strand1 + strand2
#doctest.run_docstring_examples(find_all_ORFs_both_strands, globals(), verbose=True)
def longest_ORF(dna):
""" Finds the longest ORF on both strands of the specified DNA and returns it
as a string
>>> longest_ORF("ATGCGAATGTAGCATCAAA")
'ATGCTACATTCGCAT'
"""
all_ORFs = find_all_ORFs_both_strands(dna)
humongest = 0
for i in range(0, len(all_ORFs)-1):
if len(all_ORFs[i]) < len(all_ORFs[i+1]):
humongest = all_ORFs[i+1]
return humongest
#doctest.run_docstring_examples(longest_ORF, globals(), verbose=True)
def longest_ORF_noncoding(dna, num_trials):
""" Computes the maximum length of the longest ORF over num_trials shuffles
of the specfied DNA sequence
dna: a DNA sequence
num_trials: the number of random shuffles
returns: the maximum length longest ORF """
dna_shuffles = []
for i in range(0, num_trials):
dna_shuffles.append(shuffle_string(dna))
for i in range(0, len(dna_shuffles)-1):
if longest_ORF(dna_shuffles[i]) < longest_ORF(dna_shuffles[i+1]):
humongest = longest_ORF(dna_shuffles[i+1])
return len(humongest)
#print longest_ORF_noncoding('ATGCGAATGTAGCATCAAA', 100)
def coding_strand_to_AA(dna):
""" Computes the Protein encoded by a sequence of DNA. This function
does not check for start and stop codons (it assumes that the input
DNA sequence represents an protein coding region).
dna: a DNA sequence represented as a string
returns: a string containing the sequence of amino acids encoded by the
the input DNA fragment
>>> coding_strand_to_AA("ATGCGA")
'MR'
>>> coding_strand_to_AA("ATGCCCGCTTT")
'MPA'
"""
amino_acids = []
for i in range(0, len(dna)-2, 3):
amino_acid = aa_table[dna[i:i+3]]
amino_acids.append(amino_acid)
amino_acids_str = ''.join(amino_acids)
return amino_acids_str
#doctest.run_docstring_examples(coding_strand_to_AA, globals(), verbose=True)
def gene_finder(dna):
""" Returns the amino acid sequences that are likely coded by the specified dna
dna: a DNA sequence
returns: a list of all amino acid sequences coded by the sequence dna.
"""
threshold = longest_ORF_noncoding(dna, 1500)
all_ORFs = find_all_ORFs_both_strands(dna)
amino_acids = []
for i in range(0, len(all_ORFs)):
if len(all_ORFs[i]) > threshold:
amino_acid = coding_strand_to_AA(all_ORFs[i])
amino_acids.append(amino_acid)
return amino_acids
if __name__ == "__main__":
#import doctest
#doctest.testmod()
genes = gene_finder(dna)
#write to genes.txt file
file_ = open('genes.txt', 'wb')
dump(genes, file_)
#read from genes.txt file
file_ = open('genes.txt', 'rb+')
print(load(file_))