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 CC 60 SCAR 13.1
CC
60
Cov
92%
rotate_translate
12.0
rotate
6.0
calc_bfps
5.4
CRAP 3.7
« HackaMol/Roles/MolReadRole.pm ↑ Summary HackaMol/Molecule.pm »
1 package HackaMol::Roles::AtomGroupRole;
2 17 $HackaMol::Roles::AtomGroupRole::VERSION = '0.053';
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 11 ▶sub _lsq_slope {
Subroutine _lsq_slope: called CC=3 SCAR=11.0
63 # private function used for each coordinate
64 # translation of tcl function written by Justin Gullingsrud @ uiuc.edu
65 # algorithm reference: Bevington
66 # https://www.ks.uiuc.edu/Research/vmd/mailing_list/vmd-l/att-2279/fit_angle.tcl
67 # Fit the points x to x = ai + b, i=0...N-1, and return the value of a
68 # a = 12/( (N(N^2 - 1)) ) sum[ (i-(N-1)/2) * xi]
69 11 ▶ my $xis = shift || die "expecting array_ref of cartesian coordinate [x y or z]";
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
70 11 my $n = @$xis;
71 11 my $sum = 0;
72 11 my $d = ($n-1)/2;
73 11 my $i = 0;
74 11 $sum += ($i++ - $d)*$_ foreach @$xis;
75 11 return $sum * 12 / ($n * ( $n * $n - 1)) ;
76 }
77  
78 2 ▶sub centered_vector {
Subroutine centered_vector: called CC=2 SCAR=6.9
Pod documented
79 2 my $self = shift;
80 2 my $centered_flag = shift;
81 2 my @mvrs = map {$_->xyz} $self->all_atoms;
82 2 ▶ die "2 atoms needed for a centered_vector" unless @mvrs > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @mvrs > 1 0 2 2
83 2 my @x = map { $_->[0] } @mvrs;
84 2 my @y = map { $_->[1] } @mvrs;
85 2 my @z = map { $_->[2] } @mvrs;
86 2 my $mvr = V( map { _lsq_slope($_) } \@x,\@y,\@z);
87 2 return $mvr->versor;
88 }
89  
90 2 ▶sub calc_bfps {
Subroutine calc_bfps: called CC=5 SCAR=16.9
Pod undocumented
91  
92 # this should be rerun for each selection
93 #10.1016/j.jmb.2015.09.024
94 2 my $self = shift;
95 2 ▶ unless ( $self->count_atoms > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->count_atoms > 1) 0 2 2
96 0 warn "calc_bfps> group not large enough\n";
97 0 return;
98 }
99 2 my @atoms = $self->all_atoms;
100 2 my @bfacts = map { $_->bfact } @atoms;
101 2 my $bfact_mean = sum(@bfacts) / @bfacts;
102 2 my $sd = 0;
103 2 $sd += ( $_ - $bfact_mean )**2 foreach @bfacts;
104 2 ▶ unless ( $sd > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sd > 0) 0 2 2
105 0 warn "calc_bfps> no variance in the group bfactors\n";
106 0 return;
107 }
108 2 my $bfact_std = sqrt( $sd / ( @bfacts - 1 ) );
109 2 foreach my $atom (@atoms) {
110 6 my $bfp = ( $atom->bfact - $bfact_mean ) / $bfact_std;
111 6 $atom->bfp($bfp);
112 }
113 2 return map { $_->bfp } @atoms;
114 }
115  
116 9 ▶sub dipole {
Subroutine dipole: called CC=4 SCAR=13.9
Pod documented
117 9 my $self = shift;
118 9 ▶ return ( V(0) ) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 8 9
119 8 my @atoms = $self->all_atoms;
120 8 my @vectors = grep { defined } map { $_->get_coords( $_->t ) } @atoms;
121 8 my @charges = grep { defined } map { $_->get_charges( $_->t ) } @atoms;
122 8 my $dipole = V( 0, 0, 0 );
123 8 ▶ if ( $#vectors != $#charges ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#vectors != $#charges) 1 7 8
124 1 carp
125 "build_dipole> mismatch number of coords and charges. all defined?";
126 1 return $dipole;
127 }
128 7 $dipole += $vectors[$_] * $charges[$_] foreach 0 .. $#charges;
129 7 return ($dipole);
130 }
131  
132 40 ▶sub COM {
Subroutine COM: called CC=3 SCAR=11.0
Pod documented
133 40 my $self = shift;
134 40 ▶ return ( V(0) ) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 39 40
135 39 my @atoms = $self->all_atoms;
136 39 my @m_vectors = map { $_->mass * $_->get_coords( $_->t ) } @atoms;
137 39 my $com = V( 0, 0, 0 );
138 39 $com += $_ foreach @m_vectors;
139 39 return ( $com / $self->total_mass );
140 }
141  
142 4 ▶sub center {
Subroutine center: called CC=3 SCAR=11.0
Pod undocumented
143 4 my $self = shift;
144 4 ▶ return ( V(0) ) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 0 4 4
145 4 my @atoms = $self->all_atoms;
146 4 my @vectors = map { $_->get_coords( $_->t ) } @atoms;
147 4 my $center = V( 0, 0, 0 );
148 4 $center += $_ foreach @vectors;
149 4 return ( $center / $self->count_atoms );
150 }
151  
152 6 ▶sub COZ {
Subroutine COZ: called CC=3 SCAR=11.0
Pod documented
153 6 my $self = shift;
154 6 ▶ return ( V(0) ) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 5 6
155 5 my @atoms = $self->all_atoms;
156 5 my @z_vectors = map { $_->Z * $_->get_coords( $_->t ) } @atoms;
157 5 my $coz = V( 0, 0, 0 );
158 5 $coz += $_ foreach @z_vectors;
159 5 return ( $coz / $self->total_Z );
160 }
161  
162 39 ▶sub gt {
Subroutine gt: called CC=1 SCAR=0.0
Pod documented
163  
164 #set group time
165 39 my $self = shift;
166 39 my $t = shift;
167 39 $self->do_forall( 't', $t );
168 }
169  
170 63 ▶sub do_forall {
Subroutine do_forall: called CC=3 SCAR=11.0
Pod undocumented
171 63 my $self = shift;
172 63 my $method = shift;
173 63 ▶ do { carp "doing nothing for all"; return } unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 62 63
174 62 my @atoms = $self->all_atoms;
175 62 $_->$method(@_) foreach @atoms;
176 }
177  
178 5 ▶sub total_charge {
Subroutine total_charge: called CC=3 SCAR=11.0
Pod documented
179 5 my $self = shift;
180 5 ▶ return (0) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 4 5
181 4 my @atoms = $self->all_atoms;
182 4 my @charges = map { $_->get_charges( $_->t ) } @atoms;
183 4 my $sum = 0;
184 4 $sum += $_ foreach @charges;
185 4 return ($sum);
186 }
187  
188 48 ▶sub total_mass {
Subroutine total_mass: called CC=3 SCAR=11.0
Pod documented
189 48 my $self = shift;
190 48 ▶ return (0) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 47 48
191 47 my @masses = map { $_->mass } $self->all_atoms;
192 47 my $sum = 0;
193 47 $sum += $_ foreach @masses;
194 47 return ($sum);
195 }
196  
197 7 ▶sub total_Z {
Subroutine total_Z: called CC=3 SCAR=11.0
Pod documented
198 7 my $self = shift;
199 7 ▶ return (0) unless ( $self->count_atoms );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 1 6 7
200 6 my @Zs = map { $_->Z } $self->all_atoms;
201 6 my $sum = 0;
202 6 $sum += $_ foreach @Zs;
203 6 return ($sum);
204 }
205  
206 6 ▶sub dipole_moment {
Subroutine dipole_moment: called CC=1 SCAR=0.0
Pod documented
207 6 my $self = shift;
208 6 return ( abs( $self->dipole ) * $angste_debye );
209 }
210  
211 13 ▶sub bin_atoms {
Subroutine bin_atoms: called CC=1 SCAR=0.0
Pod documented
212  
213 # Called with no arguments.
214 # Returns a hash with a count of unique atom symbols
215 13 my $self = shift;
216 13 my $bin_hr = $self->bin_this('symbol');
217 13 return ($bin_hr);
218 }
219  
220 5 ▶sub count_unique_atoms {
Subroutine count_unique_atoms: called CC=1 SCAR=0.0
Pod documented
221 5 my $self = shift;
222 5 my $bin_hr = $self->bin_atoms;
223 5 return ( scalar( keys %{$bin_hr} ) );
224 }
225  
226 7 ▶sub bin_atoms_name {
Subroutine bin_atoms_name: called CC=2 SCAR=6.9
Pod documented
227  
228 # return something like C4H10 sort in order of descending Z
229 7 my $self = shift;
230 7 my $bin_hr = $self->bin_atoms;
231 7 my $z_hr;
232 7 $z_hr->{ $_->symbol } = $_->Z foreach $self->all_atoms;
233  
234 my @names = map {
235 9 my $name = $_ . $bin_hr->{$_};
236 9 $name =~ s/(\w+)1$/$1/;
237 9 $name; # substitue 1 away?
238 }
239 sort {
240 2 $z_hr->{$b} <=> $z_hr->{$a} # sort by Z! see above...
241 7 } keys %{$bin_hr};
242 7 return join( '', @names );
243 }
244  
245 5 ▶sub translate {
Subroutine translate: called CC=5 SCAR=16.1
Pod documented
246 5 my $self = shift;
247 5 ▶ my $tvec = shift or croak "pass MVR translation vector";
Branch: 1 branch
BranchTrueFalseTotal
my $tvec = shift() or croak("pass MVR translation vector") 4 1 5
248 4 my $tf = shift;
249  
250 4 my @atoms = $self->all_atoms;
251 4 ▶ do { carp "no atoms to translate"; return } unless (@atoms);
Branch: 1 branch
BranchTrueFalseTotal
unless (@atoms) 1 3 4
252 3 ▶ $tf = $atoms[0]->t unless ( defined($tf) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tf 2 1 3
253  
254 3 foreach my $at (@atoms) {
255 69 my $v = $at->xyz + $tvec;
256 69 $at->set_coords( $tf, $v );
257 }
258 }
259  
260 8 ▶sub rotate {
Subroutine rotate: called CC=6 SCAR=17.9
Pod documented
261  
262 #rotate about origin. having origin allows rotation of subgroup
263 #without having to translate everything.
264 8 my $self = shift;
265 8 ▶ my $rvec = shift or croak "pass MVR rotation vector";
Branch: 1 branch
BranchTrueFalseTotal
my $rvec = shift() or croak("pass MVR rotation vector") 7 1 8
266 7 ▶ my $ang = shift or croak "pass rotation angle";
Branch: 1 branch
BranchTrueFalseTotal
my $ang = shift() or croak("pass rotation angle") 6 1 7
267 6 ▶ my $orig = shift or croak "pass MVR origin";
Branch: 1 branch
BranchTrueFalseTotal
my $orig = shift() or croak("pass MVR origin") 5 1 6
268 5 my $tf = shift;
269  
270 5 my @atoms = $self->all_atoms;
271 5 my $t = $atoms[0]->t;
272 5 ▶ $tf = $t unless ( defined($tf) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tf 1 4 5
273 5 $rvec = $rvec->versor; #unit vector
274  
275 5 my @cor = map { $_->get_coords($t) - $orig } @atoms;
276 5 my @rcor = $rvec->rotate_3d( deg2rad($ang), @cor );
277  
278 5 $atoms[$_]->set_coords( $tf, $rcor[$_] + $orig ) foreach 0 .. $#rcor;
279 }
280  
281 0 ▶sub rotate_translate {
Subroutine rotate_translate: not called CC=3 SCAR=24.8
Pod undocumented
282  
283 # args:
284 # 1. rotation matrix (3x3): ar, each column (cx,cy,cz, below) is a Math::Vector::Real
285 # 2. translate vector, MVR
286 # r' = x*cx + y*cy + z*cz + v_trans
287 # 3. t final, the final t location for transformed coordinates [defaults to current t]
288 0 my $self = shift;
289 0 my $rmat = shift;
290 0 my $trns = shift;
291 0 my $tf = shift;
292 0 my @atoms = $self->all_atoms;
293 0 my ( $cx, $cy, $cz ) = @{$rmat};
294  
295 0 my $t = $atoms[0]->t;
296 0 ▶ $tf = $t unless ( defined($tf) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tf 0 0 0
297  
298 0 foreach my $atom (@atoms) {
299 0 my $xyz = $atom->xyz;
300  
301 #my ($x,$y,$z) = @{$xyz};
302 0 my $xr = $cx * $xyz;
303 0 my $yr = $cy * $xyz;
304 0 my $zr = $cz * $xyz;
305  
306 #my $xyz_new = $x*$cx + $y*$cy + $z*$cz + $trns;
307 0 my $xyz_new = V( $xr, $yr, $zr ) + $trns;
308 0 $atom->set_coords( $tf, $xyz_new );
309 }
310  
311 }
312  
313 3 ▶sub fix_serial {
Subroutine fix_serial: called CC=3 SCAR=11.0
Pod documented
314 3 my @atoms = shift->all_atoms;
315 3 my $offset = shift;
316 3 ▶ $offset = 1 unless defined($offset);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 1 2 3
317 3 $atoms[$_]->{serial} = $_ + $offset foreach ( 0 .. $#atoms );
318 3 return $offset;
319 }
320  
321 4 ▶sub print_xyz_ts {
Subroutine print_xyz_ts: called CC=1 SCAR=0.0
Pod documented
322 4 _print_ts( 'print_xyz', @_ );
323 }
324  
325 2 ▶sub print_pdb_ts {
Subroutine print_pdb_ts: called CC=1 SCAR=0.0
Pod documented
326 2 _print_ts( 'print_pdb', @_ );
327 }
328  
329 6 ▶sub _print_ts {
Subroutine _print_ts: called CC=5 SCAR=16.1
330  
331 #use one sub for xyz_ts and pdb_ts writing
332 6 my $print_method = shift;
333  
334 # two args: \@ts, optional filename
335 6 my $self = shift;
336 6 my $ts = shift;
337 6 ▶ unless ( defined($ts) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ts) 1 5 6
338 1 croak "must pass arrayref containing ts";
339 }
340 5 my @ts = @$ts;
341 5 ▶ unless ( scalar(@ts) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @ts) 1 4 5
342 1 croak "must pass array with atleast one t";
343 }
344 4 my $tmax = $self->tmax;
345 4 my $nt = grep { $_ > $tmax } @ts;
346 4 ▶ croak "$nt ts out of bounds" if $nt;
Branch: 1 branch
BranchTrueFalseTotal
if $nt 2 2 4
347  
348 2 my $tnow = $self->what_time;
349  
350 # take the first out of the loop to setup fh
351 2 $self->gt( shift @ts );
352 2 my $fh = $self->$print_method(@_);
353  
354 2 foreach my $t (@ts) {
355 4 $self->gt($t);
356 4 $fh = $self->$print_method($fh);
357 }
358  
359 # return to original t
360 2 $self->gt($tnow);
361 }
362  
363 34 ▶sub bin_this {
Subroutine bin_this: called CC=4 SCAR=13.9
Pod documented
364  
365 #return hash{$_->method}++
366 34 my $self = shift;
367 34 my $method = shift;
368  
369 34 ▶ return ( {} ) unless $self->count_atoms;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count_atoms 2 32 34
370  
371 32 my @atoms = $self->all_atoms;
372  
373 # just test the first one...
374 32 ▶ croak "Atom does not do $method" unless $atoms[0]->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $atoms[0]->can($method) 0 32 32
375  
376 32 my $bin;
377 32 $bin->{$_}++ foreach ( map { $_->$method } @atoms );
378 32 return ($bin);
379  
380 }
381  
382 12 ▶sub tmax {
Subroutine tmax: called CC=3 SCAR=11.0
Pod documented
383  
384 # still not the best implementation! what about atoms without coords?
385 12 my $self = shift;
386 12 my $tbin = $self->bin_this('count_coords');
387 12 my @ts = keys(%$tbin);
388 12 ▶ croak "tmax differences within group" if ( scalar(@ts) > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ts > 1 1 11 12
389 11 ▶ $ts[0] ? return $ts[0] - 1 : return 0;
Branch: 1 branch
BranchTrueFalseTotal
$ts[0] ? : 10 1 11
390 }
391  
392 9 ▶sub what_time {
Subroutine what_time: called CC=2 SCAR=6.9
Pod documented
393 9 my $self = shift;
394 9 my $tbin = $self->bin_this('t');
395 9 my @ts = keys(%$tbin);
396 9 ▶ carp "what_time> t differences within group!!" if ( scalar(@ts) > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ts > 1 1 8 9
397 9 return $ts[0];
398 }
399  
400 11 ▶sub string_xyz {
Subroutine string_xyz: called CC=4 SCAR=13.9
Pod undocumented
401 11 my $self = shift;
402 11 my $add_info_to_blank = shift;
403  
404 11 my $string;
405 11 ▶ $string .= $self->count_atoms . "\n" unless $self->qcat_print;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->qcat_print 11 0 11
406 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
407 11 $string .= "\n";
408  
409 11 foreach my $at ( $self->all_atoms ) {
410 $string .= sprintf( "%3s %10.6f %10.6f %10.6f\n",
411 936 $at->symbol, @{ $at->get_coords( $at->t ) } );
412 }
413 11 return $string;
414 }
415  
416 10 ▶sub print_xyz {
Subroutine print_xyz: called CC=1 SCAR=0.0
Pod documented
417 10 my $self = shift;
418 10 my $fh = _open_file_unless_fh(shift);
419  
420 9 print $fh $self->string_xyz;
421  
422 # my @atoms = $self->all_atoms;
423 #print $fh $self->count_atoms . "\n\n" unless $self->qcat_print;
424 #foreach my $at (@atoms) {
425 # printf $fh (
426 # "%3s %10.6f %10.6f %10.6f\n",
427 # $at->symbol, @{ $at->get_coords( $at->t ) }
428 # );
429 #}
430  
431 9 return ($fh); # returns filehandle for future writing
432  
433 }
434  
435 6 ▶sub string_pdb {
Subroutine string_pdb: called CC=5 SCAR=16.1
Pod undocumented
436 6 my $self = shift;
437  
438 6 my $t = $self->what_time;
439 6 my @atoms = $self->all_atoms;
440  
441 6 my $string;
442 6 ▶ $string .= sprintf( "MODEL %2i\n", $t + 1 ) unless $self->qcat_print;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->qcat_print 5 1 6
443  
444 6 my $atform =
445 "%-6s%5i %-3s%1s%3s%2s%4s%1s %8.3f%8.3f%8.3f%6.2f%6.2f %4s%2s\n";
446  
447 6 foreach my $at (@atoms) {
448  
449 # front pad one space if name length is < 4
450 18 my $form = $atform;
451 18 ▶ if ( length $at->name > 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $at->name > 3) 0 18 18
452 0 $form =
453 "%-6s%5i %4s%1s%3s%2s%4s%1s %8.3f%8.3f%8.3f%6.2f%6.2f %4s%2s\n";
454 }
455 $string .= sprintf(
456 $form,
457 (
458 144 map { $at->$_ }
459 qw (
460 record_name
461 serial
462 name
463 altloc
464 resname
465 chain
466 resid
467 icode
468 )
469 ),
470 18 @{ $at->get_coords( $at->t ) },
471 $at->occ,
472 $at->bfact,
473 $at->segid,
474 $at->symbol, # $at->charge
475 );
476 }
477 6 ▶ $string .= "ENDMDL\n" unless $self->qcat_print;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->qcat_print 5 1 6
478 6 return $string;
479 }
480  
481 6 ▶sub print_pdb {
Subroutine print_pdb: called CC=1 SCAR=0.0
Pod documented
482 6 my $self = shift;
483 6 my $fh = _open_file_unless_fh(shift);
484  
485 6 print $fh $self->string_pdb;
486  
487 # my @atoms = $self->all_atoms;
488 # printf $fh ( "MODEL %2i\n", $atoms[0]->t + 1 ) unless $self->qcat_print;
489 # my $atform = "%-6s%5i %-3s%1s%3s %1s%4i%1s %8.3f%8.3f%8.3f%6.2f%6.2f %4s%2s\n";
490  
491 # foreach my $at (@atoms) {
492 # # front pad one space if name length is < 4
493 # my $form = $atform;
494 # if (length $at->name > 3){
495 # $form = "%-6s%5i %4s%1s%3s %1s%4i%1s %8.3f%8.3f%8.3f%6.2f%6.2f %4s%2s\n"
496 # }
497 # printf $fh (
498 # $form,
499 # (
500 # map { $at->$_ }
501 # qw (
502 # record_name
503 # serial
504 # name
505 # altloc
506 # resname
507 # chain
508 # resid
509 # icode
510 # )
511 # ),
512 # @{ $at->get_coords( $at->t ) },
513 # $at->occ,
514 # $at->bfact,
515 # $at->segid,
516 # $at->symbol, # $at->charge
517 # );
518  
519 # }
520 # print $fh "ENDMDL\n" unless $self->qcat_print;
521  
522 6 return ($fh); # returns filehandle for future writing
523 }
524  
525 16 ▶sub _open_file_unless_fh {
Subroutine _open_file_unless_fh: called CC=5 SCAR=16.1
526  
527 16 my $file = shift; # could be file or filehandle
528  
529 16 my $fh = \*STDOUT; # default to standard out
530 # if argument is passed, check if filehandle
531 16 ▶ if ( defined($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 9 7 16
532 9 ▶ if ( ref($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 6 3 9
533 6 ▶ if ( reftype($file) eq "GLOB" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($file) eq 'GLOB') { } 5 1 6
534 5 $fh = $file;
535 }
536 else {
537 1 croak "trying write to reference that is not a GLOB";
538 }
539 }
540 else {
541 3 ▶ carp "overwrite $file" if ( -e $file );
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 1 2 3
542 3 $fh = FileHandle->new(">$file");
543 }
544 }
545  
546 15 return ($fh);
547 }
548  
549 17 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
550  
551 17 1;
552  
553 __END__
« HackaMol/Roles/MolReadRole.pm ↑ Summary HackaMol/Molecule.pm »