forked from qfo/benchmark-webservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoTest.drw
263 lines (237 loc) · 10.1 KB
/
GoTest.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#
# 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
printlevel := 2;
Set(printgc=false); Set(gc=5e6):
WriteLock(lock, 'initializing computation');
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:
# hacky thing to make sure Ontolgy_name is loaded and can be accessed.
GOname(55):
allterms := Indices(Ontology_name):
GOparents := table({}):
for i in allterms do GOparents[i] := GOsuperclassR(i) od:
ddir := eval(symbol(lowercase(dataset).'DBpath'));
DB := GODB := ReadDb(ddir.'ServerIndexed.db'):
lprint('GO annotations from '.GODB['FileName']);
# extract important all GO annotations from ServerIndexed.db
LoadGOset := proc(iSet)
global DB, freqs, GOs, nProt;
directMap := evalb(iSet=intersect()):
nProt := If(directMap,GODB[TotEntries],length(iSet)):
GOs := CreateArray(1..nProt,[]):
DB := GODB;
for i to nProt do
eNr := If(directMap, i, iSet[i]);
gos := SearchTag('GO', Entry(eNr));
if gos='' then next fi;
gos := SearchDelim('; ',gos);
gos := [seq(SearchDelim('@',z), z=gos)];
for z in gos do
annotation := parse(z[2]);
evidence := {seq( x[1], x=annotation)};
if length( intersect(evidence, filter) )>0 then
GOs[i] := append(GOs[i], parse(z[1,4..-1]) );
fi;
od:
od:
# compute GO term frequencies of proteins in iSet;
freqs := table(0); cnts := 0;
for p in GOs do for go in p do
cnts := cnts+1;
freqs[go] := freqs[go]+1;
for parent in GOparents[go] do freqs[parent] := freqs[parent]+1; od:
od od:
if cnts>0 then for i in Indices(freqs) do freqs[i] := freqs[i]/cnts od fi:
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:
ComputePerformance := proc(iSet, prjNrs:list)
global DB;
# measure similarities between putative orthologs
Sims := [seq(Stat(prj[z,'Title']), z=prjNrs)];
directMap := evalb(iSet=intersect()):
iList := [op(iSet)]:
onts := [3674, 5575, 8150]:
nP := length(prjNrs);
rawData := CreateArray(1..nP,[]):
for pNr to nP do
DB := dbs[prjNrs[pNr]];
for i to nProt do
if length(GOs[i]) > 0 then
oE := If(directMap, i, iSet[i]);
eNr := If( prj[prjNrs[pNr],'directMap'], oE, DbEntryNrFromENr(oE) );
vps := ParseLongList(SearchTag('VP', Entry(eNr)));
for vp in vps do
if vp<oE then next fi: # uni-directional
if not member(vp, iSet) then next fi; #
if directMap then k := vp;
else
k := SearchOrderedArray(vp, iList):
assert(k>0 and k<=nProt and iList[k]=vp);
fi:
if length(GOs[k]) = 0 then next fi:
pairs := [];
for ig1 to length(GOs[i]) do for ig2 to length(GOs[k]) do
go1 := GOs[i,ig1]; go2 := GOs[k,ig2];
ic := intersect( {go1, op(GOparents[go1])},
{go2, op(GOparents[go2])} );
if length(ic)=0 then next fi: # skip if not same ontology
ont := sum(SearchArray(z, onts),z=ic); # get ontology
if ont<1 or ont>3 then next fi:
# 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(freqs[int]);
sim := If(info=0, 0, -info/(ln(freqs[go1])+ln(freqs[go2])));
if info > infoRun then infoRun := info; infoInt := int fi;
if sim > simRun then simRun := sim; simInt := int fi;
od:
pairs := append(pairs, [ont, simRun, infoRun, ig1, ig2] );
od od:
if length(pairs)>0 then
simPair := ComputeSimilarity(pairs, 'method'=measure);
Sims[pNr] + simPair;
rawData[pNr] := append(rawData[pNr], [oE, 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('GO/GO.%a.tsv.gz', hash_sha2([prjs[p,'PID'],filter,measure,meth]));
OpenWriting(rawdir.fn[1..-4]);
printf('# GO Similarities between orthologs from %s\n',prjs[p,'Title']);
printf('# Protein ID 1<tab>Protein ID 2<tab>GO 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)];
LoadGOset(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])] ):
LoadGOset(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]) );
lprint('checking '.resFnPart);
if FileExists(resFnPart.'.gz') then
lprint('--> loading file');
SimsRes[curP] := 0;
ReadProgram(resFnPart):
assert(type(Sims,Stat));
assert(type(RawDataFn,string));
SimsRes[curP] := Sims;
RawDataFnRes[curP] := RawDataFn;
maxP := maxP - 1;
else
lprint('--> computing go-sim');
LoadGOset(intersect()):
printf('loaded %d go annotations\n', sum(length(z), z=GOs));
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;