Demian Riccardi


HackaMol - HackaMol: Object-Oriented Library for Molecular Hacking


version 0.00_12


   use HackaMol;
   use Math::Vector::Real;
   use Math::Vector::Real::Random;
   use Math::Trig;

   my $hack = HackaMol->new( name => "hackitup" );
   my @atoms = $hack->read_file_atoms("t/lib/1L2Y.pdb");
   # all coordinates from NMR ensemble are loaded into atoms
   my $mol = HackaMol::Molecule->new(
       name  => 'trp-cage',
       atoms => [@atoms]
   #recenter all coordinates to center of mass
   foreach my $t ( 0 .. $atoms[0]->count_coords - 1 ) {
       $mol->translate( -$mol->COM );
   # print coordinates from t=0 to and return filehandle
   my $fh = $mol->print_xyz( $mol->name . ".xyz" );
   # print coordinates for @t=(1..4) to same filehandle
   foreach my $t ( 1 .. 4 ) {
   foreach ( 1 .. 10 ) {
           V( 0, 0, 1 ),    # rotation vector
           36,              # rotate by 36 degrees
           V( 5, 0, 0 )     # origin of rotation
   # translate/rotate method is provided by AtomGroupRole
   # populate groups byatom resid attr
   my @groups = $hack->group_by_atom_attr( 'resid', $mol->all_atoms );

   # silly rotation of sidechains about their own center of mass   
   foreach my $ang ( 1 .. 10 ) {
       $_->rotate( V( 1, 1, 1 ), 36, $_->COM ) foreach $mol->all_groups;
   $fh->close;    # done filling with coordinates
   #example/ picks up from here


The HackaMol library enables users to build simple, yet powerful scripts for carrying out computational work on molecules at multiple scales. The molecular object system organizes atoms within molecules using groups, bonds, angles, and dihedrals. HackaMol seeks to provide intuitive attributes and methods that may be harnessed to coerce molecular computation through a common core. The library is inspired by PerlMol, BioPerl, MMTSB, and my own experiences as a researcher.

The library is organized into two regions: HackaMol, the core (contained here) that has classes for atoms and molecules, and HackaMolX, the extensions, such as HackaMolX::PDB, a parser for protein databank files, and HackaMolX::Calculator, an abstract calculator for coercing molecular computation, that use the core. The three major goals of the core are for it to be well-tested, well-documented, and easy to install. The goal of the extensions is to provide a more flexible space for researchers to develop and share new methods that use the core. Extensions are in the works, but the HackaMolX namespace has not been established yet!

HackaMol uses Math::Vector::Real (MVR) for all the vector operations. MVR is a lightweight solution with a fast XS dropin that overlaps very well with the desirables for working with atoms and coarse grained molecules. Extensions that treat much larger systems will definitely benefit from the capabilities PDL or Math::GSL.

The HackaMol class (loaded in Synopsis) uses the core classes to provide some object building utilities described below. This class consumes HackaMol::MolReadRole to provide structure readers for xyz and pdb coordinates. See Open Babel if other formats needed (All suggestions, contributions welcome!).



takes a list of atoms and returns a list of bonds. The bonds are generated for "list neighbors" by simply stepping through the atom list one at a time. e.g.

  my @bonds = $hack->build_bonds(@atoms[1,3,5]);

will return two bonds: B13 and B35


takes a list of atoms and returns a list of angles. The angles are generated analagously to build_bonds, e.g.

  my @angles = $hack->build_angles(@atoms[1,3,5]);

will return one angle: A135


takes a list of atoms and returns a list of dihedrals. The dihedrals are generated analagously to build_bonds, e.g.

  my @dihedral = $hack->build_dihedrals(@atoms[1,3,5]);

will croak! you need atleast four atoms.

  my @dihedral = $hack->build_dihedrals(@atoms[1,3,5,6,9]);

will return two dihedrals: D1356 and D3569


takes atom attribute and a list of atoms as arguments and builds AtomGroup objects by attribute. Grouping by graphical searches are needed!


takes hash argument list and returns bonds. Find bonds between bond_atoms and the candidates.

  my @oxy_bonds = $hack->find_bonds_brute(
                                    bond_atoms => [$hg],
                                    candidates => [$mol->all_atoms],
                                    fudge      => 0.45,
                                    max_bonds  => 6,

fudge is optional with Default is 0.45 (open babel uses same default); max_bonds is optional with default of 99. max_bonds is compared against the atom bond count, which are incremented during the search. Before returning the bonds, the bond_count are returned the values before the search. For now, molecules are responsible for setting the number of bonds in atoms. find_bonds_brute uses a bruteforce algorithm that tests the interatomic separation against the sum of the covalent radii + fudge. It will not test for bond between atoms if either atom has >= max_bonds. It does not return a self bond for an atom ( next if refaddr($ati) == refaddr($atj) ).



name is a rw str provided by HackaMol::NameRole.





Demian Riccardi <>


This software is copyright (c) 2014 by Demian Riccardi.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.