blib/lib/Chemistry/Bond/Find.pm

filter: Statementstmt 15.5% 46 / 295 Branchbran 2.8% 4 / 140 Conditioncond 2.6% 2 / 75 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 32.0% 8 / 25 Podpod 10.5% 2 / 19 totaltotal 10.3% 62 / 597 CC 114 SCAR 53.9
CC
114
Cov
10%
resolve_conflicts
650.0
assign_initial_coordinations
420.0
assign_initial_bonds
210.0
CRAP 220.3
« blib/lib/Chemistry/File/MDLMol.pm ↑ Summary blib/lib/Chemistry/Mok.pm »
1 package Chemistry::Bond::Find;
2  
3 11 $VERSION = '0.21';
4 11 our $DEBUG = 0;
5 # $Id: Find.pm,v 1.6 2004/06/16 19:36:29 itubert Exp $
6  
7 =head1 NAME
8  
9 Chemistry::Bond::Find - Detect bonds in a molecule from atomic 3D coordinates and assign formal bond orders
10  
11 =head1 SYNOPSIS
12  
13 use Chemistry::Bond::Find ':all'; # export all available functions
14  
15 # $mol is a Chemistry::Mol object
16 find_bonds($mol);
17 assign_bond_orders($mol);
18  
19 =head1 DESCRIPTION
20  
21 This module provides functions for detecting the bonds in a molecule from its
22 3D coordinates by using simple cutoffs, and for guessing the formal bond
23 orders.
24  
25 This module is part of the PerlMol project, L<http://www.perlmol.org/>.
26  
27 =head1 FUNCTIONS
28  
29 These functions may be exported, although nothing is exported by default.
30  
31 =over
32  
33 =cut
34  
35 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 11 ▶use Chemistry::Mol;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 11 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 11 our @ISA = qw(Exporter);
41 11 our @EXPORT_OK = qw(find_bonds assign_bond_orders );
42 11 our %EXPORT_TAGS = (all => \@EXPORT_OK);
43  
44 # table taken from
45 # http://environmentalchemistry.com/yogi/periodic/covalentradius.html
46 11 my %Covalent_Radius = (
47 Ag => 1.34, Al => 1.18, Ar => 0.98, As => 1.20, At => 1.45, Au => 1.34,
48 B => 0.82, Ba => 1.98, Be => 0.90, Bi => 1.46, Br => 1.14, C => 0.77,
49 Ca => 1.74, Cd => 1.48, Ce => 1.65, Cl => 0.99, Co => 1.16, Cr => 1.18,
50 Cs => 2.35, Cu => 1.17, Dy => 1.59, Er => 1.57, Eu => 1.85, F => 0.72,
51 Fe => 1.17, Ga => 1.26, Gd => 1.61, Ge => 1.22, H => 0.32, He => 0.93,
52 Hf => 1.44, Hg => 1.49, Ho => 1.58, I => 1.33, In => 1.44, Ir => 1.27,
53 K => 2.03, Kr => 1.12, La => 1.69, Li => 1.23, Lu => 1.56, Mg => 1.36,
54 Mn => 1.17, Mo => 1.30, N => 0.75, Na => 1.54, Nb => 1.34, Nd => 1.64,
55 Ne => 0.71, Ni => 1.15, O => 0.73, Os => 1.26, P => 1.06, Pb => 1.47,
56 Pd => 1.28, Pm => 1.63, Po => 1.46, Pr => 1.65, Pt => 1.30, Rb => 2.16,
57 Re => 1.28, Rh => 1.25, Ru => 1.25, S => 1.02, Sb => 1.40, Sc => 1.44,
58 Se => 1.16, Si => 1.11, Sm => 1.62, Sn => 1.41, Sr => 1.91, Ta => 1.34,
59 Tb => 1.59, Tc => 1.27, Te => 1.36, Th => 1.65, Ti => 1.32, Tl => 1.48,
60 Tm => 1.56, U => 1.42, V => 1.22, W => 1.30, Xe => 1.31, Y => 1.62,
61 Yb => 1.74, Zn => 1.25, Zr => 1.45,
62 );
63  
64 11 my $Default_Radius = 1.5; # radius for unknown elements
65  
66 # I considered inlining this function, but the performance gain was minimal
67 # ( < 5 % ), so it's probably better to leave it here
68 # $opts->{cuttof} Hash Table of cutoffs.
69 # Example: key = "H C", value = [0.76, 1.42]
70 0 ▶sub are_bonded {
Subroutine are_bonded: not called CC=4 SCAR=30.0
Pod undocumented
71 0 my ($symbol_a, $symbol_b, $r, $opts) = @_;
72 return $r < ($opts->{cuttoffs}{"$symbol_a $symbol_b"} ||=
73 (($Covalent_Radius{$symbol_a} || $Default_Radius)
74 + ($Covalent_Radius{$symbol_b} || $Default_Radius))
75 0 ▶ * $opts->{tolerance});
Condition0%0/9
$opts->{"cuttoffs"}{"$symbol_a $symbol_b"} ||= (($Covalent_Radius{$symbol_a} || $Default_Radius) + ($Covalent_Radius{$symbol_b} || $Default_Radius)) * $opts->{"tolerance"}
l!l&&r!l&&!r
000
$Covalent_Radius{$symbol_a} || $Default_Radius
l!l&&r!l&&!r
000
$Covalent_Radius{$symbol_b} || $Default_Radius
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts->{"cuttoffs"}{"$symbol_a $symbol_b"} ||= (($Covalent_Radius{$symbol_a} || $Default_Radius) + ($Covalent_Radius{$symbol_b} || $Default_Radius)) * $opts->{"tolerance"}
$opts->{"cuttoffs"}{"$symbol_a $symbol_b"} $Covalent_Radius{$symbol_a} $Default_Radius
MC/DC0%0/2
$Covalent_Radius{$symbol_b} || $Default_Radius
$Covalent_Radius{$symbol_b} $Default_Radius
Truth table0%0/4
A ||= B || C
A= $opts->{"cuttoffs"}{"$symbol_a $symbol_b"}B= $Covalent_Radius{$symbol_a}C= $Default_Radius
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/3
A || B
A= $Covalent_Radius{$symbol_b}B= $Default_Radius
ABresult
1X1
011
000
76 }
77  
78  
79 =item C<find_bonds($mol, %options)>
80  
81 Finds and adds the bonds in a molecule. Only use it in molecules that have no
82 explicit bonds; for example, after reading a file with 3D coordinates but no
83 bond orders.
84  
85 Available options:
86  
87 =over
88  
89 =item tolerance
90  
91 Defaults to 1.1. Two atoms are considered to be bound if the distance between
92 them is less than the sum of their covalent radii multiplied by the tolerance.
93  
94 =item margin
95  
96 NOTE: in general setting this option is not recommended, unless you know what
97 you are doing. It is used by the space partitioning algorithm to determine the
98 "bucket size". It defaults to 2 * Rmax * tolerance, where Rmax is the largest
99 covalent radius among the elements found in the molecule. For example, if a
100 molecule has C, H, N, O, and I, Rmax = R(I) = 1.33, so the margin defaults to 2
101 * 1.33 * 1.1 = 2.926. This margin ensures that no bonds are missed by the
102 partitioning algorithm.
103  
104 Using a smaller value gives faster results, but at the risk of missing some
105 bonds. In this example, if you are certain that your molecule doesn't contain
106 I-I bonds (but it has C-I bonds), you can set margin to (0.77 + 1.33) * 1.1 =
107 2.31 and you still won't miss any bonds (0.77 is the radius of carbon). This
108 only has a significant impact for molecules with a thousand atoms or more, but
109 it can reduce the execution time by 50% in some cases.
110  
111 =item orders
112  
113 If true, assign the bond orders after finding them, by calling
114 C<assign_bond_orders($mol, %opts)>.
115  
116 =item bond_class
117  
118 The class that will be used for creating the new bonds. The default is
119 L<Chemistry::Bond>.
120  
121 =back
122  
123 =cut
124  
125 # The new algorithm based on a suggestion by BrowserUK
126 0 ▶sub find_bonds {
Subroutine find_bonds: not called CC=2 SCAR=17.9
Pod documented
127 0 my ($mol, %opts) = @_;
128 0 %opts = (min_atoms => 20, tolerance => 1.1, %opts,
129 cutoffs => {}); # set defaults
130 0 my $margin = guess_margin($mol, \%opts);
131 0 %opts = (margin => $margin, %opts);
132 0 my $grid = {};
133 0 partition_space($mol, $grid, \%opts);
134 0 find_bonds_grid($mol, $grid, \%opts);
135 0 ▶ if ($opts{orders}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"orders"}) 0 0 0
136 0 assign_bond_orders($mol, %opts);
137 }
138 }
139  
140 11 ▶use POSIX 'floor';
Subroutine BEGIN: called CC=1 SCAR=0.0
141 11 my $Y = 1000;
142 11 my $X = $Y * $Y;
143 11 my $Z = 1;
144 11 my $ORIGIN = int(($Y**3 + $Y**2 + $Y)/2);
145  
146 # used by the new BrowserUK algorithm
147 0 ▶sub partition_space {
Subroutine partition_space: not called CC=2 SCAR=17.9
Pod undocumented
148 0 my ($mol, $grid, $opts) = @_;
149 0 my $margin = $opts->{margin};
150 0 for my $atom ($mol->atoms) {
151 0 my (@vec) = $atom->coords->array;
152 0 my (@norm_vec) = map { floor($_ / $margin) } @vec;
153 0 my $n = $X * $norm_vec[0] + $Y * $norm_vec[1]
154 + $norm_vec[2] + $ORIGIN;
155 0 push @{$grid->{$n}}, $atom;
156 }
157 }
158  
159 # used by the new BrowserUK algorithm
160 0 ▶sub find_bonds_grid {
Subroutine find_bonds_grid: not called CC=4 SCAR=30.0
Pod undocumented
161 0 my ($mol, $grid, $opts) = @_;
162 0 ▶ while (my ($n, $atoms) = each %$grid) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$grid) 0 0 0
163 #print "Cell $n has ". @$atoms . " atoms\n";
164 0 find_bonds_n2_one_set($mol, $atoms, $opts);
165 0 for my $neigh_n (
166 $n+$Z,
167 $n+$Z+$Y, $n+$Z-$Y, $n+$Z+$X, $n+$Z-$X,
168 $n+$Z+$Y+$X, $n+$Z+$Y-$X, $n+$Z-$Y+$X, $n+$Z-$Y-$X,
169 $n+$Y, $n+$Y+$X, $n+$Y-$X,
170 $n+$X,
171 ) {
172 0 ▶ if ($grid->{$neigh_n}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($grid->{$neigh_n}) 0 0 0
173 0 find_bonds_n2_two_sets($mol, $atoms, $grid->{$neigh_n}, $opts);
174 }
175 }
176 }
177 }
178  
179 # used by both find_bonds variants to figure out the maximum cutoff
180 0 ▶sub guess_margin {
Subroutine guess_margin: not called CC=3 SCAR=24.8
Pod undocumented
181 0 my ($mol, $opts) = @_;
182 0 my $formula = $mol->formula_hash;
183 0 my $max = 0;
184 0 for my $elem (keys %$formula) {
185 0 ▶ $max = $Covalent_Radius{$elem} if $Covalent_Radius{$elem} > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $Covalent_Radius{$elem} > $max 0 0 0
186 }
187 0 $max *= 2 * $opts->{tolerance};
188 #printf "MARGIN guessed at (%.2f)\n", $max;
189 0 $max;
190 }
191  
192 # brute-force N^2 algorithm
193 0 ▶sub find_bonds_n2_one_set {
Subroutine find_bonds_n2_one_set: not called CC=5 SCAR=34.0
Pod undocumented
194 0 my ($mol, $atoms, $opts) = @_;
195 0 ▶ my $bond_class = $opts->{bond_class} || "Chemistry::Bond";
Condition0%0/2
$opts->{'bond_class'} || 'Chemistry::Bond'
l!l
00
MC/DC0%0/1
$opts->{'bond_class'} || 'Chemistry::Bond'
$opts->{'bond_class'}
Truth table0%0/2
A
A= $opts->{'bond_class'}
Aresult
11
00
196 0 ▶ for (my $i = 0; $i < @$atoms; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$atoms) 0 0 0
197 0 ▶ for (my $j = $i + 1; $j < @$atoms; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$atoms) 0 0 0
198 0 my ($a1, $a2) = ($atoms->[$i], $atoms->[$j]);
199 0 ▶ if (are_bonded($a1->symbol, $a2->symbol, scalar $a1->distance($a2), $opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (are_bonded($a1->symbol, $a2->symbol, scalar $a1->distance($a2), $opts)) 0 0 0
200 0 $mol->add_bond($bond_class->new(atoms=>[$a1, $a2]));
201 }
202 }
203 }
204 }
205  
206 # brute force N*M algorithm for finding bonds between to sets of atoms
207 0 ▶sub find_bonds_n2_two_sets {
Subroutine find_bonds_n2_two_sets: not called CC=5 SCAR=34.0
Pod undocumented
208 0 my ($mol, $atoms1, $atoms2, $opts) = @_;
209 0 ▶ my $bond_class = $opts->{bond_class} || "Chemistry::Bond";
Condition0%0/2
$opts->{'bond_class'} || 'Chemistry::Bond'
l!l
00
MC/DC0%0/1
$opts->{'bond_class'} || 'Chemistry::Bond'
$opts->{'bond_class'}
Truth table0%0/2
A
A= $opts->{'bond_class'}
Aresult
11
00
210 0 for my $a1 (@$atoms1) {
211 0 for my $a2 (@$atoms2) {
212 0 ▶ if (are_bonded($a1->symbol, $a2->symbol, scalar $a1->distance($a2), $opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (are_bonded($a1->symbol, $a2->symbol, scalar $a1->distance($a2), $opts)) 0 0 0
213 0 $mol->add_bond($bond_class->new(atoms=>[$a1, $a2]));
214 }
215 }
216 }
217 }
218  
219 # The recursive algorithm used in version 0.05, here for historical purposes
220 0 ▶sub find_bonds_rec {
Subroutine find_bonds_rec: not called CC=1 SCAR=6.9
Pod undocumented
221 0 my ($mol, %opts) = @_;
222 0 %opts = (min_atoms => 20, tolerance => 1.1, %opts,
223 cutoffs => {}); # set defaults
224 0 my $margin = guess_margin($mol, \%opts);
225 0 %opts = (margin => $margin, %opts);
226 0 _partition($mol, [$mol->atoms], 0, \%opts);
227 }
228  
229 # partition space recursively, used by find_bonds_rec
230 0 ▶sub _partition {
Subroutine _partition: not called CC=5 SCAR=34.0
231 0 my ($mol, $atoms, $dir, $opts) = @_;
232  
233 #printf "_partition(%s, $dir)\n", scalar(@$atoms);
234  
235 0 ▶ if (@$atoms < $opts->{min_atoms}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$atoms < $opts->{"min_atoms"}) 0 0 0
236 #print "BOTTOM!\n";
237 0 find_bonds_n2_one_set($mol, $atoms, $opts);
238 0 return;
239 }
240  
241 0 my $min = ($atoms->[0]->coords->array)[$dir];
242 0 my $max = $min;
243 0 my $center;
244 0 for my $atom (@$atoms) {
245 0 my $coord = ($atom->coords->array)[$dir];
246 0 $center += $coord;
247 0 ▶ $min = $coord if $coord < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $coord < $min 0 0 0
248 0 ▶ $max = $coord if $coord > $min;
Branch: 1 branch
BranchTrueFalseTotal
if $coord > $min 0 0 0
249 }
250 0 $center /= @$atoms;
251 #printf "center($dir)=%.2f; range=(%.2f, %.2f)\n", $center, $min, $max;
252  
253 0 my @left = grep { ($_->coords->array)[$dir] < $center } @$atoms;
254 0 my @right = grep { ($_->coords->array)[$dir] >= $center } @$atoms;
255 0 _partition($mol, \@left, ($dir + 1) % 3, $opts);
256 0 _partition($mol, \@right, ($dir + 1) % 3, $opts);
257  
258 # merge the interface between the two halves
259 0 my $margin = $opts->{margin};
260 my @left_margin =
261 0 grep { ($_->coords->array)[$dir] > $center - $margin } @left;
262 my @right_margin =
263 0 grep { ($_->coords->array)[$dir] < $center + $margin } @right;
264 0 find_bonds_n2_two_sets($mol, \@left_margin, \@right_margin, $opts);
265 }
266  
267  
268 =item C<assign_bond_orders($mol, %opts)>
269  
270 Assign the formal bond orders in a molecule. The bonds must already be defined,
271 either by C<find_bonds> or because the molecule was read from a file that
272 included bonds but no bond orders. If the bond orders were already defined
273 (maybe the molecule came from a file that did include bond orders after all),
274 the original bond orders are erased and the process begins from scratch. Two
275 different algorithms are available, and may be selected by using the "method"
276 option:
277  
278 assign_bond_orders($mol, method => 'itub');
279 assign_bond_orders($mol, method => 'baber');
280  
281 =over
282  
283 =item itub
284  
285 This is the default if no method is specified. Developed from scratch by the
286 author of this module, this algorithm requires only the connection table
287 information, and it requires that all hydrogen atoms be explicit. It looks for
288 an atom with unsatisfied valence, increases a bond order, and then does the
289 same recursively for the neighbors. If everybody's not happy at the end, it
290 backtracks and tries another bond. The recursive part does not cover the whole
291 molecule, but only the contiguous region of "unhappy" atoms next to the
292 starting atom and their neighbors. This permits separating the molecule into
293 independent regions, so that if one is solved and there's a problem in another,
294 we don't have to backtrack to the first one.
295  
296 The itub algorithm has the following additional options:
297  
298 =over
299  
300 =item use_coords
301  
302 Although the algorithm does not I<require> 3D coordinates, it uses them by
303 default to improve the initial guesses of which bond orders should be
304 increased. To avoid using coordinates, add the C<use_coords> option with a
305 false value:
306  
307 assign_bond_orders($mol, use_coords => 0);
308  
309 The results are the same most of the time, but using good coordinates improves
310 the results for complicated cases such as fused heteroaromatic systems.
311  
312 =item scratch
313  
314 If true, start the bond order assignment from scratch by assuming that all bond
315 orders are 1. If false, start from the current bond orders and try to fix the
316 unsatisfied valences. This option is true by default.
317  
318 =back
319  
320 =item baber
321  
322 A bond order assignment algorithm based on Baber, J. C.; Hodgkin, E. E.
323 J. Chem. Inf. Comp. Sci. 1992, 32, 401-406 (with some interpretation).
324  
325 This algorithm uses the 3D coordinates along with various cutoffs and
326 confidence formulas to guess the bond orders. It then tries to resolve
327 conflicts by looping through the atoms (but is not recursive or backtracking).
328 It does not require explicit hydrogens (although it's better when they are
329 available) because it was designed for use with real crystallographic data
330 which often doesn't have hydrogen atoms.
331  
332 This method doesn't always give a good answer, especially for conjugated and
333 aromatic systems. The variation used in this module adds some random numbers to
334 resolve some ambiguities and break loops, so the results are not even entirely
335 deterministic (the 'itub' method is deterministic but the result may depend on
336 the input order of the atoms).
337  
338 =back
339  
340 =cut
341  
342 14 ▶sub assign_bond_orders {
Subroutine assign_bond_orders: called CC=3 SCAR=13.3
Pod documented
343 14 my ($mol, %opts) = @_;
344 14 ▶ if ($opts{method} and $opts{method} eq 'baber') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'method'} and $opts{'method'} eq 'baber') { } 0 14 14
Condition33%1/3
$opts{"method"} and $opts{"method"} eq "baber"
!ll&&!rl&&r
0140
MC/DC0%0/2
$opts{"method"} and $opts{"method"} eq "baber"
$opts{"method"} $opts{"method"} eq "baber"
Truth table33%1/3
A and B
A= $opts{"method"}B= $opts{"method"} eq "baber"
ABresult
0X0
100
111
345 0 assign_bond_orders_baber($mol, %opts);
346 } else {
347 14 assign_bond_orders_itub($mol, %opts);
348 }
349 }
350  
351 ####### Bond order assignment algorithm by Ivan Tubert-Brohman
352  
353 # The "typical" valence that we expect an atom to have satisfied. If not
354 # given, a value of 1 is assumed.
355 11 my %MIN_VALENCES = ( O => 2, C => 4, S => 2, H => 1, N => 3, P => 3, Si => 2,
356 F => 1, Cl => 1, Br => 1, I => 1 );
357  
358 # $ALLOWED_INCREASES{$from}{$to} means that element $from is willing to
359 # exceed its minimum valence by having a multiple bond to element $to.
360 11 my %ALLOWED_INCREASES = (
361 Cl => { O => 7 },
362 Br => { O => 7 },
363 I => { O => 7 },
364 S => { O => 6, C => 3, S => 4 },
365 N => { O => 5, C => 4, N => 4 },
366 Si => { O => 4, C => 4 },
367 O => { C => 3, O => 3 },
368 P => { O => 5, C => 4 },
369 );
370  
371 14 ▶sub assign_bond_orders_itub {
Subroutine assign_bond_orders_itub: called CC=7 SCAR=29.0
Pod undocumented
372 14 my ($mol, %opts) = @_;
373 14 %opts = (use_coords => 1, scratch => 1, funny => {}, %opts);
374 #my @funny_atoms;
375  
376 14 ▶ if ($opts{scratch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"scratch"}) 0 14 14
377 0 $_->order(1) for $mol->bonds;
378 }
379 14 for my $atom ($mol->atoms) {
380 40 ▶ if (wants_more_bonds($atom)) {
Branch: 1 branch
BranchTrueFalseTotal
if (wants_more_bonds($atom)) 0 40 40
381 0 my $ret = make_happy($atom, \%opts, []);
382 0 ▶ print "Atom $atom made happy? '$ret'\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
383 0 ▶ unless ($ret) { # atom is funny (has formal charge or unpaired e-)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
384 0 $opts{funny}{$atom} = 1;
385 #push @funny_atoms, $atom;
386 }
387 }
388 }
389 }
390  
391 40 ▶sub wants_more_bonds {
Subroutine wants_more_bonds: called CC=3 SCAR=11.3
Pod undocumented
392 40 my ($atom) = @_;
393 40 my $valence = $atom->valence;
394 #$n_bonds += $_->order for $atom->bonds;
395  
396 40 ▶ my $ret = ($valence < ($MIN_VALENCES{$atom->symbol} || 1));
Condition50%1/2
$MIN_VALENCES{$atom->symbol} || 1
l!l
400
MC/DC0%0/1
$MIN_VALENCES{$atom->symbol} || 1
$MIN_VALENCES{$atom->symbol}
Truth table50%1/2
A
A= $MIN_VALENCES{$atom->symbol}
Aresult
11
00
397 40 ▶ print " $atom wants more bonds? '$ret'\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 40 40
398 40 $ret;
399 }
400  
401 # the heart of the algorithm; increase a bond order, and then do the same
402 # recursively for the neighbors. If everybody's not happy at the end,
403 # backtrack and try another bond. Note that this search does not cover the
404 # whole molecule, but only the "unhappy" atoms and their neighbors. This
405 # permits separating the molecule into independent regions, so that if one
406 # is solved and there's a problem in another, we don't have to backtrack
407 # to the first one.
408 0 ▶sub make_happy {
Subroutine make_happy: not called CC=11 SCAR=48.8
Pod undocumented
409 0 my ($atom, $opts, $q) = @_;
410  
411 0 ▶ if (!$opts->{funny}{$atom} and wants_more_bonds($atom)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts->{'funny'}{$atom} and wants_more_bonds($atom)) { } 0 0 0
Condition0%0/3
not $opts->{"funny"}{$atom} and wants_more_bonds($atom)
!ll&&!rl&&r
000
MC/DC0%0/2
not $opts->{"funny"}{$atom} and wants_more_bonds($atom)
not $opts->{"funny"}{$atom} wants_more_bonds($atom)
Truth table0%0/3
not(A) and B
A= not $opts->{"funny"}{$atom}B= wants_more_bonds($atom)
ABresult
0X0
100
111
412 0 push @$q, ($atom->neighbors); # the queue of atoms to be checked
413 0 for my $bn (sorted_neighbors($atom, $opts)) {
414 0 my ($nei, $bond) = @$bn{'to', 'bond'};
415 # note: it would be better to find the bond that is most likely to
416 # be increased by taking bond length into account
417 0 ▶ if (accepts_more_bonds($nei, $atom)) {
Branch: 1 branch
BranchTrueFalseTotal
if (accepts_more_bonds($nei, $atom)) 0 0 0
418 0 my $order = $bond->order;
419 0 ▶ print "increasing $bond($atom-$nei)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
420 0 $bond->order($order+1); # increase order, be happy
421 # now make sure everybody's happy
422 0 push @$q, $nei->neighbors($atom); # our friend's neighbors
423 # need to be happy too
424 0 ▶ if (make_happy($atom, $opts, $q)) { # are we happy now?
Branch: 1 branch
BranchTrueFalseTotal
if (make_happy($atom, $opts, $q)) { } 0 0 0
425 0 return 1; # everybody's happy?
426 } else {
427 # something's wrong, will have to backtrack
428 0 ▶ print "decreasing $bond($atom-$nei)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
429 0 $bond->order($order);
430 0 push @$q, $nei; # remember; neighbor's not happy either
431 }
432 }
433 }
434 # couldn't find happiness
435 } else {
436 0 my $next = shift @$q;
437 0 ▶ unless ($next) { # no one left; everybody is happy!
Branch: 1 branch
BranchTrueFalseTotal
unless ($next) 0 0 0
438 0 ▶ print "no more atoms left to check at $atom\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
439 0 return 1;
440 }
441 0 return (make_happy($next, $opts, $q)); # happy if next atom is happy
442 }
443 0 ▶ print "not happy at $atom\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
444 0 0; # not happy
445 }
446  
447 0 ▶sub sorted_neighbors {
Subroutine sorted_neighbors: not called CC=5 SCAR=34.0
Pod undocumented
448 0 my ($atom, $opts) = @_;
449 0 my $use_coords = $opts->{use_coords};
450  
451 0 ▶ print "sorting neighbors\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
452 return map {
453 $_->{bn}
454 0 } sort {
455 ($b->{wants} cmp $a->{wants}) # those who want go first
456 0 ▶ || ($use_coords ? ($a->{len} <=> $b->{len}) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$use_coords ? : 0 0 0
Condition0%0/3
$Chemistry::Bond::Find::b->{'wants'} cmp $Chemistry::Bond::Find::a->{'wants'} || ($use_coords ? $Chemistry::Bond::Find::a->{'len'} <=> $Chemistry::Bond::Find::b->{'len'} : 0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$Chemistry::Bond::Find::b->{'wants'} cmp $Chemistry::Bond::Find::a->{'wants'} || ($use_coords ? $Chemistry::Bond::Find::a->{'len'} <=> $Chemistry::Bond::Find::b->{'len'} : 0)
$Chemistry::Bond::Find::b->{'wants'} cmp $Chemistry::Bond::Find::a->{'wants'} ($use_coords ? $Chemistry::Bond::Find::a->{'len'} <=> $Chemistry::Bond::Find::b->{'len'} : 0)
Truth table0%0/3
A || B
A= $Chemistry::Bond::Find::b->{'wants'} cmp $Chemistry::Bond::Find::a->{'wants'}B= ($use_coords ? $Chemistry::Bond::Find::a->{'len'} <=> $Chemistry::Bond::Find::b->{'len'} : 0)
ABresult
1X1
011
000
457 # if they both want to the same degree, the shortest
458 # bond goes first if we are using coords
459 } map {
460 0 +{
461 wants => wants_more_bonds($_->{to}),
462 bn => $_,
463 len => !$use_coords || $_->{bond}->length,
464 }
465 0 ▶ } $atom->bonds_neighbors;
Condition0%0/3
!$use_coords || $_->{'bond'}->length
l!l&&r!l&&!r
000
MC/DC0%0/2
!$use_coords || $_->{'bond'}->length
!$use_coords $_->{'bond'}->length
Truth table0%0/3
not(A) || B
A= !$use_coordsB= $_->{'bond'}->length
ABresult
1X1
011
000
466 }
467  
468 0 ▶sub accepts_more_bonds {
Subroutine accepts_more_bonds: not called CC=5 SCAR=34.0
Pod undocumented
469 0 my ($atom, $to) = @_;
470 0 my ($symbol, $to_symbol) = ($atom->symbol, $to->symbol);
471  
472 #my $n_bonds = 0;
473 #$n_bonds += $_->order for $atom->bonds;
474 0 my $valence = $atom->valence;
475  
476 # not enough bonds even for the minimum valence?
477 0 ▶ return 1 if ($valence < ($MIN_VALENCES{$atom->symbol} || 1));
Branch: 1 branch
BranchTrueFalseTotal
if $valence < ($MIN_VALENCES{$atom->symbol} || 1) 0 0 0
Condition0%0/2
$MIN_VALENCES{$atom->symbol} || 1
l!l
00
MC/DC0%0/1
$MIN_VALENCES{$atom->symbol} || 1
$MIN_VALENCES{$atom->symbol}
Truth table0%0/2
A
A= $MIN_VALENCES{$atom->symbol}
Aresult
11
00
478  
479 0 ▶ if ($valence < ($ALLOWED_INCREASES{$symbol}{$to_symbol} || 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valence < ($ALLOWED_INCREASES{$symbol}{$to_symbol} || 0)) { } 0 0 0
Condition0%0/2
$ALLOWED_INCREASES{$symbol}{$to_symbol} || 0
l!l
00
MC/DC0%0/1
$ALLOWED_INCREASES{$symbol}{$to_symbol} || 0
$ALLOWED_INCREASES{$symbol}{$to_symbol}
Truth table0%0/2
A
A= $ALLOWED_INCREASES{$symbol}{$to_symbol}
Aresult
11
00
480 # make sure we are willing to make multiple bonds with this guy
481 0 return 1;
482 } else {
483 0 return 0; # max. valence satisfied
484 }
485 }
486  
487  
488 ############
489 # Bond order assignment algorithm based on Baber, J. C.; Hodgkin, E. E.
490 # J. Chem. Inf. Comp. Sci. 1992, 32, 401-406
491  
492 # this algorithm uses the 3D coordinates along with various cutoffs and
493 # confidence formulas to assign the bond orders. It does not require all
494 # explicit hydrogens (although it's better when they are available) because
495 # it's design for use with real crystallographic data which often doesn't
496 # have hydrogen.
497  
498 11 my %Valences = (
499 C => [4], N => [3,4], O => [2],
500 P => [4, 5], S => [2, 4, 6], As => [4, 5], Se => [2, 4, 6],
501 Te => [2, 4, 6],
502 F => [1], Cl => [1, 3], Br => [1, 3, 5], I => [1, 3, 5, 7],
503 H => [1],
504 );
505  
506 # note; this table should only have single-letter symbols
507 11 my %Bond_Orders = (
508 "C C" => { is => 1.49, id => 1.31, it => 1.18, wsd => 1.38, wdt => 1.21 },
509 "C N" => { is => 1.42, id => 1.32, it => 1.14, wsd => 1.34, wdt => 1.20 },
510 "C O" => { is => 1.41, id => 1.22, wsd => 1.28 },
511 "C S" => { is => 1.78, id => 1.68, wsd => 1.70 },
512 "N N" => { is => 1.40, id => 1.22, wsd => 1.32 },
513 "N O" => { is => 1.39, id => 1.22, wsd => 1.25 },
514 "O S" => { is => 1.58, id => 1.45, wsd => 1.54 },
515 #"O S" => { is => 1.58, id => 1.45, wsd => 1.51 }, # modified value
516 "O P" => { is => 1.60, id => 1.48, wsd => 1.52 },
517 );
518  
519 # add keys for reverse bond for ease of lookup
520 11 for (keys %Bond_Orders) {
521 88 $Bond_Orders{ scalar reverse $_ } = $Bond_Orders{$_};
522 }
523  
524 0 ▶sub assign_bond_orders_baber {
Subroutine assign_bond_orders_baber: not called CC=5 SCAR=34.0
Pod undocumented
525 0 my ($mol, %opts) = @_;
526 0 ▶ my $max_tries = $opts{max_tries} || 10;
Condition0%0/2
$opts{'max_tries'} || 10
l!l
00
MC/DC0%0/1
$opts{'max_tries'} || 10
$opts{'max_tries'}
Truth table0%0/2
A
A= $opts{'max_tries'}
Aresult
11
00
527  
528 0 assign_initial_bonds($mol);
529 0 assign_initial_coordinations($mol);
530 0 my $tries = 0;
531 0 ▶ while (resolve_conflicts($mol)) {
Branch: 1 branch
BranchTrueFalseTotal
if (resolve_conflicts($mol)) 0 0 0
532 0 ▶ last if $tries++ > $max_tries;
Branch: 1 branch
BranchTrueFalseTotal
if $tries++ > $max_tries 0 0 0
533 0 ▶ print "try again\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
534 }
535 0 $tries;
536 }
537  
538 0 ▶sub assign_initial_bonds {
Subroutine assign_initial_bonds: not called CC=14 SCAR=53.5
Pod undocumented
539 0 my ($mol) = @_;
540  
541 0 for my $bond ($mol->bonds) {
542 #my %bond_has = map { ($_->symbol, 1 ) } $bond->atoms;
543 0 my $symbols = join " ", map { $_->symbol } $bond->atoms;
544 0 my ($order, $confidence);
545 0 my $l_obs = $bond->length;
546 0 ▶ if ($symbols =~ /\b(H|F|Cl|Br|I)\b/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($symbols =~ /\b(H|F|Cl|Br|I)\b/) { } 0 0 0
elsif ($symbols =~ /\bSi\b/) { } 0 0 0
elsif ($symbols =~ /\bB\b/) { } 0 0 0
elsif (my $pars = $Bond_Orders{$symbols}) { } 0 0 0
547 0 $order = 1;
548 0 $confidence = 10000;
549 } elsif ($symbols =~ /\bSi\b/) {
550 0 $order = 1;
551 0 $confidence = 20 * ($l_obs - 1.4);
552 } elsif ($symbols =~ /\bB\b/) {
553 0 $order = 1;
554 0 $confidence = 20 * ($l_obs - 1.2);
555 } elsif (my $pars = $Bond_Orders{$symbols}) {
556 0 ▶ if ($pars->{it}) { # may have triple bonds
Branch: 1 branch
BranchTrueFalseTotal
if ($pars->{'it'}) { } 0 0 0
557 0 ▶ if ($l_obs > $pars->{wsd}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($l_obs > $pars->{'wsd'}) { } 0 0 0
elsif ($pars->{'id'} < $l_obs and $l_obs < $pars->{'wsd'}) { } 0 0 0
elsif ($pars->{'id'} > $l_obs and $l_obs > $pars->{'wdt'}) { } 0 0 0
558 0 $order = 1;
559 $confidence = ($l_obs - $pars->{wsd}) /
560 0 (2 * ($pars->{is} - $pars->{wsd}));
561 ▶ } elsif ($pars->{id} < $l_obs and $l_obs < $pars->{wsd}) {
Condition0%0/3
$pars->{"id"} < $l_obs and $l_obs < $pars->{"wsd"}
!ll&&!rl&&r
000
MC/DC0%0/2
$pars->{"id"} < $l_obs and $l_obs < $pars->{"wsd"}
$pars->{"id"} < $l_obs $l_obs < $pars->{"wsd"}
Truth table0%0/3
A and B
A= $pars->{"id"} < $l_obsB= $l_obs < $pars->{"wsd"}
ABresult
0X0
100
111
562 0 $order = 2;
563 $confidence = ($pars->{wsd} - $l_obs) /
564 0 (2 * ($pars->{wsd} - $pars->{id}));
565 ▶ } elsif ($pars->{id} > $l_obs and $l_obs > $pars->{wdt}) {
Condition0%0/3
$pars->{"id"} > $l_obs and $l_obs > $pars->{"wdt"}
!ll&&!rl&&r
000
MC/DC0%0/2
$pars->{"id"} > $l_obs and $l_obs > $pars->{"wdt"}
$pars->{"id"} > $l_obs $l_obs > $pars->{"wdt"}
Truth table0%0/3
A and B
A= $pars->{"id"} > $l_obsB= $l_obs > $pars->{"wdt"}
ABresult
0X0
100
111
566 0 $order = 2;
567 $confidence = ($l_obs - $pars->{wdt}) /
568 0 (2 * ($pars->{id} - $pars->{wdt}));
569 } else {
570 0 $order = 3;
571 $confidence = ($pars->{wdt} - $l_obs) /
572 0 (2 * ($pars->{wdt} - $pars->{it}));
573 }
574 } else { # only single and double
575 0 ▶ if ($l_obs > $pars->{wsd}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l_obs > $pars->{'wsd'}) { } 0 0 0
576 0 $order = 1;
577 $confidence = ($l_obs - $pars->{wsd}) /
578 0 (2 * ($pars->{is} - $pars->{wsd}));
579 } else {
580 0 $order = 2;
581 $confidence = ($pars->{wsd} - $l_obs) /
582 0 (2 * ($pars->{wsd} - $pars->{id}));
583 }
584 }
585 } else { # unknown atom pair; let's assume it's always single
586 0 $order = 1;
587 0 $confidence = 100_000;
588 }
589  
590 0 $confidence *= 10; # normalization constant
591 0 $bond->order($order);
592 0 $bond->attr("bond-find/confidence", $confidence);
593 0 ▶ print "bond $bond($symbols) len=$l_obs, order=$order, ",
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
594 "conf=$confidence\n"
595 if $DEBUG;
596 }
597 }
598  
599 0 ▶sub assign_initial_coordinations {
Subroutine assign_initial_coordinations: not called CC=20 SCAR=60.4
Pod undocumented
600 0 my ($mol) = @_;
601  
602 0 for my $atom ($mol->atoms) {
603 0 my @neighbors = $atom->neighbors;
604 0 my $a_obs = 0;
605 0 my $n = 0;
606 0 my ($max_conns, $confidence);
607 0 ▶ for (my $i = 0; $i < @neighbors - 1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @neighbors - 1) 0 0 0
608 0 ▶ for (my $j = $i + 1; $j < @neighbors; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @neighbors) 0 0 0
609 0 my $angle = Chemistry::Atom::angle_deg($neighbors[$i],
610 $atom, $neighbors[$j]);
611 # don't count linear angles for octahedral geometries
612 0 ▶ $n++, $a_obs += $angle unless @neighbors > 2 and $angle > 150;
Branch: 1 branch
BranchTrueFalseTotal
unless @neighbors > 2 and $angle > 150 0 0 0
Condition0%0/3
@neighbors > 2 and $angle > 150
!ll&&!rl&&r
000
MC/DC0%0/2
@neighbors > 2 and $angle > 150
@neighbors > 2 $angle > 150
Truth table0%0/3
A and B
A= @neighbors > 2B= $angle > 150
ABresult
0X0
100
111
613 }
614 }
615 0 ▶ $a_obs /= $n if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 0 0 0
616  
617 0 ▶ if ($n == 0) {
Branch: 7 branches
BranchTrueFalseTotal
if ($n == 0) { } 0 0 0
elsif ($a_obs > 150) { } 0 0 0
elsif (120 > $a_obs and $a_obs >= 115) { } 0 0 0
elsif (150 > $a_obs and $a_obs >= 120) { } 0 0 0
elsif (109.5 > $a_obs and $a_obs >= 99) { } 0 0 0
elsif (115 > $a_obs and $a_obs >= 109.5) { } 0 0 0
elsif ($a_obs < 99) { } 0 0 0
618 0 $max_conns = 1; # linear
619 0 $confidence = 10000;
620 0 $n = 1;
621 } elsif ($a_obs > 150) {
622 0 $max_conns = 2; # linear
623 0 $confidence = ($a_obs - 150) / 30;
624 ▶ } elsif (120 > $a_obs and $a_obs >= 115) {
Condition0%0/3
120 > $a_obs and $a_obs >= 115
!ll&&!rl&&r
000
MC/DC0%0/2
120 > $a_obs and $a_obs >= 115
120 > $a_obs $a_obs >= 115
Truth table0%0/3
A and B
A= 120 > $a_obsB= $a_obs >= 115
ABresult
0X0
100
111
625 0 $max_conns = 3; # trigonal
626 0 $confidence = ($a_obs - 115) / 5;
627 ▶ } elsif (150 > $a_obs and $a_obs >= 120) {
Condition0%0/3
150 > $a_obs and $a_obs >= 120
!ll&&!rl&&r
000
MC/DC0%0/2
150 > $a_obs and $a_obs >= 120
150 > $a_obs $a_obs >= 120
Truth table0%0/3
A and B
A= 150 > $a_obsB= $a_obs >= 120
ABresult
0X0
100
111
628 0 $max_conns = 3; # trigonal
629 0 $confidence = (150 - $a_obs) / 30;
630 ▶ } elsif (109.5 > $a_obs and $a_obs >= 99) {
Condition0%0/3
"109.5" > $a_obs and $a_obs >= 99
!ll&&!rl&&r
000
MC/DC0%0/2
"109.5" > $a_obs and $a_obs >= 99
"109.5" > $a_obs $a_obs >= 99
Truth table0%0/3
A and B
A= "109.5" > $a_obsB= $a_obs >= 99
ABresult
0X0
100
111
631 0 $max_conns = 4; # tetrahedral
632 0 $confidence = ($a_obs - 99) / 10.5;
633 ▶ } elsif (115 > $a_obs and $a_obs >= 109.5) {
Condition0%0/3
115 > $a_obs and $a_obs >= "109.5"
!ll&&!rl&&r
000
MC/DC0%0/2
115 > $a_obs and $a_obs >= "109.5"
115 > $a_obs $a_obs >= "109.5"
Truth table0%0/3
A and B
A= 115 > $a_obsB= $a_obs >= "109.5"
ABresult
0X0
100
111
634 0 $max_conns = 4; # tetrahedral
635 0 $confidence = (115 - $a_obs) / 5.5;
636 } elsif ($a_obs < 99) {
637 0 $max_conns = 6; # octahedral
638 0 $confidence = (9 - $a_obs) / 9;
639 } else {
640 0 confess("impossible coordination angle $a_obs!");
641 }
642  
643 0 $confidence *= 100 * $n;
644 0 ▶ if ($Valences{$atom->symbol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Valences{$atom->symbol}) { } 0 0 0
645 0 $atom->attr("bond-find/valence", $Valences{$atom->symbol}[0]);
646 } else {
647 0 $atom->attr("bond-find/valence", $max_conns);
648 }
649 0 $atom->attr("bond-find/confidence", $confidence);
650 0 $atom->attr("bond-find/max_conns", $max_conns);
651 0 ▶ print "Atom $atom has CN $max_conns with a conf. of $confidence\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
652 if $DEBUG;
653 }
654 }
655  
656 0 ▶sub resolve_conflicts {
Subroutine resolve_conflicts: not called CC=25 SCAR=64.8
Pod undocumented
657 0 my ($mol) = @_;
658
659 0 my $changes = 0;
660 0 for my $atom ($mol->atoms) {
661 0 my $valence = $atom->attr('bond-find/valence');
662 0 my $max_conns = $atom->attr('bond-find/max_conns');
663 0 my $confidence = $atom->attr('bond-find/confidence');
664 0 my $n_conns = $atom->bonds;
665  
666 0 my $n_bonds = 0;
667 0 $n_bonds += $_->order for $atom->bonds;
668  
669 0 ▶ if ($n_conns > $valence) {
Branch: 3 branches
BranchTrueFalseTotal
if ($n_conns > $valence) { } 0 0 0
elsif ($n_bonds > $valence) { } 0 0 0
elsif ($n_bonds + $max_conns - $n_conns < $valence) { } 0 0 0
670 0 my $next_valence = next_valence($atom->symbol, $valence);
671 0 ▶ if ($next_valence) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_valence) { } 0 0 0
672 0 ▶ print "increasing valence of $atom to $next_valence\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
673 if $DEBUG;
674 0 $atom->attr('bond-find/valence', $next_valence);
675 0 ++$changes, redo;
676 } else {
677 0 warn "too many conns $n_conns to $atom with valence $valence\n";
678 0 next;
679 }
680 } elsif ($n_bonds > $valence) {
681 0 my $next_valence = next_valence($atom->symbol, $valence);
682 0 ▶ if ($next_valence) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_valence) 0 0 0
683 0 ▶ print "increasing valence of $atom to $next_valence\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
684 if $DEBUG;
685 0 $atom->attr('bond-find/valence', $next_valence);
686 0 ++$changes, next;
687 }
688 0 my ($min_conf, $min_bond);
689 0 for my $bond ($atom->bonds) {
690 0 ▶ if ($bond->order > 1 and
Branch: 1 branch
BranchTrueFalseTotal
if ($bond->order > 1 and !defined($min_conf) || $bond->attr("bond-find/confidence") < $min_conf) 0 0 0
Condition0%0/6
$bond->order > 1 and !defined($min_conf) || $bond->attr("bond-find/confidence") < $min_conf
!ll&&!rl&&r
000
not defined $min_conf or $bond->attr("bond-find/confidence") < $min_conf
l!l&&r!l&&!r
000
MC/DC0%0/3
$bond->order > 1 and !defined($min_conf) || $bond->attr("bond-find/confidence") < $min_conf
$bond->order > 1 not defined $min_conf $bond->attr("bond-find/confidence") < $min_conf
Truth table0%0/4
A and not(B) or C
A= $bond->order > 1B= not defined $min_confC= $bond->attr("bond-find/confidence") < $min_conf
ABCresult
0XX0
1000
11X1
1011
691 (not defined $min_conf
692 or $bond->attr("bond-find/confidence") < $min_conf)) {
693 0 $min_conf = $bond->attr("bond-find/confidence");
694 0 $min_bond = $bond;
695 }
696 }
697 0 my $new_order = $min_bond->order - 1;
698 0 $min_bond->order($new_order);
699 0 ▶ print "Decreasing order of $min_bond to $new_order\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
700 0 $min_bond->attr('bond-find/confidence', $min_conf*1.2+rand());
701 0 ++$changes, next;
702 } elsif ($n_bonds + $max_conns - $n_conns < $valence) {
703 0 my ($min_conf, $min_bond);
704 0 for my $bond ($atom->bonds) {
705 0 ▶ if (($bond->order == 1
Branch: 1 branch
BranchTrueFalseTotal
if ($bond->order == 1 || $atom->symbol =~ /^[CN]$/ && $bond->order == 2 and !defined($min_conf) || $bond->attr("bond-find/confidence") < $min_conf) 0 0 0
Condition0%0/12
$bond->order == 1 || $atom->symbol =~ /^[CN]$/ && $bond->order == 2 and !defined($min_conf) || $bond->attr("bond-find/confidence") < $min_conf
!ll&&!rl&&r
000
$bond->order == 1 or $atom->symbol =~ /^[CN]$/ and $bond->order == 2
l!l&&r!l&&!r
000
$atom->symbol =~ /^[CN]$/ and $bond->order == 2
!ll&&!rl&&r
000
not defined $min_conf or $bond->attr("bond-find/confidence") < $min_conf
l!l&&r!l&&!r
000
MC/DC0%0/5
$bond->order == 1 || $atom->symbol =~ /^[CN]$/ && $bond->order == 2 and !defined($min_conf) || $bond->attr("bond-find/confidence") < $min_conf
$bond->order == 1 $atom->symbol =~ /^[CN]$/ $bond->order == 2 not defined $min_conf $bond->attr("bond-find/confidence") < $min_conf
Truth table0%0/8
A or B and C and not(D) or E
A= $bond->order == 1B= $atom->symbol =~ /^[CN]$/C= $bond->order == 2D= not defined $min_confE= $bond->attr("bond-find/confidence") < $min_conf
ABCDEresult
00XXX0
010XX0
1XX000
011000
1XX1X1
1XX011
0111X1
011011
706 or $atom->symbol =~ /^[CN]$/ and $bond->order == 2) and
707 (not defined $min_conf
708 or $bond->attr("bond-find/confidence") < $min_conf)) {
709 0 $min_conf = $bond->attr("bond-find/confidence");
710 0 $min_bond = $bond;
711 }
712 }
713  
714 0 ▶ if ($confidence > 95 or $min_conf < $confidence) {
Branch: 1 branch
BranchTrueFalseTotal
if ($confidence > 95 or $min_conf < $confidence) { } 0 0 0
Condition0%0/3
$confidence > 95 or $min_conf < $confidence
l!l&&r!l&&!r
000
MC/DC0%0/2
$confidence > 95 or $min_conf < $confidence
$confidence > 95 $min_conf < $confidence
Truth table0%0/3
A or B
A= $confidence > 95B= $min_conf < $confidence
ABresult
1X1
011
000
715 0 my $new_order = $min_bond->order + 1;
716 0 $min_bond->order($new_order);
717 0 ▶ print "Increasing order of $min_bond to $new_order\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
718 0 $min_bond->attr('bond-find/confidence', $min_conf*1.2+rand());
719 } else {
720 0 $max_conns++;
721 0 $atom->attr("bond-find/max_conns", $max_conns);
722 0 ▶ print "Increasing coord. num. of $min_bond to $max_conns\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Bond::Find::DEBUG 0 0 0
723 if $DEBUG;
724 }
725 0 ++$changes, next;
726 }
727 }
728 0 $changes;
729 }
730  
731 0 ▶sub next_valence {
Subroutine next_valence: not called CC=4 SCAR=30.0
Pod undocumented
732 0 my ($symbol, $current) = @_;
733  
734 0 ▶ if ($Valences{$symbol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Valences{$symbol}) 0 0 0
735 0 for my $v (@{$Valences{$symbol}}) {
736 0 ▶ return $v if $v > $current;
Branch: 1 branch
BranchTrueFalseTotal
if $v > $current 0 0 0
737 }
738 }
739 0 return undef;
740 }
741  
742 11 1;
743  
744 =back
745  
746 =head1 VERSION
747  
748 0.21
749  
750 =head1 TO DO
751  
752 Some future version should let the user specify the desired cutoffs, and
753 not always create a bond but call a user-supplied function instead. This way
754 these functions could be used for other purposes such as finding hydrogen bonds
755 or neighbor lists.
756  
757 Add some tests.
758  
759 =head1 SEE ALSO
760  
761 L<Chemistry::Mol>, L<Chemistry::Atom>, L<Chemistry::Bond>,
762 L<http://www.perlmol.org/>.
763  
764 =head1 AUTHOR
765  
766 Ivan Tubert E<lt>itub@cpan.orgE<gt>
767  
768 The new C<find_bonds> algorithm was loosely based on a suggestion by BrowserUK
769 on perlmonks.org (L<http://perlmonks.org/index.pl?node_id=352838>).
770  
771 =head1 COPYRIGHT
772  
773 Copyright (c) 2004 Ivan Tubert. All rights reserved. This program is free
774 software; you can redistribute it and/or modify it under the same terms as
775 Perl itself.
776  
777 =cut
778  
« blib/lib/Chemistry/File/MDLMol.pm ↑ Summary blib/lib/Chemistry/Mok.pm »