MayaChemTools

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