forked from 865699871/IAG
-
Notifications
You must be signed in to change notification settings - Fork 2
/
calculatednonISEndpoint.py
183 lines (162 loc) · 6.76 KB
/
calculatednonISEndpoint.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
from inferringAncestorGenomeStructure.MatchingOptimization import MatchingOptimization
import os
def transformToAdjacency_for_distance(file):
adjacency_list = []
with open(file) as df:
while True:
line = df.readline()[:-2]
if not line:
break
item = line.split(' ')
chr_type = item[0]
last = ''
start = ''
sequence = item[1:]
for j in range(len(sequence)):
if j == 0:
if chr_type == 's':
if sequence[j].startswith('-'):
adjacency_list.append(['$', sequence[j][1:] + 'b'])
last = sequence[j][1:] + 'a'
else:
adjacency_list.append(['$', sequence[j] + 'a'])
last = sequence[j] + 'b'
else:
if sequence[j].startswith('-'):
last = sequence[j][1:] + 'a'
start = sequence[j][1:] + 'b'
else:
last = sequence[j] + 'b'
start = sequence[j] + 'a'
else:
if sequence[j].startswith('-'):
adjacency_list.append([last, sequence[j][1:] + 'b'])
last = sequence[j][1:] + 'a'
else:
adjacency_list.append([last, sequence[j] + 'a'])
last = sequence[j] + 'b'
if chr_type == 's':
adjacency_list.append([last, '$'])
else:
adjacency_list.append([last, start])
new_adjacency_list = []
for j in adjacency_list:
new_adjacency_list.append(j[0]+'@'+j[1])
return new_adjacency_list
def calculateBPReuseRateForPrePT(filelist,ploidy):
mo = MatchingOptimization(filelist,matching_dim1=4, matching_dim2=2,
relation1=1, relation2=2)
mo.optimization()
mo.matching_relation()
mo.output_new_sequence(filelist[0]+'.relabelforbp',filelist[1]+'.relabelforbp')
filelist = [filelist[0]+'.relabelforbp',
filelist[1]+'.relabelforbp']
endpoints = {}
for i in filelist:
adj = transformToAdjacency_for_distance(i)
for j in adj:
endpoint1 = j.split('@')[0]
endpoint2 = j.split('@')[1]
if endpoint1 not in endpoints.keys():
endpoints[endpoint1] = [endpoint2]
else:
if endpoint2 not in endpoints[endpoint1]:
endpoints[endpoint1].append(endpoint2)
if endpoint2 not in endpoints.keys():
endpoints[endpoint2] = [endpoint1]
else:
if endpoint1 not in endpoints[endpoint2]:
endpoints[endpoint2].append(endpoint1)
rate = 0
for i in endpoints.keys():
if i == '$':
continue
if len(endpoints[i]) == ploidy:
rate += 1
rate = round(rate / (len(endpoints.keys()) - 1), 4)
os.remove(filelist[0])
os.remove(filelist[1])
return rate
def calculateBPReuseRateForPostPs(filelist,ploidy):
mo = MatchingOptimization([filelist[1],filelist[0]],
matching_dim1=2, matching_dim2=2,
relation1=1, relation2=1)
mo.optimization()
mo.matching_relation()
mo.output_new_sequence(filelist[1] + '.relabelforbp',filelist[0] + '.relabelforbp')
mo = MatchingOptimization([filelist[2],filelist[0]],
matching_dim1=2, matching_dim2=2,
relation1=1, relation2=1)
mo.optimization()
mo.matching_relation()
mo.output_new_sequence(filelist[2] + '.relabelforbp',filelist[0] + '.relabelforbp')
filelist = [filelist[0] + '.relabelforbp',
filelist[1] + '.relabelforbp',
filelist[2] + '.relabelforbp']
endpoints = {}
for i in filelist:
adj = transformToAdjacency_for_distance(i)
for j in adj:
endpoint1 = j.split('@')[0]
endpoint2 = j.split('@')[1]
if endpoint1 not in endpoints.keys():
endpoints[endpoint1] = [endpoint2]
else:
if endpoint2 not in endpoints[endpoint1]:
endpoints[endpoint1].append(endpoint2)
if endpoint2 not in endpoints.keys():
endpoints[endpoint2] = [endpoint1]
else:
if endpoint1 not in endpoints[endpoint2]:
endpoints[endpoint2].append(endpoint1)
rate = 0
for i in endpoints.keys():
if i == '$':
continue
if len(endpoints[i]) == ploidy:
rate += 1
rate = round(rate / (len(endpoints.keys()) - 1), 4)
os.remove(filelist[0])
os.remove(filelist[1])
os.remove(filelist[2])
return rate
def calculateBPReuseRateForPrePS(filelist,ploidy):
endpoints = {}
for i in filelist:
adj = transformToAdjacency_for_distance(i)
for j in adj:
endpoint1 = j.split('@')[0]
endpoint2 = j.split('@')[1]
if endpoint1 not in endpoints.keys():
endpoints[endpoint1] = [endpoint2]
else:
if endpoint2 not in endpoints[endpoint1]:
endpoints[endpoint1].append(endpoint2)
if endpoint2 not in endpoints.keys():
endpoints[endpoint2] = [endpoint1]
else:
if endpoint1 not in endpoints[endpoint2]:
endpoints[endpoint2].append(endpoint1)
rate = 0
for i in endpoints.keys():
if i == '$':
continue
if len(endpoints[i]) == ploidy:
rate += 1
rate = round(rate / (len(endpoints.keys()) - 1), 4)
return rate
blockdir = 'D:/InferAncestorGenome/' \
'DCW_HN1_YMR_newpair/IAG/inputFiles/'
resultdir = 'D:/InferAncestorGenome/' \
'DCW_HN1_YMR_newpair/IAG/outputFiles/'
filelist = [blockdir + 'PT.filter.block',blockdir + 'PS.filter.block']
prePTrate = calculateBPReuseRateForPrePT(filelist,3)
filelist = [blockdir + 'PS.filter.block',
resultdir + '1_PT_pre/PT_preWGD_ancestor.block',
resultdir + '2_PS_post/PR.filter.block.double']
postPSrate = calculateBPReuseRateForPostPs(filelist,3)
filelist = [resultdir + '2_PS_post/PS_postWGD_ancestor.block',blockdir + 'PR.filter.block']
prePSrate = calculateBPReuseRateForPrePS(filelist,3)
print('prePT:'+str(prePTrate))
print('postPS:'+str(postPSrate))
print('prePS:' + str(prePSrate))