HackaMol/Roles/ReadXyzRole.pm

filter: Statementstmt 100% 44 / 44 Branchbran 100% 20 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 100% 71 / 71 SCAR 24.8
CC
12
Cov
100%
read_xyz_atoms
12.0
BEGIN
1.0
BEGIN
1.0
CRAP 12.0
« HackaMol/Roles/FileFetchRole.pm ↑ Summary HackaMol/Atom.pm »
1 package HackaMol::Roles::ReadXyzRole;
2 11 $HackaMol::Roles::ReadXyzRole::VERSION = '0.051';
3 # ABSTRACT: Read files with molecular information
4 11 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Math::Vector::Real;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub read_xyz_atoms {
10  
11 #read xyz file and generate list of Atom objects
12 13 ▶ my $self = shift;
Subroutine read_xyz_atoms: called CC=12 SCAR=24.8
Pod documented
13 13 my $fh = shift;
14 # my $file = shift;
15 # my $fh = FileHandle->new("<$file") or croak "unable to open $file";
16  
17 13 my @atoms;
18 13 my ( $n, $t ) = ( 0, 0 );
19  
20 13 my $nat = undef;
21 13 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 669 10 679
22  
23 669 ▶ if (/^(\s*\d+\s*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^(\s*\d+\s*)$/) { } 16 653 669
elsif (/(\w+|\d+)(\s+-*\d+\.\d+){3}/) { } 638 15 653
24 16 $n = (split)[0];
25 16 ▶ if ( defined($nat) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nat) 5 11 16
26 5 ▶ croak "number of atoms has changed\n" unless ( $nat == $n );
Branch: 1 branch
BranchTrueFalseTotal
unless $nat == $n 1 4 5
27 4 $t++;
28 }
29 15 $nat = $n;
30 15 $n = 0;
31 }
32 elsif (/(\w+|\d+)(\s+-*\d+\.\d+){3}/) {
33 638 my @stuff = split;
34 638 my $sym = $stuff[0];
35 638 my $xyz = V( @stuff[ 1, 2, 3 ] );
36 638 ▶ if ( $t == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 0) { } 624 14 638
37 624 ▶ if ( $sym =~ /\d/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sym =~ /\d/) { } 12 612 624
38 12 $atoms[$n] = HackaMol::Atom->new(
39 name => "at$n",
40 Z => $sym,
41 coords => [$xyz]
42 );
43 }
44 else {
45 612 $atoms[$n] = HackaMol::Atom->new(
46 name => "at$n",
47 symbol => $sym,
48 coords => [$xyz]
49 );
50 }
51 }
52 else {
53 14 ▶ if ( $sym =~ /\d/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sym =~ /\d/) { } 10 4 14
54 10 ▶ croak "atoms have changed from last model to current: $t\n"
Branch: 1 branch
BranchTrueFalseTotal
if $sym != $atoms[$n]->Z 1 9 10
55 if ( $sym != $atoms[$n]->Z );
56 }
57 else {
58 4 ▶ croak "atoms have changed from last model to current: $t\n"
Branch: 1 branch
BranchTrueFalseTotal
if $sym ne $atoms[$n]->symbol 1 3 4
59 if ( $sym ne $atoms[$n]->symbol );
60 }
61 12 $atoms[$n]->set_coords( $t, $xyz );
62  
63 }
64 636 $n++;
65 }
66 }
67  
68 # set iatom to track the array. diff from serial which refers to pdb
69 10 $atoms[$_]->iatom($_) foreach ( 0 .. $#atoms );
70 10 return (\@atoms);
71 }
72  
73 11 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 11 1;
76  
77 __END__
78  
79 =pod
80  
81 =head1 NAME
82  
83 HackaMol::Roles::ReadXyzRole - Read files with molecular information
84  
85 =head1 VERSION
86  
87 version 0.051
88  
89 =head1 SYNOPSIS
90  
91 my @atoms = HackaMol->new
92 ->read_xyz_atoms("some.xyz");
93  
94 =head1 DESCRIPTION
95  
96 The HackaMol::Roles::ReadXyzRole provides read_xyz_atoms reading xyz files.
97  
98 =head1 METHODS
99  
100 =head2 read_xyz_atoms
101  
102 One argument: the filename
103 Returns a list of HackaMol::Atom objects.
104  
105 =head1 SEE ALSO
106  
107 =over 4
108  
109 =item *
110  
111 L<HackaMol>
112  
113 =item *
114  
115 L<HackaMol::Atom>
116  
117 =item *
118  
119 L<HackaMol::Roles::MolReadRole>
120  
121 =back
122  
123 =head1 AUTHOR
124  
125 Demian Riccardi <demianriccardi@gmail.com>
126  
127 =head1 COPYRIGHT AND LICENSE
128  
129 This software is copyright (c) 2017 by Demian Riccardi.
130  
131 This is free software; you can redistribute it and/or modify it under
132 the same terms as the Perl 5 programming language system itself.
133  
134 =cut
« HackaMol/Roles/FileFetchRole.pm ↑ Summary HackaMol/Atom.pm »