line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Chemistry::Bond; |
2
|
|
|
|
|
|
|
$VERSION = '0.37'; |
3
|
|
|
|
|
|
|
# $Id: Bond.pm,v 1.36 2009/05/10 19:37:58 itubert Exp $ |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Chemistry::Bond - Chemical bonds as objects in molecules |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Chemistry::Bond; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# assuming we have molecule $mol with atoms $a1 and $a2 |
14
|
|
|
|
|
|
|
$bond = Chemistry::Bond->new( |
15
|
|
|
|
|
|
|
id => "b1", |
16
|
|
|
|
|
|
|
type => '=', |
17
|
|
|
|
|
|
|
atoms => [$a1, $a2] |
18
|
|
|
|
|
|
|
order => '2', |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
$mol->add_bond($bond); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# simpler way of doing the same: |
23
|
|
|
|
|
|
|
$mol->new_bond( |
24
|
|
|
|
|
|
|
id => "b1", |
25
|
|
|
|
|
|
|
type => '=', |
26
|
|
|
|
|
|
|
atoms => [$a1, $a2] |
27
|
|
|
|
|
|
|
order => '2', |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 DESCRIPTION |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
This module includes objects to describe chemical bonds. |
33
|
|
|
|
|
|
|
A bond is defined as a list of atoms (typically two), with some |
34
|
|
|
|
|
|
|
associated properties. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 Bond Attributes |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
In addition to common attributes such as id, name, and type, |
39
|
|
|
|
|
|
|
bonds have the order attribute. The bond order is a number, typically the |
40
|
|
|
|
|
|
|
integer 1, 2, 3, or 4. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
43
|
|
|
|
|
|
|
|
44
|
17
|
|
|
17
|
|
479
|
use 5.006; |
|
17
|
|
|
|
|
59
|
|
|
17
|
|
|
|
|
751
|
|
45
|
17
|
|
|
17
|
|
99
|
use strict; |
|
17
|
|
|
|
|
33
|
|
|
17
|
|
|
|
|
698
|
|
46
|
17
|
|
|
17
|
|
100
|
use Scalar::Util 'weaken'; |
|
17
|
|
|
|
|
35
|
|
|
17
|
|
|
|
|
1042
|
|
47
|
17
|
|
|
17
|
|
98
|
use base qw(Chemistry::Obj); |
|
17
|
|
|
|
|
31
|
|
|
17
|
|
|
|
|
31802
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $N = 0; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 METHODS |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=over 4 |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item Chemistry::Bond->new(name => value, ...) |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Create a new Bond object with the specified attributes. Sensible defaults |
58
|
|
|
|
|
|
|
are used when possible. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub new { |
63
|
9
|
|
|
9
|
1
|
27
|
my $class = shift; |
64
|
9
|
|
|
|
|
27
|
my %args = @_; |
65
|
9
|
|
|
|
|
36
|
my $self = bless { |
66
|
|
|
|
|
|
|
id => $class->nextID(), |
67
|
|
|
|
|
|
|
type => '', |
68
|
|
|
|
|
|
|
atoms => [], |
69
|
|
|
|
|
|
|
order => 1, |
70
|
|
|
|
|
|
|
} , $class; |
71
|
|
|
|
|
|
|
|
72
|
9
|
|
|
|
|
121
|
$self->$_($args{$_}) for (keys %args); |
73
|
9
|
|
|
|
|
51
|
$self; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub nextID { |
77
|
10
|
|
|
10
|
0
|
89
|
"b".++$N; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub reset_id { |
81
|
0
|
|
|
0
|
0
|
0
|
$N = 0; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item $bond->order() |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Sets or gets the bond order. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Chemistry::Obj::accessor('order'); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item $bond->length |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Returns the length of the bond, i.e., the distance between the two atom |
96
|
|
|
|
|
|
|
objects in the bond. Returns zero if the bond does not have exactly two atoms. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub length { |
101
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
102
|
|
|
|
|
|
|
|
103
|
1
|
50
|
|
|
|
2
|
if (@{$self->{atoms}} == 2) { |
|
1
|
|
|
|
|
5
|
|
104
|
1
|
|
|
|
|
68
|
my $v = $self->{atoms}[1]{coords} - $self->{atoms}[0]{coords}; |
105
|
1
|
|
|
|
|
52
|
return $v->length; |
106
|
|
|
|
|
|
|
} else { |
107
|
0
|
|
|
|
|
0
|
return 0; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item $bond->aromatic($bool) |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Set or get whether the bond is considered to be aromatic. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=cut |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub aromatic { |
118
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
119
|
0
|
0
|
|
|
|
0
|
if (@_) { |
120
|
0
|
|
|
|
|
0
|
($self->{aromatic}) = @_; |
121
|
0
|
|
|
|
|
0
|
return $self; |
122
|
|
|
|
|
|
|
} else { |
123
|
0
|
|
|
|
|
0
|
return $self->{aromatic}; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item $bond->print |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Convert the bond to a string representation. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub print { |
134
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
135
|
0
|
|
|
|
|
0
|
my ($indent) = @_; |
136
|
0
|
|
0
|
|
|
0
|
$indent ||= 0; |
137
|
0
|
|
|
|
|
0
|
my $l = sprintf "%.4g", $self->length; |
138
|
0
|
|
|
|
|
0
|
my $atoms = join " ", map {$_->id} $self->atoms; |
|
0
|
|
|
|
|
0
|
|
139
|
0
|
|
|
|
|
0
|
my $ret = <
|
140
|
|
|
|
|
|
|
$self->{id}: |
141
|
|
|
|
|
|
|
type: $self->{type} |
142
|
|
|
|
|
|
|
order: $self->{order} |
143
|
|
|
|
|
|
|
atoms: "$atoms" |
144
|
|
|
|
|
|
|
length: $l |
145
|
|
|
|
|
|
|
EOF |
146
|
0
|
|
|
|
|
0
|
$ret .= " attr:\n"; |
147
|
0
|
|
|
|
|
0
|
$ret .= $self->print_attr($indent); |
148
|
0
|
|
|
|
|
0
|
$ret =~ s/^/" "x$indent/gem; |
|
0
|
|
|
|
|
0
|
|
149
|
0
|
|
|
|
|
0
|
$ret; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item $bond->atoms() |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
If called with no parameters, return a list of atoms in the bond. If called |
155
|
|
|
|
|
|
|
with a list (or a reference to an array) of atom objects, define the atoms in |
156
|
|
|
|
|
|
|
the bond and call $atom->add_bond for each atom in the list. Note: changing the |
157
|
|
|
|
|
|
|
atoms in a bond may have strange side effects; it is safer to treat bonds as |
158
|
|
|
|
|
|
|
immutable except with respect to properties such as name and type. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=cut |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub atoms { |
163
|
13
|
|
|
13
|
1
|
25
|
my $self = shift; |
164
|
13
|
100
|
|
|
|
46
|
if (@_) { |
165
|
9
|
50
|
|
|
|
412
|
$self->{atoms} = ref $_[0] ? $_[0] : [@_]; |
166
|
9
|
|
|
|
|
20
|
for my $a (@{$self->{atoms}}) { |
|
9
|
|
|
|
|
27
|
|
167
|
18
|
|
|
|
|
69
|
weaken($a); |
168
|
18
|
|
|
|
|
67
|
$a->add_bond($self); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
} else { |
171
|
4
|
|
|
|
|
6
|
return (@{$self->{atoms}}); |
|
4
|
|
|
|
|
20
|
|
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub _weaken { |
176
|
84
|
|
|
84
|
|
119
|
my $self = shift; |
177
|
84
|
|
|
|
|
113
|
for my $a (@{$self->{atoms}}) { |
|
84
|
|
|
|
|
176
|
|
178
|
168
|
|
|
|
|
414
|
weaken($a); |
179
|
|
|
|
|
|
|
} |
180
|
84
|
|
|
|
|
338
|
weaken($self->{parent}); |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# This method is private and should only be called from $mol->delete_bond |
184
|
|
|
|
|
|
|
sub delete_atoms { |
185
|
14
|
|
|
14
|
0
|
23
|
my $self = shift; |
186
|
14
|
|
|
|
|
23
|
for my $a (@{$self->{atoms}}) { # delete bond from each atom |
|
14
|
|
|
|
|
36
|
|
187
|
28
|
|
|
|
|
85
|
$a->delete_bond($self); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item $bond->delete |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Calls $mol->delete_bond($bond) on the bond's parent molecule. Note that a bond |
194
|
|
|
|
|
|
|
should belong to only one molecule or strange things may happen. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub delete { |
199
|
14
|
|
|
14
|
1
|
28
|
my ($self) = @_; |
200
|
14
|
|
|
|
|
37
|
$self->parent->_delete_bond($self); |
201
|
14
|
|
|
|
|
52
|
$self->{deleted} = 1; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub parent { |
205
|
39
|
|
|
39
|
0
|
55
|
my $self = shift; |
206
|
39
|
100
|
|
|
|
98
|
if (@_) { |
207
|
25
|
|
|
|
|
54
|
($self->{parent}) = @_; |
208
|
25
|
|
|
|
|
61
|
weaken($self->{parent}); |
209
|
25
|
|
|
|
|
78
|
$self; |
210
|
|
|
|
|
|
|
} else { |
211
|
14
|
|
|
|
|
63
|
$self->{parent}; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
1; |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=back |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 VERSION |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
0.37 |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head1 SEE ALSO |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
L, L, L |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
The PerlMol website L |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 AUTHOR |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Ivan Tubert-Brohman Eitub@cpan.orgE |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head1 COPYRIGHT |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is |
238
|
|
|
|
|
|
|
free software; you can redistribute it and/or modify it under the same terms as |
239
|
|
|
|
|
|
|
Perl itself. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=cut |
242
|
|
|
|
|
|
|
|