Atom.pm

filter: Statementstmt 83.1% 237 / 285 Branchbran 72.2% 65 / 90 Conditioncond 59.4% 22 / 37 MC/DCmcdc 47.8% 11 / 23 Subroutinesub 90.0% 45 / 50 Podpod 88.2% 30 / 34 totaltotal 79.0% 410 / 519 CC 72 SCAR 18.5
CC
72
Cov
79%
internal_coords
20.0
bonds_neighbors
20.0
distance
13.2
CRAP 6.3
« Mol.pm ↑ Summary File/Formula.pm »
1 package Chemistry::Atom;
2  
3 16 our $VERSION = '0.40'; # VERSION
4 # $Id$
5  
6 =head1 NAME
7  
8 Chemistry::Atom - Chemical atoms as objects in molecules
9  
10 =head1 SYNOPSIS
11  
12 use Chemistry::Atom;
13  
14 my $atom = new Chemistry::Atom(
15 id => 'a1',
16 coords => [$x, $y, $z],
17 symbol => 'Br'
18 );
19  
20 print $atom->print;
21  
22 =head1 DESCRIPTION
23  
24 This module includes objects to describe chemical atoms.
25 An atom is defined by its symbol and its coordinates, among other attributes.
26 Atomic coordinates are described by a Math::VectorReal
27 object, so that they can be easily used in vector operations.
28  
29 =head2 Atom Attributes
30  
31 In addition to common attributes such as id, name, and type,
32 atoms have the following attributes, which are accessed or
33 modified through methods defined below: bonds, coords, internal_coords,
34 Z, symbol, etc.
35  
36 In general, to get the value of a property, use $atom->method without
37 any parameters. To set the value, use $atom->method($new_value). When setting
38 an attribute, the accessor returns the atom object, so that accessors can be
39 chained:
40  
41 $atom->symbol("C")->name("CA")->coords(1,2,3);
42  
43 =cut
44  
45 # Considering to add the following attributes:
46 # mass_number (A)
47  
48 16 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 16 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 16 ▶use Math::VectorReal qw(O vector);
Subroutine BEGIN: called CC=1 SCAR=0.0
53 16 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 16 ▶use base qw(Chemistry::Obj Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 16 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 16 our @EXPORT_OK = qw(distance angle dihedral angle_deg dihedral_deg);
59 16 our %EXPORT_TAGS = (
60 all => \@EXPORT_OK,
61 );
62  
63  
64  
65 16 my $N = 0; # Number of atoms created so far, used to generate default IDs.
66  
67 16 our @ELEMENTS = qw(
68 n
69 H He
70 Li Be B C N O F Ne
71 Na Mg Al Si P S Cl Ar
72 K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr
73 Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe
74 Cs Ba
75 La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb
76 Lu Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn
77 Fr Ra
78 Ac Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No
79 Lr Rf Db Sg Bh Hs Mt Ds Uuu Uub Uut Uuq Uup Uuh Uus Uuo
80 );
81  
82 16 our %ELEMENTS;
83 16 ▶for (my $i = 1; $i < @ELEMENTS; ++$i){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @Chemistry::Atom::ELEMENTS) 1888 16 1904
84 1888 $ELEMENTS{$ELEMENTS[$i]} = $i;
85 }
86 16 $ELEMENTS{D} = $ELEMENTS{T} = 1;
87  
88 16 my %Atomic_masses = (
89 "H" => 1.00794, "D" => 2.014101, "T" => 3.016049, "He" => 4.002602,
90 "Li" => 6.941, "Be" => 9.012182, "B" => 10.811, "C" => 12.0107,
91 "N" => 14.00674, "O" => 15.9994, "F" => 18.9984032, "Ne" => 20.1797,
92 "Na" => 22.989770, "Mg" => 24.3050, "Al" => 26.981538, "Si" => 28.0855,
93 "P" => 30.973761, "S" => 32.066, "Cl" => 35.4527, "Ar" => 39.948,
94 "K" => 39.0983, "Ca" => 40.078, "Sc" => 44.955910, "Ti" => 47.867,
95 "V" => 50.9415, "Cr" => 51.9961, "Mn" => 54.938049, "Fe" => 55.845,
96 "Co" => 58.933200, "Ni" => 58.6934, "Cu" => 63.546, "Zn" => 65.39,
97 "Ga" => 69.723, "Ge" => 72.61, "As" => 74.92160, "Se" => 78.96,
98 "Br" => 79.904, "Kr" => 83.80, "Rb" => 85.4678, "Sr" => 87.62,
99 "Y" => 88.90585, "Zr" => 91.224, "Nb" => 92.90638, "Mo" => 95.94,
100 "Tc" => 98, "Ru" => 101.07, "Rh" => 102.90550, "Pd" => 106.42,
101 "Ag" => 107.8682, "Cd" => 112.411, "In" => 114.818, "Sn" => 118.710,
102 "Sb" => 121.760, "Te" => 127.60, "I" => 126.90447, "Xe" => 131.29,
103 "Cs" => 132.90545, "Ba" => 137.327, "La" => 138.9055, "Ce" => 140.116,
104 "Pr" => 140.90765, "Nd" => 144.24, "Pm" => 145, "Sm" => 150.36,
105 "Eu" => 151.964, "Gd" => 157.25, "Tb" => 158.92534, "Dy" => 162.50,
106 "Ho" => 164.93032, "Er" => 167.26, "Tm" => 168.93421, "Yb" => 173.04,
107 "Lu" => 174.967, "Hf" => 178.49, "Ta" => 180.9479, "W" => 183.84,
108 "Re" => 186.207, "Os" => 190.23, "Ir" => 192.217, "Pt" => 195.078,
109 "Au" => 196.96655, "Hg" => 200.59, "Tl" => 204.3833, "Pb" => 207.2,
110 "Bi" => 208.98038, "Po" => 209, "At" => 210, "Rn" => 222,
111 "Fr" => 223, "Ra" => 226, "Ac" => 227, "Th" => 232.038,
112 "Pa" => 231.03588, "U" => 238.0289, "Np" => 237, "Pu" => 244,
113 "Am" => 243, "Cm" => 247, "Bk" => 247, "Cf" => 251,
114 "Es" => 252, "Fm" => 257, "Md" => 258, "No" => 259,
115 "Lr" => 262, "Rf" => 261, "Db" => 262, "Sg" => 266,
116 "Bh" => 264, "Hs" => 269, "Mt" => 268, "Ds" => 271,
117 );
118  
119 =head1 METHODS
120  
121 =over 4
122  
123 =item Chemistry::Atom->new(name => value, ...)
124  
125 Create a new Atom object with the specified attributes.
126  
127 =cut
128  
129 536 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
130 536 my $class = shift;
131 536 my %args = @_;
132  
133 536 my $self = bless {
134 id => $class->nextID(),
135 coords => vector(0, 0, 0),
136 Z => 0,
137 symbol => '',
138 bonds => [],
139 }, $class;
140  
141 536 $self->$_($args{$_}) for (keys %args);
142 536 $self;
143 }
144  
145 538 ▶sub nextID {
Subroutine nextID: called CC=1 SCAR=0.0
Pod undocumented
146 538 "a".++$N;
147 }
148  
149 0 ▶sub reset_id {
Subroutine reset_id: not called CC=1 SCAR=6.9
Pod undocumented
150 0 $N = 0;
151 }
152  
153  
154 =item $atom->Z($new_Z)
155  
156 Sets and returns the atomic number (Z). If the symbol of the atom doesn't
157 correspond to a known element, Z = undef.
158  
159 =cut
160  
161 7 ▶sub Z {
Subroutine Z: called CC=2 SCAR=6.9
Pod documented
162 7 my $self = shift;
163  
164 7 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4 3 7
165 4 $self->{symbol} = $ELEMENTS[$_[0]];
166 4 $self->{Z} = $_[0];
167 4 return $self;
168 } else {
169 3 return $self->{Z};
170 }
171 }
172  
173  
174 =item $atom->symbol($new_symbol)
175  
176 Sets and returns the atomic symbol.
177  
178 =cut
179  
180 1091 ▶sub symbol {
Subroutine symbol: called CC=2 SCAR=6.9
Pod documented
181 1091 my $self = shift;
182  
183 1091 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 531 560 1091
184 531 my $symbol = shift;
185 531 $symbol =~ s/ //g;
186 531 $self->{Z} = $ELEMENTS{$symbol};
187 531 $self->{symbol} = $symbol;
188 531 return $self;
189 } else {
190 560 return $self->{symbol};
191 }
192 }
193  
194 =item $atom->mass($new_mass)
195  
196 Sets and returns the atomic mass in atomic mass units. Any arbitrary mass may
197 be set explicitly by using this method. However, if no mass is set explicitly
198 and this method is called as an accessor, the return value is the following:
199  
200 1) If the mass number is undefined (see the mass_number method below), the
201 relative atomic mass from the 1995 IUPAC recommendation is used. (Table stolen
202 from the Chemistry::MolecularMass module by Maksim A. Khrapov).
203  
204 2) If the mass number is defined and the L<Chemistry::Isotope> module is
205 available and it knows the mass for the isotope, the exact mass of the isotope
206 is used; otherwise, the mass number is returned.
207  
208 =cut
209  
210 13 ▶sub mass {
Subroutine mass: called CC=6 SCAR=17.9
Pod documented
211 13 my $self = shift;
212 13 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 11 13
213 2 $self->{mass} = shift;
214 2 return $self;
215 } else {
216 11 ▶ if (defined $self->{mass}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'mass'}) { } 2 9 11
elsif (defined $self->{'mass_number'}) { } 2 7 9
217 2 return $self->{mass};
218 } elsif (defined $self->{mass_number}) {
219 2 ▶ if (eval { require Chemistry::Isotope } and
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Chemistry::Isotope } } and my $m = Chemistry::Isotope::isotope_mass($self->{'mass_number'}, $self->{'Z'})) { } 1 1 2
Condition66%2/3
eval { do { require Chemistry::Isotope } } and my $m = Chemistry::Isotope::isotope_mass($self->{"mass_number"}, $self->{"Z"})
!ll&&!rl&&r
011
MC/DC50%1/2
eval { do { require Chemistry::Isotope } } and my $m = Chemistry::Isotope::isotope_mass($self->{"mass_number"}, $self->{"Z"})
eval { do { require Chemistry::Isotope } } my $m = Chemistry::Isotope::isotope_mass($self->{"mass_number"}, $self->{"Z"})
Truth table66%2/3
A and B
A= eval { do { require Chemistry::Isotope } }B= my $m = Chemistry::Isotope::isotope_mass($self->{"mass_number"}, $self->{"Z"})
ABresult
0X0
100
111
220 my $m = Chemistry::Isotope::isotope_mass(
221 $self->{mass_number}, $self->{Z})
222 ) {
223 1 return $m;
224 } else {
225 1 return $self->{mass_number};
226 }
227 } else {
228 7 return $Atomic_masses{$self->symbol};
229 }
230 }
231 }
232  
233 =item $atom->mass_number($new_mass_number)
234  
235 Sets or gets the mass number. The mass number is undefined unless is
236 set explicitly (this module does not try to guess a default mass number based
237 on the natural occurring isotope distribution).
238  
239 =cut
240  
241 16 Chemistry::Obj::accessor('mass_number');
242  
243 =item $atom->coords
244  
245 my $vector = $atom->coords; # get a Math::VectorReal object
246 $atom->coords($vector); # set a Math::VectorReal object
247 $atom->coords([$x, $y, $z]); # also accepts array refs
248 $atom->coords($x, $y, $z); # also accepts lists
249  
250 Sets or gets the atom's coordinates. It can take as a parameter a
251 Math::VectorReal object, a reference to an array, or the list of coordinates.
252  
253 =cut
254  
255 29 ▶sub coords {
Subroutine coords: called CC=4 SCAR=13.9
Pod documented
256 29 my $self = shift;
257  
258 29 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 6 23 29
259 6 ▶ if (UNIVERSAL::isa($_[0], "Math::VectorReal")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'Math::VectorReal')) { } 2 4 6
elsif (ref $_[0] eq 'ARRAY') { } 3 1 4
260 2 $self->{coords} = $_[0];
261 } elsif (ref $_[0] eq "ARRAY") {
262 3 $self->{coords} = vector(@{$_[0]});
263 } else {
264 1 $self->{coords} = vector(@_);
265 }
266 } else {
267 23 return $self->{coords};
268 }
269 6 $self;
270 }
271  
272 =item $atom->internal_coords
273  
274 # get a Chemistry::InternalCoords object
275 my $ic = $atom->internal_coords;
276  
277 # set a Chemistry::InternalCoords object
278 $atom->internal_coords($vic);
279  
280 # also accepts array refs
281 $atom->internal_coords([8, 1.54, 7, 109.47, 6, 120.0]);
282  
283 # also accepts lists
284 $atom->internal_coords(8, 1.54, 7, 109.47, 6, 120.0);
285  
286 Sets or gets the atom's internal coordinates. It can take as a parameter a
287 Chemistry::InternalCoords object, a reference to an array, or the list of
288 coordinates. In the last two cases, the list elements are the following: atom
289 number or reference for distance, distance, atom number or reference for angle,
290 angle in degrees, atom number or reference for dihedral, dihedral in degrees.
291  
292 =cut
293  
294 0 ▶sub internal_coords {
Subroutine internal_coords: not called CC=4 SCAR=30.0
Pod documented
295 0 my $self = shift;
296  
297 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
298 0 ▶ if (UNIVERSAL::isa($_[0], "Chemistry::InternalCoords")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'Chemistry::InternalCoords')) { } 0 0 0
elsif (ref $_[0] eq 'ARRAY') { } 0 0 0
299 0 $self->{internal_coords} = $_[0];
300 } elsif (ref $_[0] eq "ARRAY") {
301 0 require Chemistry::InternalCoords;
302 $self->{internal_coords} =
303 0 Chemistry::InternalCoords->new($self, @{$_[0]});
304 } else {
305 0 require Chemistry::InternalCoords;
306 $self->{internal_coords} =
307 0 Chemistry::InternalCoords->new($self, @_);
308 }
309 } else {
310 0 return $self->{internal_coords};
311 }
312 0 $self;
313 }
314  
315 =item $atom->x3, $atom->y3, $atom->z3
316  
317 Get the x, y or z 3D coordinate of the atom. This methods are just accessors
318 that don't change the coordinates. $atom->x3 is short for
319 ($atom->coords->array)[0], and so on.
320  
321 =cut
322  
323 2 ▶sub x3 { ($_[0]->coords->array)[0] }
Subroutine x3: called CC=1 SCAR=0.0
Pod documented
324 2 ▶sub y3 { ($_[0]->coords->array)[1] }
Subroutine y3: called CC=1 SCAR=0.0
Pod documented
325 2 ▶sub z3 { ($_[0]->coords->array)[2] }
Subroutine z3: called CC=1 SCAR=0.0
Pod documented
326  
327 =item $atom->formal_charge($charge)
328  
329 Set or get the formal charge of the atom.
330  
331 =cut
332  
333 16 Chemistry::Obj::accessor('formal_charge');
334  
335 =item $atom->formal_radical($radical)
336  
337 Set or get the formal radical multiplicity for the atom.
338  
339 =cut
340  
341 16 Chemistry::Obj::accessor('formal_radical');
342  
343 =item $atom->implicit_hydrogens($h_count)
344  
345 Set or get the number of implicit hydrogen atoms bonded to the atom.
346  
347 =cut
348  
349 21 ▶sub implicit_hydrogens { shift->hydrogens(@_) }
Subroutine implicit_hydrogens: called CC=1 SCAR=0.0
Pod documented
350  
351 =item $atom->hydrogens($h_count)
352  
353 Set or get the number of implicit hydrogen atoms bonded to the atom
354 (DEPRECATED: USE C<implicit_hydrogens> INSTEAD).
355  
356 =cut
357  
358 16 Chemistry::Obj::accessor('hydrogens');
359  
360 =item $atom->total_hydrogens($h_count)
361  
362 Get the total number of hydrogen atoms bonded to the atom (implicit +
363 explicit).
364  
365 =cut
366  
367 5 ▶sub total_hydrogens {
Subroutine total_hydrogens: called CC=1 SCAR=0.0
Pod documented
368 5 my ($self) = @_;
369 16 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
370 5 $self->hydrogens + grep { $_->symbol eq 'H' } $self->neighbors;
371 }
372  
373 =item $atom->sprout_hydrogens
374  
375 Convert all the implicit hydrogens for this atom to explicit hydrogens. Note:
376 it does B<not> generate coordinates for the new atoms.
377  
378 =cut
379  
380 2 ▶sub sprout_hydrogens {
Subroutine sprout_hydrogens: called CC=3 SCAR=11.1
Pod documented
381 2 my ($self) = @_;
382 2 ▶ for (1 .. $self->implicit_hydrogens || 0) {
Condition50%1/2
$self->implicit_hydrogens || 0
l!l
20
MC/DC0%0/1
$self->implicit_hydrogens || 0
$self->implicit_hydrogens
Truth table50%1/2
A
A= $self->implicit_hydrogens
Aresult
11
00
383 4 $self->parent->new_bond(
384 atoms => [$self, $self->parent->new_atom(symbol => 'H')]);
385 }
386 2 $self->implicit_hydrogens(0);
387 }
388  
389 =item $atom->collapse_hydrogens
390  
391 Delete neighboring hydrogen atoms and add them as implicit hydrogens for this
392 atom.
393  
394 =cut
395  
396 2 ▶sub collapse_hydrogens {
Subroutine collapse_hydrogens: called CC=3 SCAR=11.0
Pod documented
397 2 my ($self) = @_;
398 16 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
399 2 my $implicit = $self->implicit_hydrogens;
400 2 for my $nei ($self->neighbors) {
401 4 ▶ $nei->delete, $implicit++ if $nei->symbol eq 'H';
Branch: 1 branch
BranchTrueFalseTotal
if $nei->symbol eq "H" 4 0 4
402 }
403 2 $self->implicit_hydrogens($implicit);
404 }
405  
406 16 my %VALENCE_TABLE = (
407 Br => 1, Cl => 1, B => 3, C => 4, N => 3, O => 2, P => 3, S => 2,
408 F => 1, I => 1,
409 );
410  
411 # to make it easier to test
412 21 ▶sub _calc_implicit_hydrogens {
Subroutine _calc_implicit_hydrogens: called CC=13 SCAR=25.7
413 21 my ($self, $symbol, $valence, $charge, $radical) = @_;
414 16 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
415  
416 21 my $h_count = $VALENCE_TABLE{$symbol} - $valence;
417 # should account for non-kekulized aromatic bonds
418  
419 # some common charge situations
420 21 ▶ if (($symbol =~ /^(?:[NOSFI]|Cl|Br)$/) && $charge == -1) {
Branch: 4 branches
BranchTrueFalseTotal
if ($symbol =~ /^(?:[NOSFI]|Cl|Br)$/ and $charge == -1) { } 4 17 21
elsif ($symbol =~ /^[NOSP]$/ and $charge == 1) { } 4 13 17
elsif ($symbol eq 'C' and $charge) { } 2 11 13
elsif ($symbol eq 'B' and $charge == -1) { } 1 20 21
Condition100%3/3
$symbol =~ /^(?:[NOSFI]|Cl|Br)$/ and $charge == -1
!ll&&!rl&&r
984
MC/DC100%2/2
$symbol =~ /^(?:[NOSFI]|Cl|Br)$/ and $charge == -1
$symbol =~ /^(?:[NOSFI]|Cl|Br)$/ $charge == -1
Truth table100%3/3
A and B
A= $symbol =~ /^(?:[NOSFI]|Cl|Br)$/B= $charge == -1
ABresult
0X0
100
111
421 4 $h_count--;
422 ▶ } elsif ($symbol =~ /^[NOSP]$/ && $charge == 1) {
Condition100%3/3
$symbol =~ /^[NOSP]$/ and $charge == 1
!ll&&!rl&&r
1214
MC/DC100%2/2
$symbol =~ /^[NOSP]$/ and $charge == 1
$symbol =~ /^[NOSP]$/ $charge == 1
Truth table100%3/3
A and B
A= $symbol =~ /^[NOSP]$/B= $charge == 1
ABresult
0X0
100
111
423 4 $h_count++;
424 ▶ } elsif ($symbol eq 'C' && $charge) {
Condition100%3/3
$symbol eq "C" and $charge
!ll&&!rl&&r
562
MC/DC100%2/2
$symbol eq "C" and $charge
$symbol eq "C" $charge
Truth table100%3/3
A and B
A= $symbol eq "C"B= $charge
ABresult
0X0
100
111
425 2 $h_count--;
426 ▶ } elsif ($symbol eq 'B' && $charge == -1) {
Condition66%2/3
$symbol eq "B" and $charge == -1
!ll&&!rl&&r
1001
MC/DC50%1/2
$symbol eq "B" and $charge == -1
$symbol eq "B" $charge == -1
Truth table66%2/3
A and B
A= $symbol eq "B"B= $charge == -1
ABresult
0X0
100
111
427 1 $h_count++;
428 }
429  
430 # some common radical situations
431 21 ▶ if ($radical == 1 or $radical == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if ($radical == 1 or $radical == 3) { } 3 18 21
elsif ($radical == 2) { } 2 16 18
Condition100%3/3
$radical == 1 or $radical == 3
l!l&&r!l&&!r
1218
MC/DC100%2/2
$radical == 1 or $radical == 3
$radical == 1 $radical == 3
Truth table100%3/3
A or B
A= $radical == 1B= $radical == 3
ABresult
1X1
011
000
432 # carbene, singlet or triplet
433 3 $h_count -= 2;
434 } elsif ($radical == 2) {
435 # radical (doublet)
436 2 $h_count--;
437 }
438  
439 21 ▶ $h_count = 0 if $h_count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $h_count < 0 0 21 21
440 21 $h_count;
441 }
442  
443 =item $atom->calc_implicit_hydrogens
444  
445 Use heuristics to figure out how many implicit hydrogens should the atom have
446 to satisfy its normal "organic" valence. Returns the number of hydrogens but
447 does not affect the atom object.
448  
449 =cut
450  
451 3 ▶sub calc_implicit_hydrogens {
Subroutine calc_implicit_hydrogens: called CC=1 SCAR=0.0
Pod documented
452 3 my ($self) = @_;
453 3 $self->_calc_implicit_hydrogens(
454 $self->symbol, $self->explicit_valence,
455 $self->formal_charge, $self->formal_radical,
456 );
457 }
458  
459 =item $atom->add_implicit_hydrogens
460  
461 Similar to calc_implicit_hydrogens, but it also sets the number of implicit
462 hydrogens in the atom to the new calculated value. Equivalent to
463  
464 $atom->implicit_hydrogens($atom->calc_implicit_hydrogens);
465  
466 It returns the atom object.
467  
468 =cut
469  
470 3 ▶sub add_implicit_hydrogens {
Subroutine add_implicit_hydrogens: called CC=1 SCAR=0.0
Pod documented
471 3 my ($self) = @_;
472 3 my $h_count = $self->calc_implicit_hydrogens;
473 3 $self->implicit_hydrogens($h_count);
474 }
475  
476 =item $atom->aromatic($bool)
477  
478 Set or get whether the atom is considered to be aromatic. This property may be
479 set arbitrarily, it doesn't imply any kind of "intelligent aromaticity
480 detection"! (For that, look at the L<Chemistry::Ring> module).
481  
482 =cut
483  
484 16 Chemistry::Obj::accessor('aromatic');
485  
486 =item $atom->valence
487  
488 Returns the sum of the bond orders of the bonds in which the atom participates,
489 including implicit hydrogens (which are assumed to have bond orders of one).
490  
491 =cut
492  
493 3 ▶sub valence {
Subroutine valence: called CC=3 SCAR=11.0
Pod documented
494 3 my ($self) = @_;
495 3 my $valence = 0;
496 3 $valence += $_->order for $self->bonds;
497 3 ▶ $valence += $self->hydrogens || 0;
Condition100%2/2
$self->hydrogens || 0
l!l
21
MC/DC100%1/1
$self->hydrogens || 0
$self->hydrogens
Truth table100%2/2
A
A= $self->hydrogens
Aresult
11
00
498 3 $valence;
499 }
500  
501 =item $atom->explicit_valence
502  
503 Like C<valence>, but excluding implicit hydrogen atoms. To get the raw number
504 of bonds, without counting bond orders, call $atom->bonds in scalar context.
505  
506 =cut
507  
508 6 ▶sub explicit_valence {
Subroutine explicit_valence: called CC=2 SCAR=6.9
Pod documented
509 6 my ($self) = @_;
510 6 my $valence = 0;
511 6 $valence += $_->order for $self->bonds;
512 6 $valence;
513 }
514  
515 # this method is for internal use only; called by $mol->add_bond
516 20 ▶sub add_bond {
Subroutine add_bond: called CC=3 SCAR=11.0
Pod undocumented
517 20 my $self = shift;
518 20 my $bond = shift;
519 20 my %seen;
520 #return if first { $_ eq $bond } @{$self->{bonds}};
521  
522 20 for my $atom (@{$bond->{atoms}}){ #for each atom...
523 40 ▶ if ($atom ne $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atom ne $self) 20 20 40
524 20 my $b = {to=>$atom, bond=>$bond};
525 20 weaken($b->{to});
526 20 weaken($b->{bond});
527 20 push @{$self->{bonds}}, $b;
528 }
529 }
530 }
531  
532 # make sure the atom doesn't cause circular references
533 120 ▶sub _weaken {
Subroutine _weaken: called CC=2 SCAR=6.9
534 120 my $self = shift;
535 120 for my $b (@{$self->{bonds}}) {
536 182 weaken($b->{to});
537 182 weaken($b->{bond});
538 }
539 120 weaken($self->{parent});
540 }
541  
542 # This method is private. Bonds should be deleted from the
543 # mol object. These methods should only be called by
544 # $bond->delete_atoms, which is called by $mol->delete_bond
545 28 ▶sub delete_bond {
Subroutine delete_bond: called CC=1 SCAR=0.0
Pod undocumented
546 28 my ($self, $bond) = @_;
547 28 $self->{bonds} = [ grep { $_->{bond} ne $bond } @{$self->{bonds}} ];
548 }
549  
550 =item $atom->delete
551  
552 Calls $mol->delete_atom($atom) on the atom's parent molecule.
553  
554 =cut
555  
556 7 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
557 7 my ($self) = @_;
558 7 $self->{parent}->_delete_atom($self);
559 }
560  
561 =item $atom->parent
562  
563 Returns the atom's containing object (the molecule to which the atom belongs).
564 An atom can only have one parent.
565  
566 =cut
567  
568 561 ▶sub parent {
Subroutine parent: called CC=2 SCAR=6.9
Pod documented
569 561 my $self = shift;
570 561 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 553 8 561
571 553 ($self->{parent}) = @_;
572 553 weaken($self->{parent});
573 553 $self;
574 } else {
575 8 $self->{parent};
576 }
577 }
578  
579 =item $atom->neighbors($from)
580  
581 Return a list of neighbors. If an atom object $from is specified, it will be
582 excluded from the list (this is useful if an atom wants to know who its
583 neighbor's neighbors are, without counting itself).
584  
585 =cut
586  
587 17 ▶sub neighbors {
Subroutine neighbors: called CC=4 SCAR=14.5
Pod documented
588 17 my $self = shift;
589 17 my $from = shift;
590 17 my @ret = ();
591  
592 17 for my $b (@{$self->{bonds}}) {
593 24 ▶ push @ret, $b->{to} unless $from && $b->{to} eq $from;
Branch: 1 branch
BranchTrueFalseTotal
unless $from and $b->{"to"} eq $from 24 0 24
Condition33%1/3
$from and $b->{"to"} eq $from
!ll&&!rl&&r
2400
MC/DC0%0/2
$from and $b->{"to"} eq $from
$from $b->{"to"} eq $from
Truth table33%1/3
A and B
A= $fromB= $b->{"to"} eq $from
ABresult
0X0
100
111
594 }
595 17 @ret;
596 }
597  
598 =item $atom->bonds($from)
599  
600 Return a list of bonds. If an atom object $from is specified, bonds to
601 that atom will be excluded from the list.
602  
603 =cut
604  
605 22 ▶sub bonds {
Subroutine bonds: called CC=4 SCAR=14.5
Pod documented
606 22 my $self = shift;
607 22 my $from = shift;
608 22 my @ret = ();
609  
610 22 for my $b (@{$self->{bonds}}) {
611 23 ▶ push @ret, $b->{bond} unless $from && $b->{to} eq $from;
Branch: 1 branch
BranchTrueFalseTotal
unless $from and $b->{"to"} eq $from 23 0 23
Condition33%1/3
$from and $b->{"to"} eq $from
!ll&&!rl&&r
2300
MC/DC0%0/2
$from and $b->{"to"} eq $from
$from $b->{"to"} eq $from
Truth table33%1/3
A and B
A= $fromB= $b->{"to"} eq $from
ABresult
0X0
100
111
612 }
613 22 @ret;
614 }
615  
616 =item $atom->bonds_neighbors($from)
617  
618 Return a list of hash references, representing the bonds and neighbors from the
619 atom. If an atom object $from is specified, it will be excluded from the list.
620 The elements of the hash are 'to', and atom reference, and 'bond', a bond
621 reference. For example,
622  
623 for my $bn ($atom->bonds_neighbors) {
624 print "bond $bn->{bond} point to atom $bn->{to}\n";
625 }
626  
627 =cut
628  
629 0 ▶sub bonds_neighbors {
Subroutine bonds_neighbors: not called CC=4 SCAR=30.0
Pod documented
630 0 my $self = shift;
631 0 my $from = shift;
632 0 my @ret = ();
633  
634 0 for my $b (@{$self->{bonds}}) {
635 0 ▶ push @ret, {%$b} unless $from && $b->{to} eq $from;
Branch: 1 branch
BranchTrueFalseTotal
unless $from and $b->{"to"} eq $from 0 0 0
Condition0%0/3
$from and $b->{"to"} eq $from
!ll&&!rl&&r
000
MC/DC0%0/2
$from and $b->{"to"} eq $from
$from $b->{"to"} eq $from
Truth table0%0/3
A and B
A= $fromB= $b->{"to"} eq $from
ABresult
0X0
100
111
636 }
637 0 @ret;
638 }
639  
640 =item ($distance, $closest_atom) = $atom->distance($obj)
641  
642 Returns the minimum distance to $obj, which can be an atom, a molecule, or a
643 vector. In scalar context it returns only the distance; in list context it
644 also returns the closest atom found. It can also be called as a function,
645 Chemistry::Atom::distance (which can be exported).
646  
647 =cut
648  
649 5 ▶sub distance {
Subroutine distance: called CC=8 SCAR=25.8
Pod documented
650 5 my $self = shift;
651 5 my $obj = shift;
652 5 my $min_length;
653 5 my $closest_atom = $obj;
654  
655 5 ▶ if ($obj->isa('Chemistry::Atom')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($obj->isa('Chemistry::Atom')) { } 3 2 5
elsif ($obj->isa('Math::VectorReal')) { } 1 1 2
elsif ($obj->isa('Chemistry::Mol')) { } 0 1 1
656 3 my $v = $self->coords - $obj->coords;
657 3 $min_length = $v->length;
658 } elsif ($obj->isa('Math::VectorReal')) {
659 1 my $v = $self->coords - $obj;
660 1 $min_length = $v->length;
661 } elsif ($obj->isa('Chemistry::Mol')) {
662 0 my @atoms = $obj->atoms;
663 0 ▶ my $a = shift @atoms or return undef; # ensure there's at least 1 atom
Branch: 1 branch
BranchTrueFalseTotal
my $a = shift @atoms or return undef 0 0 0
664 0 $min_length = $self->distance($a);
665 0 $closest_atom = $a;
666 0 for $a (@atoms) {
667 0 my $l = $self->distance($a);
668 0 ▶ $min_length = $l, $closest_atom = $a if $l < $min_length;
Branch: 1 branch
BranchTrueFalseTotal
if $l < $min_length 0 0 0
669 }
670 } else {
671 1 croak "atom->distance() undefined for objects of type '", ref $obj,"'";
672 }
673 4 ▶ wantarray ? ($min_length, $closest_atom) : $min_length;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
674 }
675  
676 =item $atom->angle($atom2, $atom3)
677  
678 Returns the angle in radians between the atoms involved. $atom2 is the atom in
679 the middle. Can also be called as Chemistry::Atom::angle($atom1, $atom2,
680 $atom3). This function can be exported. Note: if you override this method,
681 you may also need to override angle_deg or strange things may happen.
682  
683 =cut
684  
685 # $a2 is the one in the center
686 11 ▶sub angle {
Subroutine angle: called CC=7 SCAR=19.5
Pod documented
687 11 ▶ @_ == 3 or croak "Chemistry::Atom::angle requires three atoms!\n";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("Chemistry::Atom::angle requires three atoms!\n") 10 1 11
688 10 my @c;
689 10 for my $a (@_) { # extract coordinates
690 28 ▶ ref $a or croak "Chemistry::Atom::angle: $a is not an object";
Branch: 1 branch
BranchTrueFalseTotal
ref $a or croak("Chemistry::Atom::angle: $a is not an object") 27 1 28
691 27 ▶ push @c, $a->isa("Chemistry::Atom") ? $a->coords :
Branch: 2 branches
BranchTrueFalseTotal
$a->isa('Chemistry::Atom') ? : 5 22 27
$a->isa('Math::VectorReal') ? : 21 1 22
692 $a->isa("Math::VectorReal") ? $a :
693 croak "angle: $a is neither an atom nor a vector!\n";
694 }
695 8 my $v1 = $c[0] - $c[1];
696 8 my $v2 = $c[2] - $c[1];
697 8 ▶ my $l = ($v1->length * $v2->length) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $l = $v1->length * $v2->length or return 0 4 4 8
698 4 acos(($v1 . $v2) / $l);
699 }
700  
701 =item $atom->angle_deg($atom2, $atom3)
702  
703 Same as angle(), but returns the value in degrees. May be exported.
704  
705 =cut
706  
707 2 ▶sub angle_deg {
Subroutine angle_deg: called CC=1 SCAR=0.0
Pod documented
708 2 rad2deg(angle(@_));
709 }
710  
711 =item $atom->dihedral($atom2, $atom3, $atom4)
712  
713 Returns the dihedral angle in radians between the atoms involved. Can also be
714 called as Chemistry::Atom::dihedral($atom1, $atom2, $atom3, $atom4). May be
715 exported. Note: if you override this method, you may also need to override
716 dihedral_deg and angle or strange things may happen.
717  
718 =cut
719  
720 4 ▶sub dihedral {
Subroutine dihedral: called CC=6 SCAR=18.0
Pod documented
721 4 ▶ @_ == 4 or croak "Chemistry::Atom::dihedral requires four atoms!\n";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or croak("Chemistry::Atom::dihedral requires four atoms!\n") 3 1 4
722 3 my @c;
723 3 for my $a (@_) { # extract coordinates
724 12 ▶ push @c, $a->isa("Chemistry::Atom") ? $a->coords :
Branch: 2 branches
BranchTrueFalseTotal
$a->isa('Chemistry::Atom') ? : 4 8 12
$a->isa('Math::VectorReal') ? : 8 0 8
725 $a->isa("Math::VectorReal") ? $a :
726 croak "angle: $a is neither an atom nor a vector!\n";
727 }
728 3 my $v1 = $c[0] - $c[1];
729 3 my $v2 = $c[2] - $c[1];
730 3 my $v3 = $c[3] - $c[2];
731 3 my $x1 = $v1 x $v2;
732 3 my $x2 = $v3 x $v2;
733 3 my $abs_dih = angle($x1, O(), $x2);
734 3 ▶ $v1 . $x2 > 0 ? $abs_dih : -$abs_dih;
Branch: 1 branch
BranchTrueFalseTotal
$v1 . $x2 > 0 ? : 0 3 3
735 }
736  
737 =item $atom->dihedral_deg($atom2, $atom3, $atom4)
738  
739 Same as dihedral(), but returns the value in degrees. May be exported.
740  
741 =cut
742  
743 1 ▶sub dihedral_deg {
Subroutine dihedral_deg: called CC=1 SCAR=0.0
Pod documented
744 1 rad2deg(dihedral(@_));
745 }
746  
747 =item $atom->print
748  
749 Convert the atom to a string representation (used for debugging).
750  
751 =cut
752  
753 0 ▶sub print {
Subroutine print: not called CC=2 SCAR=14.8
Pod documented
754 0 my $self = shift;
755 0 my ($indent) = @_;
756  
757 16 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
758 0 ▶ $indent ||= 0;
Condition0%0/2
$indent ||= 0
l!l
00
MC/DC0%0/1
$indent ||= 0
$indent
Truth table0%0/2
A
A= $indent
Aresult
11
00
759 0 my $bonds = join " ", map {$_->id} $self->bonds;
760 0 my $neighbors = join " ", map {$_->id} $self->neighbors;
761 my $coords = $self->{coords}->stringify(
762 0 'x3: %g
763 y3: %g
764 z3: %g'
765 );
766  
767 0 my $ret = <<EOF;
768 $self->{id}:
769 symbol: $self->{symbol}
770 name : $self->{name}
771 $coords
772 formal_charge: $self->{formal_charge}
773 bonds: "$bonds"
774 neighbors: "$neighbors"
775 EOF
776 0 $ret .= " attr:\n";
777 0 $ret .= $self->print_attr($indent+2);
778 0 $ret =~ s/^/" "x$indent/gem;
779 0 $ret;
780 }
781  
782 =item my $info = $atom->sprintf($format)
783  
784 Format interesting atomic information in a concise way, as specified by
785 a printf-like format.
786  
787 %s - symbol
788 %Z - atomic number
789 %n - name
790 %q - formal charge
791 %h - implicit hydrogen count
792 %v - valence
793 %i - id
794 %8.3m - mass, formatted as %8.3f with core sprintf
795 %8.3x - x coordinate, formatted as %8.3f with core sprintf
796 %8.3y - y coordinate, formatted as %8.3f with core sprintf
797 %8.3z - z coordinate, formatted as %8.3f with core sprintf
798 %% - %
799  
800 =cut
801  
802 3 ▶sub sprintf {
Subroutine sprintf: called CC=7 SCAR=21.8
Pod documented
803 3 my ($atom, $format) = @_;
804 16 ▶ no warnings 'uninitialized'; # don't care if some properties are undefined
Subroutine BEGIN: called CC=1 SCAR=0.0
805 3 ▶ $format ||= "%f";
Condition50%1/2
$format ||= "%f"
l!l
30
MC/DC0%0/1
$format ||= "%f"
$format
Truth table50%1/2
A
A= $format
Aresult
11
00
806 3 $format =~ s/%%/\\%/g; # escape %% with a \
807 0 ▶ $format =~ s/(?<!\\)%q/$atom->formal_charge || 0/eg; # %q
Condition0%0/2
$atom->formal_charge || 0
l!l
00
MC/DC0%0/1
$atom->formal_charge || 0
$atom->formal_charge
Truth table0%0/2
A
A= $atom->formal_charge
Aresult
11
00
808 3 $format =~ s/(?<!\\)%s/$atom->symbol/eg; # %s
809 3 $format =~ s/(?<!\\)%Z/$atom->Z/eg; # %Z
810 0 $format =~ s/(?<!\\)%n/$atom->name/eg; # %n
811 0 $format =~ s/(?<!\\)%h/$atom->hydrogens/eg; # %h
812 0 $format =~ s/(?<!\\)%v/$atom->valence/eg; # %v
813 3 $format =~ s/(?<!\\)%(\d*\.?\d*)m/
814 0 ▶ $1 ? sprintf "%$1f", $atom->mass : $atom->mass/eg; # %m
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 0 0
815 3 $format =~ s/(?<!\\)%(\d*\.?\d*)x/
816 1 ▶ $1 ? sprintf "%$1f", $atom->x3 : $atom->x3/eg; # %x
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 1 1
817 3 $format =~ s/(?<!\\)%(\d*\.?\d*)y/
818 1 ▶ $1 ? sprintf "%$1f", $atom->y3 : $atom->y3/eg; # %y
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 1 1
819 3 $format =~ s/(?<!\\)%(\d*\.?\d*)z/
820 1 ▶ $1 ? sprintf "%$1f", $atom->z3 : $atom->z3/eg; # %z
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 1 1
821 0 $format =~ s/(?<!\\)%i/$atom->id/eg; # %i
822 3 $format =~ s/\\(.)/$1/g; # other \ escapes
823 3 $format;
824 }
825  
826 =item $atom->printf($format)
827  
828 Same as $atom->sprintf, but prints to standard output automatically. Used
829 for quick and dirty atomic information dumping.
830  
831 =cut
832  
833 0 ▶sub printf {
Subroutine printf: not called CC=1 SCAR=6.9
Pod documented
834 0 my ($atom, $format) = @_;
835 0 print $atom->sprintf($format);
836 }
837  
838 16 1;
839  
840 =back
841  
842 =head1 SOURCE CODE REPOSITORY
843  
844 L<https://github.com/perlmol/Chemistry-Mol>
845  
846 =head1 SEE ALSO
847  
848 L<Chemistry::Mol>, L<Chemistry::Bond>,
849 L<Math::VectorReal>, L<Chemistry::Tutorial>,
850 L<Chemistry::InternalCoords>
851  
852 =head1 AUTHOR
853  
854 Ivan Tubert-Brohman E<lt>itub@cpan.orgE<gt>
855  
856 =head1 COPYRIGHT
857  
858 Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is
859 free software; you can redistribute it and/or modify it under the same terms as
860 Perl itself.
861  
862 =cut
863  
« Mol.pm ↑ Summary File/Formula.pm »