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
/
deletion_mutants.py
executable file
·224 lines (201 loc) · 6.85 KB
/
deletion_mutants.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
#!/usr/bin/env python
import os
import sys
import copy
import random
num2word = {
1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five',
6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten',
11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen',
15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen', 19: 'nineteen',
20: 'twenty',
}
#====================
def makeHtmlTable(origlist, del_set):
table = ''
table += '<table style="border-collapse: collapse; border: 2px solid black; '
width = 60*(len(origlist) + 1)
height = 25*(len(del_set) + 1)
table += 'width: {0:d}px; height: {1:d}px;">'.format(width, height)
table += '<tr><th> </th>'
for i in range(len(origlist)):
table += '<th align="center">Gene {0}</th>'.format(i+1)
table += '</tr>'
colors = ['DarkRed', 'DarkSlateGray', 'DarkGreen', 'Indigo', 'MidnightBlue', 'DarkOliveGreen', ]
for i,deletion in enumerate(del_set):
table += '<tr><th align="center">Del #{0}</th>'.format(i+1)
for gene in origlist:
if gene in deletion:
#black fill
table += '<td bgcolor="{0}"> </td>'.format(colors[i % len(colors)])
else:
table += '<td bgcolor="#EEEEEE"> </td>'
table += '</tr>'
table += '</table>'
return table
#====================
def writeQuestion(origlist, del_set):
### write question
#Genes a, b, c, d, e, and f are closely linked in a chromosome, but their order is unknown.
#Four deletions in the region are found to uncover recessive alleles of the genes as follows:
#Deletion?1 uncovers a, d, and f; Deletion?2 uncovers d, e, and f; Deletion?3 uncovers c, d, and e;
#Deletion?4 uncovers b and c. Which one of the following is the correct order for the genes?
sortedlist = copy.copy(origlist)
sortedlist.sort()
question = ''
sys.stderr.write("\n")
question += (("<p>A total of %s genes "%(num2word[len(origlist)]))
+list2text(sortedlist)+" are closely linked in a single chromosome, but "
+"their order is unknown. ")
question += (("A total of %s deletions in the region are found "%(num2word[len(del_set)]))
+"to uncover recessive alleles of the genes as follows:</p><ul> ")
for i,deletion in enumerate(del_set):
question += ("<li>Deletion #%d uncovers the %s genes %s;</li> "%(i+1, num2word[len(deletion)], list2text(deletion)))
question += ("</ul> <h5>What the correct order for the %s genes?</h5> "%(num2word[len(origlist)]))
question += ("<p>Hint 1: the first gene on the end is gene %s.</p> "%(origlist[0]))
question += ("<p>Hint 2: enter your answer in the blank using only {0} letters with no spaces or commas.</p> ".format(num2word[len(origlist)]))
#sys.stderr.write(question)
sys.stderr.write("\n")
sys.stderr.write("Answer: %s\n"%(list2string(origlist)))
sys.stderr.write("\n")
#sys.stdout.write("Which one of the following is the correct order for the genes?")
sys.stderr.write("##########\n")
return question
#====================
def list2string(mylist):
mystring = ""
for letter in mylist:
mystring += letter
return mystring
#====================
def list2text(mylist):
mystring = ""
if len(mylist) > 2:
sublist = mylist[:-1]
for letter in sublist:
mystring += letter+", "
mystring += "and "+mylist[-1]
elif len(mylist) == 2:
mystring = mylist[0]+" and "+mylist[1]
return mystring
#====================
def makeDeletions(num_items):
charlist = list("ABCDEFGHJKMPQRSTWXYZ")
itemlist = charlist[:num_items]
random.shuffle(itemlist)
# complicated step to get version of list that is most alphabetical
if itemlist[0] > itemlist[-1]:
itemlist.reverse()
origlist = copy.copy(itemlist)
print("itemlist", itemlist)
num_deletions = 5
min_deletion_size = 2
max_deletion_size = min(len(itemlist) - 1,5)
N = len(itemlist)
# N permutations 2, P(N, 2) minus three
num_sub_sets = N - 1
#print("Expect %d del pairs"%(num_sub_sets))
i = 0
iters = 0
del_set = []
split_gene_pairs = {}
used_gene_pairs = {}
while len(split_gene_pairs) < num_sub_sets or len(used_gene_pairs) < num_sub_sets:
iters += 1
#print("%d of %d gene pairs"%(len(split_gene_pairs), num_sub_sets))
if random.random() < 0.1:
delsize = random.randint(min_deletion_size, max_deletion_size)
elif len(itemlist) > 4 and random.random() < 0.7:
delsize = random.randint(min_deletion_size+2, max_deletion_size)
else:
delsize = random.randint(min_deletion_size+1, max_deletion_size)
delstart = random.randint(0, len(itemlist)-delsize)
deletion = itemlist[delstart:delstart+delsize]
add_value = 0
if delstart > 0:
key = itemlist[delstart-1]+itemlist[delstart]
#print key
if split_gene_pairs.get(key) is None:
split_gene_pairs[key] =True
add_value += 1
if delstart+delsize < len(itemlist):
key = itemlist[delstart+delsize-1]+itemlist[delstart+delsize]
#print key
if split_gene_pairs.get(key) is None:
split_gene_pairs[key] =True
add_value += 1
## need to make sure every pair of genes is in a least one deletion
for j in range(len(deletion)-1):
key = deletion[j]+deletion[j+1]
if used_gene_pairs.get(key) is None:
used_gene_pairs[key] =True
add_value += 1
deletion.sort()
if add_value > 0 and not deletion in del_set:
i+=1
del_set.append(deletion)
print(("Del %d: %s"%(i, str(deletion))))
#print(split_gene_pairs)
#print i, len(used_genes), delsize, delstart, deletion
print("")
print(("Total used gene pairs: %d"%(len(used_gene_pairs))))
print(("Total split gene pairs: %d"%(len(split_gene_pairs))))
print("\n##########\n")
return origlist, del_set
#====================
def makeBlackboard(question, table, origlist):
blackboard = ''
blackboard += 'FIB\t'
blackboard += table
blackboard += question
blackboard += '\t'
answer = ''.join(origlist)
blackboard += answer
blackboard += '\t'
blackboard += answer[::-1]
blackboard += '\t'
commas = ','.join(origlist)
blackboard += commas
blackboard += '\t'
blackboard += commas[::-1]
blackboard += '\t'
spaces = ' '.join(origlist)
blackboard += spaces
blackboard += '\t'
blackboard += spaces[::-1]
blackboard += '\n'
return blackboard
#====================
#====================
if __name__ == '__main__':
if len(sys.argv) >= 2:
num_items = int(sys.argv[1])
else:
num_items = 6
if len(sys.argv) >= 3:
no_table = True
else:
no_table = False
if num_items < 4:
print("Sorry, you must have at least 4 genes for this program")
sys.exit(1)
if num_items > 20:
print("Sorry, you must have less than 20 genes for this program")
sys.exit(1)
duplicates = 92
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):
origlist, del_set = makeDeletions(num_items)
random.shuffle(del_set)
if no_table is False:
table = makeHtmlTable(origlist, del_set)
else:
table = ''
print(table)
question = writeQuestion(origlist, del_set)
print(question)
blackboard = makeBlackboard(question, table, origlist)
f.write(blackboard)
f.close()