blib/lib/Chemistry/Ring.pm

filter: Statementstmt 44.1% 38 / 86 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 56.2% 9 / 16 Podpod 87.5% 7 / 8 totaltotal 40.0% 54 / 135 CC 21 SCAR 32.6
CC
21
Cov
36%
is_aromatic
66.7
plane
30.0
aromatize_mol
20.4
CRAP 26.1
« blib/lib/Chemistry/File/SMARTS.pm ↑ Summary blib/lib/Chemistry/InternalCoords/Builder.pm »
1 package Chemistry::Ring;
2 10 $VERSION = '0.19';
3 #$Id: Ring.pm,v 1.1.1.1 2005/03/29 23:57:36 itubert Exp $
4  
5 =head1 NAME
6  
7 Chemistry::Ring - Represent a ring as a substructure of a molecule
8  
9 =head1 SYNOPSIS
10  
11 use Chemistry::Ring;
12
13 # already have a molecule in $mol...
14 # create a ring with the first six atoms in $mol
15 my $ring = Chemistry::Ring->new;
16 $ring->add_atom($_) for $mol->atoms(1 .. 6);
17  
18 # find the centroid
19 my $vector = $ring->centroid;
20  
21 # find the plane that fits the ring
22 my ($normal, $distance) = $ring->plane;
23  
24 # is the ring aromatic?
25 print "is aromatic!\n" if $ring->is_aromatic;
26  
27 # "aromatize" a molecule
28 Chemistry::Ring::aromatize_mol($mol);
29  
30 # get the rings involving an atom (after aromatizing)
31 my $rings = $mol->atoms(3)->attr('ring/rings');
32  
33 =head1 DESCRIPTION
34  
35 This module provides some basic methods for representing a ring. A ring is
36 a subclass of molecule, because it has atoms and bonds. Besides that, it
37 has some useful geometric methods for finding the centroid and the ring plane,
38 and methods for aromaticity detection.
39  
40 This module does not detect the rings by itself; for that, look at
41 L<Chemistry::Ring::Find>.
42  
43 This module is part of the PerlMol project, L<http://www.perlmol.org/>.
44  
45 =cut
46  
47 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 10 ▶use Math::VectorReal qw(:axis vector);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 10 ▶use Statistics::Regression;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 10 ▶use Chemistry::Mol;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 10 ▶use base 'Chemistry::Mol', 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 10 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 10 our @EXPORT_OK = qw(aromatize_mol);
56 10 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
57  
58 10 our $N = 0;
59 10 our $DEBUG = 0;
60  
61 =head1 METHODS
62  
63 =over 4
64  
65 =item Chemistry::Ring->new(name => value, ...)
66  
67 Create a new Ring object with the specified attributes. Same as
68 C<< Chemistry::Mol->new >>.
69  
70 =cut
71  
72 0 ▶sub nextID {
Subroutine nextID: not called CC=1 SCAR=6.9
Pod undocumented
73 0 "ring".++$N;
74 }
75  
76  
77 # make sure we don't become parent of the atoms added to us
78 0 ▶sub add_atom { shift->SUPER::add_atom_np(@_) }
Subroutine add_atom: not called CC=1 SCAR=6.9
Pod documented
79 0 ▶sub add_bond { shift->SUPER::add_bond_np(@_) }
Subroutine add_bond: not called CC=1 SCAR=6.9
Pod documented
80  
81 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
82 0 my $self = shift;
83 0 return <<EOF;
84 ring:
85 id: $self->{id}
86 0 atoms: @{$self->{atoms}}
87 0 bonds: @{$self->{bonds}}
88 EOF
89 }
90  
91 =item $ring->centroid
92  
93 Returs a vector with the centroid, defined as the average of the coordinates
94 of all the atoms in the ring. The vecotr is a L<Math::VectorReal> object.
95  
96 =cut
97  
98 0 ▶sub centroid {
Subroutine centroid: not called CC=2 SCAR=17.9
Pod documented
99 0 my $self = shift;
100 0 my $c = O; # origin
101 0 my $n = 0;
102 0 for my $a ($self->atoms) {
103 0 $c += $a->coords;
104 0 ++$n;
105 }
106 0 $c = $c / $n;
107 }
108  
109 =item my ($norm, $d) = $ring->plane
110  
111 Returns the normal and distance to the origin that define the plane that best
112 fits the atoms in the ring, by using multivariate regression. The normal
113 vector is a L<Math::VectorReal> object.
114  
115 =cut
116  
117 0 ▶sub plane {
Subroutine plane: not called CC=5 SCAR=34.0
Pod documented
118 0 my $self = shift;
119 0 my $reg = Statistics::Regression->new(3, "plane for $self", [qw(b mx my)]);
120 0 for my $atom ($self->atoms) {
121 0 my ($x, $y, $z) = $atom->coords->array;
122 0 $reg->include($z, [1.0, $x, $y]);
123 }
124 0 ▶ $reg->print if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Chemistry::Ring::DEBUG 0 0 0
125  
126 # convert the theta vector (z = a + bx + cy) to a normal vector and
127 # distance to the origin
128 0 my @coef = (@{$reg->theta}, -1.0); # -1 is d in a + bx + cx + dz = 0
129 0 my $d = shift @coef; # distance (not normalized)
130 0 my $sum_sq = 0; # normalization constant
131 0 $sum_sq += $_*$_ for @coef;
132 0 ▶ $sum_sq ||= 1;
Condition0%0/2
$sum_sq ||= 1
l!l
00
MC/DC0%0/1
$sum_sq ||= 1
$sum_sq
Truth table0%0/2
A
A= $sum_sq
Aresult
11
00
133 0 ($d, @coef) = map { $_ / $sum_sq } ($d, @coef); # normalize
134 0 return (vector(@coef)->norm, $d);
135 }
136  
137 =item $ring->is_aromatic
138  
139 Naively guess whether ring is aromatic from the molecular graph, with a method
140 based on Hückel's rule. This method is not very accurate, but works for simple
141 molecules. Returns true or false.
142  
143 =cut
144  
145 0 ▶sub is_aromatic {
Subroutine is_aromatic: not called CC=9 SCAR=42.0
Pod documented
146 0 my ($self) = @_;
147 0 my $n_pi = 0;
148  
149 0 for my $atom ($self->atoms) {
150 10 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
151 0 ▶ return 0 if ($atom->bonds + $atom->hydrogens > 3);
Branch: 1 branch
BranchTrueFalseTotal
if $atom->bonds + $atom->hydrogens > 3 0 0 0
152  
153 # build bond order histogram
154 0 my @order_freq = (0,0,0,0);
155 0 for my $bond ($atom->bonds) {
156 0 $order_freq[$bond->order]++;
157 }
158
159 0 ▶ return 0 if ($order_freq[3] or $order_freq[2] > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $order_freq[3] or $order_freq[2] > 1 0 0 0
Condition0%0/3
$order_freq[3] or $order_freq[2] > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$order_freq[3] or $order_freq[2] > 1
$order_freq[3] $order_freq[2] > 1
Truth table0%0/3
A or B
A= $order_freq[3]B= $order_freq[2] > 1
ABresult
1X1
011
000
160 0 ▶ if ($order_freq[2] == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($order_freq[2] == 1) { } 0 0 0
elsif ($atom->symbol =~ /^[NOS]$/) { } 0 0 0
161 0 $n_pi += 1;
162 } elsif ($atom->symbol =~ /^[NOS]$/) {
163 0 $n_pi += 2;
164 }
165 }
166 #print "n_pi = $n_pi\n";
167 0 ▶ return ($n_pi % 4 == 2) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$n_pi % 4 == 2 ? : 0 0 0
168 }
169  
170 10 1;
171  
172 =back
173  
174 =head1 EXPORTABLE SUBROUTINES
175  
176 Nothing is exported by default, but you can export these subroutines
177 explicitly, or all of them by using the ':all' tag.
178  
179 =over
180  
181 =item aromatize_mol($mol)
182  
183 Finds all the aromatic rings in the molecule and marks all the atoms and bonds
184 in those rings as aromatic.
185  
186 It also adds the 'ring/rings' attribute to the molecule and to all ring atoms
187 and bonds; this attribute is an array reference containing the list of rings
188 that involve that atom or bond (or all the rings in the case of the molecule).
189 NOTE (the ring/rings attribute is experimental and might change in future
190 versions).
191  
192 =cut
193  
194 14 ▶sub aromatize_mol {
Subroutine aromatize_mol: called CC=8 SCAR=30.2
Pod documented
195 14 my ($mol) = @_;
196  
197 14 require Chemistry::Ring::Find;
198  
199 14 $_->aromatic(0) for ($mol->atoms, $mol->bonds);
200  
201 14 my @rings = Chemistry::Ring::Find::find_rings($mol);
202 14 $mol->attr("ring/rings", \@rings);
203 14 $_->attr("ring/rings", []) for ($mol->atoms, $mol->bonds);
204  
205 14 for my $ring (@rings) {
206 0 ▶ if ($ring->is_aromatic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ring->is_aromatic) 0 0 0
207 0 $_->aromatic(1) for ($ring->atoms, $ring->bonds);
208 }
209 0 for ($ring->atoms, $ring->bonds) {
210 0 ▶ my $ringlist = $_->attr("ring/rings") || [];
Condition0%0/2
$_->attr('ring/rings') || []
l!l
00
MC/DC0%0/1
$_->attr('ring/rings') || []
$_->attr('ring/rings')
Truth table0%0/2
A
A= $_->attr('ring/rings')
Aresult
11
00
211 0 push @$ringlist, $ring;
212 0 weaken($ringlist->[-1]);
213 0 $_->attr("ring/rings", $ringlist);
214 }
215 }
216 14 @rings;
217 }
218  
219 =back
220  
221 =head1 VERSION
222  
223 0.19
224  
225 =head1 SEE ALSO
226  
227 L<Chemistry::Mol>, L<Chemistry::Atom>, L<Chemistry::Ring::Find>,
228 L<Math::VectorReal>.
229  
230 =head1 AUTHOR
231  
232 Ivan Tubert-Brohman <itub@cpan.org>
233  
234 =head1 COPYRIGHT
235  
236 Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is
237 free software; you can redistribute it and/or modify it under the same terms as
238 Perl itself.
239  
240 =cut
241  
« blib/lib/Chemistry/File/SMARTS.pm ↑ Summary blib/lib/Chemistry/InternalCoords/Builder.pm »