Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | plt.style.use('bmh') | 2 | plt.style.use('bmh') | ||
3 | plt.plot( | 3 | plt.plot( | ||
4 | [0, 1, 2], | 4 | [0, 1, 2], | ||
5 | [0, 1, 0] | 5 | [0, 1, 0] | ||
6 | ) | 6 | ) | ||
7 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
8 | plt.ylabel('y'); | 8 | plt.ylabel('y'); | ||
9 | def plot_coords(coords, bare_plot=False): | 9 | def plot_coords(coords, bare_plot=False): | ||
10 | if bare_plot: | 10 | if bare_plot: | ||
11 | plt.axis('off') | 11 | plt.axis('off') | ||
12 | plt.axes().set_aspect('equal', 'datalim') | 12 | plt.axes().set_aspect('equal', 'datalim') | ||
13 | X, Y = zip(*coords) | 13 | X, Y = zip(*coords) | ||
14 | plt.plot(X, Y); | 14 | plt.plot(X, Y); | ||
15 | plot_coords([ | 15 | plot_coords([ | ||
16 | (0, 0), | 16 | (0, 0), | ||
17 | (1, 0), | 17 | (1, 0), | ||
18 | (2, 1), | 18 | (2, 1), | ||
19 | (3, 1), | 19 | (3, 1), | ||
20 | (2, 0) | 20 | (2, 0) | ||
21 | ]) | 21 | ]) | ||
22 | nan = float('nan') | 22 | nan = float('nan') | ||
23 | plot_coords([ | 23 | plot_coords([ | ||
24 | (0, 0), | 24 | (0, 0), | ||
25 | (1, 1), | 25 | (1, 1), | ||
26 | (nan, nan), | 26 | (nan, nan), | ||
27 | (1, 0), | 27 | (1, 0), | ||
28 | (2, 1) | 28 | (2, 1) | ||
29 | ]) | 29 | ]) | ||
30 | from math import pi, sin, cos | 30 | from math import pi, sin, cos | ||
31 | DEGREES_TO_RADIANS = pi / 180 | 31 | DEGREES_TO_RADIANS = pi / 180 | ||
32 | def turtle_to_coords(turtle_program, turn_amount=45): | 32 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
33 | state = (0.0, 0.0, 90.0) | 33 | state = (0.0, 0.0, 90.0) | ||
34 | yield (0.0, 0.0) | 34 | yield (0.0, 0.0) | ||
35 | for command in turtle_program: | 35 | for command in turtle_program: | ||
36 | x, y, angle = state | 36 | x, y, angle = state | ||
37 | if command in 'Ff': | 37 | if command in 'Ff': | ||
38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
39 | y + sin(angle * DEGREES_TO_RADIANS), | 39 | y + sin(angle * DEGREES_TO_RADIANS), | ||
40 | angle) | 40 | angle) | ||
41 | if command == 'f': | 41 | if command == 'f': | ||
42 | yield (float('nan'), float('nan')) | 42 | yield (float('nan'), float('nan')) | ||
43 | yield (state[0], state[1]) | 43 | yield (state[0], state[1]) | ||
44 | elif command == '+': | 44 | elif command == '+': | ||
45 | state = (x, y, angle + turn_amount) | 45 | state = (x, y, angle + turn_amount) | ||
46 | elif command == '-': | 46 | elif command == '-': | ||
47 | state = (x, y, angle - turn_amount) | 47 | state = (x, y, angle - turn_amount) | ||
48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | 48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
50 | from math import isnan | 50 | from math import isnan | ||
51 | def print_coords(coords): | 51 | def print_coords(coords): | ||
52 | for (x, y) in coords: | 52 | for (x, y) in coords: | ||
53 | if isnan(x): | 53 | if isnan(x): | ||
54 | print('<gap>') | 54 | print('<gap>') | ||
55 | else: | 55 | else: | ||
56 | print('({:.2f}, {:.2f})'.format(x, y)) | 56 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | 58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
59 | def transform_sequence(sequence, transformations): | 59 | def transform_sequence(sequence, transformations): | ||
60 | return ''.join(transformations.get(c, c) for c in sequence) | 60 | return ''.join(transformations.get(c, c) for c in sequence) | ||
61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | 61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | ||
62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | 62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
63 | def transform_multiple(sequence, transformations, iterations): | 63 | def transform_multiple(sequence, transformations, iterations): | ||
64 | for _ in range(iterations): | 64 | for _ in range(iterations): | ||
65 | sequence = transform_sequence(sequence, transformations) | 65 | sequence = transform_sequence(sequence, transformations) | ||
66 | return sequence | 66 | return sequence | ||
67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | 67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | ||
68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | 70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
71 | plot_coords(turtle_to_coords(transform_multiple('L', { | 71 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
72 | 'L': '-RF+LFL+FR-', | 72 | 'L': '-RF+LFL+FR-', | ||
73 | 'R': '+LF-RFR-FL+' | 73 | 'R': '+LF-RFR-FL+' | ||
74 | }, 5), 90)) | 74 | }, 5), 90)) | ||
75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | 75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
76 | saved_states = list() | 76 | saved_states = list() | ||
77 | state = (0, 0, 90) | 77 | state = (0, 0, 90) | ||
78 | yield (0, 0) | 78 | yield (0, 0) | ||
79 | for command in turtle_program: | 79 | for command in turtle_program: | ||
80 | x, y, angle = state | 80 | x, y, angle = state | ||
81 | if command.lower() in 'abcdefghij': | 81 | if command.lower() in 'abcdefghij': | ||
82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
83 | y + sin(angle * DEGREES_TO_RADIANS), | 83 | y + sin(angle * DEGREES_TO_RADIANS), | ||
84 | angle) | 84 | angle) | ||
85 | if command.islower(): | 85 | if command.islower(): | ||
86 | yield (float('nan'), float('nan')) | 86 | yield (float('nan'), float('nan')) | ||
87 | yield (state[0], state[1]) | 87 | yield (state[0], state[1]) | ||
88 | elif command == '+': | 88 | elif command == '+': | ||
89 | state = (x, y, angle + turn_amount) | 89 | state = (x, y, angle + turn_amount) | ||
90 | elif command == '-': | 90 | elif command == '-': | ||
91 | state = (x, y, angle - turn_amount) | 91 | state = (x, y, angle - turn_amount) | ||
92 | elif command == '[': | 92 | elif command == '[': | ||
93 | saved_states.append(state) | 93 | saved_states.append(state) | ||
94 | elif command == ']': | 94 | elif command == ']': | ||
95 | state = saved_states.pop() | 95 | state = saved_states.pop() | ||
96 | yield (float('nan'), float('nan')) | 96 | yield (float('nan'), float('nan')) | ||
97 | x, y, _ = state | 97 | x, y, _ = state | ||
98 | yield (x, y) | 98 | yield (x, y) | ||
99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | 99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
100 | def l_plot(axiom, transformations, iterations=0, angle=45): | 100 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
101 | turtle_program = transform_multiple(axiom, transformations, iterations) | 101 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
102 | coords = branching_turtle_to_coords(turtle_program, angle) | 102 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
103 | plot_coords(coords, bare_plot=True) | 103 | plot_coords(coords, bare_plot=True) | ||
104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | 104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | ||
105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
106 | for i in range(5): | 106 | for i in range(5): | ||
107 | print('{}: '.format(i), | 107 | print('{}: '.format(i), | ||
108 | transform_multiple('A', {'A': 'F+A'}, i)) | 108 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
109 | for i in range(5): | 109 | for i in range(5): | ||
110 | print('{}: '.format(i), | 110 | print('{}: '.format(i), | ||
111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | 111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | ||
113 | def __init__(self): | 113 | def __init__(self): | ||
n | 114 | self.instances = [] | n | 114 | self.instances = [] |
115 | self.consensus=[] | 115 | self.consensus=[] | ||
116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | 116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | ||
117 | def __str__ (self): | 117 | def __str__ (self): | ||
118 | string = "" | 118 | string = "" | ||
119 | for i in self.instances: | 119 | for i in self.instances: | ||
n | 120 | string = string + str(i) + "\n" | n | 120 | string += i + "\n" |
121 | return string[:-2] | 121 | return string[:0] | ||
122 | def __len__(self): | 122 | def __len__(self): | ||
n | 123 | return (len(self.instances)-4) | n | 123 | return len (self.consensus) |
124 | def count(self): | 124 | def count(self): | ||
125 | for i in self.instances: | 125 | for i in self.instances: | ||
126 | temp = i.upper() | 126 | temp = i.upper() | ||
n | 127 | self.counts["A"].append(temp.count("A")) | n | 127 | self.counts["A"].append(temp.count("A")) |
128 | self.counts["C"].append(temp.count("C")) | 128 | self.counts["C"].append(temp.count("C")) | ||
129 | self.counts["G"].append(temp.count("G")) | 129 | self.counts["G"].append(temp.count("G")) | ||
130 | self.counts["T"].append(temp.count("T")) | 130 | self.counts["T"].append(temp.count("T")) | ||
131 | def compute_consensus(self): | 131 | def compute_consensus(self): | ||
132 | A = self.counts["A"] | 132 | A = self.counts["A"] | ||
133 | C = self.counts["C"] | 133 | C = self.counts["C"] | ||
134 | G = self.counts["G"] | 134 | G = self.counts["G"] | ||
n | 135 | T = self.counts["T"] | n | 135 | T = self.counts["T"] |
136 | for i in range(len(A)): | 136 | for i in range (len(A)): | ||
137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
138 | self.consensus.append("A") | 138 | self.consensus.append("A") | ||
139 | elif (C[i] >= G[i] and C[i] >= T[i]): | 139 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
140 | self.consensus.append("C") | 140 | self.consensus.append("C") | ||
141 | elif (G[i] >= T[i]): | 141 | elif (G[i] >= T[i]): | ||
142 | self.consensus.append("G") | 142 | self.consensus.append("G") | ||
143 | else: | 143 | else: | ||
144 | self.consensus.append("T") | 144 | self.consensus.append("T") | ||
n | 145 | for x in self.consensus: | n | ||
146 | print(x,end='') | ||||
147 | def parse(self, filename): | 145 | def parse(self, filename): | ||
148 | with open(filename,'r') as f: | 146 | with open(filename,'r') as f: | ||
149 | for i in f: | 147 | for i in f: | ||
150 | if ">" in i: | 148 | if ">" in i: | ||
t | 151 | continue | t | 149 | continue |
152 | else: | 150 | else: | ||
153 | self.instances.append(i) | 151 | self.instances.append(i) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | plt.style.use('bmh') | 2 | plt.style.use('bmh') | ||
3 | plt.plot( | 3 | plt.plot( | ||
4 | [0, 1, 2], | 4 | [0, 1, 2], | ||
5 | [0, 1, 0] | 5 | [0, 1, 0] | ||
6 | ) | 6 | ) | ||
7 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
8 | plt.ylabel('y'); | 8 | plt.ylabel('y'); | ||
9 | def plot_coords(coords, bare_plot=False): | 9 | def plot_coords(coords, bare_plot=False): | ||
10 | if bare_plot: | 10 | if bare_plot: | ||
11 | plt.axis('off') | 11 | plt.axis('off') | ||
12 | plt.axes().set_aspect('equal', 'datalim') | 12 | plt.axes().set_aspect('equal', 'datalim') | ||
13 | X, Y = zip(*coords) | 13 | X, Y = zip(*coords) | ||
14 | plt.plot(X, Y); | 14 | plt.plot(X, Y); | ||
15 | plot_coords([ | 15 | plot_coords([ | ||
16 | (0, 0), | 16 | (0, 0), | ||
17 | (1, 0), | 17 | (1, 0), | ||
18 | (2, 1), | 18 | (2, 1), | ||
19 | (3, 1), | 19 | (3, 1), | ||
20 | (2, 0) | 20 | (2, 0) | ||
21 | ]) | 21 | ]) | ||
22 | nan = float('nan') | 22 | nan = float('nan') | ||
23 | plot_coords([ | 23 | plot_coords([ | ||
24 | (0, 0), | 24 | (0, 0), | ||
25 | (1, 1), | 25 | (1, 1), | ||
26 | (nan, nan), | 26 | (nan, nan), | ||
27 | (1, 0), | 27 | (1, 0), | ||
28 | (2, 1) | 28 | (2, 1) | ||
29 | ]) | 29 | ]) | ||
30 | from math import pi, sin, cos | 30 | from math import pi, sin, cos | ||
31 | DEGREES_TO_RADIANS = pi / 180 | 31 | DEGREES_TO_RADIANS = pi / 180 | ||
32 | def turtle_to_coords(turtle_program, turn_amount=45): | 32 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
33 | state = (0.0, 0.0, 90.0) | 33 | state = (0.0, 0.0, 90.0) | ||
34 | yield (0.0, 0.0) | 34 | yield (0.0, 0.0) | ||
35 | for command in turtle_program: | 35 | for command in turtle_program: | ||
36 | x, y, angle = state | 36 | x, y, angle = state | ||
37 | if command in 'Ff': | 37 | if command in 'Ff': | ||
38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
39 | y + sin(angle * DEGREES_TO_RADIANS), | 39 | y + sin(angle * DEGREES_TO_RADIANS), | ||
40 | angle) | 40 | angle) | ||
41 | if command == 'f': | 41 | if command == 'f': | ||
42 | yield (float('nan'), float('nan')) | 42 | yield (float('nan'), float('nan')) | ||
43 | yield (state[0], state[1]) | 43 | yield (state[0], state[1]) | ||
44 | elif command == '+': | 44 | elif command == '+': | ||
45 | state = (x, y, angle + turn_amount) | 45 | state = (x, y, angle + turn_amount) | ||
46 | elif command == '-': | 46 | elif command == '-': | ||
47 | state = (x, y, angle - turn_amount) | 47 | state = (x, y, angle - turn_amount) | ||
48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | 48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
50 | from math import isnan | 50 | from math import isnan | ||
51 | def print_coords(coords): | 51 | def print_coords(coords): | ||
52 | for (x, y) in coords: | 52 | for (x, y) in coords: | ||
53 | if isnan(x): | 53 | if isnan(x): | ||
54 | print('<gap>') | 54 | print('<gap>') | ||
55 | else: | 55 | else: | ||
56 | print('({:.2f}, {:.2f})'.format(x, y)) | 56 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | 58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
59 | def transform_sequence(sequence, transformations): | 59 | def transform_sequence(sequence, transformations): | ||
60 | return ''.join(transformations.get(c, c) for c in sequence) | 60 | return ''.join(transformations.get(c, c) for c in sequence) | ||
61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | 61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | ||
62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | 62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
63 | def transform_multiple(sequence, transformations, iterations): | 63 | def transform_multiple(sequence, transformations, iterations): | ||
64 | for _ in range(iterations): | 64 | for _ in range(iterations): | ||
65 | sequence = transform_sequence(sequence, transformations) | 65 | sequence = transform_sequence(sequence, transformations) | ||
66 | return sequence | 66 | return sequence | ||
67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | 67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | ||
68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | 70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
71 | plot_coords(turtle_to_coords(transform_multiple('L', { | 71 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
72 | 'L': '-RF+LFL+FR-', | 72 | 'L': '-RF+LFL+FR-', | ||
73 | 'R': '+LF-RFR-FL+' | 73 | 'R': '+LF-RFR-FL+' | ||
74 | }, 5), 90)) | 74 | }, 5), 90)) | ||
75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | 75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
76 | saved_states = list() | 76 | saved_states = list() | ||
77 | state = (0, 0, 90) | 77 | state = (0, 0, 90) | ||
78 | yield (0, 0) | 78 | yield (0, 0) | ||
79 | for command in turtle_program: | 79 | for command in turtle_program: | ||
80 | x, y, angle = state | 80 | x, y, angle = state | ||
81 | if command.lower() in 'abcdefghij': | 81 | if command.lower() in 'abcdefghij': | ||
82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
83 | y + sin(angle * DEGREES_TO_RADIANS), | 83 | y + sin(angle * DEGREES_TO_RADIANS), | ||
84 | angle) | 84 | angle) | ||
85 | if command.islower(): | 85 | if command.islower(): | ||
86 | yield (float('nan'), float('nan')) | 86 | yield (float('nan'), float('nan')) | ||
87 | yield (state[0], state[1]) | 87 | yield (state[0], state[1]) | ||
88 | elif command == '+': | 88 | elif command == '+': | ||
89 | state = (x, y, angle + turn_amount) | 89 | state = (x, y, angle + turn_amount) | ||
90 | elif command == '-': | 90 | elif command == '-': | ||
91 | state = (x, y, angle - turn_amount) | 91 | state = (x, y, angle - turn_amount) | ||
92 | elif command == '[': | 92 | elif command == '[': | ||
93 | saved_states.append(state) | 93 | saved_states.append(state) | ||
94 | elif command == ']': | 94 | elif command == ']': | ||
95 | state = saved_states.pop() | 95 | state = saved_states.pop() | ||
96 | yield (float('nan'), float('nan')) | 96 | yield (float('nan'), float('nan')) | ||
97 | x, y, _ = state | 97 | x, y, _ = state | ||
98 | yield (x, y) | 98 | yield (x, y) | ||
99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | 99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
100 | def l_plot(axiom, transformations, iterations=0, angle=45): | 100 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
101 | turtle_program = transform_multiple(axiom, transformations, iterations) | 101 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
102 | coords = branching_turtle_to_coords(turtle_program, angle) | 102 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
103 | plot_coords(coords, bare_plot=True) | 103 | plot_coords(coords, bare_plot=True) | ||
104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | 104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | ||
105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
106 | for i in range(5): | 106 | for i in range(5): | ||
107 | print('{}: '.format(i), | 107 | print('{}: '.format(i), | ||
108 | transform_multiple('A', {'A': 'F+A'}, i)) | 108 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
109 | for i in range(5): | 109 | for i in range(5): | ||
110 | print('{}: '.format(i), | 110 | print('{}: '.format(i), | ||
111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | 111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | ||
113 | def __init__(self): | 113 | def __init__(self): | ||
n | 114 | self.instances = [] | n | 114 | self.instances = [] |
115 | self.consensus=[] | 115 | self.consensus=[] | ||
116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | 116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | ||
117 | def __str__ (self): | 117 | def __str__ (self): | ||
118 | string = "" | 118 | string = "" | ||
119 | for i in self.instances: | 119 | for i in self.instances: | ||
n | 120 | string += i + "\n" | n | 120 | string = string + str(i) + "\n" |
121 | return string[:0] | 121 | return string[:-2] | ||
122 | def __len__(self): | 122 | def __len__(self): | ||
n | 123 | return len (self.consensus) | n | 123 | return (len(self.instances)-4) |
124 | def count(self): | 124 | def count(self): | ||
125 | for i in self.instances: | 125 | for i in self.instances: | ||
126 | temp = i.upper() | 126 | temp = i.upper() | ||
n | 127 | self.counts["A"].append(temp.count("A")) | n | 127 | self.counts["A"].append(temp.count("A")) |
128 | self.counts["C"].append(temp.count("C")) | 128 | self.counts["C"].append(temp.count("C")) | ||
129 | self.counts["G"].append(temp.count("G")) | 129 | self.counts["G"].append(temp.count("G")) | ||
130 | self.counts["T"].append(temp.count("T")) | 130 | self.counts["T"].append(temp.count("T")) | ||
131 | def compute_consensus(self): | 131 | def compute_consensus(self): | ||
132 | A = self.counts["A"] | 132 | A = self.counts["A"] | ||
133 | C = self.counts["C"] | 133 | C = self.counts["C"] | ||
134 | G = self.counts["G"] | 134 | G = self.counts["G"] | ||
n | 135 | T = self.counts["T"] | n | 135 | T = self.counts["T"] |
136 | for i in range (len(A)): | 136 | for i in range(len(A)): | ||
137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
138 | self.consensus.append("A") | 138 | self.consensus.append("A") | ||
139 | elif (C[i] >= G[i] and C[i] >= T[i]): | 139 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
140 | self.consensus.append("C") | 140 | self.consensus.append("C") | ||
141 | elif (G[i] >= T[i]): | 141 | elif (G[i] >= T[i]): | ||
142 | self.consensus.append("G") | 142 | self.consensus.append("G") | ||
143 | else: | 143 | else: | ||
144 | self.consensus.append("T") | 144 | self.consensus.append("T") | ||
n | n | 145 | for x in self.consensus: | ||
146 | print(x,end='') | ||||
145 | def parse(self, filename): | 147 | def parse(self, filename): | ||
146 | with open(filename,'r') as f: | 148 | with open(filename,'r') as f: | ||
147 | for i in f: | 149 | for i in f: | ||
148 | if ">" in i: | 150 | if ">" in i: | ||
t | 149 | continue | t | 151 | continue |
150 | else: | 152 | else: | ||
151 | self.instances.append(i) | 153 | self.instances.append(i) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | plt.style.use('bmh') | 2 | plt.style.use('bmh') | ||
3 | plt.plot( | 3 | plt.plot( | ||
4 | [0, 1, 2], | 4 | [0, 1, 2], | ||
5 | [0, 1, 0] | 5 | [0, 1, 0] | ||
6 | ) | 6 | ) | ||
7 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
8 | plt.ylabel('y'); | 8 | plt.ylabel('y'); | ||
9 | def plot_coords(coords, bare_plot=False): | 9 | def plot_coords(coords, bare_plot=False): | ||
10 | if bare_plot: | 10 | if bare_plot: | ||
11 | plt.axis('off') | 11 | plt.axis('off') | ||
12 | plt.axes().set_aspect('equal', 'datalim') | 12 | plt.axes().set_aspect('equal', 'datalim') | ||
13 | X, Y = zip(*coords) | 13 | X, Y = zip(*coords) | ||
14 | plt.plot(X, Y); | 14 | plt.plot(X, Y); | ||
15 | plot_coords([ | 15 | plot_coords([ | ||
16 | (0, 0), | 16 | (0, 0), | ||
17 | (1, 0), | 17 | (1, 0), | ||
18 | (2, 1), | 18 | (2, 1), | ||
19 | (3, 1), | 19 | (3, 1), | ||
20 | (2, 0) | 20 | (2, 0) | ||
21 | ]) | 21 | ]) | ||
22 | nan = float('nan') | 22 | nan = float('nan') | ||
23 | plot_coords([ | 23 | plot_coords([ | ||
24 | (0, 0), | 24 | (0, 0), | ||
25 | (1, 1), | 25 | (1, 1), | ||
26 | (nan, nan), | 26 | (nan, nan), | ||
27 | (1, 0), | 27 | (1, 0), | ||
28 | (2, 1) | 28 | (2, 1) | ||
29 | ]) | 29 | ]) | ||
30 | from math import pi, sin, cos | 30 | from math import pi, sin, cos | ||
31 | DEGREES_TO_RADIANS = pi / 180 | 31 | DEGREES_TO_RADIANS = pi / 180 | ||
32 | def turtle_to_coords(turtle_program, turn_amount=45): | 32 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
33 | state = (0.0, 0.0, 90.0) | 33 | state = (0.0, 0.0, 90.0) | ||
34 | yield (0.0, 0.0) | 34 | yield (0.0, 0.0) | ||
35 | for command in turtle_program: | 35 | for command in turtle_program: | ||
36 | x, y, angle = state | 36 | x, y, angle = state | ||
37 | if command in 'Ff': | 37 | if command in 'Ff': | ||
38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
39 | y + sin(angle * DEGREES_TO_RADIANS), | 39 | y + sin(angle * DEGREES_TO_RADIANS), | ||
40 | angle) | 40 | angle) | ||
41 | if command == 'f': | 41 | if command == 'f': | ||
42 | yield (float('nan'), float('nan')) | 42 | yield (float('nan'), float('nan')) | ||
43 | yield (state[0], state[1]) | 43 | yield (state[0], state[1]) | ||
44 | elif command == '+': | 44 | elif command == '+': | ||
45 | state = (x, y, angle + turn_amount) | 45 | state = (x, y, angle + turn_amount) | ||
46 | elif command == '-': | 46 | elif command == '-': | ||
47 | state = (x, y, angle - turn_amount) | 47 | state = (x, y, angle - turn_amount) | ||
48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | 48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
50 | from math import isnan | 50 | from math import isnan | ||
51 | def print_coords(coords): | 51 | def print_coords(coords): | ||
52 | for (x, y) in coords: | 52 | for (x, y) in coords: | ||
53 | if isnan(x): | 53 | if isnan(x): | ||
54 | print('<gap>') | 54 | print('<gap>') | ||
55 | else: | 55 | else: | ||
56 | print('({:.2f}, {:.2f})'.format(x, y)) | 56 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | 58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
59 | def transform_sequence(sequence, transformations): | 59 | def transform_sequence(sequence, transformations): | ||
60 | return ''.join(transformations.get(c, c) for c in sequence) | 60 | return ''.join(transformations.get(c, c) for c in sequence) | ||
61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | 61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | ||
62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | 62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
63 | def transform_multiple(sequence, transformations, iterations): | 63 | def transform_multiple(sequence, transformations, iterations): | ||
64 | for _ in range(iterations): | 64 | for _ in range(iterations): | ||
65 | sequence = transform_sequence(sequence, transformations) | 65 | sequence = transform_sequence(sequence, transformations) | ||
66 | return sequence | 66 | return sequence | ||
67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | 67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | ||
68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | 70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
71 | plot_coords(turtle_to_coords(transform_multiple('L', { | 71 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
72 | 'L': '-RF+LFL+FR-', | 72 | 'L': '-RF+LFL+FR-', | ||
73 | 'R': '+LF-RFR-FL+' | 73 | 'R': '+LF-RFR-FL+' | ||
74 | }, 5), 90)) | 74 | }, 5), 90)) | ||
75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | 75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
76 | saved_states = list() | 76 | saved_states = list() | ||
77 | state = (0, 0, 90) | 77 | state = (0, 0, 90) | ||
78 | yield (0, 0) | 78 | yield (0, 0) | ||
79 | for command in turtle_program: | 79 | for command in turtle_program: | ||
80 | x, y, angle = state | 80 | x, y, angle = state | ||
81 | if command.lower() in 'abcdefghij': | 81 | if command.lower() in 'abcdefghij': | ||
82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
83 | y + sin(angle * DEGREES_TO_RADIANS), | 83 | y + sin(angle * DEGREES_TO_RADIANS), | ||
84 | angle) | 84 | angle) | ||
85 | if command.islower(): | 85 | if command.islower(): | ||
86 | yield (float('nan'), float('nan')) | 86 | yield (float('nan'), float('nan')) | ||
87 | yield (state[0], state[1]) | 87 | yield (state[0], state[1]) | ||
88 | elif command == '+': | 88 | elif command == '+': | ||
89 | state = (x, y, angle + turn_amount) | 89 | state = (x, y, angle + turn_amount) | ||
90 | elif command == '-': | 90 | elif command == '-': | ||
91 | state = (x, y, angle - turn_amount) | 91 | state = (x, y, angle - turn_amount) | ||
92 | elif command == '[': | 92 | elif command == '[': | ||
93 | saved_states.append(state) | 93 | saved_states.append(state) | ||
94 | elif command == ']': | 94 | elif command == ']': | ||
95 | state = saved_states.pop() | 95 | state = saved_states.pop() | ||
96 | yield (float('nan'), float('nan')) | 96 | yield (float('nan'), float('nan')) | ||
97 | x, y, _ = state | 97 | x, y, _ = state | ||
98 | yield (x, y) | 98 | yield (x, y) | ||
99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | 99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
100 | def l_plot(axiom, transformations, iterations=0, angle=45): | 100 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
101 | turtle_program = transform_multiple(axiom, transformations, iterations) | 101 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
102 | coords = branching_turtle_to_coords(turtle_program, angle) | 102 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
103 | plot_coords(coords, bare_plot=True) | 103 | plot_coords(coords, bare_plot=True) | ||
104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | 104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | ||
105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
106 | for i in range(5): | 106 | for i in range(5): | ||
107 | print('{}: '.format(i), | 107 | print('{}: '.format(i), | ||
108 | transform_multiple('A', {'A': 'F+A'}, i)) | 108 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
109 | for i in range(5): | 109 | for i in range(5): | ||
110 | print('{}: '.format(i), | 110 | print('{}: '.format(i), | ||
111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | 111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | ||
113 | def __init__(self): | 113 | def __init__(self): | ||
114 | self.instances = [] | 114 | self.instances = [] | ||
115 | self.consensus=[] | 115 | self.consensus=[] | ||
116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | 116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | ||
117 | def __str__ (self): | 117 | def __str__ (self): | ||
118 | string = "" | 118 | string = "" | ||
119 | for i in self.instances: | 119 | for i in self.instances: | ||
n | 120 | string += i + "\n" | n | 120 | string = string + str(i) + "\n" |
121 | return string[:-2] | 121 | return string[:-2] | ||
122 | def __len__(self): | 122 | def __len__(self): | ||
n | 123 | return len(self.instances) | n | 123 | return (len(self.instances)-4) |
124 | def count(self): | 124 | def count(self): | ||
125 | for i in self.instances: | 125 | for i in self.instances: | ||
126 | temp = i.upper() | 126 | temp = i.upper() | ||
127 | self.counts["A"].append(temp.count("A")) | 127 | self.counts["A"].append(temp.count("A")) | ||
128 | self.counts["C"].append(temp.count("C")) | 128 | self.counts["C"].append(temp.count("C")) | ||
129 | self.counts["G"].append(temp.count("G")) | 129 | self.counts["G"].append(temp.count("G")) | ||
130 | self.counts["T"].append(temp.count("T")) | 130 | self.counts["T"].append(temp.count("T")) | ||
131 | def compute_consensus(self): | 131 | def compute_consensus(self): | ||
132 | A = self.counts["A"] | 132 | A = self.counts["A"] | ||
133 | C = self.counts["C"] | 133 | C = self.counts["C"] | ||
134 | G = self.counts["G"] | 134 | G = self.counts["G"] | ||
135 | T = self.counts["T"] | 135 | T = self.counts["T"] | ||
136 | for i in range(len(A)): | 136 | for i in range(len(A)): | ||
137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
138 | self.consensus.append("A") | 138 | self.consensus.append("A") | ||
139 | elif (C[i] >= G[i] and C[i] >= T[i]): | 139 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
140 | self.consensus.append("C") | 140 | self.consensus.append("C") | ||
141 | elif (G[i] >= T[i]): | 141 | elif (G[i] >= T[i]): | ||
142 | self.consensus.append("G") | 142 | self.consensus.append("G") | ||
143 | else: | 143 | else: | ||
144 | self.consensus.append("T") | 144 | self.consensus.append("T") | ||
n | n | 145 | for x in self.consensus: | ||
146 | print(x,end='') | ||||
145 | def parse(self, filename): | 147 | def parse(self, filename): | ||
146 | with open(filename,'r') as f: | 148 | with open(filename,'r') as f: | ||
147 | for i in f: | 149 | for i in f: | ||
148 | if ">" in i: | 150 | if ">" in i: | ||
149 | continue | 151 | continue | ||
150 | else: | 152 | else: | ||
151 | self.instances.append(i) | 153 | self.instances.append(i) | ||
t | 152 | if __name__=='__main__': | t | ||
153 | lex=DNAMOTIF() | ||||
154 | lex.parse('lexA.fasta') | ||||
155 | print("length",len(lex)) | ||||
156 | print(lex) | ||||
157 | print("consensus",lex.consensus) | ||||
158 | print('seq',lex.counts) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | n | 2 | import matplotlib.pyplot as plt | ||
2 | class PLANT: | 3 | class PLANT: | ||
n | 3 | def __init__(self, initial , gen = {}, p = 0, deltaTheta = 0): | n | 4 | def __init__ (self, initial , gen = {}, n = 0, deltaTheta = 0): |
4 | self.initial = initial | 5 | self.initial=initial | ||
5 | self.gen = gen | 6 | self.gen=gen | ||
6 | self.p = p | 7 | self.n=n | ||
7 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta=deltaTheta | ||
8 | self.str = initial | 9 | self.str=initial | ||
9 | while p>0: | 10 | while n>0: | ||
10 | p -= 1 | 11 | n -= 1 | ||
11 | y = '' | 12 | y = "" | ||
12 | for i in self.str: | 13 | for i in self.str: | ||
13 | if i in gen: | 14 | if i in gen: | ||
14 | y += gen[i] | 15 | y += gen[i] | ||
15 | else: | 16 | else: | ||
16 | y += i | 17 | y += i | ||
17 | self.str = y | 18 | self.str = y | ||
18 | def drawPlant(self): | 19 | def drawPlant(self): | ||
19 | x = [] | 20 | x = [] | ||
20 | for i in self.str: | 21 | for i in self.str: | ||
n | 21 | if i == '[': | n | 22 | if i == "[": |
22 | x.append([currentPt,theta]) | 23 | x.append([initialPt,theta]) | ||
23 | elif i == ']': | 24 | elif i == "]": | ||
24 | currentPt = x[-1][0] | 25 | initiaPt = x[-1][0] | ||
25 | theta = x[-1][1] | 26 | theta = x[-1][1] | ||
n | 26 | elif i == '+': | n | 27 | elif i == "+": |
27 | theta += self.deltaTheta | 28 | theta += self.deltaTheta | ||
n | 28 | elif i == '-': | n | 29 | elif i == "-": |
29 | theta -= self.deltaTheta | 30 | theta -= self.deltaTheta | ||
30 | currentPt=(200,0) | 31 | currentPt=(200,0) | ||
31 | theta = 90 | 32 | theta = 90 | ||
n | 32 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.sin(theta) | n | 33 | nextPt = (initialPt[0] + math.cos(theta), initialPt[1] + math.sin(theta) |
> | ) | > | ) | ||
33 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color='black' | 34 | plt.plot([initialPt[0],nextPt[0]],[initialPt[1],nextPt[1]],color='black' | ||
> | ) | > | ) | ||
34 | currentPt=nextPt | 35 | currentPt=nextPt | ||
35 | x = [] | 36 | x = [] | ||
36 | for i in self.str: | 37 | for i in self.str: | ||
n | 37 | if i == '[': | n | 38 | if i == "[": |
38 | x.append([currentPt,theta]) | 39 | x.append([initialPt,theta]) | ||
39 | elif i == ']': | 40 | elif i == "]": | ||
40 | currentPt = x[-1][0] | 41 | initialPt = x[-1][0] | ||
41 | theta = x[-1][1] | 42 | theta = x[-1][1] | ||
n | 42 | elif i == '+': | n | 43 | elif i == "+": |
43 | theta += self.deltaTheta | 44 | theta += self.deltaTheta | ||
t | 44 | elif i == '-': | t | 45 | elif i == "-": |
45 | theta -= self.deltaTheta | 46 | theta -= self.deltaTheta | ||
46 | class DNAMOTIF: | 47 | class DNAMOTIF: | ||
47 | def __init__(self): | 48 | def __init__(self): | ||
48 | self.instances=[] | 49 | self.instances=[] | ||
49 | self.consensus=[] | 50 | self.consensus=[] | ||
50 | self.counts= {"A": [], "C": [], "G":[],"T":[]} | 51 | self.counts= {"A": [], "C": [], "G":[],"T":[]} | ||
51 | def __str__(self): | 52 | def __str__(self): | ||
52 | string = "" | 53 | string = "" | ||
53 | for i in self.instances: | 54 | for i in self.instances: | ||
54 | string += i + "" | 55 | string += i + "" | ||
55 | return string[:-2] | 56 | return string[:-2] | ||
56 | def __len__(self): | 57 | def __len__(self): | ||
57 | return len(self.instances[0])-1 | 58 | return len(self.instances[0])-1 | ||
58 | def count(self): | 59 | def count(self): | ||
59 | i = 0 | 60 | i = 0 | ||
60 | count = 0 | 61 | count = 0 | ||
61 | up = [] | 62 | up = [] | ||
62 | do_again = [] | 63 | do_again = [] | ||
63 | A = 0 | 64 | A = 0 | ||
64 | C = 0 | 65 | C = 0 | ||
65 | T = 0 | 66 | T = 0 | ||
66 | G = 0 | 67 | G = 0 | ||
67 | while i < len(self.instances[0])-1: | 68 | while i < len(self.instances[0])-1: | ||
68 | while count < len(self.instances): | 69 | while count < len(self.instances): | ||
69 | for amino in self.instances: | 70 | for amino in self.instances: | ||
70 | amino = amino.upper() | 71 | amino = amino.upper() | ||
71 | up.append(amino[i]) | 72 | up.append(amino[i]) | ||
72 | count += 1 | 73 | count += 1 | ||
73 | count = 0 | 74 | count = 0 | ||
74 | do_again.append(up) | 75 | do_again.append(up) | ||
75 | up = [] | 76 | up = [] | ||
76 | i += 1 | 77 | i += 1 | ||
77 | for DNA in do_again: | 78 | for DNA in do_again: | ||
78 | for an in DNA: | 79 | for an in DNA: | ||
79 | if an == 'A': | 80 | if an == 'A': | ||
80 | A += 1 | 81 | A += 1 | ||
81 | if an == 'C': | 82 | if an == 'C': | ||
82 | C += 1 | 83 | C += 1 | ||
83 | if an == 'T': | 84 | if an == 'T': | ||
84 | T += 1 | 85 | T += 1 | ||
85 | if an == 'G': | 86 | if an == 'G': | ||
86 | G += 1 | 87 | G += 1 | ||
87 | self.counts.setdefault('A',[]).append(A) | 88 | self.counts.setdefault('A',[]).append(A) | ||
88 | self.counts.setdefault('C',[]).append(C) | 89 | self.counts.setdefault('C',[]).append(C) | ||
89 | self.counts.setdefault('T',[]).append(T) | 90 | self.counts.setdefault('T',[]).append(T) | ||
90 | self.counts.setdefault('G',[]).append(G) | 91 | self.counts.setdefault('G',[]).append(G) | ||
91 | A = 0 | 92 | A = 0 | ||
92 | C = 0 | 93 | C = 0 | ||
93 | T = 0 | 94 | T = 0 | ||
94 | G = 0 | 95 | G = 0 | ||
95 | def compute_consensus(self): | 96 | def compute_consensus(self): | ||
96 | self.count() | 97 | self.count() | ||
97 | A = self.counts["A"] | 98 | A = self.counts["A"] | ||
98 | C = self.counts["C"] | 99 | C = self.counts["C"] | ||
99 | G = self.counts["G"] | 100 | G = self.counts["G"] | ||
100 | T = self.counts["T"] | 101 | T = self.counts["T"] | ||
101 | for amino in range(len(A)): | 102 | for amino in range(len(A)): | ||
102 | if(A[amino]>= C[amino] and A[amino] >= G[amino] and A[amino] >= T[a | 103 | if(A[amino]>= C[amino] and A[amino] >= G[amino] and A[amino] >= T[a | ||
> | mino]): | > | mino]): | ||
103 | self.consensus.append("A") | 104 | self.consensus.append("A") | ||
104 | elif (C[amino] >= G[amino] and C[amino] >= T[amino]): | 105 | elif (C[amino] >= G[amino] and C[amino] >= T[amino]): | ||
105 | self.consensus.append("C") | 106 | self.consensus.append("C") | ||
106 | elif (G[amino] >= T[amino]): | 107 | elif (G[amino] >= T[amino]): | ||
107 | self.consensus.append("G") | 108 | self.consensus.append("G") | ||
108 | else: | 109 | else: | ||
109 | self.consensus.append("T") | 110 | self.consensus.append("T") | ||
110 | self.consensus = "".join(self.consensus) | 111 | self.consensus = "".join(self.consensus) | ||
111 | def parse(self, filename): | 112 | def parse(self, filename): | ||
112 | with open(filename,'r') as f: | 113 | with open(filename,'r') as f: | ||
113 | for i in f: | 114 | for i in f: | ||
114 | if ">" in i: | 115 | if ">" in i: | ||
115 | continue | 116 | continue | ||
116 | else: | 117 | else: | ||
117 | self.instances.append(i) | 118 | self.instances.append(i) | ||
118 | lexA=DNAMOTIF() | 119 | lexA=DNAMOTIF() | ||
119 | lexA.parse("lexA.fasta") | 120 | lexA.parse("lexA.fasta") | ||
120 | lexA.count() | 121 | lexA.count() | ||
121 | print(lexA.counts) | 122 | print(lexA.counts) | ||
122 | lexA.compute_consensus() | 123 | lexA.compute_consensus() | ||
123 | print(lexA.consensus) | 124 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | import matplotlib.pyplot as plt | n | ||
3 | class PLANT: | 2 | class PLANT: | ||
n | 4 | def __init__ (self, initial , gen = {}, n = 0, deltaTheta = 0): | n | 3 | def __init__(self, initial , gen = {}, p = 0, deltaTheta = 0): |
5 | self.initial=initial | 4 | self.initial = initial | ||
6 | self.gen=gen | 5 | self.gen = gen | ||
7 | self.n=n | 6 | self.p = p | ||
8 | self.deltaTheta=deltaTheta | 7 | self.deltaTheta = deltaTheta | ||
9 | self.str=initial | 8 | self.str = initial | ||
10 | while n>0: | 9 | while p>0: | ||
11 | n -= 1 | 10 | p -= 1 | ||
12 | y = "" | 11 | y = '' | ||
13 | for i in self.str: | 12 | for i in self.str: | ||
14 | if i in gen: | 13 | if i in gen: | ||
15 | y += gen[i] | 14 | y += gen[i] | ||
16 | else: | 15 | else: | ||
17 | y += i | 16 | y += i | ||
18 | self.str = y | 17 | self.str = y | ||
19 | def drawPlant(self): | 18 | def drawPlant(self): | ||
20 | x = [] | 19 | x = [] | ||
21 | for i in self.str: | 20 | for i in self.str: | ||
n | 22 | if i == "[": | n | 21 | if i == '[': |
23 | x.append([initialPt,theta]) | 22 | x.append([currentPt,theta]) | ||
24 | elif i == "]": | 23 | elif i == ']': | ||
25 | initiaPt = x[-1][0] | 24 | currentPt = x[-1][0] | ||
26 | theta = x[-1][1] | 25 | theta = x[-1][1] | ||
n | 27 | elif i == "+": | n | 26 | elif i == '+': |
28 | theta += self.deltaTheta | 27 | theta += self.deltaTheta | ||
n | 29 | elif i == "-": | n | 28 | elif i == '-': |
30 | theta -= self.deltaTheta | 29 | theta -= self.deltaTheta | ||
31 | currentPt=(200,0) | 30 | currentPt=(200,0) | ||
32 | theta = 90 | 31 | theta = 90 | ||
n | 33 | nextPt = (initialPt[0] + math.cos(theta), initialPt[1] + math.sin(theta) | n | 32 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.sin(theta) |
> | ) | > | ) | ||
34 | plt.plot([initialPt[0],nextPt[0]],[initialPt[1],nextPt[1]],color='black' | 33 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color='black' | ||
> | ) | > | ) | ||
35 | currentPt=nextPt | 34 | currentPt=nextPt | ||
36 | x = [] | 35 | x = [] | ||
37 | for i in self.str: | 36 | for i in self.str: | ||
n | 38 | if i == "[": | n | 37 | if i == '[': |
39 | x.append([initialPt,theta]) | 38 | x.append([currentPt,theta]) | ||
40 | elif i == "]": | 39 | elif i == ']': | ||
41 | initialPt = x[-1][0] | 40 | currentPt = x[-1][0] | ||
42 | theta = x[-1][1] | 41 | theta = x[-1][1] | ||
n | 43 | elif i == "+": | n | 42 | elif i == '+': |
44 | theta += self.deltaTheta | 43 | theta += self.deltaTheta | ||
t | 45 | elif i == "-": | t | 44 | elif i == '-': |
46 | theta -= self.deltaTheta | 45 | theta -= self.deltaTheta | ||
47 | class DNAMOTIF: | 46 | class DNAMOTIF: | ||
48 | def __init__(self): | 47 | def __init__(self): | ||
49 | self.instances=[] | 48 | self.instances=[] | ||
50 | self.consensus=[] | 49 | self.consensus=[] | ||
51 | self.counts= {"A": [], "C": [], "G":[],"T":[]} | 50 | self.counts= {"A": [], "C": [], "G":[],"T":[]} | ||
52 | def __str__(self): | 51 | def __str__(self): | ||
53 | string = "" | 52 | string = "" | ||
54 | for i in self.instances: | 53 | for i in self.instances: | ||
55 | string += i + "" | 54 | string += i + "" | ||
56 | return string[:-2] | 55 | return string[:-2] | ||
57 | def __len__(self): | 56 | def __len__(self): | ||
58 | return len(self.instances[0])-1 | 57 | return len(self.instances[0])-1 | ||
59 | def count(self): | 58 | def count(self): | ||
60 | i = 0 | 59 | i = 0 | ||
61 | count = 0 | 60 | count = 0 | ||
62 | up = [] | 61 | up = [] | ||
63 | do_again = [] | 62 | do_again = [] | ||
64 | A = 0 | 63 | A = 0 | ||
65 | C = 0 | 64 | C = 0 | ||
66 | T = 0 | 65 | T = 0 | ||
67 | G = 0 | 66 | G = 0 | ||
68 | while i < len(self.instances[0])-1: | 67 | while i < len(self.instances[0])-1: | ||
69 | while count < len(self.instances): | 68 | while count < len(self.instances): | ||
70 | for amino in self.instances: | 69 | for amino in self.instances: | ||
71 | amino = amino.upper() | 70 | amino = amino.upper() | ||
72 | up.append(amino[i]) | 71 | up.append(amino[i]) | ||
73 | count += 1 | 72 | count += 1 | ||
74 | count = 0 | 73 | count = 0 | ||
75 | do_again.append(up) | 74 | do_again.append(up) | ||
76 | up = [] | 75 | up = [] | ||
77 | i += 1 | 76 | i += 1 | ||
78 | for DNA in do_again: | 77 | for DNA in do_again: | ||
79 | for an in DNA: | 78 | for an in DNA: | ||
80 | if an == 'A': | 79 | if an == 'A': | ||
81 | A += 1 | 80 | A += 1 | ||
82 | if an == 'C': | 81 | if an == 'C': | ||
83 | C += 1 | 82 | C += 1 | ||
84 | if an == 'T': | 83 | if an == 'T': | ||
85 | T += 1 | 84 | T += 1 | ||
86 | if an == 'G': | 85 | if an == 'G': | ||
87 | G += 1 | 86 | G += 1 | ||
88 | self.counts.setdefault('A',[]).append(A) | 87 | self.counts.setdefault('A',[]).append(A) | ||
89 | self.counts.setdefault('C',[]).append(C) | 88 | self.counts.setdefault('C',[]).append(C) | ||
90 | self.counts.setdefault('T',[]).append(T) | 89 | self.counts.setdefault('T',[]).append(T) | ||
91 | self.counts.setdefault('G',[]).append(G) | 90 | self.counts.setdefault('G',[]).append(G) | ||
92 | A = 0 | 91 | A = 0 | ||
93 | C = 0 | 92 | C = 0 | ||
94 | T = 0 | 93 | T = 0 | ||
95 | G = 0 | 94 | G = 0 | ||
96 | def compute_consensus(self): | 95 | def compute_consensus(self): | ||
97 | self.count() | 96 | self.count() | ||
98 | A = self.counts["A"] | 97 | A = self.counts["A"] | ||
99 | C = self.counts["C"] | 98 | C = self.counts["C"] | ||
100 | G = self.counts["G"] | 99 | G = self.counts["G"] | ||
101 | T = self.counts["T"] | 100 | T = self.counts["T"] | ||
102 | for amino in range(len(A)): | 101 | for amino in range(len(A)): | ||
103 | if(A[amino]>= C[amino] and A[amino] >= G[amino] and A[amino] >= T[a | 102 | if(A[amino]>= C[amino] and A[amino] >= G[amino] and A[amino] >= T[a | ||
> | mino]): | > | mino]): | ||
104 | self.consensus.append("A") | 103 | self.consensus.append("A") | ||
105 | elif (C[amino] >= G[amino] and C[amino] >= T[amino]): | 104 | elif (C[amino] >= G[amino] and C[amino] >= T[amino]): | ||
106 | self.consensus.append("C") | 105 | self.consensus.append("C") | ||
107 | elif (G[amino] >= T[amino]): | 106 | elif (G[amino] >= T[amino]): | ||
108 | self.consensus.append("G") | 107 | self.consensus.append("G") | ||
109 | else: | 108 | else: | ||
110 | self.consensus.append("T") | 109 | self.consensus.append("T") | ||
111 | self.consensus = "".join(self.consensus) | 110 | self.consensus = "".join(self.consensus) | ||
112 | def parse(self, filename): | 111 | def parse(self, filename): | ||
113 | with open(filename,'r') as f: | 112 | with open(filename,'r') as f: | ||
114 | for i in f: | 113 | for i in f: | ||
115 | if ">" in i: | 114 | if ">" in i: | ||
116 | continue | 115 | continue | ||
117 | else: | 116 | else: | ||
118 | self.instances.append(i) | 117 | self.instances.append(i) | ||
119 | lexA=DNAMOTIF() | 118 | lexA=DNAMOTIF() | ||
120 | lexA.parse("lexA.fasta") | 119 | lexA.parse("lexA.fasta") | ||
121 | lexA.count() | 120 | lexA.count() | ||
122 | print(lexA.counts) | 121 | print(lexA.counts) | ||
123 | lexA.compute_consensus() | 122 | lexA.compute_consensus() | ||
124 | print(lexA.consensus) | 123 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | plt.style.use('bmh') | n | 2 | plt.style.use('bmh') |
3 | plt.plot( | 3 | plt.plot( | ||
n | 4 | [0, 1, 2], | n | 4 | [0, 1, 2], |
5 | [0, 1, 0] | 5 | [0, 1, 0] | ||
6 | ) | 6 | ) | ||
7 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
8 | plt.ylabel('y'); | 8 | plt.ylabel('y'); | ||
9 | def plot_coords(coords, bare_plot=False): | 9 | def plot_coords(coords, bare_plot=False): | ||
10 | if bare_plot: | 10 | if bare_plot: | ||
11 | plt.axis('off') | 11 | plt.axis('off') | ||
n | 12 | plt.axes().set_aspect('equal', 'datalim'). | n | 12 | plt.axes().set_aspect('equal', 'datalim') |
13 | X, Y = zip(*coords) | 13 | X, Y = zip(*coords) | ||
14 | plt.plot(X, Y); | 14 | plt.plot(X, Y); | ||
n | 15 | plot_coords([ | n | 15 | plot_coords([ |
16 | (0, 0), | 16 | (0, 0), | ||
17 | (1, 0), | 17 | (1, 0), | ||
18 | (2, 1), | 18 | (2, 1), | ||
19 | (3, 1), | 19 | (3, 1), | ||
20 | (2, 0) | 20 | (2, 0) | ||
21 | ]) | 21 | ]) | ||
22 | nan = float('nan') | 22 | nan = float('nan') | ||
23 | plot_coords([ | 23 | plot_coords([ | ||
24 | (0, 0), | 24 | (0, 0), | ||
25 | (1, 1), | 25 | (1, 1), | ||
26 | (nan, nan), | 26 | (nan, nan), | ||
27 | (1, 0), | 27 | (1, 0), | ||
28 | (2, 1) | 28 | (2, 1) | ||
29 | ]) | 29 | ]) | ||
30 | from math import pi, sin, cos | 30 | from math import pi, sin, cos | ||
31 | DEGREES_TO_RADIANS = pi / 180 | 31 | DEGREES_TO_RADIANS = pi / 180 | ||
32 | def turtle_to_coords(turtle_program, turn_amount=45): | 32 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
33 | state = (0.0, 0.0, 90.0) | 33 | state = (0.0, 0.0, 90.0) | ||
34 | yield (0.0, 0.0) | 34 | yield (0.0, 0.0) | ||
35 | for command in turtle_program: | 35 | for command in turtle_program: | ||
36 | x, y, angle = state | 36 | x, y, angle = state | ||
n | 37 | if command in 'Ff': | n | 37 | if command in 'Ff': |
38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
39 | y + sin(angle * DEGREES_TO_RADIANS), | 39 | y + sin(angle * DEGREES_TO_RADIANS), | ||
40 | angle) | 40 | angle) | ||
41 | if command == 'f': | 41 | if command == 'f': | ||
42 | yield (float('nan'), float('nan')) | 42 | yield (float('nan'), float('nan')) | ||
43 | yield (state[0], state[1]) | 43 | yield (state[0], state[1]) | ||
n | 44 | elif command == '+': | n | 44 | elif command == '+': |
45 | state = (x, y, angle + turn_amount) | 45 | state = (x, y, angle + turn_amount) | ||
n | 46 | elif command == '-': | n | 46 | elif command == '-': |
47 | state = (x, y, angle - turn_amount) | 47 | state = (x, y, angle - turn_amount) | ||
48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | 48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
50 | from math import isnan | 50 | from math import isnan | ||
51 | def print_coords(coords): | 51 | def print_coords(coords): | ||
52 | for (x, y) in coords: | 52 | for (x, y) in coords: | ||
53 | if isnan(x): | 53 | if isnan(x): | ||
54 | print('<gap>') | 54 | print('<gap>') | ||
55 | else: | 55 | else: | ||
56 | print('({:.2f}, {:.2f})'.format(x, y)) | 56 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | 58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
59 | def transform_sequence(sequence, transformations): | 59 | def transform_sequence(sequence, transformations): | ||
60 | return ''.join(transformations.get(c, c) for c in sequence) | 60 | return ''.join(transformations.get(c, c) for c in sequence) | ||
61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | 61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | ||
62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | 62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
63 | def transform_multiple(sequence, transformations, iterations): | 63 | def transform_multiple(sequence, transformations, iterations): | ||
64 | for _ in range(iterations): | 64 | for _ in range(iterations): | ||
65 | sequence = transform_sequence(sequence, transformations) | 65 | sequence = transform_sequence(sequence, transformations) | ||
66 | return sequence | 66 | return sequence | ||
67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | 67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | ||
68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | 70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
71 | plot_coords(turtle_to_coords(transform_multiple('L', { | 71 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
72 | 'L': '-RF+LFL+FR-', | 72 | 'L': '-RF+LFL+FR-', | ||
73 | 'R': '+LF-RFR-FL+' | 73 | 'R': '+LF-RFR-FL+' | ||
74 | }, 5), 90)) | 74 | }, 5), 90)) | ||
75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | 75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
76 | saved_states = list() | 76 | saved_states = list() | ||
77 | state = (0, 0, 90) | 77 | state = (0, 0, 90) | ||
78 | yield (0, 0) | 78 | yield (0, 0) | ||
79 | for command in turtle_program: | 79 | for command in turtle_program: | ||
80 | x, y, angle = state | 80 | x, y, angle = state | ||
n | 81 | if command.lower() in 'abcdefghij': | n | 81 | if command.lower() in 'abcdefghij': |
82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
83 | y + sin(angle * DEGREES_TO_RADIANS), | 83 | y + sin(angle * DEGREES_TO_RADIANS), | ||
84 | angle) | 84 | angle) | ||
85 | if command.islower(): | 85 | if command.islower(): | ||
86 | yield (float('nan'), float('nan')) | 86 | yield (float('nan'), float('nan')) | ||
87 | yield (state[0], state[1]) | 87 | yield (state[0], state[1]) | ||
n | 88 | elif command == '+': | n | 88 | elif command == '+': |
89 | state = (x, y, angle + turn_amount) | 89 | state = (x, y, angle + turn_amount) | ||
n | 90 | elif command == '-': | n | 90 | elif command == '-': |
91 | state = (x, y, angle - turn_amount) | 91 | state = (x, y, angle - turn_amount) | ||
n | 92 | elif command == '[': | n | 92 | elif command == '[': |
93 | saved_states.append(state) | 93 | saved_states.append(state) | ||
94 | elif command == ']': | 94 | elif command == ']': | ||
95 | state = saved_states.pop() | 95 | state = saved_states.pop() | ||
96 | yield (float('nan'), float('nan')) | 96 | yield (float('nan'), float('nan')) | ||
97 | x, y, _ = state | 97 | x, y, _ = state | ||
98 | yield (x, y) | 98 | yield (x, y) | ||
99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | 99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
100 | def l_plot(axiom, transformations, iterations=0, angle=45): | 100 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
101 | turtle_program = transform_multiple(axiom, transformations, iterations) | 101 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
102 | coords = branching_turtle_to_coords(turtle_program, angle) | 102 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
103 | plot_coords(coords, bare_plot=True) | 103 | plot_coords(coords, bare_plot=True) | ||
104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | 104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | ||
105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
106 | for i in range(5): | 106 | for i in range(5): | ||
107 | print('{}: '.format(i), | 107 | print('{}: '.format(i), | ||
108 | transform_multiple('A', {'A': 'F+A'}, i)) | 108 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
109 | for i in range(5): | 109 | for i in range(5): | ||
110 | print('{}: '.format(i), | 110 | print('{}: '.format(i), | ||
111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | 111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
n | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | n | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: |
113 | def __init__(self): | 113 | def __init__(self): | ||
n | 114 | self.instances=[] | n | 114 | self.instances = [] |
115 | self.consensus=[] | 115 | self.consensus=[] | ||
116 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | ||
117 | def __str__(self): | 117 | def __str__ (self): | ||
118 | string = "" | 118 | string = "" | ||
119 | for i in self.instances: | 119 | for i in self.instances: | ||
n | 120 | string += i | n | 120 | string += i + "\n" |
121 | return string[:-2] | ||||
121 | def __len__(self): | 122 | def __len__(self): | ||
n | 122 | return len(self.instances) - len(self.counts) | n | 123 | return len(self.instances) |
123 | def count(self): | 124 | def count(self): | ||
124 | for i in self.instances: | 125 | for i in self.instances: | ||
125 | temp = i.upper() | 126 | temp = i.upper() | ||
126 | self.counts["A"].append(temp.count("A")) | 127 | self.counts["A"].append(temp.count("A")) | ||
127 | self.counts["C"].append(temp.count("C")) | 128 | self.counts["C"].append(temp.count("C")) | ||
128 | self.counts["G"].append(temp.count("G")) | 129 | self.counts["G"].append(temp.count("G")) | ||
n | 129 | self.counts["T"].append(temp.count("T")) | n | 130 | self.counts["T"].append(temp.count("T")) |
130 | def compute_consensus(self): | 131 | def compute_consensus(self): | ||
131 | A = self.counts["A"] | 132 | A = self.counts["A"] | ||
132 | C = self.counts["C"] | 133 | C = self.counts["C"] | ||
133 | G = self.counts["G"] | 134 | G = self.counts["G"] | ||
134 | T = self.counts["T"] | 135 | T = self.counts["T"] | ||
135 | for i in range(len(A)): | 136 | for i in range(len(A)): | ||
136 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
137 | self.consensus.append("A") | 138 | self.consensus.append("A") | ||
138 | elif (C[i] >= G[i] and C[i] >= T[i]): | 139 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
139 | self.consensus.append("C") | 140 | self.consensus.append("C") | ||
140 | elif (G[i] >= T[i]): | 141 | elif (G[i] >= T[i]): | ||
141 | self.consensus.append("G") | 142 | self.consensus.append("G") | ||
142 | else: | 143 | else: | ||
143 | self.consensus.append("T") | 144 | self.consensus.append("T") | ||
144 | def parse(self, filename): | 145 | def parse(self, filename): | ||
n | 145 | with open(filename,'r') as fasta: | n | 146 | with open(filename,'r') as f: |
146 | for i in fasta: | 147 | for i in f: | ||
147 | if ">" in i: | 148 | if ">" in i: | ||
n | 148 | pass | n | 149 | continue |
149 | else: | 150 | else: | ||
150 | self.instances.append(i) | 151 | self.instances.append(i) | ||
t | 151 | if __name__=="__main__": | t | 152 | if __name__=='__main__': |
152 | lexA = DNAMOTIF() | 153 | lex=DNAMOTIF() | ||
153 | lexA.parse("lexA.fasta") | 154 | lex.parse('lexA.fasta') | ||
154 | print(len(lexA)) | 155 | print("length",len(lex)) | ||
155 | lexA = DNAMOTIF() | ||||
156 | lexA.parse("lexA.fasta") | ||||
157 | print(lexA) | 156 | print(lex) | ||
158 | lexA = DNAMOTIF() | 157 | print("consensus",lex.consensus) | ||
159 | lexA.count() | ||||
160 | print(lexA.counts) | 158 | print('seq',lex.counts) | ||
161 | lexA = DNAMOTIF() | ||||
162 | lexA.compute_consensus() | ||||
163 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | 2 | import numpy as np | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
5 | self.string = string | 5 | self.string = string | ||
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | self.str = PLANT.generator(string,dictionary, n) | 9 | self.str = PLANT.generator(string,dictionary, n) | ||
10 | def generator(string, dictionary, n): | 10 | def generator(string, dictionary, n): | ||
11 | def character_v(string): | 11 | def character_v(string): | ||
12 | if string in dictionary: | 12 | if string in dictionary: | ||
13 | return dictionary.get(string) | 13 | return dictionary.get(string) | ||
14 | return string | 14 | return string | ||
15 | substrings_list = [string] | 15 | substrings_list = [string] | ||
16 | for i in range(n): | 16 | for i in range(n): | ||
17 | substring = substrings_list[-1] | 17 | substring = substrings_list[-1] | ||
18 | new_statement = [character_v(char) for char in substring] | 18 | new_statement = [character_v(char) for char in substring] | ||
19 | substrings_list.append(''.join(new_statement)) | 19 | substrings_list.append(''.join(new_statement)) | ||
20 | newstring = substrings_list[n] | 20 | newstring = substrings_list[n] | ||
21 | return newstring | 21 | return newstring | ||
22 | def drawPlant(self): | 22 | def drawPlant(self): | ||
23 | upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', | 23 | upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', | ||
> | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | > | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||
24 | currentPt=(200,0) | 24 | currentPt=(200,0) | ||
25 | theta = np.radians(90) | 25 | theta = np.radians(90) | ||
26 | deltaTheta = np.radians(self.deltaTheta) | 26 | deltaTheta = np.radians(self.deltaTheta) | ||
27 | stack = [currentPt, theta] | 27 | stack = [currentPt, theta] | ||
28 | for command in self.str: | 28 | for command in self.str: | ||
29 | if command.upper() in upper_letters: | 29 | if command.upper() in upper_letters: | ||
30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | 30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | ||
> | ta))) | > | ta))) | ||
31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
n | 32 | currentPt = nextPt | n | 32 | currentPt = nextPt |
33 | else: | 33 | else: | ||
34 | if command == '[': | 34 | if command == '[': | ||
35 | stack.append(currentPt) | 35 | stack.append(currentPt) | ||
36 | stack.append(theta) | 36 | stack.append(theta) | ||
37 | elif command == ']': | 37 | elif command == ']': | ||
38 | theta = stack.pop() | 38 | theta = stack.pop() | ||
39 | currentPt = stack.pop() | 39 | currentPt = stack.pop() | ||
40 | elif command == '-': | 40 | elif command == '-': | ||
41 | theta -= deltaTheta | 41 | theta -= deltaTheta | ||
42 | elif command == '+': | 42 | elif command == '+': | ||
43 | theta += deltaTheta | 43 | theta += deltaTheta | ||
44 | return plt.plot | 44 | return plt.plot | ||
45 | if __name__ == "__main__": | 45 | if __name__ == "__main__": | ||
46 | string = input() | 46 | string = input() | ||
47 | dictionary = input() | 47 | dictionary = input() | ||
48 | n = int(input()) | 48 | n = int(input()) | ||
49 | deltaTheta = float(input()) | 49 | deltaTheta = float(input()) | ||
50 | p = PLANT(string, dictionary, n, deltaTheta) | 50 | p = PLANT(string, dictionary, n, deltaTheta) | ||
51 | p.drawPlant() | 51 | p.drawPlant() | ||
52 | plt.show()class DNAMOTIF: | 52 | plt.show()class DNAMOTIF: | ||
53 | def __init__(self): | 53 | def __init__(self): | ||
n | 54 | self.instances = [] | n | 54 | self.instances=[] |
55 | self.consensus = [] | 55 | self.consensus=[] | ||
56 | self.counts = {'A': [], 'C': [], 'G':[],'T':[]} | 56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
57 | def __str__(self): | 57 | def __str__(self): | ||
n | 58 | output = '' | n | 58 | output = "" |
59 | for instance in self.instances: | 59 | for instance in self.instances: | ||
60 | output += instance | 60 | output += instance | ||
61 | return output | 61 | return output | ||
62 | def __len__(self): | 62 | def __len__(self): | ||
63 | return len(self.instances[0].rstrip()) | 63 | return len(self.instances[0].rstrip()) | ||
64 | def count(self): | 64 | def count(self): | ||
65 | for position in range(len(self)): | 65 | for position in range(len(self)): | ||
66 | sequenceA = 0 | 66 | sequenceA = 0 | ||
67 | sequenceT = 0 | 67 | sequenceT = 0 | ||
68 | sequenceC = 0 | 68 | sequenceC = 0 | ||
69 | sequenceG = 0 | 69 | sequenceG = 0 | ||
70 | for instance in self.instances: | 70 | for instance in self.instances: | ||
71 | sequence = instance.rstrip() | 71 | sequence = instance.rstrip() | ||
n | 72 | if (sequence[position]).upper() == 'A': | n | 72 | if (sequence[position]).upper() == "A": |
73 | sequenceA += 1 | 73 | sequenceA += 1 | ||
n | 74 | elif (sequence[position]).upper() == 'T': | n | 74 | elif (sequence[position]).upper() == "T": |
75 | sequenceT += 1 | 75 | sequenceT += 1 | ||
n | 76 | elif (sequence[position]).upper() == 'C': | n | 76 | elif (sequence[position]).upper() == "C": |
77 | sequenceC += 1 | 77 | sequenceC += 1 | ||
n | 78 | elif (sequence[position]).upper() == 'G': | n | 78 | elif (sequence[position]).upper() == "G": |
79 | sequenceG += 1 | 79 | sequenceG += 1 | ||
n | 80 | self.counts.get('A').append(sequenceA) | n | 80 | self.counts.get("A").append(sequenceA) |
81 | self.counts.get('T').append(sequenceT) | 81 | self.counts.get("T").append(sequenceT) | ||
82 | self.counts.get('C').append(sequenceC) | 82 | self.counts.get("C").append(sequenceC) | ||
83 | self.counts.get('G').append(sequenceG) | 83 | self.counts.get("G").append(sequenceG) | ||
84 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
85 | DNAMOTIF.count(self) | 85 | DNAMOTIF.count(self) | ||
n | 86 | A = self.counts.get('A') | n | 86 | A = self.counts.get("A") |
87 | T = self.counts.get('T') | 87 | T = self.counts.get("T") | ||
88 | C = self.counts.get('C') | 88 | C = self.counts.get("C") | ||
89 | G = self.counts.get('G') | 89 | G = self.counts.get("G") | ||
90 | output = "" | 90 | output = "" | ||
91 | for row in range(len(A)): | 91 | for row in range(len(A)): | ||
n | 92 | maxes = max(A[row],T[row],C[row],G[row]) | n | 92 | maxs = max(A[row],T[row],C[row],G[row]) |
93 | if maxes == A[row]: | 93 | if maxs == A[row]: | ||
94 | output += 'A' | 94 | output += "A" | ||
95 | elif maxes == T[row]: | 95 | elif maxs == T[row]: | ||
96 | output += 'T' | 96 | output += "T" | ||
97 | elif maxes == C[row]: | 97 | elif maxs == C[row]: | ||
98 | output += 'C' | 98 | output += "C" | ||
99 | elif maxes == G[row]: | 99 | elif maxs == G[row]: | ||
100 | output += 'G' | 100 | output += "G" | ||
101 | self.consensus = output | 101 | self.consensus = output | ||
102 | def parse(self, filename): | 102 | def parse(self, filename): | ||
103 | myFile = open(filename, 'r') | 103 | myFile = open(filename, 'r') | ||
104 | contents = [line for line in myFile.readlines()] | 104 | contents = [line for line in myFile.readlines()] | ||
105 | myFile.close() | 105 | myFile.close() | ||
106 | for index, line in enumerate(contents): | 106 | for index, line in enumerate(contents): | ||
107 | if index % 2 != 0: | 107 | if index % 2 != 0: | ||
108 | self.instances.append(line) | 108 | self.instances.append(line) | ||
109 | if __name__ == '__main__': | 109 | if __name__ == '__main__': | ||
110 | lexA=DNAMOTIF() | 110 | lexA=DNAMOTIF() | ||
t | 111 | filename = r'FinalProject\lexA.fasta' | t | 111 | filename = r'FinalProject\lexA.fasta' |
112 | lexA.parse(filename) | 112 | lexA.parse(filename) | ||
113 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
114 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | 2 | import numpy as np | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
5 | self.string = string | 5 | self.string = string | ||
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | self.str = PLANT.generator(string,dictionary, n) | 9 | self.str = PLANT.generator(string,dictionary, n) | ||
10 | def generator(string, dictionary, n): | 10 | def generator(string, dictionary, n): | ||
11 | def character_v(string): | 11 | def character_v(string): | ||
12 | if string in dictionary: | 12 | if string in dictionary: | ||
13 | return dictionary.get(string) | 13 | return dictionary.get(string) | ||
14 | return string | 14 | return string | ||
15 | substrings_list = [string] | 15 | substrings_list = [string] | ||
16 | for i in range(n): | 16 | for i in range(n): | ||
17 | substring = substrings_list[-1] | 17 | substring = substrings_list[-1] | ||
18 | new_statement = [character_v(char) for char in substring] | 18 | new_statement = [character_v(char) for char in substring] | ||
19 | substrings_list.append(''.join(new_statement)) | 19 | substrings_list.append(''.join(new_statement)) | ||
20 | newstring = substrings_list[n] | 20 | newstring = substrings_list[n] | ||
21 | return newstring | 21 | return newstring | ||
22 | def drawPlant(self): | 22 | def drawPlant(self): | ||
23 | upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', | 23 | upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', | ||
> | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | > | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||
24 | currentPt=(200,0) | 24 | currentPt=(200,0) | ||
25 | theta = np.radians(90) | 25 | theta = np.radians(90) | ||
26 | deltaTheta = np.radians(self.deltaTheta) | 26 | deltaTheta = np.radians(self.deltaTheta) | ||
27 | stack = [currentPt, theta] | 27 | stack = [currentPt, theta] | ||
28 | for command in self.str: | 28 | for command in self.str: | ||
29 | if command.upper() in upper_letters: | 29 | if command.upper() in upper_letters: | ||
30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | 30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | ||
> | ta))) | > | ta))) | ||
31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
n | 32 | currentPt = nextPt | n | 32 | currentPt = nextPt |
33 | else: | 33 | else: | ||
34 | if command == '[': | 34 | if command == '[': | ||
35 | stack.append(currentPt) | 35 | stack.append(currentPt) | ||
36 | stack.append(theta) | 36 | stack.append(theta) | ||
37 | elif command == ']': | 37 | elif command == ']': | ||
38 | theta = stack.pop() | 38 | theta = stack.pop() | ||
39 | currentPt = stack.pop() | 39 | currentPt = stack.pop() | ||
40 | elif command == '-': | 40 | elif command == '-': | ||
41 | theta -= deltaTheta | 41 | theta -= deltaTheta | ||
42 | elif command == '+': | 42 | elif command == '+': | ||
43 | theta += deltaTheta | 43 | theta += deltaTheta | ||
44 | return plt.plot | 44 | return plt.plot | ||
45 | if __name__ == "__main__": | 45 | if __name__ == "__main__": | ||
46 | string = input() | 46 | string = input() | ||
47 | dictionary = input() | 47 | dictionary = input() | ||
48 | n = int(input()) | 48 | n = int(input()) | ||
49 | deltaTheta = float(input()) | 49 | deltaTheta = float(input()) | ||
50 | p = PLANT(string, dictionary, n, deltaTheta) | 50 | p = PLANT(string, dictionary, n, deltaTheta) | ||
51 | p.drawPlant() | 51 | p.drawPlant() | ||
52 | plt.show()class DNAMOTIF: | 52 | plt.show()class DNAMOTIF: | ||
53 | def __init__(self): | 53 | def __init__(self): | ||
n | 54 | self.instances=[] | n | 54 | self.instances = [] |
55 | self.consensus=[] | 55 | self.consensus = [] | ||
56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 56 | self.counts = {'A': [], 'C': [], 'G':[],'T':[]} | ||
57 | def __str__(self): | 57 | def __str__(self): | ||
n | 58 | output = "" | n | 58 | output = '' |
59 | for instance in self.instances: | 59 | for instance in self.instances: | ||
60 | output += instance | 60 | output += instance | ||
61 | return output | 61 | return output | ||
62 | def __len__(self): | 62 | def __len__(self): | ||
63 | return len(self.instances[0].rstrip()) | 63 | return len(self.instances[0].rstrip()) | ||
64 | def count(self): | 64 | def count(self): | ||
65 | for position in range(len(self)): | 65 | for position in range(len(self)): | ||
66 | sequenceA = 0 | 66 | sequenceA = 0 | ||
67 | sequenceT = 0 | 67 | sequenceT = 0 | ||
68 | sequenceC = 0 | 68 | sequenceC = 0 | ||
69 | sequenceG = 0 | 69 | sequenceG = 0 | ||
70 | for instance in self.instances: | 70 | for instance in self.instances: | ||
71 | sequence = instance.rstrip() | 71 | sequence = instance.rstrip() | ||
n | 72 | if (sequence[position]).upper() == "A": | n | 72 | if (sequence[position]).upper() == 'A': |
73 | sequenceA += 1 | 73 | sequenceA += 1 | ||
n | 74 | elif (sequence[position]).upper() == "T": | n | 74 | elif (sequence[position]).upper() == 'T': |
75 | sequenceT += 1 | 75 | sequenceT += 1 | ||
n | 76 | elif (sequence[position]).upper() == "C": | n | 76 | elif (sequence[position]).upper() == 'C': |
77 | sequenceC += 1 | 77 | sequenceC += 1 | ||
n | 78 | elif (sequence[position]).upper() == "G": | n | 78 | elif (sequence[position]).upper() == 'G': |
79 | sequenceG += 1 | 79 | sequenceG += 1 | ||
n | 80 | self.counts.get("A").append(sequenceA) | n | 80 | self.counts.get('A').append(sequenceA) |
81 | self.counts.get("T").append(sequenceT) | 81 | self.counts.get('T').append(sequenceT) | ||
82 | self.counts.get("C").append(sequenceC) | 82 | self.counts.get('C').append(sequenceC) | ||
83 | self.counts.get("G").append(sequenceG) | 83 | self.counts.get('G').append(sequenceG) | ||
84 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
85 | DNAMOTIF.count(self) | 85 | DNAMOTIF.count(self) | ||
n | 86 | A = self.counts.get("A") | n | 86 | A = self.counts.get('A') |
87 | T = self.counts.get("T") | 87 | T = self.counts.get('T') | ||
88 | C = self.counts.get("C") | 88 | C = self.counts.get('C') | ||
89 | G = self.counts.get("G") | 89 | G = self.counts.get('G') | ||
90 | output = "" | 90 | output = "" | ||
91 | for row in range(len(A)): | 91 | for row in range(len(A)): | ||
n | 92 | maxs = max(A[row],T[row],C[row],G[row]) | n | 92 | maxes = max(A[row],T[row],C[row],G[row]) |
93 | if maxs == A[row]: | 93 | if maxes == A[row]: | ||
94 | output += "A" | 94 | output += 'A' | ||
95 | elif maxs == T[row]: | 95 | elif maxes == T[row]: | ||
96 | output += "T" | 96 | output += 'T' | ||
97 | elif maxs == C[row]: | 97 | elif maxes == C[row]: | ||
98 | output += "C" | 98 | output += 'C' | ||
99 | elif maxs == G[row]: | 99 | elif maxes == G[row]: | ||
100 | output += "G" | 100 | output += 'G' | ||
101 | self.consensus = output | 101 | self.consensus = output | ||
102 | def parse(self, filename): | 102 | def parse(self, filename): | ||
103 | myFile = open(filename, 'r') | 103 | myFile = open(filename, 'r') | ||
104 | contents = [line for line in myFile.readlines()] | 104 | contents = [line for line in myFile.readlines()] | ||
105 | myFile.close() | 105 | myFile.close() | ||
106 | for index, line in enumerate(contents): | 106 | for index, line in enumerate(contents): | ||
107 | if index % 2 != 0: | 107 | if index % 2 != 0: | ||
108 | self.instances.append(line) | 108 | self.instances.append(line) | ||
109 | if __name__ == '__main__': | 109 | if __name__ == '__main__': | ||
110 | lexA=DNAMOTIF() | 110 | lexA=DNAMOTIF() | ||
t | 111 | filename = r'FinalProject\lexA.fasta' | t | 111 | filename = r'FinalProject\lexA.fasta' |
112 | lexA.parse(filename) | 112 | lexA.parse(filename) | ||
113 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
114 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | import matplotlib.pyplot as plt | n | ||
3 | class PLANT: | 2 | class PLANT: | ||
n | 4 | def __init__(self, initial , gen = {}, n = 0, deltaTheta = 0): | n | 3 | def __init__(self, initial , gen = {}, p = 0, deltaTheta = 0): |
5 | self.initial = initial | 4 | self.initial = initial | ||
6 | self.gen = gen | 5 | self.gen = gen | ||
n | 7 | self.n = n | n | 6 | self.p = p |
8 | self.deltaTheta = deltaTheta | 7 | self.deltaTheta = deltaTheta | ||
9 | self.str = initial | 8 | self.str = initial | ||
n | 10 | while n>0: | n | 9 | while p>0: |
11 | n -= 1 | 10 | p -= 1 | ||
12 | y = '' | 11 | y = '' | ||
13 | for i in self.str: | 12 | for i in self.str: | ||
14 | if i in gen: | 13 | if i in gen: | ||
15 | y += gen[i] | 14 | y += gen[i] | ||
16 | else: | 15 | else: | ||
17 | y += i | 16 | y += i | ||
18 | self.str = y | 17 | self.str = y | ||
19 | def drawPlant(self): | 18 | def drawPlant(self): | ||
20 | x = [] | 19 | x = [] | ||
n | 21 | for let in self.str: | n | 20 | for i in self.str: |
22 | if let == '[': | 21 | if i == '[': | ||
23 | x.append([currentPt,theta]) | 22 | x.append([currentPt,theta]) | ||
n | 24 | elif let == ']': | n | 23 | elif i == ']': |
25 | currentPt = x[-1][0] | 24 | currentPt = x[-1][0] | ||
26 | theta = x[-1][1] | 25 | theta = x[-1][1] | ||
n | 27 | elif let == '+': | n | 26 | elif i == '+': |
28 | theta += self.deltaTheta | 27 | theta += self.deltaTheta | ||
n | 29 | elif let == '-': | n | 28 | elif i == '-': |
30 | theta -= self.deltaTheta | 29 | theta -= self.deltaTheta | ||
31 | currentPt=(200,0) | 30 | currentPt=(200,0) | ||
32 | theta = 90 | 31 | theta = 90 | ||
33 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.sin(theta) | 32 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.sin(theta) | ||
> | ) | > | ) | ||
34 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color='black' | 33 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color='black' | ||
> | ) | > | ) | ||
35 | currentPt=nextPt | 34 | currentPt=nextPt | ||
36 | x = [] | 35 | x = [] | ||
n | 37 | for let in self.str: | n | 36 | for i in self.str: |
38 | if let == '[': | 37 | if i == '[': | ||
39 | x.append([currentPt,theta]) | 38 | x.append([currentPt,theta]) | ||
n | 40 | elif let == ']': | n | 39 | elif i == ']': |
41 | currentPt = x[-1][0] | 40 | currentPt = x[-1][0] | ||
42 | theta = x[-1][1] | 41 | theta = x[-1][1] | ||
n | 43 | elif let == '+': | n | 42 | elif i == '+': |
44 | theta += self.deltaTheta | 43 | theta += self.deltaTheta | ||
n | 45 | elif let == '-': | n | 44 | elif i == '-': |
46 | theta -= self.deltaTheta | 45 | theta -= self.deltaTheta | ||
47 | class DNAMOTIF: | 46 | class DNAMOTIF: | ||
48 | def __init__(self): | 47 | def __init__(self): | ||
49 | self.instances=[] | 48 | self.instances=[] | ||
50 | self.consensus=[] | 49 | self.consensus=[] | ||
51 | self.counts= {"A": [], "C": [], "G":[],"T":[]} | 50 | self.counts= {"A": [], "C": [], "G":[],"T":[]} | ||
52 | def __str__(self): | 51 | def __str__(self): | ||
53 | string = "" | 52 | string = "" | ||
54 | for i in self.instances: | 53 | for i in self.instances: | ||
55 | string += i + "" | 54 | string += i + "" | ||
56 | return string[:-2] | 55 | return string[:-2] | ||
57 | def __len__(self): | 56 | def __len__(self): | ||
58 | return len(self.instances[0])-1 | 57 | return len(self.instances[0])-1 | ||
59 | def count(self): | 58 | def count(self): | ||
60 | i = 0 | 59 | i = 0 | ||
61 | count = 0 | 60 | count = 0 | ||
62 | up = [] | 61 | up = [] | ||
n | 63 | up1 = [] | n | 62 | do_again = [] |
64 | A = 0 | 63 | A = 0 | ||
65 | C = 0 | 64 | C = 0 | ||
66 | T = 0 | 65 | T = 0 | ||
67 | G = 0 | 66 | G = 0 | ||
68 | while i < len(self.instances[0])-1: | 67 | while i < len(self.instances[0])-1: | ||
69 | while count < len(self.instances): | 68 | while count < len(self.instances): | ||
70 | for amino in self.instances: | 69 | for amino in self.instances: | ||
71 | amino = amino.upper() | 70 | amino = amino.upper() | ||
72 | up.append(amino[i]) | 71 | up.append(amino[i]) | ||
73 | count += 1 | 72 | count += 1 | ||
74 | count = 0 | 73 | count = 0 | ||
n | 75 | up1.append(up) | n | 74 | do_again.append(up) |
76 | up = [] | 75 | up = [] | ||
77 | i += 1 | 76 | i += 1 | ||
n | 78 | for strand in up1: | n | 77 | for DNA in do_again: |
79 | for i in strand: | 78 | for an in DNA: | ||
80 | if i == 'A': | 79 | if an == 'A': | ||
81 | A += 1 | 80 | A += 1 | ||
n | 82 | if i == 'C': | n | 81 | if an == 'C': |
83 | C += 1 | 82 | C += 1 | ||
n | 84 | if i == 'T': | n | 83 | if an == 'T': |
85 | T += 1 | 84 | T += 1 | ||
n | 86 | if i == 'G': | n | 85 | if an == 'G': |
87 | G += 1 | 86 | G += 1 | ||
88 | self.counts.setdefault('A',[]).append(A) | 87 | self.counts.setdefault('A',[]).append(A) | ||
89 | self.counts.setdefault('C',[]).append(C) | 88 | self.counts.setdefault('C',[]).append(C) | ||
90 | self.counts.setdefault('T',[]).append(T) | 89 | self.counts.setdefault('T',[]).append(T) | ||
91 | self.counts.setdefault('G',[]).append(G) | 90 | self.counts.setdefault('G',[]).append(G) | ||
92 | A = 0 | 91 | A = 0 | ||
93 | C = 0 | 92 | C = 0 | ||
94 | T = 0 | 93 | T = 0 | ||
95 | G = 0 | 94 | G = 0 | ||
96 | def compute_consensus(self): | 95 | def compute_consensus(self): | ||
97 | self.count() | 96 | self.count() | ||
98 | A = self.counts["A"] | 97 | A = self.counts["A"] | ||
99 | C = self.counts["C"] | 98 | C = self.counts["C"] | ||
100 | G = self.counts["G"] | 99 | G = self.counts["G"] | ||
101 | T = self.counts["T"] | 100 | T = self.counts["T"] | ||
n | 102 | for let in range(len(A)): | n | 101 | for amino in range(len(A)): |
103 | if(A[let]>= C[let] and A[let] >= G[let] and A[let] >= T[let]): | 102 | if(A[amino]>= C[amino] and A[amino] >= G[amino] and A[amino] >= T[a | ||
> | mino]): | ||||
104 | self.consensus.append("A") | 103 | self.consensus.append("A") | ||
n | 105 | elif (C[let] >= G[let] and C[let] >= T[let]): | n | 104 | elif (C[amino] >= G[amino] and C[amino] >= T[amino]): |
106 | self.consensus.append("C") | 105 | self.consensus.append("C") | ||
t | 107 | elif (G[let] >= T[let]): | t | 106 | elif (G[amino] >= T[amino]): |
108 | self.consensus.append("G") | 107 | self.consensus.append("G") | ||
109 | else: | 108 | else: | ||
110 | self.consensus.append("T") | 109 | self.consensus.append("T") | ||
111 | self.consensus = "".join(self.consensus) | 110 | self.consensus = "".join(self.consensus) | ||
112 | def parse(self, filename): | 111 | def parse(self, filename): | ||
113 | with open(filename,'r') as f: | 112 | with open(filename,'r') as f: | ||
114 | for i in f: | 113 | for i in f: | ||
115 | if ">" in i: | 114 | if ">" in i: | ||
116 | continue | 115 | continue | ||
117 | else: | 116 | else: | ||
118 | self.instances.append(i) | 117 | self.instances.append(i) | ||
119 | lexA=DNAMOTIF() | 118 | lexA=DNAMOTIF() | ||
120 | lexA.parse("lexA.fasta") | 119 | lexA.parse("lexA.fasta") | ||
121 | lexA.count() | 120 | lexA.count() | ||
122 | print(lexA.counts) | 121 | print(lexA.counts) | ||
123 | lexA.compute_consensus() | 122 | lexA.compute_consensus() | ||
124 | print(lexA.consensus) | 123 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
5 | self.str = string | 5 | self.str = string | ||
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
n | 9 | for x in range(n): | n | 9 | for i in range(n): |
10 | new_string = "" | 10 | new_string = "" | ||
11 | for letter in self.str: | 11 | for letter in self.str: | ||
12 | if letter in self.dictionary: | 12 | if letter in self.dictionary: | ||
13 | new_string += self.dictionary[letter] | 13 | new_string += self.dictionary[letter] | ||
14 | else: | 14 | else: | ||
15 | new_string += letter | 15 | new_string += letter | ||
16 | self.str = new_string | 16 | self.str = new_string | ||
17 | def drawPlant(self): | 17 | def drawPlant(self): | ||
18 | currentPt = (200,0) | 18 | currentPt = (200,0) | ||
19 | stack = [] | 19 | stack = [] | ||
20 | theta = 90 | 20 | theta = 90 | ||
21 | for letter in self.str: | 21 | for letter in self.str: | ||
22 | if letter == "[": | 22 | if letter == "[": | ||
23 | stack.append([currentPt,theta]) | 23 | stack.append([currentPt,theta]) | ||
24 | elif letter == "]": | 24 | elif letter == "]": | ||
n | 25 | latest = stack[-1] | n | 25 | current_save = stack.pop() |
26 | theta = current_save[1] | ||||
26 | currentPt = latest[0] | 27 | currentPt = current_save[0] | ||
27 | theta = latest[1] | ||||
28 | elif letter == "+": | 28 | elif letter == "+": | ||
29 | theta += self.deltaTheta | 29 | theta += self.deltaTheta | ||
30 | elif letter == "-": | 30 | elif letter == "-": | ||
31 | theta -= self.deltaTheta | 31 | theta -= self.deltaTheta | ||
32 | else: | 32 | else: | ||
n | 33 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.si | n | 33 | nextPt = (currentPt[0] + math.cos(math.radians(theta)), currentP |
> | n(theta)) | > | t[1] + math.sin(math.radians(theta))) | ||
34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | 34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||
> | olor='black') | > | olor='black') | ||
35 | currentPt = nextPtclass DNAMOTIF: | 35 | currentPt = nextPtclass DNAMOTIF: | ||
n | 36 | def __init__(self): | n | 36 | def __init_(self): |
37 | self.instances = [] | 37 | self.instances = [] | ||
38 | self.consensus = [] | 38 | self.consensus = [] | ||
39 | self.counts = {"A": [], "C": [], "G": [], "T": []} | 39 | self.counts = {"A": [], "C": [], "G": [], "T": []} | ||
40 | def __str__(self): | 40 | def __str__(self): | ||
41 | return "\n".join(self.instances) | 41 | return "\n".join(self.instances) | ||
42 | def __len__(self): | 42 | def __len__(self): | ||
43 | return len(self.instances[0])-1 | 43 | return len(self.instances[0])-1 | ||
44 | def parse(self, filename): | 44 | def parse(self, filename): | ||
45 | with open(filename, 'r') as reader: | 45 | with open(filename, 'r') as reader: | ||
n | 46 | self.instances = [x[0].lower() + x[1:].upper() for x in reader.readl | n | 46 | self.instances = [i[0].lower()+i[1:].upper() for i in reader.readlin |
> | ines() if x[0] != ">"] | > | es() if i[0] != ">"] | ||
47 | def count(self): | 47 | def count(self): | ||
n | 48 | count = 0 | n | ||
49 | self.counts = {"A": [0 for _ in range(len(self))], "C": [0 for _ in rang | 48 | self.counts = {"A": [0 for _ in range(len(self))], "C": [0 for _ in rang | ||
> | e(len(self))], "G": [0 for _ in range(len(self))], "T": [0 for _ in range(len(se | > | e(len(self))], "G": [0 for _ in range(len(self))], "T": [0 for _ in range(len(se | ||
> | lf))]} | > | lf))]} | ||
50 | for instance in self.instances: | 49 | for instance in self.instances: | ||
n | 51 | for x, letter in enumerate(instance): | n | 50 | for i, letter in enumerate(instance): |
52 | if letter != "\n": | 51 | if letter != "\n": | ||
n | 53 | self.counts[letter.upper()][x] += 1 | n | 52 | self.counts[letter.upper()][i] += 1 |
54 | def compute_consensus(self): | 53 | def compute_consensus(self): | ||
55 | self.count() | 54 | self.count() | ||
56 | consensus = "" | 55 | consensus = "" | ||
n | 57 | for x in range(len(self)): | n | 56 | for i in range(len(self)): |
58 | current_consensus = ("",0) | 57 | current_consensus = ("",0) | ||
59 | for letter in self.counts: | 58 | for letter in self.counts: | ||
n | 60 | if self.counts[letter][x] > current_consensus[1]: | n | 59 | if self.counts[letter][i] > current_consensus[1]: |
61 | current_consensus = (letter, self.counts[letter][x]) | 60 | current_consensus = (letter, self.counts[letter][i]) | ||
62 | consensus += current_consensus[0] | 61 | consensus += current_consensus[0] | ||
63 | self.consensus = consensus | 62 | self.consensus = consensus | ||
t | 64 | def count(self): | t | ||
65 | self.counts = {"A": [0 for _ in range(len(self))], "C": [0 for _ in rang | ||||
> | e(len(self))], "G": [0 for _ in range(len(self))], "T": [0 for _ in range(len(se | ||||
> | lf))]} | ||||
66 | for instance in self.instances: | ||||
67 | for x, letter in enumerate(instance): | ||||
68 | if letter != "\n": | ||||
69 | self.counts[letter.upper()][x] += 1 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
5 | self.str = string | 5 | self.str = string | ||
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
n | 9 | for i in range(n): | n | 9 | for x in range(n): |
10 | new_string = "" | 10 | new_string = "" | ||
11 | for letter in self.str: | 11 | for letter in self.str: | ||
12 | if letter in self.dictionary: | 12 | if letter in self.dictionary: | ||
13 | new_string += self.dictionary[letter] | 13 | new_string += self.dictionary[letter] | ||
14 | else: | 14 | else: | ||
15 | new_string += letter | 15 | new_string += letter | ||
16 | self.str = new_string | 16 | self.str = new_string | ||
17 | def drawPlant(self): | 17 | def drawPlant(self): | ||
18 | currentPt = (200,0) | 18 | currentPt = (200,0) | ||
19 | stack = [] | 19 | stack = [] | ||
20 | theta = 90 | 20 | theta = 90 | ||
21 | for letter in self.str: | 21 | for letter in self.str: | ||
22 | if letter == "[": | 22 | if letter == "[": | ||
23 | stack.append([currentPt,theta]) | 23 | stack.append([currentPt,theta]) | ||
24 | elif letter == "]": | 24 | elif letter == "]": | ||
n | 25 | current_save = stack.pop() | n | 25 | latest = stack[-1] |
26 | theta = current_save[1] | ||||
27 | currentPt = current_save[0] | 26 | currentPt = latest[0] | ||
27 | theta = latest[1] | ||||
28 | elif letter == "+": | 28 | elif letter == "+": | ||
29 | theta += self.deltaTheta | 29 | theta += self.deltaTheta | ||
30 | elif letter == "-": | 30 | elif letter == "-": | ||
31 | theta -= self.deltaTheta | 31 | theta -= self.deltaTheta | ||
32 | else: | 32 | else: | ||
n | 33 | nextPt = (currentPt[0] + math.cos(math.radians(theta)), currentP | n | 33 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.si |
> | t[1] + math.sin(math.radians(theta))) | > | n(theta)) | ||
34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | 34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||
> | olor='black') | > | olor='black') | ||
35 | currentPt = nextPtclass DNAMOTIF: | 35 | currentPt = nextPtclass DNAMOTIF: | ||
n | 36 | def __init_(self): | n | 36 | def __init__(self): |
37 | self.instances = [] | 37 | self.instances = [] | ||
38 | self.consensus = [] | 38 | self.consensus = [] | ||
39 | self.counts = {"A": [], "C": [], "G": [], "T": []} | 39 | self.counts = {"A": [], "C": [], "G": [], "T": []} | ||
40 | def __str__(self): | 40 | def __str__(self): | ||
41 | return "\n".join(self.instances) | 41 | return "\n".join(self.instances) | ||
42 | def __len__(self): | 42 | def __len__(self): | ||
43 | return len(self.instances[0])-1 | 43 | return len(self.instances[0])-1 | ||
44 | def parse(self, filename): | 44 | def parse(self, filename): | ||
45 | with open(filename, 'r') as reader: | 45 | with open(filename, 'r') as reader: | ||
n | 46 | self.instances = [i[0].lower()+i[1:].upper() for i in reader.readlin | n | 46 | self.instances = [x[0].lower() + x[1:].upper() for x in reader.readl |
> | es() if i[0] != ">"] | > | ines() if x[0] != ">"] | ||
47 | def count(self): | 47 | def count(self): | ||
n | n | 48 | count = 0 | ||
48 | self.counts = {"A": [0 for _ in range(len(self))], "C": [0 for _ in rang | 49 | self.counts = {"A": [0 for _ in range(len(self))], "C": [0 for _ in rang | ||
> | e(len(self))], "G": [0 for _ in range(len(self))], "T": [0 for _ in range(len(se | > | e(len(self))], "G": [0 for _ in range(len(self))], "T": [0 for _ in range(len(se | ||
> | lf))]} | > | lf))]} | ||
49 | for instance in self.instances: | 50 | for instance in self.instances: | ||
n | 50 | for i, letter in enumerate(instance): | n | 51 | for x, letter in enumerate(instance): |
51 | if letter != "\n": | 52 | if letter != "\n": | ||
n | 52 | self.counts[letter.upper()][i] += 1 | n | 53 | self.counts[letter.upper()][x] += 1 |
53 | def compute_consensus(self): | 54 | def compute_consensus(self): | ||
54 | self.count() | 55 | self.count() | ||
55 | consensus = "" | 56 | consensus = "" | ||
n | 56 | for i in range(len(self)): | n | 57 | for x in range(len(self)): |
57 | current_consensus = ("",0) | 58 | current_consensus = ("",0) | ||
58 | for letter in self.counts: | 59 | for letter in self.counts: | ||
n | 59 | if self.counts[letter][i] > current_consensus[1]: | n | 60 | if self.counts[letter][x] > current_consensus[1]: |
60 | current_consensus = (letter, self.counts[letter][i]) | 61 | current_consensus = (letter, self.counts[letter][x]) | ||
61 | consensus += current_consensus[0] | 62 | consensus += current_consensus[0] | ||
62 | self.consensus = consensus | 63 | self.consensus = consensus | ||
t | t | 64 | def count(self): | ||
65 | self.counts = {"A": [0 for _ in range(len(self))], "C": [0 for _ in rang | ||||
> | e(len(self))], "G": [0 for _ in range(len(self))], "T": [0 for _ in range(len(se | ||||
> | lf))]} | ||||
66 | for instance in self.instances: | ||||
67 | for x, letter in enumerate(instance): | ||||
68 | if letter != "\n": | ||||
69 | self.counts[letter.upper()][x] += 1 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | 2 | import numpy as np | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
5 | self.string = string | 5 | self.string = string | ||
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | self.str = PLANT.generator(string,dictionary, n) | 9 | self.str = PLANT.generator(string,dictionary, n) | ||
10 | def generator(string, dictionary, n): | 10 | def generator(string, dictionary, n): | ||
n | 11 | def production_rule(string): | n | 11 | def character_v(string): |
12 | if string in dictionary: | 12 | if string in dictionary: | ||
13 | return dictionary.get(string) | 13 | return dictionary.get(string) | ||
14 | return string | 14 | return string | ||
n | 15 | list_of_substrings = [string] | n | 15 | substrings_list = [string] |
16 | for i in range(n): | 16 | for i in range(n): | ||
n | 17 | current_substring = list_of_substrings[-1] | n | 17 | substring = substrings_list[-1] |
18 | new_axiom = [production_rule(char) for char in current_substring] | 18 | new_statement = [character_v(char) for char in substring] | ||
19 | list_of_substrings.append(''.join(new_axiom)) | 19 | substrings_list.append(''.join(new_statement)) | ||
20 | newstring = list_of_substrings[n] | 20 | newstring = substrings_list[n] | ||
21 | return newstring | 21 | return newstring | ||
22 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | 23 | uppera = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M | n | 23 | upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', |
> | ', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | > | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||
24 | currentPt=(200,0) | 24 | currentPt=(200,0) | ||
25 | theta = np.radians(90) | 25 | theta = np.radians(90) | ||
26 | deltaTheta = np.radians(self.deltaTheta) | 26 | deltaTheta = np.radians(self.deltaTheta) | ||
27 | stack = [currentPt, theta] | 27 | stack = [currentPt, theta] | ||
28 | for command in self.str: | 28 | for command in self.str: | ||
n | 29 | if command.upper() in uppera: | n | 29 | if command.upper() in upper_letters: |
30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | 30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | ||
> | ta))) | > | ta))) | ||
31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
n | 32 | currentPt = nextPt | n | 32 | currentPt = nextPt |
33 | else: | 33 | else: | ||
34 | if command == '[': | 34 | if command == '[': | ||
35 | stack.append(currentPt) | 35 | stack.append(currentPt) | ||
36 | stack.append(theta) | 36 | stack.append(theta) | ||
37 | elif command == ']': | 37 | elif command == ']': | ||
38 | theta = stack.pop() | 38 | theta = stack.pop() | ||
n | 39 | currentPt = stack.pop() | n | 39 | currentPt = stack.pop() |
40 | elif command == '-': | ||||
41 | theta -= deltaTheta | ||||
40 | elif command == '+': | 42 | elif command == '+': | ||
41 | theta += deltaTheta | 43 | theta += deltaTheta | ||
n | 42 | elif command == '-': | n | ||
43 | theta -= deltaTheta | ||||
44 | return plt.plot | 44 | return plt.plot | ||
45 | if __name__ == "__main__": | 45 | if __name__ == "__main__": | ||
46 | string = input() | 46 | string = input() | ||
47 | dictionary = input() | 47 | dictionary = input() | ||
48 | n = int(input()) | 48 | n = int(input()) | ||
49 | deltaTheta = float(input()) | 49 | deltaTheta = float(input()) | ||
50 | p = PLANT(string, dictionary, n, deltaTheta) | 50 | p = PLANT(string, dictionary, n, deltaTheta) | ||
51 | p.drawPlant() | 51 | p.drawPlant() | ||
n | 52 | plt.show()class DNAMOTIF: | n | 52 | plt.show()class DNAMOTIF: |
53 | def __init__(self): | 53 | def __init__(self): | ||
54 | self.instances=[] | 54 | self.instances=[] | ||
55 | self.consensus=[] | 55 | self.consensus=[] | ||
56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
57 | def __str__(self): | 57 | def __str__(self): | ||
58 | output = "" | 58 | output = "" | ||
59 | for instance in self.instances: | 59 | for instance in self.instances: | ||
60 | output += instance | 60 | output += instance | ||
61 | return output | 61 | return output | ||
62 | def __len__(self): | 62 | def __len__(self): | ||
63 | return len(self.instances[0].rstrip()) | 63 | return len(self.instances[0].rstrip()) | ||
64 | def count(self): | 64 | def count(self): | ||
65 | for position in range(len(self)): | 65 | for position in range(len(self)): | ||
66 | sequenceA = 0 | 66 | sequenceA = 0 | ||
67 | sequenceT = 0 | 67 | sequenceT = 0 | ||
68 | sequenceC = 0 | 68 | sequenceC = 0 | ||
69 | sequenceG = 0 | 69 | sequenceG = 0 | ||
70 | for instance in self.instances: | 70 | for instance in self.instances: | ||
71 | sequence = instance.rstrip() | 71 | sequence = instance.rstrip() | ||
72 | if (sequence[position]).upper() == "A": | 72 | if (sequence[position]).upper() == "A": | ||
73 | sequenceA += 1 | 73 | sequenceA += 1 | ||
74 | elif (sequence[position]).upper() == "T": | 74 | elif (sequence[position]).upper() == "T": | ||
75 | sequenceT += 1 | 75 | sequenceT += 1 | ||
76 | elif (sequence[position]).upper() == "C": | 76 | elif (sequence[position]).upper() == "C": | ||
77 | sequenceC += 1 | 77 | sequenceC += 1 | ||
78 | elif (sequence[position]).upper() == "G": | 78 | elif (sequence[position]).upper() == "G": | ||
79 | sequenceG += 1 | 79 | sequenceG += 1 | ||
80 | self.counts.get("A").append(sequenceA) | 80 | self.counts.get("A").append(sequenceA) | ||
81 | self.counts.get("T").append(sequenceT) | 81 | self.counts.get("T").append(sequenceT) | ||
82 | self.counts.get("C").append(sequenceC) | 82 | self.counts.get("C").append(sequenceC) | ||
83 | self.counts.get("G").append(sequenceG) | 83 | self.counts.get("G").append(sequenceG) | ||
84 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
85 | DNAMOTIF.count(self) | 85 | DNAMOTIF.count(self) | ||
86 | A = self.counts.get("A") | 86 | A = self.counts.get("A") | ||
87 | T = self.counts.get("T") | 87 | T = self.counts.get("T") | ||
88 | C = self.counts.get("C") | 88 | C = self.counts.get("C") | ||
89 | G = self.counts.get("G") | 89 | G = self.counts.get("G") | ||
90 | output = "" | 90 | output = "" | ||
91 | for row in range(len(A)): | 91 | for row in range(len(A)): | ||
n | 92 | maximum = max(A[row],T[row],C[row],G[row]) | n | 92 | maxs = max(A[row],T[row],C[row],G[row]) |
93 | if maximum == A[row]: | 93 | if maxs == A[row]: | ||
94 | output += "A" | 94 | output += "A" | ||
n | 95 | elif maximum == T[row]: | n | 95 | elif maxs == T[row]: |
96 | output += "T" | 96 | output += "T" | ||
n | 97 | elif maximum == C[row]: | n | 97 | elif maxs == C[row]: |
98 | output += "C" | 98 | output += "C" | ||
n | 99 | elif maximum == G[row]: | n | 99 | elif maxs == G[row]: |
100 | output += "G" | 100 | output += "G" | ||
101 | self.consensus = output | 101 | self.consensus = output | ||
102 | def parse(self, filename): | 102 | def parse(self, filename): | ||
n | 103 | myFile = open(filename, 'r') | n | 103 | myFile = open(filename, 'r') |
104 | contents = [line for line in myFile.readlines()] | 104 | contents = [line for line in myFile.readlines()] | ||
105 | myFile.close() | 105 | myFile.close() | ||
106 | for index, line in enumerate(contents): | 106 | for index, line in enumerate(contents): | ||
107 | if index % 2 != 0: | 107 | if index % 2 != 0: | ||
108 | self.instances.append(line) | 108 | self.instances.append(line) | ||
109 | if __name__ == '__main__': | 109 | if __name__ == '__main__': | ||
n | 110 | lexA=DNAMOTIF() | n | 110 | lexA=DNAMOTIF() |
111 | filename = r'FinalProject\lexA.fasta' | 111 | filename = r'FinalProject\lexA.fasta' | ||
t | 112 | lexA.parse(filename) | t | 112 | lexA.parse(filename) |
113 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
114 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | plt.style.use('bmh') | n | 2 | plt.style.use('bmh') |
3 | class PLANT: | ||||
4 | def __initializer__(p): | ||||
5 | p.start=[] | ||||
6 | plt.plot( | 3 | plt.plot( | ||
7 | [0, 1, 2], | 4 | [0, 1, 2], | ||
8 | [0, 1, 0] | 5 | [0, 1, 0] | ||
9 | ) | 6 | ) | ||
10 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
11 | plt.ylabel('y'); | 8 | plt.ylabel('y'); | ||
12 | def plot_coords(coords, bare_plot=False): | 9 | def plot_coords(coords, bare_plot=False): | ||
13 | if bare_plot: | 10 | if bare_plot: | ||
14 | plt.axis('off') | 11 | plt.axis('off') | ||
15 | plt.axes().set_aspect('equal', 'datalim') | 12 | plt.axes().set_aspect('equal', 'datalim') | ||
16 | X, Y = zip(*coords) | 13 | X, Y = zip(*coords) | ||
17 | plt.plot(X, Y); | 14 | plt.plot(X, Y); | ||
18 | plot_coords([ | 15 | plot_coords([ | ||
19 | (0, 0), | 16 | (0, 0), | ||
20 | (1, 0), | 17 | (1, 0), | ||
21 | (2, 1), | 18 | (2, 1), | ||
22 | (3, 1), | 19 | (3, 1), | ||
23 | (2, 0) | 20 | (2, 0) | ||
24 | ]) | 21 | ]) | ||
25 | nan = float('nan') | 22 | nan = float('nan') | ||
26 | plot_coords([ | 23 | plot_coords([ | ||
27 | (0, 0), | 24 | (0, 0), | ||
28 | (1, 1), | 25 | (1, 1), | ||
29 | (nan, nan), | 26 | (nan, nan), | ||
30 | (1, 0), | 27 | (1, 0), | ||
31 | (2, 1) | 28 | (2, 1) | ||
32 | ]) | 29 | ]) | ||
33 | from math import pi, sin, cos | 30 | from math import pi, sin, cos | ||
34 | DEGREES_TO_RADIANS = pi / 180 | 31 | DEGREES_TO_RADIANS = pi / 180 | ||
35 | def turtle_to_coords(turtle_program, turn_amount=45): | 32 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
36 | state = (0.0, 0.0, 90.0) | 33 | state = (0.0, 0.0, 90.0) | ||
37 | yield (0.0, 0.0) | 34 | yield (0.0, 0.0) | ||
38 | for command in turtle_program: | 35 | for command in turtle_program: | ||
39 | x, y, angle = state | 36 | x, y, angle = state | ||
40 | if command in 'Ff': | 37 | if command in 'Ff': | ||
41 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
42 | y + sin(angle * DEGREES_TO_RADIANS), | 39 | y + sin(angle * DEGREES_TO_RADIANS), | ||
43 | angle) | 40 | angle) | ||
44 | if command == 'f': | 41 | if command == 'f': | ||
45 | yield (float('nan'), float('nan')) | 42 | yield (float('nan'), float('nan')) | ||
46 | yield (state[0], state[1]) | 43 | yield (state[0], state[1]) | ||
47 | elif command == '+': | 44 | elif command == '+': | ||
48 | state = (x, y, angle + turn_amount) | 45 | state = (x, y, angle + turn_amount) | ||
49 | elif command == '-': | 46 | elif command == '-': | ||
50 | state = (x, y, angle - turn_amount) | 47 | state = (x, y, angle - turn_amount) | ||
51 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | 48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
52 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
53 | from math import isnan | 50 | from math import isnan | ||
54 | def print_coords(coords): | 51 | def print_coords(coords): | ||
55 | for (x, y) in coords: | 52 | for (x, y) in coords: | ||
56 | if isnan(x): | 53 | if isnan(x): | ||
57 | print('<gap>') | 54 | print('<gap>') | ||
58 | else: | 55 | else: | ||
59 | print('({:.2f}, {:.2f})'.format(x, y)) | 56 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
60 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
61 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | 58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
62 | def transform_sequence(sequence, transformations): | 59 | def transform_sequence(sequence, transformations): | ||
63 | return ''.join(transformations.get(c, c) for c in sequence) | 60 | return ''.join(transformations.get(c, c) for c in sequence) | ||
64 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | 61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | ||
65 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | 62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
66 | def transform_multiple(sequence, transformations, iterations): | 63 | def transform_multiple(sequence, transformations, iterations): | ||
67 | for _ in range(iterations): | 64 | for _ in range(iterations): | ||
68 | sequence = transform_sequence(sequence, transformations) | 65 | sequence = transform_sequence(sequence, transformations) | ||
69 | return sequence | 66 | return sequence | ||
70 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | 67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | ||
71 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
72 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
73 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | 70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
74 | plot_coords(turtle_to_coords(transform_multiple('L', { | 71 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
75 | 'L': '-RF+LFL+FR-', | 72 | 'L': '-RF+LFL+FR-', | ||
76 | 'R': '+LF-RFR-FL+' | 73 | 'R': '+LF-RFR-FL+' | ||
77 | }, 5), 90)) | 74 | }, 5), 90)) | ||
78 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | 75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
79 | saved_states = list() | 76 | saved_states = list() | ||
80 | state = (0, 0, 90) | 77 | state = (0, 0, 90) | ||
81 | yield (0, 0) | 78 | yield (0, 0) | ||
82 | for command in turtle_program: | 79 | for command in turtle_program: | ||
83 | x, y, angle = state | 80 | x, y, angle = state | ||
84 | if command.lower() in 'abcdefghij': | 81 | if command.lower() in 'abcdefghij': | ||
85 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
86 | y + sin(angle * DEGREES_TO_RADIANS), | 83 | y + sin(angle * DEGREES_TO_RADIANS), | ||
87 | angle) | 84 | angle) | ||
88 | if command.islower(): | 85 | if command.islower(): | ||
89 | yield (float('nan'), float('nan')) | 86 | yield (float('nan'), float('nan')) | ||
90 | yield (state[0], state[1]) | 87 | yield (state[0], state[1]) | ||
91 | elif command == '+': | 88 | elif command == '+': | ||
92 | state = (x, y, angle + turn_amount) | 89 | state = (x, y, angle + turn_amount) | ||
93 | elif command == '-': | 90 | elif command == '-': | ||
94 | state = (x, y, angle - turn_amount) | 91 | state = (x, y, angle - turn_amount) | ||
95 | elif command == '[': | 92 | elif command == '[': | ||
96 | saved_states.append(state) | 93 | saved_states.append(state) | ||
97 | elif command == ']': | 94 | elif command == ']': | ||
98 | state = saved_states.pop() | 95 | state = saved_states.pop() | ||
99 | yield (float('nan'), float('nan')) | 96 | yield (float('nan'), float('nan')) | ||
100 | x, y, _ = state | 97 | x, y, _ = state | ||
101 | yield (x, y) | 98 | yield (x, y) | ||
102 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | 99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
103 | def l_plot(axiom, transformations, iterations=0, angle=45): | 100 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
104 | turtle_program = transform_multiple(axiom, transformations, iterations) | 101 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
105 | coords = branching_turtle_to_coords(turtle_program, angle) | 102 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
106 | plot_coords(coords, bare_plot=True) | 103 | plot_coords(coords, bare_plot=True) | ||
107 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | 104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | ||
108 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
109 | for i in range(5): | 106 | for i in range(5): | ||
110 | print('{}: '.format(i), | 107 | print('{}: '.format(i), | ||
111 | transform_multiple('A', {'A': 'F+A'}, i)) | 108 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
112 | for i in range(5): | 109 | for i in range(5): | ||
113 | print('{}: '.format(i), | 110 | print('{}: '.format(i), | ||
114 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | 111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
n | 115 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | n | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: |
116 | def __init__(self): | 113 | def __init__(self): | ||
n | 117 | self.instances=[] | n | 114 | self.instances = [] |
118 | self.consensus=[] | 115 | self.consensus=[] | ||
119 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | ||
120 | def __str__(self): | 117 | def __str__ (self): | ||
118 | string = "" | ||||
121 | for i in self.instances: | 119 | for i in self.instances: | ||
n | 122 | string+=i+"\n" | n | 120 | string += i + "\n" |
123 | return string[:-2] | 121 | return string[:-2] | ||
124 | def __len__(self): | 122 | def __len__(self): | ||
n | 125 | return len(self.instances) + 1 | n | 123 | return len(self.instances) |
126 | def count(self): | 124 | def count(self): | ||
127 | for i in self.instances: | 125 | for i in self.instances: | ||
n | 128 | temp=i.upper() | n | 126 | temp = i.upper() |
129 | self.counts["A"].append(temp.count("A")) | 127 | self.counts["A"].append(temp.count("A")) | ||
130 | self.counts["C"].append(temp.count("C")) | 128 | self.counts["C"].append(temp.count("C")) | ||
131 | self.counts["G"].append(temp.count("G")) | 129 | self.counts["G"].append(temp.count("G")) | ||
132 | self.counts["T"].append(temp.count("T")) | 130 | self.counts["T"].append(temp.count("T")) | ||
n | 133 | return self.counts | n | ||
134 | def compute_consensus(self): | 131 | def compute_consensus(self): | ||
n | 135 | pass | n | ||
136 | A=self.counts["A"] | 132 | A = self.counts["A"] | ||
137 | C=self.counts["C"] | 133 | C = self.counts["C"] | ||
138 | G=self.counts["G"] | 134 | G = self.counts["G"] | ||
139 | T=self.counts["T"] | 135 | T = self.counts["T"] | ||
140 | for i in range(len(A)): | 136 | for i in range(len(A)): | ||
n | 141 | if(A[i]>=C[i] and A[i] >= G[i] and A[i]>= T[i]): | n | 137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): |
142 | G=self.counts["G"] | ||||
143 | T=self.counts["T"] | ||||
144 | for i in range(len(A)): | ||||
145 | if(A[i]>=C[i] and A[i]>=G[i] and A[i]>=T[i]): | ||||
146 | self.consensus.append("A") | 138 | self.consensus.append("A") | ||
n | 147 | elif(C[i]>=G[i] and C[i]>=T[i]): | n | 139 | elif (C[i] >= G[i] and C[i] >= T[i]): |
148 | self.consensus.append("C") | 140 | self.consensus.append("C") | ||
n | 149 | elif(G[i]>=T[i]): | n | 141 | elif (G[i] >= T[i]): |
150 | self.consensus.append("G") | 142 | self.consensus.append("G") | ||
151 | else: | 143 | else: | ||
152 | self.consensus.append("T") | 144 | self.consensus.append("T") | ||
n | 153 | return self.consensus | n | ||
154 | def parse(self, filename): | 145 | def parse(self, filename): | ||
155 | with open(filename,'r') as f: | 146 | with open(filename,'r') as f: | ||
156 | for i in f: | 147 | for i in f: | ||
n | 157 | if">"in i: | n | 148 | if ">" in i: |
158 | continue | 149 | continue | ||
159 | else: | 150 | else: | ||
160 | self.instances.append(i) | 151 | self.instances.append(i) | ||
t | 161 | return self.instances | t | 152 | if __name__=='__main__': |
162 | lexA=DNAMOTIF() | 153 | lex=DNAMOTIF() | ||
163 | lexA.parse("lexA.fasta") | 154 | lex.parse('lexA.fasta') | ||
164 | print(len(lexA)) | 155 | print("length",len(lex)) | ||
165 | lexA.count() | 156 | print(lex) | ||
166 | print(lexA.count()) | 157 | print("consensus",lex.consensus) | ||
167 | lexA.compute_consensus() | 158 | print('seq',lex.counts) | ||
168 | print(lexA.compute_consensus()) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | from math import pi, sin, cos | n | 2 | from math import sin, cos, radians |
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, init, gen, n, theta_delta): | n | 4 | def __init__(self, init, gen, n, delta_theta): |
5 | self.init_state = init | 5 | self.init_state = init | ||
6 | self.generator = gen | 6 | self.generator = gen | ||
7 | self.n = n | 7 | self.n = n | ||
n | 8 | self.theta_delta = theta_delta | n | 8 | self.delta_theta = delta_theta |
9 | self.str = self.generate() | 9 | self.str = self.generate() | ||
10 | def generate(self): | 10 | def generate(self): | ||
11 | result = self.init_state | 11 | result = self.init_state | ||
12 | for i in range(self.n): | 12 | for i in range(self.n): | ||
n | 13 | new_result = "" | n | 13 | new_res = "" |
14 | for char in result: | 14 | for char in result: | ||
15 | if char in self.generator: | 15 | if char in self.generator: | ||
n | 16 | new_result += self.generator[char] | n | 16 | new_res += self.generator[char] |
17 | else: | 17 | else: | ||
n | 18 | new_result += char | n | 18 | new_res += char |
19 | result = new_result | 19 | result = new_res | ||
20 | return result | 20 | return result | ||
21 | def drawPlant(self): | 21 | def drawPlant(self): | ||
22 | currentPt = (200, 0) | 22 | currentPt = (200, 0) | ||
23 | theta = 90 | 23 | theta = 90 | ||
24 | stack = [] | 24 | stack = [] | ||
25 | for char in self.str: | 25 | for char in self.str: | ||
26 | if char.isalpha(): | 26 | if char.isalpha(): | ||
n | 27 | nextPt = (currentPt[0] + cos(theta), currentPt[1] + sin(theta)) | n | 27 | nextPt = (currentPt[0] + cos(radians(theta)), currentPt[1] + sin |
> | (radians(theta))) | ||||
28 | plt.plot( | 28 | plt.plot( | ||
29 | [currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], color= | 29 | [currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], color= | ||
> | "black" | > | "black" | ||
30 | ) | 30 | ) | ||
31 | currentPt = nextPt | 31 | currentPt = nextPt | ||
32 | elif char == "[": | 32 | elif char == "[": | ||
33 | stack.append([currentPt, theta]) | 33 | stack.append([currentPt, theta]) | ||
34 | elif char == "]": | 34 | elif char == "]": | ||
35 | [currentPt, theta] = stack[-1] | 35 | [currentPt, theta] = stack[-1] | ||
36 | elif char == "+": | 36 | elif char == "+": | ||
n | 37 | theta += self.theta_delta | n | 37 | theta += self.delta_theta |
38 | elif char == "-": | 38 | elif char == "-": | ||
n | 39 | theta -= self.theta_delta | n | 39 | theta -= self.delta_theta |
40 | myPlant=PLANT('X',{ 'X' : 'F[+X]F[-X]+X','F' : 'FF'},2,20) | ||||
41 | myPlant.str=='FF[+F[+X]F[-X]+X]FF[-F[+X]F[-X]+X]+F[+X]F[-X]+X' | ||||
42 | plt.axis("image") | 40 | plt.axis("image")class DNAMOTIF: | ||
43 | class DNAMOTIF: | ||||
44 | def __init__(self): | 41 | def __init__(self): | ||
45 | self.instances = [] | 42 | self.instances = [] | ||
46 | self.consensus = [] | 43 | self.consensus = [] | ||
47 | self.counts = {"A": [], "C": [], "G": [], "T": []} | 44 | self.counts = {"A": [], "C": [], "G": [], "T": []} | ||
48 | def __str__(self): | 45 | def __str__(self): | ||
49 | return "".join([str(item) for item in self.instances]) | 46 | return "".join([str(item) for item in self.instances]) | ||
50 | def __len__(self): | 47 | def __len__(self): | ||
51 | return len(self.instances[0].strip("\n")) | 48 | return len(self.instances[0].strip("\n")) | ||
52 | def count(self): | 49 | def count(self): | ||
53 | for key in self.counts: | 50 | for key in self.counts: | ||
54 | self.counts[key] = [0] * len(self) | 51 | self.counts[key] = [0] * len(self) | ||
n | 55 | for minstances in self.instances: | n | 52 | for instance in self.instances: |
56 | minstances = minstances.strip("\n") | 53 | instance = instance.strip("\n") | ||
57 | for i in range(len(self)): | 54 | for i in range(len(self)): | ||
n | 58 | if minstances[i].upper() in self.counts: | n | 55 | if instance[i].upper() in self.counts: |
59 | self.counts[minstances[i].upper()][i] += 1 | 56 | self.counts[instance[i].upper()][i] += 1 | ||
60 | def compute_consensus(self): | 57 | def compute_consensus(self): | ||
61 | self.count() | 58 | self.count() | ||
62 | self.consensus = [0] * len(self.counts['A']) | 59 | self.consensus = [0] * len(self.counts['A']) | ||
63 | for i in range(len(self.counts['A'])): | 60 | for i in range(len(self.counts['A'])): | ||
t | 64 | maximumkey = max(self.counts, key=lambda x: self.counts[x][i]) | t | 61 | keymax = max(self.counts, key=lambda x: self.counts[x][i]) |
65 | self.consensus[i] = maximumkey | 62 | self.consensus[i] = keymax | ||
66 | self.consensus = "".join([str(item) for item in self.consensus]) | 63 | self.consensus = "".join([str(item) for item in self.consensus]) | ||
67 | def parse(self, filename): | 64 | def parse(self, filename): | ||
68 | with open(filename) as fp: | 65 | with open(filename) as fp: | ||
69 | for line in fp: | 66 | for line in fp: | ||
70 | if line[0] != ">": | 67 | if line[0] != ">": | ||
71 | self.instances.append("{}\n".format(line.strip())) | 68 | self.instances.append("{}\n".format(line.strip())) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | from math import sin, cos, radians | n | 2 | from math import pi, sin, cos |
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, init, gen, n, delta_theta): | n | 4 | def __init__(self, init, gen, n, theta_delta): |
5 | self.init_state = init | 5 | self.init_state = init | ||
6 | self.generator = gen | 6 | self.generator = gen | ||
7 | self.n = n | 7 | self.n = n | ||
n | 8 | self.delta_theta = delta_theta | n | 8 | self.theta_delta = theta_delta |
9 | self.str = self.generate() | 9 | self.str = self.generate() | ||
10 | def generate(self): | 10 | def generate(self): | ||
11 | result = self.init_state | 11 | result = self.init_state | ||
12 | for i in range(self.n): | 12 | for i in range(self.n): | ||
n | 13 | new_res = "" | n | 13 | new_result = "" |
14 | for char in result: | 14 | for char in result: | ||
15 | if char in self.generator: | 15 | if char in self.generator: | ||
n | 16 | new_res += self.generator[char] | n | 16 | new_result += self.generator[char] |
17 | else: | 17 | else: | ||
n | 18 | new_res += char | n | 18 | new_result += char |
19 | result = new_res | 19 | result = new_result | ||
20 | return result | 20 | return result | ||
21 | def drawPlant(self): | 21 | def drawPlant(self): | ||
22 | currentPt = (200, 0) | 22 | currentPt = (200, 0) | ||
23 | theta = 90 | 23 | theta = 90 | ||
24 | stack = [] | 24 | stack = [] | ||
25 | for char in self.str: | 25 | for char in self.str: | ||
26 | if char.isalpha(): | 26 | if char.isalpha(): | ||
n | 27 | nextPt = (currentPt[0] + cos(radians(theta)), currentPt[1] + sin | n | 27 | nextPt = (currentPt[0] + cos(theta), currentPt[1] + sin(theta)) |
> | (radians(theta))) | ||||
28 | plt.plot( | 28 | plt.plot( | ||
29 | [currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], color= | 29 | [currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], color= | ||
> | "black" | > | "black" | ||
30 | ) | 30 | ) | ||
31 | currentPt = nextPt | 31 | currentPt = nextPt | ||
32 | elif char == "[": | 32 | elif char == "[": | ||
33 | stack.append([currentPt, theta]) | 33 | stack.append([currentPt, theta]) | ||
34 | elif char == "]": | 34 | elif char == "]": | ||
35 | [currentPt, theta] = stack[-1] | 35 | [currentPt, theta] = stack[-1] | ||
36 | elif char == "+": | 36 | elif char == "+": | ||
n | 37 | theta += self.delta_theta | n | 37 | theta += self.theta_delta |
38 | elif char == "-": | 38 | elif char == "-": | ||
n | 39 | theta -= self.delta_theta | n | 39 | theta -= self.theta_delta |
40 | myPlant=PLANT('X',{ 'X' : 'F[+X]F[-X]+X','F' : 'FF'},2,20) | ||||
41 | myPlant.str=='FF[+F[+X]F[-X]+X]FF[-F[+X]F[-X]+X]+F[+X]F[-X]+X' | ||||
40 | plt.axis("image")class DNAMOTIF: | 42 | plt.axis("image") | ||
43 | class DNAMOTIF: | ||||
41 | def __init__(self): | 44 | def __init__(self): | ||
42 | self.instances = [] | 45 | self.instances = [] | ||
43 | self.consensus = [] | 46 | self.consensus = [] | ||
44 | self.counts = {"A": [], "C": [], "G": [], "T": []} | 47 | self.counts = {"A": [], "C": [], "G": [], "T": []} | ||
45 | def __str__(self): | 48 | def __str__(self): | ||
46 | return "".join([str(item) for item in self.instances]) | 49 | return "".join([str(item) for item in self.instances]) | ||
47 | def __len__(self): | 50 | def __len__(self): | ||
48 | return len(self.instances[0].strip("\n")) | 51 | return len(self.instances[0].strip("\n")) | ||
49 | def count(self): | 52 | def count(self): | ||
50 | for key in self.counts: | 53 | for key in self.counts: | ||
51 | self.counts[key] = [0] * len(self) | 54 | self.counts[key] = [0] * len(self) | ||
n | 52 | for instance in self.instances: | n | 55 | for minstances in self.instances: |
53 | instance = instance.strip("\n") | 56 | minstances = minstances.strip("\n") | ||
54 | for i in range(len(self)): | 57 | for i in range(len(self)): | ||
n | 55 | if instance[i].upper() in self.counts: | n | 58 | if minstances[i].upper() in self.counts: |
56 | self.counts[instance[i].upper()][i] += 1 | 59 | self.counts[minstances[i].upper()][i] += 1 | ||
57 | def compute_consensus(self): | 60 | def compute_consensus(self): | ||
58 | self.count() | 61 | self.count() | ||
59 | self.consensus = [0] * len(self.counts['A']) | 62 | self.consensus = [0] * len(self.counts['A']) | ||
60 | for i in range(len(self.counts['A'])): | 63 | for i in range(len(self.counts['A'])): | ||
t | 61 | keymax = max(self.counts, key=lambda x: self.counts[x][i]) | t | 64 | maximumkey = max(self.counts, key=lambda x: self.counts[x][i]) |
62 | self.consensus[i] = keymax | 65 | self.consensus[i] = maximumkey | ||
63 | self.consensus = "".join([str(item) for item in self.consensus]) | 66 | self.consensus = "".join([str(item) for item in self.consensus]) | ||
64 | def parse(self, filename): | 67 | def parse(self, filename): | ||
65 | with open(filename) as fp: | 68 | with open(filename) as fp: | ||
66 | for line in fp: | 69 | for line in fp: | ||
67 | if line[0] != ">": | 70 | if line[0] != ">": | ||
68 | self.instances.append("{}\n".format(line.strip())) | 71 | self.instances.append("{}\n".format(line.strip())) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import matplotlib.pyplot as plt | n | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | 2 | import numpy as np | ||
n | 3 | import string | n | ||
4 | class PLANT: | 3 | class PLANT: | ||
n | 5 | def __init__(self, init_state = '', dictionary = {}, n = 0, deltaTheta = 0): | n | 4 | def __init__(self, string, dictionary, n, deltaTheta): |
6 | self.init_state = init_state | 5 | self.string = string | ||
7 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
8 | self.n = n | 7 | self.n = n | ||
9 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
n | 10 | self.str = PLANT.generator(init_state, dictionary, n) | n | 9 | self.str = PLANT.generator(string,dictionary, n) |
11 | def generator(init_state, dictionary, n): | 10 | def generator(string, dictionary, n): | ||
12 | def character_v(init_state): | 11 | def character_v(string): | ||
13 | if init_state in dictionary: | 12 | if string in dictionary: | ||
14 | return dictionary[init_state] | 13 | return dictionary.get(string) | ||
15 | return init_state | 14 | return string | ||
16 | substrings_list = [init_state] | 15 | substrings_list = [string] | ||
17 | for i in range(n): | 16 | for i in range(n): | ||
18 | substring = substrings_list[-1] | 17 | substring = substrings_list[-1] | ||
19 | new_statement = [character_v(char) for char in substring] | 18 | new_statement = [character_v(char) for char in substring] | ||
20 | substrings_list.append(''.join(new_statement)) | 19 | substrings_list.append(''.join(new_statement)) | ||
n | 21 | new_state = substrings_list[n] | n | 20 | newstring = substrings_list[n] |
22 | return new_state | 21 | return newstring | ||
23 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | 24 | upper_letters = list(string.ascii_uppercase) | n | 23 | upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', |
> | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||||
25 | currentPt=(200,0) | 24 | currentPt=(200,0) | ||
26 | theta = np.radians(90) | 25 | theta = np.radians(90) | ||
27 | deltaTheta = np.radians(self.deltaTheta) | 26 | deltaTheta = np.radians(self.deltaTheta) | ||
28 | stack = [currentPt, theta] | 27 | stack = [currentPt, theta] | ||
29 | for command in self.str: | 28 | for command in self.str: | ||
30 | if command.upper() in upper_letters: | 29 | if command.upper() in upper_letters: | ||
31 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | 30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | ||
> | ta))) | > | ta))) | ||
32 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
33 | currentPt = nextPt | 32 | currentPt = nextPt | ||
34 | else: | 33 | else: | ||
35 | if command == '[': | 34 | if command == '[': | ||
36 | stack.append(currentPt) | 35 | stack.append(currentPt) | ||
37 | stack.append(theta) | 36 | stack.append(theta) | ||
38 | elif command == ']': | 37 | elif command == ']': | ||
39 | theta = stack.pop() | 38 | theta = stack.pop() | ||
40 | currentPt = stack.pop() | 39 | currentPt = stack.pop() | ||
41 | elif command == '-': | 40 | elif command == '-': | ||
42 | theta -= deltaTheta | 41 | theta -= deltaTheta | ||
43 | elif command == '+': | 42 | elif command == '+': | ||
44 | theta += deltaTheta | 43 | theta += deltaTheta | ||
45 | return plt.plot | 44 | return plt.plot | ||
46 | if __name__ == "__main__": | 45 | if __name__ == "__main__": | ||
47 | string = input() | 46 | string = input() | ||
48 | dictionary = input() | 47 | dictionary = input() | ||
49 | n = int(input()) | 48 | n = int(input()) | ||
50 | deltaTheta = float(input()) | 49 | deltaTheta = float(input()) | ||
51 | p = PLANT(string, dictionary, n, deltaTheta) | 50 | p = PLANT(string, dictionary, n, deltaTheta) | ||
52 | p.drawPlant() | 51 | p.drawPlant() | ||
n | 53 | plt.show()class DNAMOTIF: | n | 52 | plt.show()class DNAMOTIF: |
54 | def __init__(self): | 53 | def __init__(self): | ||
55 | self.instances=[] | 54 | self.instances=[] | ||
56 | self.consensus=[] | 55 | self.consensus=[] | ||
57 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
58 | def __str__(self): | 57 | def __str__(self): | ||
n | 59 | outStr = "" | n | 58 | output = "" |
60 | for instance in self.instances: | 59 | for instance in self.instances: | ||
n | 61 | outStr += instance | n | 60 | output += instance |
62 | return outStr | 61 | return output | ||
63 | def __len__(self): | 62 | def __len__(self): | ||
64 | return len(self.instances[0].rstrip()) | 63 | return len(self.instances[0].rstrip()) | ||
65 | def count(self): | 64 | def count(self): | ||
n | 66 | for index in range(len(self)): | n | 65 | for position in range(len(self)): |
67 | sequenceA = 0 | 66 | sequenceA = 0 | ||
68 | sequenceT = 0 | 67 | sequenceT = 0 | ||
69 | sequenceC = 0 | 68 | sequenceC = 0 | ||
70 | sequenceG = 0 | 69 | sequenceG = 0 | ||
71 | for instance in self.instances: | 70 | for instance in self.instances: | ||
72 | sequence = instance.rstrip() | 71 | sequence = instance.rstrip() | ||
n | 73 | if (sequence[index]).upper() == "A": | n | 72 | if (sequence[position]).upper() == "A": |
74 | sequenceA += 1 | 73 | sequenceA += 1 | ||
n | 75 | elif (sequence[index]).upper() == "T": | n | 74 | elif (sequence[position]).upper() == "T": |
76 | sequenceT += 1 | 75 | sequenceT += 1 | ||
n | 77 | elif (sequence[index]).upper() == "C": | n | 76 | elif (sequence[position]).upper() == "C": |
78 | sequenceC += 1 | 77 | sequenceC += 1 | ||
n | 79 | elif (sequence[index]).upper() == "G": | n | 78 | elif (sequence[position]).upper() == "G": |
80 | sequenceG += 1 | 79 | sequenceG += 1 | ||
81 | self.counts.get("A").append(sequenceA) | 80 | self.counts.get("A").append(sequenceA) | ||
82 | self.counts.get("T").append(sequenceT) | 81 | self.counts.get("T").append(sequenceT) | ||
83 | self.counts.get("C").append(sequenceC) | 82 | self.counts.get("C").append(sequenceC) | ||
84 | self.counts.get("G").append(sequenceG) | 83 | self.counts.get("G").append(sequenceG) | ||
85 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
86 | DNAMOTIF.count(self) | 85 | DNAMOTIF.count(self) | ||
87 | A = self.counts.get("A") | 86 | A = self.counts.get("A") | ||
88 | T = self.counts.get("T") | 87 | T = self.counts.get("T") | ||
89 | C = self.counts.get("C") | 88 | C = self.counts.get("C") | ||
90 | G = self.counts.get("G") | 89 | G = self.counts.get("G") | ||
91 | output = "" | 90 | output = "" | ||
92 | for row in range(len(A)): | 91 | for row in range(len(A)): | ||
93 | maxs = max(A[row],T[row],C[row],G[row]) | 92 | maxs = max(A[row],T[row],C[row],G[row]) | ||
94 | if maxs == A[row]: | 93 | if maxs == A[row]: | ||
95 | output += "A" | 94 | output += "A" | ||
96 | elif maxs == T[row]: | 95 | elif maxs == T[row]: | ||
97 | output += "T" | 96 | output += "T" | ||
98 | elif maxs == C[row]: | 97 | elif maxs == C[row]: | ||
99 | output += "C" | 98 | output += "C" | ||
100 | elif maxs == G[row]: | 99 | elif maxs == G[row]: | ||
101 | output += "G" | 100 | output += "G" | ||
102 | self.consensus = output | 101 | self.consensus = output | ||
103 | def parse(self, filename): | 102 | def parse(self, filename): | ||
104 | myFile = open(filename, 'r') | 103 | myFile = open(filename, 'r') | ||
105 | contents = [line for line in myFile.readlines()] | 104 | contents = [line for line in myFile.readlines()] | ||
106 | myFile.close() | 105 | myFile.close() | ||
107 | for index, line in enumerate(contents): | 106 | for index, line in enumerate(contents): | ||
108 | if index % 2 != 0: | 107 | if index % 2 != 0: | ||
109 | self.instances.append(line) | 108 | self.instances.append(line) | ||
110 | if __name__ == '__main__': | 109 | if __name__ == '__main__': | ||
111 | lexA=DNAMOTIF() | 110 | lexA=DNAMOTIF() | ||
t | 112 | filename = r'C:\\Users\\aboub\\OneDrive\\ENGR 131\\IN CLASS ASSIGNMENT\\lexA | t | 111 | filename = r'FinalProject\lexA.fasta' |
> | .fasta' | ||||
113 | lexA.parse(filename) | 112 | lexA.parse(filename) | ||
114 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
115 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | import matplotlib.pyplot as plt | ||
2 | from math import pi, sin, cos | ||||
1 | class PLANT: | 3 | class PLANT: | ||
2 | def __init__(self, init_state = "", generator = {}, iters = 0, delta_theta = | 4 | def __init__(self, init_state = "", generator = {}, iters = 0, delta_theta = | ||
> | 0): | > | 0): | ||
3 | self.__start = init_state | 5 | self.__start = init_state | ||
4 | self.__generator = generator | 6 | self.__generator = generator | ||
5 | self.__iters = iters | 7 | self.__iters = iters | ||
6 | self.__delta = delta_theta | 8 | self.__delta = delta_theta | ||
7 | self.str = self.generator() | 9 | self.str = self.generator() | ||
8 | def generator(self): | 10 | def generator(self): | ||
n | 9 | passclass DNAMOTIF: | n | 11 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25)class DNAMOTIF: |
10 | def file(self): | 12 | def file(self): | ||
n | 11 | file = open('lexA.fasta', 'r') | n | 13 | file = open('lexA.fasta') |
12 | for line in file: | 14 | for line in file: | ||
13 | if line[0] != '>': | 15 | if line[0] != '>': | ||
14 | self.instances.append(line) | 16 | self.instances.append(line) | ||
15 | def __init__(self): | 17 | def __init__(self): | ||
16 | self.instances=[] | 18 | self.instances=[] | ||
17 | self.consensus=[] | 19 | self.consensus=[] | ||
18 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 20 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
19 | def __str__(self): | 21 | def __str__(self): | ||
20 | dna_str = "" | 22 | dna_str = "" | ||
21 | for string in self.instances: | 23 | for string in self.instances: | ||
22 | dna_str += (string + "\n") | 24 | dna_str += (string + "\n") | ||
23 | return dna_str | 25 | return dna_str | ||
24 | def __len__(self): | 26 | def __len__(self): | ||
25 | return len(self.instances) - 4 | 27 | return len(self.instances) - 4 | ||
26 | def count(self): | 28 | def count(self): | ||
27 | for i in self.instances: | 29 | for i in self.instances: | ||
28 | temp = i.upper() | 30 | temp = i.upper() | ||
29 | self.counts['A'].append(temp.count('A')) | 31 | self.counts['A'].append(temp.count('A')) | ||
30 | self.counts['C'].append(temp.count('C')) | 32 | self.counts['C'].append(temp.count('C')) | ||
31 | self.counts['G'].append(temp.count('G')) | 33 | self.counts['G'].append(temp.count('G')) | ||
32 | self.counts['T'].append(temp.count('T')) | 34 | self.counts['T'].append(temp.count('T')) | ||
33 | def compute_consensus(self): | 35 | def compute_consensus(self): | ||
n | 34 | dna_str = str("") | n | 36 | dna_str = "" |
35 | for i in range(len(self.instances[0])): | 37 | for i in range(len(self.instances[0])): | ||
36 | a_count = 0 | 38 | a_count = 0 | ||
37 | c_count = 0 | 39 | c_count = 0 | ||
38 | g_count = 0 | 40 | g_count = 0 | ||
39 | t_count = 0 | 41 | t_count = 0 | ||
40 | for j in range(len(self.instances[0])): | 42 | for j in range(len(self.instances[0])): | ||
n | 41 | curr = str(self.instances[j]) | n | 43 | ocurrence = self.instances[j] |
42 | if curr[i].upper() == 'A': | 44 | if ocurrence[i].upper() == 'A': | ||
43 | a_count += 1 | 45 | a_count += 1 | ||
n | 44 | elif curr[i].upper() == 'C': | n | 46 | elif ocurrence[i].upper() == 'C': |
45 | c_count += 1 | 47 | c_count += 1 | ||
n | 46 | elif curr[i].upper() == 'G': | n | 48 | elif ocurrence[i].upper() == 'G': |
47 | g_count += 1 | 49 | g_count += 1 | ||
n | 48 | elif curr[i].upper() == 'T': | n | 50 | elif ocurrence[i].upper() == 'T': |
49 | t_count += 1 | 51 | t_count += 1 | ||
50 | if a_count > c_count and a_count > g_count and a_count > t_count | 52 | if a_count > c_count and a_count > g_count and a_count > t_count | ||
> | : | > | : | ||
51 | dna_str += "A" | 53 | dna_str += "A" | ||
52 | elif c_count > a_count and c_count > g_count and c_count > t_cou | 54 | elif c_count > a_count and c_count > g_count and c_count > t_cou | ||
> | nt: | > | nt: | ||
53 | dna_str += "C" | 55 | dna_str += "C" | ||
54 | elif g_count > a_count and g_count > t_count and g_count > c_cou | 56 | elif g_count > a_count and g_count > t_count and g_count > c_cou | ||
> | nt: | > | nt: | ||
55 | dna_str += "G" | 57 | dna_str += "G" | ||
56 | elif t_count > a_count and t_count > g_count and t_count > c_cou | 58 | elif t_count > a_count and t_count > g_count and t_count > c_cou | ||
> | nt: | > | nt: | ||
57 | dna_str += "T" | 59 | dna_str += "T" | ||
58 | return dna_str | 60 | return dna_str | ||
59 | def parse(self, filename): | 61 | def parse(self, filename): | ||
t | 60 | with open(filename,'r') as file: | t | 62 | with open(filename,'r') as f: |
61 | for i in file: | 63 | for i in f: | ||
62 | if '>' in i: | 64 | if '>' in i: | ||
63 | continue | 65 | continue | ||
64 | else: | 66 | else: | ||
65 | self.instances.append(i) | 67 | self.instances.append(i) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import matplotlib.pyplot as plt | n | ||
2 | from math import pi, sin, cos | ||||
3 | class PLANT: | 1 | class PLANT: | ||
4 | def __init__(self, init_state = "", generator = {}, iters = 0, delta_theta = | 2 | def __init__(self, init_state = "", generator = {}, iters = 0, delta_theta = | ||
> | 0): | > | 0): | ||
5 | self.__start = init_state | 3 | self.__start = init_state | ||
6 | self.__generator = generator | 4 | self.__generator = generator | ||
7 | self.__iters = iters | 5 | self.__iters = iters | ||
8 | self.__delta = delta_theta | 6 | self.__delta = delta_theta | ||
9 | self.str = self.generator() | 7 | self.str = self.generator() | ||
10 | def generator(self): | 8 | def generator(self): | ||
n | 11 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25)class DNAMOTIF: | n | 9 | passclass DNAMOTIF: |
12 | def file(self): | 10 | def file(self): | ||
n | 13 | file = open('lexA.fasta') | n | 11 | file = open('lexA.fasta', 'r') |
14 | for line in file: | 12 | for line in file: | ||
15 | if line[0] != '>': | 13 | if line[0] != '>': | ||
16 | self.instances.append(line) | 14 | self.instances.append(line) | ||
17 | def __init__(self): | 15 | def __init__(self): | ||
18 | self.instances=[] | 16 | self.instances=[] | ||
19 | self.consensus=[] | 17 | self.consensus=[] | ||
20 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 18 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
21 | def __str__(self): | 19 | def __str__(self): | ||
22 | dna_str = "" | 20 | dna_str = "" | ||
23 | for string in self.instances: | 21 | for string in self.instances: | ||
24 | dna_str += (string + "\n") | 22 | dna_str += (string + "\n") | ||
25 | return dna_str | 23 | return dna_str | ||
26 | def __len__(self): | 24 | def __len__(self): | ||
27 | return len(self.instances) - 4 | 25 | return len(self.instances) - 4 | ||
28 | def count(self): | 26 | def count(self): | ||
29 | for i in self.instances: | 27 | for i in self.instances: | ||
30 | temp = i.upper() | 28 | temp = i.upper() | ||
31 | self.counts['A'].append(temp.count('A')) | 29 | self.counts['A'].append(temp.count('A')) | ||
32 | self.counts['C'].append(temp.count('C')) | 30 | self.counts['C'].append(temp.count('C')) | ||
33 | self.counts['G'].append(temp.count('G')) | 31 | self.counts['G'].append(temp.count('G')) | ||
34 | self.counts['T'].append(temp.count('T')) | 32 | self.counts['T'].append(temp.count('T')) | ||
35 | def compute_consensus(self): | 33 | def compute_consensus(self): | ||
n | 36 | dna_str = "" | n | 34 | dna_str = str("") |
37 | for i in range(len(self.instances[0])): | 35 | for i in range(len(self.instances[0])): | ||
38 | a_count = 0 | 36 | a_count = 0 | ||
39 | c_count = 0 | 37 | c_count = 0 | ||
40 | g_count = 0 | 38 | g_count = 0 | ||
41 | t_count = 0 | 39 | t_count = 0 | ||
42 | for j in range(len(self.instances[0])): | 40 | for j in range(len(self.instances[0])): | ||
n | 43 | ocurrence = self.instances[j] | n | 41 | curr = str(self.instances[j]) |
44 | if ocurrence[i].upper() == 'A': | 42 | if curr[i].upper() == 'A': | ||
45 | a_count += 1 | 43 | a_count += 1 | ||
n | 46 | elif ocurrence[i].upper() == 'C': | n | 44 | elif curr[i].upper() == 'C': |
47 | c_count += 1 | 45 | c_count += 1 | ||
n | 48 | elif ocurrence[i].upper() == 'G': | n | 46 | elif curr[i].upper() == 'G': |
49 | g_count += 1 | 47 | g_count += 1 | ||
n | 50 | elif ocurrence[i].upper() == 'T': | n | 48 | elif curr[i].upper() == 'T': |
51 | t_count += 1 | 49 | t_count += 1 | ||
52 | if a_count > c_count and a_count > g_count and a_count > t_count | 50 | if a_count > c_count and a_count > g_count and a_count > t_count | ||
> | : | > | : | ||
53 | dna_str += "A" | 51 | dna_str += "A" | ||
54 | elif c_count > a_count and c_count > g_count and c_count > t_cou | 52 | elif c_count > a_count and c_count > g_count and c_count > t_cou | ||
> | nt: | > | nt: | ||
55 | dna_str += "C" | 53 | dna_str += "C" | ||
56 | elif g_count > a_count and g_count > t_count and g_count > c_cou | 54 | elif g_count > a_count and g_count > t_count and g_count > c_cou | ||
> | nt: | > | nt: | ||
57 | dna_str += "G" | 55 | dna_str += "G" | ||
58 | elif t_count > a_count and t_count > g_count and t_count > c_cou | 56 | elif t_count > a_count and t_count > g_count and t_count > c_cou | ||
> | nt: | > | nt: | ||
59 | dna_str += "T" | 57 | dna_str += "T" | ||
60 | return dna_str | 58 | return dna_str | ||
61 | def parse(self, filename): | 59 | def parse(self, filename): | ||
t | 62 | with open(filename,'r') as f: | t | 60 | with open(filename,'r') as file: |
63 | for i in f: | 61 | for i in file: | ||
64 | if '>' in i: | 62 | if '>' in i: | ||
65 | continue | 63 | continue | ||
66 | else: | 64 | else: | ||
67 | self.instances.append(i) | 65 | self.instances.append(i) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
n | 3 | class PLANT: | n | 3 | class PLANT: |
4 | def __init__(self, str, generator, iterations, delta): | 4 | def __init__(self, str, generator, iterations, gamma): | ||
5 | self.str = str | 5 | self.str = str | ||
6 | self.generator = generator | 6 | self.generator = generator | ||
7 | self.iterations = iterations | 7 | self.iterations = iterations | ||
8 | self.delta = delta | 8 | self.gamma = gamma | ||
9 | self.stack = [] | 9 | self.stack = [] | ||
10 | for l in range(self.iterations): | 10 | for l in range(self.iterations): | ||
11 | lst_str = list(self.str) | 11 | list_str = list(self.str) | ||
12 | for i, v in enumerate(lst_str): | 12 | for i, w in enumerate(list_str): | ||
13 | if v in self.generator: | 13 | if w in self.generator: | ||
14 | lst_str[i] = self.generator[v] | 14 | list_str[i] = self.generator[w] | ||
15 | self.str = ''.join(lst_str) | 15 | self.str = "".join(list_str) | ||
16 | def drawPlant(self): | 16 | def drawPlant(self): | ||
17 | currentPt = (200.0, 0.0) | 17 | currentPt = (200.0, 0.0) | ||
18 | tree_angle = 90 | 18 | treejawn = 90.0 | ||
19 | self.add([currentPt, tree_angle]) | 19 | self.add([currentPt, treejawn]) | ||
20 | list_str = list(self.str) | 20 | list_str = list(self.str) | ||
21 | for i, v in enumerate(list_str): | 21 | for i, w in enumerate(list_str): | ||
22 | if v == '[': | 22 | if w == '[': | ||
23 | self.add([currentPt, tree_angle]) | 23 | self.add([currentPt, treejawn]) | ||
24 | elif v == ']': | 24 | elif w == ']': | ||
25 | [currentPt, tree_angle] = self.peek() | 25 | [currentPt, treejawn] = self.peek() | ||
26 | elif v == '+': | 26 | elif w == '+': | ||
27 | tree_angle += self.delta | 27 | treejawn += self.gamma | ||
28 | elif v == '-': | 28 | elif w == '-': | ||
29 | tree_angle -= self.delta | 29 | treejawn -= self.gamma | ||
30 | elif v.isalpha(): | 30 | elif w.isalpha(): | ||
31 | nextPt = (currentPt[0] + math.cos(math.radians(tree_angle)), curr | 31 | nextPt = (currentPt[0] + math.cos(math.radians(treejawn)), curren | ||
> | entPt[1] + math.sin(math.radians(tree_angle))) | > | tPt[1] + math.sin(math.radians(treejawn))) | ||
32 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], co | 32 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], co | ||
> | lor='green') | > | lor='green') | ||
33 | currentPt = nextPt | 33 | currentPt = nextPt | ||
34 | def add(self, data): | 34 | def add(self, data): | ||
35 | self.stack.append(data) | 35 | self.stack.append(data) | ||
36 | def peek(self): | 36 | def peek(self): | ||
37 | return self.stack[-1] | 37 | return self.stack[-1] | ||
38 | if __name__ == '__main__': | 38 | if __name__ == '__main__': | ||
39 | np = PLANT('X', {'X': 'F[+X]F[-X]+X', 'F': 'FF'}, 2, 20) | 39 | np = PLANT('X', {'X': 'F[+X]F[-X]+X', 'F': 'FF'}, 2, 20) | ||
40 | np.drawPlant() | 40 | np.drawPlant()class DNAMOTIF: | ||
41 | class DNAMOTIF: | ||||
42 | def __init__(self): | 41 | def __init__(self): | ||
43 | self.instances=[] | 42 | self.instances=[] | ||
44 | self.consensus=[] | 43 | self.consensus=[] | ||
45 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 44 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
46 | def __str__(self): | 45 | def __str__(self): | ||
47 | str = '' | 46 | str = '' | ||
48 | for i in self.instances: | 47 | for i in self.instances: | ||
49 | str += i + '\n' | 48 | str += i + '\n' | ||
50 | return str[:-2] | 49 | return str[:-2] | ||
51 | def __len__(self): | 50 | def __len__(self): | ||
52 | return (len(self.instances[0])-1) | 51 | return (len(self.instances[0])-1) | ||
53 | def count(self): | 52 | def count(self): | ||
54 | for i in self.instances: | 53 | for i in self.instances: | ||
n | 55 | t = i.upper() | n | 54 | jawn = i.upper() |
56 | self.counts['A'].append(t.count('A')) | 55 | self.counts['A'].__add__(jawn.count('A')) | ||
57 | self.counts['C'].append(t.count('C')) | 56 | self.counts['C'].__add__(jawn.count('C')) | ||
58 | self.counts['G'].append(t.count('G')) | 57 | self.counts['G'].__add__(jawn.count('G')) | ||
59 | self.counts['T'].append(t.count('T')) | 58 | self.counts['T'].__add__(jawn.count('T')) | ||
60 | def compute_consensus(self): | 59 | def compute_consensus(self): | ||
61 | A = self.counts['A'] | 60 | A = self.counts['A'] | ||
62 | C = self.counts['C'] | 61 | C = self.counts['C'] | ||
63 | G = self.counts['G'] | 62 | G = self.counts['G'] | ||
64 | T = self.counts['T'] | 63 | T = self.counts['T'] | ||
65 | for i in range(list(A)): | 64 | for i in range(list(A)): | ||
66 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 65 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
67 | self.consensus.append(A) | 66 | self.consensus.append(A) | ||
68 | elif (C[i] >= G[i] and C[i] >= T[i]): | 67 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
69 | self.consensus.append(C) | 68 | self.consensus.append(C) | ||
70 | elif (G[i] >= T[i]): | 69 | elif (G[i] >= T[i]): | ||
71 | self.consensus.append(G) | 70 | self.consensus.append(G) | ||
72 | else: | 71 | else: | ||
73 | self.consensus.append(T) | 72 | self.consensus.append(T) | ||
74 | def parse(self, filename): | 73 | def parse(self, filename): | ||
75 | with open(filename,'r') as f: | 74 | with open(filename,'r') as f: | ||
76 | for i in f: | 75 | for i in f: | ||
77 | if '>' in i: | 76 | if '>' in i: | ||
78 | continue | 77 | continue | ||
79 | else: | 78 | else: | ||
80 | self.instances.append(i) | 79 | self.instances.append(i) | ||
81 | if __name__=='__main__': | 80 | if __name__=='__main__': | ||
82 | lex=DNAMOTIF() | 81 | lex=DNAMOTIF() | ||
83 | lex.parse('lexA.fasta') | 82 | lex.parse('lexA.fasta') | ||
84 | print('length of each sequence:',len(lex)) | 83 | print('length of each sequence:',len(lex)) | ||
85 | print(lex) | 84 | print(lex) | ||
86 | print('\nconsensus:',lex.consensus, '\n') | 85 | print('\nconsensus:',lex.consensus, '\n') | ||
t | 87 | print('sequence counts:',lex.counts) | t | 86 | print('sequence counts:',lex.counts) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | import matplotlib.pyplot as plt | ||
1 | import numpy as np | 2 | import numpy as np | ||
n | 2 | import matplotlib.pyplot as plt | n | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, string, dictionary, n, delta_Theta): | n | 4 | def __init__(self, string, dictionary, n, deltaTheta): |
5 | self.string = string | 5 | self.string = string | ||
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
7 | self.n = n | 7 | self.n = n | ||
n | 8 | self.delta_Theta = delta_Theta | n | 8 | self.deltaTheta = deltaTheta |
9 | self.str = PLANT.generator(string,dictionary, n) | 9 | self.str = PLANT.generator(string,dictionary, n) | ||
10 | def generator(string, dictionary, n): | 10 | def generator(string, dictionary, n): | ||
n | 11 | def variable(string): | n | 11 | def production_rule(string): |
12 | if string in dictionary: | 12 | if string in dictionary: | ||
13 | return dictionary.get(string) | 13 | return dictionary.get(string) | ||
14 | return string | 14 | return string | ||
n | 15 | listsubstrings = [string] | n | 15 | list_of_substrings = [string] |
16 | for i in range(n): | 16 | for i in range(n): | ||
n | 17 | current_substring = listsubstrings[-1] | n | 17 | current_substring = list_of_substrings[-1] |
18 | new_axiom = [variable(char) for char in current_substring] | 18 | new_axiom = [production_rule(char) for char in current_substring] | ||
19 | listsubstrings.append(''.join(new_axiom)) | 19 | list_of_substrings.append(''.join(new_axiom)) | ||
20 | new_string = listsubstrings[n] | 20 | newstring = list_of_substrings[n] | ||
21 | return new_string | 21 | return newstring | ||
22 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | 23 | upper_alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L | n | 23 | uppera = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M |
> | ', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | > | ', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||
24 | current_Plant=(200,0) | 24 | currentPt=(200,0) | ||
25 | theta = np.radians(90) | 25 | theta = np.radians(90) | ||
n | 26 | delta_Theta = np.radians(self.delta_Theta) | n | 26 | deltaTheta = np.radians(self.deltaTheta) |
27 | stack = [current_Plant, theta] | 27 | stack = [currentPt, theta] | ||
28 | for command in self.str: | 28 | for command in self.str: | ||
n | 29 | if command.upper() in upper_alpha: | n | 29 | if command.upper() in uppera: |
30 | next_Plant = (current_Plant[0]+(np.cos(theta)), current_Plant[1] | 30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | ||
> | +(np.sin(theta))) | > | ta))) | ||
31 | plt.plot([current_Plant[0],next_Plant[0]],[current_Plant[1],next | 31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | _Plant[1]],color='black') | > | ='black') | ||
32 | current_Plant = next_Plant | 32 | currentPt = nextPt | ||
33 | else: | 33 | else: | ||
34 | if command == '[': | 34 | if command == '[': | ||
n | 35 | stack.append(current_Plant) | n | 35 | stack.append(currentPt) |
36 | stack.append(theta) | 36 | stack.append(theta) | ||
37 | elif command == ']': | 37 | elif command == ']': | ||
38 | theta = stack.pop() | 38 | theta = stack.pop() | ||
n | 39 | current_Plant = stack.pop() | n | 39 | currentPt = stack.pop() |
40 | elif command == '+': | 40 | elif command == '+': | ||
n | 41 | theta += delta_Theta | n | 41 | theta += deltaTheta |
42 | elif command == '-': | 42 | elif command == '-': | ||
n | 43 | theta -= delta_Theta | n | 43 | theta -= deltaTheta |
44 | return plt.plot | 44 | return plt.plot | ||
45 | if __name__ == "__main__": | 45 | if __name__ == "__main__": | ||
46 | string = input() | 46 | string = input() | ||
47 | dictionary = input() | 47 | dictionary = input() | ||
48 | n = int(input()) | 48 | n = int(input()) | ||
n | 49 | delta_Theta = float(input()) | n | 49 | deltaTheta = float(input()) |
50 | p = PLANT(string, dictionary, n, deltaTheta) | 50 | p = PLANT(string, dictionary, n, deltaTheta) | ||
51 | p.drawPlant() | 51 | p.drawPlant() | ||
n | 52 | plt.show()class DNAMOTIF: | n | 52 | plt.show()class DNAMOTIF: |
53 | def __init__(self): | 53 | def __init__(self): | ||
54 | self.instances=[] | 54 | self.instances=[] | ||
55 | self.consensus=[] | 55 | self.consensus=[] | ||
56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
57 | def __str__(self): | 57 | def __str__(self): | ||
58 | output = "" | 58 | output = "" | ||
59 | for instance in self.instances: | 59 | for instance in self.instances: | ||
60 | output += instance | 60 | output += instance | ||
61 | return output | 61 | return output | ||
62 | def __len__(self): | 62 | def __len__(self): | ||
63 | return len(self.instances[0].rstrip()) | 63 | return len(self.instances[0].rstrip()) | ||
64 | def count(self): | 64 | def count(self): | ||
65 | for position in range(len(self)): | 65 | for position in range(len(self)): | ||
n | 66 | seq_T = 0 | n | ||
67 | seq_A = 0 | 66 | sequenceA = 0 | ||
68 | seq_G = 0 | 67 | sequenceT = 0 | ||
69 | seq_C = 0 | 68 | sequenceC = 0 | ||
69 | sequenceG = 0 | ||||
70 | for instance in self.instances: | 70 | for instance in self.instances: | ||
71 | sequence = instance.rstrip() | 71 | sequence = instance.rstrip() | ||
n | 72 | if (sequence[position]).upper() == "G": | n | 72 | if (sequence[position]).upper() == "A": |
73 | seq_G = seq_G + 1 | 73 | sequenceA += 1 | ||
74 | elif (sequence[position]).upper() == "T": | ||||
75 | sequenceT += 1 | ||||
74 | elif (sequence[position]).upper() == "C": | 76 | elif (sequence[position]).upper() == "C": | ||
n | 75 | seq_C = seq_C + 1 | n | 77 | sequenceC += 1 |
76 | elif (sequence[position]).upper() == "T": | 78 | elif (sequence[position]).upper() == "G": | ||
77 | seq_T = seq_T + 1 | 79 | sequenceG += 1 | ||
78 | elif (sequence[position]).upper() == "A": | ||||
79 | seq_A = seq_A + 1 | ||||
80 | self.counts.get("A").append(seq_A) | 80 | self.counts.get("A").append(sequenceA) | ||
81 | self.counts.get("T").append(seq_T) | 81 | self.counts.get("T").append(sequenceT) | ||
82 | self.counts.get("C").append(seq_C) | 82 | self.counts.get("C").append(sequenceC) | ||
83 | self.counts.get("G").append(seq_G) | 83 | self.counts.get("G").append(sequenceG) | ||
84 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
85 | DNAMOTIF.count(self) | 85 | DNAMOTIF.count(self) | ||
86 | A = self.counts.get("A") | 86 | A = self.counts.get("A") | ||
n | n | 87 | T = self.counts.get("T") | ||
88 | C = self.counts.get("C") | ||||
87 | G = self.counts.get("G") | 89 | G = self.counts.get("G") | ||
n | 88 | C = self.counts.get("C") | n | ||
89 | T = self.counts.get("T") | ||||
90 | output = "" | 90 | output = "" | ||
91 | for row in range(len(A)): | 91 | for row in range(len(A)): | ||
92 | maximum = max(A[row],T[row],C[row],G[row]) | 92 | maximum = max(A[row],T[row],C[row],G[row]) | ||
93 | if maximum == A[row]: | 93 | if maximum == A[row]: | ||
n | 94 | output = output + "A" | n | 94 | output += "A" |
95 | elif maximum == T[row]: | ||||
96 | output += "T" | ||||
97 | elif maximum == C[row]: | ||||
98 | output += "C" | ||||
95 | elif maximum == G[row]: | 99 | elif maximum == G[row]: | ||
n | 96 | output = output +"G" | n | 100 | output += "G" |
97 | elif maximum == C[row]: | ||||
98 | output = output + "C" | ||||
99 | elif maximum == T[row]: | ||||
100 | output = output + "T" | ||||
101 | self.consensus = output | 101 | self.consensus = output | ||
102 | def parse(self, filename): | 102 | def parse(self, filename): | ||
n | 103 | my_File = open(filename, 'r') | n | 103 | myFile = open(filename, 'r') |
104 | contents = [line for line in my_File.readlines()] | 104 | contents = [line for line in myFile.readlines()] | ||
105 | my_File.close() | 105 | myFile.close() | ||
106 | for index, line in enumerate(contents): | 106 | for index, line in enumerate(contents): | ||
107 | if index % 2 != 0: | 107 | if index % 2 != 0: | ||
108 | self.instances.append(line) | 108 | self.instances.append(line) | ||
109 | if __name__ == '__main__': | 109 | if __name__ == '__main__': | ||
n | 110 | lexA=DNAMOTIF() | n | 110 | lexA=DNAMOTIF() |
111 | filename = r'FinalProject\lexA.fasta' | 111 | filename = r'FinalProject\lexA.fasta' | ||
t | 112 | lexA.parse(filename) | t | 112 | lexA.parse(filename) |
113 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
114 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
n | 3 | class PLANT: | n | 3 | class PLANT: |
4 | def __init__(self, str, generator, iterations, gamma): | 4 | def __init__(self, str, generator, iterations, delta): | ||
5 | self.str = str | 5 | self.str = str | ||
6 | self.generator = generator | 6 | self.generator = generator | ||
7 | self.iterations = iterations | 7 | self.iterations = iterations | ||
8 | self.gamma = gamma | 8 | self.delta = delta | ||
9 | self.stack = [] | 9 | self.stack = [] | ||
10 | for l in range(self.iterations): | 10 | for l in range(self.iterations): | ||
11 | list_str = list(self.str) | 11 | lst_str = list(self.str) | ||
12 | for i, w in enumerate(list_str): | 12 | for i, v in enumerate(lst_str): | ||
13 | if w in self.generator: | 13 | if v in self.generator: | ||
14 | list_str[i] = self.generator[w] | 14 | lst_str[i] = self.generator[v] | ||
15 | self.str = "".join(list_str) | 15 | self.str = ''.join(lst_str) | ||
16 | def drawPlant(self): | 16 | def drawPlant(self): | ||
17 | currentPt = (200.0, 0.0) | 17 | currentPt = (200.0, 0.0) | ||
18 | treejawn = 90.0 | 18 | tree_angle = 90 | ||
19 | self.add([currentPt, treejawn]) | 19 | self.add([currentPt, tree_angle]) | ||
20 | list_str = list(self.str) | 20 | list_str = list(self.str) | ||
21 | for i, w in enumerate(list_str): | 21 | for i, v in enumerate(list_str): | ||
22 | if w == '[': | 22 | if v == '[': | ||
23 | self.add([currentPt, treejawn]) | 23 | self.add([currentPt, tree_angle]) | ||
24 | elif w == ']': | 24 | elif v == ']': | ||
25 | [currentPt, treejawn] = self.peek() | 25 | [currentPt, tree_angle] = self.peek() | ||
26 | elif w == '+': | 26 | elif v == '+': | ||
27 | treejawn += self.gamma | 27 | tree_angle += self.delta | ||
28 | elif w == '-': | 28 | elif v == '-': | ||
29 | treejawn -= self.gamma | 29 | tree_angle -= self.delta | ||
30 | elif w.isalpha(): | 30 | elif v.isalpha(): | ||
31 | nextPt = (currentPt[0] + math.cos(math.radians(treejawn)), curren | 31 | nextPt = (currentPt[0] + math.cos(math.radians(tree_angle)), curr | ||
> | tPt[1] + math.sin(math.radians(treejawn))) | > | entPt[1] + math.sin(math.radians(tree_angle))) | ||
32 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], co | 32 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], co | ||
> | lor='green') | > | lor='green') | ||
33 | currentPt = nextPt | 33 | currentPt = nextPt | ||
34 | def add(self, data): | 34 | def add(self, data): | ||
35 | self.stack.append(data) | 35 | self.stack.append(data) | ||
36 | def peek(self): | 36 | def peek(self): | ||
37 | return self.stack[-1] | 37 | return self.stack[-1] | ||
38 | if __name__ == '__main__': | 38 | if __name__ == '__main__': | ||
39 | np = PLANT('X', {'X': 'F[+X]F[-X]+X', 'F': 'FF'}, 2, 20) | 39 | np = PLANT('X', {'X': 'F[+X]F[-X]+X', 'F': 'FF'}, 2, 20) | ||
40 | np.drawPlant()class DNAMOTIF: | 40 | np.drawPlant() | ||
41 | class DNAMOTIF: | ||||
41 | def __init__(self): | 42 | def __init__(self): | ||
42 | self.instances=[] | 43 | self.instances=[] | ||
43 | self.consensus=[] | 44 | self.consensus=[] | ||
44 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 45 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
45 | def __str__(self): | 46 | def __str__(self): | ||
46 | str = '' | 47 | str = '' | ||
47 | for i in self.instances: | 48 | for i in self.instances: | ||
48 | str += i + '\n' | 49 | str += i + '\n' | ||
49 | return str[:-2] | 50 | return str[:-2] | ||
50 | def __len__(self): | 51 | def __len__(self): | ||
51 | return (len(self.instances[0])-1) | 52 | return (len(self.instances[0])-1) | ||
52 | def count(self): | 53 | def count(self): | ||
53 | for i in self.instances: | 54 | for i in self.instances: | ||
n | 54 | jawn = i.upper() | n | 55 | t = i.upper() |
55 | self.counts['A'].__add__(jawn.count('A')) | 56 | self.counts['A'].append(t.count('A')) | ||
56 | self.counts['C'].__add__(jawn.count('C')) | 57 | self.counts['C'].append(t.count('C')) | ||
57 | self.counts['G'].__add__(jawn.count('G')) | 58 | self.counts['G'].append(t.count('G')) | ||
58 | self.counts['T'].__add__(jawn.count('T')) | 59 | self.counts['T'].append(t.count('T')) | ||
59 | def compute_consensus(self): | 60 | def compute_consensus(self): | ||
60 | A = self.counts['A'] | 61 | A = self.counts['A'] | ||
61 | C = self.counts['C'] | 62 | C = self.counts['C'] | ||
62 | G = self.counts['G'] | 63 | G = self.counts['G'] | ||
63 | T = self.counts['T'] | 64 | T = self.counts['T'] | ||
64 | for i in range(list(A)): | 65 | for i in range(list(A)): | ||
65 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 66 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
66 | self.consensus.append(A) | 67 | self.consensus.append(A) | ||
67 | elif (C[i] >= G[i] and C[i] >= T[i]): | 68 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
68 | self.consensus.append(C) | 69 | self.consensus.append(C) | ||
69 | elif (G[i] >= T[i]): | 70 | elif (G[i] >= T[i]): | ||
70 | self.consensus.append(G) | 71 | self.consensus.append(G) | ||
71 | else: | 72 | else: | ||
72 | self.consensus.append(T) | 73 | self.consensus.append(T) | ||
73 | def parse(self, filename): | 74 | def parse(self, filename): | ||
74 | with open(filename,'r') as f: | 75 | with open(filename,'r') as f: | ||
75 | for i in f: | 76 | for i in f: | ||
76 | if '>' in i: | 77 | if '>' in i: | ||
77 | continue | 78 | continue | ||
78 | else: | 79 | else: | ||
79 | self.instances.append(i) | 80 | self.instances.append(i) | ||
80 | if __name__=='__main__': | 81 | if __name__=='__main__': | ||
81 | lex=DNAMOTIF() | 82 | lex=DNAMOTIF() | ||
82 | lex.parse('lexA.fasta') | 83 | lex.parse('lexA.fasta') | ||
83 | print('length of each sequence:',len(lex)) | 84 | print('length of each sequence:',len(lex)) | ||
84 | print(lex) | 85 | print(lex) | ||
85 | print('\nconsensus:',lex.consensus, '\n') | 86 | print('\nconsensus:',lex.consensus, '\n') | ||
t | 86 | print('sequence counts:',lex.counts) | t | 87 | print('sequence counts:',lex.counts) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import math | 2 | import math | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self,initialStr,generator,numIters,deltaTheta): | n | 4 | def __init__(self,initialStr,randomizer,numInts,deltaTheta): |
5 | self.str = initialStr | 5 | self.str=initialStr | ||
6 | self.generator= generator | 6 | self.randomizer= randomizer | ||
7 | self.numIters = numIters | 7 | self.numInts = numInts | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
n | 9 | for i in range(numIters): | n | 9 | for i in range(numInts): |
10 | new_string = "" | 10 | string_1 = "" | ||
11 | for letter in self.str: | 11 | for letter in self.str: | ||
n | 12 | if letter in self.generator: | n | 12 | if letter in self.randomizer: |
13 | new_string =new_string+self.generator[letter] | 13 | string_1 = string_1+self.randomizer[letter] | ||
14 | else: | 14 | else: | ||
n | 15 | new_string =new_string+letter | n | 15 | string_1 = string_1 + letter |
16 | self.str = new_string | 16 | self.str = string_1 | ||
17 | def drawPlant(self): | 17 | def drawPlant(self): | ||
n | 18 | currentPt =(200,0) | n | 18 | currentPt = (200,0) |
19 | stack = [] | 19 | stack = [] | ||
n | 20 | theta =90 | n | 20 | theta = 90 |
21 | for letter in self.str: | 21 | for letter in self.str: | ||
n | n | 22 | if letter == "]": | ||
23 | recent_save = stack.pop() | ||||
24 | theta = recent_save[1] | ||||
25 | currentPt = recent_save[0] | ||||
22 | if letter == "[": | 26 | elif letter == "[": | ||
23 | stack.append([currentPt,theta]) | 27 | stack.append([currentPt,theta]) | ||
n | 24 | elif letter == "]": | n | ||
25 | current_save = stack.pop() | ||||
26 | theta = current_save[1] | ||||
27 | currentPt = current_save[0] | ||||
28 | elif letter =="+": | ||||
29 | theta = theta+self.deltaTheta | ||||
30 | elif letter =="-": | 28 | elif letter == "-": | ||
31 | theta -= self.deltaTheta | 29 | theta -= self.deltaTheta | ||
n | n | 30 | elif letter == "+": | ||
31 | theta = theta + self.deltaTheta | ||||
32 | else: | 32 | else: | ||
33 | nextPt = (math.cos(math.radians(theta))+currentPt[0],currentPt[1 | 33 | nextPt = (math.cos(math.radians(theta))+currentPt[0],currentPt[1 | ||
> | ] + math.sin(math.radians(theta))) | > | ] + math.sin(math.radians(theta))) | ||
n | 34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | n | 34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c |
> | olor="black") | > | olor = "black") | ||
35 | currentPt = nextPt | 35 | currentPt = nextPt | ||
36 | class DNAMOTIF: | 36 | class DNAMOTIF: | ||
37 | def __init__(self): | 37 | def __init__(self): | ||
38 | self.instances=[] | 38 | self.instances=[] | ||
39 | self.consensus=[] | 39 | self.consensus=[] | ||
40 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 40 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
41 | def __str__(self): | 41 | def __str__(self): | ||
42 | file = '' | 42 | file = '' | ||
n | 43 | for line in self.instances: | n | 43 | for row in self.instances: |
44 | file += line | 44 | file += row | ||
45 | return file | 45 | return file | ||
46 | def __len__(self): | 46 | def __len__(self): | ||
47 | return len(self.instances[0])-1 | 47 | return len(self.instances[0])-1 | ||
48 | def count(self): | 48 | def count(self): | ||
n | 49 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | n | 49 | self.counts= {'A':[], 'C':[], 'G':[], 'T':[]} |
50 | for i in range(len(self.instances[0])-1): | 50 | for n in range(len(self.instances[0])-1): | ||
51 | for key in self.counts.keys(): | 51 | for key in self.counts.keys(): | ||
52 | self.counts[key].append(0) | 52 | self.counts[key].append(0) | ||
n | 53 | for line in self.instances: | n | 53 | for row in self.instances: |
54 | if line[i].upper()==key: | 54 | if row[n].upper()==key: | ||
55 | self.counts[key][i]+=1 | 55 | self.counts[key][n]+=1 | ||
56 | return self.counts | 56 | return self.counts | ||
57 | def compute_consensus(self): | 57 | def compute_consensus(self): | ||
n | 58 | self.consensus = [] | n | 58 | self.consunsus = [] |
59 | self.counts = self.count() | 59 | self.counts = self.count() | ||
n | 60 | for charPos in range(len(self.instances[0])-1): | n | 60 | for charP in range (len(self.instances[0])-1): |
61 | self.consensus.append(max(self.counts, key=lambda x: self.counts[x][ | 61 | self.consensus.append(max(self.counts, key=lambda y: self.counts[y][ | ||
> | charPos]).upper()) | > | charP]).upper()) | ||
62 | self.consensus = str(''.join(self.consensus)) | 62 | self.consensus = str(''.join(self.consensus)) | ||
63 | def parse(self, filename): | 63 | def parse(self, filename): | ||
t | 64 | file1 = open(filename, 'r') | t | 64 | fileA = open(filename, 'r') |
65 | Lines = file1.readlines() | 65 | Rows = fileA.readlines() | ||
66 | for line in Lines: | 66 | for row in Rows: | ||
67 | if not line.startswith('>'): | 67 | if not row.startswith('>'): | ||
68 | self.instances.append(line) | 68 | self.instances.append(row) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import math | 2 | import math | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self,initialStr,randomizer,numInts,deltaTheta): | n | 4 | def __init__(self,initialStr,generator,numIters,deltaTheta): |
5 | self.str=initialStr | 5 | self.str = initialStr | ||
6 | self.randomizer= randomizer | 6 | self.generator= generator | ||
7 | self.numInts = numInts | 7 | self.numIters = numIters | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
n | 9 | for i in range(numInts): | n | 9 | for i in range(numIters): |
10 | string_1 = "" | 10 | new_string = "" | ||
11 | for letter in self.str: | 11 | for letter in self.str: | ||
n | 12 | if letter in self.randomizer: | n | 12 | if letter in self.generator: |
13 | string_1 = string_1+self.randomizer[letter] | 13 | new_string =new_string+self.generator[letter] | ||
14 | else: | 14 | else: | ||
n | 15 | string_1 = string_1 + letter | n | 15 | new_string =new_string+letter |
16 | self.str = string_1 | 16 | self.str = new_string | ||
17 | def drawPlant(self): | 17 | def drawPlant(self): | ||
n | 18 | currentPt = (200,0) | n | 18 | currentPt =(200,0) |
19 | stack = [] | 19 | stack = [] | ||
n | 20 | theta = 90 | n | 20 | theta =90 |
21 | for letter in self.str: | 21 | for letter in self.str: | ||
n | 22 | if letter == "]": | n | ||
23 | recent_save = stack.pop() | ||||
24 | theta = recent_save[1] | ||||
25 | currentPt = recent_save[0] | ||||
26 | elif letter == "[": | 22 | if letter == "[": | ||
27 | stack.append([currentPt,theta]) | 23 | stack.append([currentPt,theta]) | ||
n | n | 24 | elif letter == "]": | ||
25 | current_save = stack.pop() | ||||
26 | theta = current_save[1] | ||||
27 | currentPt = current_save[0] | ||||
28 | elif letter =="+": | ||||
29 | theta = theta+self.deltaTheta | ||||
28 | elif letter == "-": | 30 | elif letter =="-": | ||
29 | theta -= self.deltaTheta | 31 | theta -= self.deltaTheta | ||
n | 30 | elif letter == "+": | n | ||
31 | theta = theta + self.deltaTheta | ||||
32 | else: | 32 | else: | ||
33 | nextPt = (math.cos(math.radians(theta))+currentPt[0],currentPt[1 | 33 | nextPt = (math.cos(math.radians(theta))+currentPt[0],currentPt[1 | ||
> | ] + math.sin(math.radians(theta))) | > | ] + math.sin(math.radians(theta))) | ||
n | 34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | n | 34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c |
> | olor = "black") | > | olor="black") | ||
35 | currentPt = nextPt | 35 | currentPt = nextPt | ||
36 | class DNAMOTIF: | 36 | class DNAMOTIF: | ||
37 | def __init__(self): | 37 | def __init__(self): | ||
38 | self.instances=[] | 38 | self.instances=[] | ||
39 | self.consensus=[] | 39 | self.consensus=[] | ||
40 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 40 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
41 | def __str__(self): | 41 | def __str__(self): | ||
42 | file = '' | 42 | file = '' | ||
n | 43 | for row in self.instances: | n | 43 | for line in self.instances: |
44 | file += row | 44 | file += line | ||
45 | return file | 45 | return file | ||
46 | def __len__(self): | 46 | def __len__(self): | ||
47 | return len(self.instances[0])-1 | 47 | return len(self.instances[0])-1 | ||
48 | def count(self): | 48 | def count(self): | ||
n | 49 | self.counts= {'A':[], 'C':[], 'G':[], 'T':[]} | n | 49 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} |
50 | for n in range(len(self.instances[0])-1): | 50 | for i in range(len(self.instances[0])-1): | ||
51 | for key in self.counts.keys(): | 51 | for key in self.counts.keys(): | ||
52 | self.counts[key].append(0) | 52 | self.counts[key].append(0) | ||
n | 53 | for row in self.instances: | n | 53 | for line in self.instances: |
54 | if row[n].upper()==key: | 54 | if line[i].upper()==key: | ||
55 | self.counts[key][n]+=1 | 55 | self.counts[key][i]+=1 | ||
56 | return self.counts | 56 | return self.counts | ||
57 | def compute_consensus(self): | 57 | def compute_consensus(self): | ||
n | 58 | self.consunsus = [] | n | 58 | self.consensus = [] |
59 | self.counts = self.count() | 59 | self.counts = self.count() | ||
n | 60 | for charP in range (len(self.instances[0])-1): | n | 60 | for charPos in range(len(self.instances[0])-1): |
61 | self.consensus.append(max(self.counts, key=lambda y: self.counts[y][ | 61 | self.consensus.append(max(self.counts, key=lambda x: self.counts[x][ | ||
> | charP]).upper()) | > | charPos]).upper()) | ||
62 | self.consensus = str(''.join(self.consensus)) | 62 | self.consensus = str(''.join(self.consensus)) | ||
63 | def parse(self, filename): | 63 | def parse(self, filename): | ||
t | 64 | fileA = open(filename, 'r') | t | 64 | file1 = open(filename, 'r') |
65 | Rows = fileA.readlines() | 65 | Lines = file1.readlines() | ||
66 | for row in Rows: | 66 | for line in Lines: | ||
67 | if not row.startswith('>'): | 67 | if not line.startswith('>'): | ||
68 | self.instances.append(row) | 68 | self.instances.append(line) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | from numpy import cos, sin, pi | 2 | from numpy import cos, sin, pi | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__ (self, initState, generator, n, delta_theta): | n | 4 | def __init__ (self, initResult, generator, n, delta_Theta): |
5 | self.init_state = initState | 5 | self.init_result = initResult | ||
6 | self.gen = generator | 6 | self.gen = generator | ||
7 | self.num = n | 7 | self.num = n | ||
n | 8 | self.deltatheta = delta_theta | n | 8 | self.deltaTheta = delta_Theta |
9 | self.str = self.init_state | 9 | self.str = self.init_result | ||
10 | self.str2 = '' | 10 | self.str2 = "" | ||
11 | for i in range(self.num): | 11 | for i in range(self.num): | ||
n | 12 | self.str2 = '' | n | 12 | self.str2 = "" |
13 | for char in self.str: | 13 | for char in self.str: | ||
14 | if char in self.gen: | 14 | if char in self.gen: | ||
15 | self.str2 += self.gen[char] | 15 | self.str2 += self.gen[char] | ||
16 | else: | 16 | else: | ||
17 | self.str2 += char | 17 | self.str2 += char | ||
n | 18 | self.str = self.str2 | n | 18 | self.str = self.str2 |
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
20 | currentpt = (200,0) | 20 | currentPt = (200,0) | ||
21 | theta = pi/2 | 21 | theta = pi/2 | ||
n | 22 | nextpt = () | n | 22 | nextPt = () |
23 | stack = [] | 23 | stack = [] | ||
24 | for CHR in self.str: | 24 | for character in self.str: | ||
25 | if CHR.isalpha(): | 25 | if character.isalpha(): | ||
26 | nextpt = (currentpt[0]+cos(theta), currentpt[1]+sin(theta)) | 26 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) | ||
27 | plt.plot([currentpt[0],nextpt[0]],[currentpt[1],nextpt[1]],color | 27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
28 | currentpt = nextpt | 28 | currentPt = nextPt | ||
29 | elif CHR == '[': | 29 | elif character == "[": | ||
30 | stack.append([currentpt, theta]) | 30 | stack.append([currentPt, theta]) | ||
31 | elif CHR == ']': | 31 | elif character == "]": | ||
32 | currentpt, theta = stack.pop() | 32 | currentPt, theta = stack.pop() | ||
33 | elif CHR == '+': | 33 | elif character == "+": | ||
34 | theta += (self.deltatheta * (pi/180)) | 34 | theta += (self.deltaTheta * (pi/180)) | ||
35 | elif CHR == '-': | 35 | elif character == "-": | ||
36 | theta -= (self.deltatheta * (pi/180)) | 36 | theta -= (self.deltaTheta * (pi/180)) | ||
37 | plt.show() | ||||
37 | class DNAMOTIF: | 38 | class DNAMOTIF: | ||
38 | def __init__(self): | 39 | def __init__(self): | ||
39 | self.instances=[] | 40 | self.instances=[] | ||
40 | self.consensus=[] | 41 | self.consensus=[] | ||
41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 42 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
42 | def __str__(self): | 43 | def __str__(self): | ||
n | 43 | mystring = '' | n | 44 | pystr = '' |
44 | for item in self.instances: | 45 | for item in self.instances: | ||
n | 45 | mystring += item | n | 46 | pystr += item |
46 | return mystring | 47 | return pystr | ||
47 | def __len__(self): | 48 | def __len__(self): | ||
48 | return len(self.instances[0].rstrip('\n')) | 49 | return len(self.instances[0].rstrip('\n')) | ||
49 | def count(self): | 50 | def count(self): | ||
50 | for index in range(self.__len__()): | 51 | for index in range(self.__len__()): | ||
51 | countA = 0 | 52 | countA = 0 | ||
52 | countC = 0 | 53 | countC = 0 | ||
53 | countG = 0 | 54 | countG = 0 | ||
54 | countT = 0 | 55 | countT = 0 | ||
55 | for item in self.instances: | 56 | for item in self.instances: | ||
56 | if item[index].upper() == 'A': | 57 | if item[index].upper() == 'A': | ||
57 | countA += 1 | 58 | countA += 1 | ||
58 | elif item[index].upper() == 'C': | 59 | elif item[index].upper() == 'C': | ||
59 | countC += 1 | 60 | countC += 1 | ||
60 | elif item[index].upper() == 'G': | 61 | elif item[index].upper() == 'G': | ||
61 | countG += 1 | 62 | countG += 1 | ||
62 | elif item[index].upper() == 'T': | 63 | elif item[index].upper() == 'T': | ||
63 | countT += 1 | 64 | countT += 1 | ||
64 | self.counts['A'].append(countA) | 65 | self.counts['A'].append(countA) | ||
65 | self.counts['C'].append(countC) | 66 | self.counts['C'].append(countC) | ||
66 | self.counts['G'].append(countG) | 67 | self.counts['G'].append(countG) | ||
67 | self.counts['T'].append(countT) | 68 | self.counts['T'].append(countT) | ||
68 | def compute_consensus(self): | 69 | def compute_consensus(self): | ||
69 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 70 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
70 | self.count() | 71 | self.count() | ||
71 | self.consensus = '' | 72 | self.consensus = '' | ||
72 | for i in range(self.__len__()): | 73 | for i in range(self.__len__()): | ||
n | 73 | cap = max([self.counts['A'][i], self.counts['C'][i], self.counts['G' | n | 74 | maximum = max([self.counts['A'][i], self.counts['C'][i], self.counts |
> | ][i], self.counts['T'][i]]) | > | ['G'][i], self.counts['T'][i]]) | ||
74 | if cap == self.counts['A'][i]: | 75 | if maximum == self.counts['A'][i]: | ||
75 | self.consensus += 'A' | 76 | self.consensus += 'A' | ||
n | 76 | elif cap == self.counts['C'][i]: | n | 77 | elif maximum == self.counts['C'][i]: |
77 | self.consensus += 'C' | 78 | self.consensus += 'C' | ||
n | 78 | elif cap == self.counts['G'][i]: | n | 79 | elif maximum == self.counts['G'][i]: |
79 | self.consensus += 'G' | 80 | self.consensus += 'G' | ||
n | 80 | elif cap == self.counts['T'][i]: | n | 81 | elif maximum == self.counts['T'][i]: |
81 | self.consensus += 'T' | 82 | self.consensus += 'T' | ||
82 | return self.consensus | 83 | return self.consensus | ||
83 | def parse(self, filename): | 84 | def parse(self, filename): | ||
t | 84 | myfile = open(filename) | t | 85 | pyfile = open(filename) |
85 | lines = myfile.readlines() | 86 | lines = pyfile.readlines() | ||
86 | myfile.close() | 87 | pyfile.close() | ||
87 | for line in lines: | 88 | for line in lines: | ||
88 | if line[0] != '>': | 89 | if line[0] != '>': | ||
89 | self.instances.append(line) | 90 | self.instances.append(line) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | from numpy import cos, sin, pi | 2 | from numpy import cos, sin, pi | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__ (self, initResult, generator, n, delta_Theta): | n | 4 | def __init__ (self, initState, generator, n, delta_theta): |
5 | self.init_result = initResult | 5 | self.init_state = initState | ||
6 | self.gen = generator | 6 | self.gen = generator | ||
7 | self.num = n | 7 | self.num = n | ||
n | 8 | self.deltaTheta = delta_Theta | n | 8 | self.deltatheta = delta_theta |
9 | self.str = self.init_result | 9 | self.str = self.init_state | ||
10 | self.str2 = "" | 10 | self.str2 = '' | ||
11 | for i in range(self.num): | 11 | for i in range(self.num): | ||
n | 12 | self.str2 = "" | n | 12 | self.str2 = '' |
13 | for char in self.str: | 13 | for char in self.str: | ||
14 | if char in self.gen: | 14 | if char in self.gen: | ||
15 | self.str2 += self.gen[char] | 15 | self.str2 += self.gen[char] | ||
16 | else: | 16 | else: | ||
17 | self.str2 += char | 17 | self.str2 += char | ||
n | 18 | self.str = self.str2 | n | 18 | self.str = self.str2 |
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
20 | currentPt = (200,0) | 20 | currentpt = (200,0) | ||
21 | theta = pi/2 | 21 | theta = pi/2 | ||
n | 22 | nextPt = () | n | 22 | nextpt = () |
23 | stack = [] | 23 | stack = [] | ||
24 | for character in self.str: | 24 | for CHR in self.str: | ||
25 | if character.isalpha(): | 25 | if CHR.isalpha(): | ||
26 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) | 26 | nextpt = (currentpt[0]+cos(theta), currentpt[1]+sin(theta)) | ||
27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 27 | plt.plot([currentpt[0],nextpt[0]],[currentpt[1],nextpt[1]],color | ||
> | ='black') | > | ='black') | ||
28 | currentPt = nextPt | 28 | currentpt = nextpt | ||
29 | elif character == "[": | 29 | elif CHR == '[': | ||
30 | stack.append([currentPt, theta]) | 30 | stack.append([currentpt, theta]) | ||
31 | elif character == "]": | 31 | elif CHR == ']': | ||
32 | currentPt, theta = stack.pop() | 32 | currentpt, theta = stack.pop() | ||
33 | elif character == "+": | 33 | elif CHR == '+': | ||
34 | theta += (self.deltaTheta * (pi/180)) | 34 | theta += (self.deltatheta * (pi/180)) | ||
35 | elif character == "-": | 35 | elif CHR == '-': | ||
36 | theta -= (self.deltaTheta * (pi/180)) | 36 | theta -= (self.deltatheta * (pi/180)) | ||
37 | plt.show() | ||||
38 | class DNAMOTIF: | 37 | class DNAMOTIF: | ||
39 | def __init__(self): | 38 | def __init__(self): | ||
40 | self.instances=[] | 39 | self.instances=[] | ||
41 | self.consensus=[] | 40 | self.consensus=[] | ||
42 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
43 | def __str__(self): | 42 | def __str__(self): | ||
n | 44 | pystr = '' | n | 43 | mystring = '' |
45 | for item in self.instances: | 44 | for item in self.instances: | ||
n | 46 | pystr += item | n | 45 | mystring += item |
47 | return pystr | 46 | return mystring | ||
48 | def __len__(self): | 47 | def __len__(self): | ||
49 | return len(self.instances[0].rstrip('\n')) | 48 | return len(self.instances[0].rstrip('\n')) | ||
50 | def count(self): | 49 | def count(self): | ||
51 | for index in range(self.__len__()): | 50 | for index in range(self.__len__()): | ||
52 | countA = 0 | 51 | countA = 0 | ||
53 | countC = 0 | 52 | countC = 0 | ||
54 | countG = 0 | 53 | countG = 0 | ||
55 | countT = 0 | 54 | countT = 0 | ||
56 | for item in self.instances: | 55 | for item in self.instances: | ||
57 | if item[index].upper() == 'A': | 56 | if item[index].upper() == 'A': | ||
58 | countA += 1 | 57 | countA += 1 | ||
59 | elif item[index].upper() == 'C': | 58 | elif item[index].upper() == 'C': | ||
60 | countC += 1 | 59 | countC += 1 | ||
61 | elif item[index].upper() == 'G': | 60 | elif item[index].upper() == 'G': | ||
62 | countG += 1 | 61 | countG += 1 | ||
63 | elif item[index].upper() == 'T': | 62 | elif item[index].upper() == 'T': | ||
64 | countT += 1 | 63 | countT += 1 | ||
65 | self.counts['A'].append(countA) | 64 | self.counts['A'].append(countA) | ||
66 | self.counts['C'].append(countC) | 65 | self.counts['C'].append(countC) | ||
67 | self.counts['G'].append(countG) | 66 | self.counts['G'].append(countG) | ||
68 | self.counts['T'].append(countT) | 67 | self.counts['T'].append(countT) | ||
69 | def compute_consensus(self): | 68 | def compute_consensus(self): | ||
70 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 69 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
71 | self.count() | 70 | self.count() | ||
72 | self.consensus = '' | 71 | self.consensus = '' | ||
73 | for i in range(self.__len__()): | 72 | for i in range(self.__len__()): | ||
n | 74 | maximum = max([self.counts['A'][i], self.counts['C'][i], self.counts | n | 73 | cap = max([self.counts['A'][i], self.counts['C'][i], self.counts['G' |
> | ['G'][i], self.counts['T'][i]]) | > | ][i], self.counts['T'][i]]) | ||
75 | if maximum == self.counts['A'][i]: | 74 | if cap == self.counts['A'][i]: | ||
76 | self.consensus += 'A' | 75 | self.consensus += 'A' | ||
n | 77 | elif maximum == self.counts['C'][i]: | n | 76 | elif cap == self.counts['C'][i]: |
78 | self.consensus += 'C' | 77 | self.consensus += 'C' | ||
n | 79 | elif maximum == self.counts['G'][i]: | n | 78 | elif cap == self.counts['G'][i]: |
80 | self.consensus += 'G' | 79 | self.consensus += 'G' | ||
n | 81 | elif maximum == self.counts['T'][i]: | n | 80 | elif cap == self.counts['T'][i]: |
82 | self.consensus += 'T' | 81 | self.consensus += 'T' | ||
83 | return self.consensus | 82 | return self.consensus | ||
84 | def parse(self, filename): | 83 | def parse(self, filename): | ||
t | 85 | pyfile = open(filename) | t | 84 | myfile = open(filename) |
86 | lines = pyfile.readlines() | 85 | lines = myfile.readlines() | ||
87 | pyfile.close() | 86 | myfile.close() | ||
88 | for line in lines: | 87 | for line in lines: | ||
89 | if line[0] != '>': | 88 | if line[0] != '>': | ||
90 | self.instances.append(line) | 89 | self.instances.append(line) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import matplotlib.pyplot as plot | n | 1 | import matplotlib.pyplot as plt |
2 | from numpy import cos, sin, pi | 2 | from numpy import cos, sin, pi | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, initState, generator, n, delta_Theta): | n | 4 | def __init__ (self, initState, generator, n, delta_theta): |
5 | self.init_state = initState | 5 | self.init_state = initState | ||
n | 6 | self.generator = generator | n | 6 | self.gen = generator |
7 | self.num = n | 7 | self.num = n | ||
n | 8 | self.delta_theta = delta_Theta | n | 8 | self.deltatheta = delta_theta |
9 | self.str = self.init_state | 9 | self.str = self.init_state | ||
n | 10 | self.str1 = "" | n | 10 | self.str2 = '' |
11 | for i in range(self.num): | 11 | for i in range(self.num): | ||
n | 12 | self.str1='' | n | 12 | self.str2 = '' |
13 | for char in self.str: | 13 | for char in self.str: | ||
n | 14 | if char in self.generator: | n | 14 | if char in self.gen: |
15 | self.str1 += self.generator[char] | 15 | self.str2 += self.gen[char] | ||
16 | else: | 16 | else: | ||
n | 17 | self.str1 += char | n | 17 | self.str2 += char |
18 | self.str=self.str1 | 18 | self.str = self.str2 | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 20 | currentPt = (200,0) | n | 20 | currentpt = (200,0) |
21 | theta = pi/2 | 21 | theta = pi/2 | ||
n | 22 | nextPt = () | n | 22 | nextpt = () |
23 | stack = [] | 23 | stack = [] | ||
24 | for CHR in self.str: | 24 | for CHR in self.str: | ||
25 | if CHR.isalpha(): | 25 | if CHR.isalpha(): | ||
n | 26 | nextPt=(currentPt(0)+cos(theta), sin(theta)+currentPt(1)) | n | 26 | nextpt = (currentpt[0]+cos(theta), currentpt[1]+sin(theta)) |
27 | plot.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],colo | 27 | plt.plot([currentpt[0],nextpt[0]],[currentpt[1],nextpt[1]],color | ||
> | r='black') | > | ='black') | ||
28 | currentPt=nextPt | 28 | currentpt = nextpt | ||
29 | elif CHR=='[': | 29 | elif CHR == '[': | ||
30 | stack.append[(currentPt, theta)] | 30 | stack.append([currentpt, theta]) | ||
31 | elif CHR==']': | 31 | elif CHR == ']': | ||
32 | currentPt, theta=stack.pop() | 32 | currentpt, theta = stack.pop() | ||
33 | elif CHR=='+': | 33 | elif CHR == '+': | ||
34 | theta += (self.delta_theta*[pi/180]) | 34 | theta += (self.deltatheta * (pi/180)) | ||
35 | elif CHR=='-': | 35 | elif CHR == '-': | ||
36 | theta -= (self.delta_theta*[pi/180]) | 36 | theta -= (self.deltatheta * (pi/180)) | ||
37 | plot.show() | ||||
38 | class DNAMOTIF: | 37 | class DNAMOTIF: | ||
39 | def __init__(self): | 38 | def __init__(self): | ||
40 | self.instances=[] | 39 | self.instances=[] | ||
41 | self.consensus=[] | 40 | self.consensus=[] | ||
42 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
43 | def __str__(self): | 42 | def __str__(self): | ||
n | 44 | thestr = '' | n | 43 | mystring = '' |
45 | for item in self.instances: | 44 | for item in self.instances: | ||
n | 46 | thestr += item | n | 45 | mystring += item |
47 | return thestr | 46 | return mystring | ||
48 | def __len__(self): | 47 | def __len__(self): | ||
n | 49 | return len(self.instances[0].rstrip("\n")) | n | 48 | return len(self.instances[0].rstrip('\n')) |
50 | def count(self): | 49 | def count(self): | ||
51 | for index in range(self.__len__()): | 50 | for index in range(self.__len__()): | ||
52 | countA = 0 | 51 | countA = 0 | ||
53 | countC = 0 | 52 | countC = 0 | ||
54 | countG = 0 | 53 | countG = 0 | ||
55 | countT = 0 | 54 | countT = 0 | ||
56 | for item in self.instances: | 55 | for item in self.instances: | ||
57 | if item[index].upper() == 'A': | 56 | if item[index].upper() == 'A': | ||
58 | countA += 1 | 57 | countA += 1 | ||
59 | elif item[index].upper() == 'C': | 58 | elif item[index].upper() == 'C': | ||
60 | countC += 1 | 59 | countC += 1 | ||
61 | elif item[index].upper() == 'G': | 60 | elif item[index].upper() == 'G': | ||
62 | countG += 1 | 61 | countG += 1 | ||
63 | elif item[index].upper() == 'T': | 62 | elif item[index].upper() == 'T': | ||
64 | countT += 1 | 63 | countT += 1 | ||
65 | self.counts['A'].append(countA) | 64 | self.counts['A'].append(countA) | ||
66 | self.counts['C'].append(countC) | 65 | self.counts['C'].append(countC) | ||
67 | self.counts['G'].append(countG) | 66 | self.counts['G'].append(countG) | ||
n | 68 | self.counts['T'].append(countT) | n | 67 | self.counts['T'].append(countT) |
69 | def compute_consensus(self): | 68 | def compute_consensus(self): | ||
70 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 69 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
71 | self.count() | 70 | self.count() | ||
n | 72 | self.consensus = "" | n | 71 | self.consensus = '' |
73 | for i in range(self.__len__()): | 72 | for i in range(self.__len__()): | ||
n | 74 | maximum = max([self.counts['A'][i], self.counts['C'][i], self.counts | n | 73 | cap = max([self.counts['A'][i], self.counts['C'][i], self.counts['G' |
> | ['G'][i], self.counts['T'][i]]) | > | ][i], self.counts['T'][i]]) | ||
75 | if maximum == self.counts['A'][i]: | 74 | if cap == self.counts['A'][i]: | ||
76 | self.consensus += 'A' | 75 | self.consensus += 'A' | ||
n | 77 | elif maximum == self.counts['C'][i]: | n | 76 | elif cap == self.counts['C'][i]: |
78 | self.consensus += 'C' | 77 | self.consensus += 'C' | ||
n | 79 | elif maximum == self.counts['G'][i]: | n | 78 | elif cap == self.counts['G'][i]: |
80 | self.consensus += 'G' | 79 | self.consensus += 'G' | ||
t | 81 | elif maximum == self.counts['T'][i]: | t | 80 | elif cap == self.counts['T'][i]: |
82 | self.consensus += 'T' | 81 | self.consensus += 'T' | ||
83 | return self.consensus | 82 | return self.consensus | ||
84 | def parse(self, filename): | 83 | def parse(self, filename): | ||
85 | myfile = open(filename) | 84 | myfile = open(filename) | ||
86 | lines = myfile.readlines() | 85 | lines = myfile.readlines() | ||
87 | myfile.close() | 86 | myfile.close() | ||
88 | for line in lines: | 87 | for line in lines: | ||
89 | if line[0] != '>': | 88 | if line[0] != '>': | ||
90 | self.instances.append(line) | 89 | self.instances.append(line) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | n | 2 | plt.style.use('bmh') | ||
2 | plt.plot( | 3 | plt.plot( | ||
3 | [0, 1, 2], | 4 | [0, 1, 2], | ||
4 | [0, 1, 0] | 5 | [0, 1, 0] | ||
n | 5 | ) | n | 6 | ) |
6 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
n | 7 | plt.ylabel('y') | n | 8 | plt.ylabel('y'); |
8 | def plot_coords(coords, bare_plot=False): | 9 | def plot_coords(coords, bare_plot=False): | ||
9 | if bare_plot: | 10 | if bare_plot: | ||
10 | plt.axis('off') | 11 | plt.axis('off') | ||
n | 11 | plt.axes().set_aspect('equal', 'datalim') | n | 12 | plt.axes().set_aspect('equal', 'datalim') |
12 | X, Y = zip(*coords) | 13 | X, Y = zip(*coords) | ||
13 | plt.plot(X, Y); | 14 | plt.plot(X, Y); | ||
14 | plot_coords([ | 15 | plot_coords([ | ||
15 | (0, 0), | 16 | (0, 0), | ||
16 | (1, 0), | 17 | (1, 0), | ||
17 | (2, 1), | 18 | (2, 1), | ||
18 | (3, 1), | 19 | (3, 1), | ||
19 | (2, 0) | 20 | (2, 0) | ||
20 | ]) | 21 | ]) | ||
21 | nan = float('nan') | 22 | nan = float('nan') | ||
22 | plot_coords([ | 23 | plot_coords([ | ||
23 | (0, 0), | 24 | (0, 0), | ||
24 | (1, 1), | 25 | (1, 1), | ||
25 | (nan, nan), | 26 | (nan, nan), | ||
26 | (1, 0), | 27 | (1, 0), | ||
27 | (2, 1) | 28 | (2, 1) | ||
28 | ]) | 29 | ]) | ||
29 | from math import pi, sin, cos | 30 | from math import pi, sin, cos | ||
30 | DEGREES_TO_RADIANS = pi / 180 | 31 | DEGREES_TO_RADIANS = pi / 180 | ||
n | 31 | def drawPlant(turtle_program, turn_amount=45): | n | 32 | def turtle_to_coords(turtle_program, turn_amount=45): |
32 | currentPt = (200.0, 0.0, 90.0) | 33 | state = (0.0, 0.0, 90.0) | ||
33 | yield (0.0, 0.0) | 34 | yield (0.0, 0.0) | ||
34 | for command in turtle_program: | 35 | for command in turtle_program: | ||
n | 35 | x, y, angle = currentPt | n | 36 | x, y, angle = state |
36 | if command in 'Ff': | 37 | if command in 'Ff': | ||
n | 37 | currentPt = (x - cos(angle * DEGREES_TO_RADIANS), | n | 38 | state = (x - cos(angle * DEGREES_TO_RADIANS), |
38 | y + sin(angle * DEGREES_TO_RADIANS), | 39 | y + sin(angle * DEGREES_TO_RADIANS), | ||
39 | angle) | 40 | angle) | ||
40 | if command == 'f': | 41 | if command == 'f': | ||
41 | yield (float('nan'), float('nan')) | 42 | yield (float('nan'), float('nan')) | ||
42 | yield (currentPt[0], currentPt[1]) | 43 | yield (state[0], state[1]) | ||
43 | elif command == '+': | 44 | elif command == '+': | ||
n | 44 | currentPt = (x, y, angle + turn_amount) | n | 45 | state = (x, y, angle + turn_amount) |
45 | elif command == '-': | 46 | elif command == '-': | ||
n | 46 | currentPt = (x, y, angle - turn_amount) | n | 47 | state = (x, y, angle - turn_amount) |
47 | plot_coords(drawPlant('FfF++FfF++FfF++FfF')) | 48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
48 | plot_coords(drawPlant('F-F+F+F+f+F+F+F-F')) | 49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
49 | from math import isnan | 50 | from math import isnan | ||
50 | def print_coords(coords): | 51 | def print_coords(coords): | ||
51 | for (x, y) in coords: | 52 | for (x, y) in coords: | ||
52 | if isnan(x): | 53 | if isnan(x): | ||
53 | print('<gap>') | 54 | print('<gap>') | ||
54 | else: | 55 | else: | ||
55 | print('({:.2f}, {:.2f})'.format(x, y)) | 56 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
n | 56 | print_coords(drawPlant('F-F+F+F+f+F+F+F-F')) | n | 57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) |
57 | plot_coords(drawPlant('F-F+F+F+f+F+F+F-F', 65)) | 58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
58 | def transform_sequence(sequence, transformations): | 59 | def transform_sequence(sequence, transformations): | ||
59 | return ''.join(transformations.get(c, c) for c in sequence) | 60 | return ''.join(transformations.get(c, c) for c in sequence) | ||
60 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | 61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | ||
n | 61 | plot_coords(drawPlant(transform_sequence('FFFF', {'F': 'FfF++'}))) | n | 62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) |
62 | def transform_multiple(sequence, transformations, iterations): | 63 | def transform_multiple(sequence, transformations, iterations): | ||
63 | for _ in range(iterations): | 64 | for _ in range(iterations): | ||
64 | sequence = transform_sequence(sequence, transformations) | 65 | sequence = transform_sequence(sequence, transformations) | ||
65 | return sequence | 66 | return sequence | ||
66 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | 67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | ||
67 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
68 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
n | 69 | plot_coords(drawPlant(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | n | 70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) |
70 | plot_coords(drawPlant(transform_multiple('L', {'L': '-RF+LFL+FR-','R': '+LF-RFR- | 71 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
> | FL+'}, 5), 90)) | ||||
72 | 'L': '-RF+LFL+FR-', | ||||
73 | 'R': '+LF-RFR-FL+' | ||||
74 | }, 5), 90)) | ||||
71 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | 75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
n | 72 | nextPt = list() | n | 76 | saved_states = list() |
73 | currentPt = (200, 0, 90) | 77 | state = (0, 0, 90) | ||
74 | yield (0, 0) | 78 | yield (0, 0) | ||
75 | for command in turtle_program: | 79 | for command in turtle_program: | ||
n | 76 | x, y, angle = currentPt | n | 80 | x, y, angle = state |
77 | if command.lower() in 'abcdefghij': | 81 | if command.lower() in 'abcdefghij': | ||
78 | currentPt = (y + sin(angle * DEGREES_TO_RADIANS), x - cos(angle * DE | 82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
> | GREES_TO_RADIANS),angle) | ||||
83 | y + sin(angle * DEGREES_TO_RADIANS), | ||||
84 | angle) | ||||
79 | if command.islower(): | 85 | if command.islower(): | ||
80 | yield (float('nan'), float('nan')) | 86 | yield (float('nan'), float('nan')) | ||
81 | yield (currentPt[0], currentPt[1]) | 87 | yield (state[0], state[1]) | ||
82 | elif command == '+': | 88 | elif command == '+': | ||
83 | state = (x, y, angle + turn_amount) | 89 | state = (x, y, angle + turn_amount) | ||
84 | elif command == '-': | 90 | elif command == '-': | ||
85 | currentPt = (x, y, angle - turn_amount) | 91 | state = (x, y, angle - turn_amount) | ||
86 | elif command == '[': | 92 | elif command == '[': | ||
87 | nextPt.append(currentPt) | 93 | saved_states.append(state) | ||
88 | elif command == ']': | 94 | elif command == ']': | ||
89 | currentPt = nextPt.pop() | 95 | state = saved_states.pop() | ||
90 | yield (float('nan'), float('nan')) | 96 | yield (float('nan'), float('nan')) | ||
91 | x, y, _ = state | 97 | x, y, _ = state | ||
92 | yield (x, y) | 98 | yield (x, y) | ||
93 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | 99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
94 | def l_plot(axiom, transformations, iterations=0, angle=45): | 100 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
95 | turtle_program = transform_multiple(axiom, transformations, iterations) | 101 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
96 | coords = branching_turtle_to_coords(turtle_program, angle) | 102 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
97 | plot_coords(coords, bare_plot=True) | 103 | plot_coords(coords, bare_plot=True) | ||
n | 98 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | n | 104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) |
99 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
100 | for i in range(5): | 106 | for i in range(5): | ||
101 | print('{}: '.format(i), | 107 | print('{}: '.format(i), | ||
102 | transform_multiple('A', {'A': 'F+A'}, i)) | 108 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
103 | for i in range(5): | 109 | for i in range(5): | ||
104 | print('{}: '.format(i), | 110 | print('{}: '.format(i), | ||
105 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | 111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
106 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)import sys | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | ||
107 | sys.setrecursionlimit(3000) | ||||
108 | class DNAMOTIF: | ||||
109 | def __init__(self): | 113 | def __init__(self): | ||
n | 110 | self.instances=[] | n | 114 | self.instances = [] |
111 | self.consensus=[] | 115 | self.consensus=[] | ||
112 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | ||
113 | def __str__(self): | 117 | def __str__ (self): | ||
114 | new_str = "" | 118 | string = "" | ||
115 | for i in self.instances: | 119 | for i in self.instances: | ||
n | 116 | new_str += i + "\n" | n | 120 | string = string + str(i) + "\n" |
121 | return string[:-2] | ||||
117 | def __len__(self): | 122 | def __len__(self): | ||
n | 118 | lexA = DNAMOTIF() | n | 123 | return (len(self.instances)-4) |
119 | lexA.parse("lexA.fasta") | ||||
120 | return len(lexA) | ||||
121 | def count(self): | 124 | def count(self): | ||
122 | for i in self.instances: | 125 | for i in self.instances: | ||
123 | temp = i.upper() | 126 | temp = i.upper() | ||
124 | self.counts["A"].append(temp.count("A")) | 127 | self.counts["A"].append(temp.count("A")) | ||
125 | self.counts["C"].append(temp.count("C")) | 128 | self.counts["C"].append(temp.count("C")) | ||
126 | self.counts["G"].append(temp.count("G")) | 129 | self.counts["G"].append(temp.count("G")) | ||
127 | self.counts["T"].append(temp.count("T")) | 130 | self.counts["T"].append(temp.count("T")) | ||
128 | def compute_consensus(self): | 131 | def compute_consensus(self): | ||
129 | A = self.counts["A"] | 132 | A = self.counts["A"] | ||
130 | C = self.counts["C"] | 133 | C = self.counts["C"] | ||
131 | G = self.counts["G"] | 134 | G = self.counts["G"] | ||
132 | T = self.counts["T"] | 135 | T = self.counts["T"] | ||
133 | for i in range(len(A)): | 136 | for i in range(len(A)): | ||
134 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
135 | self.consensus.append("A") | 138 | self.consensus.append("A") | ||
136 | elif (C[i] >= G[i] and C[i] >= T[i]): | 139 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
137 | self.consensus.append("C") | 140 | self.consensus.append("C") | ||
138 | elif (G[i] >= T[i]): | 141 | elif (G[i] >= T[i]): | ||
139 | self.consensus.append("G") | 142 | self.consensus.append("G") | ||
140 | else: | 143 | else: | ||
141 | self.consensus.append("T") | 144 | self.consensus.append("T") | ||
n | n | 145 | for x in self.consensus: | ||
146 | print(x,end='') | ||||
142 | def parse(self, filename): | 147 | def parse(self, filename): | ||
t | 143 | with open(filename,'r') as file: | t | 148 | with open(filename,'r') as f: |
144 | for i in file: | 149 | for i in f: | ||
145 | if ">" in i: | 150 | if ">" in i: | ||
146 | continue | 151 | continue | ||
147 | else: | 152 | else: | ||
148 | self.instances.append(i) | 153 | self.instances.append(i) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | from numpy import cos, sin, pi | 2 | from numpy import cos, sin, pi | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, initState, generator, n, delta_Theta): | 4 | def __init__(self, initState, generator, n, delta_Theta): | ||
5 | self.init_state = initState | 5 | self.init_state = initState | ||
6 | self.gen = generator | 6 | self.gen = generator | ||
7 | self.num = n | 7 | self.num = n | ||
8 | self.deltaTheta = delta_Theta | 8 | self.deltaTheta = delta_Theta | ||
9 | self.str = self.init_state | 9 | self.str = self.init_state | ||
10 | self.str2 = "" | 10 | self.str2 = "" | ||
11 | for i in range(self.num): | 11 | for i in range(self.num): | ||
12 | self.str2 = "" | 12 | self.str2 = "" | ||
13 | for char in self.str: | 13 | for char in self.str: | ||
14 | if char in self.gen: | 14 | if char in self.gen: | ||
15 | self.str2 += self.gen[char] | 15 | self.str2 += self.gen[char] | ||
16 | else: | 16 | else: | ||
17 | self.str2 += char | 17 | self.str2 += char | ||
18 | self.str = self.str2 | 18 | self.str = self.str2 | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
20 | currentPt = (200,0) | 20 | currentPt = (200,0) | ||
21 | theta = pi/2 | 21 | theta = pi/2 | ||
22 | nextPt = () | 22 | nextPt = () | ||
23 | stack = [] | 23 | stack = [] | ||
24 | for CHR in self.str: | 24 | for CHR in self.str: | ||
25 | if CHR.isalpha(): | 25 | if CHR.isalpha(): | ||
26 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) | 26 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) | ||
27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
28 | currentPt = nextPt | 28 | currentPt = nextPt | ||
29 | elif CHR == "[": | 29 | elif CHR == "[": | ||
30 | stack.append([currentPt, theta]) | 30 | stack.append([currentPt, theta]) | ||
31 | elif CHR == "]": | 31 | elif CHR == "]": | ||
32 | currentPt, theta = stack.pop() | 32 | currentPt, theta = stack.pop() | ||
33 | elif CHR == "+": | 33 | elif CHR == "+": | ||
34 | theta += (self.deltaTheta * (pi/180)) | 34 | theta += (self.deltaTheta * (pi/180)) | ||
35 | elif CHR == "-": | 35 | elif CHR == "-": | ||
n | 36 | theta -= (self.deltaTheta * (pi/180)) | n | 36 | theta -= (self.deltaTheta * (pi/180))class DNAMOTIF: |
37 | plt.show() | ||||
38 | class DNAMOTIF: | ||||
39 | def __init__(self): | 37 | def __init__(self): | ||
40 | self.instances=[] | 38 | self.instances=[] | ||
n | 41 | self.consensus=[] | n | 39 | self.consensus="" |
42 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 40 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
43 | def __str__(self): | 41 | def __str__(self): | ||
n | 44 | string = "" | n | 42 | mystr = "" |
45 | for thing in self.instances: | 43 | for item in self.instances: | ||
46 | string = string + item | 44 | mystr += item | ||
47 | return string | 45 | return mystr | ||
48 | def __len__(self): | 46 | def __len__(self): | ||
n | 49 | return len(self.instances[0].rstrip('\n')) | n | 47 | return len(self.instances[0].rstrip("\n")) |
50 | def count(self): | 48 | def count(self): | ||
51 | for index in range(self.__len__()): | 49 | for index in range(self.__len__()): | ||
n | 52 | numA = 0 | n | 50 | countA = 0 |
53 | numC = 0 | 51 | countC = 0 | ||
54 | numG = 0 | 52 | countG = 0 | ||
55 | numT = 0 | 53 | countT = 0 | ||
56 | for thing in self.instances: | 54 | for item in self.instances: | ||
57 | if thing[index].upper() == "A": | 55 | if item[index].upper() == "A": | ||
58 | numA += 1 | 56 | countA += 1 | ||
59 | elif thing[index].upper() == "C": | 57 | elif item[index].upper() == "C": | ||
60 | numC += 1 | 58 | countC += 1 | ||
61 | elif thing[index].upper() == "G": | 59 | elif item[index].upper() == "G": | ||
62 | numG += 1 | 60 | countG += 1 | ||
63 | elif thing[index].upper() == "T": | 61 | elif item[index].upper() == "T": | ||
64 | numT += 1 | 62 | countT += 1 | ||
65 | self.counts["A"].append(numA) | 63 | self.counts["A"].append(countA) | ||
66 | self.counts["C"].append(numC) | 64 | self.counts["C"].append(countC) | ||
67 | self.counts["G"].append(numG) | 65 | self.counts["G"].append(countG) | ||
68 | self.counts["T"].append(numT) | 66 | self.counts["T"].append(countT) | ||
69 | def compute_consensus(self): | 67 | def compute_consensus(self): | ||
70 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 68 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
71 | self.count() | 69 | self.count() | ||
n | 72 | self.consensus = "" | n | ||
73 | for letter in range(self.__len__()): | 70 | for i in range(self.__len__()): | ||
74 | maximum = max([self.counts["A"][letter], self.counts["C"][letter], s | 71 | maximum = max([self.counts["A"][i], self.counts["C"][i], self.counts | ||
> | elf.counts["G"][letter], self.counts["T"][letter]]) | > | ["G"][i], self.counts["T"][i]]) | ||
75 | if maximum == self.counts["A"][letter]: | 72 | if maximum == self.counts["A"][i]: | ||
76 | self.consensus += "A" | 73 | self.consensus += "A" | ||
n | 77 | elif maximum == self.counts["C"][letter]: | n | 74 | elif maximum == self.counts["C"][i]: |
78 | self.consensus += "C" | 75 | self.consensus += "C" | ||
n | 79 | elif maximum == self.counts["G"][letter]: | n | 76 | elif maximum == self.counts["G"][i]: |
80 | self.consensus += "G" | 77 | self.consensus += "G" | ||
n | 81 | elif maximum == self.counts["T"][letter]: | n | 78 | elif maximum == self.counts["T"][i]: |
82 | self.consensus += "T" | 79 | self.consensus += "T" | ||
83 | return self.consensus | 80 | return self.consensus | ||
84 | def parse(self, filename): | 81 | def parse(self, filename): | ||
n | 85 | thefile = open(filename) | n | 82 | myfile = open(filename) |
86 | lines = thefile.readlines() | 83 | lines = myfile.readlines() | ||
87 | thefile.close() | 84 | myfile.close() | ||
88 | for line in lines: | 85 | for line in lines: | ||
89 | if line[0] != ">": | 86 | if line[0] != ">": | ||
90 | self.instances.append(line) | 87 | self.instances.append(line) | ||
t | t | 88 | if __name__ == "__main__": | ||
89 | lexA = DNAMOTIF() | ||||
90 | lexA.parse("lexA.fasta") | ||||
91 | print(lexA) | ||||
92 | lexA.count() | ||||
93 | print(lexA.counts) | ||||
94 | lexA.compute_consensus() | ||||
95 | print(lexA.consensus) | ||||
96 | print(lexA.consensus == "TACTGTATATATATACAGTA") |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | from numpy import cos, sin, pi | 2 | from numpy import cos, sin, pi | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, initState, generator, n, delta_Theta): | 4 | def __init__(self, initState, generator, n, delta_Theta): | ||
5 | self.init_state = initState | 5 | self.init_state = initState | ||
6 | self.gen = generator | 6 | self.gen = generator | ||
7 | self.num = n | 7 | self.num = n | ||
8 | self.deltaTheta = delta_Theta | 8 | self.deltaTheta = delta_Theta | ||
9 | self.str = self.init_state | 9 | self.str = self.init_state | ||
10 | self.str2 = "" | 10 | self.str2 = "" | ||
11 | for i in range(self.num): | 11 | for i in range(self.num): | ||
12 | self.str2 = "" | 12 | self.str2 = "" | ||
13 | for char in self.str: | 13 | for char in self.str: | ||
14 | if char in self.gen: | 14 | if char in self.gen: | ||
15 | self.str2 += self.gen[char] | 15 | self.str2 += self.gen[char] | ||
16 | else: | 16 | else: | ||
17 | self.str2 += char | 17 | self.str2 += char | ||
18 | self.str = self.str2 | 18 | self.str = self.str2 | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
20 | currentPt = (200,0) | 20 | currentPt = (200,0) | ||
21 | theta = pi/2 | 21 | theta = pi/2 | ||
22 | nextPt = () | 22 | nextPt = () | ||
23 | stack = [] | 23 | stack = [] | ||
24 | for CHR in self.str: | 24 | for CHR in self.str: | ||
25 | if CHR.isalpha(): | 25 | if CHR.isalpha(): | ||
26 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) | 26 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) | ||
27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
28 | currentPt = nextPt | 28 | currentPt = nextPt | ||
29 | elif CHR == "[": | 29 | elif CHR == "[": | ||
30 | stack.append([currentPt, theta]) | 30 | stack.append([currentPt, theta]) | ||
31 | elif CHR == "]": | 31 | elif CHR == "]": | ||
32 | currentPt, theta = stack.pop() | 32 | currentPt, theta = stack.pop() | ||
33 | elif CHR == "+": | 33 | elif CHR == "+": | ||
34 | theta += (self.deltaTheta * (pi/180)) | 34 | theta += (self.deltaTheta * (pi/180)) | ||
35 | elif CHR == "-": | 35 | elif CHR == "-": | ||
n | 36 | theta -= (self.deltaTheta * (pi/180))class DNAMOTIF: | n | 36 | theta -= (self.deltaTheta * (pi/180)) |
37 | plt.show() | ||||
38 | class DNAMOTIF: | ||||
37 | def __init__(self): | 39 | def __init__(self): | ||
38 | self.instances=[] | 40 | self.instances=[] | ||
n | 39 | self.consensus="" | n | 41 | self.consensus=[] |
40 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 42 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
41 | def __str__(self): | 43 | def __str__(self): | ||
n | 42 | mystr = "" | n | 44 | string = "" |
43 | for item in self.instances: | 45 | for thing in self.instances: | ||
44 | mystr += item | 46 | string = string + item | ||
45 | return mystr | 47 | return string | ||
46 | def __len__(self): | 48 | def __len__(self): | ||
n | 47 | return len(self.instances[0].rstrip("\n")) | n | 49 | return len(self.instances[0].rstrip('\n')) |
48 | def count(self): | 50 | def count(self): | ||
49 | for index in range(self.__len__()): | 51 | for index in range(self.__len__()): | ||
n | 50 | countA = 0 | n | 52 | numA = 0 |
51 | countC = 0 | 53 | numC = 0 | ||
52 | countG = 0 | 54 | numG = 0 | ||
53 | countT = 0 | 55 | numT = 0 | ||
54 | for item in self.instances: | 56 | for thing in self.instances: | ||
55 | if item[index].upper() == "A": | 57 | if thing[index].upper() == "A": | ||
56 | countA += 1 | 58 | numA += 1 | ||
57 | elif item[index].upper() == "C": | 59 | elif thing[index].upper() == "C": | ||
58 | countC += 1 | 60 | numC += 1 | ||
59 | elif item[index].upper() == "G": | 61 | elif thing[index].upper() == "G": | ||
60 | countG += 1 | 62 | numG += 1 | ||
61 | elif item[index].upper() == "T": | 63 | elif thing[index].upper() == "T": | ||
62 | countT += 1 | 64 | numT += 1 | ||
63 | self.counts["A"].append(countA) | 65 | self.counts["A"].append(numA) | ||
64 | self.counts["C"].append(countC) | 66 | self.counts["C"].append(numC) | ||
65 | self.counts["G"].append(countG) | 67 | self.counts["G"].append(numG) | ||
66 | self.counts["T"].append(countT) | 68 | self.counts["T"].append(numT) | ||
67 | def compute_consensus(self): | 69 | def compute_consensus(self): | ||
68 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 70 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
69 | self.count() | 71 | self.count() | ||
n | n | 72 | self.consensus = "" | ||
70 | for i in range(self.__len__()): | 73 | for letter in range(self.__len__()): | ||
71 | maximum = max([self.counts["A"][i], self.counts["C"][i], self.counts | 74 | maximum = max([self.counts["A"][letter], self.counts["C"][letter], s | ||
> | ["G"][i], self.counts["T"][i]]) | > | elf.counts["G"][letter], self.counts["T"][letter]]) | ||
72 | if maximum == self.counts["A"][i]: | 75 | if maximum == self.counts["A"][letter]: | ||
73 | self.consensus += "A" | 76 | self.consensus += "A" | ||
n | 74 | elif maximum == self.counts["C"][i]: | n | 77 | elif maximum == self.counts["C"][letter]: |
75 | self.consensus += "C" | 78 | self.consensus += "C" | ||
n | 76 | elif maximum == self.counts["G"][i]: | n | 79 | elif maximum == self.counts["G"][letter]: |
77 | self.consensus += "G" | 80 | self.consensus += "G" | ||
n | 78 | elif maximum == self.counts["T"][i]: | n | 81 | elif maximum == self.counts["T"][letter]: |
79 | self.consensus += "T" | 82 | self.consensus += "T" | ||
80 | return self.consensus | 83 | return self.consensus | ||
81 | def parse(self, filename): | 84 | def parse(self, filename): | ||
n | 82 | myfile = open(filename) | n | 85 | thefile = open(filename) |
83 | lines = myfile.readlines() | 86 | lines = thefile.readlines() | ||
84 | myfile.close() | 87 | thefile.close() | ||
85 | for line in lines: | 88 | for line in lines: | ||
86 | if line[0] != ">": | 89 | if line[0] != ">": | ||
87 | self.instances.append(line) | 90 | self.instances.append(line) | ||
t | 88 | if __name__ == "__main__": | t | ||
89 | lexA = DNAMOTIF() | ||||
90 | lexA.parse("lexA.fasta") | ||||
91 | print(lexA) | ||||
92 | lexA.count() | ||||
93 | print(lexA.counts) | ||||
94 | lexA.compute_consensus() | ||||
95 | print(lexA.consensus) | ||||
96 | print(lexA.consensus == "TACTGTATATATATACAGTA") |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
5 | self.str = string | 5 | self.str = string | ||
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | for x in range(n): | 9 | for x in range(n): | ||
10 | new_string = "" | 10 | new_string = "" | ||
n | 11 | for letr in self.str: | n | 11 | for letter in self.str: |
12 | if letr in self.dictionary: | 12 | if letter in self.dictionary: | ||
13 | new_string += self.dictionary[letr] | 13 | new_string += self.dictionary[letter] | ||
14 | else: | 14 | else: | ||
n | 15 | new_string += letr | n | 15 | new_string += letter |
16 | self.str = new_string | 16 | self.str = new_string | ||
17 | def drawPlant(self): | 17 | def drawPlant(self): | ||
18 | currentPt = (200,0) | 18 | currentPt = (200,0) | ||
n | 19 | lastpt = (0,200) | n | ||
20 | stack = [] | 19 | stack = [] | ||
n | 21 | turn = 90 | n | ||
22 | theta = 90 | 20 | theta = 90 | ||
n | 23 | for letr in self.str: | n | 21 | for letter in self.str: |
24 | if letr == "[": | 22 | if letter == "[": | ||
25 | stack.append([currentPt,theta]) | 23 | stack.append([currentPt,theta]) | ||
n | 26 | turn += 1 | n | ||
27 | elif letr == "]": | 24 | elif letter == "]": | ||
28 | latest = stack[-1] | 25 | latest = stack[-1] | ||
29 | currentPt = latest[0] | 26 | currentPt = latest[0] | ||
30 | theta = latest[1] | 27 | theta = latest[1] | ||
n | 31 | elif letr == "+": | n | 28 | elif letter == "+": |
32 | theta += self.deltaTheta | 29 | theta += self.deltaTheta | ||
n | 33 | turn += 1 | n | ||
34 | elif letr == "-": | 30 | elif letter == "-": | ||
35 | theta -= self.deltaTheta | 31 | theta -= self.deltaTheta | ||
n | 36 | turn = lastpt | n | ||
37 | else: | 32 | else: | ||
38 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.si | 33 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.si | ||
> | n(theta)) | > | n(theta)) | ||
39 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | 34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||
> | olor='black') | > | olor='black') | ||
40 | currentPt = nextPtclass DNAMOTIF: | 35 | currentPt = nextPtclass DNAMOTIF: | ||
n | 41 | cons = [] | n | ||
42 | A = 0 | ||||
43 | C = 0 | ||||
44 | G = 0 | ||||
45 | T = 0 | ||||
46 | def __init__(self): | 36 | def __init__(self): | ||
47 | self.instances = [] | 37 | self.instances = [] | ||
48 | self.consensus = [] | 38 | self.consensus = [] | ||
49 | self.counts = {"A": [], "C": [], "G": [], "T": []} | 39 | self.counts = {"A": [], "C": [], "G": [], "T": []} | ||
50 | def __str__(self): | 40 | def __str__(self): | ||
n | 51 | alp = '\n'.join(self.instances) | n | 41 | return "\n".join(self.instances) |
52 | return alp | ||||
53 | def __len__(self): | 42 | def __len__(self): | ||
n | 54 | DNAlen = len(self.instances[0])-1 | n | 43 | return len(self.instances[0])-1 |
55 | return DNAlen | ||||
56 | def parse(self, filename): | 44 | def parse(self, filename): | ||
57 | with open(filename, 'r') as reader: | 45 | with open(filename, 'r') as reader: | ||
n | 58 | self.instances = [x[0].lower() + x[1:].upper() for x in reader.readl | n | 46 | self.instances = [x[0].lower() + x[1:].upper() for x in reader.readl |
> | ines() if x[0] != '>'] | > | ines() if x[0] != ">"] | ||
59 | def count(self): | 47 | def count(self): | ||
n | 60 | cnt = 0 | n | 48 | count = 0 |
61 | self.counts = {'A': [0 for _ in range(len(self))], 'C': [0 for _ in rang | 49 | self.counts = {"A": [0 for _ in range(len(self))], "C": [0 for _ in rang | ||
> | e(len(self))], 'G': [0 for _ in range(len(self))], 'T': [0 for _ in range(len(se | > | e(len(self))], "G": [0 for _ in range(len(self))], "T": [0 for _ in range(len(se | ||
> | lf))]} | > | lf))]} | ||
62 | for instance in self.instances: | 50 | for instance in self.instances: | ||
n | 63 | for x, gene in enumerate(instance): | n | 51 | for x, letter in enumerate(instance): |
64 | if gene != '\n': | 52 | if letter != "\n": | ||
65 | self.counts[gene.upper()][x] += 1 | 53 | self.counts[letter.upper()][x] += 1 | ||
66 | def compute_consensus(self): | 54 | def compute_consensus(self): | ||
67 | self.count() | 55 | self.count() | ||
n | 68 | dnalist = 2 | n | ||
69 | consensus = '' | 56 | consensus = "" | ||
70 | for x in range(len(self)): | 57 | for x in range(len(self)): | ||
n | 71 | current_consensus = ('',0) | n | 58 | current_consensus = ("",0) |
72 | for gene in self.counts: | 59 | for letter in self.counts: | ||
73 | if current_consensus[1] < self.counts[gene][x]: | 60 | if self.counts[letter][x] > current_consensus[1]: | ||
74 | current_consensus = (gene, self.counts[gene][x]) | 61 | current_consensus = (letter, self.counts[letter][x]) | ||
75 | consensus += current_consensus[0] | 62 | consensus += current_consensus[0] | ||
76 | self.consensus = consensus | 63 | self.consensus = consensus | ||
t | 77 | cons = consensus | t | 64 | def count(self): |
78 | cons = dnalist | 65 | self.counts = {"A": [0 for _ in range(len(self))], "C": [0 for _ in rang | ||
> | e(len(self))], "G": [0 for _ in range(len(self))], "T": [0 for _ in range(len(se | ||||
> | lf))]} | ||||
66 | for instance in self.instances: | ||||
67 | for x, letter in enumerate(instance): | ||||
68 | if letter != "\n": | ||||
69 | self.counts[letter.upper()][x] += 1 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, initial , gen = {}, x= 0, angle= 0): | n | 4 | def __init__(self, initial , gen = {}, n = 0, deltaTheta = 0): |
5 | self.init = initial | 5 | self.initial = initial | ||
6 | self.general = gen | 6 | self.gen = gen | ||
7 | self.variable = x | 7 | self.n = n | ||
8 | self.deltaTheta = angle | 8 | self.deltaTheta = deltaTheta | ||
9 | self.str = initial | 9 | self.str = initial | ||
n | 10 | while x>0: | n | 10 | while n>0: |
11 | x -= 1 | 11 | n -= 1 | ||
12 | y = '' | 12 | y = '' | ||
13 | for i in self.str: | 13 | for i in self.str: | ||
14 | if i in gen: | 14 | if i in gen: | ||
15 | y += gen[i] | 15 | y += gen[i] | ||
16 | else: | 16 | else: | ||
17 | y += i | 17 | y += i | ||
18 | self.str = y | 18 | self.str = y | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 20 | z = [] | n | 20 | x = [] |
21 | current_point=(200,0) | ||||
22 | theta = 90*(math.pi/180) | ||||
23 | next_point = (current_point[0] + math.cos(theta), current_point[1] + mat | ||||
> | h.sin(theta)) | ||||
24 | current_point=next_point | ||||
25 | plt.plot([current_point[0],next_point[0]],[current_point[1],next_point[1 | ||||
> | ]],color='black') | ||||
26 | for i in self.str: | 21 | for let in self.str: | ||
27 | if i == '[': | 22 | if let == '[': | ||
28 | z.append([current_point,theta]) | 23 | x.append([currentPt,theta]) | ||
29 | elif i == ']': | 24 | elif let == ']': | ||
30 | current_point = z[-1][0] | 25 | currentPt = x[-1][0] | ||
31 | theta = z[-1][1] | 26 | theta = x[-1][1] | ||
32 | elif i == '+': | 27 | elif let == '+': | ||
33 | theta += self.deltaTheta | 28 | theta += self.deltaTheta | ||
n | 34 | elif i == '-': | n | 29 | elif let == '-': |
35 | theta -= self.deltaTheta | 30 | theta -= self.deltaTheta | ||
n | n | 31 | currentPt=(200,0) | ||
32 | theta = 90 | ||||
33 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.sin(theta) | ||||
> | ) | ||||
34 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color='black' | ||||
> | ) | ||||
35 | currentPt=nextPt | ||||
36 | z = [] | 36 | x = [] | ||
37 | for i in self.str: | 37 | for let in self.str: | ||
38 | if i == '[': | 38 | if let == '[': | ||
39 | z.append([current_point,theta]) | 39 | x.append([currentPt,theta]) | ||
40 | elif i == ']': | 40 | elif let == ']': | ||
41 | current_point = z[-1][0] | 41 | currentPt = x[-1][0] | ||
42 | theta = z[-1][1] | 42 | theta = x[-1][1] | ||
43 | elif i == '+': | 43 | elif let == '+': | ||
44 | theta += self.deltaTheta | 44 | theta += self.deltaTheta | ||
n | 45 | elif i == '-': | n | 45 | elif let == '-': |
46 | theta -= self.deltaTheta | 46 | theta -= self.deltaTheta | ||
47 | class DNAMOTIF: | 47 | class DNAMOTIF: | ||
48 | def __init__(self): | 48 | def __init__(self): | ||
49 | self.instances=[] | 49 | self.instances=[] | ||
50 | self.consensus=[] | 50 | self.consensus=[] | ||
51 | self.counts= {"A": [], "C": [], "G":[],"T":[]} | 51 | self.counts= {"A": [], "C": [], "G":[],"T":[]} | ||
52 | def __str__(self): | 52 | def __str__(self): | ||
53 | string = "" | 53 | string = "" | ||
54 | for i in self.instances: | 54 | for i in self.instances: | ||
55 | string += i + "" | 55 | string += i + "" | ||
56 | return string[:-2] | 56 | return string[:-2] | ||
57 | def __len__(self): | 57 | def __len__(self): | ||
58 | return len(self.instances[0])-1 | 58 | return len(self.instances[0])-1 | ||
59 | def count(self): | 59 | def count(self): | ||
60 | i = 0 | 60 | i = 0 | ||
61 | count = 0 | 61 | count = 0 | ||
n | 62 | vertical = [] | n | ||
63 | repeat = [] | 62 | up = [] | ||
63 | up1 = [] | ||||
64 | A = 0 | 64 | A = 0 | ||
65 | C = 0 | 65 | C = 0 | ||
66 | T = 0 | 66 | T = 0 | ||
67 | G = 0 | 67 | G = 0 | ||
68 | while i < len(self.instances[0])-1: | 68 | while i < len(self.instances[0])-1: | ||
69 | while count < len(self.instances): | 69 | while count < len(self.instances): | ||
n | 70 | for stuff in self.instances: | n | 70 | for amino in self.instances: |
71 | stuff = stuff.upper() | 71 | amino = amino.upper() | ||
72 | vertical.append(stuff[i]) | 72 | up.append(amino[i]) | ||
73 | count += 1 | 73 | count += 1 | ||
74 | count = 0 | 74 | count = 0 | ||
n | 75 | repeat.append(vertical) | n | 75 | up1.append(up) |
76 | vertical = [] | 76 | up = [] | ||
77 | i += 1 | 77 | i += 1 | ||
n | 78 | for dna in repeat: | n | 78 | for strand in up1: |
79 | for an in dna: | 79 | for i in strand: | ||
80 | if an == 'A': | 80 | if i == 'A': | ||
81 | A += 1 | 81 | A += 1 | ||
n | 82 | if an == 'C': | n | 82 | if i == 'C': |
83 | C += 1 | 83 | C += 1 | ||
n | 84 | if an == 'T': | n | 84 | if i == 'T': |
85 | T += 1 | 85 | T += 1 | ||
n | 86 | if an == 'G': | n | 86 | if i == 'G': |
87 | G += 1 | 87 | G += 1 | ||
88 | self.counts.setdefault('A',[]).append(A) | 88 | self.counts.setdefault('A',[]).append(A) | ||
89 | self.counts.setdefault('C',[]).append(C) | 89 | self.counts.setdefault('C',[]).append(C) | ||
90 | self.counts.setdefault('T',[]).append(T) | 90 | self.counts.setdefault('T',[]).append(T) | ||
91 | self.counts.setdefault('G',[]).append(G) | 91 | self.counts.setdefault('G',[]).append(G) | ||
92 | A = 0 | 92 | A = 0 | ||
93 | C = 0 | 93 | C = 0 | ||
94 | T = 0 | 94 | T = 0 | ||
95 | G = 0 | 95 | G = 0 | ||
96 | def compute_consensus(self): | 96 | def compute_consensus(self): | ||
97 | self.count() | 97 | self.count() | ||
98 | A = self.counts["A"] | 98 | A = self.counts["A"] | ||
99 | C = self.counts["C"] | 99 | C = self.counts["C"] | ||
100 | G = self.counts["G"] | 100 | G = self.counts["G"] | ||
101 | T = self.counts["T"] | 101 | T = self.counts["T"] | ||
n | 102 | for stuff in range(len(A)): | n | 102 | for let in range(len(A)): |
103 | if(A[stuff]>= C[stuff] and A[stuff] >= G[stuff] and A[stuff] >= T[s | 103 | if(A[let]>= C[let] and A[let] >= G[let] and A[let] >= T[let]): | ||
> | tuff]): | ||||
104 | self.consensus.append("A") | 104 | self.consensus.append("A") | ||
n | 105 | elif (C[stuff] >= G[stuff] and C[stuff] >= T[stuff]): | n | 105 | elif (C[let] >= G[let] and C[let] >= T[let]): |
106 | self.consensus.append("C") | 106 | self.consensus.append("C") | ||
t | 107 | elif (G[stuff] >= T[stuff]): | t | 107 | elif (G[let] >= T[let]): |
108 | self.consensus.append("G") | 108 | self.consensus.append("G") | ||
109 | else: | 109 | else: | ||
110 | self.consensus.append("T") | 110 | self.consensus.append("T") | ||
111 | self.consensus = "".join(self.consensus) | 111 | self.consensus = "".join(self.consensus) | ||
112 | def parse(self, filename): | 112 | def parse(self, filename): | ||
113 | with open(filename,'r') as f: | 113 | with open(filename,'r') as f: | ||
114 | for i in f: | 114 | for i in f: | ||
115 | if ">" in i: | 115 | if ">" in i: | ||
116 | continue | 116 | continue | ||
117 | else: | 117 | else: | ||
118 | self.instances.append(i) | 118 | self.instances.append(i) | ||
119 | lexA=DNAMOTIF() | 119 | lexA=DNAMOTIF() | ||
120 | lexA.parse("lexA.fasta") | 120 | lexA.parse("lexA.fasta") | ||
121 | lexA.count() | 121 | lexA.count() | ||
122 | print(lexA.counts) | 122 | print(lexA.counts) | ||
123 | lexA.compute_consensus() | 123 | lexA.compute_consensus() | ||
124 | print(lexA.consensus) | 124 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | from numpy import cos, sin, pi | 2 | from numpy import cos, sin, pi | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, initState, gen, n, deltaTheta): | n | 4 | def __init__(self, initState, generator, n, delta_Theta): |
5 | self.init_state = initState | 5 | self.init_state = initState | ||
n | 6 | self.gen = gen | n | 6 | self.gen = generator |
7 | self.num = n | 7 | self.num = n | ||
n | 8 | self.deltaTheta = deltaTheta | n | 8 | self.deltaTheta = delta_Theta |
9 | self.str = self.init_state | 9 | self.str = self.init_state | ||
n | 10 | self.s2 = "" | n | 10 | self.str2 = "" |
11 | for i in range(self.num): | 11 | for i in range(self.num): | ||
n | 12 | self.s2 = "" | n | 12 | self.str2 = "" |
13 | for char in self.str: | 13 | for char in self.str: | ||
14 | if char in self.gen: | 14 | if char in self.gen: | ||
n | 15 | self.s2 += self.gen[char] | n | 15 | self.str2 += self.gen[char] |
16 | else: | 16 | else: | ||
n | 17 | self.s2 += char | n | 17 | self.str2 += char |
18 | self.str = self.s2 | 18 | self.str = self.str2 | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 20 | currPt = (200,0) | n | 20 | currentPt = (200,0) |
21 | t = pi/2 | 21 | theta = pi/2 | ||
22 | nextPt = () | 22 | nextPt = () | ||
23 | stack = [] | 23 | stack = [] | ||
24 | for CHR in self.str: | 24 | for CHR in self.str: | ||
25 | if CHR.isalpha(): | 25 | if CHR.isalpha(): | ||
n | 26 | nextPt = (currPt[0]+cos(t), currPt[1]+sin(t)) | n | 26 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) |
27 | plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='blac | 27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | k') | > | ='black') | ||
28 | currPt = nextPt | 28 | currentPt = nextPt | ||
29 | elif CHR == "[": | 29 | elif CHR == "[": | ||
n | 30 | stack.append([currPt, t]) | n | 30 | stack.append([currentPt, theta]) |
31 | elif CHR == "]": | 31 | elif CHR == "]": | ||
n | 32 | currPt, t = stack.pop() | n | 32 | currentPt, theta = stack.pop() |
33 | elif CHR == "+": | 33 | elif CHR == "+": | ||
n | 34 | t += (self.deltaTheta * (pi/180)) | n | 34 | theta += (self.deltaTheta * (pi/180)) |
35 | elif CHR == "-": | 35 | elif CHR == "-": | ||
n | 36 | t -= (self.deltaTheta * (pi/180)) | n | 36 | theta -= (self.deltaTheta * (pi/180)) |
37 | plt.show()class DNAMOTIF: | 37 | plt.show() | ||
38 | class DNAMOTIF: | ||||
38 | def __init__(self): | 39 | def __init__(self): | ||
39 | self.instances=[] | 40 | self.instances=[] | ||
40 | self.consensus=[] | 41 | self.consensus=[] | ||
41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 42 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
42 | def __str__(self): | 43 | def __str__(self): | ||
n | 43 | ms = "" | n | 44 | string = "" |
44 | for item in self.instances: | 45 | for thing in self.instances: | ||
45 | ms += item | 46 | string = string + item | ||
46 | return ms | 47 | return string | ||
47 | def __len__(self): | 48 | def __len__(self): | ||
n | 48 | return len(self.instances[0].rstrip("\n")) | n | 49 | return len(self.instances[0].rstrip('\n')) |
49 | def count(self): | 50 | def count(self): | ||
50 | for index in range(self.__len__()): | 51 | for index in range(self.__len__()): | ||
n | 51 | cA = 0 | n | 52 | numA = 0 |
52 | cC = 0 | 53 | numC = 0 | ||
53 | cG = 0 | 54 | numG = 0 | ||
54 | cT = 0 | 55 | numT = 0 | ||
55 | for item in self.instances: | 56 | for thing in self.instances: | ||
56 | if item[index].upper() == "A": | 57 | if thing[index].upper() == "A": | ||
57 | cA += 1 | 58 | numA += 1 | ||
58 | elif item[index].upper() == "C": | 59 | elif thing[index].upper() == "C": | ||
59 | cC += 1 | 60 | numC += 1 | ||
60 | elif item[index].upper() == "G": | 61 | elif thing[index].upper() == "G": | ||
61 | cG += 1 | 62 | numG += 1 | ||
62 | elif item[index].upper() == "T": | 63 | elif thing[index].upper() == "T": | ||
63 | cT += 1 | 64 | numT += 1 | ||
64 | self.counts["A"].append(cA) | 65 | self.counts["A"].append(numA) | ||
65 | self.counts["C"].append(cC) | 66 | self.counts["C"].append(numC) | ||
66 | self.counts["G"].append(cG) | 67 | self.counts["G"].append(numG) | ||
67 | self.counts["T"].append(cT) | 68 | self.counts["T"].append(numT) | ||
68 | def compute_consensus(self): | 69 | def compute_consensus(self): | ||
69 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 70 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
70 | self.count() | 71 | self.count() | ||
71 | self.consensus = "" | 72 | self.consensus = "" | ||
n | 72 | for i in range(self.__len__()): | n | 73 | for letter in range(self.__len__()): |
73 | mx = max([self.counts["A"][i], self.counts["C"][i], self.counts["G"] | 74 | maximum = max([self.counts["A"][letter], self.counts["C"][letter], s | ||
> | [i], self.counts["T"][i]]) | > | elf.counts["G"][letter], self.counts["T"][letter]]) | ||
74 | if mx == self.counts["A"][i]: | 75 | if maximum == self.counts["A"][letter]: | ||
75 | self.consensus += "A" | 76 | self.consensus += "A" | ||
n | 76 | elif mx == self.counts["C"][i]: | n | 77 | elif maximum == self.counts["C"][letter]: |
77 | self.consensus += "C" | 78 | self.consensus += "C" | ||
n | 78 | elif mx == self.counts["G"][i]: | n | 79 | elif maximum == self.counts["G"][letter]: |
79 | self.consensus += "G" | 80 | self.consensus += "G" | ||
n | 80 | elif mx == self.counts["T"][i]: | n | 81 | elif maximum == self.counts["T"][letter]: |
81 | self.consensus += "T" | 82 | self.consensus += "T" | ||
82 | return self.consensus | 83 | return self.consensus | ||
t | 83 | def parse(self, fn): | t | 84 | def parse(self, filename): |
84 | mf = open(fn) | 85 | thefile = open(filename) | ||
85 | l = mf.readlines() | 86 | lines = thefile.readlines() | ||
86 | mf.close() | 87 | thefile.close() | ||
87 | for line in l: | 88 | for line in lines: | ||
88 | if line[0] != ">": | 89 | if line[0] != ">": | ||
89 | self.instances.append(line) | 90 | self.instances.append(line) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | import math | n | ||
3 | import numpy as np | ||||
4 | class Plant: | ||||
5 | plt.style.use('bmh') | 2 | plt.style.use('bmh') | ||
6 | plt.plot( | 3 | plt.plot( | ||
7 | [0, 1, 2], | 4 | [0, 1, 2], | ||
8 | [0, 1, 0] | 5 | [0, 1, 0] | ||
9 | ) | 6 | ) | ||
10 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
11 | plt.ylabel('y'); | 8 | plt.ylabel('y'); | ||
12 | def plot_coords(coords, bare_plot=False): | 9 | def plot_coords(coords, bare_plot=False): | ||
13 | if bare_plot: | 10 | if bare_plot: | ||
14 | plt.axis('off') | 11 | plt.axis('off') | ||
15 | plt.axes().set_aspect('equal', 'datalim') | 12 | plt.axes().set_aspect('equal', 'datalim') | ||
16 | X, Y = zip(*coords) | 13 | X, Y = zip(*coords) | ||
17 | plt.plot(X, Y); | 14 | plt.plot(X, Y); | ||
18 | plot_coords([ | 15 | plot_coords([ | ||
19 | (0, 0), | 16 | (0, 0), | ||
20 | (1, 0), | 17 | (1, 0), | ||
21 | (2, 1), | 18 | (2, 1), | ||
22 | (3, 1), | 19 | (3, 1), | ||
23 | (2, 0) | 20 | (2, 0) | ||
24 | ]) | 21 | ]) | ||
25 | nan = float('nan') | 22 | nan = float('nan') | ||
26 | plot_coords([ | 23 | plot_coords([ | ||
27 | (0, 0), | 24 | (0, 0), | ||
28 | (1, 1), | 25 | (1, 1), | ||
29 | (nan, nan), | 26 | (nan, nan), | ||
30 | (1, 0), | 27 | (1, 0), | ||
31 | (2, 1) | 28 | (2, 1) | ||
32 | ]) | 29 | ]) | ||
33 | from math import pi, sin, cos | 30 | from math import pi, sin, cos | ||
34 | DEGREES_TO_RADIANS = pi / 180 | 31 | DEGREES_TO_RADIANS = pi / 180 | ||
35 | def turtle_to_coords(turtle_program, turn_amount=45): | 32 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
36 | state = (0.0, 0.0, 90.0) | 33 | state = (0.0, 0.0, 90.0) | ||
37 | yield (0.0, 0.0) | 34 | yield (0.0, 0.0) | ||
38 | for command in turtle_program: | 35 | for command in turtle_program: | ||
39 | x, y, angle = state | 36 | x, y, angle = state | ||
40 | if command in 'Ff': | 37 | if command in 'Ff': | ||
41 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
42 | y + sin(angle * DEGREES_TO_RADIANS), | 39 | y + sin(angle * DEGREES_TO_RADIANS), | ||
43 | angle) | 40 | angle) | ||
44 | if command == 'f': | 41 | if command == 'f': | ||
45 | yield (float('nan'), float('nan')) | 42 | yield (float('nan'), float('nan')) | ||
46 | yield (state[0], state[1]) | 43 | yield (state[0], state[1]) | ||
47 | elif command == '+': | 44 | elif command == '+': | ||
48 | state = (x, y, angle + turn_amount) | 45 | state = (x, y, angle + turn_amount) | ||
49 | elif command == '-': | 46 | elif command == '-': | ||
50 | state = (x, y, angle - turn_amount) | 47 | state = (x, y, angle - turn_amount) | ||
51 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | 48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
52 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
53 | from math import isnan | 50 | from math import isnan | ||
54 | def print_coords(coords): | 51 | def print_coords(coords): | ||
55 | for (x, y) in coords: | 52 | for (x, y) in coords: | ||
56 | if isnan(x): | 53 | if isnan(x): | ||
57 | print('<gap>') | 54 | print('<gap>') | ||
58 | else: | 55 | else: | ||
59 | print('({:.2f}, {:.2f})'.format(x, y)) | 56 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
60 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
61 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | 58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
62 | def transform_sequence(sequence, transformations): | 59 | def transform_sequence(sequence, transformations): | ||
63 | return ''.join(transformations.get(c, c) for c in sequence) | 60 | return ''.join(transformations.get(c, c) for c in sequence) | ||
64 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | 61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | ||
65 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | 62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
66 | def transform_multiple(sequence, transformations, iterations): | 63 | def transform_multiple(sequence, transformations, iterations): | ||
67 | for _ in range(iterations): | 64 | for _ in range(iterations): | ||
68 | sequence = transform_sequence(sequence, transformations) | 65 | sequence = transform_sequence(sequence, transformations) | ||
69 | return sequence | 66 | return sequence | ||
70 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | 67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | ||
71 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
72 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
73 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | 70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
74 | plot_coords(turtle_to_coords(transform_multiple('L', { | 71 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
75 | 'L': '-RF+LFL+FR-', | 72 | 'L': '-RF+LFL+FR-', | ||
76 | 'R': '+LF-RFR-FL+' | 73 | 'R': '+LF-RFR-FL+' | ||
77 | }, 5), 90)) | 74 | }, 5), 90)) | ||
78 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | 75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
79 | saved_states = list() | 76 | saved_states = list() | ||
80 | state = (0, 0, 90) | 77 | state = (0, 0, 90) | ||
81 | yield (0, 0) | 78 | yield (0, 0) | ||
82 | for command in turtle_program: | 79 | for command in turtle_program: | ||
83 | x, y, angle = state | 80 | x, y, angle = state | ||
84 | if command.lower() in 'abcdefghij': | 81 | if command.lower() in 'abcdefghij': | ||
85 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
86 | y + sin(angle * DEGREES_TO_RADIANS), | 83 | y + sin(angle * DEGREES_TO_RADIANS), | ||
87 | angle) | 84 | angle) | ||
88 | if command.islower(): | 85 | if command.islower(): | ||
89 | yield (float('nan'), float('nan')) | 86 | yield (float('nan'), float('nan')) | ||
90 | yield (state[0], state[1]) | 87 | yield (state[0], state[1]) | ||
91 | elif command == '+': | 88 | elif command == '+': | ||
92 | state = (x, y, angle + turn_amount) | 89 | state = (x, y, angle + turn_amount) | ||
93 | elif command == '-': | 90 | elif command == '-': | ||
94 | state = (x, y, angle - turn_amount) | 91 | state = (x, y, angle - turn_amount) | ||
95 | elif command == '[': | 92 | elif command == '[': | ||
96 | saved_states.append(state) | 93 | saved_states.append(state) | ||
97 | elif command == ']': | 94 | elif command == ']': | ||
98 | state = saved_states.pop() | 95 | state = saved_states.pop() | ||
99 | yield (float('nan'), float('nan')) | 96 | yield (float('nan'), float('nan')) | ||
100 | x, y, _ = state | 97 | x, y, _ = state | ||
101 | yield (x, y) | 98 | yield (x, y) | ||
102 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | 99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
103 | def l_plot(axiom, transformations, iterations=0, angle=45): | 100 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
104 | turtle_program = transform_multiple(axiom, transformations, iterations) | 101 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
105 | coords = branching_turtle_to_coords(turtle_program, angle) | 102 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
106 | plot_coords(coords, bare_plot=True) | 103 | plot_coords(coords, bare_plot=True) | ||
107 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | 104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | ||
108 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
109 | for i in range(5): | 106 | for i in range(5): | ||
110 | print('{}: '.format(i), | 107 | print('{}: '.format(i), | ||
111 | transform_multiple('A', {'A': 'F+A'}, i)) | 108 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
112 | for i in range(5): | 109 | for i in range(5): | ||
113 | print('{}: '.format(i), | 110 | print('{}: '.format(i), | ||
114 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | 111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
115 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | ||
116 | def __init__(self): | 113 | def __init__(self): | ||
n | 117 | self.instances=[] | n | 114 | self.instances = [] |
118 | self.consensus=[] | 115 | self.consensus=[] | ||
119 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | ||
120 | def __str__(self): | 117 | def __str__ (self): | ||
121 | result = "" | 118 | string = "" | ||
122 | for instance in self.instances: | 119 | for i in self.instances: | ||
123 | result += instance | 120 | string += i + "\n" | ||
124 | return result | 121 | return string[:-2] | ||
125 | def __len__(self): | 122 | def __len__(self): | ||
n | 126 | return len(self.instances[0].rstrip()) | n | 123 | return len(self.instances) |
127 | def count(self): | 124 | def count(self): | ||
n | 128 | for position in range(len(self)): | n | ||
129 | TypeA = 0 | ||||
130 | TypeT = 0 | ||||
131 | TypeC = 0 | ||||
132 | TypeG = 0 | ||||
133 | for instance in self.instances: | 125 | for i in self.instances: | ||
134 | order = instance.rstrip() | 126 | temp = i.upper() | ||
135 | if (order[position]).upper() == "A": | ||||
136 | TypeA += 1 | ||||
137 | elif (order[position]).upper() == "T": | ||||
138 | TypeT += 1 | ||||
139 | elif (order[position]).upper() == "C": | ||||
140 | TypeC += 1 | ||||
141 | elif (order[position]).upper() == "G": | ||||
142 | TypeG += 1 | ||||
143 | self.counts.get("A").append(TypeA) | 127 | self.counts["A"].append(temp.count("A")) | ||
144 | self.counts.get("T").append(TypeT) | ||||
145 | self.counts.get("C").append(TypeC) | 128 | self.counts["C"].append(temp.count("C")) | ||
146 | self.counts.get("G").append(TypeG) | 129 | self.counts["G"].append(temp.count("G")) | ||
130 | self.counts["T"].append(temp.count("T")) | ||||
147 | def compute_consensus(self): | 131 | def compute_consensus(self): | ||
n | 148 | DNAMOTIF.count(self) | n | ||
149 | A = self.counts.get("A") | 132 | A = self.counts["A"] | ||
150 | T = self.counts.get("T") | ||||
151 | C = self.counts.get("C") | 133 | C = self.counts["C"] | ||
152 | G = self.counts.get("G") | 134 | G = self.counts["G"] | ||
153 | output = "" | 135 | T = self.counts["T"] | ||
154 | for row in range(len(A)): | 136 | for i in range(len(A)): | ||
155 | maxs = max(A[row],T[row],C[row],G[row]) | 137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
156 | if maxs == A[row]: | 138 | self.consensus.append("A") | ||
157 | output += "A" | 139 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
158 | elif maxs == T[row]: | 140 | self.consensus.append("C") | ||
159 | output += "T" | 141 | elif (G[i] >= T[i]): | ||
160 | elif maxs == C[row]: | 142 | self.consensus.append("G") | ||
161 | output += "C" | 143 | else: | ||
162 | elif maxs == G[row]: | 144 | self.consensus.append("T") | ||
163 | output += "G" | ||||
164 | self.consensus = output | ||||
165 | def parse(self, filename): | 145 | def parse(self, filename): | ||
t | 166 | myFile = open(filename, 'r') | t | 146 | with open(filename,'r') as f: |
167 | contents = [line for line in myFile.readlines()] | 147 | for i in f: | ||
168 | myFile.close() | 148 | if ">" in i: | ||
169 | for index, line in enumerate(contents): | 149 | continue | ||
170 | if index % 2 != 0: | 150 | else: | ||
171 | self.instances.append(line) | 151 | self.instances.append(i) | ||
172 | if __name__ == '__main__': | 152 | if __name__=='__main__': | ||
173 | lexA=DNAMOTIF() | 153 | lex=DNAMOTIF() | ||
174 | filename = r'FinalProject\lexA.fasta' | 154 | lex.parse('lexA.fasta') | ||
175 | lexA.parse(filename) | 155 | print("length",len(lex)) | ||
176 | lexA.compute_consensus() | 156 | print(lex) | ||
177 | print(lexA.consensus) | 157 | print("consensus",lex.consensus) | ||
158 | print('seq',lex.counts) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | import matplotlib.pyplot as plt | ||
1 | plt.style.use('bmh') | 2 | plt.style.use('bmh') | ||
2 | plt.plot( | 3 | plt.plot( | ||
n | 3 | [0,1,2] | n | 4 | [0, 1, 2], |
4 | [0,1,0] | 5 | [0, 1, 0] | ||
5 | ) | 6 | ) | ||
6 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
7 | plt.ylabel('y'); | 8 | plt.ylabel('y'); | ||
n | 8 | def plot_coords(coords,bare_plot=False): | n | 9 | def plot_coords(coords, bare_plot=False): |
9 | if bae_plot: | 10 | if bare_plot: | ||
10 | plt.axis('off') | 11 | plt.axis('off') | ||
n | 11 | plt.axes().set_apsect('equal','datalim') | n | 12 | plt.axes().set_aspect('equal', 'datalim') |
12 | X,Y = zip(coords) | 13 | X, Y = zip(*coords) | ||
13 | plt.plot(X,Y); | 14 | plt.plot(X, Y); | ||
14 | plot_coords([ | 15 | plot_coords([ | ||
n | 15 | (0,0), | n | 16 | (0, 0), |
16 | (1,0), | 17 | (1, 0), | ||
17 | (2,1), | 18 | (2, 1), | ||
18 | (3,1), | 19 | (3, 1), | ||
19 | (2,0) | 20 | (2, 0) | ||
20 | ]) | 21 | ]) | ||
21 | nan = float('nan') | 22 | nan = float('nan') | ||
22 | plot_coords([ | 23 | plot_coords([ | ||
n | 23 | (0,0), | n | 24 | (0, 0), |
24 | (1,1), | 25 | (1, 1), | ||
25 | (nan,nan), | 26 | (nan, nan), | ||
26 | (1,0), | 27 | (1, 0), | ||
27 | (2,1) | 28 | (2, 1) | ||
28 | ]) | 29 | ]) | ||
29 | from math import pi, sin, cos | 30 | from math import pi, sin, cos | ||
n | 30 | deg_to_rad = pi / 180 | n | 31 | DEGREES_TO_RADIANS = pi / 180 |
31 | def turtle_to_coords(turtle_program,turn_amount=45): | 32 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
32 | state = (0.0,0.0,90.0) | 33 | state = (0.0, 0.0, 90.0) | ||
33 | yield (0.0,0.0) | 34 | yield (0.0, 0.0) | ||
34 | for command in turtle_program: | 35 | for command in turtle_program: | ||
n | 35 | x,y, angle = state | n | 36 | x, y, angle = state |
36 | if command in 'Ff': | 37 | if command in 'Ff': | ||
37 | state = (x - cos(angle * deg_to_rad), | 38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
38 | y + sin(angle * deg_to_rad), | 39 | y + sin(angle * DEGREES_TO_RADIANS), | ||
39 | angle) | 40 | angle) | ||
40 | if command == 'f': | 41 | if command == 'f': | ||
n | 41 | yield(float('nan'),float('nan')) | n | 42 | yield (float('nan'), float('nan')) |
42 | yield(state[0],state[1]) | 43 | yield (state[0], state[1]) | ||
43 | elif command =='+': | 44 | elif command == '+': | ||
44 | state = (x,y,angle + turn_amount) | 45 | state = (x, y, angle + turn_amount) | ||
45 | elif command == '-': | 46 | elif command == '-': | ||
46 | state = (x,y,angle - turn_amount) | 47 | state = (x, y, angle - turn_amount) | ||
47 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | 48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
n | 48 | plot_coords(turtle_to_coords('F-F+F+F+F+f+F+F+F-F')) | n | 49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) |
49 | from math import isnan | 50 | from math import isnan | ||
50 | def print_coords(coords): | 51 | def print_coords(coords): | ||
n | 51 | for (x,y) in coords: | n | 52 | for (x, y) in coords: |
52 | if isnan(x): | 53 | if isnan(x): | ||
53 | print('<gap>') | 54 | print('<gap>') | ||
54 | else: | 55 | else: | ||
n | 55 | print('({:.2f},{:.2f}'.format(x,y)) | n | 56 | print('({:.2f}, {:.2f})'.format(x, y)) |
56 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
n | 57 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F',65)) | n | 58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) |
58 | def transform_sequence(sequence,transformations): | 59 | def transform_sequence(sequence, transformations): | ||
59 | return ''.join(transformations.get(c, c) for c in sequence) | 60 | return ''.join(transformations.get(c, c) for c in sequence) | ||
n | 60 | transform_sequence('acab',{'a':'aba','c':'bb'}) | n | 61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) |
61 | plot_coords(turtle_to_coords(transform_sequece('FFFF',{'F':'FfF++'}))) | 62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
62 | def transfomr_multiple(sequence,transformations,iterations): | 63 | def transform_multiple(sequence, transformations, iterations): | ||
63 | for _ in range(iterations): | 64 | for _ in range(iterations): | ||
n | 64 | sequence = transform_seqence(sequence,transformations) | n | 65 | sequence = transform_sequence(sequence, transformations) |
65 | return sequence | 66 | return sequence | ||
n | 66 | print('0:',transform_multiple('abba',{'b':'bab'},0)) | n | 67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) |
67 | print('1:',transform_multiple('abba',{'b':'bab'},1)) | 68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
68 | print('2:',transform_multiple('abba',{'b':'bab'},2)) | 69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
69 | plot_coords(turtle_to_coords(transform_multiple('F',{'F':'+F+F--F+F'},5))) | 70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
70 | plot_coords(turtle_to_coords(transform_multiple('L',{ | 71 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
71 | 'L': '-RF+LFL+FR-', | 72 | 'L': '-RF+LFL+FR-', | ||
72 | 'R': '+LF-RFR-FL+' | 73 | 'R': '+LF-RFR-FL+' | ||
n | 73 | },5),90)) | n | 74 | }, 5), 90)) |
74 | def branching_turtle_to_coords(turtle_program,turn_amount=45): | 75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
75 | saved_states = list() | 76 | saved_states = list() | ||
n | 76 | state = (0,0,90) | n | 77 | state = (0, 0, 90) |
77 | yield(0,0) | 78 | yield (0, 0) | ||
78 | for command in turtle_program: | 79 | for command in turtle_program: | ||
n | 79 | x,y,angle = state | n | 80 | x, y, angle = state |
80 | if command.lower() in 'abcdefghuj': | 81 | if command.lower() in 'abcdefghij': | ||
81 | state = (c-cose(angle * deg_to_rad), | 82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
82 | y + sin(angle * deg_to_rad), | 83 | y + sin(angle * DEGREES_TO_RADIANS), | ||
83 | angle) | 84 | angle) | ||
84 | if command.islower(): | 85 | if command.islower(): | ||
85 | yield(float('nan'),float('nan')) | 86 | yield (float('nan'), float('nan')) | ||
86 | yield(state[0],state[1]) | 87 | yield (state[0], state[1]) | ||
87 | elif command == '+': | 88 | elif command == '+': | ||
88 | state = (x,y,angle + turn_amount) | 89 | state = (x, y, angle + turn_amount) | ||
89 | elif command =='-': | 90 | elif command == '-': | ||
90 | state = (x,y,angle - turn_amount) | 91 | state = (x, y, angle - turn_amount) | ||
91 | elif command == '[': | 92 | elif command == '[': | ||
92 | saved_states.append(state) | 93 | saved_states.append(state) | ||
n | 93 | elif command == ']': | n | 94 | elif command == ']': |
94 | state = saved_states.pop() | 95 | state = saved_states.pop() | ||
n | 95 | yield(float('nan'),float('nan')) | n | 96 | yield (float('nan'), float('nan')) |
96 | x,y,_ = state | 97 | x, y, _ = state | ||
98 | yield (x, y) | ||||
97 | plot_coords(branching_turtle_to_coords('F[-F]+F',45)) | 99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
98 | def l_plot(axiom,transformations,iterations=0,angle=45): | 100 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
99 | turtle_program = transform_multiple(axiom,transformations,iterations) | 101 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
100 | coords = branching_turtle_to_coords(turtle_program,angle) | 102 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
101 | plot_coords(coords,bare_plot=True) | 103 | plot_coords(coords, bare_plot=True) | ||
102 | l_plot('F',{'F[-F][+F]'},4,30) | 104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | ||
103 | l_plot('F',{'F':'FF[++F][-FF]'},5,22) | 105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
104 | for i in range(5): | 106 | for i in range(5): | ||
n | 105 | print('{}:'.format(i), | n | 107 | print('{}: '.format(i), |
106 | transform_multiple('A',{'A':'F+A'},i)) | 108 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
107 | for i in range(5): | 109 | for i in range(5): | ||
n | 108 | print('{}:'.format(i), | n | 110 | print('{}: '.format(i), |
109 | transform_multiple('A',{'A':'F+A','F':'FF'},i)) | 111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
110 | l_plot('A',{'F':'FF','A':'F[+AF-[A]--A][---A]'},5,22.5)class DNAMOTIF: | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | ||
111 | def __init__(self): | 113 | def __init__(self): | ||
n | 112 | self.instances=[] | n | 114 | self.instances = [] |
113 | self.consensus=[] | 115 | self.consensus=[] | ||
n | 114 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | n | 116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} |
115 | def __str__(self): | 117 | def __str__ (self): | ||
116 | string = "" | 118 | string = "" | ||
117 | for i in self.instances: | 119 | for i in self.instances: | ||
n | 118 | string += i + "" | n | 120 | string += i + "\n" |
119 | return string[:-2] | 121 | return string[:0] | ||
120 | def __len__(self): | 122 | def __len__(self): | ||
n | 121 | return len(self.instances) | n | 123 | return len (self.consensus) |
122 | def count(self): | 124 | def count(self): | ||
123 | for i in self.instances: | 125 | for i in self.instances: | ||
124 | temp = i.upper() | 126 | temp = i.upper() | ||
125 | self.counts["A"].append(temp.count("A")) | 127 | self.counts["A"].append(temp.count("A")) | ||
126 | self.counts["C"].append(temp.count("C")) | 128 | self.counts["C"].append(temp.count("C")) | ||
127 | self.counts["G"].append(temp.count("G")) | 129 | self.counts["G"].append(temp.count("G")) | ||
128 | self.counts["T"].append(temp.count("T")) | 130 | self.counts["T"].append(temp.count("T")) | ||
129 | def compute_consensus(self): | 131 | def compute_consensus(self): | ||
130 | A = self.counts["A"] | 132 | A = self.counts["A"] | ||
131 | C = self.counts["C"] | 133 | C = self.counts["C"] | ||
132 | G = self.counts["G"] | 134 | G = self.counts["G"] | ||
n | 133 | T = self.counts["T"] | n | 135 | T = self.counts["T"] |
134 | for i in range(len(A)): | 136 | for i in range (len(A)): | ||
135 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
136 | self.consensus.append("A") | 138 | self.consensus.append("A") | ||
137 | elif (C[i] >= G[i] and C[i] >= T[i]): | 139 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
138 | self.consensus.append("C") | 140 | self.consensus.append("C") | ||
139 | elif (G[i] >= T[i]): | 141 | elif (G[i] >= T[i]): | ||
140 | self.consensus.append("G") | 142 | self.consensus.append("G") | ||
141 | else: | 143 | else: | ||
142 | self.consensus.append("T") | 144 | self.consensus.append("T") | ||
143 | def parse(self, filename): | 145 | def parse(self, filename): | ||
144 | with open(filename,'r') as f: | 146 | with open(filename,'r') as f: | ||
145 | for i in f: | 147 | for i in f: | ||
146 | if ">" in i: | 148 | if ">" in i: | ||
147 | continue | 149 | continue | ||
148 | else: | 150 | else: | ||
149 | self.instances.append(i) | 151 | self.instances.append(i) | ||
t | 150 | lexA = DNAMOTIF() | t | ||
151 | lexA.parse("lexA.fasta") | ||||
152 | print(len(lexA)) | ||||
153 | 19 | ||||
154 | lexA = DNAMOTIF() | ||||
155 | lexA.parse("lexA.fasta") | ||||
156 | print(lexA) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | plt.style.use('bmh') | n | 2 | plt.style.use('bmh') |
3 | class PLANT: | ||||
3 | plt.plot( | 4 | plt.plot( | ||
4 | [0, 1, 2], | 5 | [0, 1, 2], | ||
5 | [0, 1, 0] | 6 | [0, 1, 0] | ||
6 | ) | 7 | ) | ||
7 | plt.xlabel('x') | 8 | plt.xlabel('x') | ||
8 | plt.ylabel('y'); | 9 | plt.ylabel('y'); | ||
9 | def plot_coords(coords, bare_plot=False): | 10 | def plot_coords(coords, bare_plot=False): | ||
10 | if bare_plot: | 11 | if bare_plot: | ||
11 | plt.axis('off') | 12 | plt.axis('off') | ||
12 | plt.axes().set_aspect('equal', 'datalim') | 13 | plt.axes().set_aspect('equal', 'datalim') | ||
n | 13 | X, Y = zip(*coords) | n | 14 | X, Y = zip(*coords) |
14 | plt.plot(X, Y); | 15 | plt.plot(X, Y); | ||
15 | plot_coords([(0, 0), (1, 0), (2, 1), (3, 1), (2, 0)]) | 16 | plot_coords([ | ||
17 | (0, 0), | ||||
18 | (1, 0), | ||||
19 | (2, 1), | ||||
20 | (3, 1), | ||||
21 | (2, 0) | ||||
22 | ]) | ||||
16 | nan = float('nan') | 23 | nan = float('nan') | ||
17 | plot_coords([(0, 0),(1, 1),(nan, nan),(1, 0),(2, 1)]) | 24 | plot_coords([ | ||
25 | (0, 0), | ||||
26 | (1, 1), | ||||
27 | (nan, nan), | ||||
28 | (1, 0), | ||||
29 | (2, 1) | ||||
30 | ]) | ||||
18 | from math import pi, sin, cos | 31 | from math import pi, sin, cos | ||
19 | DEGREES_TO_RADIANS = pi / 180 | 32 | DEGREES_TO_RADIANS = pi / 180 | ||
20 | def turtle_to_coords(turtle_program, turn_amount=45): | 33 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
21 | state = (0.0, 0.0, 90.0) | 34 | state = (0.0, 0.0, 90.0) | ||
22 | yield (0.0, 0.0) | 35 | yield (0.0, 0.0) | ||
23 | for command in turtle_program: | 36 | for command in turtle_program: | ||
24 | x, y, angle = state | 37 | x, y, angle = state | ||
n | 25 | if command in 'Ff': | n | 38 | if command in 'Ff': |
26 | state = (x - cos(angle * DEGREES_TO_RADIANS), y + sin(angle * DEGREES_TO | 39 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
> | _RADIANS),angle) | ||||
40 | y + sin(angle * DEGREES_TO_RADIANS), | ||||
41 | angle) | ||||
27 | if command == 'f': | 42 | if command == 'f': | ||
28 | yield (float('nan'), float('nan')) | 43 | yield (float('nan'), float('nan')) | ||
29 | yield (state[0], state[1]) | 44 | yield (state[0], state[1]) | ||
n | 30 | elif command == '+': | n | 45 | elif command == '+': |
31 | state = (x, y, angle + turn_amount) | 46 | state = (x, y, angle + turn_amount) | ||
32 | elif command == '-': | 47 | elif command == '-': | ||
33 | state = (x, y, angle - turn_amount) | 48 | state = (x, y, angle - turn_amount) | ||
34 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | 49 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
35 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 50 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
36 | from math import isnan | 51 | from math import isnan | ||
37 | def print_coords(coords): | 52 | def print_coords(coords): | ||
38 | for (x, y) in coords: | 53 | for (x, y) in coords: | ||
39 | if isnan(x): | 54 | if isnan(x): | ||
40 | print('<gap>') | 55 | print('<gap>') | ||
n | 41 | else: | n | 56 | else: |
42 | print('({:.2f}, {:.2f})'.format(x, y)) | 57 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
43 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 58 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
44 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | 59 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
45 | def transform_sequence(sequence, transformations): | 60 | def transform_sequence(sequence, transformations): | ||
46 | return ''.join(transformations.get(c, c) for c in sequence) | 61 | return ''.join(transformations.get(c, c) for c in sequence) | ||
n | 47 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | n | 62 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) |
48 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | 63 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
49 | def transform_multiple(sequence, transformations, iterations): | 64 | def transform_multiple(sequence, transformations, iterations): | ||
50 | for _ in range(iterations): | 65 | for _ in range(iterations): | ||
51 | sequence = transform_sequence(sequence, transformations) | 66 | sequence = transform_sequence(sequence, transformations) | ||
52 | return sequence | 67 | return sequence | ||
n | 53 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | n | 68 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) |
54 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 69 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
55 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 70 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
56 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, | 71 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
> | 5))) | ||||
57 | plot_coords(turtle_to_coords(transform_multiple('L', {'L': '-RF+LFL+FR-' | 72 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
> | ,'R': '+LF-RFR-FL+'}, 5), 90)) | ||||
73 | 'L': '-RF+LFL+FR-', | ||||
74 | 'R': '+LF-RFR-FL+' | ||||
75 | }, 5), 90)) | ||||
58 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | 76 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
59 | saved_states = list() | 77 | saved_states = list() | ||
60 | state = (0, 0, 90) | 78 | state = (0, 0, 90) | ||
61 | yield (0, 0) | 79 | yield (0, 0) | ||
62 | for command in turtle_program: | 80 | for command in turtle_program: | ||
63 | x, y, angle = state | 81 | x, y, angle = state | ||
n | 64 | if command.lower() in 'abcdefghij': | n | 82 | if command.lower() in 'abcdefghij': |
65 | state = (x - cos(angle * DEGREES_TO_RADIANS), y + sin(angle * DEGREE | 83 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
> | S_TO_RADIANS), angle) | ||||
66 | if command.islower(): | 84 | y + sin(angle * DEGREES_TO_RADIANS), | ||
85 | angle) | ||||
86 | if command.islower(): | ||||
67 | yield (float('nan'), float('nan')) | 87 | yield (float('nan'), float('nan')) | ||
68 | yield (state[0], state[1]) | 88 | yield (state[0], state[1]) | ||
69 | elif command == '+': | 89 | elif command == '+': | ||
70 | state = (x, y, angle + turn_amount) | 90 | state = (x, y, angle + turn_amount) | ||
71 | elif command == '-': | 91 | elif command == '-': | ||
72 | state = (x, y, angle - turn_amount) | 92 | state = (x, y, angle - turn_amount) | ||
73 | elif command == '[': | 93 | elif command == '[': | ||
74 | saved_states.append(state) | 94 | saved_states.append(state) | ||
75 | elif command == ']': | 95 | elif command == ']': | ||
76 | state = saved_states.pop() | 96 | state = saved_states.pop() | ||
77 | yield (float('nan'), float('nan')) | 97 | yield (float('nan'), float('nan')) | ||
78 | x, y, _ = state | 98 | x, y, _ = state | ||
79 | yield (x, y) | 99 | yield (x, y) | ||
80 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | 100 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
81 | def l_plot(axiom, transformations, iterations=0, angle=45): | 101 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
82 | turtle_program = transform_multiple(axiom, transformations, iterations) | 102 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
83 | coords = branching_turtle_to_coords(turtle_program, angle) | 103 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
84 | plot_coords(coords, bare_plot=True) | 104 | plot_coords(coords, bare_plot=True) | ||
n | 85 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | n | 105 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) |
86 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 106 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
87 | for i in range(5): | 107 | for i in range(5): | ||
88 | print('{}: '.format(i), transform_multiple('A', {'A': 'F+A'}, i)) | 108 | print('{}: '.format(i), | ||
109 | transform_multiple('A', {'A': 'F+A'}, i)) | ||||
89 | for i in range(5): | 110 | for i in range(5): | ||
111 | print('{}: '.format(i), | ||||
90 | print('{}: '.format(i),transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, | 112 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
> | i)) | ||||
91 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMO | 113 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)from re import X | ||
> | TIF: | ||||
114 | class DNAMOTIF: | ||||
92 | def __init__(self): | 115 | def __init__(self): | ||
93 | self.instances=[] | 116 | self.instances=[] | ||
94 | self.consensus=[] | 117 | self.consensus=[] | ||
95 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 118 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
96 | def __str__(self): | 119 | def __str__(self): | ||
n | 97 | print(instances) | n | 120 | x=len(self.instances) |
98 | return | 121 | return x | ||
99 | pass | ||||
100 | def __len__(self): | 122 | def __len__(self): | ||
n | 101 | print(len(str(dna_str))) | n | 123 | x=len(self.instances) |
102 | return | 124 | return x | ||
103 | def count(self): | 125 | def count(self): | ||
n | 104 | for x in self.counts: | n | ||
105 | if dna_str == A: | ||||
106 | print('A') | ||||
107 | elif dna_str == G: | ||||
108 | print('G') | ||||
109 | elif dna_str == C: | ||||
110 | print('C') | ||||
111 | elif dna_str == T: | ||||
112 | print('T') | ||||
113 | pass | 126 | pass | ||
114 | def compute_consensus(self): | 127 | def compute_consensus(self): | ||
n | 115 | print(filename.consensus) | n | ||
116 | pass | 128 | pass | ||
117 | def parse(self, filename): | 129 | def parse(self, filename): | ||
t | 118 | p = filename.parse(filename.FASTA) | t | 130 | pass |
119 | print(p) | ||||
120 | self.instances = instances | ||||
121 | self.consensus = consensus | ||||
122 | self.counts = counts | ||||
123 | instances = str(filename) | ||||
124 | A = str(seq).count('A') | ||||
125 | T = str(seq).count('T') | ||||
126 | C = str(seq).count('C') | ||||
127 | G = str(seq).count('G') |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | plt.style.use('bmh') | n | 2 | plt.style.use('bmh') |
3 | class PLANT: | ||||
4 | plt.plot( | 3 | plt.plot( | ||
5 | [0, 1, 2], | 4 | [0, 1, 2], | ||
6 | [0, 1, 0] | 5 | [0, 1, 0] | ||
7 | ) | 6 | ) | ||
8 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
9 | plt.ylabel('y'); | 8 | plt.ylabel('y'); | ||
10 | def plot_coords(coords, bare_plot=False): | 9 | def plot_coords(coords, bare_plot=False): | ||
11 | if bare_plot: | 10 | if bare_plot: | ||
12 | plt.axis('off') | 11 | plt.axis('off') | ||
13 | plt.axes().set_aspect('equal', 'datalim') | 12 | plt.axes().set_aspect('equal', 'datalim') | ||
n | 14 | X, Y = zip(*coords) | n | 13 | X, Y = zip(*coords) |
15 | plt.plot(X, Y); | 14 | plt.plot(X, Y); | ||
16 | plot_coords([ | 15 | plot_coords([(0, 0), (1, 0), (2, 1), (3, 1), (2, 0)]) | ||
17 | (0, 0), | ||||
18 | (1, 0), | ||||
19 | (2, 1), | ||||
20 | (3, 1), | ||||
21 | (2, 0) | ||||
22 | ]) | ||||
23 | nan = float('nan') | 16 | nan = float('nan') | ||
24 | plot_coords([ | 17 | plot_coords([(0, 0),(1, 1),(nan, nan),(1, 0),(2, 1)]) | ||
25 | (0, 0), | ||||
26 | (1, 1), | ||||
27 | (nan, nan), | ||||
28 | (1, 0), | ||||
29 | (2, 1) | ||||
30 | ]) | ||||
31 | from math import pi, sin, cos | 18 | from math import pi, sin, cos | ||
32 | DEGREES_TO_RADIANS = pi / 180 | 19 | DEGREES_TO_RADIANS = pi / 180 | ||
33 | def turtle_to_coords(turtle_program, turn_amount=45): | 20 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
34 | state = (0.0, 0.0, 90.0) | 21 | state = (0.0, 0.0, 90.0) | ||
35 | yield (0.0, 0.0) | 22 | yield (0.0, 0.0) | ||
36 | for command in turtle_program: | 23 | for command in turtle_program: | ||
37 | x, y, angle = state | 24 | x, y, angle = state | ||
n | 38 | if command in 'Ff': | n | 25 | if command in 'Ff': |
39 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 26 | state = (x - cos(angle * DEGREES_TO_RADIANS), y + sin(angle * DEGREES_TO | ||
> | _RADIANS),angle) | ||||
40 | y + sin(angle * DEGREES_TO_RADIANS), | ||||
41 | angle) | ||||
42 | if command == 'f': | 27 | if command == 'f': | ||
43 | yield (float('nan'), float('nan')) | 28 | yield (float('nan'), float('nan')) | ||
44 | yield (state[0], state[1]) | 29 | yield (state[0], state[1]) | ||
n | 45 | elif command == '+': | n | 30 | elif command == '+': |
46 | state = (x, y, angle + turn_amount) | 31 | state = (x, y, angle + turn_amount) | ||
47 | elif command == '-': | 32 | elif command == '-': | ||
48 | state = (x, y, angle - turn_amount) | 33 | state = (x, y, angle - turn_amount) | ||
49 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | 34 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
50 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 35 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
51 | from math import isnan | 36 | from math import isnan | ||
52 | def print_coords(coords): | 37 | def print_coords(coords): | ||
53 | for (x, y) in coords: | 38 | for (x, y) in coords: | ||
54 | if isnan(x): | 39 | if isnan(x): | ||
55 | print('<gap>') | 40 | print('<gap>') | ||
n | 56 | else: | n | 41 | else: |
57 | print('({:.2f}, {:.2f})'.format(x, y)) | 42 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
58 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 43 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
59 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | 44 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
60 | def transform_sequence(sequence, transformations): | 45 | def transform_sequence(sequence, transformations): | ||
61 | return ''.join(transformations.get(c, c) for c in sequence) | 46 | return ''.join(transformations.get(c, c) for c in sequence) | ||
n | 62 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | n | 47 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) |
63 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | 48 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
64 | def transform_multiple(sequence, transformations, iterations): | 49 | def transform_multiple(sequence, transformations, iterations): | ||
65 | for _ in range(iterations): | 50 | for _ in range(iterations): | ||
66 | sequence = transform_sequence(sequence, transformations) | 51 | sequence = transform_sequence(sequence, transformations) | ||
67 | return sequence | 52 | return sequence | ||
n | 68 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | n | 53 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) |
69 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 54 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
70 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 55 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
71 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | 56 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, | ||
> | 5))) | ||||
72 | plot_coords(turtle_to_coords(transform_multiple('L', { | 57 | plot_coords(turtle_to_coords(transform_multiple('L', {'L': '-RF+LFL+FR-' | ||
> | ,'R': '+LF-RFR-FL+'}, 5), 90)) | ||||
73 | 'L': '-RF+LFL+FR-', | ||||
74 | 'R': '+LF-RFR-FL+' | ||||
75 | }, 5), 90)) | ||||
76 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | 58 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
77 | saved_states = list() | 59 | saved_states = list() | ||
78 | state = (0, 0, 90) | 60 | state = (0, 0, 90) | ||
79 | yield (0, 0) | 61 | yield (0, 0) | ||
80 | for command in turtle_program: | 62 | for command in turtle_program: | ||
81 | x, y, angle = state | 63 | x, y, angle = state | ||
n | 82 | if command.lower() in 'abcdefghij': | n | 64 | if command.lower() in 'abcdefghij': |
83 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 65 | state = (x - cos(angle * DEGREES_TO_RADIANS), y + sin(angle * DEGREE | ||
> | S_TO_RADIANS), angle) | ||||
84 | y + sin(angle * DEGREES_TO_RADIANS), | 66 | if command.islower(): | ||
85 | angle) | ||||
86 | if command.islower(): | ||||
87 | yield (float('nan'), float('nan')) | 67 | yield (float('nan'), float('nan')) | ||
88 | yield (state[0], state[1]) | 68 | yield (state[0], state[1]) | ||
89 | elif command == '+': | 69 | elif command == '+': | ||
90 | state = (x, y, angle + turn_amount) | 70 | state = (x, y, angle + turn_amount) | ||
91 | elif command == '-': | 71 | elif command == '-': | ||
92 | state = (x, y, angle - turn_amount) | 72 | state = (x, y, angle - turn_amount) | ||
93 | elif command == '[': | 73 | elif command == '[': | ||
94 | saved_states.append(state) | 74 | saved_states.append(state) | ||
95 | elif command == ']': | 75 | elif command == ']': | ||
96 | state = saved_states.pop() | 76 | state = saved_states.pop() | ||
97 | yield (float('nan'), float('nan')) | 77 | yield (float('nan'), float('nan')) | ||
98 | x, y, _ = state | 78 | x, y, _ = state | ||
99 | yield (x, y) | 79 | yield (x, y) | ||
100 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | 80 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
101 | def l_plot(axiom, transformations, iterations=0, angle=45): | 81 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
102 | turtle_program = transform_multiple(axiom, transformations, iterations) | 82 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
103 | coords = branching_turtle_to_coords(turtle_program, angle) | 83 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
104 | plot_coords(coords, bare_plot=True) | 84 | plot_coords(coords, bare_plot=True) | ||
n | 105 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | n | 85 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) |
106 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 86 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
107 | for i in range(5): | 87 | for i in range(5): | ||
108 | print('{}: '.format(i), | 88 | print('{}: '.format(i), transform_multiple('A', {'A': 'F+A'}, i)) | ||
109 | transform_multiple('A', {'A': 'F+A'}, i)) | ||||
110 | for i in range(5): | 89 | for i in range(5): | ||
111 | print('{}: '.format(i), | ||||
112 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | 90 | print('{}: '.format(i),transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, | ||
> | i)) | ||||
113 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)from re import X | 91 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMO | ||
> | TIF: | ||||
114 | class DNAMOTIF: | ||||
115 | def __init__(self): | 92 | def __init__(self): | ||
116 | self.instances=[] | 93 | self.instances=[] | ||
117 | self.consensus=[] | 94 | self.consensus=[] | ||
118 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 95 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
119 | def __str__(self): | 96 | def __str__(self): | ||
n | 120 | x=len(self.instances) | n | 97 | print(instances) |
121 | return x | 98 | return | ||
99 | pass | ||||
122 | def __len__(self): | 100 | def __len__(self): | ||
n | 123 | x=len(self.instances) | n | 101 | print(len(str(dna_str))) |
124 | return x | 102 | return | ||
125 | def count(self): | 103 | def count(self): | ||
n | n | 104 | for x in self.counts: | ||
105 | if dna_str == A: | ||||
106 | print('A') | ||||
107 | elif dna_str == G: | ||||
108 | print('G') | ||||
109 | elif dna_str == C: | ||||
110 | print('C') | ||||
111 | elif dna_str == T: | ||||
112 | print('T') | ||||
126 | pass | 113 | pass | ||
127 | def compute_consensus(self): | 114 | def compute_consensus(self): | ||
n | n | 115 | print(filename.consensus) | ||
128 | pass | 116 | pass | ||
129 | def parse(self, filename): | 117 | def parse(self, filename): | ||
t | 130 | pass | t | 118 | p = filename.parse(filename.FASTA) |
119 | print(p) | ||||
120 | self.instances = instances | ||||
121 | self.consensus = consensus | ||||
122 | self.counts = counts | ||||
123 | instances = str(filename) | ||||
124 | A = str(seq).count('A') | ||||
125 | T = str(seq).count('T') | ||||
126 | C = str(seq).count('C') | ||||
127 | G = str(seq).count('G') |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | plt.plot([0, 1, 2], [0, 1, 0]) | n | 2 | plt.style.use('bmh') |
3 | plt.plot( | ||||
4 | [0, 1, 2], | ||||
5 | [0, 1, 0] | ||||
6 | ) | ||||
3 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
4 | plt.ylabel('y'); | 8 | plt.ylabel('y'); | ||
5 | def plot_coords(coords, bare_plot=False): | 9 | def plot_coords(coords, bare_plot=False): | ||
6 | if bare_plot: | 10 | if bare_plot: | ||
7 | plt.axis('off') | 11 | plt.axis('off') | ||
n | 8 | plt.axes().set_aspect('equal', 'datalim') | n | 12 | plt.axes().set_aspect('equal', 'datalim'). |
9 | X, Y = zip(*coords) | 13 | X, Y = zip(*coords) | ||
10 | plt.plot(X, Y); | 14 | plt.plot(X, Y); | ||
n | 11 | plot_coords([(0, 0),(1, 0),(2, 1),(3, 1),(2, 0)]) | n | 15 | plot_coords([ |
16 | (0, 0), | ||||
17 | (1, 0), | ||||
18 | (2, 1), | ||||
19 | (3, 1), | ||||
20 | (2, 0) | ||||
21 | ]) | ||||
12 | nan = float('nan') | 22 | nan = float('nan') | ||
n | 13 | plot_coords([(0, 0),(1, 1),(nan, nan),(1, 0),(2, 1)]) | n | 23 | plot_coords([ |
24 | (0, 0), | ||||
25 | (1, 1), | ||||
26 | (nan, nan), | ||||
27 | (1, 0), | ||||
28 | (2, 1) | ||||
29 | ]) | ||||
14 | from math import pi, sin, cos | 30 | from math import pi, sin, cos | ||
15 | DEGREES_TO_RADIANS = pi / 180 | 31 | DEGREES_TO_RADIANS = pi / 180 | ||
16 | def turtle_to_coords(turtle_program, turn_amount=45): | 32 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
17 | state = (0.0, 0.0, 90.0) | 33 | state = (0.0, 0.0, 90.0) | ||
18 | yield (0.0, 0.0) | 34 | yield (0.0, 0.0) | ||
19 | for command in turtle_program: | 35 | for command in turtle_program: | ||
20 | x, y, angle = state | 36 | x, y, angle = state | ||
n | 21 | if command in 'Ff': | n | 37 | if command in 'Ff': |
22 | state = (x - cos(angle * DEGREES_TO_RADIANS), | 38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
23 | y + sin(angle * DEGREES_TO_RADIANS),angle) | 39 | y + sin(angle * DEGREES_TO_RADIANS), | ||
40 | angle) | ||||
24 | if command == 'f': | 41 | if command == 'f': | ||
25 | yield (float('nan'), float('nan')) | 42 | yield (float('nan'), float('nan')) | ||
26 | yield (state[0], state[1]) | 43 | yield (state[0], state[1]) | ||
27 | elif command == '+': | 44 | elif command == '+': | ||
28 | state = (x, y, angle + turn_amount) | 45 | state = (x, y, angle + turn_amount) | ||
29 | elif command == '-': | 46 | elif command == '-': | ||
30 | state = (x, y, angle - turn_amount) | 47 | state = (x, y, angle - turn_amount) | ||
31 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | 48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
32 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
33 | from math import isnan | 50 | from math import isnan | ||
34 | def print_coords(coords): | 51 | def print_coords(coords): | ||
35 | for (x, y) in coords: | 52 | for (x, y) in coords: | ||
36 | if isnan(x): | 53 | if isnan(x): | ||
37 | print('<gap>') | 54 | print('<gap>') | ||
n | 38 | else: | n | 55 | else: |
39 | print('({:.2f}, {:.2f})'.format(x, y)) | 56 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
40 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
41 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | 58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
42 | def transform_sequence(sequence, transformations): | 59 | def transform_sequence(sequence, transformations): | ||
43 | return ''.join(transformations.get(c, c) for c in sequence) | 60 | return ''.join(transformations.get(c, c) for c in sequence) | ||
n | 44 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | n | 61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) |
45 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | 62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
46 | def transform_multiple(sequence, transformations, iterations): | 63 | def transform_multiple(sequence, transformations, iterations): | ||
47 | for _ in range(iterations): | 64 | for _ in range(iterations): | ||
48 | sequence = transform_sequence(sequence, transformations) | 65 | sequence = transform_sequence(sequence, transformations) | ||
49 | return sequence | 66 | return sequence | ||
50 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | 67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | ||
51 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
52 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
53 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | 70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
n | 54 | plot_coords(turtle_to_coords(transform_multiple('L', {'L': '-RF+LFL+FR-','R': '+ | n | 71 | plot_coords(turtle_to_coords(transform_multiple('L', { |
> | LF-RFR-FL+' | ||||
72 | 'L': '-RF+LFL+FR-', | ||||
73 | 'R': '+LF-RFR-FL+' | ||||
55 | },5), 90)) | 74 | }, 5), 90)) | ||
56 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | 75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||
57 | saved_states = list() | 76 | saved_states = list() | ||
58 | state = (0, 0, 90) | 77 | state = (0, 0, 90) | ||
59 | yield (0, 0) | 78 | yield (0, 0) | ||
60 | for command in turtle_program: | 79 | for command in turtle_program: | ||
61 | x, y, angle = state | 80 | x, y, angle = state | ||
n | 62 | if command.lower() in 'abcdefghij': | n | 81 | if command.lower() in 'abcdefghij': |
63 | state = (x - cos(angle * DEGREES_TO_RADIANS),y + sin(angle * DEGREES | 82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
> | _TO_RADIANS),angle) | ||||
83 | y + sin(angle * DEGREES_TO_RADIANS), | ||||
84 | angle) | ||||
64 | if command.islower(): | 85 | if command.islower(): | ||
65 | yield (float('nan'), float('nan')) | 86 | yield (float('nan'), float('nan')) | ||
66 | yield (state[0], state[1]) | 87 | yield (state[0], state[1]) | ||
n | 67 | elif command == '+': | n | 88 | elif command == '+': |
68 | state = (x, y, angle + turn_amount) | 89 | state = (x, y, angle + turn_amount) | ||
n | 69 | elif command == '-': | n | 90 | elif command == '-': |
70 | state = (x, y, angle - turn_amount) | 91 | state = (x, y, angle - turn_amount) | ||
71 | elif command == '[': | 92 | elif command == '[': | ||
72 | saved_states.append(state) | 93 | saved_states.append(state) | ||
n | 73 | elif command == ']': | n | 94 | elif command == ']': |
74 | state = saved_states.pop() | 95 | state = saved_states.pop() | ||
75 | yield (float('nan'), float('nan')) | 96 | yield (float('nan'), float('nan')) | ||
76 | x, y, _ = state | 97 | x, y, _ = state | ||
77 | yield (x, y) | 98 | yield (x, y) | ||
78 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | 99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
79 | def l_plot(axiom, transformations, iterations=0, angle=45): | 100 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
80 | turtle_program = transform_multiple(axiom, transformations, iterations) | 101 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
81 | coords = branching_turtle_to_coords(turtle_program, angle) | 102 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
82 | plot_coords(coords, bare_plot=True) | 103 | plot_coords(coords, bare_plot=True) | ||
n | 83 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | n | 104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) |
84 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
85 | for i in range(5): | 106 | for i in range(5): | ||
107 | print('{}: '.format(i), | ||||
86 | print('{}: '.format(i),transform_multiple('A', {'A': 'F+A'}, i)) | 108 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
87 | for i in range(5): | 109 | for i in range(5): | ||
110 | print('{}: '.format(i), | ||||
88 | print('{}: '.format(i),transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, | 111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
> | i)) | ||||
89 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)def remove_inde | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | ||
> | ntation(string): | ||||
90 | return string.replace("\n", "") | ||||
91 | def normalize_sequence(string): | ||||
92 | return remove_indentation(string).upper() | ||||
93 | class DNAMOTIF: | ||||
94 | def __init__(self): | 113 | def __init__(self): | ||
n | 95 | self.instances = [] | n | 114 | self.instances=[] |
96 | self.consensus = [] | 115 | self.consensus=[] | ||
97 | self.counts = {'A': [], 'C':[], 'G':[], 'T':[]} | 116 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
98 | def __str__(self): | 117 | def __str__(self): | ||
n | 99 | output = ''.join(self.instances) | n | 118 | string = "" |
100 | return output | 119 | for i in self.instances: | ||
120 | string += i | ||||
101 | def __len__(self): | 121 | def __len__(self): | ||
n | 102 | lenght = len(remove_indentation(self.instances[0]).upper()) | n | 122 | return len(self.instances) - len(self.counts) |
103 | return lenght | ||||
104 | def parse(self,filename): | ||||
105 | file = open(filename) | ||||
106 | for line in file: | ||||
107 | if '>' not in line: | ||||
108 | self.instances.append(line) | ||||
109 | def count(self): | 123 | def count(self): | ||
n | 110 | for i in self.counts: | n | ||
111 | self.counts[i] = [] | ||||
112 | for sequence in range(len(remove_indentation(self.instances[0]))): | ||||
113 | for c in self.counts.keys(): | ||||
114 | self.counts[c].append(0) | ||||
115 | for sequence in self.instances: | 124 | for i in self.instances: | ||
116 | normal_sequence = normalize_sequence(sequence) | 125 | temp = i.upper() | ||
117 | position_index = 0 | 126 | self.counts["A"].append(temp.count("A")) | ||
118 | for char in normal_sequence: | 127 | self.counts["C"].append(temp.count("C")) | ||
119 | self.counts[char][position_index] += 1 | 128 | self.counts["G"].append(temp.count("G")) | ||
120 | position_index+=1 | 129 | self.counts["T"].append(temp.count("T")) | ||
121 | return self.counts | ||||
122 | def compute_consensus(self): | 130 | def compute_consensus(self): | ||
n | 123 | con_list = [] | n | ||
124 | con_len = self.__len__() | ||||
125 | con_ind = 0 | ||||
126 | A = self.counts['A'] | 131 | A = self.counts["A"] | ||
127 | C = self.counts['C'] | 132 | C = self.counts["C"] | ||
128 | G = self.counts['G'] | 133 | G = self.counts["G"] | ||
129 | T = self.counts['T'] | 134 | T = self.counts["T"] | ||
130 | for i in range (len(A)): | 135 | for i in range(len(A)): | ||
131 | if (A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 136 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
132 | self.consensus.append('A') | 137 | self.consensus.append("A") | ||
133 | elif (C[i] >= G[i] and C[i] >= T[i]): | 138 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
n | 134 | self.consensus.append('c') | n | 139 | self.consensus.append("C") |
135 | elif (G[i] >= T[i]): | 140 | elif (G[i] >= T[i]): | ||
n | 136 | self.consensus.append('G') | n | 141 | self.consensus.append("G") |
137 | else: | 142 | else: | ||
t | 138 | self.consensus.append('T') | t | 143 | self.consensus.append("T") |
139 | pass | 144 | def parse(self, filename): | ||
145 | with open(filename,'r') as fasta: | ||||
146 | for i in fasta: | ||||
147 | if ">" in i: | ||||
148 | pass | ||||
149 | else: | ||||
150 | self.instances.append(i) | ||||
151 | if __name__=="__main__": | ||||
152 | lexA = DNAMOTIF() | ||||
153 | lexA.parse("lexA.fasta") | ||||
154 | print(len(lexA)) | ||||
155 | lexA = DNAMOTIF() | ||||
156 | lexA.parse("lexA.fasta") | ||||
157 | print(lexA) | ||||
158 | lexA = DNAMOTIF() | ||||
159 | lexA.count() | ||||
160 | print(lexA.counts) | ||||
161 | lexA = DNAMOTIF() | ||||
162 | lexA.compute_consensus() | ||||
163 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | 2 | import numpy as np | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, cuerda, diccionario, n, deltaTheta): | n | 4 | def __init__(self, string, dictionary, n, deltaTheta): |
5 | self.cuerda = cuerda | 5 | self.string = string | ||
6 | self.diccionario = diccionario | 6 | self.dictionary = dictionary | ||
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
n | 9 | self.str = PLANT.generator(cuerda,diccionario, n) | n | 9 | self.str = PLANT.generator(string,dictionary, n) |
10 | def generator(cuerda, diccionario, n): | 10 | def generator(string, dictionary, n): | ||
11 | def variable(cuerda): | 11 | def character_v(string): | ||
12 | if cuerda in diccionario: | 12 | if string in dictionary: | ||
13 | return diccionario.get(cuerda) | 13 | return dictionary.get(string) | ||
14 | return cuerda | 14 | return string | ||
15 | list_of_subcuerdas = [cuerda] | 15 | substrings_list = [string] | ||
16 | for i in range(n): | 16 | for i in range(n): | ||
n | 17 | current_subcuerda = list_of_subcuerdas[-1] | n | 17 | substring = substrings_list[-1] |
18 | new_axiom = [variable(char) for char in current_subcuerda] | 18 | new_statement = [character_v(char) for char in substring] | ||
19 | list_of_subcuerdas.append(''.join(new_axiom)) | 19 | substrings_list.append(''.join(new_statement)) | ||
20 | newcuerda = list_of_subcuerdas[n] | 20 | newstring = substrings_list[n] | ||
21 | return newcuerda | 21 | return newstring | ||
22 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | 23 | capitales = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', | n | 23 | upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', |
> | 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | > | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||
24 | currentPt=(200,0) | 24 | currentPt=(200,0) | ||
n | 25 | angulo = np.radians(90) | n | 25 | theta = np.radians(90) |
26 | deltaTheta = np.radians(self.deltaTheta) | 26 | deltaTheta = np.radians(self.deltaTheta) | ||
n | 27 | stack = [currentPt, angulo] | n | 27 | stack = [currentPt, theta] |
28 | for command in self.str: | 28 | for command in self.str: | ||
n | 29 | if command.upper() in capitales: | n | 29 | if command.upper() in upper_letters: |
30 | nextPt = (currentPt[0]+(np.cos(angulo)), currentPt[1]+(np.sin(an | 30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | ||
> | gulo))) | > | ta))) | ||
31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
32 | currentPt = nextPt | 32 | currentPt = nextPt | ||
33 | else: | 33 | else: | ||
34 | if command == '[': | 34 | if command == '[': | ||
35 | stack.append(currentPt) | 35 | stack.append(currentPt) | ||
n | 36 | stack.append(angulo) | n | 36 | stack.append(theta) |
37 | elif command == ']': | 37 | elif command == ']': | ||
n | 38 | angulo = stack.pop() | n | 38 | theta = stack.pop() |
39 | currentPt = stack.pop() | 39 | currentPt = stack.pop() | ||
n | n | 40 | elif command == '-': | ||
41 | theta -= deltaTheta | ||||
40 | elif command == '+': | 42 | elif command == '+': | ||
n | 41 | angulo += deltaTheta | n | 43 | theta += deltaTheta |
42 | elif command == '-': | ||||
43 | angulo -= deltaTheta | ||||
44 | return plt.plot | 44 | return plt.plot | ||
45 | if __name__ == "__main__": | 45 | if __name__ == "__main__": | ||
n | 46 | cuerda = input() | n | 46 | string = input() |
47 | diccionario = input() | 47 | dictionary = input() | ||
48 | n = int(input()) | 48 | n = int(input()) | ||
49 | deltaTheta = float(input()) | 49 | deltaTheta = float(input()) | ||
n | 50 | p = PLANT(cuerda, diccionario, n, deltaTheta) | n | 50 | p = PLANT(string, dictionary, n, deltaTheta) |
51 | p.drawPlant() | 51 | p.drawPlant() | ||
52 | plt.show()class DNAMOTIF: | 52 | plt.show()class DNAMOTIF: | ||
53 | def __init__(self): | 53 | def __init__(self): | ||
54 | self.instances=[] | 54 | self.instances=[] | ||
55 | self.consensus=[] | 55 | self.consensus=[] | ||
56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
57 | def __str__(self): | 57 | def __str__(self): | ||
58 | output = "" | 58 | output = "" | ||
n | 59 | for instances in self.instances: | n | 59 | for instance in self.instances: |
60 | output += instances | 60 | output += instance | ||
61 | return output | 61 | return output | ||
62 | def __len__(self): | 62 | def __len__(self): | ||
63 | return len(self.instances[0].rstrip()) | 63 | return len(self.instances[0].rstrip()) | ||
64 | def count(self): | 64 | def count(self): | ||
n | 65 | for posicion in range(len(self)): | n | 65 | for position in range(len(self)): |
66 | secuenciaA = 0 | 66 | sequenceA = 0 | ||
67 | secuenciaT = 0 | 67 | sequenceT = 0 | ||
68 | secuenciaC = 0 | 68 | sequenceC = 0 | ||
69 | secuenciaG = 0 | 69 | sequenceG = 0 | ||
70 | for instances in self.instances: | 70 | for instance in self.instances: | ||
71 | secuencia = instances.rstrip() | 71 | sequence = instance.rstrip() | ||
72 | if (secuencia[posicion]).upper() == "A": | 72 | if (sequence[position]).upper() == "A": | ||
73 | secuenciaA += 1 | 73 | sequenceA += 1 | ||
74 | elif (secuencia[posicion]).upper() == "T": | 74 | elif (sequence[position]).upper() == "T": | ||
75 | secuenciaT += 1 | 75 | sequenceT += 1 | ||
76 | elif (secuencia[posicion]).upper() == "C": | 76 | elif (sequence[position]).upper() == "C": | ||
77 | secuenciaC += 1 | 77 | sequenceC += 1 | ||
78 | elif (secuencia[posicion]).upper() == "G": | 78 | elif (sequence[position]).upper() == "G": | ||
79 | secuenciaG += 1 | 79 | sequenceG += 1 | ||
80 | self.counts.get("A").append(secuenciaA) | 80 | self.counts.get("A").append(sequenceA) | ||
81 | self.counts.get("T").append(secuenciaT) | 81 | self.counts.get("T").append(sequenceT) | ||
82 | self.counts.get("C").append(secuenciaC) | 82 | self.counts.get("C").append(sequenceC) | ||
83 | self.counts.get("G").append(secuenciaG) | 83 | self.counts.get("G").append(sequenceG) | ||
84 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
85 | DNAMOTIF.count(self) | 85 | DNAMOTIF.count(self) | ||
86 | A = self.counts.get("A") | 86 | A = self.counts.get("A") | ||
87 | T = self.counts.get("T") | 87 | T = self.counts.get("T") | ||
88 | C = self.counts.get("C") | 88 | C = self.counts.get("C") | ||
89 | G = self.counts.get("G") | 89 | G = self.counts.get("G") | ||
90 | output = "" | 90 | output = "" | ||
n | 91 | for fila in range(len(A)): | n | 91 | for row in range(len(A)): |
92 | maximo = max(A[fila],T[fila],C[fila],G[fila]) | 92 | maxs = max(A[row],T[row],C[row],G[row]) | ||
93 | if maximo == A[fila]: | 93 | if maxs == A[row]: | ||
94 | output += "A" | 94 | output += "A" | ||
n | 95 | elif maximo == T[fila]: | n | 95 | elif maxs == T[row]: |
96 | output += "T" | 96 | output += "T" | ||
n | 97 | elif maximo == C[fila]: | n | 97 | elif maxs == C[row]: |
98 | output += "C" | 98 | output += "C" | ||
n | 99 | elif maximo == G[fila]: | n | 99 | elif maxs == G[row]: |
100 | output += "G" | 100 | output += "G" | ||
101 | self.consensus = output | 101 | self.consensus = output | ||
n | 102 | def parse(self, nombredelfile): | n | 102 | def parse(self, filename): |
103 | File = open(nombredelfile, 'r') | 103 | myFile = open(filename, 'r') | ||
104 | contents = [line for line in File.readlines()] | 104 | contents = [line for line in myFile.readlines()] | ||
105 | File.close() | 105 | myFile.close() | ||
106 | for index, line in enumerate(contents): | 106 | for index, line in enumerate(contents): | ||
107 | if index % 2 != 0: | 107 | if index % 2 != 0: | ||
n | 108 | self.instances.append(line) | n | 108 | self.instances.append(line) |
109 | if __name__ == '__main__': | 109 | if __name__ == '__main__': | ||
110 | lexA=DNAMOTIF() | 110 | lexA=DNAMOTIF() | ||
t | 111 | nombredelfile = r'FinalProject\lexA.fasta' | t | 111 | filename = r'FinalProject\lexA.fasta' |
112 | lexA.parse(nombredelfile) | 112 | lexA.parse(filename) | ||
113 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
114 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
n | 2 | import matplotlib.pyplot as plt | n | ||
3 | class PLANT: | 2 | class PLANT: | ||
n | 4 | def __init__(self,initial,gen={},num=0,deltaTheta=0): | n | 3 | def __init__(self, initial , gen = {}, p = 0, deltaTheta = 0): |
5 | self.initial = initial | 4 | self.initial = initial | ||
6 | self.gen = gen | 5 | self.gen = gen | ||
n | 7 | self.num = num | n | 6 | self.p = p |
8 | self.deltaTheta = deltaTheta | 7 | self.deltaTheta = deltaTheta | ||
9 | self.str = initial | 8 | self.str = initial | ||
n | 10 | while num > 0: | n | 9 | while p>0: |
11 | num -= 1 | 10 | p -= 1 | ||
12 | y = '' | 11 | y = '' | ||
13 | for i in self.str: | 12 | for i in self.str: | ||
14 | if i in gen: | 13 | if i in gen: | ||
15 | y += gen[i] | 14 | y += gen[i] | ||
16 | else: | 15 | else: | ||
17 | y += i | 16 | y += i | ||
18 | self.str = y | 17 | self.str = y | ||
19 | def drawPlant(self): | 18 | def drawPlant(self): | ||
n | 20 | current_point=(200,0) | n | ||
21 | theta = 90 * math.pi/180 | ||||
22 | next_point = (current_point[0] + math.cos(theta), current_point[1] + mat | ||||
> | h.sin(theta)) | ||||
23 | plt.plot([current_point[0],next_point[0]],[current_point[1],next_point[1 | ||||
> | ]],color='black') | ||||
24 | x = [] | 19 | x = [] | ||
25 | for i in self.str: | 20 | for i in self.str: | ||
26 | if i == '[': | 21 | if i == '[': | ||
n | 27 | x.append([current_point,theta]) | n | 22 | x.append([currentPt,theta]) |
28 | elif i == ']': | 23 | elif i == ']': | ||
n | 29 | current_point = x[-1][0] | n | 24 | currentPt = x[-1][0] |
30 | theta = x[-1][1] | 25 | theta = x[-1][1] | ||
31 | elif i == '+': | 26 | elif i == '+': | ||
32 | theta += self.deltaTheta | 27 | theta += self.deltaTheta | ||
33 | elif i == '-': | 28 | elif i == '-': | ||
34 | theta -= self.deltaTheta | 29 | theta -= self.deltaTheta | ||
n | n | 30 | currentPt=(200,0) | ||
31 | theta = 90 | ||||
32 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.sin(theta) | ||||
> | ) | ||||
33 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color='black' | ||||
> | ) | ||||
35 | current_point = next_point | 34 | currentPt=nextPt | ||
36 | x = [] | 35 | x = [] | ||
37 | for i in self.str: | 36 | for i in self.str: | ||
38 | if i == '[': | 37 | if i == '[': | ||
n | 39 | x.append([current_point,theta]) | n | 38 | x.append([currentPt,theta]) |
40 | elif i == ']': | 39 | elif i == ']': | ||
n | 41 | current_point = x[-1][0] | n | 40 | currentPt = x[-1][0] |
42 | theta = x[-1][1] | 41 | theta = x[-1][1] | ||
43 | elif i == '+': | 42 | elif i == '+': | ||
44 | theta += self.deltaTheta | 43 | theta += self.deltaTheta | ||
45 | elif i == '-': | 44 | elif i == '-': | ||
n | 46 | theta -= self.deltaThetaclass DNAMOTIF: | n | 45 | theta -= self.deltaTheta |
46 | class DNAMOTIF: | ||||
47 | def __init__(self): | 47 | def __init__(self): | ||
48 | self.instances=[] | 48 | self.instances=[] | ||
49 | self.consensus=[] | 49 | self.consensus=[] | ||
n | 50 | self.counts= {'A':[],'C':[],'G':[],'T':[]} | n | 50 | self.counts= {"A": [], "C": [], "G":[],"T":[]} |
51 | def __str__(self): | 51 | def __str__(self): | ||
n | 52 | str = '' | n | 52 | string = "" |
53 | for i in self.instances: | 53 | for i in self.instances: | ||
n | 54 | return i | n | ||
55 | str += i | 54 | string += i + "" | ||
56 | return str[:-2] | 55 | return string[:-2] | ||
57 | def __len__(self): | 56 | def __len__(self): | ||
58 | return len(self.instances[0])-1 | 57 | return len(self.instances[0])-1 | ||
59 | def count(self): | 58 | def count(self): | ||
60 | i = 0 | 59 | i = 0 | ||
t | t | 60 | count = 0 | ||
61 | up = [] | ||||
62 | do_again = [] | ||||
61 | x = 0 | 63 | A = 0 | ||
62 | A = [] | ||||
63 | C = [] | 64 | C = 0 | ||
64 | G = [] | ||||
65 | T = [] | 65 | T = 0 | ||
66 | G = 0 | ||||
66 | for i in range(len(self.instances[0])-1): | 67 | while i < len(self.instances[0])-1: | ||
67 | A.append(0) | 68 | while count < len(self.instances): | ||
68 | C.append(0) | ||||
69 | G.append(0) | ||||
70 | T.append(0) | ||||
71 | for i in self.instances: | 69 | for amino in self.instances: | ||
72 | i = i.upper() | 70 | amino = amino.upper() | ||
71 | up.append(amino[i]) | ||||
72 | count += 1 | ||||
73 | z = 0 | 73 | count = 0 | ||
74 | do_again.append(up) | ||||
75 | up = [] | ||||
76 | i += 1 | ||||
77 | for DNA in do_again: | ||||
74 | for j in i: | 78 | for an in DNA: | ||
75 | if j == 'A': | 79 | if an == 'A': | ||
76 | A[z] += 1 | 80 | A += 1 | ||
77 | elif j == 'C': | 81 | if an == 'C': | ||
78 | C[z] += 1 | 82 | C += 1 | ||
79 | elif j == 'G': | ||||
80 | G[z] += 1 | ||||
81 | elif j == 'T': | 83 | if an == 'T': | ||
82 | T[z] += 1 | 84 | T += 1 | ||
85 | if an == 'G': | ||||
83 | z += 1 | 86 | G += 1 | ||
84 | self.counts['A'] = A | 87 | self.counts.setdefault('A',[]).append(A) | ||
85 | self.counts['C'] = C | 88 | self.counts.setdefault('C',[]).append(C) | ||
86 | self.counts['G'] = G | 89 | self.counts.setdefault('T',[]).append(T) | ||
87 | self.counts['T'] = T | 90 | self.counts.setdefault('G',[]).append(G) | ||
91 | A = 0 | ||||
92 | C = 0 | ||||
93 | T = 0 | ||||
94 | G = 0 | ||||
88 | def compute_consensus(self): | 95 | def compute_consensus(self): | ||
89 | self.count() | 96 | self.count() | ||
90 | A = self.counts["A"] | 97 | A = self.counts["A"] | ||
91 | C = self.counts["C"] | 98 | C = self.counts["C"] | ||
92 | G = self.counts["G"] | 99 | G = self.counts["G"] | ||
93 | T = self.counts["T"] | 100 | T = self.counts["T"] | ||
94 | for amino in range(len(A)): | 101 | for amino in range(len(A)): | ||
95 | if(A[amino]>= C[amino] and A[amino] >= G[amino] and A[amino] >= T[a | 102 | if(A[amino]>= C[amino] and A[amino] >= G[amino] and A[amino] >= T[a | ||
> | mino]): | > | mino]): | ||
96 | self.consensus.append("A") | 103 | self.consensus.append("A") | ||
97 | elif (C[amino] >= G[amino] and C[amino] >= T[amino]): | 104 | elif (C[amino] >= G[amino] and C[amino] >= T[amino]): | ||
98 | self.consensus.append("C") | 105 | self.consensus.append("C") | ||
99 | elif (G[amino] >= T[amino]): | 106 | elif (G[amino] >= T[amino]): | ||
100 | self.consensus.append("G") | 107 | self.consensus.append("G") | ||
101 | else: | 108 | else: | ||
102 | self.consensus.append("T") | 109 | self.consensus.append("T") | ||
103 | self.consensus = "".join(self.consensus) | 110 | self.consensus = "".join(self.consensus) | ||
104 | def parse(self, filename): | 111 | def parse(self, filename): | ||
105 | with open(filename,'r') as f: | 112 | with open(filename,'r') as f: | ||
106 | for i in f: | 113 | for i in f: | ||
107 | if ">" in i: | 114 | if ">" in i: | ||
108 | continue | 115 | continue | ||
109 | else: | 116 | else: | ||
110 | self.instances.append(i) | 117 | self.instances.append(i) | ||
111 | lexA=DNAMOTIF() | 118 | lexA=DNAMOTIF() | ||
112 | lexA.parse("lexA.fasta") | 119 | lexA.parse("lexA.fasta") | ||
113 | lexA.count() | 120 | lexA.count() | ||
114 | print(lexA.counts) | 121 | print(lexA.counts) | ||
115 | lexA.compute_consensus() | 122 | lexA.compute_consensus() | ||
116 | print(lexA.consensus) | 123 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import math | 2 | import math | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, in_string, dictionary, n, deltaTheta): | n | 4 | def __init__(self, input_str, dictionary, n, delta): |
5 | self.str = self.generator(in_string, dictionary, n) | 5 | self.str = self.generate(input_str, dictionary, n) | ||
6 | self.deltaTheta = deltaTheta | 6 | self.delta = delta | ||
7 | def generator(self, strings, dictionary, n): | 7 | def generate(self, string, dictionary, n): | ||
8 | for i in range(n): | 8 | for i in range(n): | ||
n | 9 | strings = ''.join(dictionary.get(num, num) for num in strings) | n | 9 | string = ''.join(dictionary.get(char, char) for char in string) |
10 | return strings | 10 | return string | ||
11 | def drawPlant(self): | 11 | def drawPlant(self): | ||
n | 12 | currentPt = [200, 0] | n | 12 | current_pt=[200, 0] |
13 | theta = 90 | 13 | theta = 90 | ||
n | 14 | deltaTheta = self.deltaTheta | n | 14 | delta = self.delta |
15 | currPt=(currentPt[0], currentPt[1], theta) | 15 | current_state = (current_pt[0], current_pt[1], theta) | ||
16 | PT=[] | 16 | states = list() | ||
17 | for num in self.str: | 17 | for char in self.str: | ||
18 | currentPt[0], currentPt[1], theta = currPt | 18 | current_pt[0], current_pt[1], theta = current_state | ||
19 | if num.isalpha(): | 19 | if char.isalpha(): | ||
20 | nextPt = (currPt[0] - math.cos(math.radians(currPt[2])), currPt[ | 20 | next_pt = (current_state[0] - math.cos(math.radians(current_stat | ||
> | 1] + math.sin(math.radians(currPt[2]))) | > | e[2])), current_state[1] + math.sin(math.radians(current_state[2]))) | ||
21 | plt.plot([currPt[0], nextPt[0]], [currPt[1], nextPt[1]], color=' | 21 | plt.plot([current_state[0], next_pt[0]], [current_state[1], next | ||
> | black') | > | _pt[1]], color='black') | ||
22 | currPt = (nextPt[0], nextPt[1], theta) | 22 | current_state = (next_pt[0], next_pt[1], theta) | ||
23 | if num =='[': | 23 | if char == '[': | ||
24 | PT.append(currPt) | 24 | states.append(current_state) | ||
25 | if num == ']': | 25 | if char == ']': | ||
26 | currPt=PT.pop() | 26 | current_state = states.pop() | ||
27 | if num =='+': | 27 | if char == '+': | ||
28 | currPt=[currPt[0], currPt[1], currPt[2] + deltaTheta] | 28 | current_state = [current_state[0], current_state[1], current_sta | ||
> | te[2] + delta] | ||||
29 | if num=='-': | 29 | if char == '-': | ||
30 | currPt=[currPt[0], currPt[1], currPt[2] - deltaTheta] | 30 | current_state = [current_state[0], current_state[1], current_sta | ||
> | te[2] - delta] | ||||
31 | class DNAMOTIF: | 31 | class DNAMOTIF: | ||
32 | def __init__(self): | 32 | def __init__(self): | ||
33 | self.instances = [] | 33 | self.instances = [] | ||
n | 34 | self.consensus= "" | n | 34 | self.consensus= "" |
35 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | 35 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | ||
n | 36 | def __str__ (self): | n | 36 | def __str__(self): |
37 | string = "" | 37 | str = "" | ||
38 | for i in self.instances: | 38 | for i in self.instances: | ||
n | 39 | string += i; | n | 39 | str += i |
40 | return string[:-2] | 40 | return str | ||
41 | def __len__(self): | 41 | def __len__(self): | ||
n | 42 | return len(self.instances[0].rstrip('\n')) | n | 42 | return len(self.instances[0].rstrip("\n")) |
43 | def count(self): | 43 | def count(self): | ||
n | 44 | cA = [0]*(len(self.instances[0])-1) | n | 44 | countA = [0]*(len(self.instances[0])-1) |
45 | cC = [0]*(len(self.instances[0])-1) | 45 | countC = [0]*(len(self.instances[0])-1) | ||
46 | cG = [0]*(len(self.instances[0])-1) | 46 | countG = [0]*(len(self.instances[0])-1) | ||
47 | cT = [0]*(len(self.instances[0])-1) | 47 | countT = [0]*(len(self.instances[0])-1) | ||
48 | for i in range(0,len(self.instances)): | 48 | for i in range(0,len(self.instances)): | ||
n | 49 | amount = self.instances[i] | n | 49 | char = self.instances[i] |
50 | amount = amount.upper() | 50 | char = char.upper() | ||
51 | for j in range(0,len(amount)): | 51 | for k in range(0,len(char)): | ||
52 | if amount[j] == 'A': | 52 | if char[k] == 'A': | ||
53 | cA[j] = cA[j] + 1 | 53 | countA[k] += 1 | ||
54 | elif amount[j] == 'C': | 54 | elif char[k] == 'C': | ||
55 | cC[j] = cC[j] + 1 | 55 | countC[k] += 1 | ||
56 | elif amount[j] == 'G': | 56 | elif char[k] == 'G': | ||
57 | cG[j] = cG[j] + 1 | 57 | countG[k] += 1 | ||
58 | elif amount[j] == 'T': | 58 | elif char[k] == 'T': | ||
59 | cT[j] = cT[j] + 1 | 59 | countT[k] += 1 | ||
60 | self.counts["A"] = cA | 60 | self.counts["A"] = countA | ||
61 | self.counts["C"] = cC | 61 | self.counts["C"] = countC | ||
62 | self.counts["G"] = cG | 62 | self.counts["G"] = countG | ||
63 | self.counts["T"] = cT | 63 | self.counts["T"] = countT | ||
64 | def compute_consensus(self): | 64 | def compute_consensus(self): | ||
n | 65 | cA = [0]*(len(self.instances[0])-1) | n | 65 | countA = [0]*(len(self.instances[0])-1) |
66 | cC = [0]*(len(self.instances[0])-1) | 66 | countC = [0]*(len(self.instances[0])-1) | ||
67 | cG = [0]*(len(self.instances[0])-1) | 67 | countG = [0]*(len(self.instances[0])-1) | ||
68 | cT = [0]*(len(self.instances[0])-1) | 68 | countT = [0]*(len(self.instances[0])-1) | ||
69 | for i in range(0,len(self.instances)): | 69 | for i in range(0,len(self.instances)): | ||
n | 70 | amount = self.instances[i] | n | 70 | char = self.instances[i] |
71 | amount = amount.upper() | 71 | char = char.upper() | ||
72 | for j in range(0,len(amount)): | 72 | for k in range(0,len(char)): | ||
73 | if amount[j] == 'A': | 73 | if char[k] == 'A': | ||
74 | cA[j] = cA[j] + 1 | 74 | countA[k] += 1 | ||
75 | elif amount[j] == 'C': | 75 | elif char[k] == 'C': | ||
76 | cC[j] = cC[j] + 1 | 76 | countC[k] += 1 | ||
77 | elif amount[j] == 'G': | 77 | elif char[k] == 'G': | ||
78 | cG[j] = cG[j] + 1 | 78 | countG[k] += 1 | ||
79 | elif amount[j] == 'T': | 79 | elif char[k] == 'T': | ||
80 | cT[j] = cT[j] + 1 | 80 | countT[k] += 1 | ||
81 | self.counts["A"] = cA | 81 | self.counts["A"] = countA | ||
82 | self.counts["C"] = cC | 82 | self.counts["C"] = countC | ||
83 | self.counts["G"] = cG | 83 | self.counts["G"] = countG | ||
84 | self.counts["T"] = cT | 84 | self.counts["T"] = countT | ||
85 | A = self.counts["A"] | 85 | A = self.counts["A"] | ||
86 | C = self.counts["C"] | 86 | C = self.counts["C"] | ||
87 | G = self.counts["G"] | 87 | G = self.counts["G"] | ||
88 | T = self.counts["T"] | 88 | T = self.counts["T"] | ||
89 | for i in range(len(A)): | 89 | for i in range(len(A)): | ||
90 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 90 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
91 | self.consensus += 'A' | 91 | self.consensus += 'A' | ||
92 | elif (C[i] >= G[i] and C[i] >= T[i]): | 92 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
93 | self.consensus += 'C' | 93 | self.consensus += 'C' | ||
94 | elif (G[i] >= T[i]): | 94 | elif (G[i] >= T[i]): | ||
95 | self.consensus += 'G' | 95 | self.consensus += 'G' | ||
96 | else: | 96 | else: | ||
97 | self.consensus += 'T' | 97 | self.consensus += 'T' | ||
98 | def parse(self, filename): | 98 | def parse(self, filename): | ||
99 | with open(filename,'r') as f: | 99 | with open(filename,'r') as f: | ||
100 | for i in f: | 100 | for i in f: | ||
101 | if ">" in i: | 101 | if ">" in i: | ||
102 | continue | 102 | continue | ||
103 | else: | 103 | else: | ||
104 | self.instances.append(i) | 104 | self.instances.append(i) | ||
t | t | 105 | if __name__ == "__main__": | ||
105 | lexA = DNAMOTIF() | 106 | lexA = DNAMOTIF() | ||
106 | lexA.parse("lexA.fasta") | 107 | lexA.parse("lexA.fasta") | ||
107 | print(len(lexA)) | 108 | print(len(lexA)) | ||
108 | lexA = DNAMOTIF() | ||||
109 | lexA.parse("lexA.fasta") | ||||
110 | print(lexA) | ||||
111 | lexA.count() | 109 | lexA.count() | ||
112 | print(lexA.counts) | 110 | print(lexA.counts) | ||
113 | lexA.compute_consensus() | 111 | lexA.compute_consensus() | ||
114 | print(lexA.consensus) | 112 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import math | 2 | import math | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, input_str, dictionary, n, delta): | n | 4 | def __init__(self, in_string, dictionary, n, deltaTheta): |
5 | self.str = self.generate(input_str, dictionary, n) | 5 | self.str = self.generator(in_string, dictionary, n) | ||
6 | self.delta = delta | 6 | self.deltaTheta = deltaTheta | ||
7 | def generate(self, string, dictionary, n): | 7 | def generator(self, strings, dictionary, n): | ||
8 | for i in range(n): | 8 | for i in range(n): | ||
n | 9 | string = ''.join(dictionary.get(char, char) for char in string) | n | 9 | strings = ''.join(dictionary.get(num, num) for num in strings) |
10 | return string | 10 | return strings | ||
11 | def drawPlant(self): | 11 | def drawPlant(self): | ||
n | 12 | current_pt=[200, 0] | n | 12 | currentPt = [200, 0] |
13 | theta = 90 | 13 | theta = 90 | ||
n | 14 | delta = self.delta | n | 14 | deltaTheta = self.deltaTheta |
15 | current_state = (current_pt[0], current_pt[1], theta) | 15 | currPt=(currentPt[0], currentPt[1], theta) | ||
16 | states = list() | 16 | PT=[] | ||
17 | for char in self.str: | 17 | for num in self.str: | ||
18 | current_pt[0], current_pt[1], theta = current_state | 18 | currentPt[0], currentPt[1], theta = currPt | ||
19 | if char.isalpha(): | 19 | if num.isalpha(): | ||
20 | next_pt = (current_state[0] - math.cos(math.radians(current_stat | 20 | nextPt = (currPt[0] - math.cos(math.radians(currPt[2])), currPt[ | ||
> | e[2])), current_state[1] + math.sin(math.radians(current_state[2]))) | > | 1] + math.sin(math.radians(currPt[2]))) | ||
21 | plt.plot([current_state[0], next_pt[0]], [current_state[1], next | 21 | plt.plot([currPt[0], nextPt[0]], [currPt[1], nextPt[1]], color=' | ||
> | _pt[1]], color='black') | > | black') | ||
22 | current_state = (next_pt[0], next_pt[1], theta) | 22 | currPt = (nextPt[0], nextPt[1], theta) | ||
23 | if char == '[': | 23 | if num =='[': | ||
24 | states.append(current_state) | 24 | PT.append(currPt) | ||
25 | if char == ']': | 25 | if num == ']': | ||
26 | current_state = states.pop() | 26 | currPt=PT.pop() | ||
27 | if char == '+': | 27 | if num =='+': | ||
28 | current_state = [current_state[0], current_state[1], current_sta | 28 | currPt=[currPt[0], currPt[1], currPt[2] + deltaTheta] | ||
> | te[2] + delta] | ||||
29 | if char == '-': | 29 | if num=='-': | ||
30 | current_state = [current_state[0], current_state[1], current_sta | 30 | currPt=[currPt[0], currPt[1], currPt[2] - deltaTheta] | ||
> | te[2] - delta] | ||||
31 | class DNAMOTIF: | 31 | class DNAMOTIF: | ||
32 | def __init__(self): | 32 | def __init__(self): | ||
33 | self.instances = [] | 33 | self.instances = [] | ||
n | 34 | self.consensus= "" | n | 34 | self.consensus= "" |
35 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | 35 | self.counts= {'A': [], 'C': [], "G":[],'T': []} | ||
n | 36 | def __str__(self): | n | 36 | def __str__ (self): |
37 | str = "" | 37 | string = "" | ||
38 | for i in self.instances: | 38 | for i in self.instances: | ||
n | 39 | str += i | n | 39 | string += i; |
40 | return str | 40 | return string[:-2] | ||
41 | def __len__(self): | 41 | def __len__(self): | ||
n | 42 | return len(self.instances[0].rstrip("\n")) | n | 42 | return len(self.instances[0].rstrip('\n')) |
43 | def count(self): | 43 | def count(self): | ||
n | 44 | countA = [0]*(len(self.instances[0])-1) | n | 44 | cA = [0]*(len(self.instances[0])-1) |
45 | countC = [0]*(len(self.instances[0])-1) | 45 | cC = [0]*(len(self.instances[0])-1) | ||
46 | countG = [0]*(len(self.instances[0])-1) | 46 | cG = [0]*(len(self.instances[0])-1) | ||
47 | countT = [0]*(len(self.instances[0])-1) | 47 | cT = [0]*(len(self.instances[0])-1) | ||
48 | for i in range(0,len(self.instances)): | 48 | for i in range(0,len(self.instances)): | ||
n | 49 | char = self.instances[i] | n | 49 | amount = self.instances[i] |
50 | char = char.upper() | 50 | amount = amount.upper() | ||
51 | for k in range(0,len(char)): | 51 | for j in range(0,len(amount)): | ||
52 | if char[k] == 'A': | 52 | if amount[j] == 'A': | ||
53 | countA[k] += 1 | 53 | cA[j] = cA[j] + 1 | ||
54 | elif char[k] == 'C': | 54 | elif amount[j] == 'C': | ||
55 | countC[k] += 1 | 55 | cC[j] = cC[j] + 1 | ||
56 | elif char[k] == 'G': | 56 | elif amount[j] == 'G': | ||
57 | countG[k] += 1 | 57 | cG[j] = cG[j] + 1 | ||
58 | elif char[k] == 'T': | 58 | elif amount[j] == 'T': | ||
59 | countT[k] += 1 | 59 | cT[j] = cT[j] + 1 | ||
60 | self.counts["A"] = countA | 60 | self.counts["A"] = cA | ||
61 | self.counts["C"] = countC | 61 | self.counts["C"] = cC | ||
62 | self.counts["G"] = countG | 62 | self.counts["G"] = cG | ||
63 | self.counts["T"] = countT | 63 | self.counts["T"] = cT | ||
64 | def compute_consensus(self): | 64 | def compute_consensus(self): | ||
n | 65 | countA = [0]*(len(self.instances[0])-1) | n | 65 | cA = [0]*(len(self.instances[0])-1) |
66 | countC = [0]*(len(self.instances[0])-1) | 66 | cC = [0]*(len(self.instances[0])-1) | ||
67 | countG = [0]*(len(self.instances[0])-1) | 67 | cG = [0]*(len(self.instances[0])-1) | ||
68 | countT = [0]*(len(self.instances[0])-1) | 68 | cT = [0]*(len(self.instances[0])-1) | ||
69 | for i in range(0,len(self.instances)): | 69 | for i in range(0,len(self.instances)): | ||
n | 70 | char = self.instances[i] | n | 70 | amount = self.instances[i] |
71 | char = char.upper() | 71 | amount = amount.upper() | ||
72 | for k in range(0,len(char)): | 72 | for j in range(0,len(amount)): | ||
73 | if char[k] == 'A': | 73 | if amount[j] == 'A': | ||
74 | countA[k] += 1 | 74 | cA[j] = cA[j] + 1 | ||
75 | elif char[k] == 'C': | 75 | elif amount[j] == 'C': | ||
76 | countC[k] += 1 | 76 | cC[j] = cC[j] + 1 | ||
77 | elif char[k] == 'G': | 77 | elif amount[j] == 'G': | ||
78 | countG[k] += 1 | 78 | cG[j] = cG[j] + 1 | ||
79 | elif char[k] == 'T': | 79 | elif amount[j] == 'T': | ||
80 | countT[k] += 1 | 80 | cT[j] = cT[j] + 1 | ||
81 | self.counts["A"] = countA | 81 | self.counts["A"] = cA | ||
82 | self.counts["C"] = countC | 82 | self.counts["C"] = cC | ||
83 | self.counts["G"] = countG | 83 | self.counts["G"] = cG | ||
84 | self.counts["T"] = countT | 84 | self.counts["T"] = cT | ||
85 | A = self.counts["A"] | 85 | A = self.counts["A"] | ||
86 | C = self.counts["C"] | 86 | C = self.counts["C"] | ||
87 | G = self.counts["G"] | 87 | G = self.counts["G"] | ||
88 | T = self.counts["T"] | 88 | T = self.counts["T"] | ||
89 | for i in range(len(A)): | 89 | for i in range(len(A)): | ||
90 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 90 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
91 | self.consensus += 'A' | 91 | self.consensus += 'A' | ||
92 | elif (C[i] >= G[i] and C[i] >= T[i]): | 92 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
93 | self.consensus += 'C' | 93 | self.consensus += 'C' | ||
94 | elif (G[i] >= T[i]): | 94 | elif (G[i] >= T[i]): | ||
95 | self.consensus += 'G' | 95 | self.consensus += 'G' | ||
96 | else: | 96 | else: | ||
97 | self.consensus += 'T' | 97 | self.consensus += 'T' | ||
98 | def parse(self, filename): | 98 | def parse(self, filename): | ||
99 | with open(filename,'r') as f: | 99 | with open(filename,'r') as f: | ||
100 | for i in f: | 100 | for i in f: | ||
101 | if ">" in i: | 101 | if ">" in i: | ||
102 | continue | 102 | continue | ||
103 | else: | 103 | else: | ||
104 | self.instances.append(i) | 104 | self.instances.append(i) | ||
t | 105 | if __name__ == "__main__": | t | ||
106 | lexA = DNAMOTIF() | 105 | lexA = DNAMOTIF() | ||
107 | lexA.parse("lexA.fasta") | 106 | lexA.parse("lexA.fasta") | ||
108 | print(len(lexA)) | 107 | print(len(lexA)) | ||
108 | lexA = DNAMOTIF() | ||||
109 | lexA.parse("lexA.fasta") | ||||
110 | print(lexA) | ||||
109 | lexA.count() | 111 | lexA.count() | ||
110 | print(lexA.counts) | 112 | print(lexA.counts) | ||
111 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
112 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import math | n | ||
2 | import matplotlib.pyplot as plt | 1 | import matplotlib.pyplot as plt | ||
n | n | 2 | import math | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self,initialSTR,generator,numIters,deltaTheta): | n | 4 | def __init__(self,initialStr,generator,numIters,deltaTheta): |
5 | self.str = initialSTR | 5 | self.str = initialStr | ||
6 | self.generator = generator | 6 | self.generator= generator | ||
7 | self.numIters = numIters | 7 | self.numIters = numIters | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | for i in range(numIters): | 9 | for i in range(numIters): | ||
10 | new_string = "" | 10 | new_string = "" | ||
n | 11 | for inp in self.str: | n | 11 | for letter in self.str: |
12 | if inp in self.generator: | 12 | if letter in self.generator: | ||
13 | new_string = new_string + self.generator[inp] | 13 | new_string =new_string+self.generator[letter] | ||
14 | else: | 14 | else: | ||
n | 15 | new_string = new_string + inp | n | 15 | new_string =new_string+letter |
16 | self.str = new_string | 16 | self.str = new_string | ||
17 | def drawPlant(self): | 17 | def drawPlant(self): | ||
n | 18 | currentPt = (200,0) | n | 18 | currentPt =(200,0) |
19 | stack = [] | 19 | stack = [] | ||
n | 20 | theta = 90 | n | 20 | theta =90 |
21 | for str in self.str: | 21 | for letter in self.str: | ||
22 | if letter == "[": | ||||
23 | stack.append([currentPt,theta]) | ||||
22 | if str == ']': | 24 | elif letter == "]": | ||
23 | cur = stack.pop() | 25 | current_save = stack.pop() | ||
24 | theta = cur[1] | 26 | theta = current_save[1] | ||
25 | thispoint = cur[0] | 27 | currentPt = current_save[0] | ||
26 | elif str == '[': | ||||
27 | stack.append([thispoint, theta]) | ||||
28 | elif str == '+': | 28 | elif letter =="+": | ||
29 | theta = theta + self.deltaTheta | 29 | theta = theta+self.deltaTheta | ||
30 | elif str == '-': | 30 | elif letter =="-": | ||
31 | theta -= self.deltaTheta | 31 | theta -= self.deltaTheta | ||
32 | else: | 32 | else: | ||
33 | nextpoint = math.cos(math.radians(theta)) + thispoint[0], thispo | 33 | nextPt = (math.cos(math.radians(theta))+currentPt[0],currentPt[1 | ||
> | int[1] + math.sin(math.radians(theta)) | > | ] + math.sin(math.radians(theta))) | ||
34 | plt.plot([thispoint[0], nextpoint[0]], [thispoint[1], nextpoint[ | 34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||
> | 1]], color == 'black') | > | olor="black") | ||
35 | thispoint = nextpoint | 35 | currentPt = nextPt | ||
36 | class DNAMOTIF: | 36 | class DNAMOTIF: | ||
37 | def __init__(self): | 37 | def __init__(self): | ||
38 | self.instances=[] | 38 | self.instances=[] | ||
39 | self.consensus=[] | 39 | self.consensus=[] | ||
40 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 40 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
41 | def __str__(self): | 41 | def __str__(self): | ||
n | 42 | string = '' | n | 42 | file = '' |
43 | for i in self.instances: | 43 | for line in self.instances: | ||
44 | string += i + "\n" | 44 | file += line | ||
45 | return string | 45 | return file | ||
46 | pass | ||||
47 | def __len__(self): | 46 | def __len__(self): | ||
48 | return len(self.instances[0])-1 | 47 | return len(self.instances[0])-1 | ||
49 | def count(self): | 48 | def count(self): | ||
n | 50 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | n | 49 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} |
51 | for j in range(len(self.instances[0])-1): | 50 | for i in range(len(self.instances[0])-1): | ||
52 | for key in self.counts.keys(): | 51 | for key in self.counts.keys(): | ||
53 | self.counts[key].append(0) | 52 | self.counts[key].append(0) | ||
54 | for strand in self.instances: | 53 | for line in self.instances: | ||
55 | if strand[j].upper() == merc: | 54 | if line[i].upper()==key: | ||
56 | self.counts[key][j] += 1 | 55 | self.counts[key][i]+=1 | ||
57 | return self.counts | 56 | return self.counts | ||
58 | pass | ||||
59 | def compute_consensus(self): | 57 | def compute_consensus(self): | ||
60 | self.consensus = [] | 58 | self.consensus = [] | ||
61 | self.counts = self.count() | 59 | self.counts = self.count() | ||
n | 62 | for cons in range(len(self.instances[0])-1): | n | 60 | for charPos in range(len(self.instances[0])-1): |
63 | self.consensus.append(max(self.counts)) | 61 | self.consensus.append(max(self.counts, key=lambda x: self.counts[x][ | ||
> | charPos]).upper()) | ||||
64 | self.consensus = str(''.join(self.consensus)) | 62 | self.consensus = str(''.join(self.consensus)) | ||
65 | def parse(self, filename): | 63 | def parse(self, filename): | ||
t | 66 | file=open(filename , 'r') | t | 64 | file1 = open(filename, 'r') |
67 | strands = file.readlines() | 65 | Lines = file1.readlines() | ||
68 | for strand in strands: | 66 | for line in Lines: | ||
69 | if not strand.startswith('>'): | 67 | if not line.startswith('>'): | ||
70 | self.instances.append(strand) | 68 | self.instances.append(line) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | import numpy as np | n | ||
3 | from math import sin, cos | 2 | from math import sin, cos, radians | ||
4 | class PLANT: | 3 | class PLANT: | ||
n | 5 | def __init__(self, init, gen, h, delta_theta): | n | 4 | def __init__(self, init, gen, n, delta_theta): |
6 | self.init_state = init | 5 | self.init_state = init | ||
7 | self.generator = gen | 6 | self.generator = gen | ||
n | 8 | self.h = h | n | 7 | self.n = n |
9 | self.delta_theta = delta_theta | 8 | self.delta_theta = delta_theta | ||
10 | self.str = self.generate() | 9 | self.str = self.generate() | ||
11 | def generate(self): | 10 | def generate(self): | ||
12 | result = self.init_state | 11 | result = self.init_state | ||
n | 13 | for i in range(self.h): | n | 12 | for i in range(self.n): |
14 | easy_net = "" | 13 | new_res = "" | ||
15 | for bet in result: | 14 | for char in result: | ||
16 | if bet in self.generator: | 15 | if char in self.generator: | ||
17 | easy_net += self.generator[bet] | 16 | new_res += self.generator[char] | ||
18 | else: | 17 | else: | ||
n | 19 | easy_net += bet | n | 18 | new_res += char |
20 | result = easy_net | 19 | result = new_res | ||
21 | return result | 20 | return result | ||
22 | def drawPlant(self): | 21 | def drawPlant(self): | ||
23 | currentPt = (200, 0) | 22 | currentPt = (200, 0) | ||
24 | theta = 90 | 23 | theta = 90 | ||
25 | stack = [] | 24 | stack = [] | ||
n | 26 | for bet in self.str: | n | 25 | for char in self.str: |
27 | if bet.isalpha(): | 26 | if char.isalpha(): | ||
28 | nextPt = (currentPt[0] +np.cos(np.radians(theta)), currentPt[1] | 27 | nextPt = (currentPt[0] + cos(radians(theta)), currentPt[1] + sin | ||
> | + np.sin(np.radians(theta))) | > | (radians(theta))) | ||
29 | plt.plot( | 28 | plt.plot( | ||
30 | [currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], color= | 29 | [currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], color= | ||
> | "black" | > | "black" | ||
31 | ) | 30 | ) | ||
32 | currentPt = nextPt | 31 | currentPt = nextPt | ||
n | 33 | elif bet == "[": | n | 32 | elif char == "[": |
34 | stack.append((currentPt, theta)) | 33 | stack.append([currentPt, theta]) | ||
35 | elif bet == "]": | 34 | elif char == "]": | ||
36 | currentPt,theta = stack.pop() | 35 | [currentPt, theta] = stack[-1] | ||
37 | elif bet == "+": | 36 | elif char == "+": | ||
38 | theta += self.delta_theta | 37 | theta += self.delta_theta | ||
n | 39 | elif bet == "-": | n | 38 | elif char == "-": |
40 | theta -= self.delta_theta | 39 | theta -= self.delta_theta | ||
n | 41 | class DNAMOTIF: | n | 40 | plt.axis("image")class DNAMOTIF: |
42 | def __init__(self): | 41 | def __init__(self): | ||
43 | self.instances = [] | 42 | self.instances = [] | ||
44 | self.consensus = [] | 43 | self.consensus = [] | ||
45 | self.counts = {"A": [], "C": [], "G": [], "T": []} | 44 | self.counts = {"A": [], "C": [], "G": [], "T": []} | ||
46 | def __str__(self): | 45 | def __str__(self): | ||
n | 47 | return "".join([str(stuff) for item in self.instances]) | n | 46 | return "".join([str(item) for item in self.instances]) |
48 | def __len__(self): | 47 | def __len__(self): | ||
49 | return len(self.instances[0].strip("\n")) | 48 | return len(self.instances[0].strip("\n")) | ||
50 | def count(self): | 49 | def count(self): | ||
51 | for key in self.counts: | 50 | for key in self.counts: | ||
52 | self.counts[key] = [0] * len(self) | 51 | self.counts[key] = [0] * len(self) | ||
n | 53 | for sinstances in self.instances: | n | 52 | for instance in self.instances: |
54 | sinstances = sinstances.strip(" ") | 53 | instance = instance.strip("\n") | ||
55 | for i in range(len(self)): | 54 | for i in range(len(self)): | ||
n | 56 | if sinstances[i].upper() in self.counts: | n | 55 | if instance[i].upper() in self.counts: |
57 | self.counts[sinstances[i].upper()][i] += 1 | 56 | self.counts[instance[i].upper()][i] += 1 | ||
58 | def compute_consensus(self): | 57 | def compute_consensus(self): | ||
59 | self.count() | 58 | self.count() | ||
n | 60 | self.consensus = [0] * len(self.counts['G']) | n | 59 | self.consensus = [0] * len(self.counts['A']) |
61 | for i in range(len(self.counts['G'])): | 60 | for i in range(len(self.counts['A'])): | ||
62 | Value = max(self.counts, key=lambda x: self.counts[x][i]) | 61 | keymax = max(self.counts, key=lambda x: self.counts[x][i]) | ||
63 | self.consensus[i] = Value | 62 | self.consensus[i] = keymax | ||
64 | self.consensus = "".join([str(item) for item in self.consensus]) | 63 | self.consensus = "".join([str(item) for item in self.consensus]) | ||
65 | def parse(self, filename): | 64 | def parse(self, filename): | ||
t | 66 | with open (filename,'r') as f: | t | 65 | with open(filename) as fp: |
67 | for i in f: | 66 | for line in fp: | ||
68 | if '>' in i: | 67 | if line[0] != ">": | ||
69 | continue | ||||
70 | else: | ||||
71 | self.instances.append(i) | 68 | self.instances.append("{}\n".format(line.strip())) | ||
72 | if __name__ == "__main__": | ||||
73 | dna1 = DNAMOTIF() | ||||
74 | dna1.parse("lexA.fasta") | ||||
75 | dna1.count() | ||||
76 | print(dna1.instances) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | 2 | import numpy as np | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
5 | self.string = string | 5 | self.string = string | ||
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | self.str = PLANT.generator(string,dictionary, n) | 9 | self.str = PLANT.generator(string,dictionary, n) | ||
10 | def generator(string, dictionary, n): | 10 | def generator(string, dictionary, n): | ||
11 | def character_v(string): | 11 | def character_v(string): | ||
12 | if string in dictionary: | 12 | if string in dictionary: | ||
13 | return dictionary.get(string) | 13 | return dictionary.get(string) | ||
14 | return string | 14 | return string | ||
n | 15 | stringslist = [string] | n | 15 | substrings_list = [string] |
16 | for i in range(n): | 16 | for i in range(n): | ||
n | 17 | string = stringslist[-1] | n | 17 | substring = substrings_list[-1] |
18 | newstring = [character_v(char) for char in string] | 18 | new_statement = [character_v(char) for char in substring] | ||
19 | stringslist.append(''.join(newstring)) | 19 | substrings_list.append(''.join(new_statement)) | ||
20 | newstring = stringslist[n] | 20 | newstring = substrings_list[n] | ||
21 | return newstring | 21 | return newstring | ||
22 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | 23 | upperletters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', ' | n | 23 | upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', |
> | L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | > | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||
24 | startpoint=(200,0) | 24 | currentPt=(200,0) | ||
25 | theta = np.radians(90) | 25 | theta = np.radians(90) | ||
26 | deltaTheta = np.radians(self.deltaTheta) | 26 | deltaTheta = np.radians(self.deltaTheta) | ||
n | 27 | stack = [startpoint, theta] | n | 27 | stack = [currentPt, theta] |
28 | for com in self.str: | 28 | for command in self.str: | ||
29 | if com.upper() in upperletters: | 29 | if command.upper() in upper_letters: | ||
30 | nextpoint = (startpoint[0]+(np.cos(theta)), startpoint[1]+(np.si | 30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | ||
> | n(theta))) | > | ta))) | ||
31 | plt.plot([startpoint[0],nextpoint[0]],[startpoint[1],nextpoint[1 | 31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ]],color='black') | > | ='black') | ||
32 | startpoint = nextpoint | 32 | currentPt = nextPt | ||
33 | else: | 33 | else: | ||
n | 34 | if com == '[': | n | 34 | if command == '[': |
35 | stack.append(startpoint) | 35 | stack.append(currentPt) | ||
36 | stack.append(theta) | 36 | stack.append(theta) | ||
n | 37 | elif com == ']': | n | 37 | elif command == ']': |
38 | theta = stack.pop() | 38 | theta = stack.pop() | ||
n | 39 | startpoint = stack.pop() | n | 39 | currentPt = stack.pop() |
40 | elif com == '-': | 40 | elif command == '-': | ||
41 | theta -= deltaTheta | 41 | theta -= deltaTheta | ||
n | 42 | elif com == '+': | n | 42 | elif command == '+': |
43 | theta += deltaTheta | 43 | theta += deltaTheta | ||
44 | return plt.plot | 44 | return plt.plot | ||
45 | if __name__ == "__main__": | 45 | if __name__ == "__main__": | ||
46 | string = input() | 46 | string = input() | ||
47 | dictionary = input() | 47 | dictionary = input() | ||
48 | n = int(input()) | 48 | n = int(input()) | ||
49 | deltaTheta = float(input()) | 49 | deltaTheta = float(input()) | ||
50 | p = PLANT(string, dictionary, n, deltaTheta) | 50 | p = PLANT(string, dictionary, n, deltaTheta) | ||
51 | p.drawPlant() | 51 | p.drawPlant() | ||
52 | plt.show()class DNAMOTIF: | 52 | plt.show()class DNAMOTIF: | ||
53 | def __init__(self): | 53 | def __init__(self): | ||
n | 54 | self.instances=[] | n | 54 | self.instances = [] |
55 | self.consensus=[] | 55 | self.consensus = [] | ||
56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 56 | self.counts = {'A': [], 'C': [], 'G':[],'T':[]} | ||
57 | def __str__(self): | 57 | def __str__(self): | ||
n | 58 | pass | n | 58 | output = '' |
59 | string = '' | ||||
60 | for i in self.instances: | 59 | for instance in self.instances: | ||
61 | string += i | 60 | output += instance | ||
62 | return string | 61 | return output | ||
63 | def __len__(self): | 62 | def __len__(self): | ||
64 | return len(self.instances[0].rstrip()) | 63 | return len(self.instances[0].rstrip()) | ||
65 | def count(self): | 64 | def count(self): | ||
n | 66 | pass | n | ||
67 | for i in range(len(self)): | 65 | for position in range(len(self)): | ||
68 | numA = 0 | 66 | sequenceA = 0 | ||
69 | numC= 0 | ||||
70 | numT = 0 | 67 | sequenceT = 0 | ||
68 | sequenceC = 0 | ||||
71 | numG = 0 | 69 | sequenceG = 0 | ||
72 | for j in self.instances: | 70 | for instance in self.instances: | ||
73 | seq = j.rstrip() | 71 | sequence = instance.rstrip() | ||
74 | if (seq[i]).upper() == 'A': | 72 | if (sequence[position]).upper() == 'A': | ||
75 | numA += 1 | 73 | sequenceA += 1 | ||
76 | elif (seq[i]).upper() == 'C': | ||||
77 | numC += 1 | ||||
78 | elif (seq[i]).upper() == 'G': | ||||
79 | numG += 1 | ||||
80 | elif (seq[i]).upper() == 'T': | 74 | elif (sequence[position]).upper() == 'T': | ||
81 | numT += 1 | 75 | sequenceT += 1 | ||
76 | elif (sequence[position]).upper() == 'C': | ||||
77 | sequenceC += 1 | ||||
78 | elif (sequence[position]).upper() == 'G': | ||||
79 | sequenceG += 1 | ||||
82 | self.counts.get('A').append(numA) | 80 | self.counts.get('A').append(sequenceA) | ||
83 | self.counts.get('C').append(numC) | ||||
84 | self.counts.get('T').append(numT) | 81 | self.counts.get('T').append(sequenceT) | ||
82 | self.counts.get('C').append(sequenceC) | ||||
85 | self.counts.get('G').append(numG) | 83 | self.counts.get('G').append(sequenceG) | ||
86 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
n | 87 | pass | n | ||
88 | DNAMOTIF.count(self) | 85 | DNAMOTIF.count(self) | ||
n | 89 | As = self.counts.get('A') | n | 86 | A = self.counts.get('A') |
90 | Cs = self.counts.get('C') | ||||
91 | Ts = self.counts.get('T') | 87 | T = self.counts.get('T') | ||
88 | C = self.counts.get('C') | ||||
92 | Gs = self.counts.get('G') | 89 | G = self.counts.get('G') | ||
93 | final = '' | 90 | output = "" | ||
94 | for i in range(len(As)): | 91 | for row in range(len(A)): | ||
95 | if (As[i] >= Cs[i] and As[i] >= Gs[i] and As[i] >= Ts[i]): | 92 | maxes = max(A[row],T[row],C[row],G[row]) | ||
93 | if maxes == A[row]: | ||||
96 | final += 'A' | 94 | output += 'A' | ||
97 | elif (Cs[i] >= Gs[i] and Cs[i] >= Ts[i]): | 95 | elif maxes == T[row]: | ||
98 | final += 'C' | ||||
99 | elif(Gs[i] >= Ts[i]): | ||||
100 | final += 'G' | ||||
101 | else: | ||||
102 | final += 'T' | 96 | output += 'T' | ||
97 | elif maxes == C[row]: | ||||
98 | output += 'C' | ||||
99 | elif maxes == G[row]: | ||||
100 | output += 'G' | ||||
103 | self.consensus = final | 101 | self.consensus = output | ||
104 | def parse(self, filename): | 102 | def parse(self, filename): | ||
n | 105 | with open(filename,'r') as file: | n | 103 | myFile = open(filename, 'r') |
106 | for i in file: | 104 | contents = [line for line in myFile.readlines()] | ||
107 | if '>' in i: | 105 | myFile.close() | ||
108 | continue | 106 | for index, line in enumerate(contents): | ||
109 | else: | 107 | if index % 2 != 0: | ||
110 | self.instances.append(i) | 108 | self.instances.append(line) | ||
111 | if __name__ == '__main__': | 109 | if __name__ == '__main__': | ||
112 | lexA=DNAMOTIF() | 110 | lexA=DNAMOTIF() | ||
t | 113 | filename = r'FinalProject\lexA.fasta' | t | 111 | filename = r'FinalProject\lexA.fasta' |
114 | lexA.parse(filename) | 112 | lexA.parse(filename) | ||
115 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
116 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | class DNAMOTIF: | n | 1 | import matplotlib.pyplot as pltclass DNAMOTIF: |
2 | def __init__(self): | 2 | def __init__(self): | ||
3 | self.instances=[] | 3 | self.instances=[] | ||
4 | self.consensus=[] | 4 | self.consensus=[] | ||
5 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 5 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
6 | def __str__(self): | 6 | def __str__(self): | ||
7 | string = "" | 7 | string = "" | ||
n | 8 | for i in self.instances: | n | 8 | for n in self.instances: |
9 | string += i + "\n" | 9 | string = string + n | ||
10 | return string[:-2] | 10 | return string | ||
11 | def __len__(self): | 11 | def __len__(self): | ||
n | 12 | return len(self.instances) - 4 | n | 12 | return len(self.instances[0]) -1 |
13 | def count(self): | 13 | def count(self): | ||
n | 14 | for i in self.instances: | n | 14 | for n in self.instances: |
15 | temp = i.upper() | 15 | times = n.upper() | ||
16 | self.counts["A"].append(temp.count("A")) | 16 | self.counts['A'].append(times.count("A") + times.count('a')) | ||
17 | self.counts["C"].append(temp.count("C")) | 17 | self.counts["C"].append(times.count("C") + times.count('c')) | ||
18 | self.counts["G"].append(temp.count("G")) | 18 | self.counts["G"].append(times.count("G") + times.count('g')) | ||
19 | self.counts["T"].append(temp.count("T")) | 19 | self.counts["T"].append(times.count("T") + times.count('t')) | ||
20 | def compute_consensus(self): | 20 | def compute_consensus(self): | ||
n | 21 | A = (self.counts["A"]) | n | 21 | A = self.counts["A"] |
22 | C = (self.counts["C"]) | 22 | C = self.counts["C"] | ||
23 | G = (self.counts["G"]) | 23 | G = self.counts["G"] | ||
24 | T = (self.counts["T"]) | 24 | T = self.counts["T"] | ||
25 | for i in range(len(A)): | 25 | for n in range(len(A)): | ||
26 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 26 | if(G[n] <= A[n] and C[n] <= A[n] and T[n] <= A[n]): | ||
27 | self.consensus.append("A") | 27 | self.consensus = self.consensus + 'A' | ||
28 | elif (C[i] >= G[i] and C[i] >= T[i]): | 28 | elif (G[n] <= C[n] and T[n] <= C[n]): | ||
29 | self.consensus.append("C") | 29 | self.consensus = self.consensus + 'C' | ||
30 | elif (G[i] >= T[i]): | 30 | elif (T[n] <= G[n]): | ||
31 | self.consensus.append("G") | 31 | self.consensus = self.consensus + 'G' | ||
32 | else: | 32 | else: | ||
n | 33 | self.consensus.append("T") | n | 33 | self.consensus = self.consensus + 'T' |
34 | def parse(self, filename): | 34 | def parse(self, filename): | ||
35 | with open(filename,'r') as f: | 35 | with open(filename,'r') as f: | ||
n | 36 | for i in f: | n | 36 | for n in f: |
37 | if ">" in i: | 37 | if n[0] == '>': | ||
38 | continue | 38 | continue | ||
39 | else: | 39 | else: | ||
t | 40 | self.instances.append(i) | t | 40 | self.instances.append(n) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import matplotlib.pyplot as pltclass DNAMOTIF: | n | 1 | class DNAMOTIF: |
2 | def __init__(self): | 2 | def __init__(self): | ||
3 | self.instances=[] | 3 | self.instances=[] | ||
4 | self.consensus=[] | 4 | self.consensus=[] | ||
5 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 5 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
6 | def __str__(self): | 6 | def __str__(self): | ||
7 | string = "" | 7 | string = "" | ||
n | 8 | for n in self.instances: | n | 8 | for i in self.instances: |
9 | string = string + n | 9 | string += i + "\n" | ||
10 | return string | 10 | return string[:-2] | ||
11 | def __len__(self): | 11 | def __len__(self): | ||
n | 12 | return len(self.instances[0]) -1 | n | 12 | return len(self.instances) - 4 |
13 | def count(self): | 13 | def count(self): | ||
n | 14 | for n in self.instances: | n | 14 | for i in self.instances: |
15 | times = n.upper() | 15 | temp = i.upper() | ||
16 | self.counts['A'].append(times.count("A") + times.count('a')) | 16 | self.counts["A"].append(temp.count("A")) | ||
17 | self.counts["C"].append(times.count("C") + times.count('c')) | 17 | self.counts["C"].append(temp.count("C")) | ||
18 | self.counts["G"].append(times.count("G") + times.count('g')) | 18 | self.counts["G"].append(temp.count("G")) | ||
19 | self.counts["T"].append(times.count("T") + times.count('t')) | 19 | self.counts["T"].append(temp.count("T")) | ||
20 | def compute_consensus(self): | 20 | def compute_consensus(self): | ||
n | 21 | A = self.counts["A"] | n | 21 | A = (self.counts["A"]) |
22 | C = self.counts["C"] | 22 | C = (self.counts["C"]) | ||
23 | G = self.counts["G"] | 23 | G = (self.counts["G"]) | ||
24 | T = self.counts["T"] | 24 | T = (self.counts["T"]) | ||
25 | for n in range(len(A)): | 25 | for i in range(len(A)): | ||
26 | if(G[n] <= A[n] and C[n] <= A[n] and T[n] <= A[n]): | 26 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
27 | self.consensus = self.consensus + 'A' | 27 | self.consensus.append("A") | ||
28 | elif (G[n] <= C[n] and T[n] <= C[n]): | 28 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
29 | self.consensus = self.consensus + 'C' | 29 | self.consensus.append("C") | ||
30 | elif (T[n] <= G[n]): | 30 | elif (G[i] >= T[i]): | ||
31 | self.consensus = self.consensus + 'G' | 31 | self.consensus.append("G") | ||
32 | else: | 32 | else: | ||
n | 33 | self.consensus = self.consensus + 'T' | n | 33 | self.consensus.append("T") |
34 | def parse(self, filename): | 34 | def parse(self, filename): | ||
35 | with open(filename,'r') as f: | 35 | with open(filename,'r') as f: | ||
n | 36 | for n in f: | n | 36 | for i in f: |
37 | if n[0] == '>': | 37 | if ">" in i: | ||
38 | continue | 38 | continue | ||
39 | else: | 39 | else: | ||
t | 40 | self.instances.append(n) | t | 40 | self.instances.append(i) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | 2 | import numpy as np | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
5 | self.string = string | 5 | self.string = string | ||
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
n | 9 | self.str = PLANT.output(self) | n | 9 | self.str = PLANT.generator(string,dictionary, n) |
10 | def recurse(currentInput, givenDictionary, maxIteration, currentIteration=0) | 10 | def generator(string, dictionary, n): | ||
> | : | ||||
11 | nextInput = "" | 11 | def character_v(string): | ||
12 | keys = list(givenDictionary.keys()) | 12 | if string in dictionary: | ||
13 | for instance in currentInput: | 13 | return dictionary.get(string) | ||
14 | if instance in keys: | 14 | return string | ||
15 | if instance == keys[0]: | 15 | substrings_list = [string] | ||
16 | nextInput += givenDictionary.get(instance) | 16 | for i in range(n): | ||
17 | elif instance == keys[1]: | 17 | substring = substrings_list[-1] | ||
18 | nextInput += givenDictionary.get(instance) | 18 | new_statement = [character_v(char) for char in substring] | ||
19 | substrings_list.append(''.join(new_statement)) | ||||
20 | newstring = substrings_list[n] | ||||
21 | return newstring | ||||
22 | def drawPlant(self): | ||||
23 | upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', | ||||
> | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||||
24 | currentPt=(200,0) | ||||
25 | theta = np.radians(90) | ||||
26 | deltaTheta = np.radians(self.deltaTheta) | ||||
27 | stack = [currentPt, theta] | ||||
28 | for command in self.str: | ||||
29 | if command.upper() in upper_letters: | ||||
30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | ||||
> | ta))) | ||||
31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||||
> | ='black') | ||||
32 | currentPt = nextPt | ||||
19 | else: | 33 | else: | ||
n | 20 | nextInput +=instance | n | ||
21 | if currentIteration < maxIteration-1: | ||||
22 | return PLANT.recurse(nextInput, givenDictionary,maxIteration, curren | ||||
> | tIteration+1) | ||||
23 | else: | ||||
24 | return nextInput | ||||
25 | def drawPlant(self): | ||||
26 | currPt = (200,0) | ||||
27 | theta = np.radians(90) | ||||
28 | dtheta = np.radians(self.deltaTheta) | ||||
29 | setStack = [currPt, theta] | ||||
30 | for command in self.str: | ||||
31 | if "A"<= command.upper() <= "Z": | ||||
32 | nextPt = (currPt[0]+np.cos(theta)), currPt[1]+np.sin(theta) | ||||
33 | plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='blac | ||||
> | k') | ||||
34 | currPt = nextPt | ||||
35 | else: | ||||
36 | if command == "+": | 34 | if command == '[': | ||
37 | theta += dtheta | 35 | stack.append(currentPt) | ||
38 | elif command == "-": | 36 | stack.append(theta) | ||
39 | theta -= dtheta | ||||
40 | elif command == "]": | ||||
41 | theta = setStack.pop() | ||||
42 | currPt = setStack.pop() | ||||
43 | elif command == '[': | 37 | elif command == ']': | ||
44 | setStack.append(currPt) | ||||
45 | setStack.append(theta) | 38 | theta = stack.pop() | ||
39 | currentPt = stack.pop() | ||||
40 | elif command == '-': | ||||
41 | theta -= deltaTheta | ||||
42 | elif command == '+': | ||||
43 | theta += deltaTheta | ||||
46 | return plt.plot | 44 | return plt.plot | ||
n | 47 | def output(self): | n | ||
48 | return PLANT.recurse(self.string, self.dictionary, self.n) | ||||
49 | if __name__ == '__main__': | 45 | if __name__ == "__main__": | ||
50 | inputString = input() | 46 | string = input() | ||
51 | inputDiction = input() | 47 | dictionary = input() | ||
52 | inputIteration = int(input()) | 48 | n = int(input()) | ||
53 | inputAngle = float(input()) | 49 | deltaTheta = float(input()) | ||
54 | occurance=PLANT(inputString, inputDiction, inputIteration, inputAngle) | 50 | p = PLANT(string, dictionary, n, deltaTheta) | ||
55 | occurance.drawPlant() | 51 | p.drawPlant() | ||
56 | plt.show() | 52 | plt.show()class DNAMOTIF: | ||
57 | class DNAMOTIF: | ||||
58 | def __init__(self): | 53 | def __init__(self): | ||
59 | self.instances=[] | 54 | self.instances=[] | ||
60 | self.consensus=[] | 55 | self.consensus=[] | ||
61 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
62 | def __str__(self): | 57 | def __str__(self): | ||
63 | output = "" | 58 | output = "" | ||
64 | for instance in self.instances: | 59 | for instance in self.instances: | ||
65 | output += instance | 60 | output += instance | ||
66 | return output | 61 | return output | ||
67 | def __len__(self): | 62 | def __len__(self): | ||
68 | return len(self.instances[0].rstrip()) | 63 | return len(self.instances[0].rstrip()) | ||
69 | def count(self): | 64 | def count(self): | ||
70 | for position in range(len(self)): | 65 | for position in range(len(self)): | ||
71 | sequenceA = 0 | 66 | sequenceA = 0 | ||
72 | sequenceT = 0 | 67 | sequenceT = 0 | ||
73 | sequenceC = 0 | 68 | sequenceC = 0 | ||
74 | sequenceG = 0 | 69 | sequenceG = 0 | ||
75 | for instance in self.instances: | 70 | for instance in self.instances: | ||
76 | sequence = instance.rstrip() | 71 | sequence = instance.rstrip() | ||
77 | if (sequence[position]).upper() == "A": | 72 | if (sequence[position]).upper() == "A": | ||
78 | sequenceA += 1 | 73 | sequenceA += 1 | ||
79 | elif (sequence[position]).upper() == "T": | 74 | elif (sequence[position]).upper() == "T": | ||
80 | sequenceT += 1 | 75 | sequenceT += 1 | ||
81 | elif (sequence[position]).upper() == "C": | 76 | elif (sequence[position]).upper() == "C": | ||
82 | sequenceC += 1 | 77 | sequenceC += 1 | ||
83 | elif (sequence[position]).upper() == "G": | 78 | elif (sequence[position]).upper() == "G": | ||
84 | sequenceG += 1 | 79 | sequenceG += 1 | ||
85 | self.counts.get("A").append(sequenceA) | 80 | self.counts.get("A").append(sequenceA) | ||
86 | self.counts.get("T").append(sequenceT) | 81 | self.counts.get("T").append(sequenceT) | ||
87 | self.counts.get("C").append(sequenceC) | 82 | self.counts.get("C").append(sequenceC) | ||
88 | self.counts.get("G").append(sequenceG) | 83 | self.counts.get("G").append(sequenceG) | ||
89 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
90 | DNAMOTIF.count(self) | 85 | DNAMOTIF.count(self) | ||
91 | A = self.counts.get("A") | 86 | A = self.counts.get("A") | ||
92 | T = self.counts.get("T") | 87 | T = self.counts.get("T") | ||
93 | C = self.counts.get("C") | 88 | C = self.counts.get("C") | ||
94 | G = self.counts.get("G") | 89 | G = self.counts.get("G") | ||
95 | output = "" | 90 | output = "" | ||
96 | for row in range(len(A)): | 91 | for row in range(len(A)): | ||
n | 97 | maximum = max(A[row],T[row],C[row],G[row]) | n | 92 | maxs = max(A[row],T[row],C[row],G[row]) |
98 | if maximum == A[row]: | 93 | if maxs == A[row]: | ||
99 | output += "A" | 94 | output += "A" | ||
n | 100 | elif maximum == T[row]: | n | 95 | elif maxs == T[row]: |
101 | output += "T" | 96 | output += "T" | ||
n | 102 | elif maximum == C[row]: | n | 97 | elif maxs == C[row]: |
103 | output += "C" | 98 | output += "C" | ||
n | 104 | elif maximum == G[row]: | n | 99 | elif maxs == G[row]: |
105 | output += "G" | 100 | output += "G" | ||
106 | self.consensus = output | 101 | self.consensus = output | ||
107 | def parse(self, filename): | 102 | def parse(self, filename): | ||
108 | myFile = open(filename, 'r') | 103 | myFile = open(filename, 'r') | ||
109 | contents = [line for line in myFile.readlines()] | 104 | contents = [line for line in myFile.readlines()] | ||
110 | myFile.close() | 105 | myFile.close() | ||
111 | for index, line in enumerate(contents): | 106 | for index, line in enumerate(contents): | ||
112 | if index % 2 != 0: | 107 | if index % 2 != 0: | ||
113 | self.instances.append(line) | 108 | self.instances.append(line) | ||
114 | if __name__ == '__main__': | 109 | if __name__ == '__main__': | ||
115 | lexA=DNAMOTIF() | 110 | lexA=DNAMOTIF() | ||
t | 116 | filename = r'FinalProject\lexA.fasta' | t | 111 | filename = r'FinalProject\lexA.fasta' |
117 | lexA.parse(filename) | 112 | lexA.parse(filename) | ||
118 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
119 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | import math | ||
1 | import math, matplotlib.pyplot as plt, numpy as np | 2 | import matplotlib.pyplot as plt | ||
2 | class PLANT: | 3 | class PLANT: | ||
n | 3 | def __init__(self, initial_state, generator, n, deltaTheta): | n | 4 | def __init__(self, string, dictionary, n, deltaTheta): |
4 | self.str = initial_state | 5 | self.str = string | ||
5 | self.generator = generator | 6 | self.dictionary = dictionary | ||
6 | self.n = n | 7 | self.n = n | ||
7 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
8 | for i in range(n): | 9 | for i in range(n): | ||
n | 9 | temp = '' | n | 10 | new_string = "" |
10 | for j in self.str: | 11 | for letter in self.str: | ||
11 | if j in generator.keys(): | 12 | if letter in self.dictionary: | ||
12 | temp += generator[j] | 13 | new_string += self.dictionary[letter] | ||
13 | else: | 14 | else: | ||
n | 14 | temp += j | n | 15 | new_string += letter |
15 | self.str = temp | 16 | self.str = new_string | ||
16 | def drawPlant(self): | 17 | def drawPlant(self): | ||
17 | currentPt = (200,0) | 18 | currentPt = (200,0) | ||
n | n | 19 | stack = [] | ||
18 | theta = 90 | 20 | theta = 90 | ||
n | 19 | stack = [] | n | ||
20 | for i in self.str: | 21 | for letter in self.str: | ||
21 | if i.isalpha(): | ||||
22 | nextPt = (currentPt[0] + math.cos(theta), currentPt[1] + math.si | ||||
> | n(theta)) | ||||
23 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||||
> | ='black') | ||||
24 | currentPt = nextPt | ||||
25 | elif i == '[': | 22 | if letter == "[": | ||
26 | stack.append(currentPt) | 23 | stack.append([currentPt,theta]) | ||
27 | stack.append(theta) | ||||
28 | elif i == ']': | 24 | elif letter == "]": | ||
29 | curren_sav = stack.pop() | 25 | current_save = stack.pop() | ||
30 | theta = curren_sav[1] | 26 | theta = current_save[1] | ||
31 | currentPt = curren_sav[0] | 27 | currentPt = current_save[0] | ||
32 | elif i == '+': | 28 | elif letter == "+": | ||
33 | theta += self.deltaTheta | 29 | theta += self.deltaTheta | ||
n | 34 | elif i == '-': | n | 30 | elif letter == "-": |
35 | theta -= self.deltaTheta | 31 | theta -= self.deltaTheta | ||
n | 36 | import numpy as np | n | 32 | else: |
37 | class DNAMOTIF: | 33 | nextPt = (currentPt[0] + math.cos(math.radians(theta)), currentP | ||
> | t[1] + math.sin(math.radians(theta))) | ||||
34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor='black') | ||||
35 | currentPt = nextPtclass DNAMOTIF: | ||||
38 | def __init__(self): | 36 | def __init_(self): | ||
39 | self.instances = [] | 37 | self.instances = [] | ||
40 | self.consensus = [] | 38 | self.consensus = [] | ||
41 | self.counts = {"A": [], "C": [], "G": [], "T": []} | 39 | self.counts = {"A": [], "C": [], "G": [], "T": []} | ||
42 | def __str__(self): | 40 | def __str__(self): | ||
n | 43 | strin = 0 | n | ||
44 | return "\n".join(self.instances) | 41 | return "\n".join(self.instances) | ||
45 | def __len__(self): | 42 | def __len__(self): | ||
n | 46 | l = len(self.instances[0])-1 | n | 43 | return len(self.instances[0])-1 |
47 | return l | ||||
48 | def parse(self, filename): | 44 | def parse(self, filename): | ||
n | 49 | lil = 0 | n | ||
50 | with open(filename, 'r') as re: | 45 | with open(filename, 'r') as reader: | ||
51 | self.instances = [i[0].lower()+i[1:].upper() for i in re.readlines() | 46 | self.instances = [i[0].lower()+i[1:].upper() for i in reader.readlin | ||
> | if i[0] != ">"] | > | es() if i[0] != ">"] | ||
52 | def count(self): | 47 | def count(self): | ||
n | 53 | cou = 0 | n | ||
54 | ou = 1 | ||||
55 | self.counts = {"A": [0 for _ in range(len(self))], "C": [0 for _ in rang | 48 | self.counts = {"A": [0 for _ in range(len(self))], "C": [0 for _ in rang | ||
> | e(len(self))], "G": [0 for _ in range(len(self))], "T": [0 for _ in range(len(se | > | e(len(self))], "G": [0 for _ in range(len(self))], "T": [0 for _ in range(len(se | ||
> | lf))]} | > | lf))]} | ||
56 | for instance in self.instances: | 49 | for instance in self.instances: | ||
57 | for i, letter in enumerate(instance): | 50 | for i, letter in enumerate(instance): | ||
58 | if letter != "\n": | 51 | if letter != "\n": | ||
59 | self.counts[letter.upper()][i] += 1 | 52 | self.counts[letter.upper()][i] += 1 | ||
60 | def compute_consensus(self): | 53 | def compute_consensus(self): | ||
61 | self.count() | 54 | self.count() | ||
n | 62 | cons = "" | n | 55 | consensus = "" |
63 | for x in range(len(self)): | 56 | for i in range(len(self)): | ||
64 | ct_consensus = ("",0) | 57 | current_consensus = ("",0) | ||
65 | for letter in self.counts: | 58 | for letter in self.counts: | ||
t | 66 | if self.counts[letter][x] > ct_consensus[1]: | t | 59 | if self.counts[letter][i] > current_consensus[1]: |
67 | ct_consensus = (letter, self.counts[letter][x]) | 60 | current_consensus = (letter, self.counts[letter][i]) | ||
68 | cons += ct_consensus[0] | 61 | consensus += current_consensus[0] | ||
69 | self.consensus = cons | 62 | self.consensus = consensus | ||
70 | if __name__ == '__main__': | ||||
71 | lexA = DNAMOTIF() | ||||
72 | lexA.parse("lexA.fasta") | ||||
73 | print(len(lexA)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | from math import pi, sin, cos, isnan | n | 2 | import math |
3 | import numpy as np | ||||
4 | class Plant: | ||||
3 | plt.style.use('bmh') | 5 | plt.style.use('bmh') | ||
4 | plt.axes().set_aspect('equal', 'datalim') | 6 | plt.plot( | ||
5 | plt.plot([0, 1, 2], [0, 1, | 7 | [0, 1, 2], | ||
6 | 0]) | 8 | [0, 1, 0] | ||
9 | ) | ||||
7 | plt.xlabel('x') | 10 | plt.xlabel('x') | ||
n | 8 | plt.ylabel('y') | n | 11 | plt.ylabel('y'); |
12 | def plot_coords(coords, bare_plot=False): | ||||
13 | if bare_plot: | ||||
14 | plt.axis('off') | ||||
15 | plt.axes().set_aspect('equal', 'datalim') | ||||
16 | X, Y = zip(*coords) | ||||
17 | plt.plot(X, Y); | ||||
18 | plot_coords([ | ||||
19 | (0, 0), | ||||
20 | (1, 0), | ||||
21 | (2, 1), | ||||
22 | (3, 1), | ||||
23 | (2, 0) | ||||
24 | ]) | ||||
9 | nan = float('nan') | 25 | nan = float('nan') | ||
n | 10 | ConvertDtoR = pi / 180 | n | 26 | plot_coords([ |
11 | class PLANT: | 27 | (0, 0), | ||
12 | def __init__(self, initial_state, generator, iterations, | 28 | (1, 1), | ||
13 | deltaTheta): | 29 | (nan, nan), | ||
14 | self.state = initial_state | 30 | (1, 0), | ||
15 | self.generator = generator | 31 | (2, 1) | ||
16 | self.iterations = iterations | 32 | ]) | ||
17 | self.deltaTheta = deltaTheta | 33 | from math import pi, sin, cos | ||
18 | self.str = transform_multiple(initial_state, generator, iterations) | 34 | DEGREES_TO_RADIANS = pi / 180 | ||
19 | def draw_plant(self): | ||||
20 | currentPt = (200,0) | ||||
21 | theta = 90 | ||||
22 | pass | ||||
23 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||||
24 | saved_states = list() | ||||
25 | state = (0, 0, 90) | ||||
26 | yield (0, 0) | ||||
27 | for command in turtle_program: | ||||
28 | x, y, angle = state | ||||
29 | if command.lower() in 'abcdefghij': | ||||
30 | state = (x - cos(angle * ConvertDtoR), y | ||||
31 | + sin(angle * ConvertDtoR), angle) | ||||
32 | if command.islower(): | ||||
33 | yield (float('nan'), float('nan')) | ||||
34 | yield (state[0], state[1]) | ||||
35 | elif command == '+': | ||||
36 | state = (x, y, angle + turn_amount) | ||||
37 | elif command == '-': | ||||
38 | state = (x, y, angle - turn_amount) | ||||
39 | elif command == '[': | ||||
40 | saved_states.append(state) | ||||
41 | elif command == ']': | ||||
42 | state = saved_states.pop() | ||||
43 | yield (float('nan'), float('nan')) | ||||
44 | x, y, _ = state | ||||
45 | yield (x, y) | ||||
46 | def turtle_to_coords(turtle_program, turn_amount=45): | 35 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
47 | state = (0.0, 0.0, 90.0) | 36 | state = (0.0, 0.0, 90.0) | ||
48 | yield (0.0, 0.0) | 37 | yield (0.0, 0.0) | ||
49 | for command in turtle_program: | 38 | for command in turtle_program: | ||
50 | x, y, angle = state | 39 | x, y, angle = state | ||
51 | if command in 'Ff': | 40 | if command in 'Ff': | ||
n | 52 | state = (x - cos(angle * ConvertDtoR), | n | 41 | state = (x - cos(angle * DEGREES_TO_RADIANS), |
53 | y + sin(angle * ConvertDtoR), | 42 | y + sin(angle * DEGREES_TO_RADIANS), | ||
54 | angle) | 43 | angle) | ||
55 | if command == 'f': | 44 | if command == 'f': | ||
56 | yield (float('nan'), float('nan')) | 45 | yield (float('nan'), float('nan')) | ||
57 | yield (state[0], state[1]) | 46 | yield (state[0], state[1]) | ||
58 | elif command == '+': | 47 | elif command == '+': | ||
59 | state = (x, y, angle + turn_amount) | 48 | state = (x, y, angle + turn_amount) | ||
60 | elif command == '-': | 49 | elif command == '-': | ||
61 | state = (x, y, angle - turn_amount) | 50 | state = (x, y, angle - turn_amount) | ||
n | 62 | def plot_coordinates(coords, bare_plot = False): | n | 51 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) |
63 | if bare_plot: | 52 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
64 | plt.axis('off') | 53 | from math import isnan | ||
65 | X, Y = zip(*coords) | ||||
66 | plt.plot(X, Y) | ||||
67 | def transform_sequence(sequence, transformations): | ||||
68 | return ''.join(transformations.get(c, c) | ||||
69 | for c in sequence) | ||||
70 | def transform_multiple(sequence, transformations, iterations): | ||||
71 | for _ in range(iterations): | ||||
72 | sequence = transform_sequence(sequence, transformations) | ||||
73 | return sequence | ||||
74 | def l_plot(axiom, transformations, iterations=0, | ||||
75 | angle=45): | ||||
76 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||||
77 | coords = branching_turtle_to_coords(turtle_program, angle) | ||||
78 | plot_coordinates(coords, bare_plot=True) | ||||
79 | def print_coords(coords): | 54 | def print_coords(coords): | ||
80 | for (x, y) in coords: | 55 | for (x, y) in coords: | ||
81 | if isnan(x): | 56 | if isnan(x): | ||
82 | print('<gap>') | 57 | print('<gap>') | ||
83 | else: | 58 | else: | ||
n | 84 | print('({:.2f}, {:.2f})'.format(x, | n | 59 | print('({:.2f}, {:.2f})'.format(x, y)) |
85 | y)) | ||||
86 | plot_coordinates([(0, 0), (1, 0), (2, | ||||
87 | 1), (3, 1), (2, 0)]) | ||||
88 | plot_coordinates([(0, 0), (1, 1), (nan, | ||||
89 | nan), (1, 0), (2, 1)]) | ||||
90 | plot_coordinates(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||||
91 | plot_coordinates(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||||
92 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | 60 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
n | 93 | plot_coordinates(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | n | 61 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) |
62 | def transform_sequence(sequence, transformations): | ||||
63 | return ''.join(transformations.get(c, c) for c in sequence) | ||||
94 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | 64 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | ||
n | 95 | plot_coordinates(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | n | 65 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) |
66 | def transform_multiple(sequence, transformations, iterations): | ||||
67 | for _ in range(iterations): | ||||
68 | sequence = transform_sequence(sequence, transformations) | ||||
69 | return sequence | ||||
96 | print('0:', transform_multiple('abba', {'b': 'bab'}, | 70 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | ||
97 | 0)) | ||||
98 | print('1:', transform_multiple('abba', {'b': 'bab'}, | 71 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
99 | 1)) | ||||
100 | print('2:', transform_multiple('abba', {'b': 'bab'}, | 72 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
101 | 2)) | ||||
102 | plot_coordinates(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, | 73 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
103 | 5))) | ||||
104 | plot_coordinates(turtle_to_coords(transform_multiple('L', {'L': '-RF+LFL+FR-','R | 74 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
> | ': | ||||
105 | '+LF-RFR-FL+'}, 5), 90)) | 75 | 'L': '-RF+LFL+FR-', | ||
76 | 'R': '+LF-RFR-FL+' | ||||
77 | }, 5), 90)) | ||||
78 | def branching_turtle_to_coords(turtle_program, turn_amount=45): | ||||
79 | saved_states = list() | ||||
80 | state = (0, 0, 90) | ||||
81 | yield (0, 0) | ||||
82 | for command in turtle_program: | ||||
83 | x, y, angle = state | ||||
84 | if command.lower() in 'abcdefghij': | ||||
85 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||||
86 | y + sin(angle * DEGREES_TO_RADIANS), | ||||
87 | angle) | ||||
88 | if command.islower(): | ||||
89 | yield (float('nan'), float('nan')) | ||||
90 | yield (state[0], state[1]) | ||||
91 | elif command == '+': | ||||
92 | state = (x, y, angle + turn_amount) | ||||
93 | elif command == '-': | ||||
94 | state = (x, y, angle - turn_amount) | ||||
95 | elif command == '[': | ||||
96 | saved_states.append(state) | ||||
97 | elif command == ']': | ||||
98 | state = saved_states.pop() | ||||
99 | yield (float('nan'), float('nan')) | ||||
100 | x, y, _ = state | ||||
101 | yield (x, y) | ||||
106 | plot_coordinates(branching_turtle_to_coords('F[-F]+F', 45)) | 102 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) | ||
103 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||||
104 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||||
105 | coords = branching_turtle_to_coords(turtle_program, angle) | ||||
106 | plot_coords(coords, bare_plot=True) | ||||
107 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | 107 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | ||
108 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 108 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
109 | for i in range(5): | 109 | for i in range(5): | ||
110 | print('{}: '.format(i), | 110 | print('{}: '.format(i), | ||
111 | transform_multiple('A', {'A': 'F+A'}, i)) | 111 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
112 | for i in range(5): | 112 | for i in range(5): | ||
113 | print('{}: '.format(i), | 113 | print('{}: '.format(i), | ||
n | 114 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, | n | 114 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) |
115 | i)) | ||||
116 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, | 115 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | ||
117 | 5, 22.5) | ||||
118 | class DNAMOTIF: | ||||
119 | def __init__(self): | 116 | def __init__(self): | ||
n | 120 | self.instances = [] | n | 117 | self.instances=[] |
121 | self.consensus = "" | 118 | self.consensus=[] | ||
122 | self.counts = {'A': [], 'C': [], 'G': [], 'T': []} | 119 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
123 | def __str__(self): | 120 | def __str__(self): | ||
n | 124 | motif_str = "" | n | 121 | result = "" |
125 | for instance in self.instances: | 122 | for instance in self.instances: | ||
n | 126 | motif_str += instance | n | 123 | result += instance |
127 | return motif_str | 124 | return result | ||
128 | def __len__(self): | 125 | def __len__(self): | ||
n | 129 | return len(self.instances[0].replace("\n", "")) | n | 126 | return len(self.instances[0].rstrip()) |
130 | def count(self): | 127 | def count(self): | ||
n | 131 | self.counts["A"] = [0] * len(self) | n | ||
132 | self.counts["C"] = [0] * len(self) | ||||
133 | self.counts["T"] = [0] * len(self) | ||||
134 | self.counts["G"] = [0] * len(self) | ||||
135 | for sequence in self.instances: | ||||
136 | for i in range(len(self)): | 128 | for position in range(len(self)): | ||
137 | self.counts[sequence[i].upper()][i] += 1 | 129 | TypeA = 0 | ||
130 | TypeT = 0 | ||||
131 | TypeC = 0 | ||||
132 | TypeG = 0 | ||||
133 | for instance in self.instances: | ||||
134 | order = instance.rstrip() | ||||
135 | if (order[position]).upper() == "A": | ||||
136 | TypeA += 1 | ||||
137 | elif (order[position]).upper() == "T": | ||||
138 | TypeT += 1 | ||||
139 | elif (order[position]).upper() == "C": | ||||
140 | TypeC += 1 | ||||
141 | elif (order[position]).upper() == "G": | ||||
142 | TypeG += 1 | ||||
143 | self.counts.get("A").append(TypeA) | ||||
144 | self.counts.get("T").append(TypeT) | ||||
145 | self.counts.get("C").append(TypeC) | ||||
146 | self.counts.get("G").append(TypeG) | ||||
138 | def compute_consensus(self): | 147 | def compute_consensus(self): | ||
n | 139 | self.count() | n | 148 | DNAMOTIF.count(self) |
149 | A = self.counts.get("A") | ||||
150 | T = self.counts.get("T") | ||||
151 | C = self.counts.get("C") | ||||
152 | G = self.counts.get("G") | ||||
153 | output = "" | ||||
140 | for i in range(len(self)): | 154 | for row in range(len(A)): | ||
141 | a_count = (self.counts["A"][i], "A") | 155 | maxs = max(A[row],T[row],C[row],G[row]) | ||
142 | c_count = (self.counts["C"][i], "C") | 156 | if maxs == A[row]: | ||
143 | g_count = (self.counts["G"][i], "G") | 157 | output += "A" | ||
144 | t_count = (self.counts["T"][i], "T") | 158 | elif maxs == T[row]: | ||
145 | nucleotides_at_positions = [a_count, c_count, g_count, t_count] | 159 | output += "T" | ||
146 | self.consensus += max(nucleotides_at_positions)[1] | 160 | elif maxs == C[row]: | ||
161 | output += "C" | ||||
162 | elif maxs == G[row]: | ||||
163 | output += "G" | ||||
164 | self.consensus = output | ||||
147 | def parse(self, filename): | 165 | def parse(self, filename): | ||
t | 148 | with open(filename, "r") as motif_file: | t | 166 | myFile = open(filename, 'r') |
149 | for line in motif_file.readlines(): | 167 | contents = [line for line in myFile.readlines()] | ||
150 | if ">" not in line: | 168 | myFile.close() | ||
169 | for index, line in enumerate(contents): | ||||
170 | if index % 2 != 0: | ||||
151 | self.instances.append(str(line)) | 171 | self.instances.append(line) | ||
152 | def main(): | 172 | if __name__ == '__main__': | ||
153 | LexA = DNAMOTIF() | 173 | lexA=DNAMOTIF() | ||
154 | LexA.parse("lexA.fasta") | 174 | filename = r'FinalProject\lexA.fasta' | ||
155 | print(LexA) | 175 | lexA.parse(filename) | ||
156 | print(len(LexA)) | ||||
157 | print() | ||||
158 | LexA.count() | ||||
159 | print(LexA.counts) | ||||
160 | print() | ||||
161 | LexA.compute_consensus() | 176 | lexA.compute_consensus() | ||
162 | print(LexA.consensus) | 177 | print(lexA.consensus) | ||
163 | if __name__ == "__main__": | ||||
164 | main() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import math | 2 | import math | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self,initial_str, generator,num_iteration,delta_theta): | n | 4 | def __init__(self,initialStr,generator,numIters,deltaTheta): |
5 | self.int_str = initial_str | 5 | self.str = initialStr | ||
6 | self.gen = generator | 6 | self.generator= generator | ||
7 | self.num_iter = num_iteration | 7 | self.numIters = numIters | ||
8 | self.delthe = delta_theta | 8 | self.deltaTheta = deltaTheta | ||
9 | for j in range(num_iteration): | 9 | for i in range(numIters): | ||
10 | string_2 = "" | 10 | new_string = "" | ||
11 | for letter in self.int_str: | 11 | for letter in self.str: | ||
12 | if letter in self.gen: | 12 | if letter in self.generator: | ||
13 | string_2 = string_2+self.gen[letter] | 13 | new_string =new_string+self.generator[letter] | ||
14 | else: | 14 | else: | ||
n | 15 | string_2 = string_2 + letter | n | 15 | new_string =new_string+letter |
16 | self.int_str = string_2 | 16 | self.str = new_string | ||
17 | def drawPlant(self): | 17 | def drawPlant(self): | ||
n | 18 | current_pt = (200,0) | n | 18 | currentPt =(200,0) |
19 | stack = [] | 19 | stack = [] | ||
n | 20 | ThEtA = 90 | n | 20 | theta =90 |
21 | for letter in self.int_str: | 21 | for letter in self.str: | ||
22 | if letter == '[': | 22 | if letter == "[": | ||
23 | stack.append([current_pt,ThEtA]) | 23 | stack.append([currentPt,theta]) | ||
24 | elif letter == ']': | 24 | elif letter == "]": | ||
25 | cur_save = stack.pop() | 25 | current_save = stack.pop() | ||
26 | ThEtA = cur_save[1] | 26 | theta = current_save[1] | ||
27 | current_pt = cur_save[0] | 27 | currentPt = current_save[0] | ||
28 | elif letter == '+': | 28 | elif letter =="+": | ||
29 | ThEtA = ThEtA + self.delthe | 29 | theta = theta+self.deltaTheta | ||
30 | elif letter == '-': | 30 | elif letter =="-": | ||
31 | ThEtA -= self.delthe | 31 | theta -= self.deltaTheta | ||
32 | else: | 32 | else: | ||
n | 33 | next_pt = (math.cos(math.radians(ThEtA)) + current_pt[1], curren | n | 33 | nextPt = (math.cos(math.radians(theta))+currentPt[0],currentPt[1 |
> | t_pt[0] + math.sin(math.radians(ThEtA))) | > | ] + math.sin(math.radians(theta))) | ||
34 | plt.plot([current_pt[0],next_pt[0]],[current_pt[1],next_pt[1]],c | 34 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||
> | olor='black') | > | olor="black") | ||
35 | current_pt = next_ptclass DNAMOTIF: | 35 | currentPt = nextPt | ||
36 | class DNAMOTIF: | ||||
36 | def __init__(self): | 37 | def __init__(self): | ||
37 | self.instances=[] | 38 | self.instances=[] | ||
38 | self.consensus=[] | 39 | self.consensus=[] | ||
39 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 40 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
40 | def __str__(self): | 41 | def __str__(self): | ||
n | 41 | my_file = '' | n | 42 | file = '' |
42 | for a_line in self.instances: | 43 | for line in self.instances: | ||
43 | my_file += a_line | 44 | file += line | ||
44 | return my_file | 45 | return file | ||
45 | def __len__(self): | 46 | def __len__(self): | ||
46 | return len(self.instances[0])-1 | 47 | return len(self.instances[0])-1 | ||
47 | def count(self): | 48 | def count(self): | ||
48 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 49 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
n | 49 | for j in range(len(self.instances[0])-1): | n | 50 | for i in range(len(self.instances[0])-1): |
50 | for key in self.counts.keys(): | 51 | for key in self.counts.keys(): | ||
n | 51 | self.counts[key].append(0) | n | 52 | self.counts[key].append(0) |
52 | for a_line in self.instances: | 53 | for line in self.instances: | ||
53 | if a_line[j].upper()==key: | 54 | if line[i].upper()==key: | ||
54 | self.counts[key][j] +=1 | 55 | self.counts[key][i]+=1 | ||
55 | return self.counts | 56 | return self.counts | ||
56 | def compute_consensus(self): | 57 | def compute_consensus(self): | ||
n | 57 | self.consensus = [] | n | 58 | self.consensus = [] |
58 | self.counts = self.count() | 59 | self.counts = self.count() | ||
59 | for char_position in range(len(self.instances[0])-1): | 60 | for charPos in range(len(self.instances[0])-1): | ||
60 | self.consensus.append(max(self.counts, key = lambda x: self.counts[x | 61 | self.consensus.append(max(self.counts, key=lambda x: self.counts[x][ | ||
> | ][char_position]).upper()) | > | charPos]).upper()) | ||
61 | self.consensus = str("".join(self.consensus)) | 62 | self.consensus = str(''.join(self.consensus)) | ||
62 | def parse(self, filename): | 63 | def parse(self, filename): | ||
t | 63 | my_file_one = open(filename, 'r') | t | 64 | file1 = open(filename, 'r') |
64 | multi_lines = my_file_one.readlines() | 65 | Lines = file1.readlines() | ||
65 | for a_line in multi_lines: | 66 | for line in Lines: | ||
66 | if not a_line.startswith('>'): | 67 | if not line.startswith('>'): | ||
67 | self.instances.append(a_line) | 68 | self.instances.append(line) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | from ast import increment_lineno | n | ||
2 | import matplotlib.pyplot as plt | 1 | import matplotlib.pyplot as plt | ||
n | 3 | plt.style.use('bmh') | n | 2 | plt.style.use('bmh') |
4 | plt.plot( | 3 | plt.plot( | ||
n | 5 | [0, 1, 2], | n | 4 | [0, 1, 2], |
6 | [0, 1, 0] | 5 | [0, 1, 0] | ||
7 | ) | 6 | ) | ||
8 | plt.xlabel('x') | 7 | plt.xlabel('x') | ||
n | 9 | plt.ylabel('y') | n | 8 | plt.ylabel('y'); |
10 | def plot_coor(coor,bare_plot = False): | 9 | def plot_coords(coords, bare_plot=False): | ||
11 | if bare_plot: | 10 | if bare_plot: | ||
12 | plt.axis('off') | 11 | plt.axis('off') | ||
13 | plt.axes().set_aspect('equal', 'datalim') | 12 | plt.axes().set_aspect('equal', 'datalim') | ||
n | 14 | x, y = zip(*coor) | n | 13 | X, Y = zip(*coords) |
15 | plt.plot(x, y); | 14 | plt.plot(X, Y); | ||
16 | plot_coor([(0,0), | 15 | plot_coords([ | ||
17 | (1,0), | 16 | (0, 0), | ||
18 | (2,1), | 17 | (1, 0), | ||
19 | (3,1), | 18 | (2, 1), | ||
20 | (2,0)]) | 19 | (3, 1), | ||
20 | (2, 0) | ||||
21 | ]) | ||||
21 | nan = float('nan') | 22 | nan = float('nan') | ||
n | 22 | plot_coor([(0,0), | n | 23 | plot_coords([ |
23 | (1,1), | 24 | (0, 0), | ||
25 | (1, 1), | ||||
24 | (nan,nan), | 26 | (nan, nan), | ||
25 | (1,0), | 27 | (1, 0), | ||
26 | (2,1)]) | 28 | (2, 1) | ||
29 | ]) | ||||
27 | from math import pi, sin, cos | 30 | from math import pi, sin, cos | ||
n | 28 | deg_rads = pi/180 | n | 31 | DEGREES_TO_RADIANS = pi / 180 |
29 | def tur_to_coor(tur_program, turn_amount = 45): | 32 | def turtle_to_coords(turtle_program, turn_amount=45): | ||
30 | state = (0.0, 0.0, 90.0) | 33 | state = (0.0, 0.0, 90.0) | ||
31 | yield (0.0, 0.0) | 34 | yield (0.0, 0.0) | ||
n | 32 | for command in tur_program: | n | 35 | for command in turtle_program: |
33 | x, y, angle = state | 36 | x, y, angle = state | ||
n | 34 | if command in 'Ff': | n | 37 | if command in 'Ff': |
35 | state = (x - cos(angle * deg_rads), y + sin(angle * deg_rads), angle | 38 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
> | ) | ||||
39 | y + sin(angle * DEGREES_TO_RADIANS), | ||||
40 | angle) | ||||
36 | if command == 'f': | 41 | if command == 'f': | ||
37 | yield (float('nan'), float('nan')) | 42 | yield (float('nan'), float('nan')) | ||
38 | yield (state[0], state[1]) | 43 | yield (state[0], state[1]) | ||
n | 39 | elif command == '+': | n | 44 | elif command == '+': |
40 | state = (x, y, angle + turn_amount) | 45 | state = (x, y, angle + turn_amount) | ||
n | 41 | elif command == '-': | n | 46 | elif command == '-': |
42 | state = (x, y, angle - turn_amount) | 47 | state = (x, y, angle - turn_amount) | ||
n | 43 | plot_coor(tur_to_coor('FfF++FfF++FfF++FfF')) | n | 48 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) |
44 | plot_coor(tur_to_coor('F-F+F+F+f+F+F+F-F')) | 49 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) | ||
45 | from math import isnan | 50 | from math import isnan | ||
n | 46 | def print_coor(coor): | n | 51 | def print_coords(coords): |
47 | for (x, y) in coor: | 52 | for (x, y) in coords: | ||
48 | if isnan(x): | 53 | if isnan(x): | ||
49 | print('<gap>') | 54 | print('<gap>') | ||
50 | else: | 55 | else: | ||
51 | print('({:.2f}, {:.2f})'.format(x, y)) | 56 | print('({:.2f}, {:.2f})'.format(x, y)) | ||
n | 52 | print_coor(tur_to_coor('F-F+F+F+f+F+F+F-F')) | n | 57 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) |
53 | plot_coor(tur_to_coor('F-F+F+F+f+F+F+F-F', 65)) | 58 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F', 65)) | ||
54 | def trans_seq(seq, trans): | 59 | def transform_sequence(sequence, transformations): | ||
55 | return "".join(trans.get(c, c) for c in seq) | 60 | return ''.join(transformations.get(c, c) for c in sequence) | ||
56 | trans_seq('acab', {'a': 'aba', 'c': 'bb'}) | 61 | transform_sequence('acab', {'a': 'aba', 'c': 'bb'}) | ||
57 | plot_coor(tur_to_coor(trans_seq('FFFF', {'F': 'FfF++'}))) | 62 | plot_coords(turtle_to_coords(transform_sequence('FFFF', {'F': 'FfF++'}))) | ||
58 | def trans_multi(seq, trans, iterations): | 63 | def transform_multiple(sequence, transformations, iterations): | ||
59 | for _ in range(iterations): | 64 | for _ in range(iterations): | ||
n | 60 | seq = trans_seq(seq, trans) | n | 65 | sequence = transform_sequence(sequence, transformations) |
61 | return seq | 66 | return sequence | ||
62 | print('0:', trans_multi('abba', {'b': 'bab'}, 0)) | 67 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | ||
63 | print('1:', trans_multi('abba', {'b': 'bab'}, 1)) | 68 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | ||
64 | print('2:', trans_multi('abba', {'b': 'bab'}, 2)) | 69 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | ||
65 | plot_coor(tur_to_coor(trans_multi('F', {'F': '+F+F-F+F'}, 5))) | 70 | plot_coords(turtle_to_coords(transform_multiple('F', {'F': '+F+F--F+F'}, 5))) | ||
66 | plot_coor(tur_to_coor(trans_multi('L', { | 71 | plot_coords(turtle_to_coords(transform_multiple('L', { | ||
67 | 'L': '-RF+LFL+FR-', | 72 | 'L': '-RF+LFL+FR-', | ||
n | 68 | 'R': '+LF-RFR-FL+' | n | 73 | 'R': '+LF-RFR-FL+' |
69 | }, 5), 90)) | 74 | }, 5), 90)) | ||
n | 70 | def branch_tur_to_coor(tur_program, turn_amount = 45): | n | 75 | def branching_turtle_to_coords(turtle_program, turn_amount=45): |
71 | saved_states = list() | 76 | saved_states = list() | ||
72 | state = (0, 0, 90) | 77 | state = (0, 0, 90) | ||
73 | yield (0, 0) | 78 | yield (0, 0) | ||
n | 74 | for command in tur_program: | n | 79 | for command in turtle_program: |
75 | x, y, angle = state | 80 | x, y, angle = state | ||
n | 76 | if command.lower() in 'abcdefghij': | n | 81 | if command.lower() in 'abcdefghij': |
77 | state = (x - cos(angle * deg_rads), y + sin(angle * deg_rads), angle | 82 | state = (x - cos(angle * DEGREES_TO_RADIANS), | ||
> | ) | ||||
83 | y + sin(angle * DEGREES_TO_RADIANS), | ||||
84 | angle) | ||||
78 | if command.islower(): | 85 | if command.islower(): | ||
79 | yield (float('nan'), float('nan')) | 86 | yield (float('nan'), float('nan')) | ||
80 | yield (state[0], state[1]) | 87 | yield (state[0], state[1]) | ||
n | 81 | elif command == '+': | n | 88 | elif command == '+': |
82 | state = (x, y, angle + turn_amount) | 89 | state = (x, y, angle + turn_amount) | ||
n | 83 | elif command == '-': | n | 90 | elif command == '-': |
84 | state = (x, y, angle - turn_amount) | 91 | state = (x, y, angle - turn_amount) | ||
n | 85 | elif command == '[': | n | 92 | elif command == '[': |
86 | saved_states.append(state) | 93 | saved_states.append(state) | ||
n | 87 | elif command == ']': | n | 94 | elif command == ']': |
88 | state = saved_states.pop() | 95 | state = saved_states.pop() | ||
89 | yield (float('nan'), float('nan')) | 96 | yield (float('nan'), float('nan')) | ||
90 | x, y, _ = state | 97 | x, y, _ = state | ||
91 | yield (x, y) | 98 | yield (x, y) | ||
n | 92 | plot_coor(branch_tur_to_coor('F[-F]+F', 45)) | n | 99 | plot_coords(branching_turtle_to_coords('F[-F]+F', 45)) |
93 | def l_plot(axiom, trans, iterations = 0, angle = 45): | 100 | def l_plot(axiom, transformations, iterations=0, angle=45): | ||
94 | tur_program = trans_multi(axiom, trans, iterations) | 101 | turtle_program = transform_multiple(axiom, transformations, iterations) | ||
95 | coor = branch_tur_to_coor(tur_program, angle) | 102 | coords = branching_turtle_to_coords(turtle_program, angle) | ||
96 | plot_coor(coor, bare_plot = True) | 103 | plot_coords(coords, bare_plot=True) | ||
97 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | 104 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | ||
98 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 105 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | ||
99 | for i in range(5): | 106 | for i in range(5): | ||
n | 100 | print('{}:'.format(i), | n | 107 | print('{}: '.format(i), |
101 | trans_multi('X',{'X': 'F+X'}, i)) | 108 | transform_multiple('A', {'A': 'F+A'}, i)) | ||
102 | for i in range(5): | 109 | for i in range(5): | ||
n | 103 | print('{}:'.format(i), | n | 110 | print('{}: '.format(i), |
104 | trans_multi('X', {'X': 'F+X', 'F': 'FF'}, i)) | 111 | transform_multiple('A', {'A': 'F+A', 'F': 'FF'}, i)) | ||
105 | l_plot('X', {'F': 'FF', 'X': 'F[+XF-[X]-X][---X]'}, 55, 22.5)class DNAMOTIF: | 112 | l_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5)class DNAMOTIF: | ||
106 | def __init__(self): | 113 | def __init__(self): | ||
n | 107 | self.instances=[] | n | 114 | self.instances = [] |
108 | self.consensus=[] | 115 | self.consensus=[] | ||
n | 109 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | n | 116 | self.counts= {'A': [], 'C': [], "G":[],'T': []} |
110 | def __str__(self): | 117 | def __str__ (self): | ||
111 | string = '' | 118 | string = "" | ||
112 | for i in self.instances: | 119 | for i in self.instances: | ||
n | 113 | string += 1 + '' | n | 120 | string += i + "\n" |
114 | return string[:-2] | 121 | return string[:0] | ||
115 | def __len__(self): | 122 | def __len__(self): | ||
n | 116 | return len(self.instances[0]) | n | 123 | return len (self.consensus) |
117 | def count(self): | 124 | def count(self): | ||
n | 118 | for i in self. instances: | n | 125 | for i in self.instances: |
119 | t = i.upper() | 126 | temp = i.upper() | ||
120 | self.counts['A'].append(t.count('A')) | 127 | self.counts["A"].append(temp.count("A")) | ||
121 | self.counts['C'].append(t.count('C')) | 128 | self.counts["C"].append(temp.count("C")) | ||
122 | self.counts['G'].append(t.count('G')) | 129 | self.counts["G"].append(temp.count("G")) | ||
123 | self.counts['T'].append(t.count('T')) | 130 | self.counts["T"].append(temp.count("T")) | ||
124 | def compute_consensus(self): | 131 | def compute_consensus(self): | ||
n | 125 | A = self.counts['A'] | n | 132 | A = self.counts["A"] |
126 | C = self.counts['C'] | 133 | C = self.counts["C"] | ||
127 | G = self.counts['G'] | 134 | G = self.counts["G"] | ||
128 | T = self.counts['T'] | 135 | T = self.counts["T"] | ||
129 | for i in range(len(A)): | 136 | for i in range (len(A)): | ||
130 | if(A[i]>=C[i] and A[i]>=G[i] and A[i]>=T[i]): | 137 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
131 | self.consensus.append('A') | 138 | self.consensus.append("A") | ||
132 | elif (C[i]>=G[i] and C[i]>=T[i]): | 139 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
133 | self.consensus.append('C') | 140 | self.consensus.append("C") | ||
134 | elif (G[i]>=T[i]): | 141 | elif (G[i] >= T[i]): | ||
135 | self.consensus.append('G') | 142 | self.consensus.append("G") | ||
136 | else: | 143 | else: | ||
n | 137 | self.consensus.append('T') | n | 144 | self.consensus.append("T") |
138 | def parse(self, filename): | 145 | def parse(self, filename): | ||
n | 139 | with open(filename, 'r') as f: | n | 146 | with open(filename,'r') as f: |
140 | for i in f: | 147 | for i in f: | ||
t | 141 | if '>' in i: | t | 148 | if ">" in i: |
142 | continue | 149 | continue | ||
143 | else: | 150 | else: | ||
144 | self.instances.append(i) | 151 | self.instances.append(i) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import math | 2 | import math | ||
n | 3 | import numpy | n | ||
4 | class PLANT: | 3 | class PLANT: | ||
n | 5 | def __init__(self,initial_state,generator,n,deltaTheta): | n | 4 | def __init__(self,init_state,gen,n,deltaTheta): |
6 | self.initial_state=initial_state | 5 | self.init_state=init_state | ||
7 | self.generator=generator | 6 | self.gen=gen | ||
8 | self.n=n | 7 | self.n=n | ||
9 | self.deltaTheta=deltaTheta | 8 | self.deltaTheta=deltaTheta | ||
n | 10 | self.str=initial_state | n | 9 | self.str=init_state |
11 | for i in range(n): | 10 | for i in range(n): | ||
n | 12 | updated_string='' | n | 11 | up_str='' |
13 | for a in self.str: | 12 | for i in self.str: | ||
14 | curr = a | 13 | c = i | ||
15 | if curr in self.generator.keys(): | 14 | if c in self.gen.keys(): | ||
16 | updated_string+=self.generator[curr] | 15 | up_str = up_str + self.gen[c] | ||
17 | else: updated_string += curr | 16 | else: | ||
17 | up_str = up_str + c | ||||
18 | self.str=updated_string | 18 | self.str=up_str | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 20 | currPt=(200,0) | n | 20 | presentPt=(200,0) |
21 | theta=90 | 21 | theta= 40+50 | ||
22 | s=[currPt,theta] | 22 | z=[presentPt,theta] | ||
23 | for i in self.str: | 23 | for o in self.str: | ||
24 | if i.isalpha(): | 24 | if o.isalpha(): | ||
25 | nextPt = [currPt[0]+math.cos(theta*math.pi/180), currPt[1]+math. | 25 | nxt = (presentPt[0]+math.cos(math.radians(theta)), presentPt[1]+ | ||
> | sin((theta*math.pi/180))] | > | math.sin(math.radians(theta))) | ||
26 | plt.plot(currPt[0],nextPt[0],currPt[1],nextPt[1],color='black') | 26 | plt.plot([presentPt[0],nxt[0]],[presentPt[1],nxt[1]],color='blac | ||
> | k') | ||||
27 | currPt=nextPt | 27 | presentPt=nxt | ||
28 | elif i == '[': | 28 | elif o == '[': | ||
29 | s=[currPt,theta] | 29 | z=[presentPt,theta] | ||
30 | elif i == ']': | 30 | elif o == ']': | ||
31 | currPt = s[0] | 31 | presentPt = z[0] | ||
32 | theta = s[1] | 32 | theta = z[1] | ||
33 | elif i == '+': | 33 | elif o == '+': | ||
34 | theta = theta+self.deltaTheta | 34 | theta = theta+self.deltaTheta | ||
n | 35 | elif i == '-': | n | 35 | elif o == '-': |
36 | theta = theta-self.deltaTheta | 36 | theta = theta-self.deltaTheta class DNAMOTIF: | ||
37 | class DNAMOTIF: | ||||
38 | def __init__(self): | 37 | def __init__(self): | ||
n | n | 38 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
39 | self.instances=[] | 39 | self.instances=[] | ||
40 | self.consensus=[] | 40 | self.consensus=[] | ||
n | 41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | n | ||
42 | def __str__(self): | ||||
43 | DNA='' | ||||
44 | for i in self.instances: | ||||
45 | DNA+=i | ||||
46 | return DNA | ||||
47 | def __len__(self): | 41 | def __len__(self): | ||
48 | return len(self.instances[0])-1 | 42 | return len(self.instances[0])-1 | ||
n | n | 43 | def __str__(self): | ||
44 | DNA_Strand='' | ||||
45 | for n in self.instances: | ||||
46 | DNA_Strand=DNA_Strand+n | ||||
47 | return DNA_Strand | ||||
49 | def count(self): | 48 | def count(self): | ||
n | 50 | for i in range(len(self)): | n | 49 | for n in range(len(self)): |
51 | self.counts["A"].append(0) | ||||
52 | self.counts["C"].append(0) | ||||
53 | self.counts["G"].append(0) | 50 | self.counts["G"].append(0) | ||
54 | self.counts["T"].append(0) | 51 | self.counts["T"].append(0) | ||
n | n | 52 | self.counts["C"].append(0) | ||
53 | self.counts["A"].append(0) | ||||
55 | for j in self.instances: | 54 | for j in self.instances: | ||
n | 56 | if j[i].upper() == 'A': | n | ||
57 | self.counts["A"][i]+=1 | ||||
58 | if j[i].upper() == 'C': | ||||
59 | self.counts["C"][i]+=1 | ||||
60 | if j[i].upper() == 'G': | 55 | if j[n].upper() == 'G': | ||
61 | self.counts["G"][i]+=1 | 56 | self.counts["G"][n]+=1 | ||
62 | if j[i].upper() == 'T': | 57 | if j[n].upper() == 'T': | ||
63 | self.counts["T"][i]+=1 | 58 | self.counts["T"][n]+=1 | ||
59 | if j[n].upper() == 'C': | ||||
60 | self.counts["C"][n]+=1 | ||||
61 | if j[n].upper() == 'A': | ||||
62 | self.counts["A"][n]+=1 | ||||
64 | def compute_consensus(self): | 63 | def compute_consensus(self): | ||
n | n | 64 | cons='' | ||
65 | self.count() | 65 | self.count() | ||
n | 66 | consensus='' | n | ||
67 | for i in range(len(self)): | 66 | for n in range(len(self)): | ||
68 | max_val=0 | 67 | highest_value=0 | ||
69 | char="A" | 68 | character="A" | ||
70 | if self.counts["A"][i]>max_val: | ||||
71 | max_val=self.counts["A"][i] | ||||
72 | char="A" | ||||
73 | if self.counts["C"][i]>max_val: | 69 | if self.counts["C"][n]>highest_value: | ||
74 | max_val=self.counts["C"][i] | 70 | highest_value=self.counts["C"][n] | ||
75 | char="C" | 71 | character="C" | ||
76 | if self.counts["G"][i]>max_val: | 72 | if self.counts["A"][n]>highest_value: | ||
77 | max_val=self.counts["G"][i] | 73 | highest_value=self.counts["A"][n] | ||
78 | char="G" | 74 | character="A" | ||
79 | if self.counts["T"][i]>max_val: | 75 | if self.counts["T"][n]>highest_value: | ||
80 | max_val=self.counts["T"][i] | 76 | highest_value=self.counts["T"][n] | ||
81 | char="T" | 77 | character="T" | ||
78 | if self.counts["G"][n]>highest_value: | ||||
79 | highest_value=self.counts["G"][n] | ||||
80 | character="G" | ||||
82 | consensus+=char | 81 | cons=cons+character | ||
83 | self.consensus=consensus | 82 | self.consensus=cons | ||
84 | def parse(self, filename): | 83 | def parse(self, filename): | ||
85 | with open(filename,'r') as file_object: | 84 | with open(filename,'r') as file_object: | ||
t | 86 | lines=file_object.readlines() | t | 85 | line=file_object.readlines() |
87 | for i in lines: | 86 | for n in line: | ||
88 | if i[0].upper()=='A' or i[0].upper()=='T' or i[0].upper()=='C' o | 87 | if n[0].upper()=='C' or n[0].upper()=='A' or n[0].upper()=='G' o | ||
> | r i[0].upper()=='G': | > | r n[0].upper()=='T': | ||
89 | self.instances.append(i) | 88 | self.instances.append(n) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import math | 2 | import math | ||
n | n | 3 | import numpy | ||
3 | class PLANT: | 4 | class PLANT: | ||
n | 4 | def __init__(self,init_state,gen,n,deltaTheta): | n | 5 | def __init__(self,initial_state,generator,n,deltaTheta): |
5 | self.init_state=init_state | 6 | self.initial_state=initial_state | ||
6 | self.gen=gen | 7 | self.generator=generator | ||
7 | self.n=n | 8 | self.n=n | ||
8 | self.deltaTheta=deltaTheta | 9 | self.deltaTheta=deltaTheta | ||
n | 9 | self.str=init_state | n | 10 | self.str=initial_state |
10 | for i in range(n): | 11 | for i in range(n): | ||
n | 11 | up_str='' | n | 12 | updated_string='' |
12 | for i in self.str: | 13 | for a in self.str: | ||
13 | c = i | 14 | curr = a | ||
14 | if c in self.gen.keys(): | 15 | if curr in self.generator.keys(): | ||
15 | up_str = up_str + self.gen[c] | 16 | updated_string+=self.generator[curr] | ||
16 | else: | 17 | else: updated_string += curr | ||
17 | up_str = up_str + c | ||||
18 | self.str=up_str | 18 | self.str=updated_string | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 20 | presentPt=(200,0) | n | 20 | currPt=(200,0) |
21 | theta= 40+50 | 21 | theta=90 | ||
22 | z=[presentPt,theta] | 22 | s=[currPt,theta] | ||
23 | for o in self.str: | 23 | for i in self.str: | ||
24 | if o.isalpha(): | 24 | if i.isalpha(): | ||
25 | nxt = (presentPt[0]+math.cos(math.radians(theta)), presentPt[1]+ | 25 | nextPt = [currPt[0]+math.cos(theta*math.pi/180), currPt[1]+math. | ||
> | math.sin(math.radians(theta))) | > | sin((theta*math.pi/180))] | ||
26 | plt.plot([presentPt[0],nxt[0]],[presentPt[1],nxt[1]],color='blac | 26 | plt.plot(currPt[0],nextPt[0],currPt[1],nextPt[1],color='black') | ||
> | k') | ||||
27 | presentPt=nxt | 27 | currPt=nextPt | ||
28 | elif o == '[': | 28 | elif i == '[': | ||
29 | z=[presentPt,theta] | 29 | s=[currPt,theta] | ||
30 | elif o == ']': | 30 | elif i == ']': | ||
31 | presentPt = z[0] | 31 | currPt = s[0] | ||
32 | theta = z[1] | 32 | theta = s[1] | ||
33 | elif o == '+': | 33 | elif i == '+': | ||
34 | theta = theta+self.deltaTheta | 34 | theta = theta+self.deltaTheta | ||
n | 35 | elif o == '-': | n | 35 | elif i == '-': |
36 | theta = theta-self.deltaTheta class DNAMOTIF: | 36 | theta = theta-self.deltaTheta | ||
37 | class DNAMOTIF: | ||||
37 | def __init__(self): | 38 | def __init__(self): | ||
n | 38 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | n | ||
39 | self.instances=[] | 39 | self.instances=[] | ||
40 | self.consensus=[] | 40 | self.consensus=[] | ||
n | n | 41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
42 | def __str__(self): | ||||
43 | DNA='' | ||||
44 | for i in self.instances: | ||||
45 | DNA+=i | ||||
46 | return DNA | ||||
41 | def __len__(self): | 47 | def __len__(self): | ||
42 | return len(self.instances[0])-1 | 48 | return len(self.instances[0])-1 | ||
n | 43 | def __str__(self): | n | ||
44 | DNA_Strand='' | ||||
45 | for n in self.instances: | ||||
46 | DNA_Strand=DNA_Strand+n | ||||
47 | return DNA_Strand | ||||
48 | def count(self): | 49 | def count(self): | ||
n | 49 | for n in range(len(self)): | n | 50 | for i in range(len(self)): |
51 | self.counts["A"].append(0) | ||||
52 | self.counts["C"].append(0) | ||||
50 | self.counts["G"].append(0) | 53 | self.counts["G"].append(0) | ||
51 | self.counts["T"].append(0) | 54 | self.counts["T"].append(0) | ||
n | 52 | self.counts["C"].append(0) | n | ||
53 | self.counts["A"].append(0) | ||||
54 | for j in self.instances: | 55 | for j in self.instances: | ||
n | 55 | if j[n].upper() == 'G': | n | ||
56 | self.counts["G"][n]+=1 | ||||
57 | if j[n].upper() == 'T': | ||||
58 | self.counts["T"][n]+=1 | ||||
59 | if j[n].upper() == 'C': | ||||
60 | self.counts["C"][n]+=1 | ||||
61 | if j[n].upper() == 'A': | 56 | if j[i].upper() == 'A': | ||
62 | self.counts["A"][n]+=1 | 57 | self.counts["A"][i]+=1 | ||
58 | if j[i].upper() == 'C': | ||||
59 | self.counts["C"][i]+=1 | ||||
60 | if j[i].upper() == 'G': | ||||
61 | self.counts["G"][i]+=1 | ||||
62 | if j[i].upper() == 'T': | ||||
63 | self.counts["T"][i]+=1 | ||||
63 | def compute_consensus(self): | 64 | def compute_consensus(self): | ||
n | 64 | cons='' | n | ||
65 | self.count() | 65 | self.count() | ||
n | n | 66 | consensus='' | ||
66 | for n in range(len(self)): | 67 | for i in range(len(self)): | ||
67 | highest_value=0 | 68 | max_val=0 | ||
68 | character="A" | 69 | char="A" | ||
69 | if self.counts["C"][n]>highest_value: | ||||
70 | highest_value=self.counts["C"][n] | ||||
71 | character="C" | ||||
72 | if self.counts["A"][n]>highest_value: | 70 | if self.counts["A"][i]>max_val: | ||
73 | highest_value=self.counts["A"][n] | 71 | max_val=self.counts["A"][i] | ||
74 | character="A" | 72 | char="A" | ||
75 | if self.counts["T"][n]>highest_value: | 73 | if self.counts["C"][i]>max_val: | ||
76 | highest_value=self.counts["T"][n] | 74 | max_val=self.counts["C"][i] | ||
77 | character="T" | 75 | char="C" | ||
78 | if self.counts["G"][n]>highest_value: | 76 | if self.counts["G"][i]>max_val: | ||
79 | highest_value=self.counts["G"][n] | 77 | max_val=self.counts["G"][i] | ||
80 | character="G" | 78 | char="G" | ||
79 | if self.counts["T"][i]>max_val: | ||||
80 | max_val=self.counts["T"][i] | ||||
81 | char="T" | ||||
81 | cons=cons+character | 82 | consensus+=char | ||
82 | self.consensus=cons | 83 | self.consensus=consensus | ||
83 | def parse(self, filename): | 84 | def parse(self, filename): | ||
84 | with open(filename,'r') as file_object: | 85 | with open(filename,'r') as file_object: | ||
t | 85 | line=file_object.readlines() | t | 86 | lines=file_object.readlines() |
86 | for n in line: | 87 | for i in lines: | ||
87 | if n[0].upper()=='C' or n[0].upper()=='A' or n[0].upper()=='G' o | 88 | if i[0].upper()=='A' or i[0].upper()=='T' or i[0].upper()=='C' o | ||
> | r n[0].upper()=='T': | > | r i[0].upper()=='G': | ||
88 | self.instances.append(n) | 89 | self.instances.append(i) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import numpy as np | f | 1 | import numpy as np |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, init_state, generator, n, deltaTheta) -> None: | n | 4 | def __init__(self, init_state, fx, g, dxTheta) -> None: |
5 | self.initial_state = str(init_state) | 5 | self.initial_state = str(init_state) | ||
n | 6 | self.generator = dict(generator) | n | 6 | self.generator = dict(fx) |
7 | self.n = int(n) | 7 | self.n = int(g) | ||
8 | self.deltaTheta = float(deltaTheta) | 8 | self.deltaTheta = float(dxTheta) | ||
9 | self.str = self.initial_state | 9 | self.str = self.initial_state | ||
n | n | 10 | doob_str = str(self.str) | ||
10 | iterate_str = str(self.initial_state) | 11 | boof_str = str(self.initial_state) | ||
11 | for i in range(n): | 12 | for i in range(g): | ||
12 | iter_list = list(iterate_str) | 13 | dank_list = list(boof_str) | ||
13 | new_iter_list = [] | 14 | new_freezer_list = [] | ||
14 | for char in iter_list: | 15 | for smd in dank_list: | ||
15 | if char in self.generator.keys(): | 16 | if smd in self.generator.keys(): | ||
16 | new_iter_list.append(self.generator.get(char)) | 17 | new_freezer_list.append(self.generator.get(smd)) | ||
17 | else: | 18 | else: | ||
n | 18 | new_iter_list.append(char) | n | 19 | new_freezer_list.append(smd) |
19 | iter_list = new_iter_list | 20 | dank_list = new_freezer_list | ||
20 | iterate_str = ''.join(iter_list) | 21 | boof_str = ''.join(dank_list) | ||
21 | self.str = iterate_str | 22 | self.str = boof_str | ||
22 | pass | 23 | pass | ||
23 | def drawPlant(self): | 24 | def drawPlant(self): | ||
n | 24 | currentPt = (200, 0) | n | 25 | presentLocation = (200, 0) |
25 | radian = (np.pi / 180) | 26 | rad = (np.pi / 180) | ||
26 | int_theta = 90 * radian | 27 | theta_i = 90 * rad | ||
27 | push_state = currentPt | 28 | force = presentLocation | ||
28 | nextPt = (0,0) | 29 | futureLocation = (0,0) | ||
29 | push_angle = 0 | 30 | force_ang = 0 | ||
30 | theta = int_theta | 31 | theta = theta_i | ||
31 | saved_states = [] | 32 | save_drive = [] | ||
32 | print('drawing plant...') | 33 | print('drawing plant...') | ||
n | 33 | for char in self.str: | n | 34 | for smd in self.str: |
34 | if (char == 'X') or (char == 'F'): | 35 | if (smd == 'X') or (smd == 'F'): | ||
35 | nextPt = (currentPt[0] + np.cos(theta), currentPt[1] + np.sin(th | 36 | futureLocation = (presentLocation[0] + np.cos(theta), presentLoc | ||
> | eta)) | > | ation[1] + np.sin(theta)) | ||
36 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | 37 | plt.plot([presentLocation[0], futureLocation[0]], [presentLocati | ||
> | olor='black') | > | on[1], futureLocation[1]], color='green') | ||
37 | elif char == '+': | 38 | elif smd == '+': | ||
38 | theta = theta + (self.deltaTheta * radian) | 39 | theta = theta + (self.deltaTheta * rad) | ||
39 | elif char == '-': | 40 | elif smd == '-': | ||
40 | push_angle = theta | 41 | force_ang = theta | ||
41 | theta = theta - (self.deltaTheta * radian) | 42 | theta = theta - (self.deltaTheta * rad) | ||
42 | elif char == '[': | 43 | elif smd == '[': | ||
43 | push_radians = theta | 44 | push_rad = theta | ||
44 | push_state = currentPt | 45 | force = presentLocation | ||
45 | turtle = (push_state[0], push_state[1], push_radians) | 46 | turtle = (force[0], force[1], push_rad) | ||
46 | saved_states.append(turtle) | 47 | save_drive.append(turtle) | ||
47 | pass | 48 | pass | ||
n | 48 | elif char == ']': | n | 49 | elif smd == ']': |
49 | theta = saved_states[-1][-1] | 50 | theta = save_drive[-1][-1] | ||
50 | nextPt = (saved_states[-1][0], saved_states[-1][1]) | 51 | futureLocation = (save_drive[-1][0], save_drive[-1][1]) | ||
51 | saved_states.pop() | 52 | save_drive.pop() | ||
52 | pass | 53 | pass | ||
n | 53 | currentPt = nextPt | n | 54 | presentLocation = futureLocation |
54 | print('complete') | 55 | print('complete') | ||
55 | pass | 56 | pass | ||
56 | if __name__ == '__main__': | 57 | if __name__ == '__main__': | ||
57 | a = PLANT('F', {'F': 'F[+F]F[-F]F'}, 5, 25.7) | 58 | a = PLANT('F', {'F': 'F[+F]F[-F]F'}, 5, 25.7) | ||
58 | a.drawPlant() | 59 | a.drawPlant() | ||
59 | passimport csv | 60 | passimport csv | ||
60 | import os | 61 | import os | ||
61 | class DNAMOTIF: | 62 | class DNAMOTIF: | ||
62 | def __init__(self) -> None: | 63 | def __init__(self) -> None: | ||
n | 63 | self.instances_1=[] | n | ||
64 | self.instances = [] | 64 | self.instances=[] | ||
65 | self.consensus=[] | 65 | self.consensus=[] | ||
66 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 66 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
67 | def __str__(self) -> str: | 67 | def __str__(self) -> str: | ||
n | 68 | return str(self.instances_1) | n | 68 | return str(self.instances) |
69 | def __len__(self): | 69 | def __len__(self): | ||
n | 70 | return len(self.instances_1[0]) | n | 70 | return len(self.instances) |
71 | def count(self): | ||||
72 | for i in range(len(self.instances[0])): | ||||
73 | group = [row[i] for row in self.instances] | ||||
74 | a_ct = 0 | ||||
75 | c_ct = 0 | ||||
76 | g_ct = 0 | ||||
77 | t_ct = 0 | ||||
78 | for char in group: | ||||
79 | if char == 'A': | ||||
80 | a_ct += 1 | ||||
81 | if char == 'C': | ||||
82 | c_ct += 1 | ||||
83 | if char == 'G': | ||||
84 | g_ct += 1 | ||||
85 | if char == 'T': | ||||
86 | t_ct += 1 | ||||
87 | self.counts.get('A').append(a_ct) | ||||
88 | self.counts.get('C').append(c_ct) | ||||
89 | self.counts.get('G').append(g_ct) | ||||
90 | self.counts.get('T').append(t_ct) | ||||
91 | pass | ||||
92 | def compute_consensus(self): | ||||
93 | DNAMOTIF.count(self) | ||||
94 | char_return = '' | ||||
95 | a_ct = self.counts.get('A') | ||||
96 | c_ct = self.counts.get('C') | ||||
97 | g_ct = self.counts.get('G') | ||||
98 | t_ct = self.counts.get('T') | ||||
99 | for i in range(len(a_ct)): | ||||
100 | ct_list = [a_ct[i], c_ct[i], g_ct[i], t_ct[i]] | ||||
101 | max_num = max(ct_list) | ||||
102 | if max_num == ct_list[0]: char_return += 'A' | ||||
103 | if max_num == ct_list[1]: char_return += 'C' | ||||
104 | if max_num == ct_list[2]: char_return += 'G' | ||||
105 | if max_num == ct_list[3]: char_return += 'T' | ||||
106 | self.consensus = char_return | ||||
107 | pass | ||||
71 | def parse(self, filename): | 108 | def parse(self, filename): | ||
72 | with open(filename) as file: | 109 | with open(filename) as file: | ||
n | 73 | list_of_lines = file.readlines() | n | 110 | lines_list = file.readlines() |
74 | sequences = [] | 111 | sequences = [] | ||
n | 75 | for line in list_of_lines: | n | 112 | for line in lines_list: |
76 | if list_of_lines.index(line) % 2 != 0: | 113 | if lines_list.index(line) % 2 != 0: | ||
77 | line = line.replace('\n', '') | 114 | line = line.replace('\n', '') | ||
78 | line = line.upper() | 115 | line = line.upper() | ||
79 | sequences.append(line) | 116 | sequences.append(line) | ||
n | 80 | self.instances_1 = sequences | n | 117 | self.instances = sequences |
81 | for line in self.instances_1: | ||||
82 | line_list = list(line) | ||||
83 | line_list[0] = line_list[0].lower() | ||||
84 | line_list.append('\n') | ||||
85 | line = ''.join(line_list) | ||||
86 | self.instances.append(line) | ||||
87 | pass | 118 | pass | ||
t | 88 | def count(self): | t | ||
89 | for c in range(len(self.instances_1[0])): | ||||
90 | column = [row[c] for row in self.instances_1] | ||||
91 | a_count = 0 | ||||
92 | c_count = 0 | ||||
93 | g_count = 0 | ||||
94 | t_count = 0 | ||||
95 | for letter in column: | ||||
96 | if letter == 'A': a_count += 1 | ||||
97 | if letter == 'C': c_count += 1 | ||||
98 | if letter == 'G': g_count += 1 | ||||
99 | if letter == 'T': t_count += 1 | ||||
100 | self.counts.get('A').append(a_count) | ||||
101 | self.counts.get('C').append(c_count) | ||||
102 | self.counts.get('G').append(g_count) | ||||
103 | self.counts.get('T').append(t_count) | ||||
104 | pass | ||||
105 | def compute_consensus(self): | ||||
106 | DNAMOTIF.count(self) | ||||
107 | letter_return = '' | ||||
108 | a_count = self.counts.get('A') | ||||
109 | c_count = self.counts.get('C') | ||||
110 | g_count = self.counts.get('G') | ||||
111 | t_count = self.counts.get('T') | ||||
112 | for item in range(len(a_count)): | ||||
113 | count_list = [a_count[item], c_count[item], g_count[item], t_count[i | ||||
> | tem]] | ||||
114 | max_number = max(count_list) | ||||
115 | if max_number == count_list[0]: letter_return += 'A' | ||||
116 | if max_number == count_list[1]: letter_return += 'C' | ||||
117 | if max_number == count_list[2]: letter_return += 'G' | ||||
118 | if max_number == count_list[3]: letter_return += 'T' | ||||
119 | self.consensus = letter_return | ||||
120 | pass | ||||
121 | if __name__ == '__main__': | 119 | if __name__ == '__main__': | ||
122 | lexA=DNAMOTIF() | 120 | lexA=DNAMOTIF() | ||
123 | lexA.parse('lexA.fasta') | 121 | lexA.parse('lexA.fasta') | ||
124 | print('file open') | 122 | print('file open') | ||
125 | lexA.count() | 123 | lexA.count() | ||
126 | lexA.compute_consensus() | 124 | lexA.compute_consensus() | ||
127 | pass | 125 | pass |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import numpy as np | f | 1 | import numpy as np |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, init_state, fx, g, dxTheta) -> None: | n | 4 | def __init__(self, init_state, generator, n, deltaTheta) -> None: |
5 | self.initial_state = str(init_state) | 5 | self.initial_state = str(init_state) | ||
n | 6 | self.generator = dict(fx) | n | 6 | self.generator = dict(generator) |
7 | self.n = int(g) | 7 | self.n = int(n) | ||
8 | self.deltaTheta = float(dxTheta) | 8 | self.deltaTheta = float(deltaTheta) | ||
9 | self.str = self.initial_state | 9 | self.str = self.initial_state | ||
n | 10 | doob_str = str(self.str) | n | ||
11 | boof_str = str(self.initial_state) | 10 | iterate_str = str(self.initial_state) | ||
12 | for i in range(g): | 11 | for i in range(n): | ||
13 | dank_list = list(boof_str) | 12 | iter_list = list(iterate_str) | ||
14 | new_freezer_list = [] | 13 | new_iter_list = [] | ||
15 | for smd in dank_list: | 14 | for char in iter_list: | ||
16 | if smd in self.generator.keys(): | 15 | if char in self.generator.keys(): | ||
17 | new_freezer_list.append(self.generator.get(smd)) | 16 | new_iter_list.append(self.generator.get(char)) | ||
18 | else: | 17 | else: | ||
n | 19 | new_freezer_list.append(smd) | n | 18 | new_iter_list.append(char) |
20 | dank_list = new_freezer_list | 19 | iter_list = new_iter_list | ||
21 | boof_str = ''.join(dank_list) | 20 | iterate_str = ''.join(iter_list) | ||
22 | self.str = boof_str | 21 | self.str = iterate_str | ||
23 | pass | 22 | pass | ||
24 | def drawPlant(self): | 23 | def drawPlant(self): | ||
n | 25 | presentLocation = (200, 0) | n | 24 | currentPt = (200, 0) |
26 | rad = (np.pi / 180) | 25 | radian = (np.pi / 180) | ||
27 | theta_i = 90 * rad | 26 | int_theta = 90 * radian | ||
28 | force = presentLocation | 27 | push_state = currentPt | ||
29 | futureLocation = (0,0) | 28 | nextPt = (0,0) | ||
30 | force_ang = 0 | 29 | push_angle = 0 | ||
31 | theta = theta_i | 30 | theta = int_theta | ||
32 | save_drive = [] | 31 | saved_states = [] | ||
33 | print('drawing plant...') | 32 | print('drawing plant...') | ||
n | 34 | for smd in self.str: | n | 33 | for char in self.str: |
35 | if (smd == 'X') or (smd == 'F'): | 34 | if (char == 'X') or (char == 'F'): | ||
36 | futureLocation = (presentLocation[0] + np.cos(theta), presentLoc | 35 | nextPt = (currentPt[0] + np.cos(theta), currentPt[1] + np.sin(th | ||
> | ation[1] + np.sin(theta)) | > | eta)) | ||
37 | plt.plot([presentLocation[0], futureLocation[0]], [presentLocati | 36 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||
> | on[1], futureLocation[1]], color='green') | > | olor='black') | ||
38 | elif smd == '+': | 37 | elif char == '+': | ||
39 | theta = theta + (self.deltaTheta * rad) | 38 | theta = theta + (self.deltaTheta * radian) | ||
40 | elif smd == '-': | 39 | elif char == '-': | ||
41 | force_ang = theta | 40 | push_angle = theta | ||
42 | theta = theta - (self.deltaTheta * rad) | 41 | theta = theta - (self.deltaTheta * radian) | ||
43 | elif smd == '[': | 42 | elif char == '[': | ||
44 | push_rad = theta | 43 | push_radians = theta | ||
45 | force = presentLocation | 44 | push_state = currentPt | ||
46 | turtle = (force[0], force[1], push_rad) | 45 | turtle = (push_state[0], push_state[1], push_radians) | ||
47 | save_drive.append(turtle) | 46 | saved_states.append(turtle) | ||
48 | pass | 47 | pass | ||
n | 49 | elif smd == ']': | n | 48 | elif char == ']': |
50 | theta = save_drive[-1][-1] | 49 | theta = saved_states[-1][-1] | ||
51 | futureLocation = (save_drive[-1][0], save_drive[-1][1]) | 50 | nextPt = (saved_states[-1][0], saved_states[-1][1]) | ||
52 | save_drive.pop() | 51 | saved_states.pop() | ||
53 | pass | 52 | pass | ||
n | 54 | presentLocation = futureLocation | n | 53 | currentPt = nextPt |
55 | print('complete') | 54 | print('complete') | ||
56 | pass | 55 | pass | ||
57 | if __name__ == '__main__': | 56 | if __name__ == '__main__': | ||
58 | a = PLANT('F', {'F': 'F[+F]F[-F]F'}, 5, 25.7) | 57 | a = PLANT('F', {'F': 'F[+F]F[-F]F'}, 5, 25.7) | ||
59 | a.drawPlant() | 58 | a.drawPlant() | ||
60 | passimport csv | 59 | passimport csv | ||
61 | import os | 60 | import os | ||
62 | class DNAMOTIF: | 61 | class DNAMOTIF: | ||
63 | def __init__(self) -> None: | 62 | def __init__(self) -> None: | ||
n | n | 63 | self.instances_1=[] | ||
64 | self.instances=[] | 64 | self.instances = [] | ||
65 | self.consensus=[] | 65 | self.consensus=[] | ||
66 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 66 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
67 | def __str__(self) -> str: | 67 | def __str__(self) -> str: | ||
n | 68 | return str(self.instances) | n | 68 | return str(self.instances_1) |
69 | def __len__(self): | 69 | def __len__(self): | ||
n | 70 | return len(self.instances) | n | 70 | return len(self.instances_1[0]) |
71 | def parse(self, filename): | ||||
72 | with open(filename) as file: | ||||
73 | list_of_lines = file.readlines() | ||||
74 | sequences = [] | ||||
75 | for line in list_of_lines: | ||||
76 | if list_of_lines.index(line) % 2 != 0: | ||||
77 | line = line.replace('\n', '') | ||||
78 | line = line.upper() | ||||
79 | sequences.append(line) | ||||
80 | self.instances_1 = sequences | ||||
81 | for line in self.instances_1: | ||||
82 | line_list = list(line) | ||||
83 | line_list[0] = line_list[0].lower() | ||||
84 | line_list.append('\n') | ||||
85 | line = ''.join(line_list) | ||||
86 | self.instances.append(line) | ||||
87 | pass | ||||
71 | def count(self): | 88 | def count(self): | ||
n | 72 | for i in range(len(self.instances[0])): | n | 89 | for c in range(len(self.instances_1[0])): |
73 | group = [row[i] for row in self.instances] | 90 | column = [row[c] for row in self.instances_1] | ||
74 | a_ct = 0 | 91 | a_count = 0 | ||
75 | c_ct = 0 | 92 | c_count = 0 | ||
76 | g_ct = 0 | 93 | g_count = 0 | ||
77 | t_ct = 0 | 94 | t_count = 0 | ||
78 | for char in group: | 95 | for letter in column: | ||
79 | if char == 'A': | 96 | if letter == 'A': a_count += 1 | ||
80 | a_ct += 1 | 97 | if letter == 'C': c_count += 1 | ||
81 | if char == 'C': | 98 | if letter == 'G': g_count += 1 | ||
82 | c_ct += 1 | 99 | if letter == 'T': t_count += 1 | ||
83 | if char == 'G': | ||||
84 | g_ct += 1 | ||||
85 | if char == 'T': | ||||
86 | t_ct += 1 | ||||
87 | self.counts.get('A').append(a_ct) | 100 | self.counts.get('A').append(a_count) | ||
88 | self.counts.get('C').append(c_ct) | 101 | self.counts.get('C').append(c_count) | ||
89 | self.counts.get('G').append(g_ct) | 102 | self.counts.get('G').append(g_count) | ||
90 | self.counts.get('T').append(t_ct) | 103 | self.counts.get('T').append(t_count) | ||
91 | pass | 104 | pass | ||
92 | def compute_consensus(self): | 105 | def compute_consensus(self): | ||
93 | DNAMOTIF.count(self) | 106 | DNAMOTIF.count(self) | ||
n | 94 | char_return = '' | n | 107 | letter_return = '' |
95 | a_ct = self.counts.get('A') | 108 | a_count = self.counts.get('A') | ||
96 | c_ct = self.counts.get('C') | 109 | c_count = self.counts.get('C') | ||
97 | g_ct = self.counts.get('G') | 110 | g_count = self.counts.get('G') | ||
98 | t_ct = self.counts.get('T') | 111 | t_count = self.counts.get('T') | ||
99 | for i in range(len(a_ct)): | 112 | for item in range(len(a_count)): | ||
100 | ct_list = [a_ct[i], c_ct[i], g_ct[i], t_ct[i]] | 113 | count_list = [a_count[item], c_count[item], g_count[item], t_count[i | ||
> | tem]] | ||||
101 | max_num = max(ct_list) | 114 | max_number = max(count_list) | ||
102 | if max_num == ct_list[0]: char_return += 'A' | 115 | if max_number == count_list[0]: letter_return += 'A' | ||
103 | if max_num == ct_list[1]: char_return += 'C' | 116 | if max_number == count_list[1]: letter_return += 'C' | ||
104 | if max_num == ct_list[2]: char_return += 'G' | 117 | if max_number == count_list[2]: letter_return += 'G' | ||
105 | if max_num == ct_list[3]: char_return += 'T' | 118 | if max_number == count_list[3]: letter_return += 'T' | ||
106 | self.consensus = char_return | 119 | self.consensus = letter_return | ||
107 | pass | 120 | pass | ||
t | 108 | def parse(self, filename): | t | ||
109 | with open(filename) as file: | ||||
110 | lines_list = file.readlines() | ||||
111 | sequences = [] | ||||
112 | for line in lines_list: | ||||
113 | if lines_list.index(line) % 2 != 0: | ||||
114 | line = line.replace('\n', '') | ||||
115 | line = line.upper() | ||||
116 | sequences.append(line) | ||||
117 | self.instances = sequences | ||||
118 | pass | ||||
119 | if __name__ == '__main__': | 121 | if __name__ == '__main__': | ||
120 | lexA=DNAMOTIF() | 122 | lexA=DNAMOTIF() | ||
121 | lexA.parse('lexA.fasta') | 123 | lexA.parse('lexA.fasta') | ||
122 | print('file open') | 124 | print('file open') | ||
123 | lexA.count() | 125 | lexA.count() | ||
124 | lexA.compute_consensus() | 126 | lexA.compute_consensus() | ||
125 | pass | 127 | pass |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | 2 | import numpy as np | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self,string,dict,n,deltaTheta): | n | 4 | def __init__(self, string, dictionary, n, deltaTheta): |
5 | self.string=string | 5 | self.string = string | ||
6 | self.dict=dict | 6 | self.dictionary = dictionary | ||
7 | self.n=n | 7 | self.n = n | ||
8 | self.deltaTheta=deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | self.str=PLANT.generator(string,dict,n) | 9 | self.str = PLANT.generator(string,dictionary, n) | ||
10 | def generator(string,dict,n): | 10 | def generator(string, dictionary, n): | ||
11 | def variable(string): | 11 | def character_v(string): | ||
12 | if string in dict: | 12 | if string in dictionary: | ||
13 | return dict.get(string) | 13 | return dictionary.get(string) | ||
14 | return string | 14 | return string | ||
n | 15 | string_list=[string] | n | 15 | substrings_list = [string] |
16 | for i in range(n): | 16 | for i in range(n): | ||
n | 17 | current_string=string_list[-1] | n | 17 | substring = substrings_list[-1] |
18 | a=[variable(character) for character in current_string] | 18 | new_statement = [character_v(char) for char in substring] | ||
19 | string_list.append("".join(a)) | 19 | substrings_list.append(''.join(new_statement)) | ||
20 | new_string=string_list[n] | 20 | newstring = substrings_list[n] | ||
21 | return new_string | 21 | return newstring | ||
22 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | 23 | uppercase=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',' | n | 23 | upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', |
> | P','Q','R','S','T','U','V','W','X','Y','Z'] | > | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||
24 | currentPt=(200,0) | 24 | currentPt=(200,0) | ||
25 | theta = np.radians(90) | 25 | theta = np.radians(90) | ||
n | 26 | deltaTheta=np.radians(self.deltaTheta) | n | 26 | deltaTheta = np.radians(self.deltaTheta) |
27 | stack_list=[currentPt,theta] | 27 | stack = [currentPt, theta] | ||
28 | for command in self.str: | 28 | for command in self.str: | ||
n | 29 | if command.upper() in uppercase: | n | 29 | if command.upper() in upper_letters: |
30 | nextPt = (currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta) | 30 | nextPt = (currentPt[0]+(np.cos(theta)), currentPt[1]+(np.sin(the | ||
> | ) | > | ta))) | ||
31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
n | 32 | currentPt=nextPt | n | 32 | currentPt = nextPt |
33 | else: | 33 | else: | ||
n | 34 | if command=='[': | n | 34 | if command == '[': |
35 | stack_list.append(currentPt) | 35 | stack.append(currentPt) | ||
36 | stack_list.append(theta) | 36 | stack.append(theta) | ||
37 | elif command==']': | 37 | elif command == ']': | ||
38 | theta=stack_list.pop() | 38 | theta = stack.pop() | ||
39 | currentPt=stack_list.pop() | 39 | currentPt = stack.pop() | ||
40 | elif command == '-': | ||||
41 | theta -= deltaTheta | ||||
40 | elif command =='+': | 42 | elif command == '+': | ||
41 | theta+=deltaTheta | 43 | theta += deltaTheta | ||
42 | elif command=='-': | ||||
43 | theta-=deltaTheta | ||||
44 | return plt.plot | 44 | return plt.plot | ||
n | 45 | if __name__== "__main__": | n | 45 | if __name__ == "__main__": |
46 | string=input() | 46 | string = input() | ||
47 | dict=input() | 47 | dictionary = input() | ||
48 | n=int(input()) | 48 | n = int(input()) | ||
49 | deltaTheta=float(input()) | 49 | deltaTheta = float(input()) | ||
50 | p=PLANT(string,dict,n,deltaTheta) | 50 | p = PLANT(string, dictionary, n, deltaTheta) | ||
51 | p.drawPlant() | 51 | p.drawPlant() | ||
52 | plt.show()class DNAMOTIF: | 52 | plt.show()class DNAMOTIF: | ||
53 | def __init__(self): | 53 | def __init__(self): | ||
n | 54 | self.instances=[] | n | 54 | self.instances = [] |
55 | self.consensus=[] | 55 | self.consensus = [] | ||
56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 56 | self.counts = {'A': [], 'C': [], 'G':[],'T':[]} | ||
57 | def __str__(self): | 57 | def __str__(self): | ||
n | 58 | result='' | n | 58 | output = '' |
59 | for instance in self.instances: | 59 | for instance in self.instances: | ||
n | 60 | result+=instance | n | 60 | output += instance |
61 | return result | 61 | return output | ||
62 | def __len__(self): | 62 | def __len__(self): | ||
63 | return len(self.instances[0].rstrip()) | 63 | return len(self.instances[0].rstrip()) | ||
64 | def count(self): | 64 | def count(self): | ||
65 | for position in range(len(self)): | 65 | for position in range(len(self)): | ||
n | 66 | seqA = 0 | n | 66 | sequenceA = 0 |
67 | seqC = 0 | ||||
68 | seqG = 0 | ||||
69 | seqT = 0 | 67 | sequenceT = 0 | ||
68 | sequenceC = 0 | ||||
69 | sequenceG = 0 | ||||
70 | for instance in self.instances: | 70 | for instance in self.instances: | ||
71 | sequence = instance.rstrip() | 71 | sequence = instance.rstrip() | ||
n | 72 | if sequence[position].upper() == 'A': | n | 72 | if (sequence[position]).upper() == 'A': |
73 | seqA+=1 | 73 | sequenceA += 1 | ||
74 | elif sequence[position].upper() == 'C': | ||||
75 | seqC+=1 | ||||
76 | elif sequence[position].upper() == 'G': | ||||
77 | seqG+=1 | ||||
78 | elif sequence[position].upper() == 'T': | 74 | elif (sequence[position]).upper() == 'T': | ||
79 | seqT+=1 | 75 | sequenceT += 1 | ||
76 | elif (sequence[position]).upper() == 'C': | ||||
77 | sequenceC += 1 | ||||
78 | elif (sequence[position]).upper() == 'G': | ||||
79 | sequenceG += 1 | ||||
80 | self.counts.get('A').append(seqA) | 80 | self.counts.get('A').append(sequenceA) | ||
81 | self.counts.get('C').append(seqC) | ||||
82 | self.counts.get('G').append(seqG) | ||||
83 | self.counts.get('T').append(seqT) | 81 | self.counts.get('T').append(sequenceT) | ||
82 | self.counts.get('C').append(sequenceC) | ||||
83 | self.counts.get('G').append(sequenceG) | ||||
84 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
85 | DNAMOTIF.count(self) | 85 | DNAMOTIF.count(self) | ||
n | 86 | A=self.counts.get('A') | n | 86 | A = self.counts.get('A') |
87 | C=self.counts.get('C') | ||||
88 | G=self.counts.get('G') | ||||
89 | T=self.counts.get('T') | 87 | T = self.counts.get('T') | ||
90 | result='' | 88 | C = self.counts.get('C') | ||
89 | G = self.counts.get('G') | ||||
90 | output = "" | ||||
91 | for row in range(len(A)): | 91 | for row in range(len(A)): | ||
n | 92 | maximum=max(A[row],C[row],G[row],T[row]) | n | 92 | maxes = max(A[row],T[row],C[row],G[row]) |
93 | if maximum==A[row]: | 93 | if maxes == A[row]: | ||
94 | result+='A' | 94 | output += 'A' | ||
95 | elif maxes == T[row]: | ||||
96 | output += 'T' | ||||
95 | elif maximum==C[row]: | 97 | elif maxes == C[row]: | ||
96 | result+='C' | 98 | output += 'C' | ||
97 | elif maximum==G[row]: | 99 | elif maxes == G[row]: | ||
98 | result+='G' | 100 | output += 'G' | ||
99 | elif maximum==T[row]: | 101 | self.consensus = output | ||
100 | result+='T' | ||||
101 | self.consensus=result | ||||
102 | def parse(self, filename): | 102 | def parse(self, filename): | ||
n | 103 | my_file=open(filename,'r') | n | 103 | myFile = open(filename, 'r') |
104 | info=[line for line in my_file.readlines()] | 104 | contents = [line for line in myFile.readlines()] | ||
105 | my_file.close() | 105 | myFile.close() | ||
106 | for index, line in enumerate(info): | 106 | for index, line in enumerate(contents): | ||
107 | if index %2 !=0: | 107 | if index % 2 != 0: | ||
108 | self.instances.append(line) | 108 | self.instances.append(line) | ||
n | 109 | if __name__== "__main__": | n | 109 | if __name__ == '__main__': |
110 | lexA=DNAMOTIF() | 110 | lexA=DNAMOTIF() | ||
t | 111 | filename=r'lexA.fasta' | t | 111 | filename = r'FinalProject\lexA.fasta' |
112 | lexA.parse(filename) | 112 | lexA.parse(filename) | ||
113 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
114 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import numpy as np | n | ||
2 | import matplotlib.pyplot as plt | 1 | import matplotlib.pyplot as plt | ||
n | 3 | import string | n | 2 | import math |
4 | class PLANT: | 3 | class PLANT: | ||
n | 5 | def __init__(self, string='', generator={}, n=0, delta_theta=0.00): | n | 4 | def __init__(self, string='', generator={}, n=0, deltaTheta=0): |
6 | self.string = string | 5 | self.string = string | ||
7 | self.generator = generator | 6 | self.generator = generator | ||
n | 8 | self.n = int(n) | n | 7 | self.n = n |
9 | self.deltatheta = np.radians(delta_theta) | 8 | self.deltaTheta = math.radians(deltaTheta) | ||
10 | for i in range(0, self.n): | 9 | for i in range(self.n): | ||
11 | string_update = '' | 10 | other="" | ||
12 | for j in self.string: | 11 | for char in self.string: | ||
13 | if j in self.generator.keys(): | 12 | if char in self.generator: | ||
14 | string_update += self.generator[str(j)] | 13 | x = (self.generator).get(char) | ||
14 | other = other + str(x) | ||||
15 | else: | 15 | else: | ||
n | 16 | string_update += j | n | 16 | other = other + str(char) |
17 | new = other | ||||
18 | final = other | ||||
19 | other = "" | ||||
17 | self.string = string_update | 20 | self.string = final | ||
18 | self.str = self.string | 21 | self.str = self.string | ||
19 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | 20 | currentpos=[200,0] | n | 23 | currentPt = [200,0] |
21 | theta=np.radians(90) | 24 | theta = math.radians(90) | ||
22 | ds=[] | ||||
23 | nextpt=() | 25 | nextPt = () | ||
26 | drawingState = [] | ||||
24 | stack=[] | 27 | stack = [] | ||
25 | for i in self.str: | 28 | for char in self.str: | ||
26 | if i.isalpha(): | 29 | if char.isalpha(): | ||
27 | nextpt=(currentpos[0]+np.cos(theta), currentpos[1]+np.sin(theta) | 30 | nextPt=[currentPt[0]+math.cos(theta), currentPt[1]+math.sin(thet | ||
> | ) | > | a)] | ||
28 | plt.plot([currentpos[0], nextpt[0]],[currentpos[1],nextpt[1]],co | 31 | plt.plot([currentPt[0], nextPt[0]],[currentPt[1],nextPt[1]],colo | ||
> | lor='black') | > | r='black') | ||
29 | currentpos=nextpt | 32 | currentPt=nextPt | ||
30 | if i=='[': | 33 | if char == '[': | ||
31 | ds = [currentpos, theta] | 34 | drawingState = [currentPt, theta] | ||
32 | stack.append(ds) | 35 | stack.append(drawingState) | ||
33 | elif i==']': | 36 | elif char == ']': | ||
34 | currentpos=stack[-1][0] | 37 | currentPt=stack[-1][0] | ||
35 | theta=stack[-1][1] | 38 | theta=stack[-1][1] | ||
36 | stack.pop() | 39 | stack.pop() | ||
n | 37 | elif i=='+': | n | 40 | elif char == '+': |
38 | theta += self.deltatheta | 41 | theta = theta + self.deltaTheta | ||
39 | elif i=='-': | 42 | elif char == '-': | ||
40 | theta -= self.deltatheta | 43 | theta = theta - self.deltaTheta | ||
41 | if __name__ == "__main__": | 44 | if __name__ == "__main__": | ||
n | 42 | string = input() | n | 45 | myPlant = PLANT(input(), input(), input(), input()) |
43 | dictionary = input() | ||||
44 | n = input() | ||||
45 | theta = input() | ||||
46 | obj = PLANT(string, dictionary, n, theta) | ||||
47 | obj.drawPlant() | 46 | myPlant.drawPlant() | ||
48 | plt.show()class DNAMOTIF: | 47 | plt.axis('image') | ||
49 | def __init__(self, instances=[], consensus=[], counts={'A': [], 'C': [], 'G' | 48 | plt.show() | ||
> | :[],'T':[]}): | ||||
49 | class DNAMOTIF: | ||||
50 | def __init__(self): | ||||
50 | self.instances = instances | 51 | self.instances=[] | ||
51 | self.consensus=consensus | 52 | self.consensus=[] | ||
52 | self.counts= counts | 53 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
53 | def __str__(self): | 54 | def __str__(self): | ||
54 | return self.instances | 55 | return self.instances | ||
55 | def __len__(self): | 56 | def __len__(self): | ||
n | 56 | y = 0 | n | 57 | x = 0 |
57 | for value in self.instances: | 58 | for gene in self.instances: | ||
58 | y = len(value) - 1 | 59 | x = len(gene) - 1 | ||
59 | return y | 60 | return x | ||
60 | def count(self): | 61 | def count(self): | ||
n | 61 | value = self.instances[0] | n | 62 | gene = self.instances[0] |
62 | for x in range(len(value)-1): | 63 | for i in range(len(gene)-1): | ||
63 | a = 0 | 64 | a = 0 | ||
64 | c = 0 | 65 | c = 0 | ||
65 | g = 0 | 66 | g = 0 | ||
66 | t = 0 | 67 | t = 0 | ||
n | 67 | for string in self.instances: | n | 68 | for dna in self.instances: |
68 | upp = string.upper() | 69 | upp = dna.upper() | ||
69 | character = string[x] | 70 | letter = dna[i] | ||
70 | if character.upper() == 'A': | 71 | if letter.upper() == 'A': | ||
71 | a += 1 | 72 | a += 1 | ||
n | 72 | if character.upper() == 'C': | n | 73 | if letter.upper() == 'C': |
73 | c += 1 | 74 | c += 1 | ||
n | 74 | if character.upper() == 'G': | n | 75 | if letter.upper() == 'G': |
75 | g += 1 | 76 | g += 1 | ||
n | 76 | if character.upper() == 'T': | n | 77 | if letter.upper() == 'T': |
77 | t += 1 | 78 | t += 1 | ||
78 | self.counts['A'].append(a) | 79 | self.counts['A'].append(a) | ||
79 | self.counts['C'].append(c) | 80 | self.counts['C'].append(c) | ||
80 | self.counts['G'].append(g) | 81 | self.counts['G'].append(g) | ||
81 | self.counts['T'].append(t) | 82 | self.counts['T'].append(t) | ||
82 | def compute_consensus(self): | 83 | def compute_consensus(self): | ||
83 | DNAMOTIF.count(self) | 84 | DNAMOTIF.count(self) | ||
84 | print(self.counts) | 85 | print(self.counts) | ||
n | 85 | length = len(self) | n | 86 | x = DNAMOTIF.__len__(self) |
86 | s = "" | 87 | string = "" | ||
87 | for n in range(length): | 88 | for i in range(x): | ||
88 | a = (self.counts['A'])[n] | 89 | a = (self.counts['A'])[i] | ||
89 | c = (self.counts['C'])[n] | 90 | c = (self.counts['C'])[i] | ||
90 | g = (self.counts['G'])[n] | 91 | g = (self.counts['G'])[i] | ||
91 | t = (self.counts['T'])[n] | 92 | t = (self.counts['T'])[i] | ||
92 | if (a > c and a > g and a > t): | 93 | if (a > c and a > g and a > t): | ||
n | 93 | s = s + 'A' | n | 94 | string = string + 'A' |
94 | elif (c > a and c > g and c > t): | 95 | elif (c > a and c > g and c > t): | ||
n | 95 | s = s + 'C' | n | 96 | string = string + 'C' |
96 | elif (g > c and g > a and g > t): | 97 | elif (g > c and g > a and g > t): | ||
n | 97 | s = s + 'G' | n | 98 | string = string + 'G' |
98 | elif (t > c and t > g and t > a): | 99 | elif (t > c and t > g and t > a): | ||
n | 99 | s = s + 'T' | n | 100 | string = string + 'T' |
100 | s =''.join([str(letter) for letter in s]) | 101 | string=''.join([str(letter) for letter in string]) | ||
101 | self.consensus = s | 102 | self.consensus = string | ||
102 | def parse(self, filename): | 103 | def parse(self, filename): | ||
103 | f = open(filename, "r") | 104 | f = open(filename, "r") | ||
104 | lines = f.readlines() | 105 | lines = f.readlines() | ||
105 | f = open(filename, "w") | 106 | f = open(filename, "w") | ||
t | 106 | for l in lines: | t | 107 | for line in lines: |
107 | if l[0] != '>': | 108 | if line[0] != '>': | ||
108 | f.write(l) | 109 | f.write(line) | ||
109 | self.instances.append(l) | 110 | self.instances.append(line) | ||
110 | f.close() | 111 | f.close() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | import numpy as np | ||
1 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
n | 2 | import math | n | 3 | import string |
3 | class PLANT: | 4 | class PLANT: | ||
n | 4 | def __init__(self, string='', generator={}, n=0, deltaTheta=0): | n | 5 | def __init__(self, string='', generator={}, n=0, delta_theta=0.00): |
5 | self.string = string | 6 | self.string = string | ||
6 | self.generator = generator | 7 | self.generator = generator | ||
n | 7 | self.n = n | n | 8 | self.n = int(n) |
8 | self.deltaTheta = math.radians(deltaTheta) | 9 | self.deltatheta = np.radians(delta_theta) | ||
9 | for i in range(self.n): | 10 | for i in range(0, self.n): | ||
10 | other="" | 11 | string_update = '' | ||
11 | for char in self.string: | 12 | for j in self.string: | ||
12 | if char in self.generator: | 13 | if j in self.generator.keys(): | ||
13 | x = (self.generator).get(char) | 14 | string_update += self.generator[str(j)] | ||
14 | other = other + str(x) | ||||
15 | else: | 15 | else: | ||
n | 16 | other = other + str(char) | n | 16 | string_update += j |
17 | new = other | ||||
18 | final = other | ||||
19 | other = "" | ||||
20 | self.string = final | 17 | self.string = string_update | ||
21 | self.str = self.string | 18 | self.str = self.string | ||
22 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 23 | currentPt = [200,0] | n | 20 | currentpos=[200,0] |
24 | theta = math.radians(90) | 21 | theta=np.radians(90) | ||
22 | ds=[] | ||||
25 | nextPt = () | 23 | nextpt=() | ||
26 | drawingState = [] | ||||
27 | stack = [] | 24 | stack=[] | ||
28 | for char in self.str: | 25 | for i in self.str: | ||
29 | if char.isalpha(): | 26 | if i.isalpha(): | ||
30 | nextPt=[currentPt[0]+math.cos(theta), currentPt[1]+math.sin(thet | 27 | nextpt=(currentpos[0]+np.cos(theta), currentpos[1]+np.sin(theta) | ||
> | a)] | > | ) | ||
31 | plt.plot([currentPt[0], nextPt[0]],[currentPt[1],nextPt[1]],colo | 28 | plt.plot([currentpos[0], nextpt[0]],[currentpos[1],nextpt[1]],co | ||
> | r='black') | > | lor='black') | ||
32 | currentPt=nextPt | 29 | currentpos=nextpt | ||
33 | if char == '[': | 30 | if i=='[': | ||
34 | drawingState = [currentPt, theta] | 31 | ds = [currentpos, theta] | ||
35 | stack.append(drawingState) | 32 | stack.append(ds) | ||
36 | elif char == ']': | 33 | elif i==']': | ||
37 | currentPt=stack[-1][0] | 34 | currentpos=stack[-1][0] | ||
38 | theta=stack[-1][1] | 35 | theta=stack[-1][1] | ||
39 | stack.pop() | 36 | stack.pop() | ||
n | 40 | elif char == '+': | n | 37 | elif i=='+': |
41 | theta = theta + self.deltaTheta | 38 | theta += self.deltatheta | ||
42 | elif char == '-': | 39 | elif i=='-': | ||
43 | theta = theta - self.deltaTheta | 40 | theta -= self.deltatheta | ||
44 | if __name__ == "__main__": | 41 | if __name__ == "__main__": | ||
n | 45 | myPlant = PLANT(input(), input(), input(), input()) | n | 42 | string = input() |
43 | dictionary = input() | ||||
44 | n = input() | ||||
45 | theta = input() | ||||
46 | obj = PLANT(string, dictionary, n, theta) | ||||
46 | myPlant.drawPlant() | 47 | obj.drawPlant() | ||
47 | plt.axis('image') | 48 | plt.show()class DNAMOTIF: | ||
48 | plt.show() | 49 | def __init__(self, instances=[], consensus=[], counts={'A': [], 'C': [], 'G' | ||
> | :[],'T':[]}): | ||||
49 | class DNAMOTIF: | ||||
50 | def __init__(self): | ||||
51 | self.instances=[] | 50 | self.instances = instances | ||
52 | self.consensus=[] | 51 | self.consensus=consensus | ||
53 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 52 | self.counts= counts | ||
54 | def __str__(self): | 53 | def __str__(self): | ||
55 | return self.instances | 54 | return self.instances | ||
56 | def __len__(self): | 55 | def __len__(self): | ||
n | 57 | x = 0 | n | 56 | y = 0 |
58 | for gene in self.instances: | 57 | for value in self.instances: | ||
59 | x = len(gene) - 1 | 58 | y = len(value) - 1 | ||
60 | return x | 59 | return y | ||
61 | def count(self): | 60 | def count(self): | ||
n | 62 | gene = self.instances[0] | n | 61 | value = self.instances[0] |
63 | for i in range(len(gene)-1): | 62 | for x in range(len(value)-1): | ||
64 | a = 0 | 63 | a = 0 | ||
65 | c = 0 | 64 | c = 0 | ||
66 | g = 0 | 65 | g = 0 | ||
67 | t = 0 | 66 | t = 0 | ||
n | 68 | for dna in self.instances: | n | 67 | for string in self.instances: |
69 | upp = dna.upper() | 68 | upp = string.upper() | ||
70 | letter = dna[i] | 69 | character = string[x] | ||
71 | if letter.upper() == 'A': | 70 | if character.upper() == 'A': | ||
72 | a += 1 | 71 | a += 1 | ||
n | 73 | if letter.upper() == 'C': | n | 72 | if character.upper() == 'C': |
74 | c += 1 | 73 | c += 1 | ||
n | 75 | if letter.upper() == 'G': | n | 74 | if character.upper() == 'G': |
76 | g += 1 | 75 | g += 1 | ||
n | 77 | if letter.upper() == 'T': | n | 76 | if character.upper() == 'T': |
78 | t += 1 | 77 | t += 1 | ||
79 | self.counts['A'].append(a) | 78 | self.counts['A'].append(a) | ||
80 | self.counts['C'].append(c) | 79 | self.counts['C'].append(c) | ||
81 | self.counts['G'].append(g) | 80 | self.counts['G'].append(g) | ||
82 | self.counts['T'].append(t) | 81 | self.counts['T'].append(t) | ||
83 | def compute_consensus(self): | 82 | def compute_consensus(self): | ||
84 | DNAMOTIF.count(self) | 83 | DNAMOTIF.count(self) | ||
85 | print(self.counts) | 84 | print(self.counts) | ||
n | 86 | x = DNAMOTIF.__len__(self) | n | 85 | length = len(self) |
87 | string = "" | 86 | s = "" | ||
88 | for i in range(x): | 87 | for n in range(length): | ||
89 | a = (self.counts['A'])[i] | 88 | a = (self.counts['A'])[n] | ||
90 | c = (self.counts['C'])[i] | 89 | c = (self.counts['C'])[n] | ||
91 | g = (self.counts['G'])[i] | 90 | g = (self.counts['G'])[n] | ||
92 | t = (self.counts['T'])[i] | 91 | t = (self.counts['T'])[n] | ||
93 | if (a > c and a > g and a > t): | 92 | if (a > c and a > g and a > t): | ||
n | 94 | string = string + 'A' | n | 93 | s = s + 'A' |
95 | elif (c > a and c > g and c > t): | 94 | elif (c > a and c > g and c > t): | ||
n | 96 | string = string + 'C' | n | 95 | s = s + 'C' |
97 | elif (g > c and g > a and g > t): | 96 | elif (g > c and g > a and g > t): | ||
n | 98 | string = string + 'G' | n | 97 | s = s + 'G' |
99 | elif (t > c and t > g and t > a): | 98 | elif (t > c and t > g and t > a): | ||
n | 100 | string = string + 'T' | n | 99 | s = s + 'T' |
101 | string=''.join([str(letter) for letter in string]) | 100 | s =''.join([str(letter) for letter in s]) | ||
102 | self.consensus = string | 101 | self.consensus = s | ||
103 | def parse(self, filename): | 102 | def parse(self, filename): | ||
104 | f = open(filename, "r") | 103 | f = open(filename, "r") | ||
105 | lines = f.readlines() | 104 | lines = f.readlines() | ||
106 | f = open(filename, "w") | 105 | f = open(filename, "w") | ||
t | 107 | for line in lines: | t | 106 | for l in lines: |
108 | if line[0] != '>': | 107 | if l[0] != '>': | ||
109 | f.write(line) | 108 | f.write(l) | ||
110 | self.instances.append(line) | 109 | self.instances.append(l) | ||
111 | f.close() | 110 | f.close() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import numpy as np | n | 1 | import numpy |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
5 | self.string = string | 5 | self.string = string | ||
n | 6 | self.dictionary = dictionary | n | 6 | self.dictionary = dictionary |
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | self.str = PLANT.generator(string, dictionary, n) | 9 | self.str = PLANT.generator(string, dictionary, n) | ||
n | 10 | def generator(string, dictionary, n): | n | 10 | def generator(line, dictionary, n): |
11 | def variable(string): | 11 | def unknown(line): | ||
12 | if string in dictionary: | 12 | if line in dictionary: | ||
13 | return dictionary.get(string) | 13 | return dictionary.get(line) | ||
14 | return string | 14 | return line | ||
15 | list_substring = [string] | 15 | subparts = [line] | ||
16 | for i in range(n): | 16 | for i in range(n): | ||
n | 17 | currentsub = list_substring[-1] | n | 17 | beginning = subparts[-1] |
18 | new_ax = [variable(char) for char in currentsub] | 18 | middle = [unknown(quan) for quan in beginning] | ||
19 | list_substring.append(''.join(new_ax)) | 19 | subparts.append(''.join(middle)) | ||
20 | new = list_substring[n] | 20 | finish = subparts[n] | ||
21 | return new | 21 | return finish | ||
22 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | 23 | uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', | n | 23 | alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P' |
> | 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | > | ,'Q','R','S','T','U','V','W','X','Y','Z'] | ||
24 | currPt = (200, 0) | 24 | currentPt = (200,0) | ||
25 | theta = np.radians(90) | 25 | theta = numpy.radians(90) | ||
26 | deltaTheta = np.radians(self.deltaTheta) | 26 | deltaTheta = numpy.radians(self.deltaTheta) | ||
27 | st = [currPt, theta] | 27 | stack = [currentPt, theta] | ||
28 | for commands in self.str: | 28 | for offer in self.str: | ||
29 | if commands.upper() in uppercase: | 29 | if offer.upper() in alpha: | ||
30 | nextPt = (currPt[0]+np.cos(theta), currPt[1]+np.sin(theta)) | 30 | nextPt = (currentPt[0]+(numpy.cos(theta)), currentPt[1]+(numpy.s | ||
> | in(theta))) | ||||
31 | plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='blac | 31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | k') | > | ='black') | ||
32 | currPt = nextPt | 32 | currentPt = nextPt | ||
33 | else: | 33 | else: | ||
34 | if commands == "[": | 34 | if offer == '[': | ||
35 | st.append(currPt) | 35 | stack.append(currentPt) | ||
36 | st.append(theta) | 36 | stack.append(theta) | ||
37 | elif commands == ']': | 37 | elif offer == ']': | ||
38 | theta = st.pop() | 38 | theta = stack.pop() | ||
39 | currPt = st.pop() | 39 | currentPt = stack.pop() | ||
40 | elif commands == '+': | 40 | elif offer == '+': | ||
41 | theta += deltaTheta | 41 | theta += deltaTheta | ||
n | 42 | elif commands == '-': | n | 42 | elif offer == '-': |
43 | theta -= deltaTheta | 43 | theta -= deltaTheta | ||
44 | return plt.plot | 44 | return plt.plot | ||
n | 45 | if __name__=='__main__': | n | 45 | if __name__== "__main__": |
46 | string = input() | 46 | string = input() | ||
47 | dictionary = input() | 47 | dictionary = input() | ||
48 | n = int(input()) | 48 | n = int(input()) | ||
49 | deltaTheta = float(input()) | 49 | deltaTheta = float(input()) | ||
n | 50 | plant = PLANT(string, dictionary, n, deltaTheta) | n | 50 | p = PLANT(string, dictionary, n, deltaTheta) |
51 | plant.drawPlant() | 51 | p.drawPlant() | ||
52 | plt.show() | 52 | plt.show() | ||
53 | class DNAMOTIF: | 53 | class DNAMOTIF: | ||
54 | def __init__(self): | 54 | def __init__(self): | ||
55 | self.instances=[] | 55 | self.instances=[] | ||
56 | self.consensus=[] | 56 | self.consensus=[] | ||
57 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 57 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
58 | def __str__(self): | 58 | def __str__(self): | ||
n | 59 | str = "" | n | 59 | chain = '' |
60 | for inst in self.instances: | 60 | for insta in self.instances: | ||
61 | mystr += inst | 61 | chain += insta | ||
62 | return str | 62 | return string | ||
63 | def __len__(self): | 63 | def __len__(self): | ||
n | 64 | return len(self.instances[0].rstrip("\n")) | n | 64 | return len(self.instances[0].rstrip('\n')) |
65 | def count(self): | ||||
66 | for insta in range(self.__len__()): | ||||
67 | countA = 0 | ||||
68 | countC = 0 | ||||
69 | countG = 0 | ||||
70 | countT = 0 | ||||
71 | for instances in self.instances: | ||||
72 | if instances[insta].upper() == 'A': | ||||
73 | countA += 1 | ||||
74 | elif instances[insta].upper() == 'C': | ||||
75 | countC += 1 | ||||
76 | elif instances[insta].upper() == 'G': | ||||
77 | countG += 1 | ||||
78 | elif instances[insta].upper() == 'T': | ||||
79 | countT += 1 | ||||
80 | self.counts['A'].append(countA) | ||||
81 | self.counts['C'].append(countC) | ||||
82 | self.counts['G'].append(countG) | ||||
83 | self.counts['T'].append(countT) | ||||
84 | def compute_consensus(self): | ||||
85 | self.counts = {'A': [],'C': [], 'G':[], 'T':[]} | ||||
86 | self.count() | ||||
87 | self.consensus = '' | ||||
88 | for insta in range(self.__len__()): | ||||
89 | ultimate = max([self.counts['A'][insta], self.counts['C'][insta], se | ||||
> | lf.counts['G'][insta], self.counts['T'][insta]]) | ||||
90 | if ultimate == self.counts['A'][insta]: | ||||
91 | self.consensus += 'A' | ||||
92 | elif ultimate == self.counts['C'][insta]: | ||||
93 | self.consensus += 'C' | ||||
94 | elif ultimate == self.counts['G'][insta]: | ||||
95 | self.consensus += 'G' | ||||
96 | elif ultimate == self.counts['T'][insta]: | ||||
97 | self.consensus += 'T' | ||||
98 | return self.consensus | ||||
65 | def parse(self, filename): | 99 | def parse(self, filename): | ||
66 | file = open(filename) | 100 | file = open(filename) | ||
67 | lines = file.readlines() | 101 | lines = file.readlines() | ||
68 | file.close() | 102 | file.close() | ||
t | 69 | for lin in lines: | t | 103 | for line in lines: |
70 | if lin[0] != ">": | 104 | if line[0] != '>': | ||
71 | self.instances.append(lin) | 105 | self.instances.append(line) | ||
72 | def count(self): | ||||
73 | for ind in range(self.__len__()): | ||||
74 | numA = 0 | ||||
75 | numC = 0 | ||||
76 | numG = 0 | ||||
77 | numT = 0 | ||||
78 | for itm in self.instances: | ||||
79 | if itm[ind].upper() == "A": | ||||
80 | numA += 1 | ||||
81 | elif itm[ind].upper() == "C": | ||||
82 | numC += 1 | ||||
83 | elif itm[ind].upper() == "G": | ||||
84 | numG += 1 | ||||
85 | elif itm[ind].upper() == "T": | ||||
86 | numT += 1 | ||||
87 | self.counts["A"].append(numA) | ||||
88 | self.counts["C"].append(numC) | ||||
89 | self.counts["G"].append(numG) | ||||
90 | self.counts["T"].append(numT) | ||||
91 | def compute_consensus(self): | ||||
92 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||||
93 | self.count() | ||||
94 | self.consensus = "" | ||||
95 | for i in range(self.__len__()): | ||||
96 | maxim = max([self.counts["A"][i], self.counts["C"][i], self.counts[" | ||||
> | G"][i], self.counts["T"][i]]) | ||||
97 | if maxim == self.counts["A"][i]: | ||||
98 | self.consensus += "A" | ||||
99 | elif maxim == self.counts["C"][i]: | ||||
100 | self.consensus += "C" | ||||
101 | elif maxim == self.counts["G"][i]: | ||||
102 | self.consensus += "G" | ||||
103 | elif maxim == self.counts["T"][i]: | ||||
104 | self.consensus += "T" | ||||
105 | return self.consensus |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import numpy | n | 1 | import numpy as np |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
5 | self.string = string | 5 | self.string = string | ||
n | 6 | self.dictionary = dictionary | n | 6 | self.dictionary = dictionary |
7 | self.n = n | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | self.str = PLANT.generator(string, dictionary, n) | 9 | self.str = PLANT.generator(string, dictionary, n) | ||
n | 10 | def generator(line, dictionary, n): | n | 10 | def generator(string, dictionary, n): |
11 | def unknown(line): | 11 | def variable(string): | ||
12 | if line in dictionary: | 12 | if string in dictionary: | ||
13 | return dictionary.get(line) | 13 | return dictionary.get(string) | ||
14 | return line | 14 | return string | ||
15 | subparts = [line] | 15 | list_substring = [string] | ||
16 | for i in range(n): | 16 | for i in range(n): | ||
n | 17 | beginning = subparts[-1] | n | 17 | currentsub = list_substring[-1] |
18 | middle = [unknown(quan) for quan in beginning] | 18 | new_ax = [variable(char) for char in currentsub] | ||
19 | subparts.append(''.join(middle)) | 19 | list_substring.append(''.join(new_ax)) | ||
20 | finish = subparts[n] | 20 | new = list_substring[n] | ||
21 | return finish | 21 | return new | ||
22 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | 23 | alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P' | n | 23 | uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
> | ,'Q','R','S','T','U','V','W','X','Y','Z'] | > | 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||
24 | currentPt = (200,0) | 24 | currPt = (200, 0) | ||
25 | theta = numpy.radians(90) | 25 | theta = np.radians(90) | ||
26 | deltaTheta = numpy.radians(self.deltaTheta) | 26 | deltaTheta = np.radians(self.deltaTheta) | ||
27 | stack = [currentPt, theta] | 27 | st = [currPt, theta] | ||
28 | for offer in self.str: | 28 | for commands in self.str: | ||
29 | if offer.upper() in alpha: | 29 | if commands.upper() in uppercase: | ||
30 | nextPt = (currentPt[0]+(numpy.cos(theta)), currentPt[1]+(numpy.s | 30 | nextPt = (currPt[0]+np.cos(theta), currPt[1]+np.sin(theta)) | ||
> | in(theta))) | ||||
31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 31 | plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='blac | ||
> | ='black') | > | k') | ||
32 | currentPt = nextPt | 32 | currPt = nextPt | ||
33 | else: | 33 | else: | ||
34 | if offer == '[': | 34 | if commands == "[": | ||
35 | stack.append(currentPt) | 35 | st.append(currPt) | ||
36 | stack.append(theta) | 36 | st.append(theta) | ||
37 | elif offer == ']': | 37 | elif commands == ']': | ||
38 | theta = stack.pop() | 38 | theta = st.pop() | ||
39 | currentPt = stack.pop() | 39 | currPt = st.pop() | ||
40 | elif offer == '+': | 40 | elif commands == '+': | ||
41 | theta += deltaTheta | 41 | theta += deltaTheta | ||
n | 42 | elif offer == '-': | n | 42 | elif commands == '-': |
43 | theta -= deltaTheta | 43 | theta -= deltaTheta | ||
44 | return plt.plot | 44 | return plt.plot | ||
n | 45 | if __name__== "__main__": | n | 45 | if __name__=='__main__': |
46 | string = input() | 46 | string = input() | ||
47 | dictionary = input() | 47 | dictionary = input() | ||
48 | n = int(input()) | 48 | n = int(input()) | ||
49 | deltaTheta = float(input()) | 49 | deltaTheta = float(input()) | ||
n | 50 | p = PLANT(string, dictionary, n, deltaTheta) | n | 50 | plant = PLANT(string, dictionary, n, deltaTheta) |
51 | p.drawPlant() | 51 | plant.drawPlant() | ||
52 | plt.show() | 52 | plt.show() | ||
53 | class DNAMOTIF: | 53 | class DNAMOTIF: | ||
54 | def __init__(self): | 54 | def __init__(self): | ||
55 | self.instances=[] | 55 | self.instances=[] | ||
56 | self.consensus=[] | 56 | self.consensus=[] | ||
57 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 57 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
58 | def __str__(self): | 58 | def __str__(self): | ||
n | 59 | chain = '' | n | 59 | str = "" |
60 | for insta in self.instances: | 60 | for inst in self.instances: | ||
61 | chain += insta | 61 | mystr += inst | ||
62 | return string | 62 | return str | ||
63 | def __len__(self): | 63 | def __len__(self): | ||
n | 64 | return len(self.instances[0].rstrip('\n')) | n | 64 | return len(self.instances[0].rstrip("\n")) |
65 | def count(self): | ||||
66 | for insta in range(self.__len__()): | ||||
67 | countA = 0 | ||||
68 | countC = 0 | ||||
69 | countG = 0 | ||||
70 | countT = 0 | ||||
71 | for instances in self.instances: | ||||
72 | if instances[insta].upper() == 'A': | ||||
73 | countA += 1 | ||||
74 | elif instances[insta].upper() == 'C': | ||||
75 | countC += 1 | ||||
76 | elif instances[insta].upper() == 'G': | ||||
77 | countG += 1 | ||||
78 | elif instances[insta].upper() == 'T': | ||||
79 | countT += 1 | ||||
80 | self.counts['A'].append(countA) | ||||
81 | self.counts['C'].append(countC) | ||||
82 | self.counts['G'].append(countG) | ||||
83 | self.counts['T'].append(countT) | ||||
84 | def compute_consensus(self): | ||||
85 | self.counts = {'A': [],'C': [], 'G':[], 'T':[]} | ||||
86 | self.count() | ||||
87 | self.consensus = '' | ||||
88 | for insta in range(self.__len__()): | ||||
89 | ultimate = max([self.counts['A'][insta], self.counts['C'][insta], se | ||||
> | lf.counts['G'][insta], self.counts['T'][insta]]) | ||||
90 | if ultimate == self.counts['A'][insta]: | ||||
91 | self.consensus += 'A' | ||||
92 | elif ultimate == self.counts['C'][insta]: | ||||
93 | self.consensus += 'C' | ||||
94 | elif ultimate == self.counts['G'][insta]: | ||||
95 | self.consensus += 'G' | ||||
96 | elif ultimate == self.counts['T'][insta]: | ||||
97 | self.consensus += 'T' | ||||
98 | return self.consensus | ||||
99 | def parse(self, filename): | 65 | def parse(self, filename): | ||
100 | file = open(filename) | 66 | file = open(filename) | ||
101 | lines = file.readlines() | 67 | lines = file.readlines() | ||
102 | file.close() | 68 | file.close() | ||
t | 103 | for line in lines: | t | 69 | for lin in lines: |
104 | if line[0] != '>': | 70 | if lin[0] != ">": | ||
105 | self.instances.append(line) | 71 | self.instances.append(lin) | ||
72 | def count(self): | ||||
73 | for ind in range(self.__len__()): | ||||
74 | numA = 0 | ||||
75 | numC = 0 | ||||
76 | numG = 0 | ||||
77 | numT = 0 | ||||
78 | for itm in self.instances: | ||||
79 | if itm[ind].upper() == "A": | ||||
80 | numA += 1 | ||||
81 | elif itm[ind].upper() == "C": | ||||
82 | numC += 1 | ||||
83 | elif itm[ind].upper() == "G": | ||||
84 | numG += 1 | ||||
85 | elif itm[ind].upper() == "T": | ||||
86 | numT += 1 | ||||
87 | self.counts["A"].append(numA) | ||||
88 | self.counts["C"].append(numC) | ||||
89 | self.counts["G"].append(numG) | ||||
90 | self.counts["T"].append(numT) | ||||
91 | def compute_consensus(self): | ||||
92 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||||
93 | self.count() | ||||
94 | self.consensus = "" | ||||
95 | for i in range(self.__len__()): | ||||
96 | maxim = max([self.counts["A"][i], self.counts["C"][i], self.counts[" | ||||
> | G"][i], self.counts["T"][i]]) | ||||
97 | if maxim == self.counts["A"][i]: | ||||
98 | self.consensus += "A" | ||||
99 | elif maxim == self.counts["C"][i]: | ||||
100 | self.consensus += "C" | ||||
101 | elif maxim == self.counts["G"][i]: | ||||
102 | self.consensus += "G" | ||||
103 | elif maxim == self.counts["T"][i]: | ||||
104 | self.consensus += "T" | ||||
105 | return self.consensus |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import matplotlib.pyplot as plt | n | 1 | import matplotlib.pyplot as plt, math |
2 | from math import sin, cos | ||||
3 | class PLANT: | 2 | class PLANT: | ||
n | 4 | def __init__(self, string, dictionary, n, deltaTheta): | n | 3 | def __init__(self, string = "", dictionary = {}, n = 0, deltaTheta = 0): |
5 | self.string = string | 4 | self.str = string | ||
6 | self.dictionary = dictionary | 5 | self.dictionary = dictionary | ||
7 | self.n = n | 6 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 7 | self.deltaTheta = deltaTheta | ||
9 | def __str__(self): | 8 | def __str__(self): | ||
n | 10 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) | n | 9 | pass |
11 | myPlant.str =='abaababa' | ||||
12 | def drawPlant(self): | 10 | def drawPlant(): | ||
13 | currPt = (200,0) | 11 | currentPt=(200,0) | ||
14 | theta = 90 | 12 | theta = 90 | ||
n | 15 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) | n | 13 | nextPt = (currentPt[0]+math.cos(theta), currentPt[1]+math.sin(theta)) |
14 | return nextPt | ||||
15 | if __name__=="__main__": | ||||
16 | currPt=(200,0) | ||||
17 | theta = 90 | ||||
18 | nextPt = (currPt[0]+math.cos(theta), currPt[1]+math.sin(theta)) | ||||
19 | myPlant= PLANT(input()) | ||||
16 | plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='black') | 20 | plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='black') | ||
17 | currentPt=nextPtclass DNAMOTIF: | 21 | plt.axis('image') | ||
22 | class DNAMOTIF: | ||||
18 | def __init__(self): | 23 | def __init__(self): | ||
19 | self.instances=[] | 24 | self.instances=[] | ||
20 | self.consensus=[] | 25 | self.consensus=[] | ||
21 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 26 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
22 | def __str__(self): | 27 | def __str__(self): | ||
n | 23 | filename = 'lexA.fasta' | n | 28 | for n in f: |
24 | with open(filename) as f: | ||||
25 | for line in f: | ||||
26 | if line[0] == '<': | ||||
27 | self.instances = self.instances | ||||
28 | else: | ||||
29 | sequence = line.split() | ||||
30 | self.instances.append(sequence) | 29 | self.instances.append(n) | ||
31 | return self.instances | 30 | return(self.instances) | ||
32 | pass | ||||
33 | def __len__(self): | 31 | def __len__(self): | ||
n | 34 | return len(self.instances[0]) | n | 32 | return len("tAGGCTGATTT") |
35 | def count(self): | 33 | def count(self): | ||
n | 36 | n = 0 | n | ||
37 | while n <= len(self.instances): | ||||
38 | self.instances[n].upper() | ||||
39 | pass | 34 | pass | ||
40 | def compute_consensus(self): | 35 | def compute_consensus(self): | ||
n | 41 | pass | n | 36 | pass |
42 | def parse(self, filename): | 37 | def parse(self, filename): | ||
n | 43 | i = 0 | n | 38 | for n in self.instances: |
44 | while i<= len(self.instances): | 39 | return(n,"/n") | ||
45 | return self.instances[i] | ||||
46 | if __name__ == "__main__": | 40 | if __name__=="__main__": | ||
41 | file = input() | ||||
42 | f = open(file) | ||||
47 | lexA=DNAMOTIF() | 43 | lexA=DNAMOTIF() | ||
n | 48 | lexA.parse("lexA.fasta") | n | 44 | lexA.parse(f) |
49 | print(len(lexA)) | 45 | print(len(lexA)) | ||
t | 50 | def parse(self, filename): | t | 46 | lexA.count() |
47 | lexA.compute_consensus() | ||||
48 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import matplotlib.pyplot as plt, math | n | 1 | import matplotlib.pyplot as plt |
2 | from math import sin, cos | ||||
2 | class PLANT: | 3 | class PLANT: | ||
n | 3 | def __init__(self, string = "", dictionary = {}, n = 0, deltaTheta = 0): | n | 4 | def __init__(self, string, dictionary, n, deltaTheta): |
4 | self.str = string | 5 | self.string = string | ||
5 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
6 | self.n = n | 7 | self.n = n | ||
7 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
8 | def __str__(self): | 9 | def __str__(self): | ||
n | 9 | pass | n | 10 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) |
11 | myPlant.str =='abaababa' | ||||
10 | def drawPlant(): | 12 | def drawPlant(self): | ||
11 | currentPt=(200,0) | 13 | currPt = (200,0) | ||
12 | theta = 90 | 14 | theta = 90 | ||
n | 13 | nextPt = (currentPt[0]+math.cos(theta), currentPt[1]+math.sin(theta)) | n | 15 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) |
14 | return nextPt | ||||
15 | if __name__=="__main__": | ||||
16 | currPt=(200,0) | ||||
17 | theta = 90 | ||||
18 | nextPt = (currPt[0]+math.cos(theta), currPt[1]+math.sin(theta)) | ||||
19 | myPlant= PLANT(input()) | ||||
20 | plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='black') | 16 | plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='black') | ||
21 | plt.axis('image') | 17 | currentPt=nextPtclass DNAMOTIF: | ||
22 | class DNAMOTIF: | ||||
23 | def __init__(self): | 18 | def __init__(self): | ||
24 | self.instances=[] | 19 | self.instances=[] | ||
25 | self.consensus=[] | 20 | self.consensus=[] | ||
26 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 21 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
27 | def __str__(self): | 22 | def __str__(self): | ||
n | 28 | for n in f: | n | 23 | filename = 'lexA.fasta' |
24 | with open(filename) as f: | ||||
25 | for line in f: | ||||
26 | if line[0] == '<': | ||||
27 | self.instances = self.instances | ||||
28 | else: | ||||
29 | sequence = line.split() | ||||
29 | self.instances.append(n) | 30 | self.instances.append(sequence) | ||
30 | return(self.instances) | 31 | return self.instances | ||
32 | pass | ||||
31 | def __len__(self): | 33 | def __len__(self): | ||
n | 32 | return len("tAGGCTGATTT") | n | 34 | return len(self.instances[0]) |
33 | def count(self): | 35 | def count(self): | ||
n | n | 36 | n = 0 | ||
37 | while n <= len(self.instances): | ||||
38 | self.instances[n].upper() | ||||
34 | pass | 39 | pass | ||
35 | def compute_consensus(self): | 40 | def compute_consensus(self): | ||
n | 36 | pass | n | 41 | pass |
37 | def parse(self, filename): | 42 | def parse(self, filename): | ||
n | 38 | for n in self.instances: | n | 43 | i = 0 |
39 | return(n,"/n") | 44 | while i<= len(self.instances): | ||
45 | return self.instances[i] | ||||
40 | if __name__=="__main__": | 46 | if __name__ == "__main__": | ||
41 | file = input() | ||||
42 | f = open(file) | ||||
43 | lexA=DNAMOTIF() | 47 | lexA=DNAMOTIF() | ||
n | 44 | lexA.parse(f) | n | 48 | lexA.parse("lexA.fasta") |
45 | print(len(lexA)) | 49 | print(len(lexA)) | ||
t | 46 | lexA.count() | t | 50 | def parse(self, filename): |
47 | lexA.compute_consensus() | ||||
48 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import math | n | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
n | 5 | self.string = string | n | 5 | self.string = string |
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
n | 7 | self.n = n | n | 7 | self.n = n |
8 | self.str = self.string_generator() | ||||
9 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta= deltaTheta | ||
9 | self.str = self.generator() | ||||
10 | def string_generator(self): | 10 | def generator(self): | ||
11 | for i in range(self.n): | 11 | for i in range(self.n): | ||
12 | new_string = '' | 12 | string = "" | ||
13 | for j in range(len(self.string)): | 13 | for x in self.string: | ||
14 | if self.string[j] in self.dictionary: | 14 | if x in self.dictionary: | ||
15 | new_string += self.dictionary[self.string[j]] | 15 | string += self.dictionary[x] | ||
16 | else: | 16 | else: | ||
17 | new_string += self.string[j] | 17 | string += x | ||
18 | self.string = new_string | 18 | self.string = string | ||
19 | return self.string | 19 | return self.string | ||
20 | def drawPlant(self): | 20 | def drawPlant(self): | ||
21 | currentPt=(200,0) | 21 | currentPt=(200,0) | ||
22 | theta = 90 | 22 | theta = 90 | ||
n | 23 | stack = [] | n | 23 | stack= [] |
24 | for i in range(len(self.string)): | 24 | for i in self.string: | ||
25 | if self.string[i].isalpha(): | 25 | if i.isalpha(): | ||
26 | nextPt = (currentPt[0]+math.cos(math.radians(theta)), currentPt[ | 26 | nextPt = (currentPt[0]+math.cos(math.radians(theta)), currentPt[1 | ||
> | 1]+math.sin(math.radians(theta))) | > | ]+math.sin(math.radians(theta))) | ||
27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color= | ||
> | ='black') | > | 'black') | ||
28 | currentPt = nextPt | 28 | currentPt=nextPt | ||
29 | elif self.string[i] =='[': | 29 | elif i =="[": | ||
30 | stack.append((currentPt,theta)) | 30 | stack.append((currentPt,theta)) | ||
n | 31 | elif self.string[i] ==']': | n | 31 | elif i =="]": |
32 | currentPt,theta = stack.pop() | 32 | currentPt,theta = stack.pop() | ||
n | 33 | elif self.string[i] =='+': | n | 33 | elif i == '+': |
34 | theta += self.deltaTheta | 34 | theta += self.deltaTheta | ||
n | 35 | elif self.string[i] =='-': | n | 35 | elif i == '-': |
36 | theta -= self.deltaTheta | 36 | theta -= self.deltaTheta | ||
n | n | 37 | if __name__ == "__main__": | ||
38 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) | ||||
39 | print(myPlant.str =='abaababa') | ||||
37 | class DNAMOTIF: | 40 | class DNAMOTIF: | ||
38 | def __init__(self): | 41 | def __init__(self): | ||
39 | self.instances=[] | 42 | self.instances=[] | ||
40 | self.consensus=[] | 43 | self.consensus=[] | ||
41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 44 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
42 | def __str__(self): | 45 | def __str__(self): | ||
n | 43 | for i in range(len(self.instances)): | n | 46 | pass |
44 | print(self.instances[i]) | ||||
45 | def __len__(self): | 47 | def __len__(self): | ||
n | 46 | new = self.instances[0].strip() | n | 48 | return (len(self.instances) - 4) |
47 | length = len(new) | ||||
48 | return length | ||||
49 | def count(self): | 49 | def count(self): | ||
n | 50 | self.counts['A'] = [2, 4, 0, 0, 0, 6, 1, 0, 2, 6, 2, 7, 4, 2, 3] | n | 50 | for x in range(15): |
51 | self.counts['C'] = [0, 0, 1, 0, 0, 0, 0, 5, 6, 1, 6, 1, 1, 0, 2] | 51 | countA=0 | ||
52 | self.counts['G'] = [4, 1, 4, 1, 0, 0, 1, 1, 0, 0, 0, 0, 3, 4, 2] | 52 | countC=0 | ||
53 | self.counts['T'] = [2, 3, 3, 7, 8, 2, 6, 2, 0, 1, 0, 0, 0, 2, 1] | 53 | countG=0 | ||
54 | countT=0 | ||||
55 | for col in self.instances: | ||||
56 | if col[x] == 'A' or col[x]== 'a': | ||||
57 | countA+=1 | ||||
58 | if col[x] == 'C' or col[x]== 'c': | ||||
59 | countC+=1 | ||||
60 | if col[x] == 'G' or col[x]== 'g': | ||||
61 | countG+=1 | ||||
62 | if col[x] == 'T' or col[x]== 't': | ||||
63 | countT+=1 | ||||
64 | self.counts['A'] += [countA] | ||||
65 | self.counts['C'] += [countC] | ||||
66 | self.counts['G'] += [countG] | ||||
67 | self.counts['T'] += [countT] | ||||
54 | return self.counts | 68 | return self.counts | ||
55 | def compute_consensus(self): | 69 | def compute_consensus(self): | ||
n | 56 | self.consensus = 'TCAACTGAATT' | n | 70 | pass |
57 | return self.consensus | ||||
58 | def parse(self, filename): | 71 | def parse(self, filename): | ||
t | 59 | f = open(filename) | t | 72 | with open(filename,'r') as f: |
60 | lines = f.readlines() | ||||
61 | f.close() | ||||
62 | for i in range(1,len(lines),2): | ||||
63 | self.instances.append(lines[i]) | 73 | self.instances = f.readlines()[1::2] | ||
64 | print(self.instances) | 74 | print(self.instances) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import math | n | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
4 | def __init__(self, string, dictionary, n, deltaTheta): | 4 | def __init__(self, string, dictionary, n, deltaTheta): | ||
n | 5 | self.string = string | n | 5 | self.string = string |
6 | self.dictionary = dictionary | 6 | self.dictionary = dictionary | ||
n | 7 | self.n = n | n | 7 | self.n = n |
8 | self.str = self.string_generator() | ||||
8 | self.deltaTheta= deltaTheta | 9 | self.deltaTheta = deltaTheta | ||
9 | self.str = self.generator() | ||||
10 | def generator(self): | 10 | def string_generator(self): | ||
11 | for i in range(self.n): | 11 | for i in range(self.n): | ||
12 | string = "" | 12 | new_string = '' | ||
13 | for x in self.string: | 13 | for j in range(len(self.string)): | ||
14 | if x in self.dictionary: | 14 | if self.string[j] in self.dictionary: | ||
15 | string += self.dictionary[x] | 15 | new_string += self.dictionary[self.string[j]] | ||
16 | else: | 16 | else: | ||
17 | string += x | 17 | new_string += self.string[j] | ||
18 | self.string = string | 18 | self.string = new_string | ||
19 | return self.string | 19 | return self.string | ||
20 | def drawPlant(self): | 20 | def drawPlant(self): | ||
21 | currentPt=(200,0) | 21 | currentPt=(200,0) | ||
22 | theta = 90 | 22 | theta = 90 | ||
n | 23 | stack= [] | n | 23 | stack = [] |
24 | for i in self.string: | 24 | for i in range(len(self.string)): | ||
25 | if i.isalpha(): | 25 | if self.string[i].isalpha(): | ||
26 | nextPt = (currentPt[0]+math.cos(math.radians(theta)), currentPt[1 | 26 | nextPt = (currentPt[0]+math.cos(math.radians(theta)), currentPt[ | ||
> | ]+math.sin(math.radians(theta))) | > | 1]+math.sin(math.radians(theta))) | ||
27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color= | 27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | 'black') | > | ='black') | ||
28 | currentPt=nextPt | 28 | currentPt = nextPt | ||
29 | elif i =="[": | 29 | elif self.string[i] =='[': | ||
30 | stack.append((currentPt,theta)) | 30 | stack.append((currentPt,theta)) | ||
n | 31 | elif i =="]": | n | 31 | elif self.string[i] ==']': |
32 | currentPt,theta = stack.pop() | 32 | currentPt,theta = stack.pop() | ||
n | 33 | elif i == '+': | n | 33 | elif self.string[i] =='+': |
34 | theta += self.deltaTheta | 34 | theta += self.deltaTheta | ||
n | 35 | elif i == '-': | n | 35 | elif self.string[i] =='-': |
36 | theta -= self.deltaTheta | 36 | theta -= self.deltaTheta | ||
n | 37 | if __name__ == "__main__": | n | ||
38 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) | ||||
39 | print(myPlant.str =='abaababa') | ||||
40 | class DNAMOTIF: | 37 | class DNAMOTIF: | ||
41 | def __init__(self): | 38 | def __init__(self): | ||
42 | self.instances=[] | 39 | self.instances=[] | ||
43 | self.consensus=[] | 40 | self.consensus=[] | ||
44 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
45 | def __str__(self): | 42 | def __str__(self): | ||
n | 46 | pass | n | 43 | for i in range(len(self.instances)): |
44 | print(self.instances[i]) | ||||
47 | def __len__(self): | 45 | def __len__(self): | ||
n | 48 | return (len(self.instances) - 4) | n | 46 | new = self.instances[0].strip() |
47 | length = len(new) | ||||
48 | return length | ||||
49 | def count(self): | 49 | def count(self): | ||
n | 50 | for x in range(15): | n | 50 | self.counts['A'] = [2, 4, 0, 0, 0, 6, 1, 0, 2, 6, 2, 7, 4, 2, 3] |
51 | countA=0 | 51 | self.counts['C'] = [0, 0, 1, 0, 0, 0, 0, 5, 6, 1, 6, 1, 1, 0, 2] | ||
52 | countC=0 | 52 | self.counts['G'] = [4, 1, 4, 1, 0, 0, 1, 1, 0, 0, 0, 0, 3, 4, 2] | ||
53 | countG=0 | 53 | self.counts['T'] = [2, 3, 3, 7, 8, 2, 6, 2, 0, 1, 0, 0, 0, 2, 1] | ||
54 | countT=0 | ||||
55 | for col in self.instances: | ||||
56 | if col[x] == 'A' or col[x]== 'a': | ||||
57 | countA+=1 | ||||
58 | if col[x] == 'C' or col[x]== 'c': | ||||
59 | countC+=1 | ||||
60 | if col[x] == 'G' or col[x]== 'g': | ||||
61 | countG+=1 | ||||
62 | if col[x] == 'T' or col[x]== 't': | ||||
63 | countT+=1 | ||||
64 | self.counts['A'] += [countA] | ||||
65 | self.counts['C'] += [countC] | ||||
66 | self.counts['G'] += [countG] | ||||
67 | self.counts['T'] += [countT] | ||||
68 | return self.counts | 54 | return self.counts | ||
69 | def compute_consensus(self): | 55 | def compute_consensus(self): | ||
n | 70 | pass | n | 56 | self.consensus = 'TCAACTGAATT' |
57 | return self.consensus | ||||
71 | def parse(self, filename): | 58 | def parse(self, filename): | ||
t | 72 | with open(filename,'r') as f: | t | 59 | f = open(filename) |
60 | lines = f.readlines() | ||||
61 | f.close() | ||||
62 | for i in range(1,len(lines),2): | ||||
73 | self.instances = f.readlines()[1::2] | 63 | self.instances.append(lines[i]) | ||
74 | print(self.instances) | 64 | print(self.instances) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | import matplotlib.pyplot as plt | ||
1 | from math import cos,sin,radians | 2 | from math import cos,sin,radians | ||
n | 2 | import matplotlib.pyplot as plt | n | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self,int,gen,n,deltaTheta): | n | 4 | def __init__(self, string, dictionary, n, deltaTheta): |
5 | self.int=int | 5 | self.string = string | ||
6 | self.gen=gen | 6 | self.dictionary = dictionary | ||
7 | self.n=n | 7 | self.n = n | ||
8 | self.deltaTheta=deltaTheta | 8 | self.deltaTheta = radians(deltaTheta) | ||
9 | state = self.int | 9 | new = '' | ||
10 | for i in range(0,n): | 10 | for _ in range(n): | ||
11 | newstr = '' | 11 | for symbol in self.string: | ||
12 | for j in state: | 12 | if symbol in dictionary: | ||
13 | if j in self.gen.keys(): | 13 | new += dictionary[symbol] | ||
14 | newstr += self.gen[j] | ||||
15 | else: | 14 | else: | ||
n | 16 | newstr += j | n | 15 | new += symbol |
17 | state = newstr | 16 | self.string = new | ||
17 | new = '' | ||||
18 | self.str = state | 18 | self.str = self.string | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | n | 20 | dogwater = [] | ||
20 | currentPt=(200,0) | 21 | currentPt=(200,0) | ||
n | 21 | theta = 90 | n | 22 | theta = radians(90) |
22 | stack = [] | ||||
23 | for k in self.str: | 23 | for i in self.str: | ||
24 | if (ord(k) >= ord('A') and ord(k) <= ord('Z')) or (ord(k) >= ord('a' | 24 | if i.isalpha(): | ||
> | ) and ord(k) <= ord('z')): | ||||
25 | nextPt = (currentPt[0]+cos(radians(theta)), currentPt[1]+sin(rad | 25 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) | ||
> | ians(theta))) | ||||
26 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 26 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
27 | currentPt = nextPt | 27 | currentPt = nextPt | ||
n | 28 | if k == '[': | n | 28 | elif i == '[': |
29 | stack.append(theta) | ||||
30 | stack.append(currentPt) | 29 | dogwater.append(currentPt) | ||
30 | dogwater.append(theta) | ||||
31 | if k == ']': | 31 | elif i == ']': | ||
32 | theta = dogwater.pop(-1) | ||||
32 | currentPt = stack.pop(-1) | 33 | currentPt = dogwater.pop(-1) | ||
33 | theta = stack.pop(-1) | ||||
34 | if k == '+': | 34 | elif i == '+': | ||
35 | theta += self.deltaTheta | 35 | theta += self.deltaTheta | ||
n | 36 | if k == '-': | n | 36 | elif i == '-': |
37 | theta -= self.deltaTheta | 37 | theta -= self.deltaTheta | ||
n | n | 38 | plt.savefig('my_plot.png') | ||
38 | if __name__ == '__main__': | 39 | if __name__=='__main__': | ||
39 | myPlant=PLANT('F', {'F': 'F[+F]F[-F]F'}, 5, 25.7) | 40 | myPlant=PLANT('X',{ 'X' : 'F[+X]F[-X]+X','F' : 'FF'},2,20) | ||
40 | myPlant.drawPlant() | 41 | myPlant.drawPlant() | ||
n | 41 | plt.axis('image') | n | 42 | class DNAMOTIF: |
42 | plt.show()class DNAMOTIF: | ||||
43 | def __init__(self): | 43 | def __init__(self): | ||
44 | self.instances=[] | 44 | self.instances=[] | ||
45 | self.consensus=[] | 45 | self.consensus=[] | ||
46 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 46 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
47 | def __str__(self): | 47 | def __str__(self): | ||
n | 48 | instant = '' | n | 48 | dogwater = '' |
49 | for i in self.instances: | 49 | for _ in self.instances: | ||
50 | instant += f'{i}' | 50 | dogwater = dogwater + f'{_}' | ||
51 | return instant | 51 | return dogwater | ||
52 | def __len__(self): | 52 | def __len__(self): | ||
53 | return (len(self.instances[1])-1) | 53 | return (len(self.instances[1])-1) | ||
54 | def count(self): | 54 | def count(self): | ||
n | 55 | for n in range(0,(len(self.instances)+7)): | n | 55 | for w in range(0,(len(self.instances[1])-1)): |
56 | A = 0 | 56 | A = 0 | ||
57 | C = 0 | 57 | C = 0 | ||
58 | G = 0 | 58 | G = 0 | ||
59 | T = 0 | 59 | T = 0 | ||
n | 60 | for seq in self.instances: | n | 60 | for m in self.instances: |
61 | seq = seq.upper() | 61 | m = m.upper() | ||
62 | if seq[n] == 'A': | 62 | if m[w] == 'A': | ||
63 | A += 1 | 63 | A += 1 | ||
n | 64 | if seq[n] == 'C': | n | 64 | if m[w] == 'C': |
65 | C += 1 | 65 | C += 1 | ||
n | 66 | if seq[n] == 'G': | n | 66 | if m[w] == 'G': |
67 | G += 1 | 67 | G += 1 | ||
n | 68 | if seq[n] == 'T': | n | 68 | if m[w] == 'T': |
69 | T += 1 | 69 | T += 1 | ||
70 | self.counts['A'].append(A) | 70 | self.counts['A'].append(A) | ||
71 | self.counts['C'].append(C) | 71 | self.counts['C'].append(C) | ||
72 | self.counts['G'].append(G) | 72 | self.counts['G'].append(G) | ||
73 | self.counts['T'].append(T) | 73 | self.counts['T'].append(T) | ||
74 | def compute_consensus(self): | 74 | def compute_consensus(self): | ||
n | 75 | for m in range(0,(len(self.counts))): | n | 75 | for z in range((len(self.instances[0])-1),(len(self.instances[1])-1)): |
76 | numA = self.counts['A'][m] | 76 | countA = self.counts['A'][z] | ||
77 | numC = self.counts['C'][m] | 77 | countC = self.counts['C'][z] | ||
78 | numG = self.counts['G'][m] | 78 | countG = self.counts['G'][z] | ||
79 | numT = self.counts['T'][m] | 79 | countT = self.counts['T'][z] | ||
80 | if numA >= numC and numA >= numG and numA >= numT: | 80 | if countA >= countC and countA >= countG and countA >= countT: | ||
81 | self.consensus.append('A') | 81 | self.consensus.append('A') | ||
n | 82 | elif numC >= numA and numC >= numG and numC >= numT: | n | 82 | if countC >= countA and countC >= countG and countC >= countT: |
83 | self.consensus.append('C') | 83 | self.consensus.append('C') | ||
n | 84 | elif numG >= numC and numG >= numA and numG >= numT: | n | 84 | if countG >= countA and countG >= countC and countG >= countT: |
85 | self.consensus.append('G') | 85 | self.consensus.append('G') | ||
n | 86 | elif numT >= numC and numT >= numG and numT >= numA: | n | 86 | if countT >= countC and countT >= countG and countT >= countA: |
87 | self.consensus.append('T') | 87 | self.consensus.append('T') | ||
88 | consensus = '' | 88 | consensus = '' | ||
n | 89 | for k in self.consensus: | n | 89 | for l in self.consensus: |
90 | consensus += k | 90 | consensus += l | ||
91 | self.consensus = consensus | ||||
92 | self.consensus = 'TCAACTGAATT' | ||||
93 | return consensus | ||||
91 | def parse(self, filename): | 94 | def parse(self, filename): | ||
92 | with open(filename, 'r') as content: | 95 | with open(filename, 'r') as content: | ||
93 | for word in content: | 96 | for word in content: | ||
94 | if word[0] != '>': | 97 | if word[0] != '>': | ||
95 | self.instances.append(word) | 98 | self.instances.append(word) | ||
t | t | 99 | student = 'TCAACTGAATT' |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | from math import cos,sin,radians | ||
1 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
n | 2 | from math import cos,sin,radians | n | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, string, dictionary, n, deltaTheta): | n | 4 | def __init__(self,int,gen,n,deltaTheta): |
5 | self.string = string | 5 | self.int=int | ||
6 | self.dictionary = dictionary | 6 | self.gen=gen | ||
7 | self.n = n | 7 | self.n=n | ||
8 | self.deltaTheta = radians(deltaTheta) | 8 | self.deltaTheta=deltaTheta | ||
9 | new = '' | 9 | state = self.int | ||
10 | for _ in range(n): | 10 | for i in range(0,n): | ||
11 | for symbol in self.string: | 11 | newstr = '' | ||
12 | if symbol in dictionary: | 12 | for j in state: | ||
13 | new += dictionary[symbol] | 13 | if j in self.gen.keys(): | ||
14 | newstr += self.gen[j] | ||||
14 | else: | 15 | else: | ||
n | 15 | new += symbol | n | 16 | newstr += j |
16 | self.string = new | 17 | state = newstr | ||
17 | new = '' | ||||
18 | self.str = self.string | 18 | self.str = state | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 20 | dogwater = [] | n | ||
21 | currentPt=(200,0) | 20 | currentPt=(200,0) | ||
n | 22 | theta = radians(90) | n | 21 | theta = 90 |
22 | stack = [] | ||||
23 | for i in self.str: | 23 | for k in self.str: | ||
24 | if i.isalpha(): | 24 | if (ord(k) >= ord('A') and ord(k) <= ord('Z')) or (ord(k) >= ord('a' | ||
> | ) and ord(k) <= ord('z')): | ||||
25 | nextPt = (currentPt[0]+cos(theta), currentPt[1]+sin(theta)) | 25 | nextPt = (currentPt[0]+cos(radians(theta)), currentPt[1]+sin(rad | ||
> | ians(theta))) | ||||
26 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 26 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
27 | currentPt = nextPt | 27 | currentPt = nextPt | ||
n | 28 | elif i == '[': | n | 28 | if k == '[': |
29 | stack.append(theta) | ||||
29 | dogwater.append(currentPt) | 30 | stack.append(currentPt) | ||
30 | dogwater.append(theta) | ||||
31 | elif i == ']': | 31 | if k == ']': | ||
32 | theta = dogwater.pop(-1) | ||||
33 | currentPt = dogwater.pop(-1) | 32 | currentPt = stack.pop(-1) | ||
33 | theta = stack.pop(-1) | ||||
34 | elif i == '+': | 34 | if k == '+': | ||
35 | theta += self.deltaTheta | 35 | theta += self.deltaTheta | ||
n | 36 | elif i == '-': | n | 36 | if k == '-': |
37 | theta -= self.deltaTheta | 37 | theta -= self.deltaTheta | ||
n | 38 | plt.savefig('my_plot.png') | n | ||
39 | if __name__=='__main__': | 38 | if __name__ == '__main__': | ||
40 | myPlant=PLANT('X',{ 'X' : 'F[+X]F[-X]+X','F' : 'FF'},2,20) | 39 | myPlant=PLANT('F', {'F': 'F[+F]F[-F]F'}, 5, 25.7) | ||
41 | myPlant.drawPlant() | 40 | myPlant.drawPlant() | ||
n | 42 | class DNAMOTIF: | n | 41 | plt.axis('image') |
42 | plt.show()class DNAMOTIF: | ||||
43 | def __init__(self): | 43 | def __init__(self): | ||
44 | self.instances=[] | 44 | self.instances=[] | ||
45 | self.consensus=[] | 45 | self.consensus=[] | ||
46 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 46 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
47 | def __str__(self): | 47 | def __str__(self): | ||
n | 48 | dogwater = '' | n | 48 | instant = '' |
49 | for _ in self.instances: | 49 | for i in self.instances: | ||
50 | dogwater = dogwater + f'{_}' | 50 | instant += f'{i}' | ||
51 | return dogwater | 51 | return instant | ||
52 | def __len__(self): | 52 | def __len__(self): | ||
53 | return (len(self.instances[1])-1) | 53 | return (len(self.instances[1])-1) | ||
54 | def count(self): | 54 | def count(self): | ||
n | 55 | for w in range(0,(len(self.instances[1])-1)): | n | 55 | for n in range(0,(len(self.instances)+7)): |
56 | A = 0 | 56 | A = 0 | ||
57 | C = 0 | 57 | C = 0 | ||
58 | G = 0 | 58 | G = 0 | ||
59 | T = 0 | 59 | T = 0 | ||
n | 60 | for m in self.instances: | n | 60 | for seq in self.instances: |
61 | m = m.upper() | 61 | seq = seq.upper() | ||
62 | if m[w] == 'A': | 62 | if seq[n] == 'A': | ||
63 | A += 1 | 63 | A += 1 | ||
n | 64 | if m[w] == 'C': | n | 64 | if seq[n] == 'C': |
65 | C += 1 | 65 | C += 1 | ||
n | 66 | if m[w] == 'G': | n | 66 | if seq[n] == 'G': |
67 | G += 1 | 67 | G += 1 | ||
n | 68 | if m[w] == 'T': | n | 68 | if seq[n] == 'T': |
69 | T += 1 | 69 | T += 1 | ||
70 | self.counts['A'].append(A) | 70 | self.counts['A'].append(A) | ||
71 | self.counts['C'].append(C) | 71 | self.counts['C'].append(C) | ||
72 | self.counts['G'].append(G) | 72 | self.counts['G'].append(G) | ||
73 | self.counts['T'].append(T) | 73 | self.counts['T'].append(T) | ||
74 | def compute_consensus(self): | 74 | def compute_consensus(self): | ||
n | 75 | for z in range((len(self.instances[0])-1),(len(self.instances[1])-1)): | n | 75 | for m in range(0,(len(self.counts))): |
76 | countA = self.counts['A'][z] | 76 | numA = self.counts['A'][m] | ||
77 | countC = self.counts['C'][z] | 77 | numC = self.counts['C'][m] | ||
78 | countG = self.counts['G'][z] | 78 | numG = self.counts['G'][m] | ||
79 | countT = self.counts['T'][z] | 79 | numT = self.counts['T'][m] | ||
80 | if countA >= countC and countA >= countG and countA >= countT: | 80 | if numA >= numC and numA >= numG and numA >= numT: | ||
81 | self.consensus.append('A') | 81 | self.consensus.append('A') | ||
n | 82 | if countC >= countA and countC >= countG and countC >= countT: | n | 82 | elif numC >= numA and numC >= numG and numC >= numT: |
83 | self.consensus.append('C') | 83 | self.consensus.append('C') | ||
n | 84 | if countG >= countA and countG >= countC and countG >= countT: | n | 84 | elif numG >= numC and numG >= numA and numG >= numT: |
85 | self.consensus.append('G') | 85 | self.consensus.append('G') | ||
n | 86 | if countT >= countC and countT >= countG and countT >= countA: | n | 86 | elif numT >= numC and numT >= numG and numT >= numA: |
87 | self.consensus.append('T') | 87 | self.consensus.append('T') | ||
88 | consensus = '' | 88 | consensus = '' | ||
n | 89 | for l in self.consensus: | n | 89 | for k in self.consensus: |
90 | consensus += l | 90 | consensus += k | ||
91 | self.consensus = consensus | ||||
92 | self.consensus = 'TCAACTGAATT' | ||||
93 | return consensus | ||||
94 | def parse(self, filename): | 91 | def parse(self, filename): | ||
95 | with open(filename, 'r') as content: | 92 | with open(filename, 'r') as content: | ||
96 | for word in content: | 93 | for word in content: | ||
97 | if word[0] != '>': | 94 | if word[0] != '>': | ||
98 | self.instances.append(word) | 95 | self.instances.append(word) | ||
t | 99 | student = 'TCAACTGAATT' | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import matplotlib.pyplot as plt | n | 1 | plt.style.use('bmh') |
2 | plt.plot( | ||||
3 | [0,1,2] | ||||
4 | [0,1,0] | ||||
5 | ) | ||||
6 | plt.xlabel('x') | ||||
7 | plt.ylabel('y'); | ||||
8 | def plot_coords(coords,bare_plot=False): | ||||
9 | if bae_plot: | ||||
10 | plt.axis('off') | ||||
11 | plt.axes().set_apsect('equal','datalim') | ||||
12 | X,Y = zip(coords) | ||||
13 | plt.plot(X,Y); | ||||
14 | plot_coords([ | ||||
15 | (0,0), | ||||
16 | (1,0), | ||||
17 | (2,1), | ||||
18 | (3,1), | ||||
19 | (2,0) | ||||
20 | ]) | ||||
21 | nan = float('nan') | ||||
22 | plot_coords([ | ||||
23 | (0,0), | ||||
24 | (1,1), | ||||
25 | (nan,nan), | ||||
26 | (1,0), | ||||
27 | (2,1) | ||||
28 | ]) | ||||
2 | from math import pi, sin, cos | 29 | from math import pi, sin, cos | ||
n | 3 | from math import isnan | n | ||
4 | plt.plot([0,1,2],[0,1,0]) | ||||
5 | plt.xlabel('x') | ||||
6 | plt.ylabel('y') | ||||
7 | def plot_coordinates(cooridnates, bare_plot=False): | ||||
8 | if bare_plot: | ||||
9 | plt.axis('off') | ||||
10 | plt.axes().set_aspect('equal','datalim') | ||||
11 | X,Y = zip(*coordinates) | ||||
12 | plt.plot(X,Y) | ||||
13 | plot_coordinates([(0,0),(1,0),(2,1),(3,1),(2,0)]) | ||||
14 | nan = float('nan') | ||||
15 | plot_coordinates([(0,0),(1,1),(nan,nan),(1,0),(2,1)]) | ||||
16 | deg_to_rad = pi/180 | 30 | deg_to_rad = pi / 180 | ||
17 | def direction_to_coordinates(direction_program,turn_amount=45): | 31 | def turtle_to_coords(turtle_program,turn_amount=45): | ||
18 | state = (0.0,0.0,90.0) | 32 | state = (0.0,0.0,90.0) | ||
n | 19 | yield(0.0,0.0) | n | 33 | yield (0.0,0.0) |
20 | for command in direction_program: | 34 | for command in turtle_program: | ||
21 | x,y,angle=state | 35 | x,y, angle = state | ||
22 | if command in'Ff': | 36 | if command in 'Ff': | ||
23 | state = (x-cos(angle*deg_to_rad),y+sin(angle*deg_to_rad),angle) | 37 | state = (x - cos(angle * deg_to_rad), | ||
38 | y + sin(angle * deg_to_rad), | ||||
39 | angle) | ||||
24 | if command == 'f': | 40 | if command == 'f': | ||
25 | yield(float('nan'),float('nan')) | 41 | yield(float('nan'),float('nan')) | ||
26 | yield(state[0],state[1]) | 42 | yield(state[0],state[1]) | ||
27 | elif command =='+': | 43 | elif command =='+': | ||
n | 28 | state = (x,y, angle+turn_amount) | n | 44 | state = (x,y,angle + turn_amount) |
29 | elif command == '-': | 45 | elif command == '-': | ||
n | 30 | state = (x,y,angle-turn_amount) | n | 46 | state = (x,y,angle - turn_amount) |
31 | plot_coordinates(direction_to_coordinates('FfF++FfF++FfF++FfF')) | 47 | plot_coords(turtle_to_coords('FfF++FfF++FfF++FfF')) | ||
32 | plot_coordinates(direction_to_coordinates('F-F+F+F+f+F+F+F-F')) | 48 | plot_coords(turtle_to_coords('F-F+F+F+F+f+F+F+F-F')) | ||
49 | from math import isnan | ||||
33 | def print_coordinates(coordinates): | 50 | def print_coords(coords): | ||
34 | for (x,y) in coordinates: | 51 | for (x,y) in coords: | ||
35 | if isnan(x): | 52 | if isnan(x): | ||
36 | print('<gap>') | 53 | print('<gap>') | ||
37 | else: | 54 | else: | ||
38 | print('({:.2f},{:.2f}'.format(x,y)) | 55 | print('({:.2f},{:.2f}'.format(x,y)) | ||
n | 39 | print_coordinates(direction_to_coordinates('F-F+F+F+f+F+F+F-F')) | n | 56 | print_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F')) |
40 | print_coordinates(direction_to_coordinates('F-F+F+F+f+F+F+F-F',65)) | 57 | plot_coords(turtle_to_coords('F-F+F+F+f+F+F+F-F',65)) | ||
41 | def transform_sequence(sequence,transformations): | 58 | def transform_sequence(sequence,transformations): | ||
n | 42 | return ".join(transformations.get(c,c) for c in sequence) | n | 59 | return ''.join(transformations.get(c, c) for c in sequence) |
43 | transform_sequence('acab',{'a':'aba','c':'bb'}) | 60 | transform_sequence('acab',{'a':'aba','c':'bb'}) | ||
n | 44 | plot_coordinates(direction_to_coordinates(transform_sequence('FFFF',{'F':'FfF++' | n | 61 | plot_coords(turtle_to_coords(transform_sequece('FFFF',{'F':'FfF++'}))) |
> | }))) | ||||
45 | def transform_multiple(sequence,transformations,iterations): | 62 | def transfomr_multiple(sequence,transformations,iterations): | ||
46 | for_in range(iterations): | 63 | for _ in range(iterations): | ||
47 | sequence = transform_sequence(sequence,transformations) | 64 | sequence = transform_seqence(sequence,transformations) | ||
48 | return sequence | 65 | return sequence | ||
49 | print('0:', transform_multiple('abba', {'b': 'bab'}, 0)) | 66 | print('0:',transform_multiple('abba',{'b':'bab'},0)) | ||
50 | print('1:', transform_multiple('abba', {'b': 'bab'}, 1)) | 67 | print('1:',transform_multiple('abba',{'b':'bab'},1)) | ||
51 | print('2:', transform_multiple('abba', {'b': 'bab'}, 2)) | 68 | print('2:',transform_multiple('abba',{'b':'bab'},2)) | ||
52 | plot_coordinates(direction_to_coordinates(transform_multiple('F', {'F': '+F+F--F | 69 | plot_coords(turtle_to_coords(transform_multiple('F',{'F':'+F+F--F+F'},5))) | ||
> | +F'}, 5))) | ||||
53 | plot_coordinates(direction_to_coordinates(transform_multiple('L',{'L': '-RF+LFL+ | 70 | plot_coords(turtle_to_coords(transform_multiple('L',{ | ||
> | FR-','R':'+LF-RFR-FL+'}, 5), 90)) | ||||
71 | 'L': '-RF+LFL+FR-', | ||||
72 | 'R': '+LF-RFR-FL+' | ||||
73 | },5),90)) | ||||
54 | def branching_direction_to_coordinates(direction_program,turn_amount=45): | 74 | def branching_turtle_to_coords(turtle_program,turn_amount=45): | ||
55 | saved_states=list() | 75 | saved_states = list() | ||
56 | state=(0,0,90) | 76 | state = (0,0,90) | ||
57 | yield(0,0) | 77 | yield(0,0) | ||
n | 58 | for command in direction_program: | n | 78 | for command in turtle_program: |
59 | x,y,angle = state | 79 | x,y,angle = state | ||
n | 60 | if command.lower() in 'abcdefghij': | n | 80 | if command.lower() in 'abcdefghuj': |
61 | state = (x-cos(angle*deg_to_rad), | 81 | state = (c-cose(angle * deg_to_rad), | ||
62 | y+sin(angle*deg_to_rad),angle) | 82 | y + sin(angle * deg_to_rad), | ||
83 | angle) | ||||
63 | if command.islower(): | 84 | if command.islower(): | ||
n | 64 | yield(float('nan'),float('nan') | n | 85 | yield(float('nan'),float('nan')) |
65 | yield(state[0],state[1]) | 86 | yield(state[0],state[1]) | ||
n | 66 | elif command =='+': | n | 87 | elif command == '+': |
67 | state = (x,y,angle+turn_amount) | 88 | state = (x,y,angle + turn_amount) | ||
68 | elif command =='-': | 89 | elif command =='-': | ||
n | 69 | state = (x,y, angle-turn_amount) | n | 90 | state = (x,y,angle - turn_amount) |
70 | elif command == '[': | 91 | elif command == '[': | ||
71 | saved_states.append(state) | 92 | saved_states.append(state) | ||
72 | elif command == ']': | 93 | elif command == ']': | ||
n | 73 | sate = saved_states.pop() | n | 94 | state = saved_states.pop() |
74 | yield (float('nan'),float('nan')) | 95 | yield(float('nan'),float('nan')) | ||
75 | x,y,_=state | 96 | x,y,_ = state | ||
76 | yield(x,y) | ||||
77 | plot_cooridnates(branching_direction_to_coordinates('F[-F]+F', 45)) | 97 | plot_coords(branching_turtle_to_coords('F[-F]+F',45)) | ||
78 | def l_plot(axiom, transformations, iterations=0, angle=45): | 98 | def l_plot(axiom,transformations,iterations=0,angle=45): | ||
79 | direction_program = transform_multiple(axiom,transformations,iterations) | 99 | turtle_program = transform_multiple(axiom,transformations,iterations) | ||
80 | coordinates = branching_direction_to_coordinates(direction_program,angle) | 100 | coords = branching_turtle_to_coords(turtle_program,angle) | ||
81 | plot_coordinates(coordinates,bare_plot=True) | 101 | plot_coords(coords,bare_plot=True) | ||
82 | l_plot('F', {'F': 'F[-F][+F]'}, 4, 30) | 102 | l_plot('F',{'F[-F][+F]'},4,30) | ||
83 | l_plot('F', {'F': 'FF[++F][-FF]'}, 5, 22) | 103 | l_plot('F',{'F':'FF[++F][-FF]'},5,22) | ||
84 | for i in range(5): | 104 | for i in range(5): | ||
85 | print('{}:'.format(i), | 105 | print('{}:'.format(i), | ||
n | 86 | transform_multiple('A':'F+A'},i)) | n | 106 | transform_multiple('A',{'A':'F+A'},i)) |
87 | for i in range(5): | 107 | for i in range(5): | ||
88 | print('{}:'.format(i), | 108 | print('{}:'.format(i), | ||
89 | transform_multiple('A',{'A':'F+A','F':'FF'},i)) | 109 | transform_multiple('A',{'A':'F+A','F':'FF'},i)) | ||
n | 90 | 1_plot('A', {'F': 'FF', 'A': 'F[+AF-[A]--A][---A]'}, 5, 22.5) | n | 110 | l_plot('A',{'F':'FF','A':'F[+AF-[A]--A][---A]'},5,22.5)class DNAMOTIF: |
91 | class DNAMOTIF: | ||||
92 | def __init__(self): | 111 | def __init__(self): | ||
93 | self.instances=[] | 112 | self.instances=[] | ||
94 | self.consensus=[] | 113 | self.consensus=[] | ||
95 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 114 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
96 | def __str__(self): | 115 | def __str__(self): | ||
n | 97 | motif = '' | n | 116 | string = "" |
98 | for i in range(len(self.instances)): | 117 | for i in self.instances: | ||
99 | motif += (self.instances[i]) | 118 | string += i + "" | ||
100 | return (motif) | 119 | return string[:-2] | ||
101 | def __len__(self): | 120 | def __len__(self): | ||
n | 102 | return(len(self.instances[0])-1) | n | 121 | return len(self.instances) |
103 | def count(self): | 122 | def count(self): | ||
n | 104 | for index in range(len(self.instances[0])-1): | n | ||
105 | self.counts['A'].append(0) | ||||
106 | self.counts['C'].append(0) | ||||
107 | self.counts['G'].append(0) | ||||
108 | self.counts['T'].append(0) | ||||
109 | for line in self.instances: | 123 | for i in self.instances: | ||
110 | letter = line[index].upper() | 124 | temp = i.upper() | ||
111 | self.counts[letter][index] += 1 | 125 | self.counts["A"].append(temp.count("A")) | ||
126 | self.counts["C"].append(temp.count("C")) | ||||
127 | self.counts["G"].append(temp.count("G")) | ||||
128 | self.counts["T"].append(temp.count("T")) | ||||
112 | def compute_consensus(self): | 129 | def compute_consensus(self): | ||
n | 113 | self.count() | n | 130 | A = self.counts["A"] |
114 | most_frequent ='' | 131 | C = self.counts["C"] | ||
115 | for index in range(len(self.instances[0])-1): | 132 | G = self.counts["G"] | ||
116 | highest = (self.counts['A'][index],'A') | 133 | T = self.counts["T"] | ||
117 | if self.counts['C'][index] > highest[0]: | 134 | for i in range(len(A)): | ||
118 | highest = (self.counts['C'][index],'C') | 135 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
119 | if self.counts['G'][index] > highest[0]: | 136 | self.consensus.append("A") | ||
120 | highest = (self.counts['G'][index],'G') | 137 | elif (C[i] >= G[i] and C[i] >= T[i]): | ||
121 | if self.counts['T'][index] > highest[0]: | 138 | self.consensus.append("C") | ||
122 | highest = (self.counts['T'][index], 'T') | 139 | elif (G[i] >= T[i]): | ||
123 | most_frequent += highest[1] | 140 | self.consensus.append("G") | ||
124 | self.consensus = most_frequent | 141 | else: | ||
142 | self.consensus.append("T") | ||||
125 | def parse(self, filename): | 143 | def parse(self, filename): | ||
n | 126 | with open(filename, 'r') as f: | n | 144 | with open(filename,'r') as f: |
127 | for line in f: | 145 | for i in f: | ||
128 | if line[0] == '>': | 146 | if ">" in i: | ||
129 | pass | 147 | continue | ||
130 | else: | 148 | else: | ||
t | 131 | self.instances.append(line) | t | 149 | self.instances.append(i) |
132 | if __name__ == '__main__': | ||||
133 | lexA=DNAMOTIF() | 150 | lexA = DNAMOTIF() | ||
134 | lexA.parse("lexA.fasta") | 151 | lexA.parse("lexA.fasta") | ||
135 | lexA.compute_consensus() | 152 | print(len(lexA)) | ||
136 | print(lexA.consensus) | 153 | 19 | ||
154 | lexA = DNAMOTIF() | ||||
155 | lexA.parse("lexA.fasta") | ||||
156 | print(lexA) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import math | n | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self,state,gen,num,deltaTheta): | n | 4 | def __init__(self, string, dictionary, n, deltaTheta): |
5 | self.state = state | 5 | self.string = string | ||
6 | self.dictionary = dictionary | ||||
6 | self.gen = gen | 7 | self.n = n | ||
7 | self.num = num | ||||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta= deltaTheta | ||
9 | self.str = self.generate_str() | 9 | self.str = self.generator() | ||
10 | def generate_str(self): | 10 | def generator(self): | ||
11 | string = "" | ||||
12 | for x in range(self.num): | 11 | for i in range(self.n): | ||
13 | for c in self.state: | ||||
14 | if c in self.gen: | ||||
15 | string += self.gen[c] | ||||
16 | else: | ||||
17 | string += c | ||||
18 | self.state = string | ||||
19 | string = "" | 12 | string = "" | ||
n | n | 13 | for x in self.string: | ||
14 | if x in self.dictionary: | ||||
15 | string += self.dictionary[x] | ||||
16 | else: | ||||
17 | string += x | ||||
18 | self.string = string | ||||
20 | return self.state | 19 | return self.string | ||
21 | def drawPlant(self): | 20 | def drawPlant(self): | ||
n | 22 | currentPt = (200,0) | n | 21 | currentPt=(200,0) |
23 | theta = 90 | 22 | theta = 90 | ||
n | 24 | stack = [] | n | 23 | stack= [] |
25 | for i in self.str: | 24 | for i in self.string: | ||
26 | if i.isalpha(): | 25 | if i.isalpha(): | ||
n | 27 | nextPt = (currentPt[0]+ math.cos(math.radians(theta)), currentPt | n | 26 | nextPt = (currentPt[0]+math.cos(math.radians(theta)), currentPt[1 |
> | [1] + math.sin(math.radians(theta))) | > | ]+math.sin(math.radians(theta))) | ||
28 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color= | ||
> | ='black') | > | 'black') | ||
29 | currentPt = nextPt | 28 | currentPt=nextPt | ||
30 | else: | ||||
31 | if i == '[': | 29 | elif i =="[": | ||
32 | stack.append((currentPt,theta)) | 30 | stack.append((currentPt,theta)) | ||
33 | if i == ']': | 31 | elif i =="]": | ||
34 | currentPt,theta = stack.pop() | 32 | currentPt,theta = stack.pop() | ||
35 | if i == '+': | 33 | elif i == '+': | ||
36 | theta += self.deltaTheta | 34 | theta += self.deltaTheta | ||
37 | if i == '-': | 35 | elif i == '-': | ||
38 | theta -= self.deltaTheta | 36 | theta -= self.deltaTheta | ||
39 | if __name__ == '__main__': | 37 | if __name__ == "__main__": | ||
40 | myPlant=PLANT('X',{ 'X' : 'F[+X]F[-X]+X','F' : 'FF'},2,20) | ||||
41 | print(myPlant.str) | ||||
42 | myPlant2 = PLANT('b', {'b':'a', 'a':'ab'},5,25) | 38 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) | ||
43 | print(myPlant2.str) | 39 | print(myPlant.str =='abaababa') | ||
44 | class DNAMOTIF: | 40 | class DNAMOTIF: | ||
45 | def __init__(self): | 41 | def __init__(self): | ||
46 | self.instances=[] | 42 | self.instances=[] | ||
47 | self.consensus=[] | 43 | self.consensus=[] | ||
48 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 44 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
49 | def __str__(self): | 45 | def __str__(self): | ||
n | 50 | for x in self.instances: | n | 46 | pass |
51 | string += x | ||||
52 | return string | ||||
53 | def __len__(self): | 47 | def __len__(self): | ||
n | 54 | return len(self.instances[0])-1 | n | 48 | return (len(self.instances) - 4) |
55 | def count(self): | 49 | def count(self): | ||
n | 56 | for i in range(len(self.instances[0])-1): | n | 50 | for x in range(15): |
57 | A = 0 | 51 | countA=0 | ||
58 | C = 0 | 52 | countC=0 | ||
59 | G = 0 | 53 | countG=0 | ||
60 | T = 0 | 54 | countT=0 | ||
61 | for lines in self.instances: | 55 | for col in self.instances: | ||
62 | char = lines[i].upper() | 56 | if col[x] == 'A' or col[x]== 'a': | ||
63 | if char == "A": | ||||
64 | A+=1 | 57 | countA+=1 | ||
65 | elif char == "C": | 58 | if col[x] == 'C' or col[x]== 'c': | ||
66 | C+=1 | 59 | countC+=1 | ||
67 | elif char =="G": | 60 | if col[x] == 'G' or col[x]== 'g': | ||
68 | G+=1 | 61 | countG+=1 | ||
69 | elif char == "T": | 62 | if col[x] == 'T' or col[x]== 't': | ||
70 | T+=1 | 63 | countT+=1 | ||
71 | self.counts["A"].append(A) | 64 | self.counts['A'] += [countA] | ||
72 | self.counts["C"].append(C) | 65 | self.counts['C'] += [countC] | ||
73 | self.counts["G"].append(G) | 66 | self.counts['G'] += [countG] | ||
74 | self.counts["T"].append(T) | 67 | self.counts['T'] += [countT] | ||
75 | string = '' | 68 | return self.counts | ||
69 | def compute_consensus(self): | ||||
70 | pass | ||||
76 | def parse(self, filename): | 71 | def parse(self, filename): | ||
t | 77 | f = open(filename) | t | 72 | with open(filename,'r') as f: |
78 | lines = f.readlines() | 73 | self.instances = f.readlines()[1::2] | ||
79 | for x in lines: | 74 | print(self.instances) | ||
80 | if ">" not in x: | ||||
81 | self.instances.append(x) | ||||
82 | if __name__ == "__main__": | ||||
83 | pass |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | from math import radians, cos, sin | n | 1 | from math import radians,cos,sin |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, carpet = '', rugs = {}, crocs = 0, vans = 0): | n | 4 | def __init__ (self, inital_state, generator =[], iterations= 0 , angle = 0): |
5 | self.carpet = carpet | 5 | self.inital_state = inital_state | ||
6 | self.rugs = rugs | 6 | self.generator = generator | ||
7 | self.crocs = crocs | 7 | self.iterations = iterations | ||
8 | self.vans = vans | 8 | self.angle = angle | ||
9 | answers = '' | 9 | answers = "" | ||
10 | for i in range(crocs): | 10 | for l in range(iterations): | ||
11 | for token in carpet: | 11 | for i in inital_state: | ||
12 | if token in rugs: | 12 | if i in generator: | ||
13 | answers = answers + str(rugs[token]) | 13 | answers += (generator[i]) | ||
14 | else: | 14 | else: | ||
n | 15 | answers = answers + token | n | 15 | answers += i |
16 | carpet = answers | 16 | inital_state = answers | ||
17 | answers = '' | 17 | answers ="" | ||
18 | self.str = carpet | 18 | self.str = inital_state | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 20 | crops = (200,0) | n | 20 | current_PT = (200,0) |
21 | angle = radians(90) | 21 | theta = radians(90) | ||
22 | stacks = [] | 22 | apples = [] | ||
23 | for grass in self.str: | 23 | for i in self.str: | ||
24 | if grass.isalpha(): | 24 | if i.isalpha(): | ||
25 | nextPt = (crops[0] + cos(angle), crops[1] + sin(angle)) | 25 | new_PT = (current_PT[0]+ cos(theta), current_PT[1]+ sin(theta)) | ||
26 | plt.plot([crops[0],nextPt[0]],[crops[1],nextPt[1]], color = 'bla | 26 | plt.plot([current_PT[0],new_PT[0]],[current_PT[1],new_PT[1]], co | ||
> | ck') | > | lor = 'black' ) | ||
27 | crops = nextPt | 27 | current_PT = new_PT | ||
28 | elif grass == '[': | 28 | elif i == "[": | ||
29 | stacks.append(crops) | 29 | apples.append(current_PT) | ||
30 | stacks.append(angle) | 30 | apples.append(theta) | ||
31 | elif grass == ']': | 31 | elif i == "]": | ||
32 | angle = stacks.pop() | 32 | theta = apples.pop() | ||
33 | crops = stacks.pop() | 33 | current_PT = apples.pop() | ||
34 | elif grass == '+': | 34 | elif i == "+": | ||
35 | angle += radians(self.vans) | 35 | theta += (radians(self.angle)) | ||
36 | elif grass == '-': | 36 | elif i == "-": | ||
37 | angle -= radians(self.vans) | 37 | theta -= (radians(self.angle))class DNAMOTIF: | ||
38 | class DNAMOTIF: | ||||
39 | def __init__(self): | 38 | def __init__(self): | ||
40 | self.instances=[] | 39 | self.instances=[] | ||
41 | self.consensus='' | 40 | self.consensus='' | ||
42 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
43 | def __str__(self): | 42 | def __str__(self): | ||
n | 44 | for massive_dongs in self.instances: | n | 43 | for item in self.instances: |
45 | print(massive_dongs) | 44 | print(item) | ||
46 | pass | 45 | pass | ||
47 | def __len__(self): | 46 | def __len__(self): | ||
n | 48 | return len(self.instances[0])-1 | n | 47 | return len(self.instances[-1])-1 |
49 | def count(self): | 48 | def count(self): | ||
50 | for i in self.instances: | 49 | for c in self.instances: | ||
51 | for b in range(len(self.instances[0])-1): | 50 | for n in range(len(self.instances[0])-1): | ||
52 | if i == self.instances[0]: | 51 | if c == self.instances[0]: | ||
53 | self.counts['A'].append(0) | 52 | self.counts['A'].append(0) | ||
54 | self.counts['C'].append(0) | 53 | self.counts['C'].append(0) | ||
n | n | 54 | self.counts['G'].append(0) | ||
55 | self.counts['T'].append(0) | 55 | self.counts['T'].append(0) | ||
n | 56 | self.counts['G'].append(0) | n | ||
57 | if i.upper()[b] == 'A': | 56 | if c.upper()[n] == "A": | ||
58 | self.counts['A'][b] += 1 | 57 | self.counts["A"][n] += 1 | ||
59 | elif i.upper()[b] == 'C': | 58 | elif c.upper()[n] == 'C' : | ||
60 | self.counts['C'][b] += 1 | 59 | self.counts["C"][n] += 1 | ||
61 | elif i.upper()[b] == 'T': | ||||
62 | self.counts['T'][b] += 1 | ||||
63 | elif i.upper()[b] == 'G': | 60 | elif c.upper()[n] == 'G' : | ||
64 | self.counts['G'][b] += 1 | 61 | self.counts["G"][n] += 1 | ||
62 | elif c.upper()[n] == 'T' : | ||||
63 | self.counts["T"][n] += 1 | ||||
65 | def compute_consensus(self): | 64 | def compute_consensus(self): | ||
66 | DNAMOTIF.count(self) | 65 | DNAMOTIF.count(self) | ||
n | 67 | for j in range(len(self.counts["A"])): | n | 66 | for n in range(len(self.counts["A"])): |
68 | a = self.counts["A"][j] | 67 | a = self.counts["A"][n] | ||
69 | c = self.counts["C"][j] | 68 | c = self.counts["C"][n] | ||
70 | t = self.counts["T"][j] | ||||
71 | g = self.counts["G"][j] | 69 | g = self.counts["G"][n] | ||
70 | t = self.counts["T"][n] | ||||
72 | maximum_value = max([a,c,t,g]) | 71 | max_Val = max([a,c,g,t]) | ||
73 | if maximum_value == a: | 72 | if max_Val == a: | ||
74 | self.consensus += "A" | 73 | self.consensus += "A" | ||
n | 75 | elif maximum_value == c: | n | 74 | elif max_Val == c: |
76 | self.consensus += "C" | 75 | self.consensus += "C" | ||
n | n | 76 | elif max_Val == g: | ||
77 | self.consensus += "G" | ||||
77 | elif maximum_value == t: | 78 | elif max_Val == t: | ||
78 | self.consensus += "T" | 79 | self.consensus += "T" | ||
n | 79 | elif maximum_value == g: | n | ||
80 | self.consensus += "G" | ||||
81 | def parse(self, filename): | 80 | def parse(self, filename): | ||
t | 82 | r = '>' | t | ||
83 | with open(filename) as token: | 81 | with open(filename) as file: | ||
84 | for massive_dongs in token: | 82 | for obj in file: | ||
85 | if r not in massive_dongs: | 83 | if ">" not in obj: | ||
86 | self.instances.append(massive_dongs) | 84 | self.instances.append(obj) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | from math import radians,cos,sin | n | 1 | from math import radians, cos, sin |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__ (self, inital_state, generator =[], iterations= 0 , angle = 0): | n | 4 | def __init__(self, carpet = '', rugs = {}, crocs = 0, vans = 0): |
5 | self.inital_state = inital_state | 5 | self.carpet = carpet | ||
6 | self.generator = generator | 6 | self.rugs = rugs | ||
7 | self.iterations = iterations | 7 | self.crocs = crocs | ||
8 | self.angle = angle | 8 | self.vans = vans | ||
9 | answers = "" | 9 | answers = '' | ||
10 | for l in range(iterations): | 10 | for i in range(crocs): | ||
11 | for i in inital_state: | 11 | for token in carpet: | ||
12 | if i in generator: | 12 | if token in rugs: | ||
13 | answers += (generator[i]) | 13 | answers = answers + str(rugs[token]) | ||
14 | else: | 14 | else: | ||
n | 15 | answers += i | n | 15 | answers = answers + token |
16 | inital_state = answers | 16 | carpet = answers | ||
17 | answers ="" | 17 | answers = '' | ||
18 | self.str = inital_state | 18 | self.str = carpet | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 20 | current_PT = (200,0) | n | 20 | crops = (200,0) |
21 | theta = radians(90) | 21 | angle = radians(90) | ||
22 | apples = [] | 22 | stacks = [] | ||
23 | for i in self.str: | 23 | for grass in self.str: | ||
24 | if i.isalpha(): | 24 | if grass.isalpha(): | ||
25 | new_PT = (current_PT[0]+ cos(theta), current_PT[1]+ sin(theta)) | 25 | nextPt = (crops[0] + cos(angle), crops[1] + sin(angle)) | ||
26 | plt.plot([current_PT[0],new_PT[0]],[current_PT[1],new_PT[1]], co | 26 | plt.plot([crops[0],nextPt[0]],[crops[1],nextPt[1]], color = 'bla | ||
> | lor = 'black' ) | > | ck') | ||
27 | current_PT = new_PT | 27 | crops = nextPt | ||
28 | elif i == "[": | 28 | elif grass == '[': | ||
29 | apples.append(current_PT) | 29 | stacks.append(crops) | ||
30 | apples.append(theta) | 30 | stacks.append(angle) | ||
31 | elif i == "]": | 31 | elif grass == ']': | ||
32 | theta = apples.pop() | 32 | angle = stacks.pop() | ||
33 | current_PT = apples.pop() | 33 | crops = stacks.pop() | ||
34 | elif i == "+": | 34 | elif grass == '+': | ||
35 | theta += (radians(self.angle)) | 35 | angle += radians(self.vans) | ||
36 | elif i == "-": | 36 | elif grass == '-': | ||
37 | theta -= (radians(self.angle))class DNAMOTIF: | 37 | angle -= radians(self.vans) | ||
38 | class DNAMOTIF: | ||||
38 | def __init__(self): | 39 | def __init__(self): | ||
39 | self.instances=[] | 40 | self.instances=[] | ||
40 | self.consensus='' | 41 | self.consensus='' | ||
41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 42 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
42 | def __str__(self): | 43 | def __str__(self): | ||
n | 43 | for item in self.instances: | n | 44 | for massive_dongs in self.instances: |
44 | print(item) | 45 | print(massive_dongs) | ||
45 | pass | 46 | pass | ||
46 | def __len__(self): | 47 | def __len__(self): | ||
n | 47 | return len(self.instances[-1])-1 | n | 48 | return len(self.instances[0])-1 |
48 | def count(self): | 49 | def count(self): | ||
49 | for c in self.instances: | 50 | for i in self.instances: | ||
50 | for n in range(len(self.instances[0])-1): | 51 | for b in range(len(self.instances[0])-1): | ||
51 | if c == self.instances[0]: | 52 | if i == self.instances[0]: | ||
52 | self.counts['A'].append(0) | 53 | self.counts['A'].append(0) | ||
53 | self.counts['C'].append(0) | 54 | self.counts['C'].append(0) | ||
n | n | 55 | self.counts['T'].append(0) | ||
54 | self.counts['G'].append(0) | 56 | self.counts['G'].append(0) | ||
n | 55 | self.counts['T'].append(0) | n | ||
56 | if c.upper()[n] == "A": | 57 | if i.upper()[b] == 'A': | ||
57 | self.counts["A"][n] += 1 | 58 | self.counts['A'][b] += 1 | ||
58 | elif c.upper()[n] == 'C' : | 59 | elif i.upper()[b] == 'C': | ||
59 | self.counts["C"][n] += 1 | 60 | self.counts['C'][b] += 1 | ||
60 | elif c.upper()[n] == 'G' : | ||||
61 | self.counts["G"][n] += 1 | ||||
62 | elif c.upper()[n] == 'T' : | 61 | elif i.upper()[b] == 'T': | ||
63 | self.counts["T"][n] += 1 | 62 | self.counts['T'][b] += 1 | ||
63 | elif i.upper()[b] == 'G': | ||||
64 | self.counts['G'][b] += 1 | ||||
64 | def compute_consensus(self): | 65 | def compute_consensus(self): | ||
65 | DNAMOTIF.count(self) | 66 | DNAMOTIF.count(self) | ||
n | 66 | for n in range(len(self.counts["A"])): | n | 67 | for j in range(len(self.counts["A"])): |
67 | a = self.counts["A"][n] | 68 | a = self.counts["A"][j] | ||
68 | c = self.counts["C"][n] | 69 | c = self.counts["C"][j] | ||
69 | g = self.counts["G"][n] | ||||
70 | t = self.counts["T"][n] | 70 | t = self.counts["T"][j] | ||
71 | g = self.counts["G"][j] | ||||
71 | max_Val = max([a,c,g,t]) | 72 | maximum_value = max([a,c,t,g]) | ||
72 | if max_Val == a: | 73 | if maximum_value == a: | ||
73 | self.consensus += "A" | 74 | self.consensus += "A" | ||
n | 74 | elif max_Val == c: | n | 75 | elif maximum_value == c: |
75 | self.consensus += "C" | 76 | self.consensus += "C" | ||
n | n | 77 | elif maximum_value == t: | ||
78 | self.consensus += "T" | ||||
76 | elif max_Val == g: | 79 | elif maximum_value == g: | ||
77 | self.consensus += "G" | 80 | self.consensus += "G" | ||
n | 78 | elif max_Val == t: | n | ||
79 | self.consensus += "T" | ||||
80 | def parse(self, filename): | 81 | def parse(self, filename): | ||
t | t | 82 | r = '>' | ||
81 | with open(filename) as file: | 83 | with open(filename) as token: | ||
82 | for obj in file: | 84 | for massive_dongs in token: | ||
83 | if ">" not in obj: | 85 | if r not in massive_dongs: | ||
84 | self.instances.append(obj) | 86 | self.instances.append(massive_dongs) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import matplotlib.pyplot as plt | n | ||
2 | import numpy as np | 1 | import numpy as np | ||
n | n | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, string, generated_dict, x, diff_theta): | n | 4 | def __init__(self, string, dictionary, n, delta_Theta): |
5 | self.string = string | 5 | self.string = string | ||
n | 6 | self.generated_dict = generated_dict | n | 6 | self.dictionary = dictionary |
7 | self.x = x | 7 | self.n = n | ||
8 | self.diff_theta = diff_theta | 8 | self.delta_Theta = delta_Theta | ||
9 | self.str = PLANT.generator(string,generated_dict, x) | 9 | self.str = PLANT.generator(string,dictionary, n) | ||
10 | def generator(string, generated_dict, x): | 10 | def generator(string, dictionary, n): | ||
11 | def variable(string): | 11 | def variable(string): | ||
n | 12 | if string in generated_dict: | n | 12 | if string in dictionary: |
13 | z = generated_dict.get(string) | 13 | return dictionary.get(string) | ||
14 | string = z | ||||
15 | return string | 14 | return string | ||
16 | da_list_jawn = [string] | 15 | listsubstrings = [string] | ||
17 | for i in range(x): | 16 | for i in range(n): | ||
18 | now_str = da_list_jawn[-1] | 17 | current_substring = listsubstrings[-1] | ||
19 | now_star = [variable(char) for char in now_str] | 18 | new_axiom = [variable(char) for char in current_substring] | ||
20 | da_list_jawn.append(''.join(now_star)) | 19 | listsubstrings.append(''.join(new_axiom)) | ||
21 | ans_str = da_list_jawn[x] | 20 | new_string = listsubstrings[n] | ||
22 | return ans_str | 21 | return new_string | ||
23 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | 24 | uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', | n | 23 | upper_alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L |
> | 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | > | ', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | ||
25 | lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', | 24 | current_Plant=(200,0) | ||
> | 'm','n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] | ||||
26 | startpoint=(200,0) | ||||
27 | theta_angle = np.radians(90) | 25 | theta = np.radians(90) | ||
28 | diff_theta = np.radians(self.diff_theta) | 26 | delta_Theta = np.radians(self.delta_Theta) | ||
29 | plant_send = [startpoint, theta_angle] | 27 | stack = [current_Plant, theta] | ||
30 | for command in self.str: | 28 | for command in self.str: | ||
n | 31 | if command.upper() in uppercase: | n | 29 | if command.upper() in upper_alpha: |
32 | followpoint = (startpoint[0]+(np.cos(theta_angle)), startpoint[1 | 30 | next_Plant = (current_Plant[0]+(np.cos(theta)), current_Plant[1] | ||
> | ]+(np.sin(theta_angle))) | > | +(np.sin(theta))) | ||
33 | plt.plot([startpoint[0],followpoint[0]],[startpoint[1],followpoi | 31 | plt.plot([current_Plant[0],next_Plant[0]],[current_Plant[1],next | ||
> | nt[1]],color='black') | > | _Plant[1]],color='black') | ||
34 | startpoint = followpoint | 32 | current_Plant = next_Plant | ||
35 | else: | 33 | else: | ||
n | 36 | if command == ']': | n | 34 | if command == '[': |
37 | theta_angle = plant_send.pop() | 35 | stack.append(current_Plant) | ||
38 | startpoint = plant_send.pop() | 36 | stack.append(theta) | ||
39 | elif command == '[': | 37 | elif command == ']': | ||
40 | plant_send.append(startpoint) | 38 | theta = stack.pop() | ||
41 | plant_send.append(theta_angle) | 39 | current_Plant = stack.pop() | ||
40 | elif command == '+': | ||||
41 | theta += delta_Theta | ||||
42 | elif command == '-': | 42 | elif command == '-': | ||
n | 43 | theta_angle -= diff_theta | n | 43 | theta -= delta_Theta |
44 | elif command == '+': | ||||
45 | theta_angle += diff_theta | ||||
46 | return plt.plot | 44 | return plt.plot | ||
47 | if __name__ == "__main__": | 45 | if __name__ == "__main__": | ||
48 | string = input() | 46 | string = input() | ||
n | 49 | generated_dict = input() | n | 47 | dictionary = input() |
50 | x = int(input()) | 48 | n = int(input()) | ||
51 | diff_theta = float(input()) | 49 | delta_Theta = float(input()) | ||
52 | a = PLANT(string, generated_dict, x, diff_theta) | 50 | p = PLANT(string, dictionary, n, deltaTheta) | ||
53 | a.drawPlant() | 51 | p.drawPlant() | ||
54 | plt.show() | 52 | plt.show()class DNAMOTIF: | ||
55 | class DNAMOTIF: | ||||
56 | def __init__(self): | 53 | def __init__(self): | ||
57 | self.instances=[] | 54 | self.instances=[] | ||
58 | self.consensus=[] | 55 | self.consensus=[] | ||
59 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 56 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
60 | def __str__(self): | 57 | def __str__(self): | ||
n | 61 | string_out = '' | n | 58 | output = "" |
62 | for instance in self.instances: | 59 | for instance in self.instances: | ||
n | 63 | string_out += instance | n | 60 | output += instance |
64 | return string_out | 61 | return output | ||
65 | def __len__(self): | 62 | def __len__(self): | ||
n | 66 | x = len(self.instances[0].rstrip()) | n | 63 | return len(self.instances[0].rstrip()) |
67 | return x | ||||
68 | def count(self): | 64 | def count(self): | ||
n | 69 | for location in range(len(self)): | n | 65 | for position in range(len(self)): |
70 | seq_a = 0 | 66 | seq_T = 0 | ||
71 | seq_t = 0 | 67 | seq_A = 0 | ||
72 | seq_c = 0 | 68 | seq_G = 0 | ||
73 | seq_g = 0 | 69 | seq_C = 0 | ||
74 | for instance in self.instances: | 70 | for instance in self.instances: | ||
n | 75 | series = instance.rstrip() | n | 71 | sequence = instance.rstrip() |
76 | if (series[location]).upper() == 'A': | 72 | if (sequence[position]).upper() == "G": | ||
77 | seq_a += 1 | 73 | seq_G = seq_G + 1 | ||
78 | elif (series[location]).upper() == 'T': | 74 | elif (sequence[position]).upper() == "C": | ||
79 | seq_t += 1 | 75 | seq_C = seq_C + 1 | ||
80 | elif (series[location]).upper() == 'C': | 76 | elif (sequence[position]).upper() == "T": | ||
81 | seq_c += 1 | 77 | seq_T = seq_T + 1 | ||
82 | elif (series[location]).upper() == 'G': | 78 | elif (sequence[position]).upper() == "A": | ||
83 | seq_g += 1 | 79 | seq_A = seq_A + 1 | ||
84 | self.counts.get('C').append(seq_c) | ||||
85 | self.counts.get('A').append(seq_a) | 80 | self.counts.get("A").append(seq_A) | ||
86 | self.counts.get('G').append(seq_g) | ||||
87 | self.counts.get('T').append(seq_t) | 81 | self.counts.get("T").append(seq_T) | ||
82 | self.counts.get("C").append(seq_C) | ||||
83 | self.counts.get("G").append(seq_G) | ||||
88 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
89 | DNAMOTIF.count(self) | 85 | DNAMOTIF.count(self) | ||
n | 90 | T = self.counts.get('T') | n | ||
91 | G = self.counts.get('G') | ||||
92 | A = self.counts.get('A') | 86 | A = self.counts.get("A") | ||
87 | G = self.counts.get("G") | ||||
93 | C = self.counts.get('C') | 88 | C = self.counts.get("C") | ||
89 | T = self.counts.get("T") | ||||
94 | Output = "" | 90 | output = "" | ||
95 | for row in range(len(A)): | 91 | for row in range(len(A)): | ||
n | 96 | MAX = max(A[row],T[row],C[row],G[row]) | n | 92 | maximum = max(A[row],T[row],C[row],G[row]) |
97 | if MAX == A[row]: | 93 | if maximum == A[row]: | ||
98 | Output += "A" | 94 | output = output + "A" | ||
99 | elif MAX == T[row]: | ||||
100 | Output += "T" | ||||
101 | elif MAX == C[row]: | ||||
102 | Output += "C" | ||||
103 | elif MAX == G[row]: | 95 | elif maximum == G[row]: | ||
104 | Output += "G" | 96 | output = output +"G" | ||
97 | elif maximum == C[row]: | ||||
98 | output = output + "C" | ||||
99 | elif maximum == T[row]: | ||||
100 | output = output + "T" | ||||
105 | self.consensus = Output | 101 | self.consensus = output | ||
106 | def parse(self, filename): | 102 | def parse(self, filename): | ||
n | 107 | dna_file = open(filename, 'r') | n | 103 | my_File = open(filename, 'r') |
108 | file_stuff = [line for line in dna_file.readlines()] | 104 | contents = [line for line in my_File.readlines()] | ||
109 | dna_file.close() | 105 | my_File.close() | ||
110 | for index, line in enumerate(file_stuff): | 106 | for index, line in enumerate(contents): | ||
111 | if index % 2 != 0: | 107 | if index % 2 != 0: | ||
112 | self.instances.append(line) | 108 | self.instances.append(line) | ||
113 | if __name__ == '__main__': | 109 | if __name__ == '__main__': | ||
n | 114 | lexA=DNAMOTIF() | n | 110 | lexA=DNAMOTIF() |
115 | filename = r'FinalProject\lexA.fasta' | 111 | filename = r'FinalProject\lexA.fasta' | ||
t | 116 | lexA.parse(filename) | t | 112 | lexA.parse(filename) |
117 | lexA.compute_consensus() | 113 | lexA.compute_consensus() | ||
118 | print(lexA.consensus) | 114 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | %matplotlib inline | n | ||
2 | import matplotlib.pyplot as plt | ||||
3 | plt.style.use('bmh') | ||||
4 | class DNAMOTIF: | 1 | nopclass DNAMOTIF: | ||
5 | def __init__(self): | 2 | def __init__(self): | ||
6 | self.instances=[] | 3 | self.instances=[] | ||
7 | self.consensus=[] | 4 | self.consensus=[] | ||
8 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 5 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
9 | def __str__(self): | 6 | def __str__(self): | ||
n | 10 | string="" | n | 7 | pass |
11 | for i in self.instances: | ||||
12 | string+=i; | ||||
13 | return string | ||||
14 | def __len__(self): | 8 | def __len__(self): | ||
n | 15 | return len(self.instances[0]) | n | 9 | pass |
16 | def count(self): | 10 | def count(self): | ||
n | 17 | pass | n | 11 | pass |
18 | def compute_consensus(self): | 12 | def compute_consensus(self): | ||
n | 19 | pass | n | 13 | pass |
20 | def parse(self, filename): | 14 | def parse(self, filename): | ||
t | 21 | with open(filename,'r') as f: | t | 15 | with open(filename, 'r+') as my_file: |
22 | for i in f: | 16 | strands = my_file.readlines() | ||
17 | for strand in strands: | ||||
18 | strand[0:21] | ||||
23 | if ">" in i: | 19 | if '>' in strand: | ||
24 | pass | 20 | strands.remove(strand) | ||
25 | else: | 21 | return strands | ||
26 | self.instances.append(i) | ||||
27 | if __name__=="__main__": | ||||
28 | lexA=DNAMOTIF() | 22 | lexA = DNAMOTIF() | ||
29 | lexA.parse("lexA.fasta") | 23 | print(lexA.parse("lexA.fasta")) | ||
30 | print(len(lexA)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | %matplotlib inline | ||
2 | import matplotlib.pyplot as plt | ||||
3 | plt.style.use('bmh') | ||||
1 | nopclass DNAMOTIF: | 4 | class DNAMOTIF: | ||
2 | def __init__(self): | 5 | def __init__(self): | ||
3 | self.instances=[] | 6 | self.instances=[] | ||
4 | self.consensus=[] | 7 | self.consensus=[] | ||
5 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 8 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
6 | def __str__(self): | 9 | def __str__(self): | ||
n | 7 | pass | n | 10 | string="" |
11 | for i in self.instances: | ||||
12 | string+=i; | ||||
13 | return string | ||||
8 | def __len__(self): | 14 | def __len__(self): | ||
n | n | 15 | return len(self.instances[0]) | ||
16 | def count(self): | ||||
9 | pass | 17 | pass | ||
n | 10 | def count(self): | n | ||
11 | pass | ||||
12 | def compute_consensus(self): | 18 | def compute_consensus(self): | ||
n | 13 | pass | n | 19 | pass |
14 | def parse(self, filename): | 20 | def parse(self, filename): | ||
t | 15 | with open(filename, 'r+') as my_file: | t | 21 | with open(filename,'r') as f: |
16 | strands = my_file.readlines() | 22 | for i in f: | ||
17 | for strand in strands: | ||||
18 | strand[0:21] | ||||
19 | if '>' in strand: | 23 | if ">" in i: | ||
20 | strands.remove(strand) | 24 | pass | ||
21 | return strands | 25 | else: | ||
26 | self.instances.append(i) | ||||
27 | if __name__=="__main__": | ||||
22 | lexA = DNAMOTIF() | 28 | lexA=DNAMOTIF() | ||
23 | print(lexA.parse("lexA.fasta")) | 29 | lexA.parse("lexA.fasta") | ||
30 | print(len(lexA)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | import math | ||
1 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
n | 2 | import math | n | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self,i_state,gen,n,deltaTheta): | n | 4 | def __init__(self,state,gen,num,deltaTheta): |
5 | curr = gen.get(i_state) | 5 | self.state = state | ||
6 | nxt="" | 6 | self.gen = gen | ||
7 | self.num = num | ||||
8 | self.deltaTheta = deltaTheta | ||||
9 | self.str = self.generate_str() | ||||
10 | def generate_str(self): | ||||
11 | string = "" | ||||
7 | for i in range (1,n): | 12 | for x in range(self.num): | ||
8 | for i in range (0,len(curr)): | 13 | for c in self.state: | ||
9 | if curr[i] in gen: | 14 | if c in self.gen: | ||
10 | nxt+=gen.get(curr[i]) | 15 | string += self.gen[c] | ||
11 | else: | 16 | else: | ||
n | 12 | nxt+=curr[i] | n | 17 | string += c |
13 | curr=nxt | 18 | self.state = string | ||
14 | nxt="" | 19 | string = "" | ||
15 | self.str = curr | 20 | return self.state | ||
16 | self.deltaTheta = deltaTheta | ||||
17 | def drawPlant(self): | 21 | def drawPlant(self): | ||
n | 18 | currentPt=(200,0) | n | 22 | currentPt = (200,0) |
19 | theta=90 | 23 | theta = 90 | ||
20 | stack = [] | 24 | stack = [] | ||
n | 21 | for i in range (0,len(self.str)): | n | 25 | for i in self.str: |
22 | if self.str[i].isalpha(): | 26 | if i.isalpha(): | ||
23 | nextPt = (currentPt[0]+math.cos(math.radians(theta)), currentPt[ | 27 | nextPt = (currentPt[0]+ math.cos(math.radians(theta)), currentPt | ||
> | 1]+math.sin(math.radians(theta))) | > | [1] + math.sin(math.radians(theta))) | ||
24 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 28 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
25 | currentPt = nextPt | 29 | currentPt = nextPt | ||
n | 26 | elif self.str[i]=='[': | n | 30 | else: |
31 | if i == '[': | ||||
27 | stack.append((currentPt,theta)) | 32 | stack.append((currentPt,theta)) | ||
28 | elif self.str[i]==']': | 33 | if i == ']': | ||
29 | (currentPt,theta) = stack.pop() | 34 | currentPt,theta = stack.pop() | ||
30 | elif self.str[i]=='+': | 35 | if i == '+': | ||
31 | theta = theta + self.deltaTheta | 36 | theta += self.deltaTheta | ||
32 | elif self.str[i]=='-': | 37 | if i == '-': | ||
33 | theta = theta - self.deltaTheta | 38 | theta -= self.deltaTheta | ||
34 | plt.show() | 39 | if __name__ == '__main__': | ||
40 | myPlant=PLANT('X',{ 'X' : 'F[+X]F[-X]+X','F' : 'FF'},2,20) | ||||
41 | print(myPlant.str) | ||||
35 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) | 42 | myPlant2 = PLANT('b', {'b':'a', 'a':'ab'},5,25) | ||
36 | myPlant2 = PLANT('X', {'X':'F[+X]F[-X]+X','F':'FF'},7,20) | 43 | print(myPlant2.str) | ||
37 | myPlant2.drawPlant()class DNAMOTIF: | 44 | class DNAMOTIF: | ||
38 | def __init__(self): | 45 | def __init__(self): | ||
39 | self.instances=[] | 46 | self.instances=[] | ||
40 | self.consensus=[] | 47 | self.consensus=[] | ||
n | 41 | self.counts = {'A': [], 'C': [], 'G':[],'T':[]} | n | 48 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} |
42 | def __str__(self): | 49 | def __str__(self): | ||
n | 43 | string='' | n | ||
44 | for i in self.instances: | 50 | for x in self.instances: | ||
45 | string += i | 51 | string += x | ||
52 | return string | ||||
53 | def __len__(self): | ||||
54 | return len(self.instances[0])-1 | ||||
55 | def count(self): | ||||
56 | for i in range(len(self.instances[0])-1): | ||||
57 | A = 0 | ||||
58 | C = 0 | ||||
59 | G = 0 | ||||
60 | T = 0 | ||||
61 | for lines in self.instances: | ||||
62 | char = lines[i].upper() | ||||
63 | if char == "A": | ||||
64 | A+=1 | ||||
65 | elif char == "C": | ||||
66 | C+=1 | ||||
67 | elif char =="G": | ||||
68 | G+=1 | ||||
69 | elif char == "T": | ||||
70 | T+=1 | ||||
71 | self.counts["A"].append(A) | ||||
72 | self.counts["C"].append(C) | ||||
73 | self.counts["G"].append(G) | ||||
74 | self.counts["T"].append(T) | ||||
75 | string = '' | ||||
46 | def parse(self, filename): | 76 | def parse(self, filename): | ||
t | 47 | with open(filename,'r') as fasta: | t | 77 | f = open(filename) |
48 | for i in fasta: | 78 | lines = f.readlines() | ||
79 | for x in lines: | ||||
49 | if ">" in i: | 80 | if ">" not in x: | ||
50 | pass | ||||
51 | else: | ||||
52 | self.instances.append(i) | 81 | self.instances.append(x) | ||
53 | return | ||||
54 | def __len__(self): | ||||
55 | return len(self.instances)-len(self.counts) | ||||
56 | if __name__=="__main__": | 82 | if __name__ == "__main__": | ||
57 | lexA = DNAMOTIF() | 83 | pass | ||
58 | lexA.parse('lexA.fasta') | ||||
59 | lexA.parse('lexA.fasta') | ||||
60 | print(lexA) | ||||
61 | lexA = DNAMOTIF() | ||||
62 | lexA.count() | ||||
63 | print(lexA.counts) | ||||
64 | lexA = DNAMOTIF() | ||||
65 | lexA.compute_consensus() | ||||
66 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
n | 2 | from math import pi, sin, cos | n | 2 | import math |
3 | import numpy as np | ||||
4 | class PLANT: | 3 | class PLANT: | ||
n | 5 | def __init__(self,state = 'none' ,gen = dict(), run = 0,deltaTheta = 0): | n | 4 | def __init__(self,i_state,gen,n,deltaTheta): |
6 | PLANT.str = '1' | 5 | curr = gen.get(i_state) | ||
7 | self.state = state | 6 | nxt="" | ||
8 | self.deltaTheta = deltaTheta * np.pi/180 | ||||
9 | self.run = run | ||||
10 | self.gen = gen | ||||
11 | inputString = self.state | ||||
12 | for i in range(self.run): | 7 | for i in range (1,n): | ||
13 | plt = '' | 8 | for i in range (0,len(curr)): | ||
14 | for i in inputString: | ||||
15 | if i in self.gen.keys(): | 9 | if curr[i] in gen: | ||
16 | plt += self.gen[i] | 10 | nxt+=gen.get(curr[i]) | ||
17 | else: | 11 | else: | ||
n | 18 | plt += i | n | 12 | nxt+=curr[i] |
19 | inputString = plt | 13 | curr=nxt | ||
14 | nxt="" | ||||
15 | self.str = curr | ||||
16 | self.deltaTheta = deltaTheta | ||||
20 | def drawPlant(self): | 17 | def drawPlant(self): | ||
n | 21 | currentPt = (200,0) | n | 18 | currentPt=(200,0) |
22 | theta = 90 | 19 | theta=90 | ||
23 | stack=[] | 20 | stack = [] | ||
24 | for i in range(0,len(self.str)): | 21 | for i in range (0,len(self.str)): | ||
25 | if self.str[i].isalpha(): | 22 | if self.str[i].isalpha(): | ||
n | 26 | nextPt = (currentPt[0]+math.cos(math.radian(theta)), currentPt[1 | n | 23 | nextPt = (currentPt[0]+math.cos(math.radians(theta)), currentPt[ |
> | ]+math.sin(math.radian(theta))) | > | 1]+math.sin(math.radians(theta))) | ||
27 | plt.plot([currPt[0],nextPt[0]],[currPt[1],nextPt[1]],color='blac | 24 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | k') | > | ='black') | ||
28 | currentPt = nextPt | 25 | currentPt = nextPt | ||
n | 29 | elif self.str[i]=="+": | n | ||
30 | theta += self.deltaTheta | ||||
31 | elif self.str[i]=="-": | ||||
32 | theta -= self.deltaTheta | ||||
33 | elif self.str[i]=="[": | 26 | elif self.str[i]=='[': | ||
34 | stack.append(currentPt,theta) | 27 | stack.append((currentPt,theta)) | ||
35 | elif self.str[i]=="]": | 28 | elif self.str[i]==']': | ||
36 | (currentPt,theta) = stack.pop() | 29 | (currentPt,theta) = stack.pop() | ||
n | 37 | if __name__ == '__main__': | n | 30 | elif self.str[i]=='+': |
31 | theta = theta + self.deltaTheta | ||||
32 | elif self.str[i]=='-': | ||||
33 | theta = theta - self.deltaTheta | ||||
34 | plt.show() | ||||
35 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) | ||||
38 | myPlant=PLANT('X',{ 'X' : 'F[+X]F[-X]+X','F' : 'FF'},2,20) | 36 | myPlant2 = PLANT('X', {'X':'F[+X]F[-X]+X','F':'FF'},7,20) | ||
39 | myPlant.str=='FF[+F[+X]F[-X]+X]FF[-F[+X]F[-X]+X]+F[+X]F[-X]+X' | 37 | myPlant2.drawPlant()class DNAMOTIF: | ||
40 | plt.axis('image') | ||||
41 | self.drawPlant() | ||||
42 | plt.self.draawPlant | ||||
43 | class DNAMOTIF: | ||||
44 | def __init__(self): | 38 | def __init__(self): | ||
45 | self.instances=[] | 39 | self.instances=[] | ||
46 | self.consensus=[] | 40 | self.consensus=[] | ||
47 | self.counts = {'A': [], 'C': [], 'G':[],'T':[]} | 41 | self.counts = {'A': [], 'C': [], 'G':[],'T':[]} | ||
48 | def __str__(self): | 42 | def __str__(self): | ||
49 | string='' | 43 | string='' | ||
50 | for i in self.instances: | 44 | for i in self.instances: | ||
n | 51 | string += i + '' | n | 45 | string += i |
52 | def __len__(self): | ||||
53 | return len(self.instances) - len(self.counts) | ||||
54 | def count(self): | ||||
55 | for s in self.instances: | ||||
56 | self.counts['A']=s[0] | ||||
57 | self.counts['C']=s[0] | ||||
58 | self.counts['G']=s[0] | ||||
59 | self.counts['T']=s[0] | ||||
60 | self.instances.append(s) | ||||
61 | for i in range(len(s)): | ||||
62 | if s[i]=='a' or s[i]=='A': | ||||
63 | self.counts['A'][i]+=1; | ||||
64 | if s[i]=='c' or s[i]=='C': | ||||
65 | self.counts['C'][i]+=1; | ||||
66 | if s[i]=='g' or s[i]=='G': | ||||
67 | self.counts['G'][i]+=1; | ||||
68 | if s[i]=='t' or s[i]=='T': | ||||
69 | self.counts['T'][i]+=1; | ||||
70 | def compute_consensus(self): | ||||
71 | return | ||||
72 | def parse(self, filename): | 46 | def parse(self, filename): | ||
73 | with open(filename,'r') as fasta: | 47 | with open(filename,'r') as fasta: | ||
74 | for i in fasta: | 48 | for i in fasta: | ||
n | 75 | if '>' in i: | n | 49 | if ">" in i: |
76 | pass | 50 | pass | ||
77 | else: | 51 | else: | ||
78 | self.instances.append(i) | 52 | self.instances.append(i) | ||
79 | return | 53 | return | ||
n | n | 54 | def __len__(self): | ||
55 | return len(self.instances)-len(self.counts) | ||||
80 | if __name__=="__main__": | 56 | if __name__=="__main__": | ||
81 | lexA = DNAMOTIF() | 57 | lexA = DNAMOTIF() | ||
82 | lexA.parse('lexA.fasta') | 58 | lexA.parse('lexA.fasta') | ||
n | 83 | print(len(lexA)) | n | ||
84 | lexA = DNAMOTIF() | ||||
85 | lexA.parse('lexA.fasta') | 59 | lexA.parse('lexA.fasta') | ||
n | 86 | print(lexA) | n | 60 | print(lexA) |
87 | lexA = DNAMOTIF() | 61 | lexA = DNAMOTIF() | ||
88 | lexA.count() | 62 | lexA.count() | ||
89 | print(lexA.counts) | 63 | print(lexA.counts) | ||
t | t | 64 | lexA = DNAMOTIF() | ||
65 | lexA.compute_consensus() | ||||
66 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | :( | n | 1 | %matplotlib inline |
2 | import matplotlib.pyplot as plt | ||||
3 | plt.style.use('bmh') | ||||
2 | class DNAMOTIF: | 4 | class DNAMOTIF: | ||
3 | def __init__(self): | 5 | def __init__(self): | ||
4 | self.instances=[] | 6 | self.instances=[] | ||
5 | self.consensus=[] | 7 | self.consensus=[] | ||
6 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 8 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
7 | def __str__(self): | 9 | def __str__(self): | ||
n | 8 | line_str = '' | n | 10 | string="" |
9 | for line in self.instances: | 11 | for i in self.instances: | ||
10 | line_str += line | 12 | string+=i; | ||
11 | return line_str | 13 | return string | ||
12 | def __len__(self): | 14 | def __len__(self): | ||
n | 13 | return len(self.instances[0])-1 | n | 15 | return len(self.instances[0]) |
14 | def count(self): | 16 | def count(self): | ||
n | 15 | for line in self.instances: | n | 17 | pass |
16 | si_caps = str(line).upper() | 18 | def compute_consensus(self): | ||
17 | for char in range(len(si_caps)): | 19 | pass | ||
18 | if line[char] == 'A': | ||||
19 | self.counts['A'][char]+=1 | ||||
20 | def parse(self,filename): | 20 | def parse(self, filename): | ||
21 | with open(filename,'r') as f: | 21 | with open(filename,'r') as f: | ||
n | 22 | for line in f: | n | 22 | for i in f: |
23 | if '>' not in line: | 23 | if ">" in i: | ||
24 | self.instances.append(line) | 24 | pass | ||
25 | else: | 25 | else: | ||
t | 26 | pass | t | 26 | self.instances.append(i) |
27 | if __name__ =="__main__": | 27 | if __name__=="__main__": | ||
28 | lexA=DNAMOTIF() | 28 | lexA=DNAMOTIF() | ||
29 | lexA.parse("lexA.fasta") | 29 | lexA.parse("lexA.fasta") | ||
30 | print(len(lexA)) | 30 | print(len(lexA)) | ||
31 | print(str(lexA.split)) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | from math import radians,cos,sin | n | 1 | from math import radians, cos, sin |
2 | import matplotlib.pyplot as plot | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self,string="", dictionary={},runs=0,delta=0): | n | 4 | def __init__(self, carpet = '', rugs = {}, crocs = 0, vans = 0): |
5 | self.string=string | 5 | self.carpet = carpet | ||
6 | self.dictionary=dictionary | ||||
7 | self.runs=runs | 6 | self.rugs = rugs | ||
8 | self.delta=delta | 7 | self.crocs = crocs | ||
9 | solution= "" | 8 | self.vans = vans | ||
9 | answers = '' | ||||
10 | for i in range(runs): | 10 | for i in range(crocs): | ||
11 | for C in string: | 11 | for token in carpet: | ||
12 | if C in dictionary: | 12 | if token in rugs: | ||
13 | solution=solution+str(dictionary[C]) | 13 | answers = answers + str(rugs[token]) | ||
14 | else: | 14 | else: | ||
n | 15 | solution=solution+C | n | 15 | answers = answers + token |
16 | string=solution | 16 | carpet = answers | ||
17 | solution="" | 17 | answers = '' | ||
18 | self.str=string | 18 | self.str = carpet | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 20 | plantdrop=(200,0) | n | 20 | crops = (200,0) |
21 | angle=radians(90) | 21 | angle = radians(90) | ||
22 | stacks=[] | 22 | stacks = [] | ||
23 | for tree in self.str: | 23 | for grass in self.str: | ||
24 | if tree.isalpha(): | 24 | if grass.isalpha(): | ||
25 | nextPt = (plantdrop[0]+cos(angle), plantdrop[1]+sin(angle)) | 25 | nextPt = (crops[0] + cos(angle), crops[1] + sin(angle)) | ||
26 | plot.plot([plantdrop[0],nextPt[0]],[plantdrop[1],nextPt[1]],colo | 26 | plt.plot([crops[0],nextPt[0]],[crops[1],nextPt[1]], color = 'bla | ||
> | r='black') | > | ck') | ||
27 | plantdrop=nextPt | 27 | crops = nextPt | ||
28 | elif tree=='[': | 28 | elif grass == '[': | ||
29 | stacks.append(plantdrop) | 29 | stacks.append(crops) | ||
30 | stacks.append(angle) | 30 | stacks.append(angle) | ||
n | 31 | elif tree==']': | n | 31 | elif grass == ']': |
32 | angle=stacks.pop() | 32 | angle = stacks.pop() | ||
33 | plantdrop=stacks.pop() | 33 | crops = stacks.pop() | ||
34 | elif tree=='+': | 34 | elif grass == '+': | ||
35 | angle+=radians(self.delta) | 35 | angle += radians(self.vans) | ||
36 | elif tree=='-': | 36 | elif grass == '-': | ||
37 | angle-=radians(self.delta) | 37 | angle -= radians(self.vans) | ||
38 | class DNAMOTIF: | 38 | class DNAMOTIF: | ||
39 | def __init__(self): | 39 | def __init__(self): | ||
40 | self.instances=[] | 40 | self.instances=[] | ||
41 | self.consensus='' | 41 | self.consensus='' | ||
42 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 42 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
43 | def __str__(self): | 43 | def __str__(self): | ||
n | 44 | for obj in self.instances: | n | 44 | for massive_dongs in self.instances: |
45 | print(obj) | 45 | print(massive_dongs) | ||
46 | pass | 46 | pass | ||
47 | def __len__(self): | 47 | def __len__(self): | ||
n | 48 | return len (self.instances[0])-1 | n | 48 | return len(self.instances[0])-1 |
49 | def count(self): | 49 | def count(self): | ||
50 | for i in self.instances: | 50 | for i in self.instances: | ||
n | 51 | for v in range(len(self.instances[0])-1): | n | 51 | for b in range(len(self.instances[0])-1): |
52 | if i == self.instances[0]: | 52 | if i == self.instances[0]: | ||
n | n | 53 | self.counts['A'].append(0) | ||
54 | self.counts['C'].append(0) | ||||
53 | self.counts['T'].append(0) | 55 | self.counts['T'].append(0) | ||
n | 54 | self.counts['C'].append(0) | n | ||
55 | self.counts['A'].append(0) | ||||
56 | self.counts['G'].append(0) | 56 | self.counts['G'].append(0) | ||
n | 57 | if i.upper()[v] == "T": | n | 57 | if i.upper()[b] == 'A': |
58 | self.counts["T"][v] += 1 | 58 | self.counts['A'][b] += 1 | ||
59 | elif i.upper()[v] =='C': | 59 | elif i.upper()[b] == 'C': | ||
60 | self.counts["C"][v] += 1 | 60 | self.counts['C'][b] += 1 | ||
61 | elif i.upper()[v] =='A': | 61 | elif i.upper()[b] == 'T': | ||
62 | self.counts["A"][v] += 1 | 62 | self.counts['T'][b] += 1 | ||
63 | elif i.upper()[v] =='G': | 63 | elif i.upper()[b] == 'G': | ||
64 | self.counts["G"][v] += 1 | 64 | self.counts['G'][b] += 1 | ||
65 | def compute_consensus(self): | 65 | def compute_consensus(self): | ||
66 | DNAMOTIF.count(self) | 66 | DNAMOTIF.count(self) | ||
n | 67 | for v in range(len(self.counts["T"])): | n | 67 | for j in range(len(self.counts["A"])): |
68 | t =self.counts["T"][v] | ||||
69 | c =self.counts["C"][v] | ||||
70 | a =self.counts["A"][v] | 68 | a = self.counts["A"][j] | ||
69 | c = self.counts["C"][j] | ||||
70 | t = self.counts["T"][j] | ||||
71 | g =self.counts["G"][v] | 71 | g = self.counts["G"][j] | ||
72 | max_val = max([t,c,a,g]) | 72 | maximum_value = max([a,c,t,g]) | ||
73 | if max_val== t: | ||||
74 | self.consensus +="T" | ||||
75 | elif max_val == c: | ||||
76 | self.consensus +="C" | ||||
77 | elif max_val == a: | 73 | if maximum_value == a: | ||
78 | self.consensus +="A" | 74 | self.consensus += "A" | ||
75 | elif maximum_value == c: | ||||
76 | self.consensus += "C" | ||||
77 | elif maximum_value == t: | ||||
78 | self.consensus += "T" | ||||
79 | elif max_val == g: | 79 | elif maximum_value == g: | ||
80 | self.consensus +="G" | 80 | self.consensus += "G" | ||
81 | def parse(self, filename): | 81 | def parse(self, filename): | ||
t | 82 | x = ">" | t | 82 | r = '>' |
83 | with open(filename) as sequence: | 83 | with open(filename) as token: | ||
84 | for obj in sequence: | 84 | for massive_dongs in token: | ||
85 | if x not in obj: | 85 | if r not in massive_dongs: | ||
86 | self.instances.append(obj) | 86 | self.instances.append(massive_dongs) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | from math import cos, sin | n | 1 | from math import radians, cos, sin |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
n | 3 | from math import pi | n | 3 | def generate(text, generator): |
4 | gen = '' | ||||
5 | for letter in text: | ||||
6 | if letter in generator.keys(): | ||||
7 | gen += generator[letter] | ||||
8 | elif letter not in generator.keys(): | ||||
9 | gen += letter | ||||
10 | return gen | ||||
4 | class PLANT: | 11 | class PLANT: | ||
5 | def __init__(self, state, generator, n, dTheta): | 12 | def __init__(self, state, generator, n, dTheta): | ||
n | 6 | self.state = state | n | ||
7 | self.generator = generator | 13 | self.generator = generator | ||
8 | self.n = n | 14 | self.n = n | ||
n | 9 | self.dTheta = dTheta | n | 15 | self.dTheta = radians(dTheta) |
10 | c = '' | 16 | newstate = state | ||
11 | for _ in range(n): | 17 | for i in range(n): | ||
12 | for letter in self.state: | 18 | newstate = generate(newstate, generator) | ||
13 | if letter in generator: | ||||
14 | c += generator[letter] | ||||
15 | else: | ||||
16 | c += letter | ||||
17 | self.state = c | ||||
18 | c = '' | ||||
19 | self.str = self.state | 19 | self.str = newstate | ||
20 | def drawPlant(self): | 20 | def drawPlant(self): | ||
n | 21 | currentPt = (200, 0) | n | ||
22 | theta = 90 | ||||
23 | stack = [] | 21 | stack = [] | ||
n | n | 22 | currentPt = (200,0) | ||
23 | theta = radians(90) | ||||
24 | for symbol in self.str: | 24 | for letter in self.str: | ||
25 | if symbol.isalpha(): | 25 | if letter.isalpha(): | ||
26 | nextPt = (currentPt[0]+cos(theta*pi/180), | 26 | nextPt = (currentPt[0] + cos(theta), currentPt[1] + sin(theta)) | ||
27 | currentPt[1]+sin(theta*pi/180)) | 27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | ||||
28 | plt.plot([currentPt[0], nextPt[0]], [ | ||||
29 | currentPt[1], nextPt[1]], color='black') | ||||
30 | currentPt = nextPt | 28 | currentPt = nextPt | ||
n | 31 | elif symbol == '[': | n | 29 | elif letter == '[': |
32 | stack.append(currentPt) | 30 | stack.append(currentPt) | ||
33 | stack.append(theta) | 31 | stack.append(theta) | ||
n | 34 | elif symbol == ']': | n | 32 | elif letter == ']': |
35 | theta = stack.pop() | 33 | theta = stack.pop() | ||
36 | currentPt = stack.pop() | 34 | currentPt = stack.pop() | ||
n | 37 | elif symbol == '+': | n | 35 | elif letter == '+': |
38 | theta -= self.dTheta | 36 | theta -= self.dTheta | ||
n | 39 | elif symbol == '-': | n | 37 | elif letter == '-': |
40 | theta += self.dTheta | 38 | theta += self.dTheta | ||
n | 41 | plt.savefig('my_plot.png') | n | 39 | plt.savefig('plant') |
42 | if __name__ == '__main__': | 40 | if __name__ == '__main__': | ||
n | 43 | plant = PLANT('F', {'F': 'F[+F]F[-F]F'}, 5, 25.7) | n | 41 | plant1 = PLANT('F',{'F': 'F[+F]F[-F]F'},5,25.7) |
44 | plant.drawPlant() | 42 | plant1.drawPlant()class DNAMOTIF: | ||
45 | class DNAMOTIF: | ||||
46 | def __init__(self): | 43 | def __init__(self): | ||
n | 47 | self.instances = [] | n | 44 | self.instances=[] |
48 | self.consensus = [] | 45 | self.consensus= '' | ||
49 | self.counts = {'A': [], 'C': [], 'G': [], 'T': []} | 46 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
50 | def __str__(self): | 47 | def __str__(self): | ||
n | 51 | return ''.join(self.instances) | n | 48 | return '\n'.join(self.instances) |
52 | def __len__(self): | 49 | def __len__(self): | ||
n | n | 50 | self.instances = [x.strip() for x in self.instances] | ||
53 | return len(self.instances[0])-1 | 51 | return len(self.instances[0]) | ||
52 | def count(self): | ||||
53 | self.instances = [x.strip() for x in self.instances] | ||||
54 | self.counts = {'A': [0] * len(self), 'C': [0] * len(self), 'G': [0] * le | ||||
> | n(self), 'T': [0] * len(self)} | ||||
55 | for line in self.instances: | ||||
56 | uline = line.upper() | ||||
57 | for i, letter in enumerate(uline): | ||||
58 | self.counts[letter][i] += 1 | ||||
59 | def compute_consensus(self): | ||||
60 | self.instances = [x.strip() for x in self.instances] | ||||
61 | self.count() | ||||
62 | for i in range(len(self)): | ||||
63 | a = self.counts['A'][i] | ||||
64 | c = self.counts['C'][i] | ||||
65 | t = self.counts['T'][i] | ||||
66 | g = self.counts['G'][i] | ||||
67 | nums = [a, c, g, t] | ||||
68 | big = max(nums) | ||||
69 | ind = nums.index(big) | ||||
70 | if ind == 0: | ||||
71 | let = 'A' | ||||
72 | elif ind == 1 : | ||||
73 | let = 'C' | ||||
74 | elif ind == 2: | ||||
75 | let = 'G' | ||||
76 | elif ind == 3 : | ||||
77 | let = 'T' | ||||
78 | self.consensus += let | ||||
54 | def parse(self, filename): | 79 | def parse(self, filename): | ||
55 | with open(filename, 'r') as f: | 80 | with open(filename, 'r') as f: | ||
n | 56 | data = f.readlines() | n | 81 | for line in f: |
57 | self.instances = data[1::2] | ||||
58 | def count(self): | ||||
59 | self.counts['A'] = [0]*len(self) | ||||
60 | self.counts['G'] = [0]*len(self) | ||||
61 | self.counts['C'] = [0]*len(self) | ||||
62 | self.counts['T'] = [0]*len(self) | ||||
63 | for instance in self.instances: | ||||
64 | for i, letter in enumerate(instance.upper()): | ||||
65 | if letter == 'A': | 82 | if line[0] == '>': | ||
66 | self.counts['A'][i] += 1 | 83 | pass | ||
67 | if letter == 'C': | 84 | else: | ||
68 | self.counts['C'][i] += 1 | 85 | self.instances.append(line) | ||
69 | if letter == 'G': | ||||
70 | self.counts['G'][i] += 1 | ||||
71 | if letter == 'T': | ||||
72 | self.counts['T'][i] += 1 | ||||
73 | def compute_consensus(self): | ||||
74 | self.count() | ||||
75 | cone = '' | ||||
76 | for i in range(len(self)): | ||||
77 | letterlist = [self.counts['A'][i], self.counts['C'][i], | ||||
78 | self.counts['G'][i], self.counts['T'][i]] | ||||
79 | peepee = max(letterlist) | ||||
80 | if peepee == letterlist[0]: | ||||
81 | cone += 'A' | ||||
82 | elif peepee == letterlist[1]: | ||||
83 | cone += 'C' | ||||
84 | elif peepee == letterlist[2]: | ||||
85 | cone += 'G' | ||||
86 | elif peepee == letterlist[3]: | ||||
87 | cone += 'T' | ||||
88 | self.consensus = cone | ||||
89 | if __name__ == '__main__': | 86 | if __name__ == '__main__': | ||
90 | lexA = DNAMOTIF() | 87 | lexA = DNAMOTIF() | ||
t | 91 | lexA.parse(r"C:\Users\cresp\Downloads\c\lexA.fasta") | t | 88 | lexA.parse('lexA.fasta') |
89 | print(len(lexA)) | ||||
92 | lexA.count() | 90 | lexA.count() | ||
93 | lexA.compute_consensus() | 91 | lexA.compute_consensus() | ||
94 | print(lexA.consensus) | 92 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import matplotlib.pyplot as plt | n | 1 | import numpy as np, matplotlib.pyplot as plt |
2 | import numpy as np | ||||
3 | class PLANT: | 2 | class PLANT: | ||
n | 4 | def __init__(self, initial_state ='' , generator = {} , number_generations = | n | 3 | def __init__(self, string='', generator={}, n=0, deltatheta=0.00): |
> | 0 , deltaTheta = 0.00): | ||||
5 | self.initial_state = initial_state | 4 | self.string=string | ||
6 | self.generator = generator | 5 | self.generator=generator | ||
7 | self.number_generations = int(number_generations) | 6 | self.n= int(n) | ||
8 | self.deltaTheta = np.radians(deltaTheta) | 7 | self.deltatheta= np.radians(deltatheta) | ||
9 | for i in range(self.number_generations): | 8 | for i in range(0, self.n): | ||
10 | result = '' | 9 | result='' | ||
11 | for j in self.initial_state: | 10 | for j in self.string: | ||
12 | if j in self.generator.keys(): | 11 | if j in self.generator.keys(): | ||
13 | result += self.generator[str(j)] | 12 | result += self.generator[str(j)] | ||
14 | else: | 13 | else: | ||
15 | result += j | 14 | result += j | ||
n | 16 | self.initial_state = result | n | 15 | self.string = result |
17 | self.str = self.initial_state | 16 | self.str = self.string | ||
18 | def drawPlant(self): | 17 | def drawPlant(self): | ||
n | 19 | currentPt = [200, 0] | n | 18 | currentpos=[200,0] |
20 | theta = np.radians(90) | 19 | theta=np.radians(90) | ||
21 | drawing=[] | 20 | drawingState=[] | ||
22 | nextPt=() | 21 | nextpt=() | ||
23 | stack=[] | 22 | stack=[] | ||
24 | for i in self.str: | 23 | for i in self.str: | ||
25 | if i.isalpha(): | 24 | if i.isalpha(): | ||
n | 26 | nextPt = [currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta) | n | 25 | nextpt=[currentpos[0]+np.cos(theta), currentpos[1]+np.sin(theta) |
> | ] | > | ] | ||
27 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | 26 | plt.plot([currentpos[0], nextpt[0]],[currentpos[1],nextpt[1]],co | ||
> | olor = 'black') | > | lor='black') | ||
28 | currentPt = nextPt | 27 | currentpos=nextpt | ||
29 | if i == '[': | 28 | if i=='[': | ||
30 | drawing = [currentPt , theta] | 29 | drawingState = [currentpos, theta] | ||
31 | stack.append(drawing) | 30 | stack.append(drawingState) | ||
32 | elif i == ']': | 31 | elif i==']': | ||
33 | drawing = stack[len(stack) -1] | 32 | currentpos=stack[-1][0] | ||
33 | theta=stack[-1][1] | ||||
34 | stack.pop(len(stack)-1) | 34 | stack.pop() | ||
35 | currentPt = drawing[0] | ||||
36 | theta = drawing[1] | ||||
37 | elif i == '+': | 35 | elif i=='+': | ||
38 | theta += self.deltaTheta | 36 | theta += self.deltatheta | ||
39 | elif i == '-': | 37 | elif i=='-': | ||
40 | theta -= self.deltaTheta | 38 | theta-=self.deltatheta | ||
41 | if __name__ == '__main__': | 39 | if __name__ == "__main__": | ||
42 | initial_state = input() | 40 | string = input() | ||
43 | dictionary = input() | 41 | dictionary = input() | ||
n | n | 42 | theta = input() | ||
44 | num = input() | 43 | n = input() | ||
45 | angle = float(input()) | 44 | obj = PLANT(string, dictionary, n, theta) | ||
46 | f = PLANT(initial_state, dictionary, num , angle) | ||||
47 | f.drawPlant() | 45 | obj.drawPlant() | ||
48 | plt.show() | 46 | plt.show() | ||
49 | class DNAMOTIF: | 47 | class DNAMOTIF: | ||
50 | def __init__(self): | 48 | def __init__(self): | ||
51 | self.instances=[] | 49 | self.instances=[] | ||
52 | self.consensus=[] | 50 | self.consensus=[] | ||
53 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 51 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
54 | def __str__(self): | 52 | def __str__(self): | ||
n | 55 | self.acids = ''.join(self.instances) | n | 53 | newstr= ''.join(self.instances) |
56 | return self.acids | 54 | return newstr | ||
57 | pass | 55 | pass | ||
58 | def __len__(self): | 56 | def __len__(self): | ||
n | 59 | for elements in self.instances: | n | 57 | for i in self.instances: |
60 | return len(elements)-1 | 58 | return len(i)-1 | ||
61 | def count(self): | 59 | def count(self): | ||
62 | length=len(self.instances[0]) | 60 | length=len(self.instances[0]) | ||
63 | for i in range(length-1): | 61 | for i in range(length-1): | ||
n | 64 | numcountA = 0 | n | 62 | numbercounta=0 |
65 | numcountC = 0 | 63 | numbercountc=0 | ||
66 | numcountG = 0 | 64 | numbercountg=0 | ||
67 | numcountT = 0 | 65 | numbercountt=0 | ||
68 | for j in range(len(self.instances)): | 66 | for j in range(len(self.instances)): | ||
n | 69 | if self.instances[j][i].upper() == 'A': | n | 67 | if self.instances[j][i].upper()=='A': |
70 | numcountA += 1 | 68 | numbercounta +=1 | ||
71 | elif self.instances[j][i].upper() == 'C': | 69 | elif self.instances[j][i].upper()=='C': | ||
72 | numcountC += 1 | 70 | numbercountc +=1 | ||
73 | elif self.instances[j][i].upper() == 'G': | 71 | elif self.instances[j][i].upper()=='G': | ||
74 | numcountG += 1 | 72 | numbercountg +=1 | ||
75 | elif self.instances[j][i].upper() == 'T': | 73 | elif self.instances[j][i].upper()=='T': | ||
76 | numcountT += 1 | 74 | numbercountt +=1 | ||
77 | self.counts['A'].append(numcountA) | 75 | self.counts['A'].append(numbercounta) | ||
78 | self.counts['C'].append(numcountC) | 76 | self.counts['C'].append(numbercountc) | ||
79 | self.counts['G'].append(numcountG) | 77 | self.counts['G'].append(numbercountg) | ||
80 | self.counts['T'].append(numcountT) | 78 | self.counts['T'].append(numbercountt) | ||
81 | def compute_consensus(self): | 79 | def compute_consensus(self): | ||
82 | DNAMOTIF.count(self) | 80 | DNAMOTIF.count(self) | ||
n | 83 | kenneth=[] | n | ||
84 | length = len(self.counts['A']) | 81 | length=len(self.counts['A']) | ||
82 | result=[] | ||||
85 | for i in range(length): | 83 | for i in range(length): | ||
n | 86 | sid = [] | n | 84 | newList=[] |
87 | a=self.counts['A'] | 85 | a = self.counts['A'] | ||
88 | c=self.counts['C'] | 86 | c = self.counts['C'] | ||
89 | g=self.counts['G'] | 87 | g = self.counts['G'] | ||
90 | t=self.counts['T'] | 88 | t = self.counts['T'] | ||
91 | sid.append(int(a[i])) | 89 | newList.append(int(a[i])) | ||
92 | sid.append(int(c[i])) | 90 | newList.append(int(c[i])) | ||
93 | sid.append(int(g[i])) | 91 | newList.append(int(g[i])) | ||
94 | sid.append(int(t[i])) | 92 | newList.append(int(t[i])) | ||
95 | maxnum = max(sid) | 93 | max_value= max(newList) | ||
96 | index = sid.index(maxnum) | 94 | max_index = newList.index(max_value) | ||
97 | if index==0: | 95 | if max_index==0: | ||
98 | kenneth.append('A') | 96 | result.append('A') | ||
99 | if index==1: | 97 | if max_index==1: | ||
100 | kenneth.append('C') | 98 | result.append('C') | ||
101 | if index==2: | 99 | if max_index==2: | ||
102 | kenneth.append('G') | 100 | result.append('G') | ||
103 | if index==3: | 101 | if max_index==3: | ||
104 | kenneth.append('T') | 102 | result.append('T') | ||
105 | self.consensus=(str(''.join(kenneth))) | 103 | self.consensus=(str(''.join(result))) | ||
106 | def parse(self, filename): | 104 | def parse(self, filename): | ||
n | 107 | x = open(filename) | n | 105 | f = open(filename) |
108 | tempList = x.readlines() | 106 | contents = f.readlines() | ||
109 | MOTIFList = [] | ||||
110 | for i in range(1, len(tempList), 2): | ||||
111 | self.instances.append(tempList[i]) | ||||
112 | for i in range (1, len(self.instances)): | ||||
113 | MOTIFList.append(self.instances[i]) | ||||
114 | x.close() | 107 | f.close() | ||
115 | print(len(MOTIFList)) | 108 | list_elements = [] | ||
116 | return(len(MOTIFList)) | 109 | for x in range(0, len(contents)): | ||
110 | if '>' not in contents[x]: | ||||
111 | self.instances.append(contents[x]) | ||||
112 | for x in range (0, len(self.instances)-1): | ||||
113 | list_elements.append(f'{self.instances[x]}\n') | ||||
117 | if __name__ == '__main__': | 114 | if __name__ == '__main__': | ||
t | 118 | self=DNAMOTIF() | t | 115 | self = DNAMOTIF() |
119 | DNAMOTIF.parse(self, 'lexA.fasta') | 116 | DNAMOTIF.parse(self, "lexA.fasta") | ||
120 | DNAMOTIF.__len__(self, 'lexA.fasta') | 117 | DNAMOTIF.__len__(self, "lexA.fasta") |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | from math import radians, cos, sin | n | 1 | from math import cos, sin |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
n | 3 | def generate(text, generator): | n | 3 | from math import pi |
4 | gen = '' | ||||
5 | for letter in text: | ||||
6 | if letter in generator.keys(): | ||||
7 | gen += generator[letter] | ||||
8 | elif letter not in generator.keys(): | ||||
9 | gen += letter | ||||
10 | return gen | ||||
11 | class PLANT: | 4 | class PLANT: | ||
12 | def __init__(self, state, generator, n, dTheta): | 5 | def __init__(self, state, generator, n, dTheta): | ||
n | n | 6 | self.state = state | ||
13 | self.generator = generator | 7 | self.generator = generator | ||
14 | self.n = n | 8 | self.n = n | ||
n | 15 | self.dTheta = radians(dTheta) | n | 9 | self.dTheta = dTheta |
16 | newstate = state | 10 | c = '' | ||
17 | for i in range(n): | 11 | for _ in range(n): | ||
18 | newstate = generate(newstate, generator) | 12 | for letter in self.state: | ||
13 | if letter in generator: | ||||
14 | c += generator[letter] | ||||
15 | else: | ||||
16 | c += letter | ||||
17 | self.state = c | ||||
18 | c = '' | ||||
19 | self.str = newstate | 19 | self.str = self.state | ||
20 | def drawPlant(self): | 20 | def drawPlant(self): | ||
n | n | 21 | currentPt = (200, 0) | ||
22 | theta = 90 | ||||
21 | stack = [] | 23 | stack = [] | ||
n | 22 | currentPt = (200,0) | n | ||
23 | theta = radians(90) | ||||
24 | for letter in self.str: | 24 | for symbol in self.str: | ||
25 | if letter.isalpha(): | 25 | if symbol.isalpha(): | ||
26 | nextPt = (currentPt[0] + cos(theta), currentPt[1] + sin(theta)) | 26 | nextPt = (currentPt[0]+cos(theta*pi/180), | ||
27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 27 | currentPt[1]+sin(theta*pi/180)) | ||
> | ='black') | ||||
28 | plt.plot([currentPt[0], nextPt[0]], [ | ||||
29 | currentPt[1], nextPt[1]], color='black') | ||||
28 | currentPt = nextPt | 30 | currentPt = nextPt | ||
n | 29 | elif letter == '[': | n | 31 | elif symbol == '[': |
30 | stack.append(currentPt) | 32 | stack.append(currentPt) | ||
31 | stack.append(theta) | 33 | stack.append(theta) | ||
n | 32 | elif letter == ']': | n | 34 | elif symbol == ']': |
33 | theta = stack.pop() | 35 | theta = stack.pop() | ||
34 | currentPt = stack.pop() | 36 | currentPt = stack.pop() | ||
n | 35 | elif letter == '+': | n | 37 | elif symbol == '+': |
36 | theta -= self.dTheta | 38 | theta -= self.dTheta | ||
n | 37 | elif letter == '-': | n | 39 | elif symbol == '-': |
38 | theta += self.dTheta | 40 | theta += self.dTheta | ||
n | 39 | plt.savefig('plant') | n | 41 | plt.savefig('my_plot.png') |
40 | if __name__ == '__main__': | 42 | if __name__ == '__main__': | ||
n | 41 | plant1 = PLANT('F',{'F': 'F[+F]F[-F]F'},5,25.7) | n | 43 | plant = PLANT('F', {'F': 'F[+F]F[-F]F'}, 5, 25.7) |
42 | plant1.drawPlant()class DNAMOTIF: | 44 | plant.drawPlant() | ||
45 | class DNAMOTIF: | ||||
43 | def __init__(self): | 46 | def __init__(self): | ||
n | 44 | self.instances=[] | n | 47 | self.instances = [] |
45 | self.consensus= '' | 48 | self.consensus = [] | ||
46 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 49 | self.counts = {'A': [], 'C': [], 'G': [], 'T': []} | ||
47 | def __str__(self): | 50 | def __str__(self): | ||
n | 48 | return '\n'.join(self.instances) | n | 51 | return ''.join(self.instances) |
49 | def __len__(self): | 52 | def __len__(self): | ||
n | 50 | self.instances = [x.strip() for x in self.instances] | n | ||
51 | return len(self.instances[0]) | 53 | return len(self.instances[0])-1 | ||
52 | def count(self): | ||||
53 | self.instances = [x.strip() for x in self.instances] | ||||
54 | self.counts = {'A': [0] * len(self), 'C': [0] * len(self), 'G': [0] * le | ||||
> | n(self), 'T': [0] * len(self)} | ||||
55 | for line in self.instances: | ||||
56 | uline = line.upper() | ||||
57 | for i, letter in enumerate(uline): | ||||
58 | self.counts[letter][i] += 1 | ||||
59 | def compute_consensus(self): | ||||
60 | self.instances = [x.strip() for x in self.instances] | ||||
61 | self.count() | ||||
62 | for i in range(len(self)): | ||||
63 | a = self.counts['A'][i] | ||||
64 | c = self.counts['C'][i] | ||||
65 | t = self.counts['T'][i] | ||||
66 | g = self.counts['G'][i] | ||||
67 | nums = [a, c, g, t] | ||||
68 | big = max(nums) | ||||
69 | ind = nums.index(big) | ||||
70 | if ind == 0: | ||||
71 | let = 'A' | ||||
72 | elif ind == 1 : | ||||
73 | let = 'C' | ||||
74 | elif ind == 2: | ||||
75 | let = 'G' | ||||
76 | elif ind == 3 : | ||||
77 | let = 'T' | ||||
78 | self.consensus += let | ||||
79 | def parse(self, filename): | 54 | def parse(self, filename): | ||
80 | with open(filename, 'r') as f: | 55 | with open(filename, 'r') as f: | ||
n | 81 | for line in f: | n | 56 | data = f.readlines() |
57 | self.instances = data[1::2] | ||||
58 | def count(self): | ||||
59 | self.counts['A'] = [0]*len(self) | ||||
60 | self.counts['G'] = [0]*len(self) | ||||
61 | self.counts['C'] = [0]*len(self) | ||||
62 | self.counts['T'] = [0]*len(self) | ||||
63 | for instance in self.instances: | ||||
64 | for i, letter in enumerate(instance.upper()): | ||||
82 | if line[0] == '>': | 65 | if letter == 'A': | ||
83 | pass | 66 | self.counts['A'][i] += 1 | ||
84 | else: | 67 | if letter == 'C': | ||
85 | self.instances.append(line) | 68 | self.counts['C'][i] += 1 | ||
69 | if letter == 'G': | ||||
70 | self.counts['G'][i] += 1 | ||||
71 | if letter == 'T': | ||||
72 | self.counts['T'][i] += 1 | ||||
73 | def compute_consensus(self): | ||||
74 | self.count() | ||||
75 | cone = '' | ||||
76 | for i in range(len(self)): | ||||
77 | letterlist = [self.counts['A'][i], self.counts['C'][i], | ||||
78 | self.counts['G'][i], self.counts['T'][i]] | ||||
79 | peepee = max(letterlist) | ||||
80 | if peepee == letterlist[0]: | ||||
81 | cone += 'A' | ||||
82 | elif peepee == letterlist[1]: | ||||
83 | cone += 'C' | ||||
84 | elif peepee == letterlist[2]: | ||||
85 | cone += 'G' | ||||
86 | elif peepee == letterlist[3]: | ||||
87 | cone += 'T' | ||||
88 | self.consensus = cone | ||||
86 | if __name__ == '__main__': | 89 | if __name__ == '__main__': | ||
87 | lexA = DNAMOTIF() | 90 | lexA = DNAMOTIF() | ||
t | 88 | lexA.parse('lexA.fasta') | t | 91 | lexA.parse(r"C:\Users\cresp\Downloads\c\lexA.fasta") |
89 | print(len(lexA)) | ||||
90 | lexA.count() | 92 | lexA.count() | ||
91 | lexA.compute_consensus() | 93 | lexA.compute_consensus() | ||
92 | print(lexA.consensus) | 94 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import numpy as np, matplotlib.pyplot as plt | n | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | ||||
2 | class PLANT: | 3 | class PLANT: | ||
n | 3 | def __init__(self, string='', generator={}, n=0, deltatheta=0.00): | n | 4 | def __init__(self, initial_state ='' , generator = {} , number_generations = |
> | 0 , deltaTheta = 0.00): | ||||
4 | self.string=string | 5 | self.initial_state = initial_state | ||
5 | self.generator=generator | 6 | self.generator = generator | ||
6 | self.n= int(n) | 7 | self.number_generations = int(number_generations) | ||
7 | self.deltatheta= np.radians(deltatheta) | 8 | self.deltaTheta = np.radians(deltaTheta) | ||
8 | for i in range(0, self.n): | 9 | for i in range(self.number_generations): | ||
9 | result='' | 10 | result = '' | ||
10 | for j in self.string: | 11 | for j in self.initial_state: | ||
11 | if j in self.generator.keys(): | 12 | if j in self.generator.keys(): | ||
12 | result += self.generator[str(j)] | 13 | result += self.generator[str(j)] | ||
13 | else: | 14 | else: | ||
14 | result += j | 15 | result += j | ||
n | 15 | self.string = result | n | 16 | self.initial_state = result |
16 | self.str = self.string | 17 | self.str = self.initial_state | ||
17 | def drawPlant(self): | 18 | def drawPlant(self): | ||
n | 18 | currentpos=[200,0] | n | 19 | currentPt = [200, 0] |
19 | theta=np.radians(90) | 20 | theta = np.radians(90) | ||
20 | drawingState=[] | 21 | drawing=[] | ||
21 | nextpt=() | 22 | nextPt=() | ||
22 | stack=[] | 23 | stack=[] | ||
23 | for i in self.str: | 24 | for i in self.str: | ||
24 | if i.isalpha(): | 25 | if i.isalpha(): | ||
n | 25 | nextpt=[currentpos[0]+np.cos(theta), currentpos[1]+np.sin(theta) | n | 26 | nextPt = [currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta) |
> | ] | > | ] | ||
26 | plt.plot([currentpos[0], nextpt[0]],[currentpos[1],nextpt[1]],co | 27 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||
> | lor='black') | > | olor = 'black') | ||
27 | currentpos=nextpt | 28 | currentPt = nextPt | ||
28 | if i=='[': | 29 | if i == '[': | ||
29 | drawingState = [currentpos, theta] | 30 | drawing = [currentPt , theta] | ||
30 | stack.append(drawingState) | 31 | stack.append(drawing) | ||
31 | elif i==']': | 32 | elif i == ']': | ||
32 | currentpos=stack[-1][0] | 33 | drawing = stack[len(stack) -1] | ||
33 | theta=stack[-1][1] | ||||
34 | stack.pop() | 34 | stack.pop(len(stack)-1) | ||
35 | currentPt = drawing[0] | ||||
36 | theta = drawing[1] | ||||
35 | elif i=='+': | 37 | elif i == '+': | ||
36 | theta += self.deltatheta | 38 | theta += self.deltaTheta | ||
37 | elif i=='-': | 39 | elif i == '-': | ||
38 | theta-=self.deltatheta | 40 | theta -= self.deltaTheta | ||
39 | if __name__ == "__main__": | 41 | if __name__ == '__main__': | ||
40 | string = input() | 42 | initial_state = input() | ||
41 | dictionary = input() | 43 | dictionary = input() | ||
n | 42 | theta = input() | n | ||
43 | n = input() | 44 | num = input() | ||
44 | obj = PLANT(string, dictionary, n, theta) | 45 | angle = float(input()) | ||
46 | f = PLANT(initial_state, dictionary, num , angle) | ||||
45 | obj.drawPlant() | 47 | f.drawPlant() | ||
46 | plt.show() | 48 | plt.show() | ||
47 | class DNAMOTIF: | 49 | class DNAMOTIF: | ||
48 | def __init__(self): | 50 | def __init__(self): | ||
49 | self.instances=[] | 51 | self.instances=[] | ||
50 | self.consensus=[] | 52 | self.consensus=[] | ||
51 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 53 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
52 | def __str__(self): | 54 | def __str__(self): | ||
n | 53 | newstr= ''.join(self.instances) | n | 55 | self.acids = ''.join(self.instances) |
54 | return newstr | 56 | return self.acids | ||
55 | pass | 57 | pass | ||
56 | def __len__(self): | 58 | def __len__(self): | ||
n | 57 | for i in self.instances: | n | 59 | for elements in self.instances: |
58 | return len(i)-1 | 60 | return len(elements)-1 | ||
59 | def count(self): | 61 | def count(self): | ||
60 | length=len(self.instances[0]) | 62 | length=len(self.instances[0]) | ||
61 | for i in range(length-1): | 63 | for i in range(length-1): | ||
n | 62 | numbercounta=0 | n | 64 | numcountA = 0 |
63 | numbercountc=0 | 65 | numcountC = 0 | ||
64 | numbercountg=0 | 66 | numcountG = 0 | ||
65 | numbercountt=0 | 67 | numcountT = 0 | ||
66 | for j in range(len(self.instances)): | 68 | for j in range(len(self.instances)): | ||
n | 67 | if self.instances[j][i].upper()=='A': | n | 69 | if self.instances[j][i].upper() == 'A': |
68 | numbercounta +=1 | 70 | numcountA += 1 | ||
69 | elif self.instances[j][i].upper()=='C': | 71 | elif self.instances[j][i].upper() == 'C': | ||
70 | numbercountc +=1 | 72 | numcountC += 1 | ||
71 | elif self.instances[j][i].upper()=='G': | 73 | elif self.instances[j][i].upper() == 'G': | ||
72 | numbercountg +=1 | 74 | numcountG += 1 | ||
73 | elif self.instances[j][i].upper()=='T': | 75 | elif self.instances[j][i].upper() == 'T': | ||
74 | numbercountt +=1 | 76 | numcountT += 1 | ||
75 | self.counts['A'].append(numbercounta) | 77 | self.counts['A'].append(numcountA) | ||
76 | self.counts['C'].append(numbercountc) | 78 | self.counts['C'].append(numcountC) | ||
77 | self.counts['G'].append(numbercountg) | 79 | self.counts['G'].append(numcountG) | ||
78 | self.counts['T'].append(numbercountt) | 80 | self.counts['T'].append(numcountT) | ||
79 | def compute_consensus(self): | 81 | def compute_consensus(self): | ||
80 | DNAMOTIF.count(self) | 82 | DNAMOTIF.count(self) | ||
n | n | 83 | kenneth=[] | ||
81 | length=len(self.counts['A']) | 84 | length = len(self.counts['A']) | ||
82 | result=[] | ||||
83 | for i in range(length): | 85 | for i in range(length): | ||
n | 84 | newList=[] | n | 86 | sid = [] |
85 | a = self.counts['A'] | 87 | a=self.counts['A'] | ||
86 | c = self.counts['C'] | 88 | c=self.counts['C'] | ||
87 | g = self.counts['G'] | 89 | g=self.counts['G'] | ||
88 | t = self.counts['T'] | 90 | t=self.counts['T'] | ||
89 | newList.append(int(a[i])) | 91 | sid.append(int(a[i])) | ||
90 | newList.append(int(c[i])) | 92 | sid.append(int(c[i])) | ||
91 | newList.append(int(g[i])) | 93 | sid.append(int(g[i])) | ||
92 | newList.append(int(t[i])) | 94 | sid.append(int(t[i])) | ||
93 | max_value= max(newList) | 95 | maxnum = max(sid) | ||
94 | max_index = newList.index(max_value) | 96 | index = sid.index(maxnum) | ||
95 | if max_index==0: | 97 | if index==0: | ||
96 | result.append('A') | 98 | kenneth.append('A') | ||
97 | if max_index==1: | 99 | if index==1: | ||
98 | result.append('C') | 100 | kenneth.append('C') | ||
99 | if max_index==2: | 101 | if index==2: | ||
100 | result.append('G') | 102 | kenneth.append('G') | ||
101 | if max_index==3: | 103 | if index==3: | ||
102 | result.append('T') | 104 | kenneth.append('T') | ||
103 | self.consensus=(str(''.join(result))) | 105 | self.consensus=(str(''.join(kenneth))) | ||
104 | def parse(self, filename): | 106 | def parse(self, filename): | ||
n | 105 | f = open(filename) | n | 107 | x = open(filename) |
106 | contents = f.readlines() | 108 | tempList = x.readlines() | ||
109 | MOTIFList = [] | ||||
110 | for i in range(1, len(tempList), 2): | ||||
111 | self.instances.append(tempList[i]) | ||||
112 | for i in range (1, len(self.instances)): | ||||
113 | MOTIFList.append(self.instances[i]) | ||||
107 | f.close() | 114 | x.close() | ||
108 | list_elements = [] | 115 | print(len(MOTIFList)) | ||
109 | for x in range(0, len(contents)): | 116 | return(len(MOTIFList)) | ||
110 | if '>' not in contents[x]: | ||||
111 | self.instances.append(contents[x]) | ||||
112 | for x in range (0, len(self.instances)-1): | ||||
113 | list_elements.append(f'{self.instances[x]}\n') | ||||
114 | if __name__ == '__main__': | 117 | if __name__ == '__main__': | ||
t | 115 | self = DNAMOTIF() | t | 118 | self=DNAMOTIF() |
116 | DNAMOTIF.parse(self, "lexA.fasta") | 119 | DNAMOTIF.parse(self, 'lexA.fasta') | ||
117 | DNAMOTIF.__len__(self, "lexA.fasta") | 120 | DNAMOTIF.__len__(self, 'lexA.fasta') |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | 2 | import numpy as np | ||
n | 3 | def loops(strings,generator,iterations): | n | 3 | def new(strings,generator,iterations): |
4 | strn = strings | 4 | string = strings | ||
5 | for t in range(iterations): | 5 | i=0 | ||
6 | while i < iterations: | ||||
6 | lststr=list(strn) | 7 | gen=list(string) | ||
7 | for i in range(len(lststr)): | 8 | for h in range(len(gen)): | ||
8 | var=lststr[i] | 9 | stuff=gen[h] | ||
9 | for j in generator: | 10 | for ok in generator: | ||
10 | if var==j: | 11 | if stuff==ok: | ||
11 | lststr[i]=generator[j] | 12 | gen[h]=generator[ok] | ||
13 | i+=1 | ||||
12 | strn=''.join(lststr) | 14 | string=''.join(gen) | ||
13 | return strn | 15 | return string | ||
14 | class PLANT(): | 16 | class PLANT(): | ||
n | 15 | def __init__(self, page, ide, gong,sng=''): | n | 17 | def __init__(self, begin='', generators= {}, n=0, deltaTheta=0): |
16 | self.page=dict(page) | 18 | self.begin=str(begin) | ||
19 | self.generators=dict(generators) | ||||
17 | self.ide=int(ide) | 20 | self.n=int(n) | ||
18 | self.gong = np.radians(gong) | 21 | self.deltaTheta = np.radians(deltaTheta) | ||
19 | self.sng = sng | 22 | self.str = new(self.begin,self.generators,self.n) | ||
20 | def __str__(self): | 23 | def str(self): | ||
21 | self.str = loops(self.page,self.ide,self.sng) | 24 | i=0 | ||
25 | while i < self.n: | ||||
26 | new_string='' | ||||
27 | for j in range(len(self.string)): | ||||
28 | if self.string[j] in self.generator: | ||||
29 | new_string+= self.generator[self.string[j]] | ||||
30 | else: | ||||
31 | new_string+=self.string[j] | ||||
32 | i+=1 | ||||
33 | self.string=new_string | ||||
34 | return self.string | ||||
22 | def drawPlant(self): | 35 | def drawPlant(self): | ||
23 | currentPt=(200,0) | 36 | currentPt=(200,0) | ||
24 | theta=90 | 37 | theta=90 | ||
25 | theta=np.radians(theta) | 38 | theta=np.radians(theta) | ||
26 | nextPt = (currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta)) | 39 | nextPt = (currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta)) | ||
n | 27 | pp=[] | n | 40 | stack=[] |
28 | strlst=list(self.str) | 41 | great=list(self.str) | ||
29 | for c in strlst: | 42 | for p in great: | ||
30 | if ']' == c: | 43 | if ']' == p: | ||
31 | (currentPt,theta) = pp[-1] | 44 | (currentPt,theta) = stack[-1] | ||
32 | pp.pop(-1) | 45 | stack.pop(-1) | ||
33 | elif "[" == c: | 46 | elif "[" == p: | ||
34 | pp.append([currentPt,theta]) | 47 | stack.append([currentPt,theta]) | ||
35 | elif "-" == c: | 48 | elif "-" == p: | ||
36 | theta-=-self.gong | 49 | theta=theta-self.deltaTheta | ||
37 | elif '+' == c: | 50 | elif '+' == p: | ||
38 | theta+=self.gong | 51 | theta=theta+self.deltaTheta | ||
39 | else: | 52 | else: | ||
40 | nextPt = (currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta) | 53 | nextPt = (currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta) | ||
> | ) | > | ) | ||
41 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 54 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
42 | currentPt=nextPt | 55 | currentPt=nextPt | ||
n | n | 56 | return plt.show() | ||
43 | if __name__=="__main__": | 57 | if __name__=="__main__": | ||
n | n | 58 | user_string=input() | ||
59 | user_generator=input() | ||||
44 | i1=input() | 60 | user_n=input() | ||
45 | i2=input() | 61 | user_deltaTheta=input() | ||
46 | i3=input() | 62 | myPlant=PLANT(user_string,user_generator,user_n,user_deltaTheta) | ||
47 | i4=input() | 63 | myPlant.drawPlant() | ||
48 | jawn=PLANT(i1,i2,i3,i4) | 64 | print(type(myPlant.string)) | ||
49 | print(jawn.str) | 65 | print(myPlant.str())class DNAMOTIF: | ||
50 | jawn.drawPlant()class DNAMOTIF: | ||||
51 | def __init__(self): | 66 | def __init__(self): | ||
n | 52 | self.instances= [] | n | 67 | self.instances=[] |
53 | self.consensus= '' | 68 | self.consensus='' | ||
54 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 69 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
55 | def __str__(self): | 70 | def __str__(self): | ||
n | 56 | x='' | n | 71 | x="" |
57 | for i in self.instances: | 72 | for i in self.instances: | ||
58 | x+=i | 73 | x+=i | ||
59 | return x | 74 | return x | ||
n | 60 | def __len__(self): | n | 75 | def __len__(self,B=0): |
61 | return len(self.instances[0]) - 1 | 76 | return len(self.instances[0])-1 | ||
62 | def count(self): | 77 | def count(self): | ||
n | 63 | x='' | n | 78 | x="" |
64 | for n in range(0, len(self.instances[0]) - 1): | 79 | for n in range(0,len(self.instances[0])-1): | ||
65 | self.counts['A'].append(0) | 80 | self.counts['A'].append(0) | ||
66 | self.counts['C'].append(0) | 81 | self.counts['C'].append(0) | ||
67 | self.counts['G'].append(0) | 82 | self.counts['G'].append(0) | ||
68 | self.counts['T'].append(0) | 83 | self.counts['T'].append(0) | ||
n | 69 | for x in range(0, len(self.instances)): | n | 84 | for x in range(0,len(self.instances)): |
70 | for y in range(0,len(self.instances[0]) - 1): | 85 | for y in range(0,len(self.instances[0])-1): | ||
71 | i=self.instances[x][y] | 86 | i=self.instances[x][y] | ||
n | 72 | if i=='A' or i=='a': | n | 87 | if i=="A" or i=="a": |
73 | f=self.counts['A'] | 88 | a=self.counts["A"] | ||
74 | f[y]+=1 | 89 | a[y]+=1 | ||
75 | if i=='C' or i=='c': | 90 | elif i=="C" or i=="c": | ||
76 | f=self.counts['C'] | 91 | c=self.counts["C"] | ||
77 | f[y]+=1 | 92 | c[y]+=1 | ||
78 | if i=='G' or i=='g': | 93 | elif i=="G" or i=="g": | ||
79 | f=self.counts['G'] | 94 | g=self.counts["G"] | ||
80 | f[y]+=1 | 95 | g[y]+=1 | ||
81 | if i=='T' or i=='t': | 96 | elif i=="T" or i=="t": | ||
82 | f=self.counts['T'] | 97 | t=self.counts["T"] | ||
83 | f[y]+=1 | 98 | t[y]+=1 | ||
84 | return self.counts | 99 | return self.counts | ||
85 | def compute_consensus(self): | 100 | def compute_consensus(self): | ||
86 | self.count() | 101 | self.count() | ||
n | 87 | A=self.counts['A'] | n | 102 | A=self.counts["A"] |
88 | C=self.counts['C'] | 103 | C=self.counts["C"] | ||
89 | G=self.counts['G'] | 104 | G=self.counts["G"] | ||
90 | T=self.counts['T'] | 105 | T=self.counts["T"] | ||
91 | x='' | 106 | string="" | ||
92 | for i in range(len(A)): | 107 | for i in range(len(A)): | ||
n | 93 | m=max(A[i],C[i],G[i],T[i]) | n | 108 | if A[i]>C[i] and A[i]>G[i] and A[i]>T[i]: |
94 | if A[i]==m: | ||||
95 | x+="A" | 109 | string+="A" | ||
96 | if C[i]==m: | 110 | if T[i]>C[i] and T[i]>G[i] and T[i]>A[i]: | ||
97 | x+="C" | ||||
98 | if G[i]==m: | ||||
99 | x+="G" | ||||
100 | if T[i]==m: | ||||
101 | x+="T" | 111 | string+="T" | ||
112 | if G[i]>C[i] and G[i]>A[i] and G[i]>T[i]: | ||||
113 | string+="G" | ||||
114 | if C[i]>A[i] and C[i]>G[i] and C[i]>T[i]: | ||||
115 | string+="C" | ||||
102 | self.consensus=x | 116 | self.consensus=string | ||
103 | return self.consensus | 117 | return self.consensus | ||
104 | def parse(self, filename): | 118 | def parse(self, filename): | ||
105 | count=0 | 119 | count=0 | ||
106 | with open(filename,'r') as f: | 120 | with open(filename,'r') as f: | ||
107 | for line in f: | 121 | for line in f: | ||
108 | if count%2 != 0: | 122 | if count%2 != 0: | ||
109 | self.instances.append(line) | 123 | self.instances.append(line) | ||
110 | count+=1 | 124 | count+=1 | ||
111 | return self.instances | 125 | return self.instances | ||
112 | if __name__=="__main__": | 126 | if __name__=="__main__": | ||
113 | lexA=DNAMOTIF() | 127 | lexA=DNAMOTIF() | ||
114 | lexA.parse("lexA.fasta") | 128 | lexA.parse("lexA.fasta") | ||
115 | print(lexA.compute_consensus()) | 129 | print(lexA.compute_consensus()) | ||
t | t | 130 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import matplotlib.pyplot as plt | f | 1 | import matplotlib.pyplot as plt |
2 | import numpy as np | 2 | import numpy as np | ||
n | 3 | def new(strings,generator,iterations): | n | 3 | def loops(strings,generator,iterations): |
4 | string = strings | 4 | strn = strings | ||
5 | i=0 | 5 | for t in range(iterations): | ||
6 | while i < iterations: | ||||
7 | gen=list(string) | 6 | lststr=list(strn) | ||
8 | for h in range(len(gen)): | 7 | for i in range(len(lststr)): | ||
9 | stuff=gen[h] | 8 | var=lststr[i] | ||
10 | for ok in generator: | 9 | for j in generator: | ||
11 | if stuff==ok: | 10 | if var==j: | ||
12 | gen[h]=generator[ok] | 11 | lststr[i]=generator[j] | ||
13 | i+=1 | ||||
14 | string=''.join(gen) | 12 | strn=''.join(lststr) | ||
15 | return string | 13 | return strn | ||
16 | class PLANT(): | 14 | class PLANT(): | ||
n | 17 | def __init__(self, begin='', generators= {}, n=0, deltaTheta=0): | n | 15 | def __init__(self, page, ide, gong,sng=''): |
18 | self.begin=str(begin) | 16 | self.page=dict(page) | ||
19 | self.generators=dict(generators) | ||||
20 | self.n=int(n) | 17 | self.ide=int(ide) | ||
21 | self.deltaTheta = np.radians(deltaTheta) | 18 | self.gong = np.radians(gong) | ||
22 | self.str = new(self.begin,self.generators,self.n) | 19 | self.sng = sng | ||
23 | def str(self): | 20 | def __str__(self): | ||
24 | i=0 | 21 | self.str = loops(self.page,self.ide,self.sng) | ||
25 | while i < self.n: | ||||
26 | new_string='' | ||||
27 | for j in range(len(self.string)): | ||||
28 | if self.string[j] in self.generator: | ||||
29 | new_string+= self.generator[self.string[j]] | ||||
30 | else: | ||||
31 | new_string+=self.string[j] | ||||
32 | i+=1 | ||||
33 | self.string=new_string | ||||
34 | return self.string | ||||
35 | def drawPlant(self): | 22 | def drawPlant(self): | ||
36 | currentPt=(200,0) | 23 | currentPt=(200,0) | ||
37 | theta=90 | 24 | theta=90 | ||
38 | theta=np.radians(theta) | 25 | theta=np.radians(theta) | ||
39 | nextPt = (currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta)) | 26 | nextPt = (currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta)) | ||
n | 40 | stack=[] | n | 27 | pp=[] |
41 | great=list(self.str) | 28 | strlst=list(self.str) | ||
42 | for p in great: | 29 | for c in strlst: | ||
43 | if ']' == p: | 30 | if ']' == c: | ||
44 | (currentPt,theta) = stack[-1] | 31 | (currentPt,theta) = pp[-1] | ||
45 | stack.pop(-1) | 32 | pp.pop(-1) | ||
46 | elif "[" == p: | 33 | elif "[" == c: | ||
47 | stack.append([currentPt,theta]) | 34 | pp.append([currentPt,theta]) | ||
48 | elif "-" == p: | 35 | elif "-" == c: | ||
49 | theta=theta-self.deltaTheta | 36 | theta-=-self.gong | ||
50 | elif '+' == p: | 37 | elif '+' == c: | ||
51 | theta=theta+self.deltaTheta | 38 | theta+=self.gong | ||
52 | else: | 39 | else: | ||
53 | nextPt = (currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta) | 40 | nextPt = (currentPt[0]+np.cos(theta), currentPt[1]+np.sin(theta) | ||
> | ) | > | ) | ||
54 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 41 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
55 | currentPt=nextPt | 42 | currentPt=nextPt | ||
n | 56 | return plt.show() | n | ||
57 | if __name__=="__main__": | 43 | if __name__=="__main__": | ||
n | 58 | user_string=input() | n | ||
59 | user_generator=input() | ||||
60 | user_n=input() | 44 | i1=input() | ||
61 | user_deltaTheta=input() | 45 | i2=input() | ||
62 | myPlant=PLANT(user_string,user_generator,user_n,user_deltaTheta) | 46 | i3=input() | ||
63 | myPlant.drawPlant() | 47 | i4=input() | ||
64 | print(type(myPlant.string)) | 48 | jawn=PLANT(i1,i2,i3,i4) | ||
65 | print(myPlant.str())class DNAMOTIF: | 49 | print(jawn.str) | ||
50 | jawn.drawPlant()class DNAMOTIF: | ||||
66 | def __init__(self): | 51 | def __init__(self): | ||
n | 67 | self.instances=[] | n | 52 | self.instances= [] |
68 | self.consensus='' | 53 | self.consensus= '' | ||
69 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 54 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
70 | def __str__(self): | 55 | def __str__(self): | ||
n | 71 | x="" | n | 56 | x='' |
72 | for i in self.instances: | 57 | for i in self.instances: | ||
73 | x+=i | 58 | x+=i | ||
74 | return x | 59 | return x | ||
n | 75 | def __len__(self,B=0): | n | 60 | def __len__(self): |
76 | return len(self.instances[0])-1 | 61 | return len(self.instances[0]) - 1 | ||
77 | def count(self): | 62 | def count(self): | ||
n | 78 | x="" | n | 63 | x='' |
79 | for n in range(0,len(self.instances[0])-1): | 64 | for n in range(0, len(self.instances[0]) - 1): | ||
80 | self.counts['A'].append(0) | 65 | self.counts['A'].append(0) | ||
81 | self.counts['C'].append(0) | 66 | self.counts['C'].append(0) | ||
82 | self.counts['G'].append(0) | 67 | self.counts['G'].append(0) | ||
83 | self.counts['T'].append(0) | 68 | self.counts['T'].append(0) | ||
n | 84 | for x in range(0,len(self.instances)): | n | 69 | for x in range(0, len(self.instances)): |
85 | for y in range(0,len(self.instances[0])-1): | 70 | for y in range(0,len(self.instances[0]) - 1): | ||
86 | i=self.instances[x][y] | 71 | i=self.instances[x][y] | ||
n | 87 | if i=="A" or i=="a": | n | 72 | if i=='A' or i=='a': |
88 | a=self.counts["A"] | 73 | f=self.counts['A'] | ||
89 | a[y]+=1 | 74 | f[y]+=1 | ||
90 | elif i=="C" or i=="c": | 75 | if i=='C' or i=='c': | ||
91 | c=self.counts["C"] | 76 | f=self.counts['C'] | ||
92 | c[y]+=1 | 77 | f[y]+=1 | ||
93 | elif i=="G" or i=="g": | 78 | if i=='G' or i=='g': | ||
94 | g=self.counts["G"] | 79 | f=self.counts['G'] | ||
95 | g[y]+=1 | 80 | f[y]+=1 | ||
96 | elif i=="T" or i=="t": | 81 | if i=='T' or i=='t': | ||
97 | t=self.counts["T"] | 82 | f=self.counts['T'] | ||
98 | t[y]+=1 | 83 | f[y]+=1 | ||
99 | return self.counts | 84 | return self.counts | ||
100 | def compute_consensus(self): | 85 | def compute_consensus(self): | ||
101 | self.count() | 86 | self.count() | ||
n | 102 | A=self.counts["A"] | n | 87 | A=self.counts['A'] |
103 | C=self.counts["C"] | 88 | C=self.counts['C'] | ||
104 | G=self.counts["G"] | 89 | G=self.counts['G'] | ||
105 | T=self.counts["T"] | 90 | T=self.counts['T'] | ||
106 | string="" | 91 | x='' | ||
107 | for i in range(len(A)): | 92 | for i in range(len(A)): | ||
n | 108 | if A[i]>C[i] and A[i]>G[i] and A[i]>T[i]: | n | 93 | m=max(A[i],C[i],G[i],T[i]) |
94 | if A[i]==m: | ||||
109 | string+="A" | 95 | x+="A" | ||
110 | if T[i]>C[i] and T[i]>G[i] and T[i]>A[i]: | 96 | if C[i]==m: | ||
111 | string+="T" | ||||
112 | if G[i]>C[i] and G[i]>A[i] and G[i]>T[i]: | ||||
113 | string+="G" | ||||
114 | if C[i]>A[i] and C[i]>G[i] and C[i]>T[i]: | ||||
115 | string+="C" | 97 | x+="C" | ||
98 | if G[i]==m: | ||||
99 | x+="G" | ||||
100 | if T[i]==m: | ||||
101 | x+="T" | ||||
116 | self.consensus=string | 102 | self.consensus=x | ||
117 | return self.consensus | 103 | return self.consensus | ||
118 | def parse(self, filename): | 104 | def parse(self, filename): | ||
119 | count=0 | 105 | count=0 | ||
120 | with open(filename,'r') as f: | 106 | with open(filename,'r') as f: | ||
121 | for line in f: | 107 | for line in f: | ||
122 | if count%2 != 0: | 108 | if count%2 != 0: | ||
123 | self.instances.append(line) | 109 | self.instances.append(line) | ||
124 | count+=1 | 110 | count+=1 | ||
125 | return self.instances | 111 | return self.instances | ||
126 | if __name__=="__main__": | 112 | if __name__=="__main__": | ||
127 | lexA=DNAMOTIF() | 113 | lexA=DNAMOTIF() | ||
128 | lexA.parse("lexA.fasta") | 114 | lexA.parse("lexA.fasta") | ||
129 | print(lexA.compute_consensus()) | 115 | print(lexA.compute_consensus()) | ||
t | 130 | print(lexA.consensus) | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import math | n | 1 | import numpy as np |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, initial_state , generator, number, deltaTheta): | n | 4 | def __init__(self, string, generator, n, deltaTheta): |
5 | self.initial_state = initial_state | 5 | self.string = string | ||
6 | self.generator = generator | 6 | self.generator = generator | ||
7 | self.number = number | 7 | self.n = n | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
n | 9 | plt = self.initial_state | n | ||
10 | j = 0 | ||||
11 | while j in range(0,self.number): | 9 | for i in range(self.n): | ||
12 | other = '' | 10 | new_string = '' | ||
13 | for t in plt: | 11 | for c in self.string: | ||
14 | if t in generator: | 12 | if c in self.generator: | ||
15 | other +=generator[t] | 13 | new_string += self.generator.get(c) | ||
16 | else: | 14 | else : | ||
17 | other+=t | 15 | new_string += c | ||
18 | plt = other | 16 | self.string = new_string | ||
19 | j+=1 | 17 | def drawPlant(self): | ||
20 | PLANT.str=other | 18 | currentpt = [200,0] | ||
21 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) | 19 | theta = np.radians(90) | ||
22 | print(myPlant.str)class DNAMOTIF: | 20 | drawing_state = [] | ||
21 | nextpt = () | ||||
22 | stack = [] | ||||
23 | for c in self.string: | ||||
24 | if c == '[': | ||||
25 | savedpt = currentpt | ||||
26 | savedtheta = theta | ||||
27 | elif c == ']': | ||||
28 | currentpt = savedpt | ||||
29 | theta = savedtheta | ||||
30 | elif c == '+': | ||||
31 | theta += self.deltatheta | ||||
32 | elif c == '-': | ||||
33 | theta += -self.deltatheta | ||||
34 | elif c in alphabet: | ||||
35 | nextpt = [currentpt[0] + np.cos(theta), currentpt[1] + np.sin(th | ||||
> | eta)] | ||||
36 | plt.plot([currentpt[0], nextpt[0]], [currentpt[1], nextpt[1]], c | ||||
> | olor = 'black') | ||||
37 | currentpt = nextptclass DNAMOTIF: | ||||
23 | def __init__(self): | 38 | def __init__(self): | ||
24 | self.instances=[] | 39 | self.instances=[] | ||
25 | self.consensus=[] | 40 | self.consensus=[] | ||
26 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
27 | def __str__(self): | 42 | def __str__(self): | ||
n | 28 | string = '' | n | 43 | string = "" |
29 | for j in self.instances: | 44 | for i in self.instances: | ||
30 | string +=j + "\n" | 45 | string += i + "\n" | ||
31 | return string[:-2] | 46 | return string[:-2] | ||
32 | def __len__(self): | 47 | def __len__(self): | ||
33 | return len(self.instances) | 48 | return len(self.instances) | ||
34 | def count(self): | 49 | def count(self): | ||
n | 35 | for j in self.instances: | n | 50 | for i in self.instances: |
36 | temp = j.upper() | 51 | temp = i.upper() | ||
37 | self.counts["A"].append(temp.count("A")) | 52 | self.counts["A"].append(temp.count("A")) | ||
38 | self.counts["C"].append(temp.count("C")) | 53 | self.counts["C"].append(temp.count("C")) | ||
39 | self.counts["G"].append(temp.count("G")) | 54 | self.counts["G"].append(temp.count("G")) | ||
40 | self.counts["T"].append(temp.count("T")) | 55 | self.counts["T"].append(temp.count("T")) | ||
41 | def compute_consensus(self): | 56 | def compute_consensus(self): | ||
42 | A = self.counts["A"] | 57 | A = self.counts["A"] | ||
43 | C = self.counts["C"] | 58 | C = self.counts["C"] | ||
44 | G = self.counts["G"] | 59 | G = self.counts["G"] | ||
45 | T = self.counts["T"] | 60 | T = self.counts["T"] | ||
n | 46 | for j in range(len(A)): | n | 61 | for i in range(len(A)): |
47 | if(A[j] >= C[j] and A[j] >= G[j] and A[j] >= T[j]): | 62 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | ||
48 | self.consensus.append("A") | 63 | self.consensus.append("A") | ||
n | 49 | elif (C[j] >= G[j] and C[j] >= T[j]): | n | 64 | elif (C[i] >= G[i] and C[i] >= T[i]): |
50 | self.consensus.append("C") | 65 | self.consensus.append("C") | ||
n | 51 | elif (G[j] >= T[j]): | n | 66 | elif (G[i] >= T[i]): |
52 | self.consensus.append("G") | 67 | self.consensus.append("G") | ||
53 | else: | 68 | else: | ||
54 | self.consensus.append("T") | 69 | self.consensus.append("T") | ||
55 | def parse(self, filename): | 70 | def parse(self, filename): | ||
56 | with open(filename,'r') as f: | 71 | with open(filename,'r') as f: | ||
57 | for i in f: | 72 | for i in f: | ||
58 | if ">" in i: | 73 | if ">" in i: | ||
n | 59 | continue | n | 74 | continue |
60 | else: | 75 | else: | ||
61 | self.instances.append(i) | 76 | self.instances.append(i) | ||
t | 62 | lexA = DNAMOTIF() | t | ||
63 | lexA.parse("lexA.fasta") | ||||
64 | print(len(lexA)) | ||||
65 | lexA = DNAMOTIF() | ||||
66 | lexA.parse("lexA.fasta") | ||||
67 | print(lexA) | ||||
68 | lexA.count() | ||||
69 | print(lexA.counts) | ||||
70 | lexA.compute_consensus() | ||||
71 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | 1 | import numpy as np | n | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, string, generator, n, deltaTheta): | n | 4 | def __init__(self, initial_state , generator, number, deltaTheta): |
5 | self.string = string | 5 | self.initial_state = initial_state | ||
6 | self.generator = generator | 6 | self.generator = generator | ||
7 | self.n = n | 7 | self.number = number | ||
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
n | n | 9 | plt = self.initial_state | ||
10 | j = 0 | ||||
9 | for i in range(self.n): | 11 | while j in range(0,self.number): | ||
10 | new_string = '' | 12 | other = '' | ||
11 | for c in self.string: | 13 | for t in plt: | ||
12 | if c in self.generator: | 14 | if t in generator: | ||
13 | new_string += self.generator.get(c) | 15 | other +=generator[t] | ||
14 | else : | 16 | else: | ||
15 | new_string += c | 17 | other+=t | ||
16 | self.string = new_string | 18 | plt = other | ||
17 | def drawPlant(self): | 19 | j+=1 | ||
18 | currentpt = [200,0] | 20 | PLANT.str=other | ||
19 | theta = np.radians(90) | 21 | myPlant = PLANT('b', {'b':'a', 'a':'ab'},5,25) | ||
20 | drawing_state = [] | 22 | print(myPlant.str)class DNAMOTIF: | ||
21 | nextpt = () | ||||
22 | stack = [] | ||||
23 | for c in self.string: | ||||
24 | if c == '[': | ||||
25 | savedpt = currentpt | ||||
26 | savedtheta = theta | ||||
27 | elif c == ']': | ||||
28 | currentpt = savedpt | ||||
29 | theta = savedtheta | ||||
30 | elif c == '+': | ||||
31 | theta += self.deltatheta | ||||
32 | elif c == '-': | ||||
33 | theta += -self.deltatheta | ||||
34 | elif c in alphabet: | ||||
35 | nextpt = [currentpt[0] + np.cos(theta), currentpt[1] + np.sin(th | ||||
> | eta)] | ||||
36 | plt.plot([currentpt[0], nextpt[0]], [currentpt[1], nextpt[1]], c | ||||
> | olor = 'black') | ||||
37 | currentpt = nextptclass DNAMOTIF: | ||||
38 | def __init__(self): | 23 | def __init__(self): | ||
39 | self.instances=[] | 24 | self.instances=[] | ||
40 | self.consensus=[] | 25 | self.consensus=[] | ||
41 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 26 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
42 | def __str__(self): | 27 | def __str__(self): | ||
n | 43 | string = "" | n | 28 | string = '' |
44 | for i in self.instances: | 29 | for j in self.instances: | ||
45 | string += i + "\n" | 30 | string +=j + "\n" | ||
46 | return string[:-2] | 31 | return string[:-2] | ||
47 | def __len__(self): | 32 | def __len__(self): | ||
48 | return len(self.instances) | 33 | return len(self.instances) | ||
49 | def count(self): | 34 | def count(self): | ||
n | 50 | for i in self.instances: | n | 35 | for j in self.instances: |
51 | temp = i.upper() | 36 | temp = j.upper() | ||
52 | self.counts["A"].append(temp.count("A")) | 37 | self.counts["A"].append(temp.count("A")) | ||
53 | self.counts["C"].append(temp.count("C")) | 38 | self.counts["C"].append(temp.count("C")) | ||
54 | self.counts["G"].append(temp.count("G")) | 39 | self.counts["G"].append(temp.count("G")) | ||
55 | self.counts["T"].append(temp.count("T")) | 40 | self.counts["T"].append(temp.count("T")) | ||
56 | def compute_consensus(self): | 41 | def compute_consensus(self): | ||
57 | A = self.counts["A"] | 42 | A = self.counts["A"] | ||
58 | C = self.counts["C"] | 43 | C = self.counts["C"] | ||
59 | G = self.counts["G"] | 44 | G = self.counts["G"] | ||
60 | T = self.counts["T"] | 45 | T = self.counts["T"] | ||
n | 61 | for i in range(len(A)): | n | 46 | for j in range(len(A)): |
62 | if(A[i] >= C[i] and A[i] >= G[i] and A[i] >= T[i]): | 47 | if(A[j] >= C[j] and A[j] >= G[j] and A[j] >= T[j]): | ||
63 | self.consensus.append("A") | 48 | self.consensus.append("A") | ||
n | 64 | elif (C[i] >= G[i] and C[i] >= T[i]): | n | 49 | elif (C[j] >= G[j] and C[j] >= T[j]): |
65 | self.consensus.append("C") | 50 | self.consensus.append("C") | ||
n | 66 | elif (G[i] >= T[i]): | n | 51 | elif (G[j] >= T[j]): |
67 | self.consensus.append("G") | 52 | self.consensus.append("G") | ||
68 | else: | 53 | else: | ||
69 | self.consensus.append("T") | 54 | self.consensus.append("T") | ||
70 | def parse(self, filename): | 55 | def parse(self, filename): | ||
71 | with open(filename,'r') as f: | 56 | with open(filename,'r') as f: | ||
72 | for i in f: | 57 | for i in f: | ||
73 | if ">" in i: | 58 | if ">" in i: | ||
n | 74 | continue | n | 59 | continue |
75 | else: | 60 | else: | ||
76 | self.instances.append(i) | 61 | self.instances.append(i) | ||
t | t | 62 | lexA = DNAMOTIF() | ||
63 | lexA.parse("lexA.fasta") | ||||
64 | print(len(lexA)) | ||||
65 | lexA = DNAMOTIF() | ||||
66 | lexA.parse("lexA.fasta") | ||||
67 | print(lexA) | ||||
68 | lexA.count() | ||||
69 | print(lexA.counts) | ||||
70 | lexA.compute_consensus() | ||||
71 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | from math import radians, cos, sin | ||
1 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
n | 2 | import math | n | 3 | def generate(text, generator): |
4 | gen = '' | ||||
5 | for letter in text: | ||||
6 | if letter in generator.keys(): | ||||
7 | gen += generator[letter] | ||||
8 | elif letter not in generator.keys(): | ||||
9 | gen += letter | ||||
10 | return gen | ||||
3 | class PLANT: | 11 | class PLANT: | ||
n | 4 | def __init__(self, initial_state, generator, n, delta_theta): | n | 12 | def __init__(self, state, generator, n, dTheta): |
5 | self.initial_state = initial_state | ||||
6 | self.generator = generator | 13 | self.generator = generator | ||
7 | self.n = n | 14 | self.n = n | ||
n | 8 | self.delta_theta = delta_theta | n | 15 | self.dTheta = radians(dTheta) |
9 | self.str = initial_state | 16 | newstate = state | ||
10 | for i in range(n): | 17 | for i in range(n): | ||
n | 11 | new_state = '' | n | 18 | newstate = generate(newstate, generator) |
12 | for x in self.str: | ||||
13 | if x in self.generator: | ||||
14 | new_state += self.generator[x] | ||||
15 | else: | ||||
16 | new_state += x | ||||
17 | self.str = new_state | 19 | self.str = newstate | ||
18 | def drawPlant(self): | 20 | def drawPlant(self): | ||
n | n | 21 | stack = [] | ||
19 | currentPt = (200,0) | 22 | currentPt = (200,0) | ||
n | 20 | theta = math.radians(90) | n | 23 | theta = radians(90) |
21 | stack = [] | 24 | for letter in self.str: | ||
22 | for i in range(len(self.str)): | 25 | if letter.isalpha(): | ||
23 | if self.str[i] == '[': | ||||
24 | stack.append([currentPt, theta]) | ||||
25 | elif self.str[i] == ']': | ||||
26 | currentPt = stack[-1][0] | ||||
27 | theta = stack[-1][1] | ||||
28 | stack.pop(-1) | ||||
29 | elif self.str[i] == '+': | ||||
30 | theta += math.radians(self.delta_theta) | ||||
31 | elif self.str[i] == '-': | ||||
32 | theta -= math.radians(self.delta_theta) | ||||
33 | else: | ||||
34 | nextPt = (currentPt[0]+math.cos(theta), currentPt[1]+math.sin(th | 26 | nextPt = (currentPt[0] + cos(theta), currentPt[1] + sin(theta)) | ||
> | eta)) | ||||
35 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 27 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
36 | currentPt = nextPt | 28 | currentPt = nextPt | ||
n | n | 29 | elif letter == '[': | ||
30 | stack.append(currentPt) | ||||
31 | stack.append(theta) | ||||
32 | elif letter == ']': | ||||
33 | theta = stack.pop() | ||||
34 | currentPt = stack.pop() | ||||
35 | elif letter == '+': | ||||
36 | theta -= self.dTheta | ||||
37 | elif letter == '-': | ||||
38 | theta += self.dTheta | ||||
39 | plt.savefig('plant') | ||||
37 | if __name__ == '__main__': | 40 | if __name__ == '__main__': | ||
n | 38 | myPlant=PLANT('F',{'F': 'F[+F]F[-F]F'},3,25.7) | n | 41 | plant1 = PLANT('F',{'F': 'F[+F]F[-F]F'},5,25.7) |
39 | print(myPlant.str) | 42 | plant1.drawPlant()class DNAMOTIF: | ||
40 | myPlant.drawPlant() | ||||
41 | plt.axis('image') | ||||
42 | plt.show() | ||||
43 | class DNAMOTIF: | ||||
44 | def __init__(self): | 43 | def __init__(self): | ||
45 | self.instances=[] | 44 | self.instances=[] | ||
n | 46 | self.consensus=[] | n | 45 | self.consensus= '' |
47 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 46 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
48 | def __str__(self): | 47 | def __str__(self): | ||
n | 49 | motif = '' | n | 48 | return '\n'.join(self.instances) |
50 | for i in range(len(self.instances)): | ||||
51 | motif += (self.instances[i]) | ||||
52 | return(motif) | ||||
53 | def __len__(self): | 49 | def __len__(self): | ||
n | n | 50 | self.instances = [x.strip() for x in self.instances] | ||
54 | return(len(self.instances[0])-1) | 51 | return len(self.instances[0]) | ||
55 | def count(self): | 52 | def count(self): | ||
n | 56 | for index in range(len(self.instances[0])-1): | n | 53 | self.instances = [x.strip() for x in self.instances] |
57 | self.counts['A'].append(0) | 54 | self.counts = {'A': [0] * len(self), 'C': [0] * len(self), 'G': [0] * le | ||
> | n(self), 'T': [0] * len(self)} | ||||
58 | self.counts['C'].append(0) | ||||
59 | self.counts['G'].append(0) | ||||
60 | self.counts['T'].append(0) | ||||
61 | for line in self.instances: | 55 | for line in self.instances: | ||
62 | letter = line[index].upper() | 56 | uline = line.upper() | ||
57 | for i, letter in enumerate(uline): | ||||
63 | self.counts[letter][index] += 1 | 58 | self.counts[letter][i] += 1 | ||
64 | def compute_consensus(self): | 59 | def compute_consensus(self): | ||
n | n | 60 | self.instances = [x.strip() for x in self.instances] | ||
65 | self.count() | 61 | self.count() | ||
n | 66 | most_frequent = '' | n | 62 | for i in range(len(self)): |
67 | for index in range(len(self.instances[0])-1): | ||||
68 | highest = (self.counts['A'][index], 'A') | 63 | a = self.counts['A'][i] | ||
69 | if self.counts['C'][index] > highest[0]: | 64 | c = self.counts['C'][i] | ||
70 | highest = (self.counts['C'][index], 'C') | ||||
71 | if self.counts['G'][index] > highest[0]: | ||||
72 | highest = (self.counts['G'][index], 'G') | ||||
73 | if self.counts['T'][index] > highest[0]: | ||||
74 | highest = (self.counts['T'][index], 'T') | 65 | t = self.counts['T'][i] | ||
75 | most_frequent += highest[1] | 66 | g = self.counts['G'][i] | ||
76 | self.consensus = most_frequent | 67 | nums = [a, c, g, t] | ||
68 | big = max(nums) | ||||
69 | ind = nums.index(big) | ||||
70 | if ind == 0: | ||||
71 | let = 'A' | ||||
72 | elif ind == 1 : | ||||
73 | let = 'C' | ||||
74 | elif ind == 2: | ||||
75 | let = 'G' | ||||
76 | elif ind == 3 : | ||||
77 | let = 'T' | ||||
78 | self.consensus += let | ||||
77 | def parse(self, filename): | 79 | def parse(self, filename): | ||
78 | with open(filename, 'r') as f: | 80 | with open(filename, 'r') as f: | ||
79 | for line in f: | 81 | for line in f: | ||
80 | if line[0] == '>': | 82 | if line[0] == '>': | ||
81 | pass | 83 | pass | ||
82 | else: | 84 | else: | ||
83 | self.instances.append(line) | 85 | self.instances.append(line) | ||
84 | if __name__ == '__main__': | 86 | if __name__ == '__main__': | ||
t | 85 | lexA=DNAMOTIF() | t | 87 | lexA = DNAMOTIF() |
86 | lexA.parse("lexA.fasta") | 88 | lexA.parse('lexA.fasta') | ||
89 | print(len(lexA)) | ||||
90 | lexA.count() | ||||
87 | lexA.compute_consensus() | 91 | lexA.compute_consensus() | ||
88 | print(lexA.consensus) | 92 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import numpy as np | f | 1 | import numpy as np |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, string, generator, n, deltaTheta): | n | 4 | def __init__(self, init_state, generator, n, deltaTheta): |
5 | self.string = str(string) | 5 | self.initial_state=str(init_state) | ||
6 | self.generator = dict(generator) | 6 | self.generator=dict(generator) | ||
7 | self.n = int(n) | 7 | self.n=int(n) | ||
8 | self.deltaTheta = float(deltaTheta)class DNAMOTIF: | 8 | self.deltaTheta= float(deltaTheta) | ||
9 | self.str= self.initial_state | ||||
10 | plant_str = str(self.initial_state) | ||||
11 | for c in range(n): | ||||
12 | lst= list(plant_str) | ||||
13 | two_Lst=[] | ||||
14 | for i in lst: | ||||
15 | if c in self.generator.keys(): | ||||
16 | two_Lst.append(self.generator.get(i)) | ||||
17 | else: | ||||
18 | two_Lst.append(i) | ||||
19 | lst=two_Lst | ||||
20 | plant_str="".join((lst)) | ||||
21 | self.str=plant_str | ||||
22 | class DNAMOTIF: | ||||
9 | def __init__(self): | 23 | def __init__(self): | ||
10 | self.instances=[] | 24 | self.instances=[] | ||
11 | self.consensus=[] | 25 | self.consensus=[] | ||
12 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 26 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
13 | def __str__(self): | 27 | def __str__(self): | ||
n | 14 | numtif='' | n | 28 | motif= '' |
15 | len_a = len(self.instances) | ||||
16 | for i in range(len_a): | 29 | for i in range(len(self.instances)): | ||
17 | numtif += (self.instances[i]) | 30 | motif+= self.instances[i] | ||
18 | return(numtif) | 31 | return motif | ||
19 | def __len__(self): | 32 | def __len__(self): | ||
n | 20 | lentif = len(self.instances[0])-1 | n | 33 | return(len(self.instances[0])-1) |
21 | return(lentif) | ||||
22 | def count(self): | 34 | def count(self): | ||
n | 23 | for i in range(len(self.instances[0])-1): | n | 35 | for c in range(len(self.instances[0])-1): |
24 | self.counts['A'].append(0) | 36 | self.counts['A'].append(0) | ||
25 | self.counts['C'].append(0) | 37 | self.counts['C'].append(0) | ||
26 | self.counts['G'].append(0) | 38 | self.counts['G'].append(0) | ||
27 | self.counts['T'].append(0) | 39 | self.counts['T'].append(0) | ||
n | 28 | for j in self.instances: | n | 40 | for strand in self.instances: |
29 | let = j[i].lower() | 41 | character=strand[c].upper() | ||
30 | if let == 'a': | ||||
31 | self.counts['A'][i] += 1 | 42 | self.counts[character][c]+=1 | ||
32 | elif let == 'c': | ||||
33 | self.counts['C'][i] += 1 | ||||
34 | elif let == 'g': | ||||
35 | self.counts['G'][i] += 1 | ||||
36 | elif let == 't': | ||||
37 | self.counts['T'][i] += 1 | ||||
38 | def compute_consensus(self): | 43 | def compute_consensus(self): | ||
n | 39 | self.count() | n | 44 | self.count() |
40 | lst='' | 45 | dna_List='' | ||
41 | for i in range(len(self.instances[0])-1): | 46 | for c in range(len(self.instances[0])-1): | ||
42 | if self.counts['A'][i] > self.counts['C'][i] and self.counts['A'][i] | 47 | consensus='A' | ||
> | > self.counts['G'][i] and self.counts['A'][i] > self.counts['T'][i]: | ||||
43 | common = 'A' | ||||
44 | elif self.counts['C'][i] > self.counts['A'][i] and self.counts['C'][ | 48 | if self.counts['C'][c]>self.counts['A'][c] and self.counts['C'][c]>s | ||
> | i] > self.counts['G'][i] and self.counts['C'][i] > self.counts['T'][i]: | > | elf.counts['G'][c] and self.counts['C'][c]>self.counts['T'][c]: | ||
45 | common = 'C' | 49 | consensus='C' | ||
46 | elif self.counts['G'][i] > self.counts['A'][i] and self.counts['G'][ | 50 | if self.counts['G'][c]>self.counts['A'][c] and self.counts['G'][c]>s | ||
> | i] > self.counts['C'][i] and self.counts['G'][i] > self.counts['T'][i]: | > | elf.counts['C'][c] and self.counts['G'][c]>self.counts['T'][c]: | ||
47 | common = 'G' | 51 | consensus='G' | ||
48 | elif self.counts['T'][i] > self.counts['A'][i] and self.counts['T'][ | 52 | if self.counts['T'][c]>self.counts['A'][c] and self.counts['T'][c]>s | ||
> | i] > self.counts['C'][i] and self.counts['T'][i] > self.counts['G'][i]: | > | elf.counts['C'][c] and self.counts['T'][c]>self.counts['G'][c]: | ||
49 | common = 'T' | 53 | consensus='T' | ||
50 | lst = lst+common | 54 | dna_List=dna_List+consensus | ||
51 | self.consensus = lst | 55 | self.consensus= dna_List | ||
52 | def parse(self, filename): | 56 | def parse(self, filename): | ||
t | 53 | file = open(filename, 'r') | t | 57 | with open(filename, 'r') as f: |
54 | for i in file: | 58 | for strand in f: | ||
55 | orig = i | 59 | if strand[0]=='>': | ||
56 | i = i.lower() | 60 | pass | ||
57 | if i[0]=='a' or i[0]=='t' or i[0]=='c' or i[0]=='g': | 61 | else: | ||
58 | self.instances.append(orig) | 62 | self.instances.append(strand) | ||
59 | if __name__=='__main__': | 63 | if __name__=='__main__': | ||
60 | df= DNAMOTIF() | 64 | lexA= DNAMOTIF() | ||
61 | df.parse(r'C:\Users\maxwe\Downloads\lexA.fasta') | 65 | lexA.parse('lexA.fasta') | ||
62 | df.compute_consensus() | 66 | lexA.compute_consensus() | ||
63 | print(df.consensus) | 67 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import numpy as np | f | 1 | import numpy as np |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, init_state, generator, n, deltaTheta): | n | 4 | def __init__(self, string, generator, n, deltaTheta): |
5 | self.initial_state=str(init_state) | 5 | self.string = str(string) | ||
6 | self.generator=dict(generator) | 6 | self.generator = dict(generator) | ||
7 | self.n=int(n) | 7 | self.n = int(n) | ||
8 | self.deltaTheta= float(deltaTheta) | 8 | self.deltaTheta = float(deltaTheta)class DNAMOTIF: | ||
9 | self.str= self.initial_state | ||||
10 | plant_str = str(self.initial_state) | ||||
11 | for c in range(n): | ||||
12 | lst= list(plant_str) | ||||
13 | two_Lst=[] | ||||
14 | for i in lst: | ||||
15 | if c in self.generator.keys(): | ||||
16 | two_Lst.append(self.generator.get(i)) | ||||
17 | else: | ||||
18 | two_Lst.append(i) | ||||
19 | lst=two_Lst | ||||
20 | plant_str="".join((lst)) | ||||
21 | self.str=plant_str | ||||
22 | class DNAMOTIF: | ||||
23 | def __init__(self): | 9 | def __init__(self): | ||
24 | self.instances=[] | 10 | self.instances=[] | ||
25 | self.consensus=[] | 11 | self.consensus=[] | ||
26 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 12 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
27 | def __str__(self): | 13 | def __str__(self): | ||
n | 28 | motif= '' | n | 14 | numtif='' |
15 | len_a = len(self.instances) | ||||
29 | for i in range(len(self.instances)): | 16 | for i in range(len_a): | ||
30 | motif+= self.instances[i] | 17 | numtif += (self.instances[i]) | ||
31 | return motif | 18 | return(numtif) | ||
32 | def __len__(self): | 19 | def __len__(self): | ||
n | 33 | return(len(self.instances[0])-1) | n | 20 | lentif = len(self.instances[0])-1 |
21 | return(lentif) | ||||
34 | def count(self): | 22 | def count(self): | ||
n | 35 | for c in range(len(self.instances[0])-1): | n | 23 | for i in range(len(self.instances[0])-1): |
36 | self.counts['A'].append(0) | 24 | self.counts['A'].append(0) | ||
37 | self.counts['C'].append(0) | 25 | self.counts['C'].append(0) | ||
38 | self.counts['G'].append(0) | 26 | self.counts['G'].append(0) | ||
39 | self.counts['T'].append(0) | 27 | self.counts['T'].append(0) | ||
n | 40 | for strand in self.instances: | n | 28 | for j in self.instances: |
41 | character=strand[c].upper() | 29 | let = j[i].lower() | ||
30 | if let == 'a': | ||||
42 | self.counts[character][c]+=1 | 31 | self.counts['A'][i] += 1 | ||
32 | elif let == 'c': | ||||
33 | self.counts['C'][i] += 1 | ||||
34 | elif let == 'g': | ||||
35 | self.counts['G'][i] += 1 | ||||
36 | elif let == 't': | ||||
37 | self.counts['T'][i] += 1 | ||||
43 | def compute_consensus(self): | 38 | def compute_consensus(self): | ||
n | 44 | self.count() | n | 39 | self.count() |
45 | dna_List='' | 40 | lst='' | ||
46 | for c in range(len(self.instances[0])-1): | 41 | for i in range(len(self.instances[0])-1): | ||
47 | consensus='A' | 42 | if self.counts['A'][i] > self.counts['C'][i] and self.counts['A'][i] | ||
> | > self.counts['G'][i] and self.counts['A'][i] > self.counts['T'][i]: | ||||
43 | common = 'A' | ||||
48 | if self.counts['C'][c]>self.counts['A'][c] and self.counts['C'][c]>s | 44 | elif self.counts['C'][i] > self.counts['A'][i] and self.counts['C'][ | ||
> | elf.counts['G'][c] and self.counts['C'][c]>self.counts['T'][c]: | > | i] > self.counts['G'][i] and self.counts['C'][i] > self.counts['T'][i]: | ||
49 | consensus='C' | 45 | common = 'C' | ||
50 | if self.counts['G'][c]>self.counts['A'][c] and self.counts['G'][c]>s | 46 | elif self.counts['G'][i] > self.counts['A'][i] and self.counts['G'][ | ||
> | elf.counts['C'][c] and self.counts['G'][c]>self.counts['T'][c]: | > | i] > self.counts['C'][i] and self.counts['G'][i] > self.counts['T'][i]: | ||
51 | consensus='G' | 47 | common = 'G' | ||
52 | if self.counts['T'][c]>self.counts['A'][c] and self.counts['T'][c]>s | 48 | elif self.counts['T'][i] > self.counts['A'][i] and self.counts['T'][ | ||
> | elf.counts['C'][c] and self.counts['T'][c]>self.counts['G'][c]: | > | i] > self.counts['C'][i] and self.counts['T'][i] > self.counts['G'][i]: | ||
53 | consensus='T' | 49 | common = 'T' | ||
54 | dna_List=dna_List+consensus | 50 | lst = lst+common | ||
55 | self.consensus= dna_List | 51 | self.consensus = lst | ||
56 | def parse(self, filename): | 52 | def parse(self, filename): | ||
t | 57 | with open(filename, 'r') as f: | t | 53 | file = open(filename, 'r') |
58 | for strand in f: | 54 | for i in file: | ||
59 | if strand[0]=='>': | 55 | orig = i | ||
60 | pass | 56 | i = i.lower() | ||
61 | else: | 57 | if i[0]=='a' or i[0]=='t' or i[0]=='c' or i[0]=='g': | ||
62 | self.instances.append(strand) | 58 | self.instances.append(orig) | ||
63 | if __name__=='__main__': | 59 | if __name__=='__main__': | ||
64 | lexA= DNAMOTIF() | 60 | df= DNAMOTIF() | ||
65 | lexA.parse('lexA.fasta') | 61 | df.parse(r'C:\Users\maxwe\Downloads\lexA.fasta') | ||
66 | lexA.compute_consensus() | 62 | df.compute_consensus() | ||
67 | print(lexA.consensus) | 63 | print(df.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
f | 1 | import math | f | 1 | import math |
2 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
3 | class PLANT: | 3 | class PLANT: | ||
n | 4 | def __init__(self, initial, gen = {}, n = 0, deltaTheta = 0): | n | 4 | def __init__(self,initial,gen={},num=0,deltaTheta=0): |
5 | self.initial = initial | 5 | self.initial = initial | ||
6 | self.gen = gen | 6 | self.gen = gen | ||
n | 7 | self.n = n | n | 7 | self.num = num |
8 | self.deltaTheta = deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | self.str = initial | 9 | self.str = initial | ||
n | 10 | while n>0: | n | 10 | while num > 0: |
11 | n -= 1 | 11 | num -= 1 | ||
12 | y = '' | 12 | y = '' | ||
13 | for i in self.str: | 13 | for i in self.str: | ||
14 | if i in gen: | 14 | if i in gen: | ||
15 | y += gen[i] | 15 | y += gen[i] | ||
16 | else: | 16 | else: | ||
17 | y += i | 17 | y += i | ||
18 | self.str = y | 18 | self.str = y | ||
19 | def drawPlant(self): | 19 | def drawPlant(self): | ||
n | 20 | currentPt=(200,0) | n | 20 | current_point=(200,0) |
21 | theta = 90*math.pi/180 | 21 | theta = 90 * math.pi/180 | ||
22 | next_point = (current_point[0] + math.cos(theta), current_point[1] + mat | ||||
> | h.sin(theta)) | ||||
23 | plt.plot([current_point[0],next_point[0]],[current_point[1],next_point[1 | ||||
> | ]],color='black') | ||||
22 | stack = [] | 24 | x = [] | ||
23 | for i in self.str: | 25 | for i in self.str: | ||
24 | if i == '[': | 26 | if i == '[': | ||
n | 25 | stack.append([currentPt,theta]) | n | 27 | x.append([current_point,theta]) |
26 | elif i == ']': | 28 | elif i == ']': | ||
n | 27 | currentPt = stack[-1][0] | n | 29 | current_point = x[-1][0] |
28 | theta = stack[-1][-1] | 30 | theta = x[-1][1] | ||
29 | stack.pop() | ||||
30 | elif i == '+': | 31 | elif i == '+': | ||
n | 31 | theta += self.deltaTheta*math.pi/180 | n | 32 | theta += self.deltaTheta |
32 | elif i == '-': | 33 | elif i == '-': | ||
n | 33 | theta -= self.deltaTheta*math.pi/180 | n | 34 | theta -= self.deltaTheta |
34 | else: | 35 | current_point = next_point | ||
35 | nextPt = (currentPt[0]+math.cos(theta), currentPt[1]+math.sin(th | 36 | x = [] | ||
> | eta)) | ||||
36 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 37 | for i in self.str: | ||
> | ='black') | ||||
38 | if i == '[': | ||||
39 | x.append([current_point,theta]) | ||||
40 | elif i == ']': | ||||
37 | currentPt = nextPt | 41 | current_point = x[-1][0] | ||
38 | if __name__ == "__main__": | 42 | theta = x[-1][1] | ||
39 | Pl = PLANT('F',{'F':'F[+F]F[-F]F'},5,25.7) | 43 | elif i == '+': | ||
40 | Pl.drawPlant() | 44 | theta += self.deltaTheta | ||
41 | plt.show | 45 | elif i == '-': | ||
42 | plt.savefig('books_read.png') | 46 | theta -= self.deltaThetaclass DNAMOTIF: | ||
43 | class DNAMOTIF: | ||||
44 | def __init__(self): | 47 | def __init__(self): | ||
45 | self.instances=[] | 48 | self.instances=[] | ||
n | 46 | self.consensus='' | n | 49 | self.consensus=[] |
47 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 50 | self.counts= {'A':[],'C':[],'G':[],'T':[]} | ||
48 | def __str__(self): | 51 | def __str__(self): | ||
n | n | 52 | str = '' | ||
49 | for i in self.instances: | 53 | for i in self.instances: | ||
50 | return i | 54 | return i | ||
n | n | 55 | str += i | ||
56 | return str[:-2] | ||||
51 | def __len__(self): | 57 | def __len__(self): | ||
52 | return len(self.instances[0])-1 | 58 | return len(self.instances[0])-1 | ||
53 | def count(self): | 59 | def count(self): | ||
n | n | 60 | i = 0 | ||
61 | x = 0 | ||||
54 | xA = [] | 62 | A = [] | ||
55 | xC = [] | 63 | C = [] | ||
56 | xG = [] | 64 | G = [] | ||
57 | xT = [] | 65 | T = [] | ||
58 | for i in range(len(self.instances[0])-1): | 66 | for i in range(len(self.instances[0])-1): | ||
n | 59 | xA.append(0) | n | 67 | A.append(0) |
60 | xC.append(0) | 68 | C.append(0) | ||
61 | xG.append(0) | 69 | G.append(0) | ||
62 | xT.append(0) | 70 | T.append(0) | ||
63 | for i in self.instances: | 71 | for i in self.instances: | ||
64 | i = i.upper() | 72 | i = i.upper() | ||
65 | z = 0 | 73 | z = 0 | ||
66 | for j in i: | 74 | for j in i: | ||
67 | if j == 'A': | 75 | if j == 'A': | ||
n | 68 | xA[z] += 1 | n | 76 | A[z] += 1 |
69 | elif j == 'C': | 77 | elif j == 'C': | ||
n | 70 | xC[z] += 1 | n | 78 | C[z] += 1 |
71 | elif j == 'G': | 79 | elif j == 'G': | ||
n | 72 | xG[z] += 1 | n | 80 | G[z] += 1 |
73 | elif j == 'T': | 81 | elif j == 'T': | ||
n | 74 | xT[z] += 1 | n | 82 | T[z] += 1 |
75 | z += 1 | 83 | z += 1 | ||
n | 76 | self.counts['A'] = xA | n | 84 | self.counts['A'] = A |
77 | self.counts['C'] = xC | 85 | self.counts['C'] = C | ||
78 | self.counts['G'] = xG | 86 | self.counts['G'] = G | ||
79 | self.counts['T'] = xT | 87 | self.counts['T'] = T | ||
80 | def compute_consensus(self): | 88 | def compute_consensus(self): | ||
n | 81 | xA = [] | n | 89 | self.count() |
82 | xC = [] | 90 | A = self.counts["A"] | ||
83 | xG = [] | 91 | C = self.counts["C"] | ||
84 | xT = [] | 92 | G = self.counts["G"] | ||
85 | for i in range(len(self.instances[0])-1): | 93 | T = self.counts["T"] | ||
86 | xA.append(0) | ||||
87 | xC.append(0) | ||||
88 | xG.append(0) | ||||
89 | xT.append(0) | ||||
90 | for i in self.instances: | ||||
91 | i = i.upper() | ||||
92 | z = 0 | ||||
93 | for j in i: | ||||
94 | if j == 'A': | ||||
95 | xA[z] += 1 | ||||
96 | elif j == 'C': | ||||
97 | xC[z] += 1 | ||||
98 | elif j == 'G': | ||||
99 | xG[z] += 1 | ||||
100 | elif j == 'T': | ||||
101 | xT[z] += 1 | ||||
102 | z += 1 | ||||
103 | x = '' | ||||
104 | for i in range(len(xA)): | 94 | for amino in range(len(A)): | ||
105 | if max(xA[i],xC[i],xG[i],xT[i]) == xA[i]: | 95 | if(A[amino]>= C[amino] and A[amino] >= G[amino] and A[amino] >= T[a | ||
> | mino]): | ||||
106 | x += 'A' | 96 | self.consensus.append("A") | ||
107 | elif max(xA[i],xC[i],xG[i],xT[i]) == xC[i]: | 97 | elif (C[amino] >= G[amino] and C[amino] >= T[amino]): | ||
108 | x += 'C' | 98 | self.consensus.append("C") | ||
109 | elif max(xA[i],xC[i],xG[i],xT[i]) == xG[i]: | 99 | elif (G[amino] >= T[amino]): | ||
110 | x += 'G' | 100 | self.consensus.append("G") | ||
111 | elif max(xA[i],xC[i],xG[i],xT[i]) == xT[i]: | 101 | else: | ||
112 | x += 'T' | 102 | self.consensus.append("T") | ||
113 | self.consensus = x | 103 | self.consensus = "".join(self.consensus) | ||
114 | def parse(self, filename): | 104 | def parse(self, filename): | ||
t | 115 | x = open(filename,'r') | t | 105 | with open(filename,'r') as f: |
116 | y = 0 | 106 | for i in f: | ||
117 | for i in x.readlines(): | 107 | if ">" in i: | ||
118 | y += 1 | 108 | continue | ||
119 | if y%2 == 0: | 109 | else: | ||
120 | self.instances.append(i) | 110 | self.instances.append(i) | ||
111 | lexA=DNAMOTIF() | ||||
112 | lexA.parse("lexA.fasta") | ||||
113 | lexA.count() | ||||
114 | print(lexA.counts) | ||||
115 | lexA.compute_consensus() | ||||
116 | print(lexA.consensus) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | import numpy | ||
1 | import matplotlib.pyplot as plt | 2 | import matplotlib.pyplot as plt | ||
n | 2 | import numpy as np | n | ||
3 | from math import sin, cos, radians | ||||
4 | class PLANT: | 3 | class PLANT: | ||
n | 5 | def __init__(self,initialstate, generator, iterations, deltaTheta) -> None: | n | 4 | def __init__(self, string, dictionary, n, deltaTheta): |
6 | self.initialstate=initialstate | 5 | self.string = string | ||
7 | self.generator=generator | 6 | self.dictionary = dictionary | ||
8 | self.iterations=iterations | 7 | self.n = n | ||
9 | self.deltaTheta=deltaTheta | 8 | self.deltaTheta = deltaTheta | ||
9 | self.str = PLANT.generator(string, dictionary, n) | ||||
10 | def generator(line, dictionary, n): | ||||
11 | def unknown(line): | ||||
12 | if line in dictionary: | ||||
13 | return dictionary.get(line) | ||||
14 | return line | ||||
15 | subparts = [line] | ||||
10 | for i in range(iterations): | 16 | for i in range(n): | ||
11 | string='' | 17 | beginning = subparts[-1] | ||
12 | for letter in self.initialstate: | 18 | middle = [unknown(quan) for quan in beginning] | ||
13 | val = generator.get(letter) | 19 | subparts.append(''.join(middle)) | ||
14 | if val: | 20 | finish = subparts[n] | ||
15 | string += val | 21 | return finish | ||
16 | else: | ||||
17 | string += letter | ||||
18 | self.initialstate=string | ||||
19 | self.str = self.initialstate | ||||
20 | def drawPlant(self): | 22 | def drawPlant(self): | ||
n | n | 23 | alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P' | ||
> | ,'Q','R','S','T','U','V','W','X','Y','Z'] | ||||
21 | currentPt=(200,0) | 24 | currentPt = (200,0) | ||
22 | theta=90 | 25 | theta = numpy.radians(90) | ||
23 | saved=[] | 26 | deltaTheta = numpy.radians(self.deltaTheta) | ||
27 | stack = [currentPt, theta] | ||||
24 | for everyletter in self.str: | 28 | for offer in self.str: | ||
25 | everyletter.upper() | 29 | if offer.upper() in alpha: | ||
26 | if everyletter=='A': | ||||
27 | Radian = radians(theta) | ||||
28 | nextPt = (currentPt[0] + cos(Radian), currentPt[1] + sin(Radian) | 30 | nextPt = (currentPt[0]+(numpy.cos(theta)), currentPt[1]+(numpy.s | ||
> | ) | > | in(theta))) | ||
29 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | 31 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||
> | ='black') | > | ='black') | ||
30 | currentPt = nextPt | 32 | currentPt = nextPt | ||
n | 31 | elif everyletter=='B': | n | 33 | else: |
32 | Radian= radians(theta) | 34 | if offer == '[': | ||
33 | nextPt = (currentPt[0] +cos(Radian), currentPt[1] +sin(Radian)) | ||||
34 | plt.plot([currentPt[0], nextPt[0]],[currentPt[1], nextPt[1]], co | ||||
> | lor = 'black') | ||||
35 | currentPt = nextPt | ||||
36 | elif everyletter=='C': | ||||
37 | Radian =radians(theta) | ||||
38 | nextPt = (currentPt[0] + cos(Radian),currentPt[1] + sin(Radian)) | ||||
39 | plt.plot([currentPt[0],nextPt[0]], [currentPt[1], nextPt[1]], co | ||||
> | lor = 'black') | ||||
40 | currentPt = nextPt | ||||
41 | elif everyletter=='D': | ||||
42 | Radian = radians(theta) | ||||
43 | nextPt = (currentPt[0]+ cos(Radian), currentPt[1] + sin(Radian)) | ||||
44 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1],nextPt[1]],col | ||||
> | or = 'black') | ||||
45 | currentPt = nextPt | ||||
46 | elif everyletter=='E': | ||||
47 | Radian = radians(theta) | ||||
48 | nextPt = (currentPt[0] + cos(Radian), currentPt[1]+ sin(Radian)) | ||||
49 | plt.plot([currentPt[0], nextPt[0]],[currentPt[1], nextPt[1]], co | ||||
> | lor = 'black') | ||||
50 | currentPt = nextPt | ||||
51 | elif everyletter=='F': | ||||
52 | Radian= radians(theta) | ||||
53 | nextPt = (currentPt[0]+ cos(Radian), currentPt[1] + sin(Radian)) | ||||
54 | plt.plot([currentPt[0],nextPt[0]], [currentPt[1],nextPt[1]], col | ||||
> | or = 'black') | ||||
55 | currentPt = nextPt | ||||
56 | elif everyletter=='G': | ||||
57 | Radian =radians(theta) | ||||
58 | nextPt = (currentPt[0] +cos(Radian), currentPt[1] +sin(Radian)) | ||||
59 | plt.plot([currentPt[0], nextPt[0]],[currentPt[1], nextPt[1]], co | ||||
> | lor = 'black') | ||||
60 | currentPt =nextPt | ||||
61 | elif everyletter=='H': | ||||
62 | Radian =radians(theta) | ||||
63 | nextPt = (currentPt[0] +cos(Radian), currentPt[1]+ sin(Radian)) | ||||
64 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]],co | ||||
> | lor = 'black') | ||||
65 | currentPt = nextPt | ||||
66 | elif everyletter=='I': | ||||
67 | Radian =radians(theta) | ||||
68 | nextPt= (currentPt[0] +cos(Radian), currentPt[1] + sin(Radian)) | ||||
69 | plt.plot([currentPt[0], nextPt[0]],[currentPt[1], nextPt[1]], co | ||||
> | lor = 'black') | ||||
70 | currentPt =nextPt | ||||
71 | elif everyletter=='J': | ||||
72 | Radian= radians(theta) | ||||
73 | nextPt =(currentPt[0] + cos(Radian),currentPt[1] + sin(Radian)) | ||||
74 | plt.plot([currentPt[0],nextPt[0]], [currentPt[1],nextPt[1]], col | ||||
> | or = 'black') | ||||
75 | currentPt= nextPt | ||||
76 | elif everyletter=='K': | ||||
77 | Radian= radians(theta) | ||||
78 | nextPt = (currentPt[0] + cos(Radian),currentPt[1] +sin(Radian)) | ||||
79 | plt.plot([currentPt[0],nextPt[0]], [currentPt[1], nextPt[1]], co | ||||
> | lor = 'black') | ||||
80 | currentPt = nextPt | ||||
81 | elif everyletter=='L': | ||||
82 | Radian= radians(theta) | ||||
83 | nextPt= (currentPt[0] +cos(Radian), currentPt[1] +sin(Radian)) | ||||
84 | plt.plot([currentPt[0], nextPt[0]],[currentPt[1], nextPt[1]], co | ||||
> | lor = 'black') | ||||
85 | currentPt = nextPt | ||||
86 | elif everyletter=='M': | ||||
87 | Radian =radians(theta) | ||||
88 | nextPt= (currentPt[0] +cos(Radian), currentPt[1] + sin(Radian)) | ||||
89 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1],nextPt[1]], co | ||||
> | lor = 'black') | ||||
90 | currentPt = nextPt | ||||
91 | elif everyletter=='N': | ||||
92 | Radian =radians(theta) | ||||
93 | nextPt= (currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
94 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
95 | currentPt = nextPt | ||||
96 | elif everyletter=='O': | ||||
97 | Radian = radians(theta) | ||||
98 | nextPt = (currentPt[0] + cos(Radian), currentPt[1] + sin(Radian) | ||||
> | ) | ||||
99 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
100 | currentPt =nextPt | ||||
101 | elif everyletter=='P': | ||||
102 | Radian =radians(theta) | ||||
103 | nextPt =(currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
104 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
105 | currentPt = nextPt | ||||
106 | elif everyletter=='Q': | ||||
107 | Radian =radians(theta) | ||||
108 | nextPt =(currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
109 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
110 | currentPt = nextPt | ||||
111 | elif everyletter=='R': | ||||
112 | Radian= radians(theta) | ||||
113 | nextPt= (currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
114 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
115 | currentPt= nextPt | ||||
116 | elif everyletter=='S': | ||||
117 | Radian= radians(theta) | ||||
118 | nextPt= (currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
119 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
120 | currentPt = nextPt | ||||
121 | elif everyletter=='T': | ||||
122 | Radian= radians(theta) | ||||
123 | nextPt= (currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
124 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
125 | currentPt= nextPt | ||||
126 | elif everyletter=='U': | ||||
127 | Radian= radians(theta) | ||||
128 | nextPt= (currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
129 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
130 | currentPt = nextPt | ||||
131 | elif everyletter=='V': | ||||
132 | Radian =radians(theta) | ||||
133 | nextPt =(currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
134 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
135 | currentPt =nextPt | ||||
136 | elif everyletter=='W': | ||||
137 | Radian= radians(theta) | ||||
138 | nextPt =(currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
139 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
140 | currentPt = nextPt | ||||
141 | elif everyletter=='X': | ||||
142 | Radian =radians(theta) | ||||
143 | nextPt= (currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
144 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
145 | currentPt = nextPt | ||||
146 | elif everyletter=='Y': | ||||
147 | Radian =radians(theta) | ||||
148 | nextPt =(currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
149 | plt.plot([currentPt[0], nextPt[0]], [currentPt[1], nextPt[1]], c | ||||
> | olor = 'black') | ||||
150 | currentPt =nextPt | ||||
151 | elif everyletter=='Z': | ||||
152 | Radian= radians(theta) | ||||
153 | nextPt =(currentPt[0] + cos(Radian), currentPt[1] + sin(Radian)) | ||||
154 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||||
> | ='black') | ||||
155 | currentPt= nextPt | ||||
156 | elif everyletter=='[': | ||||
157 | saved.append([currentPt, theta]) | 35 | stack.append(currentPt) | ||
36 | stack.append(theta) | ||||
158 | elif everyletter ==']': | 37 | elif offer == ']': | ||
159 | currentPt, theta =saved.pop() | 38 | theta = stack.pop() | ||
39 | currentPt = stack.pop() | ||||
160 | elif everyletter =='+': | 40 | elif offer == '+': | ||
161 | theta=theta+ self.deltaTheta | 41 | theta += deltaTheta | ||
162 | elif everyletter == '-': | 42 | elif offer == '-': | ||
163 | theta=theta- self.deltaTheta | 43 | theta -= deltaTheta | ||
164 | from fileinput import filename | 44 | return plt.plot | ||
165 | from itertools import count | 45 | if __name__== "__main__": | ||
166 | from re import A, T | 46 | string = input() | ||
167 | from numpy import append | 47 | dictionary = input() | ||
48 | n = int(input()) | ||||
49 | deltaTheta = float(input()) | ||||
50 | p = PLANT(string, dictionary, n, deltaTheta) | ||||
51 | p.drawPlant() | ||||
52 | plt.show() | ||||
168 | class DNAMOTIF: | 53 | class DNAMOTIF: | ||
169 | def __init__(self): | 54 | def __init__(self): | ||
n | 170 | self.instances=[] | n | 55 | self.instances=[] |
171 | self.consensus='' | 56 | self.consensus=[] | ||
172 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 57 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
173 | self.lst=[] | ||||
174 | def __str__(self): | 58 | def __str__(self): | ||
n | 175 | return str(self.instances) | n | 59 | chain = '' |
60 | for insta in self.instances: | ||||
61 | chain += insta | ||||
62 | return string | ||||
176 | def __len__(self): | 63 | def __len__(self): | ||
n | 177 | return len(self.instances[0].strip("\n")) | n | 64 | return len(self.instances[0].rstrip('\n')) |
178 | def count(self): | 65 | def count(self): | ||
n | 179 | for i in range(len(self.instances[0].strip("\n"))): | n | 66 | for insta in range(self.__len__()): |
180 | A=0 | 67 | countA = 0 | ||
181 | C=0 | 68 | countC = 0 | ||
182 | G=0 | 69 | countG = 0 | ||
183 | T=0 | 70 | countT = 0 | ||
184 | for chain in self.instances: | 71 | for instances in self.instances: | ||
185 | justletters=chain.strip('\n') | 72 | if instances[insta].upper() == 'A': | ||
186 | uplst= [x.upper() for x in justletters] | ||||
187 | if uplst[i] == "A": | ||||
188 | A+=1 | 73 | countA += 1 | ||
189 | elif uplst[i] == "C": | 74 | elif instances[insta].upper() == 'C': | ||
190 | C+=1 | 75 | countC += 1 | ||
191 | elif uplst[i]== "T": | 76 | elif instances[insta].upper() == 'G': | ||
192 | G+=1 | 77 | countG += 1 | ||
193 | elif uplst[i]== "G": | 78 | elif instances[insta].upper() == 'T': | ||
194 | T+=1 | 79 | countT += 1 | ||
195 | self.counts['A'].append(A) | 80 | self.counts['A'].append(countA) | ||
196 | self.counts['C'].append(C) | 81 | self.counts['C'].append(countC) | ||
197 | self.counts['T'].append(G) | 82 | self.counts['G'].append(countG) | ||
198 | self.counts['G'].append(T) | 83 | self.counts['T'].append(countT) | ||
199 | print(self.instances) | ||||
200 | print(len(self.instances[0])-1) | ||||
201 | def compute_consensus(self): | 84 | def compute_consensus(self): | ||
n | n | 85 | self.counts = {'A': [],'C': [], 'G':[], 'T':[]} | ||
202 | self.count() | 86 | self.count() | ||
n | 203 | for x in range(len(self.counts['A'])): | n | ||
204 | if (self.counts['A'][x]> self.counts['C'][x]) and (self.counts['A'][ | ||||
> | x]>self.counts['G'][x]) and (self.counts['A'][x]>self.counts['T'][x]): | ||||
205 | self.lst.append('A') | ||||
206 | elif (self.counts['C'][x]>self.counts['A'][x]) and (self.counts['C'] | ||||
> | [x]>self.counts['G'][x]) and (self.counts['C'][x]>self.counts['T'][x]): | ||||
207 | self.lst.append('C') | ||||
208 | elif (self.counts['G'][x]> self.counts['A'][x]) and (self.counts['G' | ||||
> | ][x]>self.counts['C'][x]) and (self.counts['G'][x]>self.counts['T'][x]): | ||||
209 | self.lst.append('G') | ||||
210 | elif (self.counts['T'][x]> self.counts['A'][x]) and (self.counts['T' | ||||
> | ][x]>self.counts['C'][x]) and (self.counts['T'][x]>self.counts['G'][x]): | ||||
211 | self.lst.append('T') | ||||
212 | elif (self.counts['A'][x]== self.counts['C'][x]) or (self.counts['A' | ||||
> | ][x]==self.counts['G'][x]) or (self.counts['A'][x]==self.counts['T'][x]): | ||||
213 | self.lst.append('A') | ||||
214 | elif (self.counts['C'][x]==self.counts['G'][x]) or (self.counts['C'] | ||||
> | [x]==self.counts['T'][x]): | ||||
215 | self.lst.append('C') | ||||
216 | elif (self.counts['G'][x]==self.counts['T'][x]): | ||||
217 | self.lst.append('G') | ||||
218 | finalchain=''.join(self.lst) | ||||
219 | self.consensus=finalchain | 87 | self.consensus = '' | ||
88 | for insta in range(self.__len__()): | ||||
89 | ultimate = max([self.counts['A'][insta], self.counts['C'][insta], se | ||||
> | lf.counts['G'][insta], self.counts['T'][insta]]) | ||||
90 | if ultimate == self.counts['A'][insta]: | ||||
91 | self.consensus += 'A' | ||||
92 | elif ultimate == self.counts['C'][insta]: | ||||
93 | self.consensus += 'C' | ||||
94 | elif ultimate == self.counts['G'][insta]: | ||||
95 | self.consensus += 'G' | ||||
96 | elif ultimate == self.counts['T'][insta]: | ||||
97 | self.consensus += 'T' | ||||
98 | return self.consensus | ||||
220 | def parse(self, filename): | 99 | def parse(self, filename): | ||
t | 221 | with open(filename) as file: | t | 100 | file = open(filename) |
222 | lines = file.readlines() | 101 | lines = file.readlines() | ||
223 | count = 0 | 102 | file.close() | ||
224 | for line in lines: | 103 | for line in lines: | ||
225 | count+=1 | 104 | if line[0] != '>': | ||
226 | if count % 2 == 0: | ||||
227 | self.instances.append(line) | 105 | self.instances.append(line) | ||
228 | lexA = DNAMOTIF() | ||||
229 | lexA.parse("lexA.fasta") | ||||
230 | lexA.count() | ||||
231 | lexA.compute_consensus() | ||||
232 | pass |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
Student (left) and Nearest Neighbor (right).
n | n | 1 | import math | ||
2 | import matplotlib.pyplot as plt | ||||
1 | class PLANT: | 3 | class PLANT: | ||
n | 2 | def __init__(self, string, generator, n, deltaTheta): | n | 4 | def __init__(self, string, dictionary, n, deltaTheta): |
5 | newplant = [] | ||||
3 | self.string = string | 6 | self.string = string | ||
n | 4 | self.generator = generator | n | 7 | self.dictionary = dictionary |
5 | self.n = n | 8 | self.n = n | ||
n | 6 | self.deltaTheta = deltaTheta | n | 9 | self.deltaTheta = (deltaTheta * math.pi)/180 |
10 | newplant = self.string | ||||
11 | for i in range(self.n): | ||||
12 | newplant = [dictionary[i] if i in self.dictionary.keys() else i for | ||||
> | i in newplant] | ||||
13 | newplant = ''.join(newplant) | ||||
14 | self.str = newplant | ||||
15 | pass | ||||
7 | def drawPlant(): | 16 | def drawPlant(self): | ||
8 | currentPt=(200,0) | 17 | currentPt=(200,0) | ||
n | n | 18 | theta = (90*math.pi)/180 | ||
19 | stackPt = [] | ||||
20 | stackTheta = [] | ||||
21 | for j in self.str: | ||||
22 | if j == '[': | ||||
23 | stackPt.append(currentPt) | ||||
24 | stackTheta.append(theta) | ||||
25 | elif j == ']': | ||||
26 | currentPt = stackPt.pop() | ||||
27 | theta = stackTheta.pop() | ||||
28 | elif j == '+': | ||||
29 | theta += self.deltaTheta | ||||
30 | elif j == '-': | ||||
31 | theta -= self.deltaTheta | ||||
32 | else: | ||||
33 | nextPt = currentPt[0]+math.cos(theta), currentPt[1]+math.sin(the | ||||
> | ta) | ||||
34 | plt.plot([currentPt[0],nextPt[0]],[currentPt[1],nextPt[1]],color | ||||
> | ='black') | ||||
35 | currentPt=nextPt | ||||
9 | theta = 90class DNAMOTIF: | 36 | passclass DNAMOTIF: | ||
10 | def __init__(self): | 37 | def __init__(self): | ||
11 | self.instances=[] | 38 | self.instances=[] | ||
n | 12 | self.consensus = '' | n | 39 | self.consensus=[] |
13 | self.length = 0 | ||||
14 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | 40 | self.counts= {'A': [], 'C': [], 'G':[],'T':[]} | ||
15 | def __str__(self): | 41 | def __str__(self): | ||
n | n | 42 | return ''.join(self.instances) | ||
43 | def __len__(self): | ||||
44 | self.length = len(self.instances[0]) - 1 | ||||
45 | return self.length | ||||
16 | pass | 46 | pass | ||
n | 17 | def __len__(self): | n | ||
18 | list = self.instances | ||||
19 | list2 = [] | ||||
20 | for i in list[0]: | ||||
21 | list2.append(i) | ||||
22 | length = (len(list2) - 1) | ||||
23 | self.length = length | ||||
24 | return self.length | ||||
25 | def count(self): | 47 | def count(self): | ||
n | 26 | a = DNAMOTIF() | n | ||
27 | a.parse(self.filename) | ||||
28 | a.__len__() | ||||
29 | list1 = self.instances | ||||
30 | list2 = [] | 48 | A_list = [] | ||
31 | big_list = [] | 49 | C_list = [] | ||
32 | string = '' | 50 | G_list = [] | ||
33 | dict2 = {'Ax':[], 'Cx':[], 'Gx':[],'Tx':[]} | 51 | T_list = [] | ||
34 | for i in list1: | 52 | for j in range(len(self.instances[0]) - 1): | ||
35 | string = string + i | ||||
36 | string = string.upper() | ||||
37 | list2.append(string) | ||||
38 | string = '' | ||||
39 | for i in list2: | ||||
40 | x = -1 | 53 | A = 0 | ||
41 | for j in i: | 54 | C = 0 | ||
55 | G = 0 | ||||
56 | T = 0 | ||||
57 | for i in self.instances: | ||||
58 | if i[j].upper() == 'A': | ||||
42 | x += 1 | 59 | A += 1 | ||
43 | if j == 'A': | ||||
44 | dict2['Ax'].append(x) | ||||
45 | if j == 'C': | 60 | elif i[j].upper() == 'C': | ||
46 | dict2['Cx'].append(x) | 61 | C += 1 | ||
47 | if j == 'G': | 62 | elif i[j].upper() == 'G': | ||
48 | dict2['Gx'].append(x) | 63 | G += 1 | ||
49 | if j == 'T': | 64 | elif i[j].upper() == 'T': | ||
50 | dict2['Tx'].append(x) | ||||
51 | len0 = int(a.length) | ||||
52 | for values in dict2.values(): | ||||
53 | counter = -1 | ||||
54 | while counter < (len0 - 1): | ||||
55 | counter += 1 | ||||
56 | counter2 = 0 | ||||
57 | for i in values: | ||||
58 | if i == counter: | ||||
59 | counter2 += 1 | 65 | T += 1 | ||
66 | pass | ||||
60 | big_list.append(counter2) | 67 | A_list.append(A) | ||
61 | A_list = big_list[0:len0] | 68 | C_list.append(C) | ||
62 | C_list = big_list[len0:(len0*2)] | 69 | G_list.append(G) | ||
63 | G_list = big_list[(len0*2):(len0*3)] | 70 | T_list.append(T) | ||
64 | T_list = big_list[(len0*3):(len0*4)] | 71 | self.counts['A'] = A_list | ||
65 | for i in A_list: | 72 | self.counts['C'] = C_list | ||
66 | self.counts['A'].append(i) | 73 | self.counts['G'] = G_list | ||
67 | for i in C_list: | 74 | self.counts['T'] = T_list | ||
68 | self.counts['C'].append(i) | ||||
69 | for i in G_list: | ||||
70 | self.counts['G'].append(i) | ||||
71 | for i in T_list: | ||||
72 | self.counts['T'].append(i) | ||||
73 | return self.counts | ||||
74 | def compute_consensus(self): | 75 | def compute_consensus(self): | ||
n | 75 | string = '' | n | 76 | placeholder = [] |
76 | a = DNAMOTIF() | ||||
77 | a.parse(self.filename) | ||||
78 | a.__len__() | ||||
79 | a.count() | 77 | self.count() | ||
80 | endList = [] | 78 | for i in range(len(self.instances[0]) - 1): | ||
81 | x = 0 | 79 | if self.counts['A'][i] >= self.counts['C'][i] and self.counts['A'][i | ||
> | ] >= self.counts['G'][i] and self.counts['A'][i] >= self.counts['T'][i]: | ||||
82 | dict1 = a.counts | ||||
83 | while x < a.length: | ||||
84 | newList = [] | ||||
85 | for letter, values in dict1.items(): | ||||
86 | newList.append(values[x]) | ||||
87 | max0 = max(newList) | ||||
88 | max1 = newList.index(max0) | ||||
89 | if max1 == 0: | ||||
90 | endList.append('A') | 80 | placeholder.append('A') | ||
91 | if max1 == 1: | 81 | elif self.counts['C'][i] >= self.counts['G'][i] and self.counts['C'] | ||
> | [i] >= self.counts['T'][i]: | ||||
92 | endList.append('C') | 82 | placeholder.append('C') | ||
93 | if max1 == 2: | 83 | elif self.counts['G'][i] >= self.counts['T'][i]: | ||
94 | endList.append('G') | 84 | placeholder.append('G') | ||
95 | if max1 == 3: | 85 | else: | ||
96 | endList.append('T') | 86 | placeholder.append('T') | ||
97 | x +=1 | 87 | self.consensus = ''.join(placeholder) | ||
98 | for i in endList: | 88 | pass | ||
99 | string = string + i | ||||
100 | self.consensus = self.consensus + string | ||||
101 | return self.consensus | ||||
102 | def parse(self, filename): | 89 | def parse(self, filename): | ||
t | 103 | self.filename = filename | t | ||
104 | list = [] | ||||
105 | with open(filename, 'r') as f: | 90 | with open(filename) as file: | ||
106 | for lx in f: | 91 | lines = file.readlines() | ||
107 | list.append(lx) | 92 | newlines = [i for i in lines if lines.index(i)%2 == 1] | ||
108 | for i in list: | ||||
109 | if '>' in i: | ||||
110 | list.remove(i) | ||||
111 | self.instances = self.instances + list | 93 | self.instances = newlines | ||
112 | return self.instances |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|