forked from NCIP/pathway-interaction-database
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MolMatch.pm
executable file
·219 lines (189 loc) · 5.69 KB
/
MolMatch.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
# 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 MolMatch;
require Exporter;
@ISA = qw(Exporter);
#@EXPORT = qw(
# CreateIsA
# CreateLabelSort
#);
use strict;
use PWLabel;
use Pathway;
## pathway pa is the "standard"
## pathway pb is what is to be adapted to the standard
## name_index: lc(name)->nametype->molid
## exid_index: lc(exid)->idtype->molid
######################################################################
sub new {
my ($self, $lv, $pa, $pb) = @_;
my $x = {};
$x->{lv} = $lv;
$x->{pa} = $pa;
$x->{pb} = $pb;
MakeIndexes($x);
return bless $x;
}
######################################################################
sub MakeIndexes {
my ($self) = @_;
my $lv = $self->{lv};
my $pa = $self->{pa};
my $pb = $self->{pb};
for my $mol (@{ $pa->Mols() }) {
my $nhash = $pa->MolName($mol);
for my $nametype (keys %{ $nhash }) {
for my $name (keys %{ $$nhash{$nametype} }) {
$self->{name_index}{$nametype . ":" . lc($name)}{$mol} = 1;
}
}
my $ehash = $pa->MolExId($mol);
for my $idtype (keys %{ $ehash }) {
for my $id (keys %{ $$ehash{$idtype} }) {
$self->{exid_index}{$idtype . ":" . lc($id)}{$mol} = 1;
}
}
}
}
######################################################################
sub MatchComplexMols {
my ($self) = @_;
my $lv = $self->{lv};
my $pa = $self->{pa};
my $pb = $self->{pb};
my (%comp_lookup, %a_empties, %b_empties);
my $complex_type = $lv->StringToLabelValue("molecule-type", "complex");
## build reverse index on "standard": component mol -> complex mol
for my $mol (@{ $pa->Mols() }) {
if ($pa->MolType($mol) == $complex_type) {
if (@{ $pa->Components($mol) } == 0) {
$a_empties{$mol} = 1;
} else {
for my $comp (@{ $pa->Components($mol) }) {
my $compmol = $pa->ComponentMol($mol, $comp);
$comp_lookup{$compmol}{$mol} = 1;
}
}
}
}
## map the empties first; they need to look like simples
for my $mol (@{ $pb->Mols() }) {
if ($pb->MolType($mol) != $complex_type) {
next;
}
if (@{ $pb->Components($mol) } == 0) {
for my $a_empty (keys %a_empties) {
if (Pathway::NamesIntersect($pa, $pb, $a_empty, $mol)) {
$pb->AddMolMap($mol, $a_empty);
last; ## just take the first one
}
}
}
}
for my $mol (@{ $pb->Mols() }) {
if ($pb->MolType($mol) != $complex_type) {
next;
}
my $found = 0;
my $hit = "";
if (@{ $pb->Components($mol) } > 0) {
## just the first component; then test all candidates that
## share this component
my $comp = @{ $pb->Components($mol) }[0];
my $compmol = $pb->ComponentMol($mol, $comp);
if (defined $pb->MolMap($compmol)) {
$compmol = $pb->MolMap($compmol);
}
if (defined $comp_lookup{$compmol}) {
for my $candidate (keys %{ $comp_lookup{$compmol} }) {
$found = Pathway::EqComplex($pa, $pb, $candidate, $mol);
if ($found) {
$hit = $candidate;
last;
}
}
}
}
if ($found) {
$pb->AddMolMap($mol, $hit);
}
}
}
######################################################################
sub MatchSimpleMols {
my ($self) = @_;
my $lv = $self->{lv};
my $pa = $self->{pa};
my $pb = $self->{pb};
my $complex_type = $lv->StringToLabelValue("molecule-type", "complex");
for my $mol (@{ $pb->Mols() }) {
if ($pb->MolType($mol) == $complex_type) {
next;
}
my $found = 0;
my $hit = "";
my $ehash = $pb->MolExId($mol);
for my $idtype (keys %{ $ehash }) {
for my $id (keys %{ $$ehash{$idtype} }) {
if (defined $self->{exid_index}{$idtype . ":" . lc($id)}) {
for my $candidate_mol (
keys %{ $self->{exid_index}{$idtype . ":" . lc($id)} }) {
if (Pathway::EqSimplex($pa, $pb, $candidate_mol, $mol)) {
if (! $found) {
$hit = $candidate_mol;
$found++;
} elsif ($hit ne $candidate_mol) {
#print STDERR "MatchSimpleMols: $mol ex hit twice\n";
$found++;
}
}
}
}
}
}
if (! $found) {
my $nhash = $pb->MolName($mol);
for my $nametype (keys %{ $nhash }) {
for my $name (keys %{ $$nhash{$nametype} }) {
if (defined $self->{name_index}{$nametype . ":" . lc($name)}) {
for my $candidate_mol (
keys %{ $self->{name_index}{$nametype . ":" . lc($name)} }) {
if (Pathway::EqSimplex($pa, $pb, $candidate_mol, $mol)) {
if (! $found) {
$hit = $candidate_mol;
$found++;
} elsif ($hit ne $candidate_mol) {
$found++;
#print STDERR "MatchSimpleMols: $mol name hit twice\n";
}
}
}
}
}
}
}
if ($found == 1) {
$pb->AddMolMap($mol, $hit);
}
}
}
######################################################################
sub MatchMols {
my ($self) = @_;
my $lv = $self->{lv};
my $pa = $self->{pa};
my $pb = $self->{pb};
## create a {map} in $pb (which may be the same as $pa)
$pa->FlattenComplexes();
$pb->FlattenComplexes();
$self->MatchSimpleMols();
$self->MatchComplexMols();
}
######################################################################
sub r_numerically { $b <=> $a };
######################################################################
sub numerically { $a <=> $b };
######################################################################
1;