blib/lib/Chemistry/File/XYZ.pm

filter: Statementstmt 94.3% 50 / 53 Branchbran 72.2% 13 / 18 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 88.8% 8 / 9 Podpod 100% 4 / 4 totaltotal 83.7% 77 / 92 CC 14 SCAR 18.0
CC
14
Cov
86%
parse_string
10.0
write_string
6.0
name_is
2.0
CRAP 6.1
↑ Summary
1 package Chemistry::File::XYZ;
2  
3 1 $VERSION = '0.11';
4 # $Id: XYZ.pm,v 1.2 2004/08/03 00:20:04 itubert Exp $
5  
6 1 ▶use base qw(Chemistry::File);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Chemistry::Mol;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Chemistry::File::XYZ - XYZ molecule format reader/writer
15  
16 =head1 SYNOPSIS
17  
18 use Chemistry::File::XYZ;
19  
20 # read an XYZ file
21 my $mol = Chemistry::Mol->read("myfile.xyz");
22  
23 # write an XYZ file
24 $mol->write("out.xyz");
25  
26 =cut
27  
28 =head1 DESCRIPTION
29  
30 This module reads XYZ files. It automatically registers the 'xyz' format with
31 Chemistry::Mol, so that XYZ files may be identified and read by
32 Chemistry::Mol->read().
33  
34 The XYZ format is not strictly defined and there are various versions floating
35 around; this module accepts the following:
36  
37 First line: atom count (optional)
38  
39 Second line: molecule name or comment (optional)
40  
41 All other lines: (symbol or atomic number), x, y, and z coordinates separated
42 by spaces, tabs, or commas.
43  
44 If the first line doesn't look like a number, the atom count is deduced from
45 the number of lines in the file. If the second line looks like it defines an
46 atom, it is assumed that there was no name or comment.
47  
48 =head1 OUTPUT OPTIONS
49  
50 On writing, the default format is the following, giving H2 as an example.
51  
52 2
53 Hydrogen molecule
54 H 0.0000 0.0000 0.0000
55 H 0.0000 0.7000 0.0000
56  
57 That is: count line, name line, and atom lines (symbol, x, y, z). These format
58 can be modified by means of certain options:
59  
60 =over
61  
62 =item name
63  
64 Control whether or not to include the name.
65  
66 =item count
67  
68 Control whether or not to include the count line.
69  
70 =item symbol
71  
72 If false, use the atomic numbers instead of the atomic symbols.
73  
74 =back
75  
76 For example,
77  
78 $mol->write("out.xyz", count => 0, name => 0, symbol => 0);
79  
80 gives the following output:
81  
82 1 0.0000 0.0000 0.0000
83 1 0.0000 0.7000 0.0000
84  
85 =cut
86  
87 1 Chemistry::Mol->register_format(xyz => __PACKAGE__);
88  
89 1 ▶sub parse_string {
Subroutine parse_string: called CC=9 SCAR=23.0
Pod documented
90 1 my ($class, $s, %opts) = @_;
91  
92 1 ▶ my $mol_class = $opts{mol_class} || 'Chemistry::Mol';
Condition50%1/2
$opts{'mol_class'} || 'Chemistry::Mol'
l!l
10
MC/DC0%0/1
$opts{'mol_class'} || 'Chemistry::Mol'
$opts{'mol_class'}
Truth table50%1/2
A
A= $opts{'mol_class'}
Aresult
11
00
93 1 ▶ my $atom_class = $opts{atom_class} || $mol_class->atom_class;
Condition33%1/3
$opts{'atom_class'} || $mol_class->atom_class
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{'atom_class'} || $mol_class->atom_class
$opts{'atom_class'} $mol_class->atom_class
Truth table33%1/3
A || B
A= $opts{'atom_class'}B= $mol_class->atom_class
ABresult
1X1
011
000
94  
95 1 my @lines = split /(?:\n|\r\n?)/, $s;
96 1 my $n_atoms;
97 1 ▶ if ($lines[0] =~ /^\s*\d+\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[0] =~ /^\s*\d+\s*$/) 1 0 1
98 1 $n_atoms = shift @lines;
99 }
100 1 my $name = '';
101 1 ▶ unless ($lines[0] =~ /^\s*([A-Z][a-z]?|\d{1,3})([,\s]+[eE\d.+-]+){3}/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lines[0] =~ /^\s*([A-Z][a-z]?|\d{1,3})([,\s]+[eE\d.+-]+){3}/) 0 1 1
102 0 $name = shift @lines;
103 }
104 1 ▶ $n_atoms = @lines unless $n_atoms;
Branch: 1 branch
BranchTrueFalseTotal
unless $n_atoms 0 1 1
105  
106 1 my $mol = $mol_class->new(name => $name);
107  
108 1 my $i = 0;
109 1 for my $line (@lines) {
110 10 $i++;
111 10 ▶ last if $i > $n_atoms;
Branch: 1 branch
BranchTrueFalseTotal
if $i > $n_atoms 0 10 10
112 10 $line =~ s/^\s+//;
113 10 my ($elem, $x, $y, $z) = split /[\s,]+/, $line;
114  
115 10 ▶ $mol->new_atom(
Branch: 1 branch
BranchTrueFalseTotal
$elem =~ /^\d+$/ ? : 1 9 10
116 ($elem =~ /^\d+$/ ? "Z" : "symbol") => $elem,
117 coords => [$x, $y, $z],
118 );
119 }
120 1 return $mol;
121 }
122  
123 0 ▶sub name_is {
Subroutine name_is: not called CC=1 SCAR=6.9
Pod documented
124 0 my ($class, $fname) = @_;
125 0 $fname =~ /\.xyz$/i;
126 }
127  
128 1 ▶sub file_is {
Subroutine file_is: called CC=1 SCAR=0.0
Pod documented
129 1 my ($class, $fname) = @_;
130 1 $fname =~ /\.xyz$/i;
131 }
132  
133 2 ▶sub write_string {
Subroutine write_string: called CC=6 SCAR=17.9
Pod documented
134 2 my ($class, $mol, %opts) = @_;
135  
136 2 %opts = (count => 1, name => 1, symbol => 1, %opts);
137  
138 # header
139 2 my $ret = '';
140 2 ▶ $ret .= $mol->atoms . "\n" if $opts{count};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"count"} 1 1 2
141 2 ▶ if ($opts{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"name"}) 1 1 2
142 1 ▶ my $name = defined $mol->name ? $mol->name : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $mol->name ? : 1 0 1
143 1 $name =~ s/\n.*//s;
144 1 $ret .= "$name\n"
145 }
146  
147 # body
148 2 for my $atom ($mol->atoms) {
149 $ret .= sprintf "%-2s %8.4f %8.4f %8.4f\n",
150 20 ▶ $opts{symbol} ? $atom->symbol : $atom->Z,
Branch: 1 branch
BranchTrueFalseTotal
$opts{'symbol'} ? : 10 10 20
151 $atom->coords->array;
152 }
153 2 $ret;
154 }
155  
156 1 1;
157  
158 =head1 VERSION
159  
160 0.11
161  
162 =head1 SEE ALSO
163  
164 L<Chemistry::Mol>, L<http://www.perlmol.org/>.
165  
166 =head1 AUTHOR
167  
168 Ivan Tubert-Brohman <itub@cpan.org>
169  
170 =cut
171  
↑ Summary