Subject: | new feature patch: Chemistry::Mol->convert() |
The attached file has an addition (and a working test) for a
Chemistry::Mol->convert() method. This just wraps up
Chemistry::Mol->read() and $mol->write() calls for convenience so that
things like this can be done:
Chemistry::Mol->convert( src=>'test.zmat', destOpts=>{format=>'xyz'} );
Chemistry::Mol->convert( src=>'test.zmat', dest=>'foo.xyz' );
And also gets handy for things like this (which could easily further be
wrapper in a shell script):
perl -MChemistry::File=:auto -e
'Chemistry::Mol->convert(src=>$ARGV[0], dest=>$ARGV[1])' test.zmat foo.xyz
thoughts/comments?
Subject: | chem_mol_convert.pl |
#!/usr/bin/perl
use strict;
use warnings;
use lib '.';
use lib 'Chemistry-File-InternalCoords-0.02/lib';
use Chemistry::File qw/ InternalCoords XYZ /;
#* write a Chemistry::Mol->convert sub ...
# wraps a read & a write of different formats.
#
#Chemistry::Mol->convert( 'foo.zmat', {}, 'foo.xyz', {} );
package Chemistry::Mol;
#############################################################################
# begin patch ====>>
#############################################################################
=pod
=item Chemistry::Mol->convert( src => ___, srcOpts=>{option=>value, ...}, dest=> ___, destOpts=>{option=>value, ...} );
Wrapper for Chemistry::Mol->read() and $mol->write calls to provide easy conversion from one format to another.
Chemistry::Mol->convert( src=>'test.zmat', destOpts=>{format=>'xyz'} );
Chemistry::Mol->convert( src=>'test.zmat', dest=>'foo.xyz' );
Chemistry::Mol->convert( src=>\*DATA, srcOpts=>{format=>'zmat'}, dest=>\*STDOUT, destOpts=>{format=>'xyz'} );
=cut
sub convert {
my $self = shift;
my $p = { src=>undef, srcOpts=>{}, dest=>undef, destOpts=>{}, @_ };
my $src = $p->{src};
my $dest = $p->{dest};
my $mol = $self->read( $src, %{$p->{srcOpts}} );
if( !$dest && !ref($src) ){ # if output is unspecified and input was a filename ...
# ... then make output be a filename w/the new extension.
$dest = $src;
$dest =~ s/\..+?$//;
$dest .= "." . $p->{destOpts}->{format};
}
$mol->write( $dest, %{$p->{destOpts}} );
}
#############################################################################
# <<==== end patch
#############################################################################
package main;
$|=1;
Chemistry::Mol->convert( src=>'test.zmat', destOpts=>{format=>'xyz'} );
Chemistry::Mol->convert( src=>'test.zmat', dest=>'foo.xyz' );
Chemistry::Mol->convert( src=>\*DATA, srcOpts=>{format=>'zmat'}, dest=>\*STDOUT, destOpts=>{format=>'xyz'} );
exit;
__DATA__
H
H 1 B1
B1 0.70000000