This repository has been archived by the owner on Oct 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consensus_sequence_arbitrary.py
executable file
·235 lines (205 loc) · 5.48 KB
/
consensus_sequence_arbitrary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python
import os
import sys
import copy
import random
num_sequence = 10 #fixed for easy
sequence_length = 9
separate = 3
dna = ['A', 'C', 'G', 'T',]
codes = {
'A': ('A'),
'B': ('C', 'G', 'T',), # not A
'C': ('C'),
'D': ('A', 'G', 'T',), # not C
'G': ('G'),
'H': ('A', 'C', 'T',), # not G
'K': ('G', 'T',),
'M': ('A', 'C',),
'N': ('A', 'C', 'G', 'T',),
'R': ('A', 'G',), # purine
'S': ('C', 'G',), # strong
'T': ('T'),
'V': ('A', 'C', 'G',), # not T
'W': ('A', 'T',), # weak
'Y': ('C', 'T',), # pyrimidine
}
invcodes = {
('A',): 'A',
('C', 'G', 'T'): 'B',
('C',): 'C',
('A', 'G', 'T'): 'D',
('G',): 'G',
('A', 'C', 'T'): 'H',
('G', 'T'): 'K',
('A', 'C'): 'M',
('A', 'C', 'G', 'T'): 'N',
('A', 'G'): 'R',
('C', 'G'): 'S',
('T',): 'T',
('A', 'C', 'G'): 'V',
('A', 'T'): 'W',
('C', 'T'): 'Y'
}
#==========================
def colorNucleotide(nt):
adenine = ' bgcolor="#e6ffe6"' #green
cytosine = ' bgcolor="#e6f3ff"' #blue
thymine = ' bgcolor="#ffe6e6"' #red
guanine = ' bgcolor="#f2f2f2"' #black
uracil = ' bgcolor="#f3e6ff"' #purple
if nt == 'A':
return adenine
elif nt == 'C':
return cytosine
elif nt == 'G':
return guanine
elif nt == 'T':
return thymine
elif nt == 'U':
return thymine
return ''
#==========================
def histogramList(seq_list):
histogram = {}
for nt in dna:
histogram[nt] = seq_list.count(nt)
return histogram
#==========================
def getConsensus(histogram):
max_value = -1
key_list = []
for k,v in histogram.items():
if v > max_value:
max_value = v
for k,v in histogram.items():
if v == max_value:
key_list.append(k)
return key_list
#==========================
def makeSequenceColumn():
seq_list = []
keys = list(codes.keys())
random.shuffle(keys)
seq_key = random.choice(keys)
seq_set = list(codes[seq_key])
for j in range(num_sequence):
seq_list.append(random.choice(seq_set))
histogram = histogramList(seq_list)
invlist = []
print(histogram)
for k,v in histogram.items():
if v > 0:
invlist.append(k)
invkey = tuple(invlist)
inv_seq_key = invcodes[invkey]
if inv_seq_key != seq_key:
print("ERROR key was supposed to be {0} but was only {1}.".format(seq_key, inv_seq_key))
return seq_list, inv_seq_key
#==========================
def makeSequences():
sequence_list = []
for j in range(num_sequence):
sequence_list.append('')
consensus = ''
for i in range(sequence_length):
seq_list, consensus_nt = makeSequenceColumn()
consensus += consensus_nt
for j, nt in enumerate(seq_list):
sequence_list[j] += nt
return consensus, sequence_list
#==========================
def makeSequencesSafe():
not_good = True
i = 0
while not_good is True:
i += 1
#print("trying sequences {0}".format(i))
consensus, sequence_list = makeSequences()
not_good = False
for s in sequence_list:
if s == consensus:
not_good = True
return consensus, sequence_list
#==========================
def scoreSequences(seq1, seq2):
min_length = min(len(seq1), len(seq2))
score = 0
for i in range(min_length):
if seq1[i] == seq2[i]:
score += 1
return score
#==========================
def makeHtmlRow(seq):
htmlrow = ""
htmlrow += "<tr>"
for i in range(len(seq)):
if i > 0 and i % separate == 0:
htmlrow += "<td> , </td> "
nt = seq[i]
htmlrow += "<td {1}> {0} </td> ".format(nt, colorNucleotide(nt))
return htmlrow
#==========================
def makeHtmlTable(sequence_list):
table = ""
table += '<table style="border-collapse: collapse; border: 1px solid silver;"> '
table += "<tr> "
for j in range(num_sequence):
table += makeHtmlRow(sequence_list[j])
table += '</tr></table> '
return table
#==========================
def printSequence(seq, consensus=None):
if consensus is not None:
score = scoreSequences(seq, consensus)
sys.stderr.write('{0:02d} '.format(score))
for i in range(len(seq)):
if i > 0 and i % separate == 0:
sys.stderr.write(', ')
sys.stderr.write(seq[i] + " ")
sys.stderr.write('\n')
#==========================
def insertCommas(my_str):
new_str = ''
for i in range(0, len(my_str), separate):
new_str += my_str[i:i+separate] + ','
return new_str[:-1]
# FIB TAB question text TAB answer text TAB answer two text
#==========================
#==========================
def makeCompleteQuestion():
consensus, sequence_list = makeSequencesSafe()
#for j in range(num_sequence):
# printSequence(sequence_list[j], consensus)
#printSequence(consensus, consensus)
table = makeHtmlTable(sequence_list)
question = "What is the consensus sequence for the table above? "
question += "<br/> <i> you may include a comma every {0} letters, but ".format(separate)
question += "do not include any extra commas or spaces in your answer. </i>"
bbquestion = 'FIB\t'
bbquestion += table + ' <br/> '
bbquestion += question + '\t'
bbquestion += consensus + '\t'
bbquestion += insertCommas(consensus)
#============================
print("blank 1. "+question)
print("A. {0}".format(consensus))
print("B. {0}".format(insertCommas(consensus)))
print('')
return bbquestion
#==========================
#==========================
if __name__ == '__main__':
duplicates = 199
outfile = 'bbq-' + os.path.splitext(os.path.basename(__file__))[0] + '-questions.txt'
print('writing to file: '+outfile)
f = open(outfile, 'w')
for i in range(duplicates):
complete_question = makeCompleteQuestion()
f.write(complete_question)
f.write('\n')
f.close()
#==========================
#==========================
#==========================
#==========================