blib/lib/Chemistry/File/SMARTS.pm

filter: Statementstmt 91.5% 316 / 345 Branchbran 53.7% 72 / 134 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 70 / 70 Podpod 14.2% 1 / 7 totaltotal 81.5% 461 / 565 CC 72 SCAR 32.9
CC
72
Cov
80%
parse_atomic_primitive
67.9
tokenize
67.4
parse_smarts
11.7
CRAP 26.8
« blib/lib/Chemistry/File/XYZ.pm ↑ Summary blib/lib/Chemistry/Ring.pm »
1 package Chemistry::File::SMARTS;
2  
3 10 $VERSION = "0.22";
4 # $Id: SMARTS.pm,v 1.8 2005/05/16 22:20:24 itubert Exp $
5  
6 10 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Chemistry::Pattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use base "Chemistry::File";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 #use Data::Dumper;
13 10 ▶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 10 Chemistry::Mol->register_format(smarts => __PACKAGE__);
71 10 our $DEBUG = 0;
72  
73 # Chemistry::File interface
74  
75 11 ▶sub parse_string {
Subroutine parse_string: called CC=1 SCAR=0.0
Pod documented
76 11 my ($self, $s, %opts) = @_;
77 11 my $patt = parse_smarts($s, \%opts);
78 11 $patt->name($s);
79 11 $patt;
80 }
81  
82  
83 ############## SMARTS PARSER ##############
84  
85 11 ▶sub parse_smarts {
Subroutine parse_smarts: called CC=10 SCAR=24.6
Pod undocumented
86 11 my ($s, $opts) = @_;
87 11 my %digits;
88  
89 11 my $toks = tokenize($s);
90 11 my $tok = shift(@$toks);
91 11 ▶ print "tok: $tok\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::SMARTS::DEBUG 0 11 11
92 11 ▶ my $mol_class = $opts->{mol_class} || "Chemistry::Pattern";
Condition50%1/2
$opts->{'mol_class'} || 'Chemistry::Pattern'
l!l
110
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 11 my $patt = $mol_class->new();
94 11 ▶ $patt->options($opts->{pattern_options}||{});
Condition50%1/2
$opts->{'pattern_options'} || {}
l!l
011
MC/DC0%0/1
$opts->{'pattern_options'} || {}
$opts->{'pattern_options'}
Truth table50%1/2
A
A= $opts->{'pattern_options'}
Aresult
11
00
95 11 my @atom_stack;
96 11 my $current_atom = parse_atom($patt, $tok, $toks);
97 11 ▶ while (defined ($tok = shift @$toks)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($tok = shift @$toks)) 26 11 37
98 26 ▶ print "tok: $tok\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::SMARTS::DEBUG 0 26 26
99 26 ▶ if ($tok eq '(') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tok eq '(') { } 2 24 26
elsif ($tok eq ')') { } 2 22 24
100 2 push @atom_stack, $current_atom;
101 } elsif ($tok eq ')') {
102 2 $current_atom = pop @atom_stack;
103 } else { # bond, atom
104 22 my $next_tok = shift @$toks;
105 22 ▶ if ($next_tok =~ /^\d+$/) { # digit
Branch: 1 branch
BranchTrueFalseTotal
if ($next_tok =~ /^\d+$/) { } 0 22 22
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 22 my $next_atom = parse_atom($patt, $next_tok, $toks);
115 22 parse_bond($patt, $tok, $current_atom, $next_atom);
116 22 $current_atom = $next_atom;
117 }
118 }
119 }
120 11 $patt;
121 }
122  
123 33 ▶sub parse_atom {
Subroutine parse_atom: called CC=4 SCAR=13.9
Pod undocumented
124 33 my ($patt, $s, $toks) = @_;
125
126 33 my $n_rec = 0;
127 33 my $name;
128  
129 33 ▶ if ($s =~ s/:(\d+)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/:(\d+)$//) 0 33 33
130 0 $name = $1;
131 }
132 my $expr =
133 join " and ", map {
134 33 join " || ", map {
135 33 join ' && ', map {
136 40 parse_atomic_primitive($_, \$n_rec);
137 } split '&', $_;
138 } split ',', $_;
139 } split ';', $s;
140  
141
142 33 my @recs;
143 33 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 33 ▶ print "atom expr: $expr\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::SMARTS::DEBUG 0 33 33
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
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
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
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
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
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
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
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
Subroutine BEGIN: called CC=1 SCAR=0.0
154 sub {
155 no warnings;
156 my (\$patt, \$atom) = \@_;
157 $expr;
158 };
159 SUB
160 33 my $atom = $patt->atom_class->new(
161 test_sub => $sub,
162 name => $name,
163 );
164 33 $patt->add_atom($atom);
165 33 $atom;
166 }
167  
168  
169 # missing primitives: @, @@
170 40 ▶sub parse_atomic_primitive {
Subroutine parse_atomic_primitive: called CC=27 SCAR=42.2
Pod undocumented
171 40 my ($s, $n_rec) = @_;
172 40 local $_ = $s;
173 40 my @terms;
174 10 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
175  
176 40 ▶ s/^(!?)H// && # Hydrogen atom
Branch: 1 branch
BranchTrueFalseTotal
s/^(!?)H// and push @terms, "$1(\$atom->symbol eq 'H')" 1 39 40
177 push @terms, "$1(\$atom->symbol eq 'H')";
178  
179 40 ▶ 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)" 17 23 40
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 40 ▶ s/(!?)\*// && # wildcard
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)\*// and push @terms, "$11" 22 18 40
190 push @terms, "${1}1";
191  
192 40 ▶ 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 40 40
length $2 ? : 0 0 0
193 push @terms, "$1(\$atom->bonds == " . (length $2 ? $2 : 1) . ')';
194  
195 40 ▶ s/(!?)a// && # aromatic
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)a// and push @terms, "$1\$atom->aromatic" 0 40 40
196 push @terms, "$1\$atom->aromatic";
197  
198 40 ▶ s/(!?)A// && # aliphatic
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)A// and push @terms, "$1(!\$atom->aromatic)" 0 40 40
199 push @terms, "$1(!\$atom->aromatic)";
200  
201 40 ▶ 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) . ')' 13 27 40
length $2 ? : 13 0 13
202 push @terms, "$1(\$atom->bonds + \$atom->hydrogens == "
203 . (length $2 ? $2 : 1) . ')';
204  
205 40 ▶ s/(!?)v(\d?)// && # valence
Branch: 2 branches
BranchTrueFalseTotal
s/(!?)v(\d?)// and push @terms, "$1(\$atom->valence == " . (length $2 ? $2 : 1) . ')' 0 40 40
length $2 ? : 0 0 0
206 push @terms, "$1(\$atom->valence == "
207 . (length $2 ? $2 : 1) . ')';
208  
209 40 ▶ 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 40 40
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 40 ▶ s/(!?)h(\d?)// && # implicit H-count
Branch: 2 branches
BranchTrueFalseTotal
s/(!?)h(\d?)// and push @terms, "$1(\$atom->hydrogens == " . (length $2 ? $2 : 1) . ')' 0 40 40
length $2 ? : 0 0 0
214 push @terms, "$1(\$atom->hydrogens == " . (length $2 ? $2 : 1) . ')';
215  
216 40 ▶ 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 40 40
length $2 ? : 0 0 0
217 push @terms, "$1(\@{\$atom->attr('ring/rings')||[]} "
218 . (length $2 ? "== $2" : "") . ')';
219
220 40 ▶ 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 40 40
length $2 ? : 0 0 0
221 push @terms, "$1(first { " . (length $2 ? "\$_->atoms == $2" : "1")
222 ." } \@{\$atom->attr('ring/rings')||[]} )";
223
224 40 ▶ s/(!?)#(\d+)// && # atomic number
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)#(\d+)// and push @terms, "$1(\$atom->Z == $2)" 0 40 40
225 push @terms, "$1(\$atom->Z == $2)";
226  
227 40 ▶ s/(!?)([+-]\d+)// && # numerical charge
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)([+-]\d+)// and push @terms, "$1(\$atom->formal_charge == $2)" 0 40 40
228 push @terms, "$1(\$atom->formal_charge == $2)";
229  
230 40 ▶ s/(!?)(\++)// && # positive charge
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)(\++)// and push @terms, "$1(\$atom->formal_charge == " . length($2) . ')' 0 40 40
231 push @terms, "$1(\$atom->formal_charge == " . length($2) . ')';
232  
233 40 ▶ s/(!?)(-+)// && # negative charge
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)(-+)// and push @terms, "$1(\$atom->formal_charge == -" . length($2) . ')' 0 40 40
234 push @terms, "$1(\$atom->formal_charge == -" . length($2) . ')';
235  
236 40 ▶ s/(!?)(\d+)// && # mass
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)(\d+)// and push @terms, "$1(\$atom->mass == $2)" 0 40 40
237 push @terms, "$1(\$atom->mass == $2)";
238  
239 40 ▶ s/(!?)([cnosp])// && # aromatic symbol
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)([cnosp])// and push @terms, "$1(\$atom->symbol eq '@{[uc $2];}' && \$atom->aromatic)" 0 40 40
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 40 ▶ while (s/(!?)\$//) { # recursive SMARTS
Branch: 1 branch
BranchTrueFalseTotal
if (s/(!?)\$//) 0 40 40
246 0 push @terms, qq{$1(\$recs[$$n_rec]->match(\$atom->parent,atom=>\$atom))};
247 0 $$n_rec++;
248 }
249  
250 40 join ' && ', @terms;
251 }
252  
253 22 ▶sub parse_bond {
Subroutine parse_bond: called CC=4 SCAR=13.9
Pod undocumented
254 22 my ($patt, $s, @atoms) = @_;
255  
256 22 ▶ return if $s eq '.'; # the disconnected non-bond
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "." 0 22 22
257  
258 22 my $expr;
259  
260 22 ▶ if ($s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s) { } 8 14 22
261 $expr =
262 join " and ", map {
263 8 join " || ", map {
264 8 join ' && ', map {
265 8 parse_bond_primitive($_);
266 } split '&', $_;
267 } split ',', $_;
268 } split ';', $s;
269 } else {
270 14 $expr = '($bond->order == 1 || $bond->aromatic)';
271 }
272  
273 22 ▶ print "bond expr: $expr\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::SMARTS::DEBUG 0 22 22
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
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
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
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
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
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 22 my $bond = $patt->bond_class->new(test_sub => $sub,
282 atoms => \@atoms);
283 22 $patt->add_bond($bond);
284 22 $bond;
285 }
286  
287 8 ▶sub parse_bond_primitive {
Subroutine parse_bond_primitive: called CC=7 SCAR=19.7
Pod undocumented
288 8 local ($_) = @_;
289 8 my @terms;
290  
291 8 ▶ s/(!?)~// && # wildcard
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)~// and push @terms, "$11" 4 4 8
292 push @terms, "${1}1";
293  
294 8 ▶ s/(!?)-// && # single
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)-// and push @terms, "$1(\$bond->order == 1 && !\$bond->aromatic)" 0 8 8
295 push @terms, "$1(\$bond->order == 1 && !\$bond->aromatic)";
296  
297 8 ▶ s/(!?)=// && # double
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)=// and push @terms, "$1(\$bond->order == 2)" 3 5 8
298 push @terms, "$1(\$bond->order == 2)";
299  
300 8 ▶ s/(!?)#// && # triple
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)#// and push @terms, "$1(\$bond->order == 3)" 1 7 8
301 push @terms, "$1(\$bond->order == 3)";
302  
303 8 ▶ s/(!?):// && # triple
Branch: 1 branch
BranchTrueFalseTotal
s/(!?):// and push @terms, "$1\$bond->aromatic" 0 8 8
304 push @terms, "$1\$bond->aromatic";
305  
306 8 ▶ s/(!?)\@// && # ring bond
Branch: 1 branch
BranchTrueFalseTotal
s/(!?)\@// and push @terms, "$1(\@{\$bond->attr('ring/rings')||[]} )" 0 8 8
307 push @terms, "$1(\@{\$bond->attr('ring/rings')||[]} )";
308
309 8 join ' && ', @terms;
310 }
311  
312 10 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 11 ▶sub tokenize {
Subroutine tokenize: called CC=25 SCAR=42.1
Pod undocumented
318 11 my ($s) = @_;
319 11 my $state = 3;
320 11 my $paren_depth = 0;
321 11 my $atom;
322 my $digit;
323 11 my $rec_smart;
324 11 my @rec_smarts;
325 11 my $bond = '';
326 11 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 11 my @chars = split '', $s;
334 11 my $char;
335 11 ▶ while (defined ($char = shift @chars)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($char = shift @chars)) 83 1 84
336 143 ▶ print "char: $char\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::File::SMARTS::DEBUG 0 143 143
337 143 ▶ if ($state == 0) { # expect atom or branch (not used!)
Branch: 8 branches
BranchTrueFalseTotal
if ($state == 0) { } 0 143 143
elsif ($state == 1) { } 54 89 143
elsif ($state == 2) { } 30 59 89
elsif ($state == 3) { } 33 26 59
elsif ($state == 4) { } 26 0 26
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 54 ▶ if ($char eq ']') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "]") 7 47 54
342 7 push @toks, $atom, @rec_smarts;
343 7 @rec_smarts = ();
344 7 $state = 4, next;
345 }
346 47 $atom .= $char;
347 47 ▶ $state = 5 if ($char eq '$'); # recursive smarts
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\$" 0 47 47
348 47 next;
349 } elsif ($state == 2) { # expect bond
350 30 ▶ if ($char =~ /[-=:~\\\/#@?,;&.!]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ m[[-=:~\\/#@?,;&.!]]) { } 8 22 30
351 8 $bond .= $char;
352 8 next;
353 } else {
354 22 push @toks, $bond;
355 22 $bond = '';
356 22 $state = 3, redo;
357 }
358 } elsif ($state == 3) { # expect atom
359 33 ▶ $state = 1, $atom = '', next if $char eq '[';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "[" 7 26 33
360 26 ▶ if ($char eq '%') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "%") 0 26 26
361 0 $state = 7, next;
362 }
363 26 $symbol = $char;
364 26 ▶ push(@toks, $symbol), last unless @chars;
Branch: 1 branch
BranchTrueFalseTotal
unless @chars 10 16 26
365 16 $char = shift @chars;
366 16 ▶ if ($ORGANIC_ELEMS{$symbol.$char}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ORGANIC_ELEMS{$symbol . $char}) { } 0 16 16
367 0 push @toks, $symbol.$char;
368 0 $state = 4, next;
369 } else {
370 16 push @toks, $symbol;
371 16 $state = 4, redo;
372 }
373 } elsif ($state == 4) { # expect atom or bond or branch
374 26 ▶ push(@toks, $char), next if ($char =~ /[()]/); # branch
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[()]/ 4 22 26
375 22 $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 11 \@toks;
399 }
400  
401  
402 10 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  
« blib/lib/Chemistry/File/XYZ.pm ↑ Summary blib/lib/Chemistry/Ring.pm »