| 1 | package Chemistry::Bond; | ||||||||||||
| 2 | 27 | $VERSION = '0.35'; | |||||||||||
| 3 | # $Id: Bond.pm,v 1.33 2005/05/20 19:01:04 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 properies. | ||||||||||||
| 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 | 27 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | 27 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 46 | 27 | ▶ | use Scalar::Util 'weaken'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 47 | 27 | ▶ | use base qw(Chemistry::Obj); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | |||||||||||||
| 49 | 27 | 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 | 144 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 63 | 144 | my $class = shift; | |||||||||||
| 64 | 144 | my %args = @_; | |||||||||||
| 65 | 144 | my $self = bless { | |||||||||||
| 66 | id => $class->nextID(), | ||||||||||||
| 67 | type => '', | ||||||||||||
| 68 | atoms => [], | ||||||||||||
| 69 | order => 1, | ||||||||||||
| 70 | } , $class; | ||||||||||||
| 71 | |||||||||||||
| 72 | 144 | $self->$_($args{$_}) for (keys %args); | |||||||||||
| 73 | 144 | $self; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 145 | ▶ | sub nextID { | ||||||||||
|
Subroutine
nextID: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 77 | 145 | "b".++$N; | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 0 | ▶ | sub reset_id { | ||||||||||
|
Subroutine
reset_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 81 | 0 | $N = 0; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | |||||||||||||
| 85 | =item $bond->order() | ||||||||||||
| 86 | |||||||||||||
| 87 | Sets or gets the bond order. | ||||||||||||
| 88 | |||||||||||||
| 89 | =cut | ||||||||||||
| 90 | |||||||||||||
| 91 | 27 | 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 | 1 | ▶ | sub length { | ||||||||||
|
Subroutine
length: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||
| 101 | 1 | my $self = shift; | |||||||||||
| 102 | |||||||||||||
| 103 | 1 | ▶ | if (@{$self->{atoms}} == 2) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 1 | my $v = $self->{atoms}[1]{coords} - $self->{atoms}[0]{coords}; | |||||||||||
| 105 | 1 | return $v->length; | |||||||||||
| 106 | } else { | ||||||||||||
| 107 | 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 | 100 | ▶ | sub aromatic { | ||||||||||
|
Subroutine
aromatic: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 118 | 100 | my $self = shift; | |||||||||||
| 119 | 100 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 26 | ($self->{aromatic}) = @_; | |||||||||||
| 121 | 26 | return $self; | |||||||||||
| 122 | } else { | ||||||||||||
| 123 | 74 | return $self->{aromatic}; | |||||||||||
| 124 | } | ||||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | =item $bond->print | ||||||||||||
| 128 | |||||||||||||
| 129 | Convert the bond to a string representation. | ||||||||||||
| 130 | |||||||||||||
| 131 | =cut | ||||||||||||
| 132 | |||||||||||||
| 133 | 0 | ▶ | sub print { | ||||||||||
|
Subroutine
print: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 134 | 0 | my $self = shift; | |||||||||||
| 135 | 0 | my ($indent) = @_; | |||||||||||
| 136 | 0 | ▶ | $indent ||= 0; | ||||||||||
|
Condition0%0/2
$indent ||= 0
MC/DC0%0/1
$indent ||= 0
$indent
Truth table0%0/2
A
A= $indent
| |||||||||||||
| 137 | 0 | my $l = sprintf "%.4g", $self->length; | |||||||||||
| 138 | 0 | my $atoms = join " ", map {$_->id} $self->atoms; | |||||||||||
| 139 | 0 | my $ret = <<EOF; | |||||||||||
| 140 | $self->{id}: | ||||||||||||
| 141 | type: $self->{type} | ||||||||||||
| 142 | order: $self->{order} | ||||||||||||
| 143 | atoms: "$atoms" | ||||||||||||
| 144 | length: $l | ||||||||||||
| 145 | EOF | ||||||||||||
| 146 | 0 | $ret .= " attr:\n"; | |||||||||||
| 147 | 0 | $ret .= $self->print_attr($indent); | |||||||||||
| 148 | 0 | $ret =~ s/^/" "x$indent/gem; | |||||||||||
| 149 | 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 | 9175 | ▶ | sub atoms { | ||||||||||
|
Subroutine
atoms: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 163 | 9175 | my $self = shift; | |||||||||||
| 164 | 9175 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 165 | 144 | ▶ | $self->{atoms} = ref $_[0] ? $_[0] : [@_]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 166 | 144 | for my $a (@{$self->{atoms}}) { | |||||||||||
| 167 | 288 | weaken($a); | |||||||||||
| 168 | 288 | $a->add_bond($self); | |||||||||||
| 169 | } | ||||||||||||
| 170 | } else { | ||||||||||||
| 171 | 9031 | return (@{$self->{atoms}}); | |||||||||||
| 172 | } | ||||||||||||
| 173 | } | ||||||||||||
| 174 | |||||||||||||
| 175 | 84 | ▶ | sub _weaken { | ||||||||||
|
Subroutine
_weaken: called CC=2 SCAR=6.9
| |||||||||||||
| 176 | 84 | my $self = shift; | |||||||||||
| 177 | 84 | for my $a (@{$self->{atoms}}) { | |||||||||||
| 178 | 168 | weaken($a); | |||||||||||
| 179 | } | ||||||||||||
| 180 | 84 | weaken($self->{parent}); | |||||||||||
| 181 | } | ||||||||||||
| 182 | |||||||||||||
| 183 | # This method is private and should only be called from $mol->delete_bond | ||||||||||||
| 184 | 14 | ▶ | sub delete_atoms { | ||||||||||
|
Subroutine
delete_atoms: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 185 | 14 | my $self = shift; | |||||||||||
| 186 | 14 | for my $a (@{$self->{atoms}}) { # delete bond from each atom | |||||||||||
| 187 | 28 | $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 | 14 | ▶ | sub delete { | ||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 199 | 14 | my ($self) = @_; | |||||||||||
| 200 | 14 | $self->parent->_delete_bond($self); | |||||||||||
| 201 | 14 | $self->{deleted} = 1; | |||||||||||
| 202 | } | ||||||||||||
| 203 | |||||||||||||
| 204 | 174 | ▶ | sub parent { | ||||||||||
|
Subroutine
parent: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 205 | 174 | my $self = shift; | |||||||||||
| 206 | 174 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 207 | 160 | ($self->{parent}) = @_; | |||||||||||
| 208 | 160 | weaken($self->{parent}); | |||||||||||
| 209 | 160 | $self; | |||||||||||
| 210 | } else { | ||||||||||||
| 211 | 14 | $self->{parent}; | |||||||||||
| 212 | } | ||||||||||||
| 213 | } | ||||||||||||
| 214 | |||||||||||||
| 215 | |||||||||||||
| 216 | |||||||||||||
| 217 | 27 | 1; | |||||||||||
| 218 | |||||||||||||
| 219 | =back | ||||||||||||
| 220 | |||||||||||||
| 221 | =head1 VERSION | ||||||||||||
| 222 | |||||||||||||
| 223 | 0.35 | ||||||||||||
| 224 | |||||||||||||
| 225 | =head1 SEE ALSO | ||||||||||||
| 226 | |||||||||||||
| 227 | L<Chemistry::Mol>, L<Chemistry::Atom>, L<Chemistry::Tutorial> | ||||||||||||
| 228 | |||||||||||||
| 229 | The PerlMol website L<http://www.perlmol.org/> | ||||||||||||
| 230 | |||||||||||||
| 231 | =head1 AUTHOR | ||||||||||||
| 232 | |||||||||||||
| 233 | Ivan Tubert-Brohman E<lt>itub@cpan.orgE<gt> | ||||||||||||
| 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 | |||||||||||||