forked from qfo/benchmark-webservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EcTest.drw
243 lines (221 loc) · 9.39 KB
/
EcTest.drw
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#
# A script to measures the similarity between the putative orthologs
# based on the code of the benchmark paper.
#
# Adrian Altenhoff, Sept 2006
# rewritten Adrian Altenhoff, July 13, 2007
# rewritten for BenchService Adrian Altenhoff, Dec 4, 2009
#
# in: resFn, filter, projs, lock, measure, meth
Set(printgc=false); Set(gc=5e6):
WriteLock(lock,'initializing computations');
prj := [seq(GetPKeyData(z),z=projs)]:
dataset := prj[-1,'ref'];
# load the project databases
nP := length(prj); dbs := CreateArray(1..nP):
for i to nP do dbs[i] := DB := ReadDb( prj[i,'fnBase'].'.db' ); od:
if not type(dbs, list(database)) then
OpenWriting(resFn);
printf('error(''could not open databases'');\n');
OpenWriting(previous);
CallSystem('rm -f '.lock);
quit
fi:
ReadProgram(datdir.'enzymes_'.dataset.'.drw'); # assigns EC
# get the superclasses for a given EC including the empty class -> any Enzyme.
ECsuperclassR := proc( c:string )
i := SearchAllString('.', c):
return( ['', seq(c[1..z-1], z=i), c] );
end:
ECFreqs := proc( proteins:{set,intersect()} )
t := []:
direct := evalb(proteins=intersect());
for i to If(direct,length(EC),length(proteins)) do
z := If(direct,i,proteins[i]);
if EC[z]<>0 then for x in EC[z] do
t := append(t, op(ECsuperclassR(x)) ):
od fi;
od:
freqs := transpose( [seq([z,0], z=[op({op(t)})] )] ):
cnts := 0;
for i to If(direct,length(EC),length(proteins)) do
z := If(direct, i, proteins[i]);
if EC[z]<>0 then for x in EC[z] do
for term in ECsuperclassR(x) do
k := SearchOrderedArray(term, freqs[1]):
assert(freqs[1,k]=term);
freqs[2,k] := freqs[2,k] + 1;
od:
cnts := cnts + 1;
od fi:
od:
if cnts>0 then freqs[2] := freqs[2] / cnts fi:
return(freqs);
end:
ComputeSimilarity := proc(data:list; 'method'=(method:string))
if method='avg Sim' then
return( avg(seq(z[2],z=data)) );
elif method='max Sim' then
return( max(seq(z[2],z=data)) );
elif method='avg Info' then
return( avg(seq(z[3], z=data)) );
elif method='max Info' then
return( max(seq(z[3], z=data)) );
elif method='avg Schlicker' then
max1 := CreateArray(1..max(seq(z[4],z=data)),1..2,-DBL_MAX);
max2 := CreateArray(1..max(seq(z[5],z=data)),1..2,-DBL_MAX);
for i to length(data) do
z := data[i];
if z[2] > max1[z[4],1] then max1[z[4]] := [z[2],i]; fi;
if z[2] > max2[z[5],1] then max2[z[5]] := [z[2],i]; fi;
od:
s := c := 0;
for i in [op(max1),op(max2)] do
if i[1]>-DBL_MAX then s := s+i[1]; c:=c+1; fi:
od:
return( s/c );
else error('similarity not implemented') fi:
end:
# Lookup Function to get the OccurenceFreqencey of a given EC term
GetFreq := proc(ec:string)
k := SearchOrderedArray(ec,freqs[1]);
assert(k>0 and k<=length(freqs[1]) and freqs[1,k]=ec);
return( freqs[2,k] );
end:
# measure similarities between putative orthologs
ComputePerformance := proc(iSet, prjNrs:list)
global DB;
Sims := [seq(Stat(prj[z,'Title']), z=prjNrs)];
directMap := evalb(iSet=intersect());
nProt := If(directMap,length(EC),length(iSet));
rawData := CreateArray(1..length(prjNrs),[]):
for pNr to length(prjNrs) do
DB := dbs[prjNrs[pNr]];
for i to nProt do
oE1 := If(directMap,i,iSet[i]);
if EC[oE1]<>0 then
eNr := If( prj[prjNrs[pNr],'directMap'], oE1, DbEntryNrFromENr(oE1) );
vps := ParseLongList(SearchTag('VP', Entry(eNr)));
for vp in vps do
if vp<oE1 then next fi: # uni-directional
if not member(vp, iSet) then next fi; # not in intersection set
if EC[vp]=0 then next fi: # ortholog has no EC tag
pairs := [];
for ig1 to length(EC[oE1]) do for ig2 to length(EC[vp]) do
ec1 := EC[oE1,ig1]; fec1 := GetFreq(ec1);
ec2 := EC[vp,ig2]; fec2 := GetFreq(ec2);
ic := intersect( {op(ECsuperclassR(ec1))}, {op(ECsuperclassR(ec2))} );
# 2*ln(prob(interClass[i]))
# similarity sim = ---------------------------
# ln(prob(go1))+ln(prob(go2))
simRun := infoRun := -DBL_MAX;
for int in ic do
info := -2*ln(GetFreq(int));
sim := If(info=0, 0, -info/(ln(fec1)+ln(fec2)));
if info > infoRun then infoRun := info; infoInt := int fi;
if sim > simRun then simRun := sim; simInt := int fi;
od:
pairs := append(pairs, [1, simRun, infoRun, ig1, ig2] );
od od:
if length(pairs)>0 then
simPair := ComputeSimilarity(pairs, 'method'=measure);
Sims[pNr] + simPair;
rawData[pNr] := append(rawData[pNr], [oE1, vp, simPair]);
fi:
od:
fi;
if mod(i,5000)=0 then
t := (curP-1+((pNr-1)*nProt+i)/(nProt*length(prjNrs)))/maxP;
WriteLock(lock, sprintf('%.0f%% done. Estimated remaining time: %.0fsec\n', 100*t, (1-t)/t*time()));
fi:
od:
od:
return(Sims,rawData);
end:
StoreRawData := proc(rawData_, prjs_, filter, measure, meth)
if type(prjs_, list) then
prjs := prjs_; rawData := rawData_;
elif type(prjs_,PKeyData) then
prjs := [prjs_]; rawData := [rawData_];
else error('unexpected prjs parameter'); fi:
RawDataFn := CreateArray(1..length(prjs)):
for p to length(prjs) do
fn := sprintf('EC/EC.%a.tsv.gz', hash_sha2([prjs[p,'PID'],filter,measure,meth]));
OpenWriting(rawdir.fn[1..-4]);
printf('# EC Similarities between orthologs from %s\n',prjs[p,'Title']);
printf('# Protein ID 1<tab>Protein ID 2<tab>EC Similarity\n');
for z in rawData[p] do
id1 := ENr2XRef(z[1], dataset);
id2 := ENr2XRef(z[2], dataset);
printf('%s\t%s\t%f\n', id1, id2, z[3]);
od:
OpenWriting(previous);
CallSystem('gzip -9f '.rawdir.fn[1..-4]);
RawDataFn[p] := fn;
od:
return( If(length(prjs)>1,RawDataFn,RawDataFn[1]) );
end:
StoreResult := proc(fn:string, Sims, PIDs, filter, measure, meth; RawDataFn:{string,list(string)} )
OpenWriting(fn):
printf('Sims := %A:\n', Sims):
printf('PIDs := %A:\n', PIDs);
if assigned(RawDataFn) then printf('RawDataFn := %A:\n', RawDataFn); fi;
printf('# filter := %A:\n# measure := %A:\n#meth := %A:\n', filter, measure, meth);
OpenWriting(previous):
CallSystem('gzip -9f '.fn);
end:
if meth='INTERSECT' then
iSet := GetProjISets(prj):
prjNrs:= [seq(i,i=1..nP)];
freqs := ECFreqs(iSet):
curP := 1; maxP := 1;
SimsRes := ComputePerformance(iSet, prjNrs):
RawDataFn := StoreRawData( SimRes[2], [seq(prj[i], i=prjNrs)], filter, measure, meth);
StoreResult(resFn, SimsRes[1], [seq(prj[i,'PID'], i=prjNrs)], filter, measure, meth, RawDataFn);
elif meth='PAIRWISE' then
maxP := nP-1;
SimsRes := CreateArray(1..maxP):
prjNrs := [seq([i,nP],i=1..maxP)];
resFnBase := resFn[1..(SearchAllString('.',resFn)[-2])];
for curP to maxP do
curPrjPIDs := [seq(prj[j,'PID'],j=prjNrs[curP])]:
resFnPart := sprintf('%s%a.drw', resFnBase, hash_sha2([curPrjPIDs,measure,'INTERSECT']) );
if FileExists(resFnPart.'.gz') then
SimsRes[curP] := 0; ReadProgram(resFnPart): assert(length(Sims)=2); SimsRes[curP] := Sims;
else
iSet := GetProjISets( [seq(prj[j],j=prjNrs[curP])] ):
freqs := ECFreqs(iSet):
SimsRes[curP] := ComputePerformance(iSet, prjNrs[curP])[1]:
StoreResult(resFnPart, SimsRes[curP], [seq(prj[j,'PID'], j=prjNrs[curP])], filter, measure, 'INTERSECT');
fi:
od:
StoreResult(resFn, SimsRes, [seq([prj[i,'PID'],prj[nP,'PID']],i=1..maxP)], filter, measure, meth);
elif length(meth)>=6 and meth[1..6]='REFSET' then
SimsRes := CreateArray(1..nP):
RawDataFnRes := CreateArray(1..nP):
resFnBase := resFn[1..(SearchAllString('.',resFn)[-2])];
maxP := nP:
for curP to nP do
resFnPart := sprintf('%s%a.drw', resFnBase, hash_sha2([prj[curP,'PID'],measure,meth]) );
if FileExists(resFnPart.'.gz') then
SimsRes[curP] := 0;
ReadProgram(resFnPart):
assert(type(Sims,Stat));
assert(type(RawDataFn,string));
SimsRes[curP] := Sims;
RawDataFnRes[curP] := RawDataFn;
maxP := maxP - 1;
else
freqs := ECFreqs(intersect()):
perf := ComputePerformance(intersect(), [curP]):
SimsRes[curP] := perf[1,1];
RawDataFnRes[curP] := StoreRawData(perf[2,1], prj[curP], filter, measure, meth);
StoreResult(resFnPart, SimsRes[curP], [prj[curP,'PID']], filter, measure, meth,
RawDataFnRes[curP]);
fi:
od:
StoreResult(resFn, SimsRes, [seq(prj[i,'PID'], i=1..nP)], filter, measure, meth, RawDataFnRes);
else error('unknown strategy method:'.meth);
fi:
CallSystem('rm -f '.lock);
done;