| 1 | package HackaMol::Roles::ReadZmatRole; | ||||||||||
| 2 | 11 | $HackaMol::Roles::ReadZmatRole::VERSION = '0.053'; | |||||||||
| 3 | # ABSTRACT: Read files with molecular information | ||||||||||
| 4 | 11 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 11 | ▶ | use HackaMol::PeriodicTable qw(%KNOWN_NAMES _trim); | ||||||||
|
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 Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 11 | ▶ | use List::MoreUtils qw(singleton); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 11 | with qw( | |||||||||
| 11 | HackaMol::Roles::NERFRole | ||||||||||
| 12 | ); | ||||||||||
| 13 | |||||||||||
| 14 | 0 | ▶ | sub read_zmat_atoms { | ||||||||
|
Subroutine
read_zmat_atoms: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||
| 15 | |||||||||||
| 16 | #xyz file and generate list of Atom object | ||||||||||
| 17 | 0 | my $self = shift; | |||||||||
| 18 | 0 | my $fh = shift; | |||||||||
| 19 | # my $file = shift; | ||||||||||
| 20 | # my $fh = FileHandle->new("<$file") or croak "unable to open $file"; | ||||||||||
| 21 | |||||||||||
| 22 | 0 | my @atoms; | |||||||||
| 23 | 0 | my ( $n, $t ) = ( 0, 0 ); | |||||||||
| 24 | |||||||||||
| 25 | 0 | my @zmat = <$fh>; | |||||||||
| 26 | 0 | @zmat = _substitute_variables(@zmat); | |||||||||
| 27 | |||||||||||
| 28 | # we have 5 types of extensions | ||||||||||
| 29 | # A. SYM 0 x y z | ||||||||||
| 30 | # B. SYM | ||||||||||
| 31 | # C. SYM i R | ||||||||||
| 32 | # D. SYM i R j Ang | ||||||||||
| 33 | # E. SYM i R j Ang k Tors | ||||||||||
| 34 | # we need to filter the indices (can't lose the location) | ||||||||||
| 35 | |||||||||||
| 36 | #type A | ||||||||||
| 37 | 0 | my @iA = grep { $zmat[$_] =~ m/^\s*\w+\s+0(\s+-*\d*\.*\d*){3}/ } 0 .. $#zmat; | |||||||||
| 38 | 0 | my @inA = singleton( 0 .. $#zmat, @iA ); | |||||||||
| 39 | |||||||||||
| 40 | #type B | ||||||||||
| 41 | 0 | my @iB = grep { $zmat[$_] =~ m/^\s*\w+\s*$/ } @inA; | |||||||||
| 42 | |||||||||||
| 43 | #type C | ||||||||||
| 44 | 0 | my @iC = grep { $zmat[$_] =~ m/^\s*\w+(\s+\d+\s+\d*\.*\d*)\s*$/ } @inA; | |||||||||
| 45 | |||||||||||
| 46 | #type D | ||||||||||
| 47 | 0 | my @iD = grep { $zmat[$_] =~ m/^\s*\w+(\s+\d+\s+\d*\.*\d*){2}\s*$/ } @inA; | |||||||||
| 48 | |||||||||||
| 49 | #type E | ||||||||||
| 50 | my @iE = grep { | ||||||||||
| 51 | 0 | $zmat[$_] =~ m/^\s*\w+(\s+\d+\s+\d*\.*\d*){2}\s+\d+\s+-*\d*\.*\d*\s*$/ | |||||||||
| 52 | } @inA; | ||||||||||
| 53 | |||||||||||
| 54 | 0 | my $diff = @zmat - (@iA+@iB+@iC+@iD+@iE); #scalar context | |||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | if ($diff){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | print "Lines in Z-matrix: ", scalar (@zmat), " Number of lines to be processed: ", scalar (@zmat) - $diff, "\n"; | |||||||||
| 58 | 0 | print "Lines missed: ", $diff, "\n"; | |||||||||
| 59 | 0 | print "\n\nHere is your Z-matrix:\n"; | |||||||||
| 60 | 0 | print $_ foreach @zmat; | |||||||||
| 61 | 0 | print "Indices of lines to be processed: ", join("\n", @iA, @iB, @iC, @iD, @iE); | |||||||||
| 62 | 0 | croak "\nThere is something funky with your zmatrix"; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | foreach my $ia (@iA) { | |||||||||
| 66 | 0 | my ( $sym, $iat1, @xyz ) = split( ' ', $zmat[$ia] ); | |||||||||
| 67 | 0 | $atoms[$ia] = HackaMol::Atom->new( | |||||||||
| 68 | name => $sym.$ia, | ||||||||||
| 69 | symbol => $sym, | ||||||||||
| 70 | coords => [ V(@xyz) ] | ||||||||||
| 71 | ); | ||||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 0 | foreach my $ib (@iB) { | |||||||||
| 75 | 0 | my $sym = $zmat[$ib]; | |||||||||
| 76 | 0 | my $a = $self->init; | |||||||||
| 77 | 0 | $sym =~ s/^\s+|\s+$//; | |||||||||
| 78 | 0 | $atoms[$ib] = HackaMol::Atom->new( | |||||||||
| 79 | name => $sym.$ib, | ||||||||||
| 80 | symbol => $sym, | ||||||||||
| 81 | coords => [$a] | ||||||||||
| 82 | ); | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | # print Dump 'B', \@atoms; | ||||||||||
| 86 | |||||||||||
| 87 | 0 | foreach my $ic (@iC) { | |||||||||
| 88 | 0 | my ( $sym, $iat1, $R ) = split( ' ', $zmat[$ic] ); | |||||||||
| 89 | 0 | my $a = $atoms[ $iat1 - 1 ]->xyz; | |||||||||
| 90 | 0 | my $b = $self->extend_a( $a, $R ); | |||||||||
| 91 | 0 | $atoms[$ic] = HackaMol::Atom->new( | |||||||||
| 92 | name => $sym.$ic, | ||||||||||
| 93 | symbol => $sym, | ||||||||||
| 94 | coords => [$b] | ||||||||||
| 95 | ); | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | # print Dump 'C', \@atoms; | ||||||||||
| 99 | |||||||||||
| 100 | 0 | foreach my $id (@iD) { | |||||||||
| 101 | 0 | my ( $sym, $iat1, $R, $iat2, $ang ) = split( ' ', $zmat[$id] ); | |||||||||
| 102 | 0 | my $a = $atoms[ $iat1 - 1 ]->xyz; | |||||||||
| 103 | 0 | my $b = $atoms[ $iat2 - 1 ]->xyz; | |||||||||
| 104 | 0 | my $c = $self->extend_ab( $b, $a, $R, $ang ); | |||||||||
| 105 | 0 | $atoms[$id] = HackaMol::Atom->new( | |||||||||
| 106 | name => $sym.$id, | ||||||||||
| 107 | symbol => _trim($sym), | ||||||||||
| 108 | coords => [$c] | ||||||||||
| 109 | ); | ||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | # print Dump 'D', \@atoms; | ||||||||||
| 113 | |||||||||||
| 114 | 0 | foreach my $ie (@iE) { | |||||||||
| 115 | 0 | my ( $sym, $iat1, $R, $iat2, $ang, $iat3, $tor ) = | |||||||||
| 116 | split( ' ', $zmat[$ie] ); | ||||||||||
| 117 | 0 | my $a = $atoms[ $iat1 - 1 ]->xyz; | |||||||||
| 118 | 0 | my $b = $atoms[ $iat2 - 1 ]->xyz; | |||||||||
| 119 | 0 | my $c = $atoms[ $iat3 - 1 ]->xyz; | |||||||||
| 120 | 0 | my $d = $self->extend_abc( $c, $b, $a, $R, $ang, $tor ); | |||||||||
| 121 | 0 | $atoms[$ie] = HackaMol::Atom->new( | |||||||||
| 122 | name => $sym.$ie, | ||||||||||
| 123 | symbol => _trim($sym), | ||||||||||
| 124 | coords => [$d] | ||||||||||
| 125 | ); | ||||||||||
| 126 | } | ||||||||||
| 127 | 0 | $atoms[$_]->iatom($_) foreach ( 0 .. $#atoms ); | |||||||||
| 128 | 0 | return (\@atoms); | |||||||||
| 129 | |||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 0 | ▶ | sub _substitute_variables{ | ||||||||
|
Subroutine
_substitute_variables: not called CC=5 SCAR=34.0
| |||||||||||
| 133 | 0 | my @Zmat = @_; | |||||||||
| 134 | |||||||||||
| 135 | 0 | chomp @Zmat; | |||||||||
| 136 | |||||||||||
| 137 | 0 | my %bin; | |||||||||
| 138 | my %var = map { | ||||||||||
| 139 | 0 | my ($key,$val) = map{ s/^\s+|\s+$//; $_ } split(/\s*=\s*/,$_); | |||||||||
| 140 | 0 | $bin{$key}++; | |||||||||
| 141 | 0 | $key => $val, | |||||||||
| 142 | 0 | } grep {/=/} @Zmat; | |||||||||
| 143 | |||||||||||
| 144 | # check for double entry of variables | ||||||||||
| 145 | 0 | my @too_many = grep {$bin{$_}>1} keys(%bin); | |||||||||
| 146 | 0 | ▶ | if (@too_many) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 0 | carp "ReadZMatRole> you have more than one entry for these variables: ". join("\n", @too_many); | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 0 | @Zmat = grep {!/(^\#)|=|(^\s*$)/} @Zmat; | |||||||||
| 151 | |||||||||||
| 152 | 0 | foreach my $line (@Zmat){ | |||||||||
| 153 | 0 | my @vals = split (/ /, $line); | |||||||||
| 154 | 0 | ▶ | next unless @vals > 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | 0 | ▶ | $line = join(' ', $vals[0], map{ exists($var{$_}) ? $var{$_} : $_ } @vals[1 .. $#vals] ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | } | ||||||||||
| 157 | 0 | return (@Zmat); | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | 11 | ▶ | no Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 161 | |||||||||||
| 162 | 11 | 1; | |||||||||
| 163 | |||||||||||
| 164 | __END__ | ||||||||||