-
Notifications
You must be signed in to change notification settings - Fork 1
/
randblos.py
82 lines (67 loc) · 1.66 KB
/
randblos.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
import random
aastring="ARNDCQEGHILKMFPSTWYVBZX*"
mat=[]
def addnum(numcounts,num):
try:
val=numcounts[num]
numcounts[num]=val+1
except:
numcounts[num]=1
return numcounts
def normalize(numcounts):
tot=0.
for i in numcounts.keys():
tot+=numcounts[i]
for i in numcounts.keys():
numcounts[i]=numcounts[i]/tot
return numcounts
def drawnum(numcounts):
r=random.random()
thresh=0.
for i in numcounts.keys():
thresh+=numcounts[i]
if r < thresh:
return i
offcounts={}
matchcounts={}
f=open("blosum62.mat")
f.readline()
lines=f.readlines()
f.close()
for i in range(22):
row=[int(n) for n in lines[i].strip().split()[1:]]
for j in range(i+1):
if i==j:
matchcounts=addnum(matchcounts,row[j])
else:
offcounts=addnum(offcounts,row[j])
matchcounts=normalize(matchcounts)
offcounts=normalize(offcounts)
for i in range(22):
row=[]
for j in range(22):
if i==j:
row.append(drawnum(matchcounts))
mat.append(row)
break
else:
row.append(drawnum(offcounts))
for i in range(len(mat)):
for j in range(i+1,len(mat)):
mat[i].append(mat[j][i])
mat.append([int(i) for i in lines[22].strip().split()[1:]])
mat.append([int(i) for i in lines[23].strip().split()[1:]])
for i in range(22):
mat[i].append(mat[22][i])
mat[i].append(mat[23][i])
for i in mat:
print(i)
f=open("randblos.mat","w")
for i in aastring:
f.write("\t"+i)
f.write("\n")
for i in range(len(mat)):
f.write(aastring[i])
for j in mat[i]:
f.write("\t"+str(j))
f.write("\n")