HackaMol/Molecule.pm

filter: Statementstmt 97.3% 147 / 151 Branchbran 96.1% 25 / 26 Conditioncond 50.0% 2 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 93.3% 28 / 30 Podpod 75.0% 9 / 12 totaltotal 94.2% 212 / 225 CC 37 SCAR 11.9
CC
37
Cov
96%
fix_serial
12.0
BUILD
5.0
__ANON__
4.0
CRAP 3.3
« HackaMol/Roles/AtomGroupRole.pm ↑ Summary HackaMol/Roles/NERFRole.pm »
1 package HackaMol::Molecule;
2 12 $HackaMol::Molecule::VERSION = '0.053';
3 #ABSTRACT: Molecule class for HackaMol
4 12 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #use MooseX::Storage;
13  
14 12 with 'HackaMol::Roles::PhysVecMVRRole',
15 'HackaMol::Roles::BondsAnglesDihedralsRole', 'HackaMol::Roles::QmMolRole'; #,
16 # 'HackaMol::Roles::SelectionRole';
17  
18 #, Storage( 'format' => 'JSON', 'io' => 'File' );
19  
20 12 extends 'HackaMol::AtomGroup';
21  
22 has 'groups' => (
23 traits => ['Array'],
24 is => 'ro',
25 isa => 'ArrayRef[HackaMol::AtomGroup]',
26 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 12 lazy => 1,
28 handles => {
29 has_groups => 'count',
30 push_groups => 'push',
31 get_groups => 'get',
32 set_groups => 'set',
33 all_groups => 'elements',
34 sort_groups => 'sort',
35 insert_groups => 'insert',
36 count_groups => 'count',
37 delete_groups => 'delete',
38 clear_groups => 'clear',
39 select_groups => 'grep',
40 map_groups => 'map',
41 },
42 );
43  
44 # an array to map t to some other label (e.g. model number from pdb)
45 has 'model_ids' => (
46 traits => ['Array'],
47 is => 'ro',
48 isa => 'ArrayRef[Str]',
49 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 12 predicate => 'has_models',
51 handles => {
52 "push_model_ids" => 'push',
53 "get_model_id" => 'get',
54 "set_model_id" => 'set',
55 "all_model_ids" => 'elements',
56 "count_model_ids" => 'count',
57 },
58 lazy => 1,
59 );
60  
61 24 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
62 24 my $self = shift;
63 24 foreach my $bond ( $self->all_bonds ) {
64 9 $_->inc_bond_count foreach $bond->all_atoms;
65 }
66  
67 #all the molecule to be build from groups or atoms
68 24 ▶ return if $self->has_atoms;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_atoms 20 4 24
69  
70 4 ▶ if ( $self->has_groups ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_groups) 2 2 4
71 2 ▶ $self->push_atoms( $self->map_groups( sub { $_->all_atoms } ) );
Subroutine __ANON__: called CC=1 SCAR=0.0
72 }
73 4 return;
74 }
75  
76 5 ▶after 'push_groups' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
77  
78 # if you push a group onto a molecule, the atoms should be added unless they
79 # exist!
80 5 my $self = shift;
81 5 my @groups = @_;
82 5 foreach my $group (@groups) {
83 100 foreach my $atom ( $group->all_atoms ) {
84 926 ▶ unless ( grep { $atom == $_ } $self->all_atoms ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$atom == $_;} $self->all_atoms) 12 914 926
85 12 $self->push_atoms($atom);
86 }
87  
88 # debug
89 # else {
90 # print "found it\n $atom \n" if grep {$atom == $_} $self->all_atoms;
91 # }
92 }
93 }
94 12 };
95  
96 4 ▶sub charge {
Subroutine charge: called CC=3 SCAR=11.0
Pod undocumented
97 4 my $self = shift;
98 4 my $t = $self->t;
99 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 3 4
100 1 my $new_q = shift;
101 1 $self->set_charges( $t, $new_q );
102 }
103 4 ▶ return $self->get_charges($t) || 0; # default to 0
Condition100%2/2
$self->get_charges($t) || 0
l!l
31
MC/DC100%1/1
$self->get_charges($t) || 0
$self->get_charges($t)
Truth table100%2/2
A
A= $self->get_charges($t)
Aresult
11
00
104 }
105  
106 # need to increase atom bond_count when push
107 4 ▶after 'push_bonds' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
108 4 my $self = shift;
109 4 foreach my $bond (@_) {
110 371 $_->inc_bond_count foreach $bond->all_atoms;
111 }
112 12 };
113  
114 # need to reduce atom bond_count when set,delete, or clear
115 3 ▶before 'delete_bonds' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
116 3 my $self = shift;
117 3 my $bond = $self->get_bonds(@_);
118 3 $_->dec_bond_count foreach $bond->all_atoms;
119 12 };
120  
121 1 ▶around 'set_bonds' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
122 1 my ( $orig, $self, $index, $bond ) = @_;
123 1 my $oldbond = $self->get_bonds($index);
124 1 ▶ if ( defined($oldbond) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oldbond) 1 0 1
125 1 $_->dec_bond_count foreach $oldbond->all_atoms;
126 }
127 1 $_->inc_bond_count foreach $bond->all_atoms;
128 1 $self->$orig( $index, $bond );
129 12 };
130  
131 2 ▶before 'clear_bonds' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
132 2 my $self = shift;
133 2 foreach my $bond ( $self->all_bonds ) {
134 288 $_->dec_bond_count foreach $bond->all_atoms;
135 }
136 12 };
137  
138 99 ▶after 't' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
139 99 my $self = shift;
140 99 ▶ $self->gt(@_) if (@_); # set t for all in group
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 94 99
141 12 };
142  
143 1 ▶sub _build_mass {
Subroutine _build_mass: called CC=2 SCAR=6.9
144 1 my $self = shift;
145 1 my $mass = 0;
146 1 $mass += $_->mass foreach $self->all_atoms;
147 1 return ($mass);
148 }
149  
150 0 ▶sub fix_serial {
Subroutine fix_serial: not called CC=3 SCAR=24.8
Pod undocumented
151 0 my @atoms = shift->all_atoms;
152 0 ▶ my $offset = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
153 0 $atoms[$_]->{serial} = $_ + $offset foreach ( 0 .. $#atoms );
154 }
155  
156 2 ▶sub all_bonds_atoms { return ( shift->_all_these_atoms( 'bonds', @_ ) ) }
Subroutine all_bonds_atoms: called CC=1 SCAR=0.0
Pod documented
157 2 ▶sub all_angles_atoms { return ( shift->_all_these_atoms( 'angles', @_ ) ) }
Subroutine all_angles_atoms: called CC=1 SCAR=0.0
Pod documented
158  
159 1 ▶sub all_dihedrals_atoms {
Subroutine all_dihedrals_atoms: called CC=1 SCAR=0.0
Pod documented
160 1 return ( shift->_all_these_atoms( 'dihedrals', @_ ) );
161 }
162  
163 5 ▶sub _all_these_atoms {
Subroutine _all_these_atoms: called CC=4 SCAR=13.9
164  
165 #these bonds, these angles, these dihedrals
166 #this bond, this angle, this dihedral
167 5 my $self = shift;
168 5 my $these = shift;
169 5 my @atoms = @_;
170 5 my $method = "all_$these";
171 5 my @all_these = $self->$method;
172 5 my @atoms_these;
173 5 foreach my $this (@all_these) {
174 321 my @thatoms = $this->all_atoms;
175 321 foreach my $atom (@atoms) {
176 push @atoms_these, $this
177 513 ▶ if ( grep { refaddr($atom) == refaddr($_) } @thatoms );
Branch: 1 branch
BranchTrueFalseTotal
if grep {builtin::refaddr($atom) == builtin::refaddr($_);} @thatoms 23 490 513
178 }
179 }
180 5 return (@atoms_these);
181 }
182  
183 2 ▶sub bond_stretch_groups {
Subroutine bond_stretch_groups: called CC=3 SCAR=11.0
Pod documented
184 2 my $self = shift;
185 2 ▶ croak "pass Bond, trans distance (Angstroms), 1+ groups to trans"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 2 1 1 2
186 unless @_ > 2;
187 1 my $t = $self->t;
188 1 my ( $bond, $dist ) = ( shift, shift );
189 1 my $vec = $bond->bond_vector;
190 1 my @groups = @_;
191 1 my $tvec = $dist * $vec->versor;
192 1 $_->translate( $tvec, $t ) foreach @groups;
193 }
194  
195 2 ▶sub bond_stretch_atoms {
Subroutine bond_stretch_atoms: called CC=3 SCAR=11.0
Pod documented
196 2 my $self = shift;
197 2 ▶ croak "pass Bond, trans distance (Angstroms), 1+ atoms to trans"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 2 1 1 2
198 unless @_ > 2;
199 1 my $t = $self->t;
200 1 my ( $bond, $dist ) = ( shift, shift );
201 1 my $vec = $bond->bond_vector;
202 1 my @atoms = @_;
203 1 my $tvec = $dist * $vec->versor;
204 1 $_->set_coords( $t, $_->xyz + $tvec ) foreach @atoms;
205 }
206  
207 3 ▶sub angle_bend_groups {
Subroutine angle_bend_groups: called CC=3 SCAR=11.0
Pod documented
208 3 my $self = shift;
209 3 ▶ croak "pass Angle, ang to rotate (degrees), 1+ groups effected"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 2 1 2 3
210 unless @_ > 2;
211 2 my $t = $self->t;
212 2 my ( $angle, $dang ) = ( shift, shift );
213 2 my $origin = $angle->get_atoms(1)->get_coords($t);
214 2 my $rvec = $angle->ang_normvec;
215 2 my @groups = @_;
216 2 $_->rotate( $rvec, $dang, $origin, $t ) foreach @groups;
217 }
218  
219 2 ▶sub angle_bend_atoms {
Subroutine angle_bend_atoms: called CC=3 SCAR=11.0
Pod documented
220 2 my $self = shift;
221 2 ▶ croak "pass Angle, ang to rotate (degrees), 1+ groups effected"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 2 1 1 2
222 unless @_ > 2;
223 1 my $t = $self->t;
224 1 my ( $angle, $dang ) = ( shift, shift );
225 1 my $origin = $angle->get_atoms(1)->get_coords($t);
226 1 my $rvec = $angle->ang_normvec;
227 1 my @atoms = @_;
228  
229 my @cor =
230 1 map { $_->get_coords($t) - $origin } @atoms; #shift origin
231 1 my @rcor = $rvec->rotate_3d( deg2rad($dang), @cor );
232  
233 #shift origin back
234 1 $atoms[$_]->set_coords( $t, $rcor[$_] + $origin ) foreach 0 .. $#rcor;
235 }
236  
237 64 ▶sub dihedral_rotate_atoms {
Subroutine dihedral_rotate_atoms: called CC=3 SCAR=11.0
Pod documented
238 64 my $self = shift;
239 64 ▶ croak "pass Dihedral, rotation angle (deg), atoms to rotate" unless @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 2 1 63 64
240 63 my $t = $self->t;
241 63 my ( $dihe, $dang ) = ( shift, shift );
242 63 my ( $atom0, $ratom1, $ratom2, $atom3 ) = $dihe->all_atoms;
243 63 my $rvec = ( $ratom2->inter_dcoords($ratom1) )->versor;
244 63 my $origin = $ratom1->xyz;
245 63 my @atoms = @_;
246 my @cor =
247 63 map { $_->get_coords($t) - $origin } @atoms; #shift origin too
248 63 my @rcor = $rvec->rotate_3d( deg2rad($dang), @cor );
249  
250 #shift origin back
251 63 $atoms[$_]->set_coords( $t, $rcor[$_] + $origin ) foreach 0 .. $#rcor;
252  
253 }
254  
255 2 ▶sub dihedral_rotate_groups {
Subroutine dihedral_rotate_groups: called CC=3 SCAR=11.0
Pod documented
256 2 my $self = shift;
257 2 ▶ croak "pass Dihedral, rotation angle (deg), atoms to rotate" unless @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 2 1 1 2
258 1 my $t = $self->t;
259 1 my ( $dihe, $dang ) = ( shift, shift );
260 1 my ( $atom0, $ratom1, $ratom2, $atom3 ) = $dihe->all_atoms;
261 1 my $rvec = ( $ratom2->inter_dcoords($ratom1) )->versor;
262 1 my $origin = $ratom1->xyz;
263 1 my @groups = @_;
264 1 $_->rotate( $rvec, $dang, $origin, $t ) foreach @groups;
265  
266 }
267  
268 12 __PACKAGE__->meta->make_immutable;
269  
270 12 1;
271  
272 __END__
« HackaMol/Roles/AtomGroupRole.pm ↑ Summary HackaMol/Roles/NERFRole.pm »