forked from NCIP/pathway-interaction-database
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestDrive.pl
executable file
·397 lines (350 loc) · 10.1 KB
/
TestDrive.pl
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/local/bin/perl
# Copyright SRA International
#
# Distributed under the OSI-approved BSD 3-Clause License.
# See http://ncip.github.com/pathway-interaction-database/LICENSE.txt for details.
BEGIN {
my @path_elems = split("/", $0);
pop @path_elems;
push @INC, join("/", @path_elems);
}
if (-d "/app/oracle/product/dbhome/current") {
$ENV{'ORACLE_HOME'} = "/app/oracle/product/dbhome/current";
} elsif (-d "/app/oracle/product/8.1.7") {
$ENV{'ORACLE_HOME'} = "/app/oracle/product/8.1.7";
} elsif (-d "/app/oracle/product/8.1.6") {
$ENV{'ORACLE_HOME'} = "/app/oracle/product/8.1.6";
}
use strict;
use Pathway;
use PWLabel;
use FileHandle;
use IdMap;
my (
$db_inst,
$db_user,
$db_pass,
$schema,
$input_type,
$input_file,
$output_type,
$output_file,
$ontology_file,
$id_file,
$moldefs_file,
$standalone,
$atomids,
$subtypelines
);
my %output_types = (
"sql" => 1,
"table" => 1,
"xml" => 1,
"dot" => 1,
"biopax" => 1,
"html" => 1
);
my %input_types = (
"js" => 1,
"xml" => 1,
"biopax" => 1
);
my ($lv, $pw, $idm);
##
## Set a timer
##
my $query_timeout = 500; ## max length (in seconds) for a request
## to run before exiting/resetting server
# $SIG{ALRM} = \&CatchAlarm;
alarm $query_timeout; ## Set timer
##
##
##
ReadOptions();
print STDOUT "After ReadOptions\n";
##
## Read (if was given) a file that tells where to start numbering
## new entities (pathways, atoms, molecules, ptms)
##
$idm = new IdMap($id_file);
print STDOUT "After IdMap\n";
##
## Read the ontology from an xml input or from the database
##
if ($input_type eq "xml") {
$lv = new PWLabel(undef, undef);
} elsif (defined $ontology_file ||
(defined $db_user && defined $db_pass && defined $db_inst &&
defined $schema)) {
$lv = ReadOntology($ontology_file, $db_user, $db_pass, $db_inst, $schema);
} else {
die "no ontology input specified";
}
##
## Read existing mol defs from an xml input or from the database
##
my $moldefs = new Pathway($lv);
if ($moldefs_file) {
ReadMolDefs($lv, $moldefs_file, $moldefs);
} elsif (defined $db_user && defined $db_pass && defined $db_inst &&
defined $schema) {
use PathwayDB;
use DBI;
my $db = DBI->connect("DBI:Oracle:" . $db_inst, $db_user, $db_pass);
if (not $db or $db->err()) {
print STDERR "Cannot connect to " . $db_user . "@" . $db_inst . "\n";
exit;
}
my $pdb = new PathwayDB($db, $schema, "", "", $moldefs, $lv);
$pdb->GetAllMolecules();
$db->disconnect();
}
##
## Read the input to be translated/processed
##
print STDOUT "Before ReadInput\n";
$pw = ReadInput($input_type, $input_file, $lv);
print STDOUT "After ReadInput\n";
exit(0);
##
## And produce the translation/analysis
##
print STDOUT "Before WriteOutput\n";
WriteOutput($output_type, $output_file, $lv, $pw);
######################################################################
sub ReadMolDefs {
my ($lv, $f, $moldefs) = @_;
use ParsePathwayXML;
my $fh = new FileHandle;
open($fh, $f) or die "cannot open $f";
my $parser = new ParsePathwayXML($lv, $moldefs);
$parser->parse($fh);
close $fh;
$moldefs->MergeMolecules();
}
######################################################################
sub ReadInput {
my ($input_type, $f, $lv) = @_;
my $pw;
if ($input_type eq "xml") {
$pw = ReadPathwayXML($lv, $f);
$pw->MergeMolecules();
} elsif ($input_type eq "js") {
use ParseJS;
use MolMatch;
$pw = new Pathway($lv);
my $js = new ParseJS($lv, $pw, $idm);
print STDOUT "After new ParseJS\n";
$js->ParseJS($f);
print STDOUT "After ParseJS\n";
$pw->MergeMolecules();
print STDOUT "After MergeMolecules\n";
my $mm = new MolMatch($lv, $moldefs, $pw);
$mm->MatchMols();
$pw->RemapMols();
} elsif ($input_type eq "biopax") {
}
$pw->BuildMolInstCache();
$pw->BuildGenericLabelCache();
if (0) {
$pw->PruneDuplicateAtoms();
}
$pw->IdentifyMacroProcesses();
$pw->ValidateSubnets();
$pw->CollapseSubnets();
return $pw;
}
######################################################################
sub WriteOutput {
my ($output_type, $output_file, $lv, $pw) = @_;
if ($output_type eq "html") {
use HTMLOutput;
open(OUTF, ">$output_file") or die "cannot open $output_file";
my $xml = new HTMLOutput($pw, $lv, *OUTF);
$xml->PrHTML();
close OUTF;
} elsif ($output_type eq "xml") {
use XMLOutput;
open(OUTF, ">$output_file") or die "cannot open $output_file";
my $xml = new XMLOutput($pw, $lv, *OUTF);
$xml->PrXML();
close OUTF;
} elsif ($output_type eq "biopax") {
use BioPAXOutput;
open(OUTF, ">$output_file") or die "cannot open $output_file";
my $xml = new BioPAXOutput($pw, $lv, *OUTF);
$xml->PrOWL();
close OUTF;
} elsif ($output_type eq "table") {
use SQLOutput;
open(OUTF, ">$output_file") or die "cannot open $output_file";
my $sql = new SQLOutput($pw, $lv, *OUTF, "table");
$sql->SetPTMExprId($idm->PTMExprId());
my %list;
for my $molid (@{ $pw->Mols() }) {
if ($idm->MolId() > 0 && $molid < $idm->MolId()) {
$list{$molid} = 1;
}
}
$sql->SetExistingDefs(\%list);
$sql->PrSQL();
close OUTF;
} elsif ($output_type eq "sql") {
use SQLOutput;
open(OUTF, ">$output_file") or die "cannot open $output_file";
my $sql = new SQLOutput($pw, $lv, *OUTF, "sql");
$sql->PrSQL();
close OUTF;
} elsif ($output_type eq "dot") {
use Cache;
use Clan;
use DOToutput;
my @lines;
my ($size, $clan, @clans, %sizeof, @cache_ids);
if ($standalone) {
## Why? There are some pathways that have non-overlapping
## graphs showing processes involving different members of
## a molecule-family. We could try to box them (or some such
## graphic trick), but that might force a closer relation than
## we want; for example, all forms (ptm) and locations of each
## family member would go in the same box.
$pw->ForceIntoOneClan();
} else {
$pw->BuildClanList();
}
for $clan (@{ $pw->Clans }) {
$size = $clan->ClanSize();
push @{ $sizeof{$size} }, $clan;
}
for $size (sort r_numerically keys %sizeof) {
for $clan (@{ $sizeof{$size} }) {
push @clans, $clan;
}
}
my $n = 0;
for $clan (@clans) {
$n++;
open(OUTF, ">$output_file.$n") or die "cannot open $output_file.$n";
my $dot = new DOToutput($pw, $lv, *OUTF);
if ($atomids) {
$dot->ShowAtomIds(1);
}
if ($standalone) {
$dot->SetStandalone(1);
$dot->ShowAtomIds(1);
$dot->ShowSubTypeLines(1);
my $html_fn = $output_file;
$html_fn =~ s/.*\///;
$html_fn =~ s/\.dot$//;
$html_fn .= ".html";
$dot->SetHTMLFileName($html_fn);
}
if ($subtypelines) {
$dot->ShowSubTypeLines(1);
}
$dot->DOTGraph($clan);
close(OUTF);
}
}
}
######################################################################
sub r_numerically { $b <=> $a };
######################################################################
sub ReadOntology {
my ($ontology_f, $db_user, $db_pass, $db_inst, $schema) = @_;
my $lv;
if ($ontology_f) {
use ParsePathwayXML;
my $fh = new FileHandle;
$lv = new PWLabel(undef, undef);
open($fh, $ontology_f) or die "cannot open $ontology_f";
my $pw0 = new Pathway($lv);
my $parser = new ParsePathwayXML($lv, $pw0);
$parser->parse($fh);
close $fh;
} else {
my $db = DBI->connect("DBI:Oracle:" . $db_inst, $db_user, $db_pass);
if (not $db or $db->err()) {
print STDERR "Cannot connect to " . $db_user . "@" . $db_pass . "\n";
exit;
}
$lv = new PWLabel($db, $schema);
$db->disconnect();
}
return $lv;
}
######################################################################
sub ReadPathwayXML {
my ($lv, $xml_f) = @_;
use ParsePathwayXML;
my $fh = new FileHandle;
open($fh, $xml_f) or die "cannot open $xml_f";
my $pw = new Pathway($lv);
my $parser = new ParsePathwayXML($lv, $pw);
$parser->parse($fh);
close $fh;
return $pw;
}
######################################################################
sub ReadOptions {
use Getopt::Long;
if (@ARGV == 0) {
print join("\n\t",
"options:",
"it: input type (xml, js, biopax)",
"ot: output type (xml, biopax, dot, table, sql)",
"if: input file",
"of: output file",
"ontf: ontology file (if not providing db params)",
"idf: file with starting ids (pathway, atom, mol, ptm) and source ids",
"moldef: xml pathway file with existing molecule defs " .
"(into which new stuff is to be mapped)",
"db_user",
"db_pass",
"db_inst",
"schema",
"standalone: standalone dot (local urls)",
"atomids: show atomids in dot",
"subtypelines: show subtypelines in dot"
) . "\n";
exit;
}
GetOptions (
"it:s" => \$input_type,
"ot:s" => \$output_type,
"if:s" => \$input_file,
"of:s" => \$output_file,
"ontf:s" => \$ontology_file,
"idf:s" => \$id_file,
"moldef:s" => \$moldefs_file,
"db_inst:s" => \$db_inst,
"db_user:s" => \$db_user,
"db_pass:s" => \$db_pass,
"schema:s" => \$schema,
"standalone" => \$standalone,
"atomids" => \$atomids,
"subtypelines" => \$subtypelines,
) or die "exiting";
if (! defined $input_types{$input_type}) {
die "input type must be one of " .
join(", ", keys %input_types) . "\n";
}
if (! defined $output_types{$output_type}) {
die "output type must be one of " .
join(", ", keys %output_types) . "\n";
}
if ($input_file eq "") {
die "specify input file";
}
if ($output_file eq "") {
die "specify output file";
}
if ($input_file eq $output_file) {
die "input and output file names are identical";
}
if ($input_type ne "xml" && $ontology_file eq "" && $db_user eq "") {
die "input type must be xml or else must specify ontology file " .
"or database parameters";
}
}
######################################################################