       1 package MolecularDescriptors::AtomsCountDescriptors;
       2 #
       3 # $RCSfile: AtomsCountDescriptors.pm,v $
       4 # $Date: 2015/02/28 20:49:19 $
       5 # $Revision: 1.10 $
       6 #
       7 # Author: Manish Sud <msud@san.rr.com>
       8 #
       9 # Copyright (C) 2015 Manish Sud. All rights reserved.
      10 #
      11 # This file is part of MayaChemTools.
      12 #
      13 # MayaChemTools is free software; you can redistribute it and/or modify it under
      14 # the terms of the GNU Lesser General Public License as published by the Free
      15 # Software Foundation; either version 3 of the License, or (at your option) any
      16 # later version.
      17 #
      18 # MayaChemTools is distributed in the hope that it will be useful, but without
      19 # any warranty; without even the implied warranty of merchantability of fitness
      20 # for a particular purpose.  See the GNU Lesser General Public License for more
      21 # details.
      22 #
      23 # You should have received a copy of the GNU Lesser General Public License
      24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
      25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
      26 # Boston, MA, 02111-1307, USA.
      27 #
      28
      29 use strict;
      30 use Carp;
      31 use Exporter;
      32 use Scalar::Util ();
      33 use Molecule;
      34 use MolecularDescriptors::MolecularDescriptors;
      35
      36 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
      37
      38 @ISA = qw(MolecularDescriptors::MolecularDescriptors Exporter);
      39 @EXPORT = qw();
      40 @EXPORT_OK = qw(GetDescriptorNames);
      41
      42 %EXPORT_TAGS = (all  => [@EXPORT, @EXPORT_OK]);
      43
      44 # Setup class variables...
      45 my($ClassName, @DescriptorNames);
      46 _InitializeClass();
      47
      48 # Overload Perl functions...
      49 use overload '""' => 'StringifyAtomsCountDescriptors';
      50
      51 # Class constructor...
      52 sub new {
      53   my($Class, %NamesAndValues) = @_;
      54
      55   # Initialize object...
      56   my $This = $Class->SUPER::new();
      57   bless $This, ref($Class) || $Class;
      58   $This->_InitializeAtomsCountDescriptors();
      59
      60   $This->_InitializeAtomsCountDescriptorsProperties(%NamesAndValues);
      61
      62   return $This;
      63 }
      64
      65 # Initialize class ...
      66 sub _InitializeClass {
      67   #Class name...
      68   $ClassName = __PACKAGE__;
      69
      70   # Descriptor names...
      71   @DescriptorNames = ('Atoms', 'HeavyAtoms');
      72
      73 }
      74
      75 # Get descriptor names as an array...
      76 #
      77 # This functionality can be either invoked as a class function or an
      78 # object method.
      79 #
      80 sub GetDescriptorNames {
      81   return @DescriptorNames;
      82 }
      83
      84 # Initialize object data...
      85 #
      86 sub _InitializeAtomsCountDescriptors {
      87   my($This) = @_;
      88
      89   # Type of MolecularDescriptor...
      90   $This->{Type} = 'AtomsCount';
      91
      92   # Intialize descriptor names and values...
      93   $This->_InitializeDescriptorNamesAndValues(@DescriptorNames);
      94
      95   return $This;
      96 }
      97
      98 # Initialize object properties...
      99 #
     100 sub _InitializeAtomsCountDescriptorsProperties {
     101   my($This, %NamesAndValues) = @_;
     102
     103   my($Name, $Value, $MethodName);
     104   while (($Name, $Value) = each  %NamesAndValues) {
     105     $MethodName = "Set${Name}";
     106     $This->$MethodName($Value);
     107   }
     108
     109   return $This;
     110 }
     111
     112 # Generate molecular weight and exact mass values...
     113 #
     114 sub GenerateDescriptors {
     115   my($This) = @_;
     116
     117   # Initialize descriptor values...
     118   $This->_InitializeDescriptorValues();
     119
     120   # Check availability of molecule...
     121   if (!$This->{Molecule}) {
     122     carp "Warning: ${ClassName}->GenerateDescriptors: $This->{Type} molecular descriptors generation didn't succeed: Molecule data is not available: Molecule object hasn't been set...";
     123     return undef;
     124   }
     125
     126   # Calculate descriptor values...
     127   if (!$This->_CalculateDescriptorValues()) {
     128     carp "Warning: ${ClassName}->GenerateDescriptors: $This->{Type} molecular descriptors generation didn't succeed...";
     129     return undef;
     130   }
     131
     132   # Set final descriptor values...
     133   $This->_SetFinalDescriptorValues();
     134
     135   return $This;
     136 }
     137
     138 # Calculate molecular weight and exact mass values..
     139 #
     140 sub _CalculateDescriptorValues {
     141   my($This) = @_;
     142   my($MolecularWeight, $ExactMass);
     143
     144   $This->{Atoms} = $This->{Molecule}->GetNumOfAtoms();
     145   $This->{HeavyAtoms} = $This->{Molecule}->GetNumOfHeavyAtoms();
     146
     147   return $This;
     148 }
     149
     150 # Setup final descriptor values...
     151 #
     152 sub _SetFinalDescriptorValues {
     153   my($This) = @_;
     154
     155   $This->{DescriptorsGenerated} = 1;
     156
     157   $This->SetDescriptorValues($This->{Atoms}, $This->{HeavyAtoms});
     158
     159   return $This;
     160 }
     161
     162 # Return a string containg data for AtomsCountDescriptors object...
     163 #
     164 sub StringifyAtomsCountDescriptors {
     165   my($This) = @_;
     166   my($TheString);
     167
     168   $TheString = "MolecularDescriptorType: $This->{Type}; " . $This->_StringifyDescriptorNamesAndValues();
     169
     170   return $TheString;
     171 }
     172
     173 # Is it a AtomsCountDescriptors object?
     174 sub _IsAtomsCountDescriptors {
     175   my($Object) = @_;
     176
     177   return (Scalar::Util::blessed($Object) && $Object->isa($ClassName)) ? 1 : 0;
     178 }
     179