blib/lib/Chemistry/File/SMILES.pm

filter: Statementstmt 46.4% 125 / 269 Branchbran 27.1% 32 / 118 Conditioncond 11.7% 6 / 51 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 66.6% 22 / 33 Podpod 22.7% 5 / 22 totaltotal 36.4% 190 / 521 CC 88 SCAR 44.9
CC
88
Cov
37%
format_atom
342.0
write_string
156.0
branch
156.0
CRAP 88.8
« blib/lib/Chemistry/Pattern.pm ↑ Summary blib/lib/Chemistry/File/Mopac.pm »
1 package Chemistry::File::SMILES;
2  
3 11 $VERSION = "0.44";
4 # $Id: SMILES.pm,v 1.14 2005/03/29 23:48:34 itubert Exp $
5  
6 11 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use base "Chemistry::File";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Chemistry::Mol;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Chemistry::Bond::Find 'assign_bond_orders';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 =head1 NAME
17  
18 Chemistry::File::SMILES - SMILES linear notation parser/writer
19  
20 =head1 SYNOPSYS
21  
22 #!/usr/bin/perl
23 use Chemistry::File::SMILES;
24  
25 # parse a SMILES string
26 my $s = 'C1CC1(=O)[O-]';
27 my $mol = Chemistry::Mol->parse($s, format => 'smiles');
28  
29 # print a SMILES string
30 print $mol->print(format => 'smiles');
31  
32 # print a unique (canonical) SMILES string
33 print $mol->print(format => 'smiles', unique => 1);
34  
35 # parse a SMILES file
36 my @mols = Chemistry::Mol->read("file.smi", format => 'smiles');
37  
38 # write a multiline SMILES file
39 Chemistry::Mol->write("file.smi", mols => \@mols);
40  
41  
42 =head1 DESCRIPTION
43  
44 This module parses a SMILES (Simplified Molecular Input Line Entry
45 Specification) string. This is a File I/O driver for the PerlMol project.
46 L<http://www.perlmol.org/>. It registers the 'smiles' format with
47 Chemistry::Mol.
48  
49 This parser interprets anything after whitespace as the molecule's name;
50 for example, when the following SMILES string is parsed, $mol->name will be
51 set to "Methyl chloride":
52  
53 CCl Methyl chloride
54  
55 The name is not included by default on output. However, if the C<name> option
56 is defined, the name will be included after the SMILES string, separated by a
57 tab.
58  
59 print $mol->print(format => 'smiles', name => 1);
60  
61 =head2 Multiline SMILES and SMILES files
62  
63 A file or string can contain multiple molecules, one per line.
64  
65 CCl Methyl chloride
66 CO Methanol
67  
68 Files with the extension '.smi' are assumed to have this format.
69  
70 =head2 Atom Mapping Numbers
71  
72 As an extension for reaction processing, SMILES strings may have atom mapping
73 numbers, which are introduced after a colon in a bracketed atom. For example,
74 [C:1]. The mapping number need not be unique. This module reads the mapping
75 numbers and stores them as the name of the atom ($atom->name).
76  
77 On output, atom names are not included by default. See the C<number> and
78 C<auto_number> options below for ways of including them.
79  
80 head1 OPTIONS
81  
82 The following options are supported in addition to the options mentioned for
83 L<Chemistry::File>, such as C<mol_class>, C<format>, and C<fatal>.
84  
85 =over
86  
87 =item aromatic
88  
89 On output, detect aromatic atoms and bonds by means of the Chemistry::Ring
90 module, and represent the organic aromatic atoms with lowercase symbols.
91  
92 =item unique
93  
94 When used on output, canonicalize the structure if it hasn't been canonicalized
95 already and generate a unique SMILES string. This option implies "aromatic".
96  
97 =item number
98  
99 For atoms that have a defined name, print the name as the "atom number". For
100 example, if an ethanol molecule has the name "42" for the oxygen atom and the
101 other atoms have undefined names, the output would be:
102  
103 CC[OH:42]
104  
105 =item auto_number
106  
107 When used on output, number all the atoms explicitly and sequentially. The
108 output for ethanol would look something like this:
109  
110 [CH3:1][CH2:2][OH:3]
111  
112 =item name
113  
114 Include the molecule name on output, as described in the previous section.
115  
116 =item kekulize
117  
118 When used on input, assign single or double bond orders to "aromatic" or
119 otherwise unspecified bonds (i.e., generate the Kekule structure). If false,
120 the bond orders will remain single. This option is true by default. This uses
121 C<assign_bond_orders> from the L<Chemistry::Bond::Find> module.
122  
123 =back
124  
125 =cut
126  
127 # INITIALIZATION
128 11 Chemistry::Mol->register_format('smiles');
129 11 my $Smiles_parser = __PACKAGE__->new_parser;
130  
131 #=begin comment
132 #
133 #=over
134 #
135 #=cut
136  
137 0 ▶sub file_is {
Subroutine file_is: not called CC=1 SCAR=6.9
Pod documented
138 0 my $self = shift;
139 0 $self->name_is(@_);
140 }
141  
142 0 ▶sub name_is {
Subroutine name_is: not called CC=1 SCAR=6.9
Pod documented
143 0 my ($self, $name) = @_;
144 0 $name =~ /\.smi/;
145 }
146  
147 0 ▶sub slurp_mol {
Subroutine slurp_mol: not called CC=1 SCAR=6.9
Pod documented
148 0 my ($self, $fh) = @_;
149 0 scalar <$fh>;
150 }
151  
152 28 ▶sub read_mol {
Subroutine read_mol: called CC=5 SCAR=16.5
Pod documented
153 28 my ($self, $fh, %opts) = @_;
154 28 %opts = (kekulize => 1, %opts);
155 28 ▶ my $mol_class = $opts{mol_class} || "Chemistry::Mol";
Condition50%1/2
$opts{'mol_class'} || 'Chemistry::Mol'
l!l
280
MC/DC0%0/1
$opts{'mol_class'} || 'Chemistry::Mol'
$opts{'mol_class'}
Truth table50%1/2
A
A= $opts{'mol_class'}
Aresult
11
00
156  
157 28 my $line = <$fh>;
158 28 ▶ return unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 14 14 28
159 14 $line =~ tr/\r\n//d;
160 14 my ($smiles, $name) = split " ", $line, 2;
161  
162 14 my $mol = $mol_class->new;
163 14 ▶ unless ($Smiles_parser->parse($smiles, $mol, \%opts)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Smiles_parser->parse($smiles, $mol, \%opts)) 0 14 14
164 0 warn "error parsing SMILES line '$line'\n";
165 0 $mol = $mol_class->new;
166 }
167 14 $mol->name($name);
168 14 $self->add_implicit_hydrogens($mol);
169 14 ▶ if ($opts{kekulize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"kekulize"}) 14 0 14
170 14 assign_bond_orders($mol, method => "itub", use_coords => 0,
171 scratch => 0, charges => 0);
172 }
173 14 $mol;
174 }
175  
176  
177 ### The contents of the original Chemistry::Smiles module start below
178  
179 11 my $Symbol = qr/
180 s|p|o|n|c|b|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|H|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  
188 11 my $Simple_symbol = qr/Br|Cl|B|C|N|O|P|S|F|I|H|s|p|o|n|c|b/;
189  
190 11 my $Bond = qr/(?:[-=#:.\/\\])?/;
191 11 my $Simple_atom = qr/($Simple_symbol)/; #3
192 11 my $Complex_atom = qr/
193 (?:
194 \[ #begin atom
195 (\d*) #4 isotope
196 ($Symbol) #5 symbol
197 (\@{0,2}) #6 chirality
198 (?:(H\d*))? #7 H-count
199 (\+{2,}|-{2,}|\+\d*|-\d*)? #8 charge
200 (?::(\d+))? #9 name
201 \] #end atom
202 )
203 /x;
204  
205 11 my $Digits = qr/(?:($Bond)(?:\d|%\d\d))*/;
206 11 my $Chain = qr/
207 \G( #1
208 (?:
209 ($Bond) #2
210 (?:$Simple_atom|$Complex_atom) #3-9
211 ($Digits) #10
212 )
213 |\(
214 |\)
215 |.+
216 )
217 /x;
218  
219 11 my $digits_re = qr/($Bond)(\%\d\d|\d)/;
220  
221 11 my %type_to_order = (
222 '-' => 1,
223 '=' => 2,
224 '#' => 3,
225 '/' => 1,
226 '\\' => 1,
227 '' => 1, # not strictly true
228 '.' => 0,
229 );
230  
231 11 my %ORGANIC_ELEMS = (
232 Br => 1, Cl => 1, B => 3, C => 4, N => 3, O => 2, P => 3, S => 2,
233 F => 1, I => 1, s => 1, p => 1, o => 1, n => 1, c => 1, b => 1,
234 );
235  
236 #=item Chemistry::Smiles->new([add_atom => \&sub1, add_bond => \&sub2])
237 #
238 #Create a SMILES parser. If the add_atom and add_bond subroutine references
239 #are given, they will be called whenever an atom or a bond needs to be added
240 #to the molecule. If they are not specified, default methods, which
241 #create a Chemistry::Mol object, will be used.
242 #
243 #=cut
244  
245 11 ▶sub new_parser {
Subroutine new_parser: called CC=3 SCAR=11.7
Pod undocumented
246 11 my $class = shift;
247 11 my %opts = @_;
248 my $self = bless {
249 add_atom => $opts{add_atom} || \&add_atom,
250 11 ▶ add_bond => $opts{add_bond} || \&add_bond,
Condition50%2/4
$opts{'add_atom'} || \&Chemistry::File::SMILES::add_atom
l!l
011
$opts{'add_bond'} || \&Chemistry::File::SMILES::add_bond
l!l
011
MC/DC0%0/1
$opts{'add_atom'} || \&Chemistry::File::SMILES::add_atom
$opts{'add_atom'}
MC/DC0%0/1
$opts{'add_bond'} || \&Chemistry::File::SMILES::add_bond
$opts{'add_bond'}
Truth table50%1/2
A
A= $opts{'add_atom'}
Aresult
11
00
Truth table50%1/2
A
A= $opts{'add_bond'}
Aresult
11
00
251 }, $class;
252 }
253  
254 #=item $obj->parse($string, $mol)
255 #
256 #Parse a Smiles $string. $mol is a "molecule state object". It can be anything;
257 #the parser doesn't do anything with it except sending it as the first parameter
258 #to the callback functions. If callback functions were not provided when
259 #constructing the parser object, $mol must be a Chemistry::Mol object, because
260 #that's what the default callback functions require.
261 #
262 #=cut
263  
264 14 ▶sub parse {
Subroutine parse: called CC=12 SCAR=26.2
Pod undocumented
265 14 my ($self, $s, $mol, $opts) = @_;
266 14 $self->{stack} = [ undef ];
267 14 $self->{digits} = {};
268  
269 14 eval {
270 14 ▶ while ($s =~ /$Chain/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /$Chain/g) 48 14 62
271 #my @a = ($1, $2, $3, $4, $5, $6, $7, $8);
272 #print Dumper(\@a);
273 48 my ($all, $bnd, $sym, $iso, $sym2, $chir, $hcnt, $chg, $name, $dig)
274 = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
275 48 ▶ if ($all eq '(') {
Branch: 4 branches
BranchTrueFalseTotal
if ($all eq '(') { } 4 44 48
elsif ($all eq ')') { } 4 40 44
elsif ($sym) { } 38 2 40
elsif ($sym2) { } 2 0 2
276 4 $self->start_branch();
277 } elsif ($all eq ')') {
278 4 $self->end_branch();
279 } elsif ($sym) { # Simple atom
280 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
281 38 my @digs = parse_digits($dig);
282 38 $self->atom($mol, $bnd, '', $sym, '', undef, '', \@digs);
283 } elsif ($sym2) { # Complex atom
284 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
285 2 my @digs = parse_digits($dig);
286 2 ▶ if ($hcnt eq 'H') {
Branch: 1 branch
BranchTrueFalseTotal
if ($hcnt eq 'H') { } 0 2 2
287 0 $hcnt = 1;
288 } else {
289 2 $hcnt =~ s/H//;
290 }
291 2 ▶ unless ($chg =~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chg =~ /\d/) 2 0 2
292 2 ▶ $chg = ($chg =~ /-/) ? -length($chg) : length($chg);
Branch: 1 branch
BranchTrueFalseTotal
$chg =~ /-/ ? : 0 2 2
293 }
294 2 ▶ $self->atom($mol, $bnd, $iso, $sym2, $chir, $hcnt || 0,
Condition50%1/2
$hcnt || 0
l!l
02
MC/DC0%0/1
$hcnt || 0
$hcnt
Truth table50%1/2
A
A= $hcnt
Aresult
11
00
295 $chg, \@digs, $name);
296 } else {
297 0 die "SMILES ERROR: '$all in $s'\n";
298 }
299 }
300 };
301 # clean up to avoid memory leak
302 14 $self->{stack} = undef;
303 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 14 14
304 0 ▶ croak $@ if $opts->{fatal};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"fatal"} 0 0 0
305 0 return;
306 }
307 14 $mol;
308 }
309  
310 40 ▶sub parse_digits {
Subroutine parse_digits: called CC=3 SCAR=12.7
Pod undocumented
311 40 my ($dig) = @_;
312 40 my @digs;
313 40 ▶ while ($dig && $dig =~ /$digits_re/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dig and $dig =~ /$digits_re/g) 0 40 40
Condition33%1/3
$dig and $dig =~ /$digits_re/g
!ll&&!rl&&r
4000
MC/DC0%0/2
$dig and $dig =~ /$digits_re/g
$dig $dig =~ /$digits_re/g
Truth table33%1/3
A and B
A= $digB= $dig =~ /$digits_re/g
ABresult
0X0
100
111
314 0 push @digs, {bnd=>$1, dig=>$2};
315 }
316 40 @digs;
317 }
318  
319 40 ▶sub atom {
Subroutine atom: called CC=8 SCAR=33.3
Pod undocumented
320 40 my $self = shift;
321 40 my ($mol,$bnd,$iso,$sym,$chir,$hcount,$chg,$digs,$name) = @_;
322 #{no warnings; local $" = ','; print "atom(@_)\n"}
323 40 my $a = $self->{add_atom}($mol,$iso,$sym,$chir,$hcount,$chg,$name);
324 40 ▶ if($self->{stack}[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stack"}[-1]) 26 14 40
325 26 $self->{add_bond}($mol, $bnd, $self->{stack}[-1], $a);
326 }
327 40 for my $dig (@$digs) {
328 0 ▶ if ($self->{digits}{$dig->{dig}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'digits'}{$dig->{'dig'}}) { } 0 0 0
329 0 ▶ if ($dig->{bnd} && $self->{digits}{$dig->{dig}}{bnd}
Branch: 1 branch
BranchTrueFalseTotal
if ($dig->{"bnd"} and $self->{"digits"}{$dig->{"dig"}}{"bnd"} and $dig->{"bnd"} ne $self->{"digits"}{$dig->{"dig"}}{"bnd"}) 0 0 0
Condition0%0/6
$dig->{"bnd"} and $self->{"digits"}{$dig->{"dig"}}{"bnd"} and $dig->{"bnd"} ne $self->{"digits"}{$dig->{"dig"}}{"bnd"}
!ll&&!rl&&r
000
$dig->{"bnd"} and $self->{"digits"}{$dig->{"dig"}}{"bnd"}
!ll&&!rl&&r
000
MC/DC0%0/3
$dig->{"bnd"} and $self->{"digits"}{$dig->{"dig"}}{"bnd"} and $dig->{"bnd"} ne $self->{"digits"}{$dig->{"dig"}}{"bnd"}
$dig->{"bnd"} $self->{"digits"}{$dig->{"dig"}}{"bnd"} $dig->{"bnd"} ne $self->{"digits"}{$dig->{"dig"}}{"bnd"}
Truth table0%0/4
A and B and C
A= $dig->{"bnd"}B= $self->{"digits"}{$dig->{"dig"}}{"bnd"}C= $dig->{"bnd"} ne $self->{"digits"}{$dig->{"dig"}}{"bnd"}
ABCresult
0XX0
10X0
1100
1111
330 && $dig->{bnd} ne $self->{digits}{$dig->{dig}}{bnd}){
331 0 die "SMILES: Inconsistent ring closure\n";
332 }
333 $self->{add_bond}($mol,
334 $dig->{bnd} || $self->{digits}{$dig->{dig}}{bnd},
335 0 ▶ $self->{digits}{$dig->{dig}}{atom}, $a);
Condition0%0/3
$dig->{'bnd'} || $self->{'digits'}{$dig->{'dig'}}{'bnd'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$dig->{'bnd'} || $self->{'digits'}{$dig->{'dig'}}{'bnd'}
$dig->{'bnd'} $self->{'digits'}{$dig->{'dig'}}{'bnd'}
Truth table0%0/3
A || B
A= $dig->{'bnd'}B= $self->{'digits'}{$dig->{'dig'}}{'bnd'}
ABresult
1X1
011
000
336 0 delete $self->{digits}{$dig->{dig}};
337 } else {
338 0 $self->{digits}{$dig->{dig}} = {atom=>$a, bnd=>$dig->{bnd}};
339 }
340 }
341 40 $self->{stack}[-1] = $a;
342 }
343  
344 #=back
345 #
346 #=head1 CALLBACK FUNCTIONS
347 #
348 #=over
349 #
350 #=item $atom = add_atom($mol, $iso, $sym, $chir, $hcount, $chg)
351 #
352 #Called by the parser whenever an atom is found. The first parameter is the
353 #state object given to $obj->parse(). The other parameters are the isotope,
354 #symbol, chirality, hydrogen count, and charge of the atom. Only the symbol is
355 #guaranteed to be defined. Mnemonic: the parameters are given in the same order
356 #that is used in a SMILES string (such as [18OH-]). This callback is expected to
357 #return something that uniquely identifies the atom that was created (it might
358 #be a number, a string, or an object).
359 #
360 #=cut
361  
362 # Default add_atom callback
363 40 ▶sub add_atom {
Subroutine add_atom: called CC=7 SCAR=20.8
Pod undocumented
364 40 my ($mol, $iso, $sym, $chir, $hcount, $chg, $name) = @_;
365 40 my $atom = $mol->new_atom(symbol => ucfirst $sym, name => $name);
366 40 ▶ $iso && $atom->attr('smiles/isotope' => $iso);
Branch: 1 branch
BranchTrueFalseTotal
$iso and $atom->attr("smiles/isotope", $iso) 0 40 40
367 40 ▶ $iso && $atom->mass($iso);
Branch: 1 branch
BranchTrueFalseTotal
$iso and $atom->mass($iso) 0 40 40
368 40 ▶ $chir && $atom->attr('smiles/chirality' => $chir);
Branch: 1 branch
BranchTrueFalseTotal
$chir and $atom->attr("smiles/chirality", $chir) 0 40 40
369 40 ▶ defined $hcount && $atom->hydrogens($hcount);
Branch: 1 branch
BranchTrueFalseTotal
defined $hcount and $atom->hydrogens($hcount) 2 38 40
370 40 ▶ $chg && $atom->formal_charge($chg);
Branch: 1 branch
BranchTrueFalseTotal
$chg and $atom->formal_charge($chg) 0 40 40
371 40 ▶ if ($sym =~ /^[a-z]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sym =~ /^[a-z]/) 0 40 40
372 0 $atom->attr("smiles/aromatic", 1);
373 }
374 40 $atom;
375 }
376  
377 #=item add_bond($mol, $type, $a1, $a2)
378 #
379 #Called by the parser whenever an bond needs to be created. The first parameter
380 #is the state object given to $obj->parse(). The other parameters are the bond
381 #type and the two atoms that need to be bonded. The atoms are identified using
382 #the return values from the add_atom() callback.
383 #
384 #=back
385 #
386 #=end comment
387 #
388 #=cut
389  
390 # Default add_bond callback
391 26 ▶sub add_bond {
Subroutine add_bond: called CC=2 SCAR=7.0
Pod undocumented
392 26 my ($mol, $type, $a1, $a2) = @_;
393 26 ▶ my $order = $type_to_order{$type} or return; # don't add bonds of order 0
Branch: 1 branch
BranchTrueFalseTotal
my $order = $type_to_order{$type} or return 26 0 26
394 26 my $bond = $mol->new_bond(type=>$type, atoms=>[$a1, $a2], order=>$order);
395 26 $bond->attr("smiles/type" => $type);
396 26 $bond;
397 }
398  
399 4 ▶sub start_branch {
Subroutine start_branch: called CC=1 SCAR=0.0
Pod undocumented
400 4 my $self = shift;
401 #print "start_branch\n";
402 4 push @{$self->{stack}}, $self->{stack}[-1];
403 }
404  
405 4 ▶sub end_branch {
Subroutine end_branch: called CC=1 SCAR=0.0
Pod undocumented
406 4 my $self = shift;
407 #print "end_branch\n";
408 4 pop @{$self->{stack}};
409 }
410
411 38 ▶sub calc_implicit_hydrogens {
Subroutine calc_implicit_hydrogens: called CC=4 SCAR=15.0
Pod undocumented
412 38 my ($self, $atom) = @_;
413 11 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
414 38 my $h_count = $ORGANIC_ELEMS{$atom->symbol} - $atom->valence;
415 38 ▶ if ($atom->attr("smiles/aromatic") and $atom->symbol =~ /^[CN]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atom->attr("smiles/aromatic") and $atom->symbol =~ /^[CN]$/) 0 38 38
Condition33%1/3
$atom->attr("smiles/aromatic") and $atom->symbol =~ /^[CN]$/
!ll&&!rl&&r
3800
MC/DC0%0/2
$atom->attr("smiles/aromatic") and $atom->symbol =~ /^[CN]$/
$atom->attr("smiles/aromatic") $atom->symbol =~ /^[CN]$/
Truth table33%1/3
A and B
A= $atom->attr("smiles/aromatic")B= $atom->symbol =~ /^[CN]$/
ABresult
0X0
100
111
416 0 $h_count--;
417 }
418 38 ▶ $h_count = 0 if $h_count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $h_count < 0 0 38 38
419 38 $h_count;
420 }
421  
422 0 ▶sub calc_implicit_hydrogens_2 {
Subroutine calc_implicit_hydrogens_2: not called CC=2 SCAR=17.9
Pod undocumented
423 0 my ($self, $atom) = @_;
424 0 my $h_count = $ORGANIC_ELEMS{$atom->symbol} - $atom->valence
425 + $atom->total_hydrogens;
426 0 ▶ $h_count = 0 if $h_count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $h_count < 0 0 0 0
427 0 $h_count;
428 }
429  
430 14 ▶sub add_implicit_hydrogens {
Subroutine add_implicit_hydrogens: called CC=3 SCAR=11.0
Pod undocumented
431 14 my ($self, $mol) = @_;
432 14 for my $atom ($mol->atoms) {
433 #print "H=".$atom->hydrogens."\n";
434 40 ▶ unless (defined $atom->hydrogens) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $atom->hydrogens) 38 2 40
435 38 my $h_count = $self->calc_implicit_hydrogens($atom);
436 38 $atom->hydrogens($h_count);
437 }
438 }
439 }
440  
441 ##### SMILES WRITER ########
442  
443 0 ▶sub write_string {
Subroutine write_string: not called CC=12 SCAR=50.5
Pod documented
444 0 my ($self, $mol_ref, %opts) = @_;
445  
446 0 my $eol;
447 my @mols;
448 0 ▶ if ($opts{mols}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'mols'}) { } 0 0 0
449 0 @mols = @{$opts{mols}};
450 0 $eol = "\n";
451 } else {
452 0 @mols = $mol_ref;
453 0 $eol = "";
454 }
455  
456 0 my $smiles;
457 0 for my $mol (@mols) {
458 0 $mol = $mol->clone;
459 0 $mol->collapse_hydrogens;
460 0 my @atoms = $mol->atoms;
461  
462 0 ▶ if (@atoms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@atoms) 0 0 0
463 0 my $i;
464 0 ▶ if ($opts{auto_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"auto_number"}) 0 0 0
465 0 $_->name(++$i) for @atoms;
466 0 $opts{number} = 1;
467 }
468 0 ▶ if ($opts{unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"unique"}) 0 0 0
469 0 ▶ unless ($atoms[0]->attr("canon/class")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($atoms[0]->attr("canon/class")) 0 0 0
470 0 require Chemistry::Canonicalize;
471 0 Chemistry::Canonicalize::canonicalize($mol);
472 }
473 0 $opts{aromatic} = 1; # all unique smiles have to be aromatic
474 @atoms = sort {
475 0 $a->attr("canon/class") <=> $b->attr("canon/class")
476 } @atoms;
477 }
478  
479 0 ▶ if ($opts{aromatic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"aromatic"}) 0 0 0
480 0 require Chemistry::Ring;
481 0 Chemistry::Ring::aromatize_mol($mol);
482 }
483  
484 0 my $visited = {};
485 0 my @s;
486 0 for my $atom (@atoms) {
487 0 ▶ next if $visited->{$atom};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$atom} 0 0 0
488 0 my $ring_atoms = {};
489  
490 # first pass to find and number the ring bonds
491 0 $self->find_ring_bonds($mol, \%opts, $atom, undef, {}, $ring_atoms);
492  
493 # second pass to actually generate the SMILES string
494 0 push @s, $self->branch($mol, \%opts, $atom, undef, $visited, $ring_atoms);
495 }
496 0 $smiles .= join '.', @s;
497 }
498  
499 0 ▶ if ($opts{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"name"}) 0 0 0
500 0 $smiles .= "\t" . $mol->name;
501 }
502 0 $smiles .= $eol;
503 }
504 0 return $smiles;
505 }
506  
507 0 ▶sub find_ring_bonds {
Subroutine find_ring_bonds: not called CC=4 SCAR=30.0
Pod undocumented
508 0 my ($self, $mol, $opts, $atom, $from_bond, $visited, $ring_atoms) = @_;
509  
510 0 $visited->{$atom} = 1;
511 0 for my $bn ($self->sorted_bonds_neighbors($atom, $opts)) {
512 0 my $nei = $bn->{to};
513 0 my $bond = $bn->{bond};
514 0 ▶ next if $visited->{$bond};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$bond} 0 0 0
515 0 $visited->{$bond} = 1;
516 0 ▶ if ($visited->{$nei}) { # closed ring
Branch: 1 branch
BranchTrueFalseTotal
if ($visited->{$nei}) { } 0 0 0
517 #print "closing ring\n";
518 0 $ring_atoms->{$nei}++;
519 } else {
520 0 $self->find_ring_bonds($mol, $opts, $nei, $bond, $visited, $ring_atoms);
521 }
522 }
523 }
524  
525 0 ▶sub branch {
Subroutine branch: not called CC=12 SCAR=50.5
Pod undocumented
526 0 my ($self, $mol, $opts, $atom, $from_bond, $visited, $digits) = @_;
527  
528 0 my $prev_branch = "";
529 0 my $smiles;
530 0 $smiles .= $self->bond_symbol($from_bond, $opts);
531 #$digits->{count}++;
532 0 $smiles .= $self->format_atom($atom, $opts);
533 0 ▶ if ($digits->{$atom}) { # opening a ring
Branch: 1 branch
BranchTrueFalseTotal
if ($digits->{$atom}) 0 0 0
534 0 my @d;
535 0 for (1 .. $digits->{$atom}) {
536 0 push @d, $self->next_digit($digits);
537 }
538 0 $digits->{$atom} = \@d;
539 0 ▶ $smiles .= join "", map { $_ < 10 ? $_ : "%$_"} @d;
Branch: 1 branch
BranchTrueFalseTotal
$_ < 10 ? : 0 0 0
540 }
541  
542 0 $visited->{$atom} = 1;
543 0 my @bns = $self->sorted_bonds_neighbors($atom, $opts);
544  
545 0 for my $bn (@bns) {
546 0 my $nei = $bn->{to};
547 0 my $bond = $bn->{bond};
548 0 ▶ next if $visited->{$bond};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$bond} 0 0 0
549 0 ▶ if ($visited->{$nei}) { # closed a ring
Branch: 1 branch
BranchTrueFalseTotal
if ($visited->{$nei}) 0 0 0
550 0 my $digit = shift @{$digits->{$nei}};
551 0 $smiles .= $self->bond_symbol($bond, $opts);
552 0 ▶ $smiles .= $digit < 10 ? $digit : "%$digit";
Branch: 1 branch
BranchTrueFalseTotal
$digit < 10 ? : 0 0 0
553 0 $digits->{used_digits}[$digit] = 0; # free for future use
554 0 $visited->{$bond} = 1;
555 }
556 }
557
558 0 for my $bn (@bns) {
559 0 my $nei = $bn->{to};
560 0 my $bond = $bn->{bond};
561 0 ▶ next if $visited->{$bond};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$bond} 0 0 0
562 0 $visited->{$bond} = 1;
563 0 ▶ unless ($visited->{$nei}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($visited->{$nei}) 0 0 0
564 0 my $branch = $self->branch($mol, $opts, $nei, $bond, $visited, $digits);
565 0 ▶ if ($prev_branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_branch) 0 0 0
566 0 $smiles .= "($prev_branch)";
567 }
568 0 $prev_branch = $branch;
569 }
570 }
571 0 $smiles .= "$prev_branch";
572 0 $smiles;
573 }
574  
575 0 ▶sub next_digit {
Subroutine next_digit: not called CC=3 SCAR=24.8
Pod undocumented
576 0 my ($self, $digits) = @_;
577 0 ▶ for (my $i = 1; $i < 100; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 100) 0 0 0
578 0 ▶ unless ($digits->{used_digits}[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($digits->{"used_digits"}[$i]) 0 0 0
579 0 $digits->{used_digits}[$i] = 1; # mark as used
580 0 return $i;
581 }
582 }
583 0 die "no more available smiles digits!"; # shouldn't happen
584 }
585  
586 0 ▶sub sorted_bonds_neighbors {
Subroutine sorted_bonds_neighbors: not called CC=2 SCAR=17.9
Pod undocumented
587 0 my ($self, $atom, $opts) = @_;
588 0 my @bn = $atom->bonds_neighbors;
589 0 ▶ if ($opts->{unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"unique"}) 0 0 0
590 @bn = sort {
591 0 $a->{to}->attr("canon/class") <=> $b->{to}->attr("canon/class")
592 } @bn;
593 }
594 0 @bn;
595 }
596  
597 11 my %ORDER_TO_TYPE = (
598 2 => '=', 1 => '', 3 => '#',
599 );
600  
601 0 ▶sub bond_symbol {
Subroutine bond_symbol: not called CC=4 SCAR=30.0
Pod undocumented
602 0 my ($self, $bond, $opts) = @_;
603 0 ▶ return '' unless $bond;
Branch: 1 branch
BranchTrueFalseTotal
unless $bond 0 0 0
604 0 ▶ return '' if $opts->{aromatic} && $bond->aromatic;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"aromatic"} and $bond->aromatic 0 0 0
Condition0%0/3
$opts->{"aromatic"} and $bond->aromatic
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"aromatic"} and $bond->aromatic
$opts->{"aromatic"} $bond->aromatic
Truth table0%0/3
A and B
A= $opts->{"aromatic"}B= $bond->aromatic
ABresult
0X0
100
111
605 0 return $ORDER_TO_TYPE{$bond->order};
606 }
607  
608 0 ▶sub format_atom {
Subroutine format_atom: not called CC=18 SCAR=58.3
Pod undocumented
609 0 my ($self, $atom, $opts) = @_;
610  
611 0 my $symbol = $atom->symbol;
612 0 ▶ $symbol = lc $symbol if $opts->{aromatic} && $atom->aromatic;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"aromatic"} and $atom->aromatic 0 0 0
Condition0%0/3
$opts->{"aromatic"} and $atom->aromatic
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"aromatic"} and $atom->aromatic
$opts->{"aromatic"} $atom->aromatic
Truth table0%0/3
A and B
A= $opts->{"aromatic"}B= $atom->aromatic
ABresult
0X0
100
111
613 0 my $s = $symbol;
614  
615 # unless atom is "simple"...
616 0 ▶ if (!$ORGANIC_ELEMS{$atom->symbol} || $atom->formal_charge
Branch: 1 branch
BranchTrueFalseTotal
if (not $ORGANIC_ELEMS{$atom->symbol} or $atom->formal_charge or $atom->total_hydrogens != $self->calc_implicit_hydrogens_2($atom) or $opts->{"number"} and defined $atom->name) 0 0 0
Condition0%0/12
not $ORGANIC_ELEMS{$atom->symbol} or $atom->formal_charge or $atom->total_hydrogens != $self->calc_implicit_hydrogens_2($atom) or $opts->{"number"} and defined $atom->name
l!l&&r!l&&!r
000
not $ORGANIC_ELEMS{$atom->symbol} or $atom->formal_charge or $atom->total_hydrogens != $self->calc_implicit_hydrogens_2($atom)
l!l&&r!l&&!r
000
not $ORGANIC_ELEMS{$atom->symbol} or $atom->formal_charge
l!l&&r!l&&!r
000
$opts->{"number"} and defined $atom->name
!ll&&!rl&&r
000
MC/DC0%0/5
not $ORGANIC_ELEMS{$atom->symbol} or $atom->formal_charge or $atom->total_hydrogens != $self->calc_implicit_hydrogens_2($atom) or $opts->{"number"} and defined $atom->name
not $ORGANIC_ELEMS{$atom->symbol} $atom->formal_charge $atom->total_hydrogens != $self->calc_implicit_hydrogens_2($atom) $opts->{"number"} defined $atom->name
Truth table0%0/6
not(A) or B or C or D and E
A= not $ORGANIC_ELEMS{$atom->symbol}B= $atom->formal_chargeC= $atom->total_hydrogens != $self->calc_implicit_hydrogens_2($atom)D= $opts->{"number"}E= defined $atom->name
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
617 || $atom->total_hydrogens != $self->calc_implicit_hydrogens_2($atom)
618 || ($opts->{number} && defined $atom->name)
619 ) {
620 # "complex atom"; bracketed
621 0 my $h_count = $atom->hydrogens;
622 0 ▶ my $charge = $atom->formal_charge || '';
Condition0%0/2
$atom->formal_charge || ''
l!l
00
MC/DC0%0/1
$atom->formal_charge || ''
$atom->formal_charge
Truth table0%0/2
A
A= $atom->formal_charge
Aresult
11
00
623 0 ▶ my $iso = $atom->attr("smiles/isotope") || '';
Condition0%0/2
$atom->attr('smiles/isotope') || ''
l!l
00
MC/DC0%0/1
$atom->attr('smiles/isotope') || ''
$atom->attr('smiles/isotope')
Truth table0%0/2
A
A= $atom->attr('smiles/isotope')
Aresult
11
00
624 0 my $number = '';
625  
626 0 ▶ if ($charge and abs($charge) > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($charge and abs $charge > 1) { } 0 0 0
elsif ($charge) { } 0 0 0
Condition0%0/3
$charge and abs $charge > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$charge and abs $charge > 1
$charge abs $charge > 1
Truth table0%0/3
A and B
A= $chargeB= abs $charge > 1
ABresult
0X0
100
111
627 0 $charge = sprintf("%+d", $charge);
628 } elsif ($charge) {
629 0 ▶ $charge = $charge > 0 ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$charge > 0 ? : 0 0 0
630 }
631  
632 0 ▶ $h_count = $h_count ? ($h_count > 1 ? "H$h_count" : 'H') : '';
Branch: 2 branches
BranchTrueFalseTotal
$h_count ? : 0 0 0
$h_count > 1 ? : 0 0 0
633  
634 0 ▶ $number = ':' . $atom->name if $opts->{number} and defined $atom->name;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"number"} and defined $atom->name 0 0 0
Condition0%0/3
$opts->{"number"} and defined $atom->name
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"number"} and defined $atom->name
$opts->{"number"} defined $atom->name
Truth table0%0/3
A and B
A= $opts->{"number"}B= defined $atom->name
ABresult
0X0
100
111
635  
636 0 $s = "[$iso$symbol$h_count$charge$number]";
637 }
638 0 $s;
639 }
640  
641  
642 11 1;
643  
644 =head1 CAVEATS
645  
646 Stereochemistry is not supported! Stereochemical descriptors such as @, @@, /,
647 and \ will be silently ignored on input, and will certainly not be produced on
648 output.
649  
650 Reading branches that start before an atom, such as (OC)C, which should be
651 equivalent to C(OC) and COC, according to some variants of the SMILES
652 specification. Many other tools don't implement this rule either.
653  
654 The kekulize option works by increasing the bond orders of atoms that don't
655 have their usual valences satisfied. This may cause problems if you have atoms
656 with explicitly low hydrogen counts.
657  
658 =head1 VERSION
659  
660 0.44
661  
662 =head1 SEE ALSO
663  
664 L<Chemistry::Mol>, L<Chemistry::File>
665  
666 The SMILES Home Page at http://www.daylight.com/dayhtml/smiles/
667  
668 The Daylight Theory Manual at
669 http://www.daylight.com/dayhtml/doc/theory/theory.smiles.html
670  
671 The PerlMol website L<http://www.perlmol.org/>
672  
673 =head1 AUTHOR
674  
675 Ivan Tubert-Brohman E<lt>itub@cpan.orgE<gt>
676  
677 =head1 COPYRIGHT
678  
679 Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is
680 free software; you can redistribute it and/or modify it under the same terms as
681 Perl itself.
682  
683 =cut
684  
« blib/lib/Chemistry/Pattern.pm ↑ Summary blib/lib/Chemistry/File/Mopac.pm »