forked from sd2020spring/GeneFinder-Zachary3352
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgene_finder.py
282 lines (223 loc) · 9.86 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
# -*- coding: utf-8 -*-
"""
@author: Zachary Sherman (Zachary3352)
"""
import random
from amino_acids import aa, codons, aa_table # you may find these useful
from load import load_seq
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
>>> get_complement('A')
'T'
>>> get_complement('C')
'G'
Didn't feel the need to add another unit test here because these two adequately test the function.
I could add unit tests to get the complements for T and G, but this feels unnecessary with such a simple function.
"""
if nucleotide == "A":
return "T"
elif nucleotide == "T":
return "A"
elif nucleotide == "C":
return "G"
elif nucleotide == "G":
return "C"
else:
return "Failed, check nucleotide letters!"
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
>>> get_reverse_complement("ATGCCCGCTTT")
'AAAGCGGGCAT'
>>> get_reverse_complement("CCGCGTTCA")
'TGAACGCGG'
This function's ability is clearly visible using only the provided doctests, so I decided not to add more.
"""
reversed = []
for nucleotide in dna:
backwards = dna[::-1] # Found out how to do this on https://www.educative.io/edpresso/how-do-you-reverse-a-string-in-python
for nucleotide in range(len(dna)):
reversed_nucleotide = get_complement(backwards[nucleotide])
reversed.append(reversed_nucleotide)
final_reversed_string = ''.join(reversed)
return final_reversed_string
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
>>> rest_of_ORF("ATGTGAA")
'ATG'
>>> rest_of_ORF("ATGAGATAGG")
'ATGAGA'
>>> rest_of_ORF("ATGTGCCC")
'ATGTGCCC'
Added this doctest:
>>> rest_of_ORF("ATGGTTTCTAATTAA")
'ATGGTTTCTAAT'
Added the above doctest to demonstrate the third possible stop codon.
"""
i = 0
while i < len(dna):
if dna[i:i+3] == codons[10][0] or dna[i:i+3] == codons[10][1] or dna[i:i+3] == codons[10][2]:
return dna[:i]
break
i = i+3
return dna
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
>>> find_all_ORFs_oneframe("ATGCATGAATGTAGATAGATGTGCCC")
['ATGCATGAATGTAGA', 'ATGTGCCC']
Added this doctest:
>>> find_all_ORFs_oneframe("ATGCATTATTGCCGTGGCTGAGTTATGATTCTTCTCTAA")
['ATGCATTATTGCCGTGGC', 'ATGATTCTTCTC']
Added the above doctest as an extra example.
"""
i = 0
current_orf = []
all_found_orfs = []
while i < len(dna):
if dna[i:i+3] == codons[3][0]:
current_orf = rest_of_ORF(dna[i:])
all_found_orfs.append(current_orf)
i = i + len(current_orf) - 3
i = i+3
return all_found_orfs
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
>>> find_all_ORFs("ATGCATGAATGTAG")
['ATGCATGAATGTAG', 'ATGAATGTAG', 'ATG']
Didn't feel the need to add another doctest here because the above test already adequately tests the function.
It shows non-nested ORFs, but includes ORFs that end at the same point.
"""
i = 0
all_orfs = []
while i < 3:
all_orfs = all_orfs + find_all_ORFs_oneframe(dna[i:])
i = i+1
return all_orfs
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
>>> find_all_ORFs_both_strands("ATGCGAATGTAGCATCAAA")
['ATGCGAATG', 'ATGCTACATTCGCAT']
Added this doctest:
>>> find_all_ORFs_both_strands("ATGCATTATTGCCGTGGCTGAGTTATGATTCTTCTCTAA")
['ATGCATTATTGCCGTGGC', 'ATGATTCTTCTC', 'ATGCAT']
Added the above doctest as an additional, more complicated test.
"""
dna_reversed = get_reverse_complement(dna)
all_ORFs_both_strands = find_all_ORFs(dna) + find_all_ORFs(dna_reversed)
return all_ORFs_both_strands
# WEEK 2 BEGINS HERE
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'
Didn't feel the need to add an additional doctest because the provided doctest includes shorter ORFs than the
ORFs the doctest finds -- the doctest finds the longest ORF.
"""
all_orfs = find_all_ORFs_both_strands(dna)
if len(all_orfs) > 0:
longest_ORF = max(all_orfs, key=len)
else:
longest_ORF = []
#print(longest_ORF)
return longest_ORF
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
I don't believe it's possible to add a doctest that always works to this function because the maximum
length longest ORF could change based on how many trials are used.
"""
longest_found_ORF = 0
for scramble in range(num_trials):
shuffled_string = shuffle_string(dna)
found_ORF = len(longest_ORF(shuffled_string))
if found_ORF > longest_found_ORF:
longest_found_ORF = found_ORF
return longest_found_ORF
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'
Added this doctest:
>>> coding_strand_to_AA("ATGAAAGACTGTCCCATTTGA")
'MKDCPI|'
Added the above doctest to test using stop codons in coding_strand_to_AA.
"""
DNA_list = []
AA_list = []
for i in range(len(dna)//3):
DNA_list.append(dna[(i+1)*3-3:(i+1)*3])
for i in range(len(DNA_list)):
AA_list.append(aa_table[DNA_list[i]])
return ''.join(AA_list)
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.
Added this doctest:
>>> gene_finder("ATAGATCCAGTAAGATAGATCCAGTAAGCATAAGTGTGTTAGACAGATCCAGTAAGCATAAGTGTGTTAGACATGAAGCCCTAGCAGATCCAGTAAGCATAAGTGTGTTAGACATAGATCCAGTAAGCATAAGTGTGTTAGACGTAATAGCACACATAGATCCAGTAAGCATAAGTGTGTTAGACAGATCCAGTAAGCATAAGTGTGTTAGACATGAAGCCCTAGCAGATCCAGTAAGCATAAGTGTGTTAGACATAGATCCAGTAAGCATAAGTGTGTTAGACGTAATAGCACACATAGATCCAGTAAGCATAAGTGTGTTAGACAGATCCAGTAAGCATAAGTGTGTTAGACATGAAGCCCTAGCAGATCCAGTAAGCATAAGTGTGTTAGACATAGATCCAGTAAGCATAAGTGTGTTAGACGTAATAGCACACATAGATCCAGTAAGCATAAGTGTGTTAGACAGATCCAGTAAGCATAAGTGTGTTAGACATGAAGCCCTAGCAGATCCAGTAAGCATAAGTGTGTTAGACATAGATCCAGTAAGCATAAGTGTGTTAGACGTAATAGCACACCATAAGTGTGTTAGACAGATCCAGTAAGCATAAGTGTGTTAGACATGAAGCCCTAGCAGATCCAGTAAGCATAAGTGTGTTAGACATAGATCCAGTAAGCATAAGTGTGTTAGACGTAATAGCACAC")
['MLTGSMSNTLMLTGSARASCLTHLCLLDLSNTLMLTGSMCAITSNTLMLTGSMSNTLMLTGSARASCLTHLCLLDLSNTLMLTGSMCAITSNTLMLTGSMSNTLMLTGSARASCLTHLCLLDLSNTLMLTGSMCAITSNTLMLTGSMSNTLMLTGSARASCLTHLCLLDLSNTLMLTGSILLDL']
Added the above doctest to "test" my final gene_finder. However, because so much DNA is needed to "get past" the
threshold, I needed to use my function to add the doctest. So I'm not really sure that it's a proper doctest.
"""
orfs_to_code = []
AA_sequences = []
threshold = longest_ORF_noncoding(dna, 1500)
#print(threshold)
all_ORFs_both_strands = find_all_ORFs_both_strands(dna)
#print(all_ORFs_both_strands)
for orf in all_ORFs_both_strands:
if len(orf) > threshold:
orfs_to_code.append(orf)
#print(orfs_to_code)
for ORF in orfs_to_code:
AA_sequences.append(coding_strand_to_AA(ORF))
return AA_sequences
from load import load_seq
dna = load_seq("./data/X73525.fa")
print(gene_finder(dna))
# if __name__ == "__main__":
# import doctest
# doctest.testmod()
# doctest.run_docstring_examples(find_all_ORFs_both_strands, globals(), verbose=True)