blib/lib/Chemistry/3DBuilder.pm

filter: Statementstmt 96.8% 123 / 127 Branchbran 82.6% 38 / 46 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 7 / 7 Subroutinesub 95.4% 21 / 22 Podpod 9.0% 1 / 11 totaltotal 89.7% 202 / 225 CC 33 SCAR 15.5
CC
33
Cov
94%
choose_dihedral
9.0
atom_hybridization
7.0
__ANON__
6.2
CRAP 4.7
« blib/lib/Chemistry/Isotope.pm ↑ Summary blib/lib/Chemistry/Domain.pm »
1 package Chemistry::3DBuilder;
2 # $Id: 3DBuilder.pm,v 1.2 2005/05/06 21:34:46 itubert Exp $
3  
4 10 $VERSION = '0.10';
5  
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Chemistry::Ring;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Chemistry::InternalCoords;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 our @EXPORT_OK = qw(build_3d);
15 10 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
16  
17 10 our $DEBUG = 0;
18  
19 =head1 NAME
20  
21 Chemistry::3DBuilder - Generate 3D coordinates from a connection table
22  
23 =head1 SYNOPSIS
24  
25 # example: convert SMILES to MDL molfile
26 use Chemistry::3DBuilder qw(build_3d);
27 use Chemistry::File::SMILES;
28 use Chemistry::File::MDLMol;
29  
30 my $s = '[O-]C(=O)C(N)C(C)CC';
31 my $mol = Chemistry::Mol->parse($s, format => 'smiles');
32  
33 build_3d($mol);
34  
35 print $mol->print(format => 'mdl');
36  
37 =head1 DESCRIPTION
38  
39 This module generates a three-dimensional molecular structure from a connection
40 table, such as that obtained by a 2D representation of the molecule or from a
41 SMILES string.
42  
43 B<NOTE>: this module is still at a very early stage of development so it has
44 important limitations. 1) It doesn't handle rings or stereochemistry yet! 2)
45 The bond lengths and atoms are very approximate as they don't really account
46 for different elements. 3) Only the sp3, sp2, and sp hybridizations are
47 supported.
48  
49 =head1 SUBROUTINES
50  
51 These subroutines may be exported; to export all, use the ':all' tag.
52  
53 =over
54  
55 =item build_3d($mol)
56  
57 Add internal and cartesian coordinates to the molecule C<$mol>.
58  
59 =cut
60  
61 # POSSIBLE PROBLEMS:
62 # allenes
63 # stereochemistry
64 # (E)-esters and acids
65  
66 14 ▶sub build_3d {
Subroutine build_3d: called CC=6 SCAR=18.1
Pod documented
67 14 my ($mol) = @_;
68  
69 # prepare molecule
70 14 $mol->collapse_hydrogens;
71 14 $_->{internal_coords} = undef for $mol->atoms;
72 14 Chemistry::Ring::aromatize_mol($mol);
73  
74 # check for rings
75 14 my $rings = $mol->attr('ring/rings');
76 14 ▶ if (@$rings) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rings) 0 14 14
77 0 warn "mol has " . @$rings . " rings\n";
78 0 return;
79 }
80  
81 14 my $user = { n => 0 };
82 14 build_chain($mol, $user);
83  
84 #print "sprout_hydrogens\n" if $DEBUG;
85 # add the hydrogens
86 14 $mol->sprout_hydrogens;
87 14 for my $h (
88 127 ▶ grep { $_->symbol eq 'H' and ! $_->internal_coords } $mol->atoms
Condition100%3/3
$_->symbol eq 'H' && !$_->internal_coords
!ll&&!rl&&r
38287
MC/DC100%2/2
$_->symbol eq 'H' && !$_->internal_coords
$_->symbol eq 'H' !$_->internal_coords
Truth table100%3/3
A && not(B)
A= $_->symbol eq 'H'B= !$_->internal_coords
ABresult
0X0
100
111
89 ) {
90 87 my ($bond) = $h->bonds;
91 87 my ($nei) = $h->neighbors;
92 87 add_atom(atom => $h, bond => $bond, from => $nei, user => $user);
93 }
94 #warn "IC($_): " . $_->internal_coords for $mol->atoms;
95  
96 # calculate the cartesian coordinates
97 14 $_->internal_coords->add_cartesians for $mol->atoms;
98 }
99  
100 14 ▶sub build_chain {
Subroutine build_chain: called CC=1 SCAR=0.0
Pod undocumented
101 14 my ($mol, $user) = @_;
102 14 dfs($mol, on_atom => \&add_atom, user => $user);
103 }
104  
105 113 ▶sub bond_length {
Subroutine bond_length: called CC=2 SCAR=6.9
Pod undocumented
106 113 my ($bond) = @_;
107 113 my (@atoms) = $bond->atoms;
108 113 ▶ if (grep { $_->symbol eq 'H' } @atoms) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->symbol eq 'H';} @atoms) { } 88 25 113
109 88 return 1.1;
110 } else {
111 25 return 1.5 - ($bond->order - 1) * 0.15;
112 }
113 }
114  
115 113 ▶sub choose_bond {
Subroutine choose_bond: called CC=1 SCAR=0.0
Pod undocumented
116 113 my ($atom, $bond, $from) = @_;
117 113 ($from, bond_length($bond));
118 }
119  
120 10 my %angle_table = (
121 sp => 180,
122 sp2 => 120,
123 sp3 => 109.47,
124 );
125  
126 99 ▶sub bond_angle {
Subroutine bond_angle: called CC=2 SCAR=7.1
Pod undocumented
127 99 my ($atom1, $atom2, $atom3) = @_;
128 99 my $hyb = atom_hybridization($atom2);
129 #warn "hyb angle($atom)=$hyb\n";
130 99 ▶ $angle_table{$hyb} || 109.47;
Branch: 1 branch
BranchTrueFalseTotal
$angle_table{$hyb} or "109.47" 99 0 99
131 }
132  
133 185 ▶sub atom_hybridization {
Subroutine atom_hybridization: called CC=7 SCAR=19.5
Pod undocumented
134 10 ▶ no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
135 185 my ($atom) = @_;
136 185 my %ord_hist;
137 185 $ord_hist{$_}++ for map { $_->order } $atom->bonds;
138 185 my $deg = $atom->bonds + $atom->implicit_hydrogens;
139 185 ▶ if ($deg == 2 and ($ord_hist{3} or $ord_hist{2} == 2)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($deg == 2 and $ord_hist{'3'} || $ord_hist{'2'} == 2) { } 4 181 185
elsif ($deg == 3 and $ord_hist{'2'} == 1) { } 22 159 181
Condition100%6/6
$deg == 2 and $ord_hist{3} || $ord_hist{2} == 2
!ll&&!rl&&r
18014
$ord_hist{3} or $ord_hist{2} == 2
l!l&&r!l&&!r
311
MC/DC100%3/3
$deg == 2 and $ord_hist{3} || $ord_hist{2} == 2
$deg == 2 $ord_hist{3} $ord_hist{2} == 2
Truth table100%4/4
A and B or C
A= $deg == 2B= $ord_hist{3}C= $ord_hist{2} == 2
ABCresult
0XX0
1000
11X1
1011
140 4 return "sp";
141 ▶ } elsif ($deg == 3 and $ord_hist{2} == 1) {
Condition100%3/3
$deg == 3 and $ord_hist{2} == 1
!ll&&!rl&&r
156322
MC/DC100%2/2
$deg == 3 and $ord_hist{2} == 1
$deg == 3 $ord_hist{2} == 1
Truth table100%3/3
A and B
A= $deg == 3B= $ord_hist{2} == 1
ABresult
0X0
100
111
142 22 return "sp2";
143 } else {
144 159 return "sp3";
145 }
146 }
147  
148 0 ▶sub bond_dihedral {
Subroutine bond_dihedral: not called CC=1 SCAR=6.9
Pod undocumented
149 0 my ($atom, $from, $ang_ref) = @_;
150 0 180;
151 }
152  
153 99 ▶sub choose_angle {
Subroutine choose_angle: called CC=1 SCAR=0.0
Pod undocumented
154 99 my ($atom, $bond, $from) = @_;
155 99 my ($len_ref, $len_val) = choose_bond($atom, $bond, $from);
156 99 ▶ my ($ang_ref) = first { $_->internal_coords } $from->neighbors($atom);
Subroutine __ANON__: called CC=1 SCAR=0.0
157 99 ($len_ref, $len_val, $ang_ref, bond_angle($atom, $from, $ang_ref));
158 }
159  
160 86 ▶sub choose_dihedral {
Subroutine choose_dihedral: called CC=9 SCAR=22.0
Pod undocumented
161 86 my ($atom, $bond, $from) = @_;
162 86 my ($len_ref, $len_val, $ang_ref, $ang_val) =
163 choose_angle($atom, $bond, $from);
164 86 my ($dih_ref, $dih_val);
165 86 my $hyb = atom_hybridization($len_ref);
166 86 my $fd;
167
168 #warn "hyb($len_ref)=$hyb\n";
169 86 ▶ if ($fd = $len_ref->attr("3dbuilder/first_dihedral")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fd = $len_ref->attr('3dbuilder/first_dihedral')) { } 47 39 86
elsif ($fd = $len_ref->attr('3dbuilder/first_dihedral_ang')) { } 9 30 39
170 47 $fd = $atom->parent->by_id($fd); # XXX
171 47 $dih_ref = $fd;
172 47 ($ang_ref, $ang_val) = $fd->internal_coords->angle;
173 47 ▶ $dih_val = $hyb eq 'sp2' ? 180 :
Branch: 2 branches
BranchTrueFalseTotal
$hyb eq 'sp2' ? : 4 43 47
$len_ref->attr('3dbuilder/second_dihedral') ? : 20 23 43
174 $len_ref->attr("3dbuilder/second_dihedral") ?
175 240 : 120;
176 47 $len_ref->attr("3dbuilder/second_dihedral", $atom->id);
177 } elsif ($fd = $len_ref->attr("3dbuilder/first_dihedral_ang")) {
178 9 $fd = $atom->parent->by_id($fd); # XXX
179 9 ($dih_ref) = $fd->internal_coords->dihedral;
180 9 ($ang_ref) = $fd->internal_coords->distance;
181 9 ▶ $dih_val = $hyb eq 'sp2' ? 180 :
Branch: 2 branches
BranchTrueFalseTotal
$hyb eq 'sp2' ? : 1 8 9
$len_ref->attr('3dbuilder/second_dihedral_ang') ? : 4 4 8
182 $len_ref->attr("3dbuilder/second_dihedral_ang") ?
183 240 : 120;
184 9 $len_ref->attr("3dbuilder/second_dihedral_ang", $atom->id);
185 } else {
186 # for fd(first_dihedralang): ang_ref = ok; dih_ref = dih_ref(fd)
187 30 ▶ $dih_ref = first { $_->internal_coords } $ang_ref->neighbors($len_ref);
Subroutine __ANON__: called CC=1 SCAR=0.0
188 30 ▶ if ($dih_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dih_ref) { } 25 5 30
189 25 $dih_val = 180;
190 25 $ang_ref->attr("3dbuilder/first_dihedral_ang", $atom->id);
191 } else {
192 10 ▶ $dih_ref = first { $_->internal_coords and $_ ne $ang_ref }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_->internal_coords and $_ ne $ang_ref 10 0 10
193 5 $len_ref->neighbors($atom);
194 5 ▶ $dih_val = $hyb eq 'sp2' ? 180 : 120;
Branch: 1 branch
BranchTrueFalseTotal
$hyb eq 'sp2' ? : 1 4 5
195 }
196 30 $len_ref->attr("3dbuilder/first_dihedral", $atom->id);
197 }
198
199 86 ($len_ref, $len_val, $ang_ref, $ang_val, $dih_ref, $dih_val);
200 }
201  
202 127 ▶sub add_atom {
Subroutine add_atom: called CC=4 SCAR=13.9
Pod undocumented
203 127 my (%opts) = @_;
204 127 my ($atom, $bond, $from, $user) = @opts{qw(atom bond from user)};
205 127 my $stack = $user->{stack};
206 127 my $n = ++$user->{n};
207  
208 #print "adding atom $n: $atom!\n" if $DEBUG;
209 127 my $ic;
210 127 ▶ if ($n == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($n == 1) { } 14 113 127
elsif ($n == 2) { } 14 99 113
elsif ($n == 3) { } 13 86 99
211 # first atom; place at origin
212 14 $ic = Chemistry::InternalCoords->new($atom);
213 } elsif ($n == 2) {
214 # second atom only needs a bond
215 14 $ic = Chemistry::InternalCoords->new(
216 $atom, choose_bond($atom, $bond, $from));
217 } elsif ($n == 3) {
218 # third atom, needs an angle
219 13 $ic = Chemistry::InternalCoords->new(
220 $atom, choose_angle($atom, $bond, $from));
221 } else {
222 # other atoms need dihedral
223 86 $ic = Chemistry::InternalCoords->new(
224 $atom, choose_dihedral($atom, $bond, $from));
225 }
226 127 $atom->internal_coords($ic);
227 }
228  
229 # do a depth-first traversal of a molecule. This should be added to
230 # Chemistry::Mol
231  
232 14 ▶sub dfs {
Subroutine dfs: called CC=3 SCAR=11.1
Pod undocumented
233 14 my ($mol, %opts) = @_;
234 14 my ($on_atom, $on_bond, $user) = @opts{qw(on_atom on_bond user)};
235 14 my $dfs;
236 my %visited;
237 40 ▶ $dfs = sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
238 40 my ($atom) = @_;
239 40 $visited{$atom} = 1;
240 40 for my $bn ($atom->bonds_neighbors) {
241 52 my $nei = $bn->{to};
242 52 my $bond = $bn->{bond};
243  
244 52 ▶ next if $visited{$bond}++;
Branch: 1 branch
BranchTrueFalseTotal
if $visited{$bond}++ 26 26 52
245 26 ▶ $on_bond->(mol => $mol, from => $atom, to => $nei, bond => $bond,
Branch: 1 branch
BranchTrueFalseTotal
if $on_bond 0 26 26
246 user => $user)
247 if $on_bond;
248  
249 26 ▶ next if $visited{$nei};
Branch: 1 branch
BranchTrueFalseTotal
if $visited{$nei} 0 26 26
250 26 ▶ $on_atom->(mol => $mol, from => $atom, atom => $nei,
Branch: 1 branch
BranchTrueFalseTotal
if $on_atom 26 0 26
251 bond => $bond, user => $user) if $on_atom;
252 26 $dfs->($nei);
253 }
254 14 };
255 14 ▶ return unless $mol->atoms;
Branch: 1 branch
BranchTrueFalseTotal
unless $mol->atoms 0 14 14
256 14 ▶ $on_atom->(mol => $mol, atom => $mol->atoms(1), from => undef,
Branch: 1 branch
BranchTrueFalseTotal
if $on_atom 14 0 14
257 bond => undef, user => $user) if $on_atom;
258 14 $dfs->($mol->atoms(1));
259 }
260  
261 10 1;
262  
263 =back
264  
265 =head1 VERSION
266  
267 0.10
268  
269 =head1 SEE ALSO
270  
271 L<Chemistry::Mol>, L<Chemistry::InternalCoords>.
272  
273 The PerlMol website L<http://www.perlmol.org/>
274  
275 =head1 AUTHOR
276  
277 Ivan Tubert-Brohman E<lt>itub@cpan.orgE<gt>
278  
279 =head1 COPYRIGHT
280  
281 Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is
282 free software; you can redistribute it and/or modify it under the same terms as
283 Perl itself.
284  
285 =cut
286  
« blib/lib/Chemistry/Isotope.pm ↑ Summary blib/lib/Chemistry/Domain.pm »