-
Notifications
You must be signed in to change notification settings - Fork 8
/
SpeciesTreeDiscordanceTest-fixedsize.drw
325 lines (287 loc) · 11.6 KB
/
SpeciesTreeDiscordanceTest-fixedsize.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
##
# Script to run the species tree discordance test
#
# input arguments:
# refset_path path to reference dataset
# project_db path to predictions in darwin db format
# title name of the method which is evaluated
# problem problem instance of tree benchmark, i.e. clade name
# treebuilder LSTree (or BIONJ)
# out_dir directory where output is written to. must exist
# assessment_fname filename where the assessment file should be written
# community_id community id
Set(printgc=false): printlevel := 2;
SetRandSeed();
CreateDayMatrices();
MAX_NR_TREES := 3000;
if not assigned(refset_path) then
error('refset_path not assigned');
fi:
if not assigned(community_id) or not assigned(assessment_fname) or not assigned(out_dir) then
error('community_id, assessment_fname and out_dir all must be defined');
fi:
if not member(treebuilder, {'LSTree','BIONJ'}) then
error('unexpected treebuilder method: '.treebuilder);
fi;
prob_short := If(lowercase(problem)='luca', 'Luca', lowercase(problem[1..3]));
prob_short[1] := uppercase(prob_short[1]);
prob_path := refset_path.'/species_tree_samples_'.prob_short.'.nwk';
print(prob_path);
if not FileExists(prob_path) then
error(sprintf('problem sample tree file "%s" does not exist. Wrong problem parameter?', prob_path));
fi:
species_tree_samples := [seq(trim(z), z=SplitLines(ReadRawFile(prob_path)))]:
SeqDB := ReadDb(refset_path.'/ServerIndexed.db');
# set of species in trees
species_set := {seq(seq(z['Label'], z=indets(ParseNewickTree(t), Leaf)), t=species_tree_samples[1..1000])};
NR_TRIALS := length(species_tree_samples);
lprint('NR_TRIALS:', NR_TRIALS);
GetAcceptedSpeciesTopology := proc(t;(h0=0):numeric)
res := NULL;
if type(t,{string,symbol}) then res := Leaf(t,h0);
else
tL := procname(t[1],h0+1);
tR := procname(t[2],h0+1);
res := Tree(tL,h0,tR);
fi:
return(res);
end:
TreeToConsecExprSeq := proc(t)
if type(t,Leaf) then return(t['Label']);
else
return( procname(t['Left']), procname(t['Right']) );
fi:
end:
GetSeqOfOrthologs := proc( curProt, remSp )
global get_orth_calls, fail_fast;
get_orth_calls := get_orth_calls + 1;
#printf('calls: %d; -->%d, %A\n', get_orth_calls, curProt, remSp):
res := NULL;
vps := ParseLongList(SearchTag('VP',Entry(curProt)));
nextSpeciesRange := GenomeRange(remSp[1]);
idx1 := SearchOrderedArray(nextSpeciesRange[1]-1, vps)+1;
idx2 := SearchOrderedArray(nextSpeciesRange[2], vps);
cand_vps := Shuffle(vps[idx1..idx2]);
if length(cand_vps)>20 then cand_vps := cand_vps[1..20] fi:
for vp in cand_vps do if fail_fast[vp]='0' and member(vp,iSet) then
if vp>=nextSpeciesRange[1] and vp<=nextSpeciesRange[2] then
if length(remSp)=1 then res := vp;
else
res := remember(procname( vp, remSp[2..-1]));
fi:
if res<>NULL then break else fail_fast[vp] := '1'; fi:
fi:
fi od:
if res<>NULL then
res := curProt, res;
else
fail_fast[curProt] := '1';
fi:
#printf(' <--%d, %A: %A\n', curProt, remSp, If(res<>NULL, res, 'n/a')):
return( res );
end:
SelectNextFam := proc()
global fail_fast, get_orth_calls, __next_tree_idx, DB:
t0 := time();
if not assigned(__next_tree_idx) then __next_tree_idx := 1 fi:
accTree := ParseNewickTree(species_tree_samples[__next_tree_idx]);
__next_tree_idx := __next_tree_idx + 1:
specList := [TreeToConsecExprSeq(accTree)];
startRange := GenomeRange(specList[1]);
fnd := false;
curDB := DB: DB := SeqDB:
for xx to 50 do
start := Rand(startRange);
if SearchTag('MINOR_ISOFORM', Entry(start))='true' then next fi:
if member(start,iSet) then fnd := true; break fi:
od:
DB := curDB:
if not fnd then
warning('no start protein in '.specList[1].' found in iSet');
return(NULL)
fi:
t1 := time();
fail_fast := CreateString(SeqDB[TotEntries], '0'):
get_orth_calls := 0;
orths := GetSeqOfOrthologs(start, specList[2..-1] );
if orths<>NULL then res := [accTree, [orths]];
else res := NULL fi:
#print('%d calls to get_orthologs. sampling completed: %A\n', get_orth_calls, nxt<>NULL);
return( res );
end:
FindCases := proc()
Logger('sampling possible trees', 'INFO');
cand := []: sampling_times := Stat('Stats of sampling times');
tLast := tBegin := time();
for trial to NR_TRIALS do
tCur := time();
nxt := SelectNextFam();
if nxt <> NULL then cand := append(cand, nxt) fi:
sampling_times + (time() - tCur);
if time()-tLast > 30 then
Logger(sprintf('%s\n %d of %d samplings done; %d samplings are complete.\n'.
' sampling for %.1fmin (%.3fsec/sampling)\n\n',
msg, trial, NR_TRIALS, length({op(cand)}), (time()-tBegin)/60,
(time()-tBegin)/trial));
tLast := time();
fi:
od:
cand := [op({op(cand)})]:
Logger(sprintf('nr of cases in total: %d', length(cand)), 'INFO');
return(cand):
end:
GetProtsInSPtree := proc()
protSet := {seq(seq(eNr, eNr=GenomeRange(g)), g=species_set)};
return( protSet );
end:
CntOrthologs := proc(iSet)
cnt := 0;
for eNr to DB[TotEntries] do
if not member(eNr,iSet) then next fi:
vps := ParseLongList(SearchTag('VP',Entry(eNr)));
for vp in vps do if member(vp, iSet) then cnt := cnt+1 fi od:
od:
return( cnt/2 );
end:
InferDistTree := proc(D, V, labs)
if treebuilder='LSTree' then
tree := LeastSquaresTree(D,V,labs);
elif treebuilder='BIONJ' then
treeRes := BioNJ(D, labs);
tree := treeRes['Tree'];
else
error('invalid tree reconstruction method: '.treebuilder)
fi:
return(tree);
end:
ComputeCongruenceStat := proc(cases:list, title)
global DB, Ndone:
RFstat := Stat('RobinsonFoulds - '.title);
IdentStat := Stat('Fraction Incorrect Trees - '.title);
rawDat := []:
DB := SeqDB:
cErr := Counter('# errors in PhylogeneticTree');
tot_nr_trees := length(cases);
if not type(Ndone, integer) then Ndone := 0 fi:
tLast := 0; t0 := time();
for cas in cases do
seqs := labs := spec := [];
speciestree := cas[1];
for i to length(cas[2]) do
eNr := cas[2,i];
seqs := append(seqs, Sequence(Entry(eNr)));
labs := append(labs, eNr);
spec := append(spec, GenomeCodeFromENr(eNr));
od:
nseq := length(seqs):
msa := traperror(MafftMSA(seqs)):
if msa=lasterror then
Logger(sprintf('Error while computing mafft msa: %a\n', msa), 'CRITICAL');
cErr+1; next
fi:
D := CreateArray(1..nseq,1..nseq):
V := CreateArray(1..nseq,1..nseq):
for i to nseq do for j from i+1 to nseq do
dps := EstimatePam(msa[AlignedSeqs,i],msa[AlignedSeqs,j],DMS):
D[i,j] := D[j,i] := dps[2];
V[i,j] := V[j,i] := dps[3];
od od:
genetree := traperror( InferDistTree(D,V,labs) ):
if genetree=lasterror then
Logger(sprintf('Error while computing distance tree: %a\n', genetree), 'CRITICAL');
cErr+1; next
fi;
pruned_st := PruneTree(copy(speciestree), spec);
for l in Leaves(pruned_st) do
x := SearchArray(l['Label'], spec);
l['Label'] := labs[x];
od:
rfdist := RobinsonFoulds([genetree, pruned_st])[1,2];
RFstat + rfdist;
IdentStat + If(rfdist > 0, 1, 0);
rawDat := append(rawDat, [genetree, pruned_st, rfdist]);
Ndone := Ndone + 1;
if time()-tLast>30 then
t := Ndone/tot_nr_trees;
msg := sprintf( 'Finished %d / %d (%.1f%%) trees in %.1f min. '.
'Estimated remaining time: %.1fmin\n',
Ndone, tot_nr_trees, 100*t, (time()-t0)/60, (1-t)/t*(time()-t0)/60 );
Logger(msg);
tLast := time();
fi;
od;
Logger( sprintf('STDTest result for %s:\n %A\n %A',
title, RFstat, cErr), 'INFO');
return( [RFstat, rawDat, IdentStat] );
end:
StoreRawData := proc(raw_data, name, fname_)
fname := fname_;
if length(fname) > 4 and fname[-3..-1] = '.gz' then
fname := fname[1..-4];
do_gzip := true;
else do_gzip := false fi:
OpenWriting(fname);
printf('# Generalized Species Tree Discordance benchmark results\n');
printf('# Reported are infered gene tree and expected species tree (both in\n');
printf('# newick format) and the normalised Robinson-Foulds distance between them\n');
printf('# Computing timestamp: %s\n', date());
printf('# Project <TAB> gene tree <TAB> species tree <TAB> RF distance\n');
for z in raw_data do
for k to 2 do for l in Leaves(z[k]) do
oE := l['Label'];
if not type(oE, integer) then next fi:
l['Label'] := ENr2XRef(oE);
od od:
printf('%s\t%s\t%s\t%f\n', name, Tree_Newick(z[1]),
Tree_Newick(z[2]), z[3]);
od:
OpenWriting(previous);
if do_gzip then CallSystem('gzip -9f '.fname); fi:
end:
StoreResult := proc(fn:string, data)
OpenWriting(fn): prints(json(data)): OpenWriting(previous);
end:
projDB := ReadDb(project_db);
title_id := ReplaceString(' ','-', ReplaceString('_', '-', title));
challenge := 'G_STD2_'.problem:
hash_of_cur_proj := hash_sha2([problem, project_db, treebuilder]);
raw_out_fn := sprintf('%s_%s_%a_raw.txt.gz', challenge, title_id, hash_of_cur_proj);
iSet := GetProtsInSPtree();
cases := FindCases();
nr_trees := length(cases);
nr_orthologs := CntOrthologs(iSet);
# limit nr of gene tree cases to compute to max, but keeping info on how many we found
cases := If(nr_trees > MAX_NR_TREES, Shuffle(cases)[1..MAX_NR_TREES], cases):
# compute gene trees and their congruence to the species tree
compRes := ComputeCongruenceStat(cases, title):
RFstat := compRes[1]; raw_data := compRes[2]; IdentStat := compRes[3];
stderr_nr_trees := 1.96 * sqrt(nr_trees/NR_TRIALS * (1 - nr_trees/NR_TRIALS) * NR_TRIALS);
assessments := [AssessmentDataset(community_id, challenge, title, 'NR_ORTHOLOGS', nr_orthologs, 0),
AssessmentDataset(community_id, challenge, title, 'NR_COMPLETED_TREE_SAMPLINGS', nr_trees, stderr_nr_trees),
AssessmentDataset(community_id, challenge, title, 'RF_DISTANCE', RFstat['Mean'], RFstat['StdErr']),
AssessmentDataset(community_id, challenge, title, 'FRAC_INCORRECT_TREES', IdentStat['Mean'], IdentStat['StdErr'])];
result := table():
result['problem'] := problem;
result['treebuilder'] := treebuilder;
recall_nr_orth := table(): recall_sampled := table():
recall_nr_orth['name'] := 'Number of predicted ortholog pairs';
recall_nr_orth['value'] := nr_orthologs;
recall_nr_orth['stderr'] := 0;
recall_sampled['name'] := 'Number of completed tree samplings (of 50k trials)';
recall_sampled['value'] := nr_trees;
recall_sampled['stderr'] := stderr_nr_trees;
result['recall_measures'] := [recall_sampled, recall_nr_orth];
prec_rf := table(): prec_id := table():
prec_rf['name'] := 'Avg RobinsonFoulds distance';
prec_rf['value'] := RFstat['Mean'];
prec_rf['stderr'] := RFstat['StdErr'];
prec_id['name'] := 'Avg fraction of incorrect trees';
prec_id['value'] := IdentStat['Mean'];
prec_id['stderr'] := IdentStat['StdErr'];
result['precision_measures'] := [prec_rf, prec_id]:
result['raw_data_fn'] := raw_out_fn:
result['participant'] := title;
StoreRawData(raw_data, title, out_dir.'/'.result['raw_data_fn']):
#StoreResult(sprintf('%s/%s_%s.json', out_dir, challenge, title_id), result);
StoreResult(assessment_fname, assessments);
done;