-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analyzer.py
792 lines (701 loc) · 24.6 KB
/
Analyzer.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
__author__ = 'Andrea Rubbi'
import string
import random
import turtle
import urllib.request
dnaletters=['a','t','g','c','A','C','G','T']
rna2amino = {'UUU':'F','UUC':'Y','UUA':'L','GGG':'G',
'UUG':'L','CUU':'L','CUC':'L','CUA':'L',
'CUG':'L','AUU':'I','AUC':'I','AUA':'I',
'AUG':'M','GUU':'V','GUC':'V','GUA':'V',
'GUG':'V','UCU':'S','UCC':'S','UCA':'S',
'UCG':'S','CCU':'P','CCC':'P','CCA':'P',
'CCG':'P','ACU':'T','ACC':'T','ACA':'T',
'ACG':'T','GCU':'A','GCC':'A','GCA':'A',
'GCG':'A','UAU':'Y','CAU':'H','CAC':'H',
'CAA':'Q','CAG':'Q','AAU':'N','AAC':'N',
'AAA':'K','AAG':'K','GAU':'D','GAC':'D',
'GAA':'E','GAG':'E','UGU':'C','UGC':'C',
'UGG':'W','CGU':'R','CGC':'R','CGA':'R',
'CGG':'R','AGU':'S','AGC':'S','AGA':'R',
'AGG':'R','GGU':'G','GGC':'G','GGA':'G',}
symbols={'r':('a','g'),'y':('c','t'),'k':('g','t'),
'm':('a','c'),'s':('c','g'),'w':('a','t'),
'b':('c','g','t'),'d':('a','g','t'),'h':('a','g','t'),
'v':('a','c','g'),'n':('a','c','g','t')}
"""dictionary rna2amino contains all the codon:amino-acid key:value pairs where:
codon: a three-letter string object representing a codon
amino-acid: an upper case one-letter string object representing the coded amino-acid
"""
def gcContent(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
dna=dna.lower()
ratio=(dna.count('c')+dna.count('g'))/len(dna)
return ratio
"""Returns the GC content ratio of a DNA sequence
Parameter:
dna: a string object representing a DNA sequence
Return value: a real number between 0 and 1
Example: gcContent("atcgttcaag") = 0.4
"""
def countCodon(dna, codon):
if type(dna)!=str or type(codon)!=str:
return ('String type required')
if len(dna)==0 or len(codon)==0:
return('No DNA or codon was insert')
for i in dna:
if i not in dnaletters:
return 'This DNA contains invalid characters'
for c in codon:
if c not in dnaletters:
return 'This Codon contains invalid characters'
dna=dna.lower()
codon=codon.lower()
l=counter=0
while l<len(dna):
if dna[l:l+3]==codon:
counter+=1
l+=3
return counter
"""Returns the number of (non-overlapping) occurrences of a codon in a DNA sequence
Parameters:
dna: a string object representing a DNA sequence
codon: a three-letter string object representing the codon to
search for
Return value: the integer number of instances of the target codon in dna
Example: countCodon("aaaaaaaa", "aaa") = 2
"""
def countACG(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
dna=dna.lower()
counter=len(dna)-dna.count('t')
return counter
"""Returns the number of nucleotides that are not T in a DNA sequence
Parameter:
dna: a string object representing a DNA sequence
Return value: the integer number of nucleotides in dna that are not T
Example: countACG("atcgttcaag") = 7
"""
def printCodons(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return ('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
dna=dna.lower()
while len(dna)>=3:
print (dna[:3],end=' ')
dna=dna[3:]
"""Prints the sequence of non-overlapping codons in dna,
Parameter:
dna: a string object representing a DNA sequence
Return value: None
Example: printCodons("ggtacactgta") would print: ggt aca ctg
"""
def kmp_matcher(T,P):
prf=[0]
L = []
u=i=j=0
m=len(P)
n=len(T)
for i in range(1,m):
while u>0 and not P[u] == P[i]:
u = prf[u-1]
if P[u] == P[i]:
u += 1
prf.append(u)
while i < n:
while j > 0 and not P[j] == T[i]:
j = prf[j-1]
if P[j] == T[i]:
if j == m-1:
L.append(i-j)
j = prf[j-1]
else:
j += 1
i += 1
else:
i+=1
return L
def findCodon(dna, codon):
if type(dna)!=str or type(codon)!=str:
return ('String type required')
if len(dna)==0:
return ('No DNA was insert')
if len(codon)==0:
return('No codon was insert')
for j in codon:
if j not in dnaletters:
return('The codon contains invalid characters')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
return kmp_matcher(dna.lower(),codon.lower())
"""Returns the index of the first occurrence of codon in dna
Parameters:
dna: a string object representing a DNA sequence
codon: a three-letter string object representing the codon to
search for
Return value:
(if codon found): the integer index of the first occurrence of codon in dna
(if codon not found): None
Example: findCodon("ggtacactacgta", "tac") = 2
"""
def findATG(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
#dna=dna.lower()
return kmp_matcher(dna.lower(),'atg')
"""Returns a list of all the positions of the codon ATG in dna
Parameter:
dna: a string object representing a DNA sequence
Return value: a list of integer numbers
Example: findATG("gatgtatgta") = [1,5]
"""
def printReadingFrames(dna):
if type(dna)!=str:
return('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
dna=dna.lower()
l=0
while len(dna[l:])>=3:
a=dna[l:l+3]
b=dna[l+1:l+4]
c=dna[l+2:l+5]
if len(dna[l+1:])<3 and len(dna[l+2:])<3:
print(a)
elif len(dna[l+2:])<3 and len(dna[l+1:])>=3:
print(a, b,sep='\t')
else:
print (a,b,c,sep='\t')
l+=3
"""Prints the sequences of non-overlapping codons in the dna
with the three possible reading frames in separate columns
Parameter:
dna: a string object representing a DNA sequence
Return value: None
Example: printReadingFrames("aggcctggc") should print
agg ggc gcc
cct ctg tgg
ggc
"""
def firstSSR(dna, seq):
if type(dna)!=str:
return('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
for c in seq:
if c not in dnaletters:
return 'This sequence contains invalid characters'
#dna=dna.lower()
matching=kmp_matcher(dna.lower(),seq.lower())
n=0
for c in matching:
if n!=0:
if c-cp>len(seq):
break
n+=1
cp=c
return n
"""Returns the length (number of repeats) of the first SSR in dna
that repeats the sequence seq
Parameters:
dna: a string object representing a DNA sequence
seq: a string object representing a short sequence of DNA
Return value:
(if seq found): the integer number of seq repeats in the first SSR
(if seq not found): 0
Example: firstSSR("aggcctggcggcggc", "ggc") = 1
"""
def longestSSR(dna, seq):
if type(dna)!=str:
return('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
for c in seq:
if c not in dnaletters:
return 'This sequence contains invalid characters'
matching=kmp_matcher(dna.lower(),seq.lower())
n=0
SSR=[]
if len(matching)==0:
return
for c in matching:
if n!=0:
if c-cp>len(seq) or c==matching[-1]:
SSR+=[n]
n=1
n+=1
cp=c
n=sorted(SSR,reverse=True)
return n[0]
"""Returns the length of the longest SSR in dna that repeats the sequence seq
Parameters:
dna: a string object representing a DNA sequence
seq: a string object representing a short sequence of DNA
Return value:
(if seq found): the integer length of longest SSR in dna repeating seq
(if seq not found): 0
Example: longestSSR("aggcctggcggcggc", "ggc") = 3
"""
def longestSSRdin(dna):
if type(dna)!=str:
return('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
dna=dna.lower()
pos=('a','c','g','t')
pairs=[]
def thisdinucleotide(dna,din):
i=count=0
longest=0
while i+len(din)<=len(dna):
if dna[i:i+len(din)]==din:
while dna[i:i+len(din)]==din:
count+=1
i+=len(din)
if count>longest:
longest,count=count,0
i+=1
count=0
return longest
for p in pos:
sett=[p+x for x in pos]
for q in sett:
count=thisdinucleotide(dna,q)
pairs+=[(q,count)]
def second ( t ) :
return t [ 1 ]
winner=sorted(pairs, key=second ,reverse=True)
return winner[0]
"""Finds the longest SSR in dna for all the possible dinucleotides
Parameter:
dna: a string object representing a DNA sequence
Return value: (if len(dna)>1): a pair (din, len)
din: a two-letter string object representing the dinucleotide with longest SSR in dna
len: integer representing the length of the longest SSR of din
(if len(dna)<2): None
Example: longestSSRdin("ctctctgcgccacacaca") = ("ca", 4)
"""
def complement(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
dna=dna.lower()
compl=''
complementary={'a':'t','t':'a','g':'c','c':'g'}
for c in dna:
if c in complementary:
compl+=complementary[c]
return compl
"""Returns the complement of a dna sequence
Parameter:
dna: a string object representing a DNA sequence
Return value: a string object representing the complement of the DNA sequence
Example: complement("acgtac") = "tgcatg"
"""
def reverseComplement(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
return complement(dna)[::-1]
"""Returns the reverse complement of a dna sequence
Parameter:
dna: a string object representing a DNA sequence
Return value: a string object representing the reverse complement of the DNA sequence
Example: reverseComplement("acgtac") = "gtacgt"
"""
def palindrome(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
return dna.lower()==reverseComplement(dna)
"""Returns true if dna is the same as its reverse complement
Parameter:
dna: a string object representing a DNA sequence
Return value: Bool
Example: palindrome("atat") = True
"""
def dna2rna(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
dna=dna.lower()
return dna.replace('t','u')
"""Returns a copy of dna in which every "t" has been replaced by a "u"
Parameter:
dna: a string object representing a DNA sequence
Return value: a string object representing an RNA sequence
Example: dna2rna("actgat") = "acugau"
"""
def transcribe(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
return dna2rna(reverseComplement(dna))
"""Returns the RNA equivalent of the reverse complement of dna
Parameter:
dna: a string object representing a DNA sequence
Return value: a string object representing an RNA sequence
Example: transcribe("acgtac") = "guacgu"
"""
def clean(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
dna=dna.lower()
elegant=''
for c in dna:
if c in dnaletters:
elegant+=c
else:
elegant+='n'
return elegant
"""Returns a new DNA string in which every character in dna
that is not an "a", "c", "g", or "t" is replaced with an "n"
Parameter:
dna: a string object representing a DNA sequence
Return value: a string object representing a clean DNA sequence
Example: clean("goat") = "gnat"
"""
def fix(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
dna=dna.lower()
fixed=''
for n in dna:
if n in symbols:
fixed+=random.choice(symbols[n])
else:
fixed+=n
return fixed
"""Returns a DNA string in which each ambiguous symbol is replaced
with one of the possible bases it represents, each with equal probability
Parameter:
dna: a string object representing a DNA sequence with ambiguous simbols
Return value: a string object representing a fixed DNA sequence
Example: fix("arta") returns either "aata" or "agta"
(with probability 1/2 for each one of these two possible returns)
"""
def fixAll(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
fixed=['']
for n in dna.lower():
if n in symbols:
fixed=[x+y for y in symbols[n] for x in fixed]
else:
fixed=[x+n for x in fixed]
return fixed
"""Returns the list containing all possible DNA strings in which each
ambiguous symbol in dna is replaced with all of the possible bases it
represents; all combinations of replacements should be considered and
returned within the list of fixed DNA strings
Parameter:
dna: a string object representing a DNA sequence with ambiguous simbols
Return value: a list of string objects representing fixed DNA sequences
Examples: fixAll("arma") = ["aaaa", "aaca", "agaa", "agca"]
fixAll("agata") = ["agata"]
"""
def readFASTA(filename):
fasta=open(filename,'r')
sequence=''
read=fasta.readlines()
for line in range(1,len(read)):
sequence+=read[line][:-1]
fasta.close()
return sequence
"""Reads a FASTA file and removes the header and all the newline characters
Returns the DNA sequence contained in the file as a string
Parameter:
filename: the name of file containing a DNA sequence in the FASTA format
Return value: a string object representing the DNA sequence in the file
"""
def getFASTA(id):
prefix = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&id='
suffix = '&rettype=fasta&retmode=text'
url = prefix + id + suffix
read_file = urllib.request.urlopen(url)
first_line = read_file.readline()
dna_sequence = ''
for line in read_file:
line = line.decode('utf-8')
dna_sequence += line[:-1]
read_file.close()
return dna_sequence
"""Fetches the DNA sequence with the given id from the NCBI database
and returns it as a string (header and newline characters must be removed)
Parameter:
id: a string object representing the identifier (NCBI accession number) of a DNA sequence
Return value: a string object containing the dna sequence
"""
def findseq(dna, seq):
if len(dna)==0 or len(seq)==0:
return('No DNA or seq was insert')
for i in dna:
if i not in dnaletters:
return 'This DNA contains invalid characters'
for c in seq:
if c not in dnaletters:
return 'This sequence contains invalid characters'
#dna=dna.lower()
return kmp_matcher(dna.lower(),seq.lower())
"""Returns the list of indexes (starting positions) of all the
occurrences of seq in dna
Parameters:
dna: a string object representing a DNA sequence
seq: a string object representing a sequence of DNA
Return value: a list of integer numbers
"""
def mark(dna):
if type(dna)!=str:
return ('String type required')
if len(dna)==0:
return('No DNA was insert')
for i in dna:
if i not in dnaletters:
return 'The DNA sequence contains invalid characters'
dna=dna.lower()
marked=''
x=0
y=0
while x+3<=len(dna):
if y==0 and dna[x:x+3]=='atg':
marked+='>>>'
x+=3
y=1
elif y==1 and (dna[x:x+3]=='taa' or dna[x:x+3]=='tag' or dna[x:x+3]=='tga'):
marked+='<<<'
x+=3
y=0
else:
marked+=dna[x:x+3]
x+=3
return marked
"""Returns a new DNA string in which every start codon (atg) in dna
is replaced with ">>>" and every stop codon (taa, tag, or tga) is
replaced with "<<<"
The function does not consider overlapping codons (but just the
reading frame starting from offset 0)
If two subsequent start codons are found (without a stop codon in
between), only the first one should be replaced with ">>>", since
codon atg also codes for the Methionine amino-acid
Parameter:
dna: a string object representing a DNA sequence
Return value: a string object represnting a marked sequence of DNA
Example: mark("ttgatggagatgcattagaag") = "ttg>>>gagatgcat<<<aag"
"""
def proteins(marked_dna):
if type(marked_dna)!=str:
return ('String type required')
if len(marked_dna)==0:
return('No DNA was insert')
for i in marked_dna:
if i not in dnaletters and i!='<' and i!='>' :
return 'The DNA sequence contains invalid characters'
rna=marked_dna.replace('t','u')
rna=rna.upper()
x=0
protein=[]
while x+3<=len(rna):
if rna[x:x+3]=='>>>':
x+=3
i = ''
while rna[x:x+3]!= '<<<' and x+3<=len(rna):
if rna[x:x+3]=='>>>':
i=''
else:
i+=rna2amino[rna[x:x+3]]
x+=3
protein.append(i)
else:
x+=3
return protein
"""Returns the list of proteins traduced from marked_dna
Proteins are represented as strings of amino-acids
Proteins are obtained from the RNA sequences obtained from
the dna enclosed between the markers ">>>" and "<<<"
Parameter:
marked_dna: a string object representing a marked sequence of DNA
Return Value: a list of string objects representing proteins
Example: proteins("ttg>>>gagcat<<<aagcag>>>aca>>>caccaacag<<<aga") = ["EH","HQQ"]
"""
""" The following lines define the base settings and functions for plotting through the turtle module
You do not need to add any code to the functions plot and bar!
"""
width = 1200 # width of the window
cols = width // 6 # number of columns of text
height = 600 # height of the window
rows = height // 100 # number of rows of text
def plot(tortoise, index, value, window):
"""Plots GC fraction value for window ending at position index."""
if (index == window) or (index - window + 1) // cols != (index - window) // cols:
tortoise.up()
tortoise.goto((index - window + 1) % cols, \
(index - window + 1) // cols + 0.7 + value * 0.25)
tortoise.down()
else:
tortoise.goto((index - window + 1) % cols, \
(index - window + 1) // cols + 0.7 + value * 0.25)
def bar(tortoise, index, rf):
"""Draws a colored bar over codon starting at position index in
reading frame rf. Puts the turtle's pen up and down to
handle line breaks properly."""
tortoise.up()
tortoise.goto(index % cols, index // cols + (rf + 1) / 5)
tortoise.down()
tortoise.forward(1)
tortoise.up()
tortoise.goto((index + 1) % cols, (index + 1) // cols + (rf + 1) / 5)
tortoise.down()
tortoise.forward(1)
tortoise.up()
tortoise.goto((index + 2) % cols, (index + 2) // cols + (rf + 1) / 5)
tortoise.down()
tortoise.forward(1)
"""
Complete the following two functions to accomplish the required tasks
"""
def orf(dna, rf, tortoise):
if type(dna) != str:
return 'The parameter you have inserted is of an invalid type.'
if dna == '':
return 'You have inserted an empty DNA sequence.'
dna = dna.lower()
tortoise.color('red')
i=rf
while i+3 <= len(dna):
m=dna[i:i+3]
if m!='atg':
bar(tortoise,i,rf)
tortoise.color('red')
i+=3
else:
while m not in ('taa','tga','tag') and i+3 <= len(dna):
tortoise.color('blue')
bar(tortoise,i,rf)
i+=3
"""Finds and draws all ORFs in the reading frame rf
Blue bars begin and end on start and stop codons
Parameters:
dna: a string object representing a sequence of DNA
rf: reading frame offset (it's value can be either 0, 1, or 2)
tortoise: the drawing turtle
Return value: None
"""
# YOUR CODE GOES HERE
# to place a bar in the current color over the codon starting at
# position index in reading frame rf, call
# bar(tortoise, index, rf)
def gcFreq(dna, window, tortoise):
"""Computes and plots the GC frequency in dna over a sliding window
Parameters:
dna: a string object representing a sequence of DNA
window: integer size of the sliding window
tortoise: the drawing turtle
Return value: None
"""
# draws red lines at 0.5 above the sequence:
tortoise.pencolor('red')
for index in range(len(dna) // cols + 1):
tortoise.up()
tortoise.goto(0, index + 0.825)
tortoise.down()
if index < len(dna) // cols:
tortoise.goto(cols - 1, index + 0.825)
else:
tortoise.goto((len(dna) - window) % cols, index + 0.825)
tortoise.up()
tortoise.pencolor('blue')
k=0
while k+window<len(dna):
plot(tortoise, window+k, gcContent(dna[k:k+window]), window)
k+=1
# YOUR CODE GOES HERE
# get initial window count
# get subsequent window counts and plot them
# to plot a fraction for the window ending at position index,
# call plot(tortoise, index, fraction, window)
"""
The viewer functuion calls the functions you have coded to build the final plot!
"""
def viewer(dna):
"""Displays GC content and ORFs in 3 forward reading frames."""
dna = dna.upper() # makes everything upper case
tortoise = turtle.Turtle()
screen = tortoise.getscreen()
screen.setup(width, height) # makes a long, thin window
screen.setworldcoordinates(0, 0, cols, rows) # scales coord system so 1 char fits at each point
screen.tracer(100)
tortoise.hideturtle()
tortoise.speed(0)
tortoise.up()
# Prints the DNA string in the window:
for index in range(len(dna)):
tortoise.goto(index % cols, index // cols)
tortoise.write(dna[index], font = ('Courier', 9, 'normal'))
# Finds ORFs in forward reading frames 0, 1, 2:
tortoise.width(5)
for rf in range(3):
orf(dna, rf, tortoise)
# Plots GC frequency:
tortoise.width(1)
gcFreq(dna, 5, tortoise)
screen.update()