blib/lib/Chemistry/File/Mopac.pm

filter: Statementstmt 23.5% 29 / 123 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 47.3% 9 / 19 Podpod 44.4% 4 / 9 totaltotal 17.2% 42 / 244 CC 41 SCAR 44.8
CC
41
Cov
15%
parse_string
240.0
format_line_ic
82.9
read_internal_coords
56.0
CRAP 88.4
« blib/lib/Chemistry/File/SMILES.pm ↑ Summary blib/lib/Chemistry/File/SLN.pm »
1 package Chemistry::File::Mopac;
2  
3 9 $VERSION = '0.15';
4 # $Id: Mopac.pm,v 1.5 2004/07/02 18:18:23 itubert Exp $
5  
6 9 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use base "Chemistry::File";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Chemistry::Mol 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Chemistry::InternalCoords;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Chemistry::File::Mopac - MOPAC 6 input file reader/writer
18  
19 =head1 SYNOPSIS
20  
21 use Chemistry::File::Mopac;
22  
23 # read a MOPAC file
24 my $mol = Chemistry::Mol->read('file.mop');
25  
26 # write a MOPAC file using cartesian coordinates
27 $mol->write('file.mop', coords => 'cartesian');
28  
29 # now with internal coordinates
30 $mol->write('file.mop', coords => 'internal');
31  
32 # rebuild the Z-matrix from scratch while we are at it
33 $mol->write('file.mop', rebuild => 1);
34  
35 =cut
36  
37 =head1 DESCRIPTION
38  
39 This module reads and writes MOPAC 6 input files. It can handle both internal
40 coordinates and cartesian coordinates. It also extracts molecules from summary
41 files, defined as those files that match /SUMMARY OF/ in the third line.
42 Perhaps a future version will extract additional information such as the energy
43 and dipole from the summary file.
44  
45 This module registers the C<mop> format with Chemistry::Mol. For detection
46 purposes, it assumes that filenames ending in .mop or .zt have the Mopac
47 format, as well as files whose first line matches /am1|pm3|mndo|mdg|pdg/i
48 (this may change in the future).
49  
50 When the module reads an input file into $mol, it puts the keywords (usually
51 the first line of the file) in $mol->attr("mopac/keywords"), the comments
52 (usually everything else on the first three lines) in
53 $mol->attr("mopac/comments") and $mol->name, and the internal coordinates for
54 each atom in $atom->internal_coords.
55  
56 When writing, the kind of coordinates used depend on the C<coords> option, as
57 shown in the SYNOPSIS. Internal coordinates are used by default. If the
58 molecule has no internal coordinates defined or the rebuild option is set,
59 the build_zmat function from Chemistry::InternalCoords::Builder is used to
60 renumber the atoms and build the Z-matrix from scratch.
61  
62 =cut
63  
64 9 Chemistry::Mol->register_format("mop");
65  
66 0 ▶sub parse_string {
Subroutine parse_string: not called CC=15 SCAR=54.8
Pod documented
67 0 my ($class, $string, %opts) = @_;
68  
69 0 ▶ my $mol_class = $opts{mol_class} || "Chemistry::Mol";
Condition0%0/2
$opts{'mol_class'} || 'Chemistry::Mol'
l!l
00
MC/DC0%0/1
$opts{'mol_class'} || 'Chemistry::Mol'
$opts{'mol_class'}
Truth table0%0/2
A
A= $opts{'mol_class'}
Aresult
11
00
70 0 ▶ my $atom_class = $opts{atom_class} || $mol_class->atom_class;
Condition0%0/3
$opts{'atom_class'} || $mol_class->atom_class
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'atom_class'} || $mol_class->atom_class
$opts{'atom_class'} $mol_class->atom_class
Truth table0%0/3
A || B
A= $opts{'atom_class'}B= $mol_class->atom_class
ABresult
1X1
011
000
71 0 ▶ my $bond_class = $opts{bond_class} || $mol_class->bond_class;
Condition0%0/3
$opts{'bond_class'} || $mol_class->bond_class
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'bond_class'} || $mol_class->bond_class
$opts{'bond_class'} $mol_class->bond_class
Truth table0%0/3
A || B
A= $opts{'bond_class'}B= $mol_class->bond_class
ABresult
1X1
011
000
72  
73 0 my $mol = $mol_class->new();
74  
75 0 my @lines = split "\n", $string;
76 0 local $_;
77  
78 # do we have a summary file?
79 0 ▶ if ($lines[2] =~ /SUMMARY OF/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[2] =~ /SUMMARY OF/) 0 0 0
80 # skip everything until FINAL GEOMETRY OBTAINED
81 0 ▶ while (defined ($_ = shift @lines)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = shift @lines)) 0 0 0
82 0 ▶ last if /FINAL GEOMETRY OBTAINED/;
Branch: 1 branch
BranchTrueFalseTotal
if /FINAL GEOMETRY OBTAINED/ 0 0 0
83 }
84 }
85  
86 0 my @header = splice @lines, 0, 3;
87 0 my @keys;
88  
89 # crazy mopac header extension rules
90 0 push @keys, $header[0];
91 0 ▶ if ($keys[0] =~ /&/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($keys[0] =~ /&/) { } 0 0 0
elsif ($keys[0] =~ /(\+\s|\s\+)/) { } 0 0 0
92 0 push @keys, $header[1];
93 0 ▶ if ($keys[1] =~ /&/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keys[1] =~ /&/) 0 0 0
94 0 push @keys, $header[2];
95 }
96 } elsif ($keys[0] =~ /(\+\s|\s\+)/) {
97 0 push @keys, $header[1];
98 0 push @header, shift @lines;
99 0 ▶ if ($keys[1] =~ /(\+\s|\s\+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keys[1] =~ /(\+\s|\s\+)/) 0 0 0
100 0 push @keys, $header[2];
101 0 push @header, shift @lines;
102 }
103 }
104  
105 0 $mol->attr("mop/keywords" => join "\n", @keys);
106 0 my $comment = join "\n", @header[@keys..$#header];
107 0 $comment =~ s/\s+$//;
108 0 $mol->attr("mopac/comments" => $comment);
109 0 $comment =~ s/\n/ /g;
110 0 $mol->name($comment);
111
112 # read coords
113 0 my @coords;
114 0 for (@lines) {
115 # Sample line below
116 # O 1.232010 1 128.812332 1 274.372818 1 3 2 1
117 0 ▶ last if /^\s*$/; #blank line
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
118 0 push @coords, [split];
119 }
120
121 # note: according to the MOPAC6 manual, triatomics must always use
122 # internal coordinates; molecules that don't specify connectivity
123 # (columns 7-9) use cartesian coordinates. I use column 8 for testing
124 # because column 7 is sometimes used for the partial charge, adding to
125 # the confusion. I assume that diatomics are always internal as well.
126 0 ▶ if (@coords <= 3 or first {defined $_->[8]} @coords) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (@coords <= 3 or &first(sub { defined $_->[8]; } , @coords)) { } 0 0 0
Condition0%0/3
@coords <= 3 or &first(sub { defined $_->[8]; } , @coords)
l!l&&r!l&&!r
000
MC/DC0%0/2
@coords <= 3 or &first(sub { defined $_->[8]; } , @coords)
@coords <= 3 &first(sub { defined $_->[8]; } , @coords)
Truth table0%0/3
A or B
A= @coords <= 3B= &first(sub { defined $_->[8]; } , @coords)
ABresult
1X1
011
000
127 0 read_internal_coords($mol, @coords);
128 } else { # Cartesian coords
129 0 read_cartesian_coords($mol, @coords);
130 }
131  
132 0 return $mol;
133 }
134  
135 0 ▶sub read_internal_coords {
Subroutine read_internal_coords: not called CC=7 SCAR=40.3
Pod undocumented
136 0 my ($mol, @coords) = @_;
137  
138 0 my $i = 0; # atom index
139  
140 0 for my $coord (@coords) {
141 0 $i++;
142 0 my ($symbol, $len_val, $len_opt, $ang_val, $ang_opt,
143 $dih_val, $dih_opt, $len_ref, $ang_ref, $dih_ref) = @$coord;
144  
145 # implicit links for the second and third atoms
146 0 ▶ $len_ref ||= 1 if $i == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 2 0 0 0
Condition0%0/2
$len_ref ||= 1
l!l
00
MC/DC0%0/1
$len_ref ||= 1
$len_ref
Truth table0%0/2
A
A= $len_ref
Aresult
11
00
147 0 ▶ if ($i == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 3) 0 0 0
148 0 ▶ $len_ref ||= 2;
Condition0%0/2
$len_ref ||= 2
l!l
00
MC/DC0%0/1
$len_ref ||= 2
$len_ref
Truth table0%0/2
A
A= $len_ref
Aresult
11
00
149 0 ▶ $ang_ref = $len_ref == 1 ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$len_ref == 1 ? : 0 0 0
150 }
151  
152 0 my $atom = $mol->new_atom(symbol => $symbol);
153 0 my $ic = Chemistry::InternalCoords->new($atom,
154 $len_ref, $len_val,
155 $ang_ref, $ang_val,
156 $dih_ref, $dih_val);
157 0 $atom->internal_coords($ic);
158 0 $ic->add_cartesians;
159 }
160 }
161  
162 0 ▶sub read_cartesian_coords {
Subroutine read_cartesian_coords: not called CC=2 SCAR=17.9
Pod undocumented
163 0 my ($mol, @coords) = @_;
164 0 for my $coord (@coords) {
165 0 my ($symbol, $x_val, $x_opt, $y_val, $y_opt,
166 $z_val, $z_opt) = @$coord;
167 0 my $atom = $mol->new_atom(symbol => $symbol,
168 coords => [$x_val, $y_val, $z_val]);
169 }
170 }
171  
172  
173 0 ▶sub file_is {
Subroutine file_is: not called CC=4 SCAR=30.0
Pod documented
174 0 my ($class, $fname) = @_;
175
176 0 ▶ return 1 if $fname =~ /\.(?:mop|zt)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $fname =~ /\.(?:mop|zt)$/i 0 0 0
177  
178 0 ▶ open F, $fname or croak "Could not open file $fname";
Branch: 1 branch
BranchTrueFalseTotal
open F, $fname or croak("Could not open file $fname") 0 0 0
179
180 0 my $line = <F>;
181 0 close F;
182 0 ▶ return 1 if $line =~ /am1|pm3|mndo|mdg|pdg/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /am1|pm3|mndo|mdg|pdg/i 0 0 0
183 0 return 0;
184 }
185  
186 0 ▶sub name_is {
Subroutine name_is: not called CC=1 SCAR=6.9
Pod documented
187 0 my ($class, $fname) = @_;
188 0 $fname =~ /\.(?:mop|zt)$/i;
189 }
190  
191 0 ▶sub write_string {
Subroutine write_string: not called CC=6 SCAR=37.4
Pod documented
192 0 my ($class, $mol, %opts) = @_;
193 0 %opts = (coords => "internal", rebuild => 0, %opts);
194 0 my $ret = $class->format_header($mol);
195 0 ▶ if ($opts{coords} eq 'cartesian') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'coords'} eq 'cartesian') { } 0 0 0
196 0 $ret .= $class->format_line_cart($_) for $mol->atoms;
197 } else {
198 0 ▶ if ($opts{rebuild} or ! $mol->atoms(1)->internal_coords ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"rebuild"} or not $mol->atoms(1)->internal_coords) 0 0 0
Condition0%0/3
$opts{"rebuild"} or not $mol->atoms(1)->internal_coords
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"rebuild"} or not $mol->atoms(1)->internal_coords
$opts{"rebuild"} not $mol->atoms(1)->internal_coords
Truth table0%0/3
A or not(B)
A= $opts{"rebuild"}B= not $mol->atoms(1)->internal_coords
ABresult
1X1
011
000
199 0 require Chemistry::InternalCoords::Builder;
200 0 Chemistry::InternalCoords::Builder::build_zmat($mol);
201 }
202 0 my %index;
203 0 @index{$mol->atoms} = (1 .. $mol->atoms);
204 0 $ret .= $class->format_line_ic($_, \%index) for $mol->atoms;
205 }
206 0 $ret;
207 }
208  
209  
210 0 ▶sub format_header {
Subroutine format_header: not called CC=3 SCAR=24.8
Pod undocumented
211 0 my ($class, $mol) = @_;
212 0 ▶ my $ret = ($mol->attr("mop/keywords") || '') . "\n";
Condition0%0/2
$mol->attr('mop/keywords') || ''
l!l
00
MC/DC0%0/1
$mol->attr('mop/keywords') || ''
$mol->attr('mop/keywords')
Truth table0%0/2
A
A= $mol->attr('mop/keywords')
Aresult
11
00
213 0 ▶ my $name = $mol->name || '';
Condition0%0/2
$mol->name || ''
l!l
00
MC/DC0%0/1
$mol->name || ''
$mol->name
Truth table0%0/2
A
A= $mol->name
Aresult
11
00
214 0 $name =~ s/\n/ /g;
215 0 $name = substr $name, 0, 80;
216 0 $ret .= "\n" . $name . "\n";
217 0 $ret;
218 }
219  
220 0 ▶sub format_line_ic {
Subroutine format_line_ic: not called CC=10 SCAR=44.2
Pod undocumented
221 0 my ($class, $atom, $index) = @_;
222 0 my $ic = $atom->internal_coords;
223 0 my ($len_ref, $len_val) = $ic->distance;
224 0 my ($ang_ref, $ang_val) = $ic->angle;
225 0 my ($dih_ref, $dih_val) = $ic->dihedral;
226 0 ▶ my $len_idx = $index->{$len_ref||0} || 0;
Condition0%0/4
$$index{$len_ref or 0} || 0
l!l
00
$len_ref || 0
l!l
00
MC/DC0%0/1
$$index{$len_ref or 0} || 0
$$index{$len_ref or 0}
MC/DC0%0/1
$len_ref || 0
$len_ref
Truth table0%0/2
A
A= $$index{$len_ref or 0}
Aresult
11
00
Truth table0%0/2
A
A= $len_ref
Aresult
11
00
227 0 ▶ my $ang_idx = $index->{$ang_ref||0} || 0;
Condition0%0/4
$$index{$ang_ref or 0} || 0
l!l
00
$ang_ref || 0
l!l
00
MC/DC0%0/1
$$index{$ang_ref or 0} || 0
$$index{$ang_ref or 0}
MC/DC0%0/1
$ang_ref || 0
$ang_ref
Truth table0%0/2
A
A= $$index{$ang_ref or 0}
Aresult
11
00
Truth table0%0/2
A
A= $ang_ref
Aresult
11
00
228 0 ▶ my $dih_idx = $index->{$dih_ref||0} || 0;
Condition0%0/4
$$index{$dih_ref or 0} || 0
l!l
00
$dih_ref || 0
l!l
00
MC/DC0%0/1
$$index{$dih_ref or 0} || 0
$$index{$dih_ref or 0}
MC/DC0%0/1
$dih_ref || 0
$dih_ref
Truth table0%0/2
A
A= $$index{$dih_ref or 0}
Aresult
11
00
Truth table0%0/2
A
A= $dih_ref
Aresult
11
00
229 9 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
230 0 ▶ sprintf "%-2s %8.3f %1d %8.3f %1d %8.3f %1d %3d %3d %3d\n",
Branch: 3 branches
BranchTrueFalseTotal
$len_idx ? : 0 0 0
$ang_idx ? : 0 0 0
$dih_idx ? : 0 0 0
231 $atom->symbol,
232 $len_val, $len_idx ? 1 : 0,
233 $ang_val, $ang_idx ? 1 : 0,
234 $dih_val, $dih_idx ? 1 : 0,
235 $len_idx, $ang_idx, $dih_idx ;
236 }
237  
238 0 ▶sub format_line_cart {
Subroutine format_line_cart: not called CC=1 SCAR=6.9
Pod undocumented
239 0 my ($class, $atom) = @_;
240 0 my ($x, $y, $z) = $atom->coords->array;
241 0 sprintf "%-2s %8.3f 1 %8.3f 1 %8.3f 1\n",
242 $atom->symbol,
243 $atom->coords->array;
244 }
245  
246 9 1;
247  
248 =head1 TO DO
249  
250 When writing a Mopac file, this version marks all coordinates as variable
251 (for the purpose of geometry optimization by Mopac). A future version should
252 have more flexibility.
253  
254 =head1 VERSION
255  
256 0.15
257  
258 =head1 SEE ALSO
259  
260 L<Chemistry::Mol>, L<Chemistry::File>, L<Chemistry::InternalCoords>,
261 L<Chemistry::InternalCoords::Builder>, L<http://www.perlmol.org/>.
262  
263 =head1 AUTHOR
264  
265 Ivan Tubert-Brohman <itub@cpan.org>
266  
267 =head1 COPYRIGHT
268  
269 Copyright (c) 2004 Ivan Tubert. All rights reserved. This program is free
270 software; you can redistribute it and/or modify it under the same terms as
271 Perl itself.
272  
273 =cut
274  
« blib/lib/Chemistry/File/SMILES.pm ↑ Summary blib/lib/Chemistry/File/SLN.pm »