| 1 | package Chemistry::File::XYZ; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 9 | $VERSION = '0.11'; | |||||||||||||||||||
| 4 | # $Id: XYZ.pm,v 1.2 2004/08/03 00:20:04 itubert Exp $ | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 9 | ▶ | use base qw(Chemistry::File); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 9 | ▶ | use Chemistry::Mol; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 9 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 9 | ▶ | 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 | 9 | Chemistry::Mol->register_format(xyz => __PACKAGE__); | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | ▶ | sub parse_string { | ||||||||||||||||||
|
Subroutine
parse_string: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||
| 90 | 0 | my ($class, $s, %opts) = @_; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | my $mol_class = $opts{mol_class} || 'Chemistry::Mol'; | ||||||||||||||||||
|
Condition0%0/2
$opts{'mol_class'} || 'Chemistry::Mol'
MC/DC0%0/1
$opts{'mol_class'} || 'Chemistry::Mol'
$opts{'mol_class'}
Truth table0%0/2
A
A= $opts{'mol_class'}
| |||||||||||||||||||||
| 93 | 0 | ▶ | my $atom_class = $opts{atom_class} || $mol_class->atom_class; | ||||||||||||||||||
|
Condition0%0/3
$opts{'atom_class'} || $mol_class->atom_class
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
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | my @lines = split /(?:\n|\r\n?)/, $s; | |||||||||||||||||||
| 96 | 0 | my $n_atoms; | |||||||||||||||||||
| 97 | 0 | ▶ | if ($lines[0] =~ /^\s*\d+\s*$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 0 | $n_atoms = shift @lines; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | 0 | my $name = ''; | |||||||||||||||||||
| 101 | 0 | ▶ | unless ($lines[0] =~ /^\s*([A-Z][a-z]?|\d{1,3})([,\s]+[eE\d.+-]+){3}/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 0 | $name = shift @lines; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | 0 | ▶ | $n_atoms = @lines unless $n_atoms; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | my $mol = $mol_class->new(name => $name); | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | my $i = 0; | |||||||||||||||||||
| 109 | 0 | for my $line (@lines) { | |||||||||||||||||||
| 110 | 0 | $i++; | |||||||||||||||||||
| 111 | 0 | ▶ | last if $i > $n_atoms; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 0 | $line =~ s/^\s+//; | |||||||||||||||||||
| 113 | 0 | my ($elem, $x, $y, $z) = split /[\s,]+/, $line; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 0 | ▶ | $mol->new_atom( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | ($elem =~ /^\d+$/ ? "Z" : "symbol") => $elem, | ||||||||||||||||||||
| 117 | coords => [$x, $y, $z], | ||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | 0 | 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 | 0 | ▶ | sub file_is { | ||||||||||||||||||
|
Subroutine
file_is: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 129 | 0 | my ($class, $fname) = @_; | |||||||||||||||||||
| 130 | 0 | $fname =~ /\.xyz$/i; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | ▶ | sub write_string { | ||||||||||||||||||
|
Subroutine
write_string: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 134 | 0 | my ($class, $mol, %opts) = @_; | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | %opts = (count => 1, name => 1, symbol => 1, %opts); | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | # header | ||||||||||||||||||||
| 139 | 0 | my $ret = ''; | |||||||||||||||||||
| 140 | 0 | ▶ | $ret .= $mol->atoms . "\n" if $opts{count}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 0 | ▶ | if ($opts{name}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 0 | ▶ | my $name = defined $mol->name ? $mol->name : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 0 | $name =~ s/\n.*//s; | |||||||||||||||||||
| 144 | 0 | $ret .= "$name\n" | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | # body | ||||||||||||||||||||
| 148 | 0 | for my $atom ($mol->atoms) { | |||||||||||||||||||
| 149 | $ret .= sprintf "%-2s %8.4f %8.4f %8.4f\n", | ||||||||||||||||||||
| 150 | 0 | ▶ | $opts{symbol} ? $atom->symbol : $atom->Z, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | $atom->coords->array; | ||||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | 0 | $ret; | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 9 | 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 | |||||||||||||||||||||