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