Chemistry-Mol

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

        - Added %S option to $mol->printf
        - Atom.pm: added total_hydrogens, implicit_hydrogens, explicit_valence,
          and deprecated hydrogens.

0.25  Jun 30 2004
        - Fixed Chemistry::File :auto so that it looks in every @INC directory.
        - Added internal coordinates for atoms.
        - Updated the tutorial a little bit.

0.24  Jun 16 2004
        - Mol:: _weaken, sort_atoms, atom_class, bond_class
        - Atom:: sprintf, printf, hydrogens, valence
        - Added the Chemistry::File::Dumper module.

0.23  May 19 2004
        - New methods: Mol::printf, sprintf, charge; Atom::aromatic,
          formal_charge; Bond::aromatic
        - Fixed POD bug.

0.22  May 17 2004
        - Fixed bug in bonds($from)
        - Added add_atom_np, add_bond_np, bonds_neighbors
        - Fixed another memory leak
        - Added Atom::formal_charge

0.21  May 13 2004
        - Fixed bug where $/ was undef'ed in a nonlocal way in File.pm.
        - Added formula parser contributed by Brent Gregersen.
        - Added %j and %% formats to Formula.pm.

0.20  May 06 2004
        - Use Scalar::Util::weaken to avoid strong cyclic references
          and ensure garbage collection.
        - New methods for Chemistry::Mol: delete_atom, delete_bond,
          clone, combine, separate, distance
        - New methods for Chemistry::Atom: angle, dihedral, angle_deg, 
          dihedral_deg, delete
        - New methods for Chemistry::Bond: delete
        - Chemistry::Mol can export read_mol
        - Chemistry::Atom can export distance, angle, dihedral

0.11  Feb 22 2004

lib/Chemistry/Atom.pm  view on Meta::CPAN

    $atom->symbol("C")->name("CA")->coords(1,2,3);

=cut

# Considering to add the following attributes:
# mass_number (A)

use 5.006;
use strict;
use warnings;
use Scalar::Util 'weaken';
use Math::VectorReal qw(O vector);
use Math::Trig;
use Carp;
use base qw(Chemistry::Obj Exporter);
use List::Util qw(first);

our @EXPORT_OK = qw(distance angle dihedral angle_deg dihedral_deg);
our %EXPORT_TAGS = (
      all  => \@EXPORT_OK,
);

lib/Chemistry/Atom.pm  view on Meta::CPAN

# this method is for internal use only; called by $mol->add_bond
sub add_bond {
    my $self = shift;
    my $bond = shift;
    my %seen;
    #return if first { $_ eq $bond } @{$self->{bonds}}; 

    for my $atom (@{$bond->{atoms}}){ #for each atom...
        if ($atom ne $self) {
            my $b = {to=>$atom, bond=>$bond};
            weaken($b->{to});
            weaken($b->{bond});
            push @{$self->{bonds}}, $b;
        }
    }
}

# make sure the atom doesn't cause circular references
sub _weaken {
    my $self = shift;
    for my $b (@{$self->{bonds}}) {
        weaken($b->{to});
        weaken($b->{bond});
    }
    weaken($self->{parent});
}

# This method is private. Bonds should be deleted from the 
# mol object. These methods should only be called by 
# $bond->delete_atoms, which is called by $mol->delete_bond
sub delete_bond {
    my ($self, $bond) = @_;
    $self->{bonds} = [ grep { $_->{bond} ne $bond } @{$self->{bonds}} ];
}

lib/Chemistry/Atom.pm  view on Meta::CPAN


Returns the atom's containing object (the molecule to which the atom belongs).
An atom can only have one parent.

=cut

sub parent {
    my $self = shift;
    if (@_) {
        ($self->{parent}) = @_;
        weaken($self->{parent});
        $self;
    } else {
        $self->{parent};
    }
}

=item $atom->neighbors($from)

Return a list of neighbors. If an atom object $from is specified, it will be
excluded from the list (this is useful if an atom wants to know who its 

lib/Chemistry/Bond.pm  view on Meta::CPAN

=head2 Bond Attributes

In addition to common attributes such as id, name, and type, 
bonds have the order attribute. The bond order is a number, typically the
integer 1, 2, 3, or 4.

=cut

use 5.006;
use strict;
use Scalar::Util 'weaken';
use base qw(Chemistry::Obj);

my $N = 0;

=head1 METHODS

=over 4

=item Chemistry::Bond->new(name => value, ...)

lib/Chemistry/Bond.pm  view on Meta::CPAN

atoms in a bond may have strange side effects; it is safer to treat bonds as
immutable except with respect to properties such as name and type.

=cut

sub atoms {
    my $self = shift;
    if (@_) {
        $self->{atoms} = ref $_[0] ? $_[0] : [@_];
        for my $a (@{$self->{atoms}}) { 
            weaken($a);
            $a->add_bond($self);
        }
    } else {
        return (@{$self->{atoms}});
    }
}

sub _weaken {
    my $self = shift;
    for my $a (@{$self->{atoms}}) {
        weaken($a);
    }
    weaken($self->{parent});
}

# This method is private and should only be called from $mol->delete_bond
sub delete_atoms {
    my $self = shift;
    for my $a (@{$self->{atoms}}) { # delete bond from each atom
        $a->delete_bond($self);
    }
}

lib/Chemistry/Bond.pm  view on Meta::CPAN

sub delete {
    my ($self) = @_;
    $self->parent->_delete_bond($self);
    $self->{deleted} = 1;
}

sub parent {
    my $self = shift;
    if (@_) {
        ($self->{parent}) = @_;
        weaken($self->{parent});
        $self;
    } else {
        $self->{parent};
    }
}



1;

lib/Chemistry/File/Dumper.pm  view on Meta::CPAN

sub read_mol {
    my ($self, $fh, %opts) = @_;
    my $mol;
    my $s = do { local $/; <$fh> };
    return unless $s;
    eval $s;
    if ($@) {
        croak "Dumper eval error: $@" if $opts{fatal};
        return;
    } 
    $mol->_weaken;
    $mol;
}

sub name_is {
    my ($self, $name, %opts) = @_;
    $name =~ /\.pl$/i;
}

sub string_is {
    my ($self, $s, %opts) = @_;

lib/Chemistry/Mol.pm  view on Meta::CPAN

C<Clone> (default is C<Storable>). The documentation of Storable claims L<Clone>
is less memory-intensive.

=cut

sub clone {
    my ($self) = @_;
    my $clone;
    if ($clone_backend eq "Storable") {
        $clone = dclone $self;
        $clone->_weaken if Storable->VERSION < 2.14;
    } elsif ($clone_backend eq "Clone") {
        require Clone;
        $clone = Clone::clone $self;
    } else {
        croak "Unknown clone backend '$clone_backend'";
    }
    $clone;
}

=item my $mol2 = $mol->safe_clone;

lib/Chemistry/Mol.pm  view on Meta::CPAN


sub safe_clone {
    my ($mol) = @_;
    my $clone = $mol->clone;
    for ($clone, $clone->atoms, $clone->bonds) {
        $_->id($_->nextID);
    }
    $clone;
} 

sub _weaken {
    my ($self) = @_;
    for ($self->atoms, $self->bonds) {
        $_->_weaken;
    }
    $self;
}

=item ($distance, $atom_here, $atom_there) = $mol->distance($obj)

Returns the minimum distance to $obj, which can be an atom, a molecule, or a
vector. In scalar context it returns only the distance; in list context it
also returns the atoms involved. The current implementation for calculating
the minimum distance between two molecules compares every possible pair of



( run in 0.597 second using v1.01-cache-2.11-cpan-65fba6d93b7 )