HackaMol/Roles/AtomGroupRole.pm

filter: Statementstmt 92.7% 270 / 291 Branchbran 88.1% 67 / 76 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 97.5% 39 / 40 Podpod 78.5% 22 / 28 totaltotal 91.1% 399 / 438 SCAR 41.3
CC
60
Cov
92%
rotate_translate
12.0
rotate
6.0
calc_bfps
5.4
CRAP 62.1
« HackaMol/Roles/ReadZmatRole.pm ↑ Summary HackaMol/Molecule.pm »
1 package HackaMol::Roles::AtomGroupRole;
2 17 $HackaMol::Roles::AtomGroupRole::VERSION = '0.051';
3 #ABSTRACT: Role for a group of atoms
4 17 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Math::Vector::Real;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #use MooseX::Storage;
13 #with Storage( 'format' => 'JSON', 'io' => 'File', traits => ['OnlyWhenBuilt'] );
14  
15 17 my $angste_debye = 4.80320;
16  
17 has 'atoms' => (
18 traits => ['Array'],
19 is => 'ro',
20 isa => 'ArrayRef[HackaMol::Atom]',
21 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 17 handles => {
23 unshift_atoms => 'unshift',
24 push_atoms => 'push',
25 select_atoms => 'grep',
26 map_atoms => 'map',
27 sort_atoms => 'sort',
28 get_atoms => 'get',
29 set_atoms => 'set',
30 insert_atoms => 'insert',
31 delete_atoms => 'delete',
32 all_atoms => 'elements',
33 count_atoms => 'count',
34 natoms => 'count',
35 clear_atoms => 'clear',
36 has_atoms => 'count',
37 },
38 lazy => 1,
39 );
40  
41 17 has 'is_constrained' => (
42 is => 'rw',
43 isa => 'Bool',
44 lazy => 1,
45 default => 0,
46 );
47  
48 17 has 'qcat_print' => (
49 is => 'rw',
50 isa => 'Bool',
51 lazy => 1,
52 default => 0,
53 );
54  
55 17 has 'info' => (
56 is => 'rw',
57 isa => 'Str',
58 lazy => 1,
59 default => "",
60 );
61  
62 sub _lsq_slope {
63 # private function used for each coordinate
64 # translation of tcl function written by Justin Gullingsrud @ uiuc.edu
65 # algorithm reference: Bevington
66 # Fit the points x to x = ai + b, i=0...N-1, and return the value of a
67 # a = 12/( (N(N^2 - 1)) ) sum[ (i-(N-1)/2) * xi]
68 11 ▶ my $xis = shift || die "expecting array_ref of cartesian coordinate [x y or z]";
Subroutine _lsq_slope: called CC=3 SCAR=11.0
Condition50%1/2
shift() || die('expecting array_ref of cartesian coordinate [x y or z]')
l!l
110
MC/DC0%0/1
shift() || die('expecting array_ref of cartesian coordinate [x y or z]')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
69 11 my $n = @$xis;
70 11 my $sum = 0;
71 11 my $d = ($n-1)/2;
72 11 my $i = 0;
73 11 $sum += ($i++ - $d)*$_ foreach @$xis;
74 11 return $sum * 12 / ($n * ( $n * $n - 1)) ;
75 }
76  
77 sub centered_vector {
78 2 ▶ my $self = shift;
Subroutine centered_vector: called CC=2 SCAR=6.9
Pod documented
79 2 my $centered_flag = shift;
80 2 my @mvrs = map {$_->xyz} $self->all_atoms;
81 2 ▶ die "2 atoms needed for a centered_vector" unless @mvrs > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @mvrs > 1 0 2 2
82 2 my @x = map { $_->[0] } @mvrs;
83 2 my @y = map { $_->[1] } @mvrs;
84 2 my @z = map { $_->[2] } @mvrs;
85 2 my $mvr = V( map { _lsq_slope($_) } \@x,\@y,\@z);
86 2 return $mvr->versor;
87 }
88  
89 sub calc_bfps {
90  
91 # this should be rerun for each selection
92 #10.1016/j.jmb.2015.09.024
93 2 ▶ my $self = shift;
Subroutine calc_bfps: called CC=5 SCAR=16.9
Pod undocumented
94 2 ▶ unless ( $self->count_atoms > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->count_atoms > 1) 0 2 2
95 0 warn "calc_bfps> group not large enough\n";
96 0 return;
97 }
98 2 my @atoms = $self->all_atoms;
99 2 my @bfacts = map { $_->bfact } @atoms;
100 2 my $bfact_mean = sum(@bfacts) / @bfacts;
101 2 my $sd = 0;
102 2 $sd += ( $_ - $bfact_mean )**2 foreach @bfacts;
103 2 ▶ unless ( $sd > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sd > 0) 0 2 2
104 0 warn "calc_bfps> no variance in the group bfactors\n";
105 0 return;
106 }
107 2 my $bfact_std = sqrt( $sd / ( @bfacts - 1 ) );
108 2 foreach my $atom (@atoms) {
109 6 my $bfp = ( $atom->bfact - $bfact_mean ) / $bfact_std;
110 6 $atom->bfp($bfp);
111 }
112 2 return map { $_->bfp } @atoms;
113 }
114  
115 sub dipole {
116 9 ▶ my $self = shift;
Subroutine dipole: called CC=4 SCAR=13.9
Pod documented
117 9 ▶ return ( V(0) ) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 8 9
118 8 my @atoms = $self->all_atoms;
119 8 my @vectors = grep { defined } map { $_->get_coords( $_->t ) } @atoms;
120 8 my @charges = grep { defined } map { $_->get_charges( $_->t ) } @atoms;
121 8 my $dipole = V( 0, 0, 0 );
122 8 ▶ if ( $#vectors != $#charges ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#vectors != $#charges) 1 7 8
123 1 carp
124 "build_dipole> mismatch number of coords and charges. all defined?";
125 1 return $dipole;
126 }
127 7 $dipole += $vectors[$_] * $charges[$_] foreach 0 .. $#charges;
128 7 return ($dipole);
129 }
130  
131 sub COM {
132 40 ▶ my $self = shift;
Subroutine COM: called CC=3 SCAR=11.0
Pod documented
133 40 ▶ return ( V(0) ) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 39 40
134 39 my @atoms = $self->all_atoms;
135 39 my @m_vectors = map { $_->mass * $_->get_coords( $_->t ) } @atoms;
136 39 my $com = V( 0, 0, 0 );
137 39 $com += $_ foreach @m_vectors;
138 39 return ( $com / $self->total_mass );
139 }
140  
141 sub center {
142 4 ▶ my $self = shift;
Subroutine center: called CC=3 SCAR=11.0
Pod undocumented
143 4 ▶ return ( V(0) ) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 0 4 4
144 4 my @atoms = $self->all_atoms;
145 4 my @vectors = map { $_->get_coords( $_->t ) } @atoms;
146 4 my $center = V( 0, 0, 0 );
147 4 $center += $_ foreach @vectors;
148 4 return ( $center / $self->count_atoms );
149 }
150  
151 sub COZ {
152 6 ▶ my $self = shift;
Subroutine COZ: called CC=3 SCAR=11.0
Pod documented
153 6 ▶ return ( V(0) ) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 5 6
154 5 my @atoms = $self->all_atoms;
155 5 my @z_vectors = map { $_->Z * $_->get_coords( $_->t ) } @atoms;
156 5 my $coz = V( 0, 0, 0 );
157 5 $coz += $_ foreach @z_vectors;
158 5 return ( $coz / $self->total_Z );
159 }
160  
161 sub gt {
162  
163 #set group time
164 39 ▶ my $self = shift;
Subroutine gt: called CC=1 SCAR=0.0
Pod documented
165 39 my $t = shift;
166 39 $self->do_forall( 't', $t );
167 }
168  
169 sub do_forall {
170 63 ▶ my $self = shift;
Subroutine do_forall: called CC=3 SCAR=11.0
Pod undocumented
171 63 my $method = shift;
172 63 ▶ do { carp "doing nothing for all"; return } unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 62 63
173 62 my @atoms = $self->all_atoms;
174 62 $_->$method(@_) foreach @atoms;
175 }
176  
177 sub total_charge {
178 5 ▶ my $self = shift;
Subroutine total_charge: called CC=3 SCAR=11.0
Pod documented
179 5 ▶ return (0) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 4 5
180 4 my @atoms = $self->all_atoms;
181 4 my @charges = map { $_->get_charges( $_->t ) } @atoms;
182 4 my $sum = 0;
183 4 $sum += $_ foreach @charges;
184 4 return ($sum);
185 }
186  
187 sub total_mass {
188 48 ▶ my $self = shift;
Subroutine total_mass: called CC=3 SCAR=11.0
Pod documented
189 48 ▶ return (0) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 47 48
190 47 my @masses = map { $_->mass } $self->all_atoms;
191 47 my $sum = 0;
192 47 $sum += $_ foreach @masses;
193 47 return ($sum);
194 }
195  
196 sub total_Z {
197 7 ▶ my $self = shift;
Subroutine total_Z: called CC=3 SCAR=11.0
Pod documented
198 7 ▶ return (0) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 6 7
199 6 my @Zs = map { $_->Z } $self->all_atoms;
200 6 my $sum = 0;
201 6 $sum += $_ foreach @Zs;
202 6 return ($sum);
203 }
204  
205 sub dipole_moment {
206 6 ▶ my $self = shift;
Subroutine dipole_moment: called CC=1 SCAR=0.0
Pod documented
207 6 return ( abs( $self->dipole ) * $angste_debye );
208 }
209  
210 sub bin_atoms {
211  
212 # Called with no arguments.
213 # Returns a hash with a count of unique atom symbols
214 13 ▶ my $self = shift;
Subroutine bin_atoms: called CC=1 SCAR=0.0
Pod documented
215 13 my $bin_hr = $self->bin_this('symbol');
216 13 return ($bin_hr);
217 }
218  
219 sub count_unique_atoms {
220 5 ▶ my $self = shift;
Subroutine count_unique_atoms: called CC=1 SCAR=0.0
Pod documented
221 5 my $bin_hr = $self->bin_atoms;
222 5 return ( scalar( keys %{$bin_hr} ) );
223 }
224  
225 sub bin_atoms_name {
226  
227 # return something like C4H10 sort in order of descending Z
228 7 ▶ my $self = shift;
Subroutine bin_atoms_name: called CC=2 SCAR=6.9
Pod documented
229 7 my $bin_hr = $self->bin_atoms;
230 7 my $z_hr;
231 7 $z_hr->{ $_->symbol } = $_->Z foreach $self->all_atoms;
232  
233 my @names = map {
234 9 my $name = $_ . $bin_hr->{$_};
235 9 $name =~ s/(\w+)1$/$1/;
236 9 $name; # substitue 1 away?
237 }
238 sort {
239 2 $z_hr->{$b} <=> $z_hr->{$a} # sort by Z! see above...
240 7 } keys %{$bin_hr};
241 7 return join( '', @names );
242 }
243  
244 sub translate {
245 5 ▶ my $self = shift;
Subroutine translate: called CC=5 SCAR=16.1
Pod documented
246 5 ▶ my $tvec = shift or croak "pass MVR translation vector";
Branch: 1 branch
BranchTrueFalseTotal
my $tvec = shift() or croak("pass MVR translation vector") 1 4 5
247 4 my $tf = shift;
248  
249 4 my @atoms = $self->all_atoms;
250 4 ▶ do { carp "no atoms to translate"; return } unless (@atoms);
Branch: 1 branch
BranchTrueFalseTotal
unless (@atoms) 1 3 4
251 3 ▶ $tf = $atoms[0]->t unless ( defined($tf) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tf 2 1 3
252  
253 3 foreach my $at (@atoms) {
254 69 my $v = $at->xyz + $tvec;
255 69 $at->set_coords( $tf, $v );
256 }
257 }
258  
259 sub rotate {
260  
261 #rotate about origin. having origin allows rotation of subgroup
262 #without having to translate everything.
263 8 ▶ my $self = shift;
Subroutine rotate: called CC=6 SCAR=17.9
Pod documented
264 8 ▶ my $rvec = shift or croak "pass MVR rotation vector";
Branch: 1 branch
BranchTrueFalseTotal
my $rvec = shift() or croak("pass MVR rotation vector") 1 7 8
265 7 ▶ my $ang = shift or croak "pass rotation angle";
Branch: 1 branch
BranchTrueFalseTotal
my $ang = shift() or croak("pass rotation angle") 1 6 7
266 6 ▶ my $orig = shift or croak "pass MVR origin";
Branch: 1 branch
BranchTrueFalseTotal
my $orig = shift() or croak("pass MVR origin") 1 5 6
267 5 my $tf = shift;
268  
269 5 my @atoms = $self->all_atoms;
270 5 my $t = $atoms[0]->t;
271 5 ▶ $tf = $t unless ( defined($tf) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tf 1 4 5
272 5 $rvec = $rvec->versor; #unit vector
273  
274 5 my @cor = map { $_->get_coords($t) - $orig } @atoms;
275 5 my @rcor = $rvec->rotate_3d( deg2rad($ang), @cor );
276  
277 5 $atoms[$_]->set_coords( $tf, $rcor[$_] + $orig ) foreach 0 .. $#rcor;
278 }
279  
280 sub rotate_translate {
281  
282 # args:
283 # 1. rotation matrix (3x3): ar, each column (cx,cy,cz, below) is a Math::Vector::Real
284 # 2. translate vector, MVR
285 # r' = x*cx + y*cy + z*cz + v_trans
286 # 3. t final, the final t location for transformed coordinates [defaults to current t]
287 0 ▶ my $self = shift;
Subroutine rotate_translate: not called CC=3 SCAR=24.8
Pod undocumented
288 0 my $rmat = shift;
289 0 my $trns = shift;
290 0 my $tf = shift;
291 0 my @atoms = $self->all_atoms;
292 0 my ( $cx, $cy, $cz ) = @{$rmat};
293  
294 0 my $t = $atoms[0]->t;
295 0 ▶ $tf = $t unless ( defined($tf) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tf 0 0 0
296  
297 0 foreach my $atom (@atoms) {
298 0 my $xyz = $atom->xyz;
299  
300 #my ($x,$y,$z) = @{$xyz};
301 0 my $xr = $cx * $xyz;
302 0 my $yr = $cy * $xyz;
303 0 my $zr = $cz * $xyz;
304  
305 #my $xyz_new = $x*$cx + $y*$cy + $z*$cz + $trns;
306 0 my $xyz_new = V( $xr, $yr, $zr ) + $trns;
307 0 $atom->set_coords( $tf, $xyz_new );
308 }
309  
310 }
311  
312 sub fix_serial {
313 3 ▶ my @atoms = shift->all_atoms;
Subroutine fix_serial: called CC=3 SCAR=11.0
Pod documented
314 3 my $offset = shift;
315 3 ▶ $offset = 1 unless defined($offset);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 1 2 3
316 3 $atoms[$_]->{serial} = $_ + $offset foreach ( 0 .. $#atoms );
317 3 return $offset;
318 }
319  
320 sub print_xyz_ts {
321 4 ▶ _print_ts( 'print_xyz', @_ );
Subroutine print_xyz_ts: called CC=1 SCAR=0.0
Pod documented
322 }
323  
324 sub print_pdb_ts {
325 2 ▶ _print_ts( 'print_pdb', @_ );
Subroutine print_pdb_ts: called CC=1 SCAR=0.0
Pod documented
326 }
327  
328 sub _print_ts {
329  
330 #use one sub for xyz_ts and pdb_ts writing
331 6 ▶ my $print_method = shift;
Subroutine _print_ts: called CC=5 SCAR=16.1
332  
333 # two args: \@ts, optional filename
334 6 my $self = shift;
335 6 my $ts = shift;
336 6 ▶ unless ( defined($ts) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ts) 1 5 6
337 1 croak "must pass arrayref containing ts";
338 }
339 5 my @ts = @$ts;
340 5 ▶ unless ( scalar(@ts) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @ts) 1 4 5
341 1 croak "must pass array with atleast one t";
342 }
343 4 my $tmax = $self->tmax;
344 4 my $nt = grep { $_ > $tmax } @ts;
345 4 ▶ croak "$nt ts out of bounds" if $nt;
Branch: 1 branch
BranchTrueFalseTotal
if $nt 2 2 4
346  
347 2 my $tnow = $self->what_time;
348  
349 # take the first out of the loop to setup fh
350 2 $self->gt( shift @ts );
351 2 my $fh = $self->$print_method(@_);
352  
353 2 foreach my $t (@ts) {
354 4 $self->gt($t);
355 4 $fh = $self->$print_method($fh);
356 }
357  
358 # return to original t
359 2 $self->gt($tnow);
360 }
361  
362 sub bin_this {
363  
364 #return hash{$_->method}++
365 34 ▶ my $self = shift;
Subroutine bin_this: called CC=4 SCAR=13.9
Pod documented
366 34 my $method = shift;
367  
368 34 ▶ return ( {} ) unless $self->count_atoms;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 2 32 34
369  
370 32 my @atoms = $self->all_atoms;
371  
372 # just test the first one...
373 32 ▶ croak "Atom does not do $method" unless $atoms[0]->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $atoms[0]->can($method) 0 32 32
374  
375 32 my $bin;
376 32 $bin->{$_}++ foreach ( map { $_->$method } @atoms );
377 32 return ($bin);
378  
379 }
380  
381 sub tmax {
382  
383 # still not the best implementation! what about atoms without coords?
384 12 ▶ my $self = shift;
Subroutine tmax: called CC=3 SCAR=11.0
Pod documented
385 12 my $tbin = $self->bin_this('count_coords');
386 12 my @ts = keys(%$tbin);
387 12 ▶ croak "tmax differences within group" if ( scalar(@ts) > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ts > 1 1 11 12
388 11 ▶ $ts[0] ? return $ts[0] - 1 : return 0;
Branch: 1 branch
BranchTrueFalseTotal
$ts[0] ? : 10 1 11
389 }
390  
391 sub what_time {
392 9 ▶ my $self = shift;
Subroutine what_time: called CC=2 SCAR=6.9
Pod documented
393 9 my $tbin = $self->bin_this('t');
394 9 my @ts = keys(%$tbin);
395 9 ▶ carp "what_time> t differences within group!!" if ( scalar(@ts) > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ts > 1 1 8 9
396 9 return $ts[0];
397 }
398  
399 sub string_xyz {
400 11 ▶ my $self = shift;
Subroutine string_xyz: called CC=4 SCAR=13.9
Pod undocumented
401 11 my $add_info_to_blank = shift;
402  
403 11 my $string;
404 11 ▶ $string .= $self->count_atoms . "\n" unless $self->qcat_print;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->qcat_print 11 0 11
405 11 ▶ $string .= $add_info_to_blank if ( defined($add_info_to_blank) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $add_info_to_blank 1 10 11
406 11 $string .= "\n";
407  
408 11 foreach my $at ( $self->all_atoms ) {
409 $string .= sprintf( "%3s %10.6f %10.6f %10.6f\n",
410 936 $at->symbol, @{ $at->get_coords( $at->t ) } );
411 }
412 11 return $string;
413 }
414  
415 sub print_xyz {
416 10 ▶ my $self = shift;
Subroutine print_xyz: called CC=1 SCAR=0.0
Pod documented
417 10 my $fh = _open_file_unless_fh(shift);
418  
419 9 print $fh $self->string_xyz;
420  
421 # my @atoms = $self->all_atoms;
422 #print $fh $self->count_atoms . "\n\n" unless $self->qcat_print;
423 #foreach my $at (@atoms) {
424 # printf $fh (
425 # "%3s %10.6f %10.6f %10.6f\n",
426 # $at->symbol, @{ $at->get_coords( $at->t ) }
427 # );
428 #}
429  
430 9 return ($fh); # returns filehandle for future writing
431  
432 }
433  
434 sub string_pdb {
435 6 ▶ my $self = shift;
Subroutine string_pdb: called CC=5 SCAR=16.1
Pod undocumented
436  
437 6 my $t = $self->what_time;
438 6 my @atoms = $self->all_atoms;
439  
440 6 my $string;
441 6 ▶ $string .= sprintf( "MODEL %2i\n", $t + 1 ) unless $self->qcat_print;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->qcat_print 5 1 6
442  
443 6 my $atform =
444 "%-6s%5i %-3s%1s%3s %1s%4s%1s %8.3f%8.3f%8.3f%6.2f%6.2f %4s%2s\n";
445  
446 6 foreach my $at (@atoms) {
447  
448 # front pad one space if name length is < 4
449 18 my $form = $atform;
450 18 ▶ if ( length $at->name > 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $at->name > 3) 0 18 18
451 0 $form =
452 "%-6s%5i %4s%1s%3s %1s%4s%1s %8.3f%8.3f%8.3f%6.2f%6.2f %4s%2s\n";
453 }
454 $string .= sprintf(
455 $form,
456 (
457 144 map { $at->$_ }
458 qw (
459 record_name
460 serial
461 name
462 altloc
463 resname
464 chain
465 resid
466 icode
467 )
468 ),
469 18 @{ $at->get_coords( $at->t ) },
470 $at->occ,
471 $at->bfact,
472 $at->segid,
473 $at->symbol, # $at->charge
474 );
475 }
476 6 ▶ $string .= "ENDMDL\n" unless $self->qcat_print;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->qcat_print 5 1 6
477 6 return $string;
478 }
479  
480 sub print_pdb {
481 6 ▶ my $self = shift;
Subroutine print_pdb: called CC=1 SCAR=0.0
Pod documented
482 6 my $fh = _open_file_unless_fh(shift);
483  
484 6 print $fh $self->string_pdb;
485  
486 # my @atoms = $self->all_atoms;
487 # printf $fh ( "MODEL %2i\n", $atoms[0]->t + 1 ) unless $self->qcat_print;
488 # my $atform = "%-6s%5i %-3s%1s%3s %1s%4i%1s %8.3f%8.3f%8.3f%6.2f%6.2f %4s%2s\n";
489  
490 # foreach my $at (@atoms) {
491 # # front pad one space if name length is < 4
492 # my $form = $atform;
493 # if (length $at->name > 3){
494 # $form = "%-6s%5i %4s%1s%3s %1s%4i%1s %8.3f%8.3f%8.3f%6.2f%6.2f %4s%2s\n"
495 # }
496 # printf $fh (
497 # $form,
498 # (
499 # map { $at->$_ }
500 # qw (
501 # record_name
502 # serial
503 # name
504 # altloc
505 # resname
506 # chain
507 # resid
508 # icode
509 # )
510 # ),
511 # @{ $at->get_coords( $at->t ) },
512 # $at->occ,
513 # $at->bfact,
514 # $at->segid,
515 # $at->symbol, # $at->charge
516 # );
517  
518 # }
519 # print $fh "ENDMDL\n" unless $self->qcat_print;
520  
521 6 return ($fh); # returns filehandle for future writing
522 }
523  
524 sub _open_file_unless_fh {
525  
526 16 ▶ my $file = shift; # could be file or filehandle
Subroutine _open_file_unless_fh: called CC=5 SCAR=16.1
527  
528 16 my $fh = \*STDOUT; # default to standard out
529 # if argument is passed, check if filehandle
530 16 ▶ if ( defined($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 9 7 16
531 9 ▶ if ( ref($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 6 3 9
532 6 ▶ if ( reftype($file) eq "GLOB" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($file) eq 'GLOB') { } 5 1 6
533 5 $fh = $file;
534 }
535 else {
536 1 croak "trying write to reference that is not a GLOB";
537 }
538 }
539 else {
540 3 ▶ carp "overwrite $file" if ( -e $file );
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 1 2 3
541 3 $fh = FileHandle->new(">$file");
542 }
543 }
544  
545 15 return ($fh);
546 }
547  
548 17 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
549  
550 17 1;
551  
552 __END__
553  
554 =pod
555  
556 =head1 NAME
557  
558 HackaMol::Roles::AtomGroupRole - Role for a group of atoms
559  
560 =head1 VERSION
561  
562 version 0.051
563  
564 =head1 SYNOPSIS
565  
566 use HackaMol::AtomGroup;
567 use HackaMol::Atom;
568  
569 my $atom1 = HackaMol::Atom->new(
570 name => 'O1',
571 coords => [ V( 2.05274, 0.01959, -0.07701 ) ],
572 Z => 8,
573 );
574
575 my $atom2 = HackaMol::Atom->new(
576 name => 'H1',
577 coords => [ V( 1.08388, 0.02164, -0.12303 ) ],
578 Z => 1,
579 );
580
581 my $atom3 = HackaMol::Atom->new(
582 name => 'H2',
583 coords => [ V( 2.33092, 0.06098, -1.00332 ) ],
584 Z => 1,
585 );
586
587 $atom1->push_charges(-0.834);
588 $_->push_charges(0.417) foreach ($atom1, $atom2);
589
590 # instance of class that consumes the AtomGroupRole
591
592 my $group = HackaMol::AtomGroup->new(atoms=> [$atom1,$atom2,$atom3]);
593
594 print $group->count_atoms . "\n"; #3
595 print $group->total_charge . "\n"; # 0
596 print $group->total_mass . "\n";
597
598 my @atoms = $group->all_atoms;
599
600 print $group->dipole_moment . "\n";
601
602 $group->do_forall('push_charges',0);
603 $group->do_forall('push_coords',$group->COM);
604  
605 $group->gt(1); # same as $group->do_forall('t',1);
606
607 print $group->dipole_moment . "\n";
608 print $group->bin_atoms_name . "\n";
609 print $group->unique_atoms . "\n";
610  
611 $group->translate(V(10,0,0));
612  
613 $group->rotate( V(1,0,0),
614 180,
615 V(0,0,0));
616
617 $group->print_xyz ; #STDOUT
618  
619 my $fh = $group->print_xyz("hackagroup.xyz"); #returns filehandle
620 $group->print_xyz($fh) foreach (1 .. 9); # boring VMD movie with 10 frames
621  
622 =head1 DESCRIPTION
623  
624 The HackaMol AtomGroupRole class provides core methods and attributes for
625 consuming classes that use groups of atoms. The original implementation of
626 this role relied heavily on attributes, builders, and clearers. Such an approach
627 naturally gives fast lookup tables, but the ability to change atoms and coordinates
628 made the role to difficult. Such an approach may be pursued again (without changing
629 the API) in the future after the API has matured. The AtomGroupRole calculates all
630 values for atoms using their own t attributes.
631  
632 =head1 METHODS
633  
634 =head2 do_for_all
635  
636 pass method and arguments down to atoms in group
637  
638 $group->do_for_all('t',1); #sets t to 1 for all atoms
639  
640 =head2 gt
641  
642 integer argument. wraps do_for_all for setting time within group
643  
644 $group->gt(1);
645  
646 =head2 dipole
647  
648 no arguments. return dipole calculated from charges and coordinates as Math::Vector::Real object
649  
650 =head2 COM
651  
652 no arguments. return center of mass calculated from masses and coordinates as Math::Vector::Real object
653  
654 =head2 COZ
655  
656 no arguments. return center of nuclear charge calculated from Zs and coordinates as Math::Vector::Real object
657  
658 =head2 total_charge
659  
660 no arguments. return sum of atom charges.
661  
662 =head2 total_mass
663  
664 no arguments. return sum of atom masses.
665  
666 =head2 total_Z
667  
668 no arguments. return sum of Zs.
669  
670 =head2 dipole_moment
671  
672 no arguments. returns the norm of the dipole in debye (assuming charges in electrons, AKMA)
673  
674 =head2 bin_atoms
675  
676 Called with no arguments. Returns a hash with a count for each unique atom symbol.
677  
678 =head2 count_unique_atoms
679  
680 no arguments. returns the number of unique atoms
681  
682 =head2 bin_atoms_name
683  
684 no arguments. returns a string summary of the atoms in the group sorted by decreasing atomic number. For example; OH2 for water or O2H2 for peroxide.
685  
686 =head2 tmax
687  
688 return (count_coords-1) if > 0; return 0 otherwise; croaks if not all atoms share the same tmax.
689  
690 =head2 translate
691  
692 requires L<Math::Vector::Real> vector argument. Optional argument: integer tf.
693  
694 Translates all atoms in group by the MVR vector. Pass tf to the translate
695 method to store new coordinates in tf rather than atom->t.
696  
697 =head2 rotate
698  
699 requires Math::Vector::Real vector, an angle (in degrees), and a MVR vector
700 origin as arguments. Optional argument: integer tf.
701  
702 Rotates all atoms in the group around the MVR vector. Pass tf to the translate
703 method to store new coordinates in tf rather than atom->t.
704  
705 =head2 print_xyz
706  
707 optional argument: filename or filehandle. with no argument, prints xyz formatted output to STDOUT. pass
708 a filename and an xyz file with that name will be written or overwritten (with warning). pass filehandle
709 for continuous writing to an open filehandle.
710  
711 =head2 print_xyz_ts
712  
713 argument: array_ref containing the values of t to be used for printing.
714 optional argument: filename or filehandle for writing out to file. For example,
715  
716 $mol->print_xyz_ts([0 .. 3, 8, 4], 'fun.xyz');
717  
718 will write the coordinates for all group atoms at t=0,1,2,3,8,4 to a file, in
719 that order.
720  
721 =head2 print_pdb
722  
723 same as print_xyz, but for pdb formatted output
724  
725 =head2 print_pdb_ts
726  
727 same as print_xyz_ts, but for pdb formatted output
728  
729 =head2 bin_this
730  
731 argument: Str , return hash_ref of binned $self->Str.
732  
733 $hash_ref{$_}++ foreach ( map {$_->$Str} $self->all_atoms );
734  
735 =head2 what_time
736  
737 returns the current setting of t by checking against all members of group.
738  
739 =head2 fix_serial
740  
741 argument, optional: Int, offset for resetting the serial number of atoms.
742 Returns the offset.
743  
744 $group->fix_serial(0); # serial starts from zero
745  
746 =head2 centered_vector
747  
748 calculates least squares fitted vector for the AtomGroup. Returns normalized Math::Vector::Real
749 object with origin V(0,0,0).
750  
751 $mvr = $group->centered_vector; # unit vector origin 0,0,0
752 # place two mercury atoms along the vector to visualize the fit
753 my $hg_1 = HackaMol::Atom->new(Z => 80, coords => [$group->center]);
754 my $hg_2 = HackaMol::Atom->new(Z => 80, coords => [$group->center + $mvr]);
755  
756 =head1 ARRAY METHODS
757  
758 =head2 push_atoms, get_atoms, set_atoms, all_atoms, count_atoms, clear_atoms
759  
760 ARRAY traits for the atoms attribute, respectively: push, get, set, elements, count, clear
761  
762 =head2 push_atoms, unshift_atoms
763  
764 push atom on to the end of the atoms array
765 or
766 unshift_atoms on to the front of the array
767  
768 $group->push_atoms($atom1, $atom2, @otheratoms);
769 $group->unshift_atoms($atom1, $atom2, @otheratoms); # maybe in reverse
770  
771 =head2 all_atoms
772  
773 returns array of all elements in atoms array
774  
775 print $_->symbol, "\n" foreach $group->all_atoms;
776  
777 =head2 get_atoms
778  
779 return element by index from atoms array
780  
781 print $group->get_atoms(1); # returns $atom2 from above
782  
783 =head2 set_atoms
784  
785 set atoms array by index
786  
787 $group->set_atoms(1, $atom1);
788  
789 =head2 count_atoms
790  
791 return number of atoms in group
792  
793 print $group->count_atoms;
794  
795 =head2 clear_atoms
796  
797 clears atoms array
798  
799 =head1 ATTRIBUTES
800  
801 =head2 atoms
802  
803 isa ArrayRef[Atom] that is lazy with public ARRAY traits described in ARRAY_METHODS
804  
805 =head2 qcat_print
806  
807 isa Bool that has a lazy default value of 0. if qcat_print, print all atoms coordinates in one go (no model breaks)
808  
809 =head1 SEE ALSO
810  
811 =over 4
812  
813 =item *
814  
815 L<HackaMol::AtomGroup>
816  
817 =item *
818  
819 L<HackaMol::Bond>
820  
821 =item *
822  
823 L<HackaMol::Angle>
824  
825 =item *
826  
827 L<HackaMol::Dihedral>
828  
829 =back
830  
831 =head1 AUTHOR
832  
833 Demian Riccardi <demianriccardi@gmail.com>
834  
835 =head1 COPYRIGHT AND LICENSE
836  
837 This software is copyright (c) 2017 by Demian Riccardi.
838  
839 This is free software; you can redistribute it and/or modify it under
840 the same terms as the Perl 5 programming language system itself.
841  
842 =cut
« HackaMol/Roles/ReadZmatRole.pm ↑ Summary HackaMol/Molecule.pm »