This repository has been archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Parser.pm
executable file
·825 lines (714 loc) · 22.4 KB
/
Parser.pm
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# Copyright SRA International
#
# Distributed under the OSI-approved BSD 3-Clause License.
# See http://ncip.github.com/pathway-interaction-database/LICENSE.txt for details.
package Parser;
require Exporter;
@ISA = qw(Exporter);
#@EXPORT = qw(
# CreateIsA
# CreateLabelSort
#);
use strict;
use PWLabel;
use Pathway;
use constant EOF => "%&%&%&%&";
my %opclass = (
"complex-def" => "MOLECULE_DEF", ## redundant, covered by "molecule-def"
"molecule-def" => "MOLECULE_DEF",
"pathway" => "PATHWAY",
"pathway-info" => "PATHWAY_INFO",
"protein" => "MOLECULE",
"rna" => "MOLECULE",
"compound" => "MOLECULE",
"complex" => "MOLECULE",
"reaction" => "ATOM",
"modification" => "ATOM",
"binding" => "ATOM",
"transcription" => "ATOM",
"translocation" => "ATOM",
# "cell-process" => "ATOM",
# "ubiquitination" => "ATOM",
# "replicative-senescence" => "ATOM",
# "dna-fragmentation" => "ATOM",
# "dna-damage" => "ATOM",
# "dna-repair" => "ATOM",
# "cell-survival" => "ATOM",
# "cell-proliferation" => "ATOM",
# "apoptosis" => "ATOM",
"macroprocess" => "ATOM",
"cell-process" => "ATOM", ## for now, synonym for macroprocess
"agent" => "EDGE",
"input" => "EDGE",
"output" => "EDGE",
"inhibitor" => "EDGE",
"component" => "COMPONENT",
"location" => "LABEL_VALUE",
"activity-state" => "LABEL_VALUE",
"edge-type" => "LABEL_VALUE",
"reversible" => "LABEL_VALUE",
"process-condition" => "LABEL_VALUE",
"process-type" => "LABEL_VALUE", ## to specify kind of macroprocess
"function" => "LABEL_VALUE", ## label on inputs/agents
"EC" => "NONUNIQUE_VALUE",
"CA" => "NONUNIQUE_VALUE",
"LL" => "NONUNIQUE_VALUE",
"GO" => "NONUNIQUE_VALUE",
"KG" => "NONUNIQUE_VALUE",
"AS" => "NONUNIQUE_VALUE",
"atom-id" => "UNIQUE_VALUE",
"pathway-id" => "UNIQUE_VALUE",
"component-seq-id" => "UNIQUE_VALUE",
"edge-seq-id" => "UNIQUE_VALUE",
"molecule-id" => "UNIQUE_VALUE",
"name" => "UNIQUE_VALUE",
"organism" => "UNIQUE_VALUE",
"source-id" => "UNIQUE_VALUE",
"source-pathway-id" => "UNIQUE_VALUE"
);
my @EXT_MOL_ID = (
"EC",
"LL",
"KG",
"CA",
"GO"
);
my @MOL_NAME = (
"AS"
);
my @LABEL = (
"function",
"edge-type",
"reversible",
"location",
"activity-state",
"process-type",
"process-condition"
);
my %label_name_of_op = (
"function" => "function",
"edge-type" => "edge-type",
"reversible" => "reversible",
"location" => "location",
"activity-state" => "activity-state",
"process-type" => "process-type",
"process-condition" => "process-type"
);
######################################################################
sub new {
my ($self, $pw, $lv, $atomidbase, $pathwayidbase) = @_;
my $x = {};
$x->{pw} = $pw;
$x->{lv} = $lv;
$x->{ROOT} = 0;
$x->{tokeni} = -1;
$x->{atomidbase} = $atomidbase;
$x->{pathwayidbase} = $pathwayidbase;
SetProcessTypes($lv);
return bless $x;
}
######################################################################
sub AtomIdMap {
my ($self) = @_;
return $self->{atomidmap};
}
######################################################################
sub PathwayIdMap {
my ($self) = @_;
return $self->{pathwayidmap};
}
######################################################################
sub SetProcessTypes {
my ($lv) = @_;
my ($pair, $label_name, $label_value_name);
## my $cell_process_lvid =
## $lv->StringToLabelValue("process-type", "cell-process");
my $macroprocess_lvid =
$lv->StringToLabelValue("process-type", "macroprocess");
for $pair (keys %{ $lv->AllNamePairs }) {
($label_name, $label_value_name) = split(/\t/, $pair);
if ($label_name eq "process-type" &&
$lv->IsA($lv->StringToLabelValue($label_name, $label_value_name),
$macroprocess_lvid) ) {
## $cell_process_lvid) ) {
if (! defined $opclass{$label_value_name}) {
$opclass{$label_value_name} = "ATOM";
}
}
}
}
######################################################################
sub ListErrors {
my ($self) = @_;
return $self->{errors}
}
######################################################################
sub OpClass {
my ($self, $op) = @_;
if (defined $opclass{$op}) {
return $opclass{$op};
} elsif ($op =~ /^\$\d+$/) {
return "LITERAL";
} elsif ($op eq "(" || $op eq ")") {
return $op;
} else {
push @{ $self->{errors} }, "#! illegal op: $op, on line " . $self->CurLine() . "\n";
}
}
######################################################################
sub NextToken {
my ($self) = @_;
$self->{tokeni}++;
if (! defined $self->{tokens}) {
push @{ $self->{errors} }, "#! empty token list\n";
return EOF;
}
if ($self->{tokeni} >= @{ $self->{tokens} }) {
return EOF;
} else {
return $self->{tokens}[$self->{tokeni}];
}
}
######################################################################
sub CurToken {
my ($self) = @_;
if (! defined $self->{tokens}) {
push @{ $self->{errors} }, "#! empty token list\n";
return EOF;
}
if ($self->{tokeni} >= @{ $self->{tokens} }) {
return EOF;
} else {
return $self->{tokens}[$self->{tokeni}];
}
}
######################################################################
sub CurLine {
my ($self) = @_;
if (! defined $self->{tokens}) {
push @{ $self->{errors} }, "#! empty token list\n";
return "end of file";
}
if ($self->{tokeni} >= @{ $self->{tokens} }) {
return "end of file";
} else {
return $self->{lineno}[$self->{tokeni}];
}
}
######################################################################
sub TokIsStringLit {
my ($tok) = @_;
return $tok =~ /^\$/;
}
######################################################################
sub FetchStringLit {
my ($self, $stridx) = @_;
$stridx =~ s/^\$//;
return $self->{str_array}[$stridx];
}
######################################################################
sub PushToken {
my ($self, $token) = @_;
my $level = $self->{level};
my $op = $self->OpClass($token);
if ($op eq "") {
push @{ $self->{errors} }, "#! unrecognized opclass $op at " . $self->CurLine . "\n"
}
$self->{opstack}[$level] = $op;
$self->{tokstack}[$level] = $token;
$self->{stacklineno}[$level] = $self->{lineno}[$self->{tokeni}];
if ($token eq "pathway") {
$self->{ROOT} = $level;
}
}
######################################################################
sub PushLevel {
my ($self, $token) = @_;
$self->{level}++;
$self->PushToken($token);
}
######################################################################
sub CheckLVID {
my ($self, $lvid, $label, $value) = @_;
if ($lvid == 0) {
push @{ $self->{errors} }, "#! can't find label value id for $value of type $label in parse unit " .
"ending at line " . $self->CurLine() . "\n";
}
}
######################################################################
sub CheckStack {
my ($self, @acceptable_ops) = @_;
for my $op (@acceptable_ops) {
if ($self->{opstack}[$self->{level}-1] eq $op) {
return 1;
}
}
push @{ $self->{errors} }, "#! ill-formed parse stack: " .
"parent = " . $self->{opstack}[$self->{level}-1] . ", " .
"child = " . $self->{opstack}[$self->{level}] . " " .
"for parse unit ending at line " . $self->CurLine() . "\n";
return 0;
}
######################################################################
sub PopLevel {
my ($self) = @_;
my $pw = $self->{pw};
my $lv = $self->{lv};
my $level = $self->{level};
my $local = \% { $self->{data}{$level} };
my $parent = \% { $self->{data}{$level-1} };
my $root = \% { $self->{data}{$self->{ROOT}} };
if ($level <= 0) {
push @{ $self->{errors} }, "#! Attempt to pop empty stack\n";
return;
}
my ($molid, $op, $atomid, $edgeid, $pathwayid, $lvid);
my ($edgetype);
my ($moltype, $value);
my ($compseqid, $compmolid);
my $curop = $self->{opstack}[$level];
my $token = $self->{tokstack}[$level];
my $parentop = $self->{opstack}[$level-1];
my $org = $self->{data}{$self->{ROOT}}{"organism"};
if ($curop eq "LABEL_VALUE") {
$$parent{label}{$token} = $$local{value};
} elsif ($curop eq "UNIQUE_VALUE") {
if ($token eq "molecule-id") {
$$local{value} = $self->MappedMolId($$local{value});
} elsif ($token eq "atom-id") {
my $original_atom_id = $$local{value};
$$local{value} = $self->MappedAtomId($$local{value});
$self->{atomidmap}{$original_atom_id} = $$local{value};
} elsif ($token eq "pathway-id") {
my $original_pathway_id = $$local{value};
$$local{value} = $self->MappedPathwayId($$local{value});
$self->{pathwayidmap}{$original_pathway_id} = $$local{value};
}
$$parent{unique_value}{$token} = $$local{value};
} elsif ($curop eq "NONUNIQUE_VALUE") {
push @{ $$parent{nonunique_value}{$token} }, $$local{value};
} elsif ($curop eq "EDGE") {
if (! $self->CheckStack("ATOM")) {
return 0;
}
$edgeid = $$local{unique_value}{"edge-seq-id"};
$molid = $$local{unique_value}{"molecule-id"};
push @{ $$parent{edge} }, "$edgeid\t$molid";
$$parent{edge_type}{$edgeid} = $token;
for $op (keys %{ $$local{label} }) {
$value = $$local{label}{$op};
$$parent{edge_label}{$edgeid}{$op} = $value;
}
for $op (keys %{ $$local{mol_label} }) {
$value = $$local{mol_label}{$op};
$$parent{mol_label}{$edgeid}{$op} = $value;
}
} elsif ($curop eq "ATOM") {
if (! $self->CheckStack("PATHWAY") ) {
return 0;
}
$atomid = $$local{unique_value}{"atom-id"};
for (@{ $$local{edge} }) {
my ($edgeid, $molid) = split /\t/;
$edgetype = $$local{edge_type}{$edgeid};
$lvid = $lv->StringToLabelValue("edge-type", $edgetype);
$self->CheckLVID($lvid, $op, $edgetype);
$pw->AddEdge($atomid, $edgeid, $lvid, $molid);
$pw->AddEdgeLabel($atomid, $edgeid, $lvid);
}
for $edgeid (keys %{ $$local{mol_label} }) {
for $op (keys %{ $$local{mol_label}{$edgeid} }) {
$value = $$local{mol_label}{$edgeid}{$op};
$lvid = $lv->StringToLabelValue($label_name_of_op{$op}, $value);
$self->CheckLVID($lvid, $op, $value);
$pw->AddMolLabel($atomid, $edgeid, $lvid);
}
}
for $edgeid (keys %{ $$local{edge_label} }) {
for $op (keys %{ $$local{edge_label}{$edgeid} }) {
$value = $$local{edge_label}{$edgeid}{$op};
$lvid = $lv->StringToLabelValue($label_name_of_op{$op}, $value);
$self->CheckLVID($lvid, $op, $value);
$pw->AddEdgeLabel($atomid, $edgeid, $lvid);
}
}
for $op (@LABEL) {
if (defined $$local{label}{$op}) {
$value = $$local{label}{$op};
$lvid = $lv->StringToLabelValue($label_name_of_op{$op}, $value);
$self->CheckLVID($lvid, $op, $value);
if ($op eq "process-type") {
if ($token ne "cell-process" && $token ne "macroprocess") {
push @{ $self->{errors} }, "#! mis-use of \"process-type\" label " .
"in parse unit ending at line " . $self->CurLine() . "\n";
} else {
$$local{label}{$op} = $lvid;
}
} elsif ($op eq "process-condition") {
$pw->AddAtomCondition($atomid, $lvid);
} else {
$pw->AddAtomLabel($atomid, $lvid);
}
}
}
push @{ $$parent{atom} }, $atomid;
##
## Don't forget the process-type
##
if (defined $$local{label}{"process-type"}) {
$lvid = $$local{label}{"process-type"};
} else {
$lvid = $lv->StringToLabelValue("process-type", $token);
$self->CheckLVID($lvid, $op, $token);
}
$pw->AddAtom($atomid, $lvid);
} elsif ($curop eq "COMPONENT") {
if (! $self->CheckStack("MOLECULE") ) {
return 0;
}
$compseqid = $$local{unique_value}{"component-seq-id"};
$compmolid = $$local{unique_value}{"molecule-id"};
$$parent{component}{$compseqid} = $compmolid;
for $op (keys %{ $$local{mol_label} }) {
$$parent{submol_label}{$compseqid}{$op} = $$local{mol_label}{$op}
}
for $op (keys %{ $$local{label} }) {
$$parent{comp_label}{$compseqid}{$op} = $$local{label}{$op}
}
} elsif ($curop eq "MOLECULE") {
if (! $self->CheckStack("COMPONENT", "EDGE", "MOLECULE_DEF") ) {
return 0;
}
$molid = $$local{unique_value}{"molecule-id"};
$moltype = $lv->StringToLabelValue("molecule-type", $token);
$pw->AddMol($molid, $moltype, $org);
$$parent{unique_value}{"molecule-id"} = $molid;
for $compseqid (keys %{ $$local{component} }) {
$compmolid = $$local{component}{$compseqid};
$pw->AddComponent($molid, $compseqid, $compmolid);
}
for $compseqid (keys %{ $$local{comp_label} }) {
for $op (keys %{ $$local{comp_label}{$compseqid} }) {
$value = $$local{comp_label}{$compseqid}{$op};
$lvid = $lv->StringToLabelValue($op, $value);
$self->CheckLVID($lvid, $op, $value);
$pw->AddComponentLabel($molid, $compseqid, $lvid);
}
}
for $compseqid (keys %{ $$local{submol_label} }) {
for $op (keys %{ $$local{submol_label}{$compseqid} }) {
$value = $$local{submol_label}{$compseqid}{$op};
$lvid = $lv->StringToLabelValue($op, $value);
$self->CheckLVID($lvid, $op, $value);
$pw->AddComponentLabel($molid, $compseqid, $lvid);
}
}
for $op (@LABEL) {
if (defined $$local{label}{$op}) {
$$parent{mol_label}{$op} = $$local{label}{$op};
}
}
for $op (@EXT_MOL_ID) {
if (defined $$local{nonunique_value}{$op}) {
for (@{ $$local{nonunique_value}{$op} }) {
$pw->AddMolExId($molid, $op, $_);
}
}
}
for my $op (@MOL_NAME) {
if (defined $$local{nonunique_value}{$op}) {
for (@{ $$local{nonunique_value}{$op} }) {
$pw->AddMolName($molid, $op, $_);
}
}
}
} elsif ($curop eq "MOLECULE_DEF") {
} elsif ($curop eq "PATHWAY") {
$pathwayid = $$local{unique_value}{"pathway-id"};
##
## If there was a PATHWAY_COMPONENT, this was already done
##
$pw->AddPathwayId($pathwayid);
$pw->SetPathwayName($pathwayid, $$local{unique_value}{"name"});
$pw->SetPathwayExId($pathwayid,
$$local{unique_value}{"source-pathway-id"});
$pw->SetPathwayOrg($pathwayid, $$local{unique_value}{"organism"});
## $pw->SetPathwaySrcId($pathwayid, "?");
for (@{ $$local{atom} }) {
$pw->AddAtomPathway($_, $pathwayid);
}
} elsif ($curop eq "PATHWAY_INFO") {
if (! $self->CheckStack("PATHWAY") ) {
return 0;
}
$pathwayid = $$local{unique_value}{"pathway-id"};
$$parent{unique_value}{"pathway-id"} = $pathwayid;
$$parent{unique_value}{"name"} = $$local{unique_value}{"name"};
$$parent{unique_value}{"source-pathway-id"} =
$$local{unique_value}{"source-pathway-id"};
$$parent{unique_value}{"organism"} = $$local{unique_value}{"organism"};
$pw->AddPathwayId($pathwayid);
$pw->SetPathwayName($pathwayid, $$local{unique_value}{"name"});
$pw->SetPathwayExId($pathwayid,
$$local{unique_value}{"source-pathway-id"});
$pw->SetPathwayOrg($pathwayid, $$local{unique_value}{"organism"});
## $pw->SetPathwaySrcId($pathwayid, "?");
}
undef %{ $local };
$self->{level}--;
return 1;
}
######################################################################
sub DoStr {
my ($self, $line) = @_;
my ($a, $b, $c);
while ($line =~ /(.*)(\")([^"]*)(")(.*)/) {
($a,$b,$c) = ($1, $3, $5);
$b =~ s/^\s+//;
$b =~ s/\s+$//;
if (($b ne "") && (not defined $self->{str}{$b})) {
$self->{next_str}++;
$self->{str}{$b} = $self->{next_str};
$self->{str_array}[$self->{next_str}] = $b;
}
$line = $a . " " . "\$" . $self->{str}{$b} . " " . $c;
}
return $line;
}
######################################################################
sub Parse {
my ($self) = @_;
my $token = $self->NextToken();
my $opclass;
while ($token ne EOF) {
$opclass = $self->OpClass($token);
if ($opclass eq "LITERAL") {
$self->{data}{$self->{level}}{value} = $self->FetchStringLit($token);
} elsif ($opclass eq "(") {
$token = $self->NextToken();
$opclass = $self->OpClass($token);
$self->PushLevel($token);
} elsif ($opclass eq ")") {
if (! $self->PopLevel()) {
return 0;
}
}
$token = $self->NextToken();
}
if ($self->{level} != 0) {
push @{ $self->{errors} }, "#! Mismatched parentheses; ending level = " .
$self->{level} . "\n"
}
if (defined $self->{errors} && @{ $self->{errors} } > 0) {
return 0;
} else {
return 1;
}
}
######################################################################
sub CheckTokenStream {
my ($self) = @_;
my ($i, $prev, $succ, $tok, $err);
my ($strprev, $strsucc, $strtok);
my $lineno = $self->{lineno};
if (! defined $self->{tokens}) {
push @{ $self->{errors} }, "#! empty token list\n";
return 0;
}
for ($i = 1; $i < @{ $self->{tokens} } - 1; $i++) {
$prev = $self->{tokens}[$i-1];
$tok = $self->{tokens}[$i];
$succ = $self->{tokens}[$i+1];
if (TokIsStringLit($tok)) {
$strtok = $self->FetchStringLit($tok);
##
## following condition to allow names with spaces to act as
## ATOM operators
##
if ($prev eq "(" && $self->OpClass($strtok) eq "ATOM") {
$tok = $strtok;
$self->{tokens}[$i] = $strtok;
}
} else {
$strtok = $tok;
}
if (TokIsStringLit($prev)) {
$strprev = $self->FetchStringLit($prev);
} else {
$strprev = $prev;
}
if (TokIsStringLit($succ)) {
$strsucc = $self->FetchStringLit($succ);
} else {
$strsucc = $succ;
}
##
## ' ) X ) ', where X is not '(' or ')'
##
if ($prev eq ")" && $succ eq ")" && $tok ne ")") {
push @{ $self->{errors} }, "#! syntax error ')' at line $$lineno[$i]\n";
$err++;
next;
}
##
## ' X Y Z ', where X, Y, Z are not '(' or ')'
##
if ($prev ne "(" && $prev ne ")" &&
$succ ne "(" && $succ ne ")" &&
$tok ne "(" && $tok ne ")" ) {
push @{ $self->{errors} }, "#! syntax error '$strsucc' at line $$lineno[$i+1]\n";
$err++;
next;
}
##
## ' ( X ) '
##
if ($prev eq "(" && $succ eq ")") {
push @{ $self->{errors} }, "#! syntax error '$strsucc' at line $$lineno[$i+1]\n";
$err++;
next;
}
##
## ' ) X ( ', where X is not ')'
##
if ($prev eq ")" && $succ eq "(" && $tok ne ")") {
push @{ $self->{errors} }, "#! syntax error '$strtok' at line $$lineno[$i]\n";
$err++;
next;
}
##
## ' ( X ', where X is a string literal
##
if ($prev eq "(" && TokIsStringLit($tok)) {
push @{ $self->{errors} }, "#! syntax error '$strtok' at line $$lineno[$i]\n";
$err++;
next;
}
##
## ' X ) ', where X is not a string literal and not ')'
##
if ($succ eq ")" && !TokIsStringLit($tok) && $tok ne ")") {
push @{ $self->{errors} }, "#! syntax error '$strtok' at line $$lineno[$i]\n";
$err++;
next;
}
}
return $err;
}
######################################################################
sub ReadFile {
my ($self, $fh) = @_;
# open(INF, $f) or die "Cannot open input file $f";
my ($line, $lineno, $token);
while (<$fh>) {
$lineno++;
chop;
$line = $_;
$line =~ s/;;.*//;
$line = $self->DoStr($line);
$line =~ s/\(/ ( /g;
$line =~ s/\)/ ) /g;
$line =~ s/\s+/ /g;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
if ($line =~ /^\s+$/) {
next;
}
for $token (split(/ /, $line)) {
push @{ $self->{tokens} }, $token;
push @{ $self->{lineno} }, $lineno;
}
}
# close INF;
close $fh;
if ($self->CheckTokenStream()) {
push @{ $self->{errors} }, "#! quitting because of syntax errors\n";
return 0;
}
return 1;
}
######################################################################
sub ReadData {
my ($self, $data) = @_;
my ($line, $lineno, $token);
for (split("\n", $data)) {
$lineno++;
$line = $_;
$line =~ s/;;.*//;
$line = $self->DoStr($line);
$line =~ s/\(/ ( /g;
$line =~ s/\)/ ) /g;
$line =~ s/\s+/ /g;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
if ($line =~ /^\s+$/) {
next;
}
for $token (split(/ /, $line)) {
push @{ $self->{tokens} }, $token;
push @{ $self->{lineno} }, $lineno;
}
}
close INF;
if ($self->CheckTokenStream()) {
push @{ $self->{errors} }, "#! quitting because of syntax errors\n";
return 0;
}
return 1;
}
######################################################################
sub MappedMolId {
my ($self, $molid) = @_;
if (defined $self->{molmap}) {
if (defined $self->{molmap}{$molid}) {
return $self->{molmap}{$molid};
} else {
push @{ $self->{errors} }, "#! no map for molecule-id $molid\n";
return 0;
}
} else {
return $molid;
}
}
######################################################################
sub MappedAtomId {
my ($self, $atomid) = @_;
if (defined $self->{atomidbase}) {
my $x = $self->{atomidbase};
$self->{atomidbase}++;
return $x;
# return $atomid + $self->{atomidbase};
} else {
return $atomid;
}
}
######################################################################
sub MappedPathwayId {
my ($self, $pathwayid) = @_;
## if (defined $self->{pathwayidbase}) {
## return $pathwayid + $self->{pathwayidbase};
## now it's a simple set pathway id, if non zero/empty string
if ($self->{pathwayidbase}) {
return $self->{pathwayidbase};
} else {
return $pathwayid;
}
}
######################################################################
sub ReadMolMap {
my ($self, $f) = @_;
if ($f) {
my ($local_mol_id, $global_mol_id);
open(MAPF, $f) or die "Cannot open molecule map file $f";
$self->{molmap} = {};
while (<MAPF>) {
chop;
($local_mol_id, $global_mol_id) = split /\t/;
$self->{molmap}{$local_mol_id} = $global_mol_id;
}
close MAPF;
}
}
######################################################################
1;
######################################################################