blib/lib/Chemistry/File/SLN.pm

filter: Statementstmt 82.9% 209 / 252 Branchbran 82.0% 82 / 100 Conditioncond 72.0% 18 / 25 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 89.6% 26 / 29 Podpod 26.3% 5 / 19 totaltotal 79.0% 346 / 438 CC 71 SCAR 21.1
CC
71
Cov
82%
read_coords
20.0
format_ctab_attr
15.3
parse_single_line
14.5
CRAP 8.2
↑ Summary Parser.yp »
1 package Chemistry::File::SLN;
2  
3 2 $VERSION = "0.11";
4 # $Id: SLN.pm,v 1.4 2005/03/29 16:38:06 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 base "Chemistry::File";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Chemistry::Mol;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Chemistry::File::SLN::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Chemistry::Bond::Find 'assign_bond_orders';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Chemistry::File::SLN - SLN linear notation parser/writer
18  
19 =head1 SYNOPSYS
20  
21 #!/usr/bin/perl
22 use Chemistry::File::SLN;
23  
24 # parse a SLN string for benzene
25 my $s = 'C[1]H:CH:CH:CH:CH:CH@1';
26 my $mol = Chemistry::Mol->parse($s, format => 'sln');
27  
28 # print a SLN string
29 print $mol->print(format => 'sln');
30  
31 # print a unique (canonical) SLN string
32 print $mol->print(format => 'sln', unique => 1);
33  
34 # parse a multiline SLN file
35 my @mols = Chemistry::Mol->read("file.sln", format => 'sln');
36  
37 # write a multiline SLN file
38 Chemistry::Mol->write("file.sln", mols => [@mols]);
39  
40  
41 =head1 DESCRIPTION
42  
43 This module parses a SLN (Sybyl Line Notation) string. This is a File I/O
44 driver for the PerlMol project. L<http://www.perlmol.org/>. It registers the
45 'sln' format with Chemistry::Mol, and recognizes filenames ending in '.sln'.
46  
47 Optional attributes for atoms, bonds, and molecules are stored as
48 $atom->attr("sln/attr"), $bond->attr("sln/attr"), and $mol->attr("sln/attr"),
49 respectively. Boolean attributes are stored with a value of 'TRUE'. That's the
50 way boolean attributes are recognized when writing, so that they can be written
51 in the shortened form.
52  
53 $sln_attr->{backbone} = 1;
54 # would be ouput as "C[backbone=1]"
55  
56 $sln_attr->{backbone} = 'TRUE';
57 # would be ouput as "C[backbone]"
58  
59 Also note that attribute names are normalized to lowercase on reading.
60  
61 =head1 OPTIONS
62  
63 The following options are available when reading:
64  
65 =over
66  
67 =item kekulize
68  
69 Assign bond orders for unsatisfied valences or for aromatic bonds. For example,
70 benzene read as C[1]H:CH:CH:CH:CH:CH@1 will be converted internally to
71 something like C[1]H=CHCH=CHCH=CH@1. This is needed if another format or
72 module expects a Kekule representation without an aromatic bond type.
73  
74 =back
75  
76 The following options are available when writing:
77  
78 =over
79  
80 =item mols
81  
82 If this option points to an array of molecules, these molecules will be
83 written, one per line, as in the example in the SYNOPSYS.
84  
85 =item aromatic
86  
87 Detect aromaticity before writing. This will ensure that aromatic bond types
88 are used instead of alternate single and double bonds.
89  
90 =item unique
91  
92 Canonicalize before writing, and produce a unique strucure. NOTE: this option
93 does not guarantee a unique representation for molecules with bracketed
94 attributes.
95  
96 =item name
97  
98 Include the name of the molecule ($mol->name) in the output string.
99  
100 =item coord3d, coords
101  
102 Include the 3D coordinates of every atom in the molecule in the output string.
103 C<coord3d> and C<coords> may be used interchangeably.
104  
105 =item attr
106  
107 Output the atom, bond, and molecule attributes found in $mol->attr("sln/attr"),
108 etc.
109  
110 =back
111  
112 =head1 CAVEATS
113  
114 This version does not implement the full SLN specification. It supports
115 simple structures and some attributes, but it does not support any of the
116 following:
117  
118 =over
119  
120 =item Macro atoms
121  
122 =item Pattern matching options
123  
124 =item Markush structures
125  
126 =item 2D Coordinates
127  
128 =back
129  
130 The SLN specification is vague on several points, and I don't have a reference
131 implementation available, so I had to make several arbitrary decisions. Also,
132 this version of this module has not been tested exhaustively, so please report
133 any bugs that you find.
134  
135 If the parser doesn't understand a string, it only says "syntax error", which
136 may not be very helpful.
137  
138 =cut
139  
140 # INITIALIZATION
141 2 Chemistry::Mol->register_format('sln');
142 2 my $Parser = Chemistry::File::SLN::Parser->new;
143  
144 0 ▶sub name_is {
Subroutine name_is: not called CC=1 SCAR=6.9
Pod documented
145 0 my ($self, $name) = @_;
146 0 $name =~ /\.sln$/i;
147 }
148  
149 0 ▶sub file_is {
Subroutine file_is: not called CC=1 SCAR=6.9
Pod documented
150 0 $_[0]->name_is($_[1]);
151 }
152  
153 72 ▶sub parse_string {
Subroutine parse_string: called CC=3 SCAR=11.8
Pod documented
154 72 my ($self, $string, %opts) = @_;
155  
156 72 my (@lines) = split /(?:\n|\r\n?)/, $string;
157 72 my @mols;
158 72 for my $line (@lines) {
159 72 my $mol = $self->parse_single_line($line, %opts);
160 72 ▶ return $mol unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 72 0 72
161 0 push @mols, $mol;
162 }
163 0 @mols;
164 }
165  
166 72 ▶sub parse_single_line {
Subroutine parse_single_line: called CC=14 SCAR=26.8
Pod undocumented
167 72 my ($self, $string, %opts) = @_;
168  
169 72 ▶ my $mol_class = $opts{mol_class} || "Chemistry::Mol";
Condition50%1/2
$opts{'mol_class'} || 'Chemistry::Mol'
l!l
720
MC/DC0%0/1
$opts{'mol_class'} || 'Chemistry::Mol'
$opts{'mol_class'}
Truth table50%1/2
A
A= $opts{'mol_class'}
Aresult
11
00
170  
171  
172 # call the actual yapp-generated parser
173 72 ▶ my $tree = $Parser->run($string) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $tree = $Parser->run($string) or return 72 0 72
174 #use Data::Dumper; print Dumper $tree;
175  
176 72 my $mol = $mol_class->new;
177 72 my @nodes = @{$tree->{chain}};
178 72 my %closures;
179 my $last_atom;
180 72 my @stack;
181
182 72 ▶ while (my $node = shift @nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = shift @nodes) 631 72 703
183 631 ▶ if ($node eq '(') {
Branch: 3 branches
BranchTrueFalseTotal
if ($node eq '(') { } 79 552 631
elsif ($node eq ')') { } 79 473 552
elsif ($last_atom) { } 401 72 473
184 79 push @stack, $last_atom;
185 } elsif ($node eq ')') {
186 79 $last_atom = pop @stack;
187 } elsif($last_atom) { # bond
188 401 my $next = shift @nodes;
189 401 ▶ if ($next->{closure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->{'closure'}) { } 23 378 401
190 23 my $atom = $closures{$next->{closure}};
191 23 $self->compile_bond($mol, $node, $last_atom, $atom);
192 } else {
193 378 my $atom = $self->compile_atom($mol, $next, \%closures);
194 378 $self->compile_bond($mol, $node, $last_atom, $atom);
195 378 $last_atom = $atom;
196 }
197 } else { # first atom
198 72 $last_atom = $self->compile_atom($mol, $node, \%closures);
199 }
200 }
201 72 ▶ if ($opts{kekulize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"kekulize"}) 0 72 72
202 0 assign_bond_orders($mol, method => "itub", use_coords => 0,
203 scratch => 0, charges => 0);
204 }
205 72 my @sln_attr;
206 72 ▶ while (my ($attr, $value) = each %{$tree->{attr}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$tree{"attr"};}) 68 72 140
207 68 ▶ if ($attr eq 'name') {
Branch: 3 branches
BranchTrueFalseTotal
if ($attr eq 'name') { } 66 2 68
elsif ($attr eq 'type') { } 0 2 2
elsif ($attr eq 'coord3d') { } 0 2 2
208 66 $mol->name($value);
209 } elsif ($attr eq 'type') {
210 0 $mol->type($value);
211 } elsif ($attr eq 'coord3d') {
212 0 $self->read_coords($mol, $value);
213 } else {
214 2 push @sln_attr, $attr, $value;
215 }
216 }
217 72 ▶ $mol->attr("sln/attr", {@sln_attr}) if @sln_attr;
Branch: 1 branch
BranchTrueFalseTotal
if @sln_attr 1 71 72
218 72 $mol;
219  
220 }
221  
222 450 ▶sub compile_atom {
Subroutine compile_atom: called CC=2 SCAR=6.9
Pod undocumented
223 450 my ($self, $mol, $node, $closures) = @_;
224 my $atom = $mol->new_atom(
225 symbol => $node->{symbol},
226 hydrogens => $node->{hcount},
227 formal_charge => $node->{attr}{charge},
228 450 );
229 450 $atom->attr("sln/attr", $node->{attr});
230 450 delete $node->{attr}{charge};
231 450 ▶ $closures->{$node->{id}} = $atom if $node->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"id"} 23 427 450
232 450 $atom;
233 }
234  
235 2 my %TYPE_TO_ORDER = (
236 '-' => 1,
237 '=' => 2,
238 '#' => 3,
239 ':' => 1,
240 '.' => 0,
241 );
242  
243 401 ▶sub compile_bond {
Subroutine compile_bond: called CC=4 SCAR=13.9
Pod undocumented
244 401 my ($self, $mol, $node, $atom1, $atom2) = @_;
245 401 my $order = $TYPE_TO_ORDER{$node->{type}};
246 401 ▶ if ($order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order) 396 5 401
247 my $bond = $mol->new_bond(
248 type => $node->{type},
249 396 atoms=>[$atom1, $atom2],
250 order => $order,
251 );
252 396 $bond->attr("sln/attr", $node->{attr});
253 396 ▶ if ($node->{type} eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq ":") 66 330 396
254 66 $_->aromatic(1) for ($atom1, $atom2, $bond);
255 }
256 }
257 }
258  
259 0 ▶sub read_coords {
Subroutine read_coords: not called CC=4 SCAR=30.0
Pod undocumented
260 0 my ($self, $mol, $coords_str) = @_;
261 0 $coords_str =~ s/[()]//g;
262 0 my (@coords) = split /,/, $coords_str;
263 0 my $fh = $mol->formula_hash;
264 0 my $n = sum(values %$fh);
265 0 my $sprout = (@coords == 3*$n);
266 0 for my $atom ($mol->atoms) {
267 0 $atom->coords(splice @coords, 0, 3);
268 0 ▶ if ($sprout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sprout) 0 0 0
269 0 for (1 .. $atom->implicit_hydrogens) {
270 0 my $H = $mol->new_atom(symbol => 'H',
271 coords => [splice @coords, 0, 3]);
272 0 $mol->new_bond(atoms => [$atom, $H]);
273 }
274 0 $atom->implicit_hydrogens(0);
275 }
276 }
277 }
278  
279  
280 ########### WRITER #################
281  
282  
283 73 ▶sub write_string {
Subroutine write_string: called CC=3 SCAR=11.7
Pod documented
284 73 my ($self, $mol_ref, %opts) = @_;
285  
286 73 my $eol;
287 my @mols;
288 73 ▶ if ($opts{mols}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'mols'}) { } 0 73 73
289 0 @mols = @{$opts{mols}};
290 0 $eol = "\n";
291 } else {
292 73 @mols = $mol_ref;
293 73 $eol = "";
294 }
295  
296 73 my $sln;
297 73 for my $mol (@mols) {
298 73 $sln .= $self->write_mol($mol, %opts) . $eol;
299 }
300 73 $sln;
301 }
302  
303 73 ▶sub write_mol {
Subroutine write_mol: called CC=4 SCAR=13.9
Pod documented
304 73 my ($self, $mol, %opts) = @_;
305  
306 73 my $oldmol = $mol;
307 73 $mol = $mol->clone;
308  
309 73 my $sln = '';
310 73 my @id_log;
311 73 ▶ if ($mol->atoms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mol->atoms) 73 0 73
312 73 my @atoms = $self->clean_mol($mol, %opts);
313  
314 73 my $visited = {};
315 73 my @s;
316 73 for my $atom (@atoms) {
317 453 ▶ next if $visited->{$atom};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$atom} 375 78 453
318 78 my $ring_atoms = {};
319  
320 # first pass to find and number the ring bonds
321 78 $self->find_ring_bonds($mol, \%opts, $atom, undef, {}, $ring_atoms);
322  
323 # second pass to actually generate the sln string
324 78 push @s, $self->branch($mol, \%opts, $atom, undef, $visited,
325 $ring_atoms, \@id_log);
326 }
327 73 $sln .= join '.', @s;
328 }
329  
330 73 $sln .= $self->format_ctab_attr($mol, \%opts, $oldmol, \@id_log);
331 }
332  
333 73 ▶sub clean_mol {
Subroutine clean_mol: called CC=4 SCAR=19.6
Pod undocumented
334 73 my ($self, $mol, %opts) = @_;
335  
336 73 $self->collapse_hydrogens($mol);
337 73 my @atoms = $mol->atoms;
338 73 ▶ if ($opts{unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"unique"}) 0 73 73
339 0 ▶ unless ($atoms[0]->attr("canon/class")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($atoms[0]->attr("canon/class")) 0 0 0
340 0 require Chemistry::Canonicalize;
341 0 Chemistry::Canonicalize::canonicalize($mol);
342 }
343 #$opts{aromatic} = 1; # all unique sln have to be aromatic
344 @atoms = sort {
345 0 $a->attr("canon/class") <=> $b->attr("canon/class")
346 } @atoms;
347 }
348  
349 73 ▶ if ($opts{aromatic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"aromatic"}) 0 73 73
350 0 require Chemistry::Ring;
351 0 Chemistry::Ring::aromatize_mol($mol);
352 }
353 73 @atoms;
354 }
355  
356 73 ▶sub format_ctab_attr {
Subroutine format_ctab_attr: called CC=12 SCAR=27.2
Pod undocumented
357 73 my ($self, $mol, $opts, $oldmol, $id_log) = @_;
358  
359 73 my $sln = '';
360 73 ▶ if ($opts->{name} or $opts->{attr} or $opts->{coords} or $opts->{coord3d}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"name"} or $opts->{"attr"} or $opts->{"coords"} or $opts->{"coord3d"}) 71 2 73
Condition66%6/9
$opts->{"name"} or $opts->{"attr"} or $opts->{"coords"} or $opts->{"coord3d"}
l!l&&r!l&&!r
7102
$opts->{"name"} or $opts->{"attr"} or $opts->{"coords"}
l!l&&r!l&&!r
7102
$opts->{"name"} or $opts->{"attr"}
l!l&&r!l&&!r
7102
MC/DC25%1/4
$opts->{"name"} or $opts->{"attr"} or $opts->{"coords"} or $opts->{"coord3d"}
$opts->{"name"} $opts->{"attr"} $opts->{"coords"} $opts->{"coord3d"}
Truth table40%2/5
A or B or C or D
A= $opts->{"name"}B= $opts->{"attr"}C= $opts->{"coords"}D= $opts->{"coord3d"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
361 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
362 71 my @attr;
363 71 my $name = $mol->name;
364 71 $name =~ s/[\r\n]//g;
365 push @attr, 'name="' . $mol->name . '"'
366 71 ▶ if $opts->{name} and length $mol->name;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"name"} and length $mol->name 66 5 71
Condition66%2/3
$opts->{"name"} and length $mol->name
!ll&&!rl&&r
0566
MC/DC50%1/2
$opts->{"name"} and length $mol->name
$opts->{"name"} length $mol->name
Truth table66%2/3
A and B
A= $opts->{"name"}B= length $mol->name
ABresult
0X0
100
111
367 71 my @coords;
368 71 ▶ if ($opts->{coord3d} or $opts->{coords}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"coord3d"} or $opts->{"coords"}) 0 71 71
Condition33%1/3
$opts->{"coord3d"} or $opts->{"coords"}
l!l&&r!l&&!r
0071
MC/DC0%0/2
$opts->{"coord3d"} or $opts->{"coords"}
$opts->{"coord3d"} $opts->{"coords"}
Truth table33%1/3
A or B
A= $opts->{"coord3d"}B= $opts->{"coords"}
ABresult
1X1
011
000
369 my @all_atoms = map {
370 0 (
371 $oldmol->by_id($_),
372 0 grep {$_->symbol eq 'H'}
373 $oldmol->by_id($_)->neighbors
374 )
375 } @$id_log;
376 push @coords, sprintf("(%.3f,%.3f,%.3f)",$_->coords->array)
377 0 for @all_atoms;
378 0 push @attr, 'coord3d=' . join(',',@coords);
379 }
380 71 ▶ if ($opts->{attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"attr"}) 71 0 71
381 71 push @attr, $self->format_sln_attr($mol);
382 }
383 71 ▶ $sln .= '<' . join(';', @attr) . '>' if @attr;
Branch: 1 branch
BranchTrueFalseTotal
if @attr 66 5 71
384 }
385 73 $sln;
386 }
387  
388  
389 453 ▶sub find_ring_bonds {
Subroutine find_ring_bonds: called CC=4 SCAR=13.9
Pod undocumented
390 453 my ($self, $mol, $opts, $atom, $from_bond, $visited, $ring_atoms) = @_;
391  
392 453 $visited->{$atom} = 1;
393 453 for my $bn (sorted_bonds_neighbors($atom, $opts)) {
394 796 my $nei = $bn->{to};
395 796 my $bond = $bn->{bond};
396 796 ▶ next if $visited->{$bond};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$bond} 398 398 796
397 398 $visited->{$bond} = 1;
398 398 ▶ if ($visited->{$nei}) { # closed ring
Branch: 1 branch
BranchTrueFalseTotal
if ($visited->{$nei}) { } 23 375 398
399 #print "closing ring\n";
400 23 $ring_atoms->{$nei}++;
401 } else {
402 375 $self->find_ring_bonds($mol, $opts, $nei,
403 $bond, $visited, $ring_atoms);
404 }
405 }
406 }
407  
408 453 ▶sub branch {
Subroutine branch: called CC=7 SCAR=19.5
Pod undocumented
409 453 my ($self, $mol, $opts, $atom, $from_bond, $visited, $digits, $id_log) = @_;
410  
411 453 my $prev_branch = "";
412 453 my $sln;
413 453 $sln .= $self->format_bond($from_bond, $opts);
414 453 my $digit;
415 453 ▶ if ($digits->{$atom}) { # opening a ring
Branch: 1 branch
BranchTrueFalseTotal
if ($digits->{$atom}) 23 430 453
416 23 $digit = $self->next_digit($digits);
417 23 $digits->{$atom} = $digit;
418 }
419 453 $sln .= $self->format_atom($atom, $opts, $digit);
420 453 push @$id_log, $atom->id;
421  
422 453 $visited->{$atom} = 1;
423 453 my @bns = sorted_bonds_neighbors($atom, $opts);
424  
425 453 for my $bn (@bns) {
426 796 my $nei = $bn->{to};
427 796 my $bond = $bn->{bond};
428 796 ▶ next if $visited->{$bond};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$bond} 398 398 796
429 398 $visited->{$bond} = 1;
430 398 ▶ if ($visited->{$nei}) { # closed a ring
Branch: 1 branch
BranchTrueFalseTotal
if ($visited->{$nei}) { } 23 375 398
431 23 ▶ if ($prev_branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_branch) 6 17 23
432 6 $sln .= "($prev_branch)";
433 }
434 $prev_branch = $self->format_bond($bond, $opts)
435 23 . '@' . $digits->{$nei};
436 23 $visited->{$bond} = 1;
437 } else {
438 375 my $branch = $self->branch($mol, $opts, $nei, $bond, $visited,
439 $digits, $id_log);
440 375 ▶ if ($prev_branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_branch) 73 302 375
441 73 $sln .= "($prev_branch)";
442 }
443 375 $prev_branch = $branch;
444 }
445 }
446 453 $sln .= "$prev_branch";
447 453 $sln;
448 }
449  
450 23 ▶sub next_digit {
Subroutine next_digit: called CC=1 SCAR=0.0
Pod undocumented
451 23 my ($self, $digits) = @_;
452 23 ++$digits->{used_digits};
453 }
454  
455 73 ▶sub collapse_hydrogens {
Subroutine collapse_hydrogens: called CC=3 SCAR=18.1
Pod undocumented
456 73 my ($self, $mol) = @_;
457  
458 73 for my $atom (grep {$_->symbol eq 'H'} $mol->atoms) {
459 0 ▶ my ($neighbor) = $atom->neighbors or next;
Branch: 1 branch
BranchTrueFalseTotal
my($neighbor) = $atom->neighbors or next 0 0 0
460 0 $atom->delete;
461 0 my $h_count = $neighbor->hydrogens;
462 0 $h_count++;
463 0 $neighbor->hydrogens($h_count);
464 }
465 }
466  
467 906 ▶sub sorted_bonds_neighbors {
Subroutine sorted_bonds_neighbors: called CC=2 SCAR=7.9
Pod undocumented
468 906 my ($atom, $opts) = @_;
469 906 my @bn = $atom->bonds_neighbors;
470 906 ▶ if ($opts->{unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"unique"}) 0 906 906
471 @bn = sort {
472 0 $a->{to}->attr("canon/class") <=> $b->{to}->attr("canon/class")
473 } @bn;
474 }
475 906 @bn;
476 }
477  
478 2 my %ORDER_TO_TYPE = (
479 1 => '', 2 => '=', 3 => '#', 4 => '', 0 => '.',
480 );
481  
482 476 ▶sub format_bond {
Subroutine format_bond: called CC=5 SCAR=16.1
Pod undocumented
483 476 my ($self, $bond, $opts) = @_;
484 476 ▶ return '' unless $bond;
Branch: 1 branch
BranchTrueFalseTotal
unless $bond 78 398 476
485 398 ▶ my $s = $bond->aromatic ? ':' : $ORDER_TO_TYPE{$bond->order};
Branch: 1 branch
BranchTrueFalseTotal
$bond->aromatic ? : 66 332 398
486 398 my @attr;
487 398 ▶ @attr = $self->format_sln_attr($bond) if $opts->{attr};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"attr"} 394 4 398
488 398 ▶ if (@attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attr) 1 397 398
489 1 $s .= '[' . join(";", @attr) . ']';
490 }
491 398 $s;
492 }
493  
494 453 ▶sub format_atom {
Subroutine format_atom: called CC=11 SCAR=24.0
Pod undocumented
495 453 my ($self, $atom, $opts, $digit) = @_;
496 453 my $s;
497 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
498 453 my $h_count = $atom->hydrogens;
499 453 my $charge = $atom->formal_charge;
500 453 my $symbol = $atom->symbol;
501  
502 453 ▶ $charge = $charge ? sprintf("%+d", $charge): '';
Branch: 1 branch
BranchTrueFalseTotal
$charge ? : 4 449 453
503 453 ▶ $h_count = $h_count ? ($h_count > 1 ? "H$h_count" : 'H') : '';
Branch: 2 branches
BranchTrueFalseTotal
$h_count ? : 285 168 453
$h_count > 1 ? : 195 90 285
504  
505 453 $s = $symbol;
506 453 my @attr;
507 453 ▶ @attr = $self->format_sln_attr($atom) if $opts->{attr};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"attr"} 447 6 453
508 453 ▶ if ($charge or $digit or @attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charge or $digit or @attr) 27 426 453
Condition100%6/6
$charge or $digit or @attr
l!l&&r!l&&!r
261426
$charge or $digit
l!l&&r!l&&!r
422427
MC/DC100%3/3
$charge or $digit or @attr
$charge $digit @attr
Truth table100%4/4
A or B or C
A= $chargeB= $digitC= @attr
ABCresult
1XX1
01X1
0011
0000
509 27 $s .= '[';
510 27 $s .= $digit;
511 27 ▶ unshift @attr, $charge if $charge;
Branch: 1 branch
BranchTrueFalseTotal
if $charge 4 23 27
512 27 ▶ if (@attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attr) 5 22 27
513 5 ▶ $s .= ':' if $digit;
Branch: 1 branch
BranchTrueFalseTotal
if $digit 1 4 5
514 5 $s .= join ';', @attr;
515 }
516 27 $s .= ']';
517 }
518 453 $s .= $h_count;
519 453 $s;
520 }
521  
522 912 ▶sub format_sln_attr {
Subroutine format_sln_attr: called CC=4 SCAR=13.9
Pod undocumented
523 912 my ($self, $obj) = @_;
524 912 ▶ my $sln_attr = $obj->attr("sln/attr") || {};
Condition100%2/2
$obj->attr('sln/attr') || {}
l!l
449463
MC/DC100%1/1
$obj->attr('sln/attr') || {}
$obj->attr('sln/attr')
Truth table100%2/2
A
A= $obj->attr('sln/attr')
Aresult
11
00
525 912 my @attr;
526 912 for my $key (sort keys %$sln_attr) {
527 6 my $val = $sln_attr->{$key};
528 6 ▶ push @attr, "$key" . ($val eq 'TRUE' ? "" : "=$val");
Branch: 1 branch
BranchTrueFalseTotal
$val eq 'TRUE' ? : 2 4 6
529 }
530 912 @attr;
531 }
532  
533 2 1;
534  
535 =head1 VERSION
536  
537 0.11
538  
539 =head1 SEE ALSO
540  
541 L<Chemistry::Mol>, L<Chemistry::File>, L<Chemistry::File::SMILES>
542  
543 The PerlMol website L<http://www.perlmol.org/>
544  
545 Ash, S.; Cline, M. A.; Homer, R. W.; Hurst, T.; Smith, G. B., SYBYL Line
546 Notation (SLN): A Versatile Language for Chemical Structure Representation. J.
547 Chem. Inf. Comput. Sci; 1997; 37(1); 71-79. DOI: 10.1021/ci960109j
548 (L<http://dx.doi.org/10.1021/ci960109j>)
549  
550 =head1 AUTHOR
551  
552 Ivan Tubert-Brohman E<lt>itub@cpan.orgE<gt>
553  
554 =head1 COPYRIGHT
555  
556 Copyright (c) 2004 Ivan Tubert-Brohman. All rights reserved. This program is
557 free software; you can redistribute it and/or modify it under the same terms as
558 Perl itself.
559  
560 =cut
561  
↑ Summary Parser.yp »