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
/
hla_genotype.py
executable file
·140 lines (117 loc) · 3.59 KB
/
hla_genotype.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
#!/usr/bin/env python
import os
import sys
import copy
import random
used_markers = {}
def createMarker(num_markers):
charlist = "ABCDEFG"
markers = []
for i in range(num_markers):
letter = charlist[i]
num = random.choice(list(range(1,10)))
marker = "%s%d"%(letter, num)
while used_markers.get(marker) is not None:
num = random.choice(list(range(1,10)))
marker = "%s%d"%(letter, num)
used_markers[marker] = True
markers.append(marker)
return set(markers)
def markersToString(markers):
mystr = ""
markerlist = list(markers)
markerlist.sort()
for marker in markerlist:
mystr += "%s,"%(marker)
return mystr[:-1]
def splitMarkers(markers1, markers2):
markerlist1 = list(markers1)
markerlist1.sort()
markerlist2 = list(markers2)
markerlist2.sort()
markerselect = []
while len(set(markerselect)) <= 1:
markerselect = []
for i in range(len(markerlist1)):
markerselect.append(random.choice((1,2)))
newmarkers = []
for i in range(len(markerselect)):
ms = markerselect[i]
if ms == 1:
newmarkers.append(markerlist1[i])
else:
newmarkers.append(markerlist2[i])
return set(newmarkers)
if __name__ == '__main__':
if len(sys.argv) >= 2:
num_markers = int(sys.argv[1])
else:
num_markers = 2
if num_markers > 7:
num_markers = 7
mom1 = createMarker(num_markers)
mom2 = createMarker(num_markers)
dad1 = createMarker(num_markers)
dad2 = createMarker(num_markers)
print(mom1, mom2, dad1, dad2)
print("")
#question = "XXX. "
question = ""
question += ('A mother has a HLA genotype of <span style="color:FireBrick">{0}</span> on one chromosome and <span style="color:DarkGoldenRod">{1}</span> on the other. '
.format(markersToString(mom1), markersToString(mom2)))
question += ('The father has a HLA genotype of <span style="color:DarkViolet">{0}</span> on one chromosome and <span style="color:DarkBlue">{1}</span> on the other. '
.format(markersToString(dad1), markersToString(dad2)))
question += 'Which one of the following is a possible genotype for one of their offspring?'
print(question)
#A father has a HLA genotype of A2,B1 on one chromosome and A7,B6 on the other.
#The mother has an HLA genotype of A4,B3 on one chromosome and A5,B9 on the other.
#Which one of the following is a possible genotype for one of their offspring?
answers = []
#wrong two moms
answer = copy.copy(mom1)
answer = answer.union(mom2)
answers.append(answer)
#wrong two dads
answer = copy.copy(dad1)
answer = answer.union(dad2)
answers.append(answer)
#real correct answer
answer = copy.copy(random.choice((dad1, dad2)))
answer = answer.union(random.choice((mom1, mom2)))
answers.append(answer)
correct = answer
#jumbled 1
answer = splitMarkers(mom1, mom2)
answer = answer.union(splitMarkers(dad1, dad2))
answers.append(answer)
#jumbled 2
answer = splitMarkers(mom1, mom2)
answer = answer.union(splitMarkers(dad1, dad2))
answers.append(answer)
stringanswers = []
for a in answers:
stringanswers.append(markersToString(a))
stringanswers = list(set(stringanswers))
correct_str = markersToString(correct)
random.shuffle(stringanswers)
charlist = "ABCDE"
answer_str = ""
for i in range(len(stringanswers)):
c = charlist[i]
a = stringanswers[i]
prefix = ''
answer_str += "{0}\t".format(a)
if a == correct_str:
answer_str += "Correct\t"
prefix = '*'
else:
answer_str += "Incorrect\t"
print(("{0}{1}. {2}".format(prefix,c,a)))
outfile = 'bbq-' + os.path.splitext(os.path.basename(__file__))[0] + '-questions.txt'
print('writing to file: '+outfile)
f = open(outfile, 'a')
f.write("MC\t")
f.write(question+"\t")
f.write(answer_str)
f.write('\n')
f.close()