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
/
gene_tree.py
executable file
·189 lines (169 loc) · 4.08 KB
/
gene_tree.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
#!/usr/bin/env python
import re
import sys
import copy
import random
import pprint
#from skbio.tree import nj
### TODO
# 1. more alphabetical order of genes in tree
# 2. add deviations to distance matrix to require averaging
# 3. better tree construction, works with #1
#====================
def item2letters(item):
letters = []
for letter in list(item):
if re.match("^[A-Z]$", letter):
letters.append(letter)
return letters
#====================
def distributeDistances(dist):
dist1 = 0
dist2 = 0
for i in range(2*dist):
if random.random() < 0.5:
dist1 += 1
else:
dist2 += 1
return (dist1, dist2)
#====================
def addDistances(item1, item2, distmatrix, dist):
letters1 = item2letters(item1)
letters2 = item2letters(item2)
for l1 in letters1:
for l2 in letters2:
key1 = l1+"-"+l2
key2 = l2+"-"+l1
distmatrix[key1] = dist
distmatrix[key2] = dist
return
#====================
def printTree(origlist, distmatrix):
prevDist = None
prevLetter = None
for i,letter in enumerate(origlist):
sys.stderr.write(letter+" ")
if i < len(origlist)-1:
nextLetter = origlist[i+1]
key = letter+"-"+nextLetter
dist = distmatrix[key]*2
else:
dist = 10000
if prevDist is None or dist < prevDist:
for j in range(dist):
sys.stderr.write("_")
sys.stderr.write("\n ")
for j in range(dist):
sys.stderr.write(" ")
sys.stderr.write("|")
if prevDist is not None:
for j in range(prevDist-dist-1):
sys.stderr.write("_")
sys.stderr.write("\n")
else:
for j in range(prevDist):
sys.stderr.write("_")
sys.stderr.write("|")
if dist < 100:
for j in range(dist-prevDist-1):
sys.stderr.write("_")
sys.stderr.write("\n ")
for j in range(prevDist):
sys.stderr.write(" ")
sys.stderr.write("\n")
prevDist = dist
prevLetter = letter
return
#====================
def printTree2(origlist, distmatrix):
for i,l1 in enumerate(origlist):
sys.stderr.write(l1+" ")
if i > 0:
l2 = origlist[i-1]
key = l1+"-"+l2
d1 = distmatrix[key]
else:
d1 = 100
if i < len(origlist)-1:
l2 = origlist[i+1]
key = l1+"-"+l2
d2 = distmatrix[key]
else:
d2 = 100
d = min(d1, d2)*2
for j in range(d):
sys.stderr.write("_")
if d2 > d1:
sys.stderr.write("|")
else:
sys.stderr.write(" ")
sys.stderr.write("\n ")
if i < len(origlist)-1:
for j in range(d2*2):
sys.stderr.write(" ")
sys.stderr.write("|_")
sys.stderr.write("\n")
#====================
def printMatrix(origlist, distmatrix):
letters = copy.copy(origlist)
letters.sort()
for l in letters:
sys.stderr.write("\t%s"%(l))
sys.stderr.write("\n")
for l1 in letters:
sys.stderr.write(l1)
for l2 in letters:
key = l1+"-"+l2
d = distmatrix[key]*2
sys.stderr.write("\t%d"%(d))
sys.stderr.write("\n")
sys.stderr.write("\n")
#====================
def addItemsToBetterList(item1, item2, betterlist):
if item1< item2:
if len(item1) == 1:
betterlist.append(item1)
if len(item2) == 1:
betterlist.append(item2)
else:
if len(item2) == 1:
betterlist.append(item2)
if len(item1) == 1:
betterlist.append(item1)
return
#====================
if __name__ == '__main__':
if len(sys.argv) >= 2:
num_items = int(sys.argv[1])
else:
num_items = 4
charlist = list("ABCDEFGHJKMPQRSTWXYZ")
itemlist = charlist[:num_items]
random.shuffle(itemlist)
if itemlist[0] > itemlist[-1]:
itemlist.reverse()
origlist = copy.copy(itemlist)
print(itemlist)
dist = 2
distmatrix = {}
for letter in itemlist:
key = letter+"-"+letter
distmatrix[key] = 0
betterlist = []
while len(itemlist) > 1:
merge = random.randint(1,len(itemlist)-1)
addDistances(itemlist[merge-1], itemlist[merge], distmatrix, dist)
addItemsToBetterList(itemlist[merge-1], itemlist[merge], betterlist)
itemlist[merge-1] = "("+itemlist[merge-1]+str(dist)+itemlist[merge]+")"
itemlist.pop(merge)
dist += random.randint(2,6)
print(itemlist)
print("\n")
pprint.pprint(origlist)
pprint.pprint(betterlist)
print("\n")
printMatrix(origlist, distmatrix)
#print("\n")
#printTree(betterlist, distmatrix)
print("\n")
printTree2(betterlist, distmatrix)