Pattern.pm

filter: Statementstmt 8.9% 21 / 234 Branchbran 0.0% 0 / 148 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 28.5% 6 / 21 Podpod 57.1% 8 / 14 totaltotal 7.6% 35 / 457 CC 88 SCAR 70.3
CC
88
Cov
5%
match_local_next
2450.0
match
90.0
match_next
56.0
CRAP 1130.3
↑ Summary Pattern/Bond.pm »
1 package Chemistry::Pattern;
2 1 $VERSION = '0.27';
3 # $Id: Pattern.pm,v 1.21 2009/05/10 20:56:06 itubert Exp $
4  
5 =head1 NAME
6  
7 Chemistry::Pattern - Chemical substructure pattern matching
8  
9 =head1 SYNOPSIS
10  
11 use Chemistry::Pattern;
12 use Chemistry::Mol;
13 use Chemistry::File::SMILES;
14  
15 # Create a pattern and a molecule from SMILES strings
16 my $mol_str = "C1CCCC1C(Cl)=O";
17 my $patt_str = "C(=O)Cl";
18 my $mol = Chemistry::Mol->parse($mol_str, format => 'smiles');
19 my $patt = Chemistry::Pattern->parse($patt_str, format => 'smiles');
20  
21 # try to match the pattern
22 while ($patt->match($mol)) {
23 @matched_atoms = $patt->atom_map;
24 print "Matched: (@matched_atoms)\n";
25 # should print something like "Matched: (a6 a8 a7)"
26 }
27  
28 =head1 DESCRIPTION
29  
30 This module implements basic pattern matching for molecules.
31 The Chemistry::Pattern class is a subclass of Chemistry::Mol, so patterns
32 have all the properties of molecules and can come from reading the same
33 file formats. Of course there are certain formats (such as SMARTS)
34 that are exclusively used to describe patterns.
35  
36 To perform a pattern matching operation on a molecule, follow these steps.
37  
38 1) Create a pattern object, either by parsing a file or string, or by adding
39 atoms and bonds by hand by using Chemistry::Mol methods. Note that atoms and
40 bonds in a pattern should be Chemistry::Pattern::Atom and
41 Chemistry::Patern::Bond objects. Let's assume that the pattern object is
42 stored in $patt and that the molecule is $mol.
43  
44 2) Execute the pattern on the molecule by calling $patt->match($mol).
45  
46 3) If $patt->match() returns true, extract the "map" that relates the pattern to
47 the molecule by calling $patt->atom_map or $patt->bond_map. These methods
48 return a list of the atoms or bonds in the molecule that are matched by the
49 corresponding atoms in the pattern. Thus $patt->atom_map(1) would be analogous
50 to the $1 special variable used for regular expresion matching. The difference
51 between Chemistry::Pattern and Perl regular expressions is that atoms and bonds
52 are always captured.
53  
54 4) If more than one match for the molecule is desired, repeat from step (2)
55 until match() returns false.
56  
57 =cut
58  
59 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use base qw(Chemistry::Mol);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Chemistry::Pattern::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Chemistry::Pattern::Bond;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 =head1 METHODS
67  
68 =over 4
69  
70 =item Chemistry::Pattern->new(name => value, ...)
71  
72 Create a new empty pattern. This is just like the Chemistry::Mol constructor,
73 with one additional option: "options", which expects a hash reference (the
74 options themselves are described under the options() method).
75  
76 =cut
77  
78 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
79 0 my $class = shift;
80 0 my $self = $class->SUPER::new(
81 options => { overlap => 1, permute => 0},
82 @_
83 );
84 0 $self->reset;
85 0 $self;
86 }
87  
88 =item $pattern->options(option => value,...)
89  
90 Available options:
91  
92 =over
93  
94 =item overlap
95  
96 If true, matches may overlap. For example, the CC pattern could match twice
97 on propane if this option is true, but only once if it is false. This option
98 is true by default.
99  
100 =item permute
101  
102 Sometimes there is more than one way of matching the same set of pattern atoms
103 on the same set of molecule atoms. If true, return these "redundant" matches.
104 For example, the CC pattern could match ethane with two different permutations
105 (forwards and backwards). This option is false by default.
106  
107 =back
108  
109 =cut
110  
111 0 ▶sub options {
Subroutine options: not called CC=3 SCAR=24.8
Pod documented
112 0 my $self = shift;
113 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
114 0 ▶ $self->{options} = {%{$self->{options}||{}}, %{$_[0]}};
Condition0%0/2
$self->{'options'} || {}
l!l
00
MC/DC0%0/1
$self->{'options'} || {}
$self->{'options'}
Truth table0%0/2
A
A= $self->{'options'}
Aresult
11
00
115 } else {
116 0 $self->{options} = {%{$self->{options}}, @_};
117 }
118 }
119  
120 0 ▶sub atom_class { "Chemistry::Pattern::Atom" }
Subroutine atom_class: not called CC=1 SCAR=6.9
Pod documented
121  
122 0 ▶sub bond_class { "Chemistry::Pattern::Bond" }
Subroutine bond_class: not called CC=1 SCAR=6.9
Pod documented
123  
124 1 our $DEBUG = 0;
125  
126 =item $patt->reset
127  
128 Reset the state of the pattern matching object, so that it begins the next
129 match from scratch instead of where it left off after the last one.
130  
131 =cut
132  
133 0 ▶sub reset {
Subroutine reset: not called CC=3 SCAR=24.8
Pod documented
134 0 my ($self, $mol, %opts) = @_;
135 0 ▶ print "Resetting to ($mol, $opts{atom})\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
136 0 $self->{flat} = $self->flatten;
137 0 $self->map_to($mol);
138 0 ▶ $self->{pending_atoms} = [$mol ? $mol->atoms : ()];
Branch: 1 branch
BranchTrueFalseTotal
$mol ? : 0 0 0
139 0 $self->{already_matched} = {};
140 0 $self->{paint_tab} = {};
141 0 $self->{anchor} = '';
142 0 my $atom = $self->next_atom($opts{atom});
143 #$self->match_local_init($atom) if $atom;
144 }
145  
146 0 ▶sub already_matched {
Subroutine already_matched: not called CC=5 SCAR=34.0
Pod undocumented
147 0 my ($self, @objs) = @_;
148 0 my @ids = map {$_->id} @objs;
149 0 my $unsorted_key = join " ", @ids;
150 0 my $key;
151 0 ▶ if ($self->{options}{permute}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'options'}{'permute'}) { } 0 0 0
152 0 $key = $unsorted_key;
153 } else {
154 0 $key = join " ", sort @ids;
155 }
156  
157 0 ▶ if ($self->{already_matched}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'already_matched'}{$key}) { } 0 0 0
158 0 ▶ print "already matched $unsorted_key\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
159 0 return 1;
160 } else {
161 0 $self->{already_matched}{$key} = 1;
162 0 ▶ print "first match of $key\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
163 0 return 0;
164 }
165 }
166  
167 0 ▶sub next_atom {
Subroutine next_atom: not called CC=6 SCAR=37.4
Pod undocumented
168 0 my ($self, $atom) = @_;
169  
170 0 ▶ print "next_atom\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
171 0 ▶ return if $self->{anchor};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"anchor"} 0 0 0
172 0 ▶ if ($atom) {
Branch: 2 branches
BranchTrueFalseTotal
if ($atom) { } 0 0 0
elsif (@{$self->{'pending_atoms'};}) { } 0 0 0
173 0 $self->{anchor} = $atom;
174 0 } elsif (@{$self->{pending_atoms}}) {
175 0 $atom = shift @{$self->{pending_atoms}};
176 0 ▶ print "\tatom $atom\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
177 }
178
179 0 $self->{next_atom} = $atom;
180 0 $atom;
181 }
182  
183 1 Chemistry::Obj::accessor "map_to";
184  
185 =item $pattern->atom_map
186  
187 Returns the list of atoms that matched the last time $pattern->match was called.
188  
189 =cut
190  
191 0 ▶sub atom_map {
Subroutine atom_map: not called CC=2 SCAR=17.9
Pod documented
192 0 my $self = shift;
193 0 my @atoms = map { $_->map_to } $self->atoms(@_);
194 0 ▶ @atoms == 1 ? $atoms[0] : @atoms;
Branch: 1 branch
BranchTrueFalseTotal
@atoms == 1 ? : 0 0 0
195 }
196  
197 =item $pattern->bond_map
198  
199 Returns the list of bonds that matched the last time $pattern->match was called.
200  
201 =cut
202  
203 0 ▶sub bond_map {
Subroutine bond_map: not called CC=2 SCAR=17.9
Pod documented
204 0 my $self = shift;
205 0 my @bonds = map { $_->map_to } $self->bonds(@_);
206 0 ▶ @bonds == 1 ? $bonds[0] : @bonds;
Branch: 1 branch
BranchTrueFalseTotal
@bonds == 1 ? : 0 0 0
207 }
208  
209 =item $pattern->match($mol, %options)
210  
211 Returns true if the pattern matches the molecule. If called again for the same
212 molecule, continues matching where it left off (in a way similar to global
213 regular expressions under scalar context). When there are no matches left,
214 returns false. To force the match to always start from scratch instead of
215 continuing where it left off, the C<reset> option may be used.
216  
217 $pattern->match($mol, atom => $atom)
218  
219 If atom => $atom is given as an option, match will only look for matches that
220 start at $atom (which should be an atom in $mol, of course). This is somewhat
221 analog to anchored regular expressions.
222  
223 To find out which atoms and bonds matched, use the atom_map and bond_map
224 methods.
225  
226 =cut
227  
228 0 ▶sub match {
Subroutine match: not called CC=9 SCAR=45.0
Pod documented
229 0 my ($self, $mol, %opts) = @_;
230 0 ▶ print "match $self $mol\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
231 0 ▶ if (defined($mol) and $self->map_to ne $mol
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mol and $self->map_to ne $mol or $opts{"reset"} or defined $opts{"atom"} and $opts{"atom"} ne $self->{"anchor"}) 0 0 0
Condition0%0/12
defined $mol and $self->map_to ne $mol or $opts{"reset"} or defined $opts{"atom"} and $opts{"atom"} ne $self->{"anchor"}
l!l&&r!l&&!r
000
defined $mol and $self->map_to ne $mol or $opts{"reset"}
l!l&&r!l&&!r
000
defined $mol and $self->map_to ne $mol
!ll&&!rl&&r
000
defined $opts{"atom"} and $opts{"atom"} ne $self->{"anchor"}
!ll&&!rl&&r
000
MC/DC0%0/5
defined $mol and $self->map_to ne $mol or $opts{"reset"} or defined $opts{"atom"} and $opts{"atom"} ne $self->{"anchor"}
defined $mol $self->map_to ne $mol $opts{"reset"} defined $opts{"atom"} $opts{"atom"} ne $self->{"anchor"}
Truth table0%0/9
A and B or C or D and E
A= defined $molB= $self->map_to ne $molC= $opts{"reset"}D= defined $opts{"atom"}E= $opts{"atom"} ne $self->{"anchor"}
ABCDEresult
11XXX1
0X1XX1
101XX1
0X0111
100111
0X00X0
0X0100
1000X0
100100
232 or $opts{reset}
233 or defined($opts{atom}) and $opts{atom} ne $self->{anchor}
234 ){
235 0 $self->reset($mol, %opts);
236 }
237 0 my $match = $self->match_next;
238 0 ▶ $self->map_to(undef) unless $match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 0 0
239 0 ▶ print "returning match: '$match'\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
240 0 $match;
241 }
242  
243 0 ▶sub match_next {
Subroutine match_next: not called CC=7 SCAR=40.3
Pod undocumented
244 0 my $self = shift;
245 0 my $match;
246 0 ▶ print "match_next\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
247  
248 0 while (1) {
249 0 ▶ $self->match_local_init($self->{next_atom}) if $self->{next_atom};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"next_atom"} 0 0 0
250 0 $match = $self->match_local_next;
251 0 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) { } 0 0 0
252 0 ▶ if ($self->already_matched($self->atom_map, $self->bond_map)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->already_matched($self->atom_map, $self->bond_map)) { } 0 0 0
253 0 $match = 0, next; # try again
254 } else {
255 0 ▶ $self->next_atom unless ($self->{options}{overlap});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"options"}{"overlap"} 0 0 0
256 0 last; # matched!
257 }
258 } else {
259 0 ▶ $self->next_atom or last;
Branch: 1 branch
BranchTrueFalseTotal
$self->next_atom or last 0 0 0
260 }
261 }
262 0 $match;
263 }
264  
265  
266 0 ▶sub match_local_init {
Subroutine match_local_init: not called CC=5 SCAR=34.0
Pod undocumented
267 0 my ($patt, $atom) = @_;
268 0 my $mol = $patt->map_to;
269  
270 0 ▶ print "match_local_init(",$patt->atoms(1),", $atom)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
271  
272 # clean up
273 0 $_->map_to(undef) for ($patt->atoms, $patt->bonds);
274 0 ▶ $patt->{paint_tab} = {} if ($mol and $patt->{options}{overlap});
Branch: 1 branch
BranchTrueFalseTotal
if $mol and $patt->{"options"}{"overlap"} 0 0 0
Condition0%0/3
$mol and $patt->{"options"}{"overlap"}
!ll&&!rl&&r
000
MC/DC0%0/2
$mol and $patt->{"options"}{"overlap"}
$mol $patt->{"options"}{"overlap"}
Truth table0%0/3
A and B
A= $molB= $patt->{"options"}{"overlap"}
ABresult
0X0
100
111
275 0 $patt->{stack} = [[0]];
276 0 $patt->{backtrack} = 0;
277 0 $patt->{next_atom} = 0;
278 0 $patt->{current_atom} = $atom;
279 }
280  
281  
282 0 ▶sub match_local_next {
Subroutine match_local_next: not called CC=49 SCAR=78.0
Pod undocumented
283 0 my ($patt) = @_;
284 0 my $stack = $patt->{stack};
285 0 my $paint_tab = $patt->{paint_tab};
286 0 my $match = 0;
287 0 my $backtrack = $patt->{backtrack};
288 0 my $where = $patt->{current_atom};
289 0 my $flat = $patt->{flat};
290 0 my $mol = $patt->map_to;
291  
292 0 ▶ print "match_local_next($where)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
293  
294 0 while (1) {
295 0 my ($idx) = @{$stack->[-1]};
296 0 my $pos = @$stack - 1;
297 0 my $what = $flat->[$pos];
298 0 ▶ if($DEBUG) { print "[@$_] " for @$stack; print "\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($Chemistry::Pattern::DEBUG) 0 0 0
299  
300 0 ▶ if ($backtrack) {
Branch: 5 branches
BranchTrueFalseTotal
if ($backtrack) { } 0 0 0
elsif ($pos >= @$flat) { } 0 0 0
elsif (@$stack == 1) { } 0 0 0
elsif ($what->isa('Chemistry::Pattern::Atom')) { } 0 0 0
elsif ($what->isa('Chemistry::Pattern::Bond')) { } 0 0 0
301 0 ▶ print "\tbacktracking...\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
302 0 ▶ if (@$stack <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$stack <= 2) 0 0 0
303 0 ▶ print "\tnowhere to backtrack\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
304 0 last;
305 }
306 0 my $to;
307 0 pop @$stack;
308 0 (undef, $what, $to) = @{$stack->[-1]};
309 0 $backtrack = 0;
310 0 ▶ if ($what) {
Branch: 1 branch
BranchTrueFalseTotal
if ($what) 0 0 0
311 0 ▶ print "\tcleaning $what\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
312 0 $paint_tab->{$what->map_to} = 0;
313 0 $what->map_to(undef);
314 }
315 0 ▶ if ($to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to) 0 0 0
316 0 ▶ print "\tcleaning $to\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
317 0 $paint_tab->{$to->map_to} = 0;
318 0 $to->map_to(undef);
319 }
320 0 next;
321 } elsif ($pos >= @$flat) { # nothing else to match
322 0 ▶ print "\tmatched!\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
323 0 $backtrack = 1;
324 0 $match = 1;
325 0 last;
326 } elsif (@$stack == 1) { # match first atom (anchored)
327 0 ▶ if (!$paint_tab->{$where} and $what->test($where)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $paint_tab->{$where} and $what->test($where)) { } 0 0 0
Condition0%0/3
not $paint_tab->{$where} and $what->test($where)
!ll&&!rl&&r
000
MC/DC0%0/2
not $paint_tab->{$where} and $what->test($where)
not $paint_tab->{$where} $what->test($where)
Truth table0%0/3
not(A) and B
A= not $paint_tab->{$where}B= $what->test($where)
ABresult
0X0
100
111
328 0 ▶ print "\tmatched initial atom $what on $where\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
329 0 $paint_tab->{$where} = 1;
330 0 $what->map_to($where);
331 0 push @$stack, [0];
332 0 next;
333 } else {
334 0 last;
335 }
336 } elsif ($what->isa("Chemistry::Pattern::Atom")) {
337 # match unanchored atom
338 0 ▶ if($idx >= $mol->atoms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= $mol->atoms) 0 0 0
339 0 ▶ print "\tno more atoms at $mol\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
340 0 $backtrack = 1, next;
341 }
342 0 my $next = $mol->atoms($idx+1);
343 0 $stack->[-1] = [++$idx];
344 0 ▶ if ($paint_tab->{$next}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paint_tab->{$next}) 0 0 0
345 0 ▶ print "\t$next already visited\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
346 0 next;
347 }
348 0 ▶ if ($what->test($next)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($what->test($next)) { } 0 0 0
349 0 ▶ print "\tmatched unanchored atom $what on $next\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
350 0 $paint_tab->{$next} = 1;
351 0 $what->map_to($next);
352 0 $stack->[-1] = [$idx, undef, $what];
353 0 push @$stack, [0];
354 } else {
355 0 ▶ print "\tunanchored atom $what didn't match $next\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
356 }
357 0 next;
358 } elsif ($what->isa("Chemistry::Pattern::Bond")) { # match bond
359 0 my ($a1, $a2) = $what->atoms;
360 0 ▶ my ($from, $to) = $a1->map_to ? ($a1, $a2) : ($a2, $a1);
Branch: 1 branch
BranchTrueFalseTotal
$a1->map_to ? : 0 0 0
361 0 ▶ $where = $a1->map_to || $a2->map_to;
Condition0%0/3
$a1->map_to || $a2->map_to
l!l&&r!l&&!r
000
MC/DC0%0/2
$a1->map_to || $a2->map_to
$a1->map_to $a2->map_to
Truth table0%0/3
A || B
A= $a1->map_toB= $a2->map_to
ABresult
1X1
011
000
362 0 ▶ print "\tfrom $from to $to\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
363 0 my @bn = $where->bonds_neighbors;
364 0 ▶ if ($idx >= @bn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= @bn) 0 0 0
365 0 ▶ print "\tno more bonds at $where\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
366 0 $backtrack = 1, next;
367 }
368 0 my $nei = $bn[$idx]{to};
369 0 my $bond = $bn[$idx]{bond};
370 0 $stack->[-1] = [++$idx];
371  
372 0 ▶ if ($paint_tab->{$bond}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paint_tab->{$bond}) 0 0 0
373 0 ▶ print "\t$bond already visited\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
374 0 next;
375 }
376 0 ▶ if ($a1->map_to and $a2->map_to) { # ring closure bond
Branch: 1 branch
BranchTrueFalseTotal
if ($a1->map_to and $a2->map_to) { } 0 0 0
Condition0%0/3
$a1->map_to and $a2->map_to
!ll&&!rl&&r
000
MC/DC0%0/2
$a1->map_to and $a2->map_to
$a1->map_to $a2->map_to
Truth table0%0/3
A and B
A= $a1->map_toB= $a2->map_to
ABresult
0X0
100
111
377 0 ▶ if ($what->test($bond)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($what->test($bond)) 0 0 0
378 0 ▶ print "\tmatched ring bond $what on $bond\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
379 0 ▶ if ($to->map_to eq $nei) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to->map_to eq $nei) { } 0 0 0
380 0 ▶ print "\tring closed $to on $nei\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
381 0 $paint_tab->{$bond} = 1;
382 0 $what->map_to($bond);
383 0 $stack->[-1] = [$idx, $what];
384 0 push @$stack, [0];
385 0 next;
386 } else {
387 0 ▶ print "\tring didn't close $to on $nei\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
388 }
389 }
390 } else {
391 0 ▶ if ($paint_tab->{$nei}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paint_tab->{$nei}) 0 0 0
392 0 ▶ print "\t$nei already visited\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
393 0 next;
394 }
395 0 ▶ if ($what->test($bond)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($what->test($bond)) { } 0 0 0
396 0 ▶ print "\tmatched bond $what on $bond\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
397 0 ▶ if ($to->test($nei)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to->test($nei)) { } 0 0 0
398 0 ▶ print "\tmatched atom $to on $nei\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
399 0 $paint_tab->{$nei} = 1;
400 0 $to->map_to($nei);
401 0 $paint_tab->{$bond} = 1;
402 0 $what->map_to($bond);
403 0 $stack->[-1] = [$idx, $what, $to];
404 0 push @$stack, [0];
405 0 next;
406 } else {
407 0 ▶ print "\tatom $to didn't match $nei\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
408 }
409 } else {
410 0 ▶ print "\tbond $what didn't match $bond\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
411 }
412 }
413 0 next;
414 } else {
415 0 die "shouldn't be here; what=($what)!";
416 }
417 }
418 0 $patt->{backtrack} = $backtrack;
419 0 $patt->{stack} = $stack;
420 0 $match;
421 }
422  
423 0 ▶sub flatten {
Subroutine flatten: not called CC=4 SCAR=30.0
Pod undocumented
424 0 my ($patt) = @_;
425 0 my $visited = {};
426 0 my $list = [];
427 0 for my $atom ($patt->atoms) {
428 0 ▶ next if $visited->{$atom};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$atom} 0 0 0
429 0 push @$list, $atom;
430 0 _flatten($patt, $atom, $visited, $list);
431 }
432 0 ▶ print "flattened list: (@$list)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Pattern::DEBUG 0 0 0
433 0 $list;
434 }
435  
436 0 ▶sub _flatten {
Subroutine _flatten: not called CC=4 SCAR=30.0
437 0 my ($patt, $atom, $visited, $list) = @_;
438  
439 0 $visited->{$atom} = 1;
440 0 for my $bn ($atom->bonds_neighbors) {
441 0 my $nei = $bn->{to};
442 0 my $bond = $bn->{bond};
443 0 ▶ next if $visited->{$bond};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$bond} 0 0 0
444 0 $visited->{$bond} = 1;
445 0 push @$list, $bond;
446 0 ▶ unless ($visited->{$nei}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($visited->{$nei}) 0 0 0
447 0 _flatten($patt, $nei, $visited, $list);
448 }
449 }
450 }
451  
452 1 1;
453  
454 =back
455  
456 =head1 VERSION
457  
458 0.27
459  
460 =head1 SEE ALSO
461  
462 L<Chemistry::Pattern::Atom>, L<Chemistry::Pattern::Bond>, L<Chemistry::Mol>,
463 L<Chemistry::File>, L<Chemistry::File::SMARTS>.
464  
465 The PerlMol website L<http://www.perlmol.org/>
466  
467 =head1 AUTHOR
468  
469 Ivan Tubert-Brohman E<lt>itub@cpan.orgE<gt>
470  
471 =head1 COPYRIGHT
472  
473 Copyright (c) 2009 Ivan Tubert-Brohman. All rights reserved. This program is
474 free software; you can redistribute it and/or modify it under the same terms as
475 Perl itself.
476  
477 =cut
478  
↑ Summary Pattern/Bond.pm »