blib/lib/Chemistry/File/SMARTS.pm

filter: Statementstmt 86.0% 173 / 201 Branchbran 47.7% 64 / 134 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 22 / 22 Podpod 14.2% 1 / 7 totaltotal 70.2% 262 / 373 CC 72 SCAR 38.7
CC
72
Cov
70%
parse_atomic_primitive
91.0
tokenize
70.5
parse_smarts
11.7
CRAP 47.7
↑ Summary
1 package Chemistry::File::SMARTS;
2  
3 2 $VERSION = "0.22";
4 # $Id: SMARTS.pm,v 1.8 2005/05/16 22:20:24 itubert Exp $
5  
6 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Chemistry::Pattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use base "Chemistry::File";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 #use Data::Dumper;
13 2 ▶use List::Util 'sum', 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Chemistry::File::SMARTS - SMARTS chemical substructure pattern linear notation parser
18  
19 =head1 SYNOPSYS
20  
21 #!/usr/bin/perl
22 use Chemistry::File::SMARTS;
23  
24 # this string matches an oxygen next to an atom with three
25 # neighbors, one of which is a hydrogen, and a positive charge
26 my $smarts = 'O[D3H+]';
27  
28 # parse a SMARTS string and compile it into a
29 # Chemistry::Pattern object
30 my $patt = Chemistry::Pattern->parse("$smarts", format => 'smarts');
31  
32 # find matches of the pattern in a Chemistry::Mol object $mol
33 my $mol = Chemistry::Mol->read("myfile.mol");
34 while ($patt->match($mol)) {
35 print "pattern matches atoms: ", $patt->atom_map, "\n"
36 }
37  
38 # NOTE: if the SMARTS pattern relies on aromaticity or ring
39 # properties, you have to make sure that the target
40 # molecule is "aromatized" first:
41 my $smarts = 'c:a';
42 my $patt = Chemistry::Pattern->parse("$smarts", format => 'smarts');
43 use Chemistry::Ring 'aromatize_mol';
44 aromatize_mol($mol); # <--- AROMATIZE!!!
45 while ($patt->match($mol)) {
46 print "pattern matches atoms: ", $patt->atom_map, "\n"
47 }
48  
49 # Note that "atom mapping numbers" end up as $atom->name
50 my $patt = Chemistry::Pattern->parse("[C:7][C:8]", format => 'smarts');
51 print $patt->atoms(1)->name; # prints 7
52  
53  
54 =head1 DESCRIPTION
55  
56 This module parse a SMARTS (SMiles ARbitrary Target Specification) string,
57 generating a L<Chemistry::Pattern> object. It is a file I/O driver for the
58 PerlMol toolkit; it's not called directly but by means of the
59 Chemistry::Pattern->parse class method.
60  
61 For a detailed description of the SMARTS language, see
62 L<http://www.daylight.com/dayhtml/doc/theory/theory.smarts.html>. Note that
63 this module doesn't implement the full language, as detailed under CAVEATS.
64  
65 This module is part of the PerlMol project, L<http://www.perlmol.org/>.
66  
67 =cut
68  
69 # Initialization
70 2 Chemistry::Mol->register_format(smarts => __PACKAGE__);
71 2 our $DEBUG = 0;
72  
73 # Chemistry::File interface
74  
75 1 ▶sub parse_string {
Subroutine parse_string: called CC=1 SCAR=0.0
Pod documented
76 1 my ($self, $s, %opts) = @_;
77 1 my $patt = parse_smarts($s, \%opts);
78 1 $patt->name($s);
79 1 $patt;
80 }
81  
82  
83 ############## SMARTS PARSER ##############
84  
85 1 ▶sub parse_smarts {
Subroutine parse_smarts: called CC=10 SCAR=24.6
Pod undocumented
86 1 my ($s, $opts) = @_;
87 1 my %digits;
88  
89 1 my $toks = tokenize($s);
90 1 my $tok = shift(@$toks);
91 1 ▶ print "tok: $tok\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::SMARTS::DEBUG 0 1 1
92 1 ▶ my $mol_class = $opts->{mol_class} || "Chemistry::Pattern";
Condition50%1/2
$opts->{'mol_class'} || 'Chemistry::Pattern'
l!l
10
MC/DC0%0/1
$opts->{'mol_class'} || 'Chemistry::Pattern'
$opts->{'mol_class'}
Truth table50%1/2
A
A= $opts->{'mol_class'}
Aresult
11
00
93 1 my $patt = $mol_class->new();
94 1 ▶ $patt->options($opts->{pattern_options}||{});
Condition50%1/2
$opts->{'pattern_options'} || {}
l!l
01
MC/DC0%0/1
$opts->{'pattern_options'} || {}
$opts->{'pattern_options'}
Truth table50%1/2
A
A= $opts->{'pattern_options'}
Aresult
11
00
95 1 my @atom_stack;
96 1 my $current_atom = parse_atom($patt, $tok, $toks);
97 1 ▶ while (defined ($tok = shift @$toks)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($tok = shift @$toks)) 5 1 6
98 5 ▶ print "tok: $tok\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::SMARTS::DEBUG 0 5 5
99 5 ▶ if ($tok eq '(') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tok eq '(') { } 1 4 5
elsif ($tok eq ')') { } 1 3 4
100 1 push @atom_stack, $current_atom;
101 } elsif ($tok eq ')') {
102 1 $current_atom = pop @atom_stack;
103 } else { # bond, atom
104 3 my $next_tok = shift @$toks;
105 3 ▶ if ($next_tok =~ /^\d+$/) { # digit
Branch: 1 branch
BranchTrueFalseTotal
if ($next_tok =~ /^\d+$/) { } 0 3 3
106 0 ▶ if ($digits{$next_tok}) { # close ring
Branch: 1 branch
BranchTrueFalseTotal
if ($digits{$next_tok}) { } 0 0 0
107 parse_bond($patt, $tok, $current_atom,
108 0 $digits{$next_tok});
109 0 $digits{$next_tok} = undef;
110 } else { # open ring
111 0 $digits{$next_tok} = $current_atom;
112 }
113 } else {
114 3 my $next_atom = parse_atom($patt, $next_tok, $toks);
115 3 parse_bond($patt, $tok, $current_atom, $next_atom);
116 3 $current_atom = $next_atom;
117 }
118 }
119 }
120 1 $patt;
121 }
122  
123 4 ▶sub parse_atom {
Subroutine parse_atom: called CC=4 SCAR=13.9
Pod undocumented
124 4 my ($patt, $s, $toks) = @_;
125
126 4 my $n_rec = 0;
127 4 my $name;
128  
129 4 ▶ if ($s =~ s/:(\d+)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/:(\d+)$//) 2 2 4
130 2 $name = $1;
131 }
132 my $expr =
133 join " and ", map {
134 4 join " || ", map {
135 4 join ' && ', map {
136 4 parse_atomic_primitive($_, \$n_rec);
137 } split '&', $_;
138 } split ',', $_;
139 } split ';', $s;
140  
141
142 4 my @recs;
143 4 for (1 .. $n_rec) {
144 0 my $rec_smarts = shift @$toks;
145 0 my $rec = Chemistry::Pattern->parse($rec_smarts,
146 pattern_options => {overlap=>0, permute=>0},
147 format => 'smarts',
148 );
149 0 push @recs, $rec;
150 }
151  
152 4 ▶ print "atom expr: $expr\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::SMARTS::DEBUG 0 4 4
153 1 ▶ my $sub = eval <<SUB;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
154 sub {
155 no warnings;
156 my (\$patt, \$atom) = \@_;
157 $expr;
158 };
159 SUB
160 4 my $atom = $patt->atom_class->new(
161 test_sub => $sub,
162 name => $name,
163 );
164 4 $patt->add_atom($atom);
165 4 $atom;
166 }
167  
168  
169 # missing primitives: @, @@
170 4 ▶sub parse_atomic_primitive {
Subroutine parse_atomic_primitive: called CC=27 SCAR=45.1
Pod undocumented
171 4 my ($s, $n_rec) = @_;
172 4 local $_ = $s;
173 4 my @terms;
174 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
175  
176 4 ▶ s/^(!?)H// && # Hydrogen atom
Branch: 1 branch
BranchTrueFalseTotal
s/^(!?)H// and push @terms, "$1(\$atom->symbol eq 'H')" 0 4 4
177 push @terms, "$1(\$atom->symbol eq 'H')";
178  
179 4 ▶ s/(!?)
Branch: 1 branch
BranchTrueFalseTotal
s/(!?) (Zr|Zn|Yb|Y|Xe|W|V|U|Tm|Tl|Ti|Th| Te|Tc|Tb|Ta|Sr|Sn|Sm|Si|Sg|Se|Sc|Sb|S|Ru|Rn|Rh|Rf|Re|Rb|Ra| Pu|Pt|Pr|Po|Pm|Pd|Pb|Pa|P|Os|O|Np|No|Ni|Ne|Nd|Nb|Na|N|Mt|Mt| Mo|Mn|Mg|Md|Lu|Lr|Li|La|Kr|K|Ir|In|I|Hs|Hs|Ho|Hg|Hf|He|Ge| Gd|Ga|Fr|Fm|Fe|F|Eu|Es|Er|Dy|Ds|Db|Cu|Cs|Cr|Co|Cm|Cl|Cf|Ce| Cd|Ca|C|Br|Bk|Bi|Bh|Be|Ba|B|Au|At|As|Ar|Am|Al|Ag|Ac) //x and push @terms, "$1(\$atom->symbol eq '$2' && ! \$atom->aromatic)" 4 0 4
180 (Zr|Zn|Yb|Y|Xe|W|V|U|Tm|Tl|Ti|Th|
181 Te|Tc|Tb|Ta|Sr|Sn|Sm|Si|Sg|Se|Sc|Sb|S|Ru|Rn|Rh|Rf|Re|Rb|Ra|
182 Pu|Pt|Pr|Po|Pm|Pd|Pb|Pa|P|Os|O|Np|No|Ni|Ne|Nd|Nb|Na|N|Mt|Mt|
183 Mo|Mn|Mg|Md|Lu|Lr|Li|La|Kr|K|Ir|In|I|Hs|Hs|Ho|Hg|Hf|He|Ge|
184 Gd|Ga|Fr|Fm|Fe|F|Eu|Es|Er|Dy|Ds|Db|Cu|Cs|Cr|Co|Cm|Cl|Cf|Ce|
185 Cd|Ca|C|Br|Bk|Bi|Bh|Be|Ba|B|Au|At|As|Ar|Am|Al|Ag|Ac)
186 //x # Order is reverse alphabetical to ensure longest match
187 && push @terms, "$1(\$atom->symbol eq '$2' && ! \$atom->aromatic)";
188  
189 4 ▶ s/(!?)\*// && # wildcard
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)\*// and push @terms, "$11" 0 4 4
190 push @terms, "${1}1";
191  
192 4 ▶ s/(!?)D(\d?)// && # explicit connections (shouldn't count h)
Branch: 2 branches
BranchTrueFalseTotal
s/(!?)D(\d?)// and push @terms, "$1(\$atom->bonds == " . (length $2 ? $2 : 1) . ')' 0 4 4
length $2 ? : 0 0 0
193 push @terms, "$1(\$atom->bonds == " . (length $2 ? $2 : 1) . ')';
194  
195 4 ▶ s/(!?)a// && # aromatic
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)a// and push @terms, "$1\$atom->aromatic" 0 4 4
196 push @terms, "$1\$atom->aromatic";
197  
198 4 ▶ s/(!?)A// && # aliphatic
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)A// and push @terms, "$1(!\$atom->aromatic)" 0 4 4
199 push @terms, "$1(!\$atom->aromatic)";
200  
201 4 ▶ s/(!?)X(\d?)// && # total connections (should add implicit H)
Branch: 2 branches
BranchTrueFalseTotal
s/(!?)X(\d?)// and push @terms, "$1(\$atom->bonds + \$atom->hydrogens == " . (length $2 ? $2 : 1) . ')' 0 4 4
length $2 ? : 0 0 0
202 push @terms, "$1(\$atom->bonds + \$atom->hydrogens == "
203 . (length $2 ? $2 : 1) . ')';
204  
205 4 ▶ s/(!?)v(\d?)// && # valence
Branch: 2 branches
BranchTrueFalseTotal
s/(!?)v(\d?)// and push @terms, "$1(\$atom->valence == " . (length $2 ? $2 : 1) . ')' 0 4 4
length $2 ? : 0 0 0
206 push @terms, "$1(\$atom->valence == "
207 . (length $2 ? $2 : 1) . ')';
208  
209 4 ▶ s/(!?)H(\d?)// && # total H-count
Branch: 2 branches
BranchTrueFalseTotal
s/(!?)H(\d?)// and push @terms, "$1(sum(map {\$_->symbol eq 'H'} \$atom->neighbors) +\n \$atom->hydrogens == " . (length $2 ? $2 : 1) . ')' 0 4 4
length $2 ? : 0 0 0
210 push @terms, "$1(sum(map {\$_->symbol eq 'H'} \$atom->neighbors) +
211 \$atom->hydrogens == " . (length $2 ? $2 : 1) . ')';
212  
213 4 ▶ s/(!?)h(\d?)// && # implicit H-count
Branch: 2 branches
BranchTrueFalseTotal
s/(!?)h(\d?)// and push @terms, "$1(\$atom->hydrogens == " . (length $2 ? $2 : 1) . ')' 0 4 4
length $2 ? : 0 0 0
214 push @terms, "$1(\$atom->hydrogens == " . (length $2 ? $2 : 1) . ')';
215  
216 4 ▶ s/(!?)R(\d?)// && # number of rings
Branch: 2 branches
BranchTrueFalseTotal
s/(!?)R(\d?)// and push @terms, "$1(\@{\$atom->attr('ring/rings')||[]} " . (length $2 ? "== $2" : "") . ')' 0 4 4
length $2 ? : 0 0 0
217 push @terms, "$1(\@{\$atom->attr('ring/rings')||[]} "
218 . (length $2 ? "== $2" : "") . ')';
219
220 4 ▶ s/(!?)r(\d?)// && # ring size
Branch: 2 branches
BranchTrueFalseTotal
s/(!?)r(\d?)// and push @terms, "$1(first { " . (length $2 ? "\$_->atoms == $2" : 1) . q[ } @{$atom->attr('ring/rings')||[]} )] 0 4 4
length $2 ? : 0 0 0
221 push @terms, "$1(first { " . (length $2 ? "\$_->atoms == $2" : "1")
222 ." } \@{\$atom->attr('ring/rings')||[]} )";
223
224 4 ▶ s/(!?)#(\d+)// && # atomic number
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)#(\d+)// and push @terms, "$1(\$atom->Z == $2)" 0 4 4
225 push @terms, "$1(\$atom->Z == $2)";
226  
227 4 ▶ s/(!?)([+-]\d+)// && # numerical charge
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)([+-]\d+)// and push @terms, "$1(\$atom->formal_charge == $2)" 0 4 4
228 push @terms, "$1(\$atom->formal_charge == $2)";
229  
230 4 ▶ s/(!?)(\++)// && # positive charge
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)(\++)// and push @terms, "$1(\$atom->formal_charge == " . length($2) . ')' 0 4 4
231 push @terms, "$1(\$atom->formal_charge == " . length($2) . ')';
232  
233 4 ▶ s/(!?)(-+)// && # negative charge
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)(-+)// and push @terms, "$1(\$atom->formal_charge == -" . length($2) . ')' 0 4 4
234 push @terms, "$1(\$atom->formal_charge == -" . length($2) . ')';
235  
236 4 ▶ s/(!?)(\d+)// && # mass
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)(\d+)// and push @terms, "$1(\$atom->mass == $2)" 0 4 4
237 push @terms, "$1(\$atom->mass == $2)";
238  
239 4 ▶ s/(!?)([cnosp])// && # aromatic symbol
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)([cnosp])// and push @terms, "$1(\$atom->symbol eq '@{[uc $2];}' && \$atom->aromatic)" 0 4 4
240 0 push @terms, "$1(\$atom->symbol eq '@{[uc $2]}' && \$atom->aromatic)";
241  
242 #s/(!?)([A-Z][a-z]?)// && # aliphatic symbol
243 #push @terms, "$1(\$atom->symbol eq '$2' && ! \$atom->aromatic)";
244  
245 4 ▶ while (s/(!?)\$//) { # recursive SMARTS
Branch: 1 branch
BranchTrueFalseTotal
if (s/(!?)\$//) 0 4 4
246 0 push @terms, qq{$1(\$recs[$$n_rec]->match(\$atom->parent,atom=>\$atom))};
247 0 $$n_rec++;
248 }
249  
250 4 join ' && ', @terms;
251 }
252  
253 3 ▶sub parse_bond {
Subroutine parse_bond: called CC=4 SCAR=13.9
Pod undocumented
254 3 my ($patt, $s, @atoms) = @_;
255  
256 3 ▶ return if $s eq '.'; # the disconnected non-bond
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "." 0 3 3
257  
258 3 my $expr;
259  
260 3 ▶ if ($s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s) { } 1 2 3
261 $expr =
262 join " and ", map {
263 1 join " || ", map {
264 1 join ' && ', map {
265 1 parse_bond_primitive($_);
266 } split '&', $_;
267 } split ',', $_;
268 } split ';', $s;
269 } else {
270 2 $expr = '($bond->order == 1 || $bond->aromatic)';
271 }
272  
273 3 ▶ print "bond expr: $expr\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::SMARTS::DEBUG 0 3 3
274 1 ▶ my $sub = eval <<SUB;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
275 sub {
276 no warnings;
277 my (\$patt, \$bond) = \@_;
278 $expr;
279 };
280 SUB
281 3 my $bond = $patt->bond_class->new(test_sub => $sub,
282 atoms => \@atoms);
283 3 $patt->add_bond($bond);
284 3 $bond;
285 }
286  
287 1 ▶sub parse_bond_primitive {
Subroutine parse_bond_primitive: called CC=7 SCAR=21.0
Pod undocumented
288 1 local ($_) = @_;
289 1 my @terms;
290  
291 1 ▶ s/(!?)~// && # wildcard
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)~// and push @terms, "$11" 0 1 1
292 push @terms, "${1}1";
293  
294 1 ▶ s/(!?)-// && # single
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)-// and push @terms, "$1(\$bond->order == 1 && !\$bond->aromatic)" 0 1 1
295 push @terms, "$1(\$bond->order == 1 && !\$bond->aromatic)";
296  
297 1 ▶ s/(!?)=// && # double
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)=// and push @terms, "$1(\$bond->order == 2)" 1 0 1
298 push @terms, "$1(\$bond->order == 2)";
299  
300 1 ▶ s/(!?)#// && # triple
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)#// and push @terms, "$1(\$bond->order == 3)" 0 1 1
301 push @terms, "$1(\$bond->order == 3)";
302  
303 1 ▶ s/(!?):// && # triple
Branch: 1 branch
BranchTrueFalseTotal
s/(!?):// and push @terms, "$1\$bond->aromatic" 0 1 1
304 push @terms, "$1\$bond->aromatic";
305  
306 1 ▶ s/(!?)\@// && # ring bond
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)\@// and push @terms, "$1(\@{\$bond->attr('ring/rings')||[]} )" 0 1 1
307 push @terms, "$1(\@{\$bond->attr('ring/rings')||[]} )";
308
309 1 join ' && ', @terms;
310 }
311  
312 2 my %ORGANIC_ELEMS = (
313 Br => 1, Cl => 1, B => 1, C => 1, N => 1, O => 1, P => 1, S => 1,
314 F => 1, I => 1, s => 1, p => 1, o => 1, n => 1, c => 1, b => 1,
315 );
316  
317 1 ▶sub tokenize {
Subroutine tokenize: called CC=25 SCAR=42.6
Pod undocumented
318 1 my ($s) = @_;
319 1 my $state = 3;
320 1 my $paren_depth = 0;
321 1 my $atom;
322 my $digit;
323 1 my $rec_smart;
324 1 my @rec_smarts;
325 1 my $bond = '';
326 1 my $symbol;
327 my @toks;
328
329 # 0 expects atom or branch
330 # after atom: atom or bond or branch
331 # after bond: atom or branch or /,;/
332 # token types: atom, bond, (, )
333 1 my @chars = split '', $s;
334 1 my $char;
335 1 ▶ while (defined ($char = shift @chars)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($char = shift @chars)) 14 0 14
336 21 ▶ print "char: $char\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::SMARTS::DEBUG 0 21 21
337 21 ▶ if ($state == 0) { # expect atom or branch (not used!)
Branch: 8 branches
BranchTrueFalseTotal
if ($state == 0) { } 0 21 21
elsif ($state == 1) { } 8 13 21
elsif ($state == 2) { } 4 9 13
elsif ($state == 3) { } 4 5 9
elsif ($state == 4) { } 5 0 5
elsif ($state == 5) { } 0 0 0
elsif ($state == 6) { } 0 0 0
elsif ($state == 7) { } 0 0 0
338 0 ▶ push(@toks, $char), $state = 2, next if ($char =~ /[()]/);
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[()]/ 0 0 0
339 0 $state = 3, redo;
340 } elsif ($state == 1) { # in complex atom
341 8 ▶ if ($char eq ']') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "]") 2 6 8
342 2 push @toks, $atom, @rec_smarts;
343 2 @rec_smarts = ();
344 2 $state = 4, next;
345 }
346 6 $atom .= $char;
347 6 ▶ $state = 5 if ($char eq '$'); # recursive smarts
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\$" 0 6 6
348 6 next;
349 } elsif ($state == 2) { # expect bond
350 4 ▶ if ($char =~ /[-=:~\\\/#@?,;&.!]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ m[[-=:~\\/#@?,;&.!]]) { } 1 3 4
351 1 $bond .= $char;
352 1 next;
353 } else {
354 3 push @toks, $bond;
355 3 $bond = '';
356 3 $state = 3, redo;
357 }
358 } elsif ($state == 3) { # expect atom
359 4 ▶ $state = 1, $atom = '', next if $char eq '[';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "[" 2 2 4
360 2 ▶ if ($char eq '%') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "%") 0 2 2
361 0 $state = 7, next;
362 }
363 2 $symbol = $char;
364 2 ▶ push(@toks, $symbol), last unless @chars;
Branch: 1 branch
BranchTrueFalseTotal
unless @chars 1 1 2
365 1 $char = shift @chars;
366 1 ▶ if ($ORGANIC_ELEMS{$symbol.$char}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ORGANIC_ELEMS{$symbol . $char}) { } 0 1 1
367 0 push @toks, $symbol.$char;
368 0 $state = 4, next;
369 } else {
370 1 push @toks, $symbol;
371 1 $state = 4, redo;
372 }
373 } elsif ($state == 4) { # expect atom or bond or branch
374 5 ▶ push(@toks, $char), next if ($char =~ /[()]/); # branch
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[()]/ 2 3 5
375 3 $state = 2, redo; # bond
376 } elsif ($state == 5) { # expect left paren
377 0 ▶ croak "expected (" unless $char eq '(';
Branch: 1 branch
BranchTrueFalseTotal
unless $char eq "(" 0 0 0
378 0 $rec_smart = '';
379 0 $paren_depth++, $state = 6, next;
380 } elsif ($state == 6) { # within recursive smarts
381 #croak "Recursive SMARTS not implemented yet\n";
382 0 ▶ $paren_depth++ if $char eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "(" 0 0 0
383 0 ▶ $paren_depth-- if $char eq ')';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq ")" 0 0 0
384 0 ▶ unless ($paren_depth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($paren_depth) 0 0 0
385 0 push @rec_smarts, $rec_smart;
386 0 $state = 1, next;
387 }
388 0 $rec_smart .= $char;
389 } elsif ($state == 7) { # double digit
390 0 ▶ $digit = $char . (shift @chars || die "expected second digit");
Condition0%0/2
shift @chars || die('expected second digit')
l!l
00
MC/DC0%0/1
shift @chars || die('expected second digit')
shift @chars
Truth table0%0/2
A
A= shift @chars
Aresult
11
00
391 0 push @toks, $digit;
392 0 $state = 2;
393 } else {
394 0 die "shouldn't be here";
395 }
396 }
397 #print Dumper \@toks if $DEBUG;
398 1 \@toks;
399 }
400  
401  
402 2 1;
403  
404 =head1 CAVEATS
405  
406 The following features are not implemented yet:
407  
408 =over
409  
410 =item chirality: @, @@
411  
412 =item component-level gruouping
413  
414 That is, the difference between these three cases:
415  
416 (SMARTS)
417 (SMARTS).(SMARTS)
418 (SMARTS).SMARTS
419  
420 =back
421  
422 The so-called parser is very lenient, so if you give it something that's not
423 quite reasonable it will ignore it or interpret it in a strange way without
424 warning.
425  
426 As shown in the synopsis, you have to make sure that the molecule is
427 "aromatized" if you want to apply to it a pattern that relies on aromaticity
428 or ring properties.
429  
430 =head1 VERSION
431  
432 0.22
433  
434 =head1 SEE ALSO
435  
436 L<Chemistry::Pattern>, L<Chemistry::Mol>, L<Chemistry::File>,
437 L<Chemistry::File::SMILES>.
438  
439 For more information about SMARTS, see the SMARTS Theory Manual at
440 L<http://www.daylight.com/dayhtml/doc/theory/theory.smarts.html>
441  
442 =head1 AUTHOR
443  
444 Ivan Tubert-Brohman E<lt>itub@cpan.orgE<gt>
445  
446 =head1 COPYRIGHT
447  
448 Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is
449 free software; you can redistribute it and/or modify it under the same terms as
450 Perl itself.
451  
452 =cut
453  
↑ Summary