blib/lib/Chemistry/Mok.pm

filter: Statementstmt 36.9% 65 / 176 Branchbran 2.2% 2 / 88 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 74.0% 20 / 27 Podpod 28.5% 2 / 7 totaltotal 29.1% 91 / 312 CC 56 SCAR 53.6
CC
56
Cov
25%
parse
552.0
run
240.0
compile_blocks
90.0
CRAP 212.0
« blib/lib/Chemistry/Bond/Find.pm ↑ Summary blib/lib/Chemistry/Pattern.pm »
1 package Chemistry::Mok;
2  
3 9 $VERSION = '0.25';
4 # $Id: Mok.pm,v 1.10 2005/05/16 21:54:21 itubert Exp $
5  
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Chemistry::Mol;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Chemistry::File ':auto';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Chemistry::Pattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Chemistry::Bond::Find qw(find_bonds assign_bond_orders);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Chemistry::Ring 'aromatize_mol';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Chemistry::3DBuilder 'build_3d';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Text::Balanced ':ALL';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 our $DEBUG = 0;
20  
21 =head1 NAME
22  
23 Chemistry::Mok - molecular awk interpreter
24  
25 =head1 SYNOPSIS
26  
27 use Chemistry::Mok;
28 $code = '/CS/g{ $n++; $l += $match->bond_map(0)->length }
29 END { printf "Average C-S bond length: %.3f\n", $l/$n; }';
30  
31 my $mok = Chemistry::Mok->new($code);
32 $mok->run({ format => mdlmol }, glob("*.mol"));
33  
34 =head1 DESCRIPTION
35  
36 This module is the engine behind the mok program. See mok(1) for a detailed
37 description of the language. Mok is part of the PerlMol project,
38 L<http://www.perlmol.org>.
39  
40 =head1 METHODS
41  
42 =over
43  
44 =cut
45  
46 0 ▶sub tokenize {
Subroutine tokenize: not called CC=5 SCAR=34.0
Pod undocumented
47 0 my ($self, $code) = @_;
48  
49 0 $code =~ s/\s*$//; # Text::Balanced complains about trailing whitespace
50 #$code =~ s/^\s*#.*//g; # remove comments at the top of the file
51 #unless($code =~ /^\s*([\/{#]|sub|BEGIN|END)/) {
52 0 ▶ unless($code =~ /^(\s*#.*)*\s*([\/{]|sub|BEGIN|END|\w+:\s*\/)/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code =~ m[^(\s*#.*)*\s*([/{]|sub|BEGIN|END|\w+:\s*/)]) 0 0 0
53 0 ▶ print "MOK: adding implicit braces\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Mok::DEBUG 0 0 0
54 0 $code = "{$code}"; # add implicit brackets for simple one-liners
55 }
56 #print "code = '$code'\n";
57 # (patt opt?)? code | sub code
58 my @toks = extract_multiple(my $c = $code,
59 [
60 { 'Chemistry::Mok::Comment' =>
61 qr/\s*#.*\s*/ },
62 { 'Chemistry::Mok::Patt' =>
63 0 ▶ sub { scalar extract_delimited($_[0],'/') } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 { 'Chemistry::Mok::Sub' =>
65 qr/\s*(?:END|BEGIN|sub\s+\w+)\s*/ },
66 { 'Chemistry::Mok::Block' =>
67 0 ▶ sub { scalar extract_codeblock($_[0],'{') } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 { 'Chemistry::Mok::PattLang' =>
69 qr/(\s*\w+):(?=\s*\/)/ },
70 { 'Chemistry::Mok::Opts' =>
71 qr/[gopGOP]+/ },
72 ],
73 );
74 0 ▶ die "Mok: error extracting: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
75 0 ▶ print "MOK: TOKENS:\n", Dumper(\@toks), "\nCODE:<<<<$code>>>>\n\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Mok::DEBUG 0 0 0
76 if $DEBUG;
77 0 @toks;
78 }
79  
80 0 ▶sub parse {
Subroutine parse: not called CC=23 SCAR=63.1
Pod undocumented
81 0 my ($self, @toks) = @_;
82  
83 0 my (@subs, @blocks);
84 0 for my $tok (@toks) {
85 0 ▶ blessed $tok or die "unparsable token '$tok'\n";
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($tok) or die "unparsable token '${tok}'\n" 0 0 0
86 }
87  
88 ### new parser
89  
90 0 my $st = 1;
91 0 my ($patt, $opts, $block, $sub, $pattlang) = ('') x 5;
92 0 my ($save) = 0;
93 0 my $line;
94 0 my $next_line = 1;
95 0 ▶ while (my $tok = shift @toks) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tok = shift @toks) 0 0 0
96 0 $line = $next_line;
97 0 $next_line += $$tok =~ y/\n//;
98 0 ▶ print "MOK: LINE=$line;\nTOK=<<<<$$tok>>>>;\nNEXT_LINE=$next_line\n\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Mok::DEBUG 0 0 0
99 if $DEBUG;
100 0 ▶ next if $tok->isa("Chemistry::Mok::Comment");
Branch: 1 branch
BranchTrueFalseTotal
if $tok->isa("Chemistry::Mok::Comment") 0 0 0
101 0 ▶ if ($st == 1) {
Branch: 5 branches
BranchTrueFalseTotal
if ($st == 1) { } 0 0 0
elsif ($st == 2) { } 0 0 0
elsif ($st == 3) { } 0 0 0
elsif ($st == 4) { } 0 0 0
elsif ($st == 5) { } 0 0 0
102 0 ▶ if ($tok->isa("Chemistry::Mok::Block")){
Branch: 4 branches
BranchTrueFalseTotal
if ($tok->isa('Chemistry::Mok::Block')) { } 0 0 0
elsif ($tok->isa('Chemistry::Mok::Sub')) { } 0 0 0
elsif ($tok->isa('Chemistry::Mok::PattLang')) { } 0 0 0
elsif ($tok->isa('Chemistry::Mok::Patt')) { } 0 0 0
103 0 $block = $$tok, $save = 1;
104 } elsif ($tok->isa("Chemistry::Mok::Sub")) {
105 0 $sub = $$tok, $st = 5, next;
106 } elsif ($tok->isa("Chemistry::Mok::PattLang")) {
107 0 $pattlang = $$tok, $st = 4, next;
108 } elsif ($tok->isa("Chemistry::Mok::Patt")) {
109 0 $patt = $$tok, $st = 2, next;
110 }
111 } elsif ($st == 2) {
112 0 ▶ if ($tok->isa("Chemistry::Mok::Block")){
Branch: 2 branches
BranchTrueFalseTotal
if ($tok->isa('Chemistry::Mok::Block')) { } 0 0 0
elsif ($tok->isa('Chemistry::Mok::Opts')) { } 0 0 0
113 0 $block = $$tok, $save = 1;
114 } elsif ($tok->isa("Chemistry::Mok::Opts")){
115 0 $opts = $$tok, $st = 3, next;
116 }
117 } elsif ($st == 3) {
118 0 ▶ if ($tok->isa("Chemistry::Mok::Block")){
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->isa("Chemistry::Mok::Block")) 0 0 0
119 0 $block = $$tok, $save = 1;
120 }
121 } elsif ($st == 4) {
122 0 ▶ if ($tok->isa("Chemistry::Mok::Patt")){
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->isa("Chemistry::Mok::Patt")) 0 0 0
123 0 $patt = $$tok, $st = 2, next;
124 }
125 } elsif ($st == 5) {
126 0 ▶ if ($tok->isa("Chemistry::Mok::Block")){
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->isa("Chemistry::Mok::Block")) 0 0 0
127 0 $block = $$tok, $save = 1;
128 }
129 } else {
130 0 confess "unknown state '$st'";
131 }
132 0 ▶ if ($save) { # save block and go back to state 1
Branch: 1 branch
BranchTrueFalseTotal
if ($save) { } 0 0 0
133 0 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) { } 0 0 0
134 0 push @subs, { block => "$sub $$tok", line => $line };
135 } else {
136 0 push @blocks, { patt => $patt, opts => $opts,
137 pattlang => $pattlang, block => $$tok,
138 line => $line};
139 }
140 0 $patt = $opts = $pattlang = $block = $sub = '';
141 0 $st = 1, $save = 0, next;
142 } else {
143 0 die "unexpected token '$$tok' (type '" . ref($tok) . "'\n";
144 }
145 }
146 0 ▶ print "MOK: BLOCKS\n", Dumper(\@blocks), "\nSUBS:\n", Dumper(\@subs), "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Mok::DEBUG 0 0 0
147 if $DEBUG;
148  
149 0 \@subs, \@blocks;
150 }
151  
152 0 ▶sub compile_subs {
Subroutine compile_subs: not called CC=4 SCAR=30.0
Pod undocumented
153 0 my ($self, @subs) = @_;
154 0 my $pack = $self->{package};
155  
156 0 for my $sub (@subs) {
157 0 my $code = <<END;
158 package Chemistry::Mok::UserCode::$pack;
159 no strict;
160 no warnings;
161 #line $sub->{line} "mok code"
162 $sub->{block}
163 END
164 0 ▶ print "MOK: COMPILING SUB: <<<<$code>>>>\n\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Mok::DEBUG 0 0 0
165 0 eval $code;
166 0 ▶ die "Mok: error compiling sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
167 }
168 }
169  
170 0 ▶sub compile_blocks {
Subroutine compile_blocks: not called CC=9 SCAR=45.0
Pod undocumented
171 0 my ($self, @blocks) = @_;
172 0 my $pack = $self->{package};
173 0 my $format = $self->{pattern_format};
174 0 my @compiled_blocks;
175  
176 0 for my $block (@blocks) {
177 #use Data::Dumper; print Dumper $block;
178 0 my $code = <<END;
179 package Chemistry::Mok::UserCode::$pack;
180 no strict;
181 no warnings;
182 sub {
183 my (\$mol, \$file, \$match, \$patt) = \@_;
184 my (\$MOL, \$FILE, \$MATCH, \$PATT, \$FH) = \@_;
185 my (\@A) = \$MATCH ? \$MATCH->atom_map : \$MOL->atoms;
186 my (\@B) = \$MATCH ? \$MATCH->bond_map : \$MOL->bonds;
187 #line $block->{line} "mok code"
188 $block->{block};
189 }
190 END
191 0 ▶ print "MOK: COMPILING BLOCK: <<<<$code>>>>\n\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Mok::DEBUG 0 0 0
192 0 my $sub = eval $code;
193 0 ▶ die "Mol: Error compiling block: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
194  
195 0 my ($patt, $patt_str);
196 0 ▶ if ($block->{patt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->{"patt"}) 0 0 0
197 0 $block->{patt} =~ m#^/(.*)/$#;
198 0 $patt_str = $1;
199 $patt = Chemistry::Pattern->parse($patt_str,
200 0 ▶ format => $block->{pattlang} || $format);
Condition0%0/3
$block->{'pattlang'} || $format
l!l&&r!l&&!r
000
MC/DC0%0/2
$block->{'pattlang'} || $format
$block->{'pattlang'} $format
Truth table0%0/3
A || B
A= $block->{'pattlang'}B= $format
ABresult
1X1
011
000
201 0 ▶ $patt->attr(global => 1) if $block->{opts} =~ /g/;
Branch: 1 branch
BranchTrueFalseTotal
if $block->{"opts"} =~ /g/ 0 0 0
202 0 ▶ $patt->options(overlap => 0) if $block->{opts} =~ /O/;
Branch: 1 branch
BranchTrueFalseTotal
if $block->{"opts"} =~ /O/ 0 0 0
203 0 ▶ $patt->options(permute => 1) if $block->{opts} =~ /p/;
Branch: 1 branch
BranchTrueFalseTotal
if $block->{"opts"} =~ /p/ 0 0 0
204 }
205 0 push @compiled_blocks, {'sub' => $sub,
206 patt => $patt, patt_str => $patt_str};
207 }
208 0 \@compiled_blocks;
209 }
210  
211 =item Chemistry::Mok->new($code, %options)
212  
213 Compile the code and return a Chemistry::Mok object. Available options:
214  
215 =over
216  
217 =item C<package>
218  
219 If the C<package> option is given, the code runs in the
220 Chemistry::Mok::UserCode::$options{package} package instead of the
221 Chemistry::Mok::UserCode::Default package. Specifying a package name is
222 recommended if you have more than one mok object and you are using global
223 varaibles, in order to avoid namespace clashes.
224  
225 =item C<pattern_format>
226  
227 The name of the format which will be used for parsing slash-delimited patterns
228 that don't define an explicit format. Mok versions until 0.16 only used the
229 'smiles' format, but newer versions can use other formats such as 'smarts',
230 'midas', 'formula_pattern', and 'sln', if available. The default is 'smarts'.
231  
232 =back
233  
234 =cut
235  
236 8 ▶sub new {
Subroutine new: called CC=4 SCAR=17.3
Pod documented
237 8 my ($class, $code, @a) = @_;
238 8 my %opts;
239  
240 # for backwards compatibility with Chemistry::Mok->new($code, $package)
241 8 ▶ unshift @a, "package" if (@a == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @a == 1 0 8 8
242 8 %opts = @a;
243
244 my $self = bless {
245 'package' => $opts{package} || "Default",
246 8 ▶ pattern_format => $opts{pattern_format} || "smarts",
Condition50%2/4
$opts{'package'} || 'Default'
l!l
08
$opts{'pattern_format'} || 'smarts'
l!l
80
MC/DC0%0/1
$opts{'package'} || 'Default'
$opts{'package'}
MC/DC0%0/1
$opts{'pattern_format'} || 'smarts'
$opts{'pattern_format'}
Truth table50%1/2
A
A= $opts{'package'}
Aresult
11
00
Truth table50%1/2
A
A= $opts{'pattern_format'}
Aresult
11
00
247 }, $class;
248  
249 8 $self->setup_package;
250 0 my @toks = $self->tokenize($code);
251 0 my ($subs, $blocks) = $self->parse(@toks);
252 0 $self->compile_subs(@$subs);
253 0 $self->{blocks} = $self->compile_blocks(@$blocks);
254
255 0 return $self;
256 }
257  
258 8 ▶sub setup_package {
Subroutine setup_package: called CC=2 SCAR=7.0
Pod undocumented
259 8 my ($self) = @_;
260 8 my $usr_pack = $self->{package};
261 # import convenience functions into the user's namespace
262 0 ▶ eval <<EVAL;
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
263 package Chemistry::Mok::UserCode::$usr_pack;
264 use Chemistry::Atom ':all';
265 use Chemistry::Ring ':all';
266 use Chemistry::Ring::Find ':all';
267 use Chemistry::Bond::Find ':all';
268 use Chemistry::Canonicalize ':all';
269 use Chemistry::InternalCoords::Builder ':all';
270 use Chemistry::Isotope ':all';
271 use Math::VectorReal ':all';
272 use Chemistry::3DBuilder ':all';
273 sub println { print "\@_", "\n" }
274 EVAL
275 8 ▶ die "Mok: error setting up 'Chemistry::Mok::UserCode::$usr_pack' $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 8 0 8
276 }
277  
278 =item $mok->run($options, @args)
279  
280 Run the code on the filenames contained in @args. $options is a hash reference
281 with runtime options. Available options:
282  
283 =over
284  
285 =item build_3d
286  
287 Generate 3D coordinates using Chemistry::3DBuilder.
288  
289 =item aromatize
290  
291 "Aromatize" each molecule as it is read. This is needed for example for
292 matching SMARTS patterns that use aromaticity or ring primitives.
293  
294 =item delete_dummies
295  
296 Delete dummy atoms after reading each molecule. A dummy atom is defined as an
297 atom with an unknown symbol (i.e., it doesn't appear on the periodic table), or
298 an atomic number of zero.
299  
300 =item find_bonds
301  
302 If set to a true value, find bonds. Use it when reading files with no bond
303 information but 3D coordinates to detect the bonds if needed (for example, if
304 you want to do match a pattern that includes bonds). If the file has explicit
305 bonds, mok will not try to find the bonds, but it will reassign the bond orders
306 from scratch.
307  
308 =item format
309  
310 The format used when calling $mol_class->read. If not given, $mol_class->read
311 tries to identify the format automatically.
312  
313 =item mol_class
314  
315 The molecule class used for reading the files. Defaults to Chemistry::Mol.
316  
317 =back
318  
319 =cut
320  
321 0 ▶sub run {
Subroutine run: not called CC=15 SCAR=54.8
Pod documented
322 0 my ($self, $opt, @args) = @_;
323 # MAIN LOOP
324 0 ▶ my $mol_class = $opt->{mol_class} || "Chemistry::Mol";
Condition0%0/2
$opt->{'mol_class'} || 'Chemistry::Mol'
l!l
00
MC/DC0%0/1
$opt->{'mol_class'} || 'Chemistry::Mol'
$opt->{'mol_class'}
Truth table0%0/2
A
A= $opt->{'mol_class'}
Aresult
11
00
325 0 FILE: for my $file (@args) {
326 #my (@mols) = $mol_class->read(
327 my %reader_opts = (
328 format => $opt->{format},
329 mol_class => $opt->{mol_class},
330 0 );
331 0 my $reader = $mol_class->file(
332 $file,
333 %reader_opts,
334 );
335 0 $reader->open('<');
336 0 $reader->read_header;
337 0 ▶ while (my @mols = $reader->read_mol($reader->fh, %reader_opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@mols) = $reader->read_mol($reader->fh, %reader_opts)) 0 0 0
338 0 MOL: for my $mol (@mols) {
339 0 ▶ if ($opt->{delete_dummies}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"delete_dummies"}) 0 0 0
340 0 $_->delete for grep { ! $_->Z } $mol->atoms;
341 }
342 0 ▶ if ($opt->{find_bonds}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"find_bonds"}) 0 0 0
343 0 ▶ find_bonds($mol) unless $mol->bonds;
Branch: 1 branch
BranchTrueFalseTotal
unless $mol->bonds 0 0 0
344 0 assign_bond_orders($mol);
345 }
346 0 ▶ if ($opt->{aromatize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"aromatize"}) 0 0 0
347 0 aromatize_mol($mol);
348 }
349 0 ▶ if ($opt->{build_3d}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"build_3d"}) 0 0 0
350 0 build_3d($mol);
351 }
352 0 BLOCK: for my $block (@{$self->{blocks}}) {
353 my ($code_block, $patt, $patt_str) =
354 0 @{$block}{qw(sub patt patt_str)};
355 0 ▶ if ($patt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($patt) { } 0 0 0
356 0 ▶ MATCH: while ($patt->match($mol)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($patt->match($mol)) 0 0 0
357 0 $code_block->($mol, $file, $patt,
358 $patt_str, $reader->fh);
359 0 ▶ last unless $patt->attr('global');
Branch: 1 branch
BranchTrueFalseTotal
unless $patt->attr("global") 0 0 0
360 }
361 } else {
362 0 $code_block->($mol, $file, $patt,
363 $patt_str, $reader->fh);
364 }
365 }
366 }
367 }
368 }
369 }
370  
371 9 1;
372  
373 __END__
« blib/lib/Chemistry/Bond/Find.pm ↑ Summary blib/lib/Chemistry/Pattern.pm »