Science_Chemistry
[ class tree: Science_Chemistry ] [ index: Science_Chemistry ] [ all elements ]

Source for file Molecule.php

Documentation is available at Molecule.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20. //
  21.  
  22. require_once "Science/Chemistry.php";
  23. require_once "Science/Chemistry/Atom.php";
  24.  
  25. /**
  26.  * Base class representing a Molecule
  27.  *
  28.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  29.  * @version 1.0
  30.  * @access  public
  31.  * @package Science_Chemistry
  32.  */
  33.  
  34.     /**
  35.      * Molecule name
  36.      *
  37.      * @var     string 
  38.      * @access  public
  39.      */
  40.     var $name = "";
  41.  
  42.     /**
  43.      * Number of atoms in the molecule
  44.      * 
  45.      * @var     integer 
  46.      * @access  public
  47.      * @see     initMolecule()
  48.      */
  49.     var $num_atoms = 0;
  50.  
  51.     /**
  52.      * Array of atom objects in the molecule
  53.      *
  54.      * @var     array 
  55.      * @access  private
  56.      * @see     initMolecule()
  57.      */
  58.     var $atoms = array();
  59.  
  60.     /**
  61.      * Atom-Atom distance matrix
  62.      *
  63.      * @var     array 
  64.      * @access  private
  65.      * @see     calcDistanceMatrix()
  66.      */
  67.     var $dist_matrix = array();
  68.  
  69.     /**
  70.      * Atom-Atom connection (bond) table
  71.      *
  72.      * @var     array 
  73.      * @access  private
  74.      * @see     calcConnectionTable()
  75.      */
  76.     var $conn_table = array();
  77.  
  78.     /**
  79.      * Distance cutoff for bond estimation
  80.      *
  81.      * @var     float 
  82.      * @access  private
  83.      * @see     setBondCutoff()
  84.      * @see     getBondCutoff()
  85.      * @see     calcConnectionTable()
  86.      */
  87.     var $BONDCUTOFF = 1.8;
  88.  
  89.     /**
  90.      * Constructor for the class, requires a molecule name
  91.      * and an optional array of Science_Chemistry_Atom objects
  92.      * 
  93.      * @param   string  $name 
  94.      * @param   optional    array   $atoms
  95.      * @return  object  Science_Chemistry_Molecule 
  96.      * @access  public
  97.      * @see     $name
  98.      * @see     initMolecule()
  99.      */
  100.     function Science_Chemistry_Molecule($name$atoms=""{
  101.         if ($name)
  102.             $this->name = $name;
  103.         else
  104.             return null;
  105.         if ($atoms && is_array($atoms))
  106.             if (!$this->initMolecule($atoms))
  107.                 return null;
  108.     }
  109.  
  110.     /**
  111.      * Initializes the array of Science_Chemistry_Atom objects
  112.      * 
  113.      * @param   array   $atoms 
  114.      * @return  boolean 
  115.      * @access  public
  116.      * @see     $num_atoms
  117.      * @see     $atoms
  118.      * @see     addAtom()
  119.      */
  120.     function initMolecule($atoms{
  121.         if (is_array($atoms)) {
  122.             for ($i=0; $i=count($atoms)$i++{
  123.                 if (!$this->addAtom($atoms[$i]))
  124.                     return false;
  125.             }
  126.             return true;
  127.         else {
  128.             return false;
  129.         }
  130.     }
  131.  
  132.     /**
  133.      * Adds a Science_Chemistry_Atom object to the list of atoms in the molecule
  134.      * 
  135.      * @param   object  Science_Chemistry_Atom   $atom 
  136.      * @return  boolean 
  137.      * @access  public
  138.      * @see     initMolecule()
  139.      */
  140.     function addAtom($atom{
  141.         if (Science_Chemistry_Atom::isAtom($atom)) {
  142.             $this->atoms[$atom;
  143.             $this->num_atoms++;
  144.             // unset the distance matrix and 
  145.             // connection table if they are not empty
  146.             // so next time either one is requested
  147.             // it gets calculated anew
  148.             if (!empty($this->dist_matrix))
  149.                 $this->dist_matrix = array();
  150.             if (!empty($this->conn_table))
  151.                 $this->conn_table = array();
  152.             return true;
  153.         else {
  154.             return false;
  155.         }
  156.     }
  157.  
  158.  
  159.     /**
  160.      * Returns an array of Atom objects
  161.      *
  162.      * @return  array 
  163.      * @access  public
  164.      * @see     $atoms
  165.      */
  166.     function getAtoms({
  167.         return $this->atoms;
  168.     }
  169.  
  170.     /**
  171.      * Checks if the object is an instance of Science_Chemistry_Molecule
  172.      *
  173.      * @param   object  Science_Chemistry_Molecule $obj 
  174.      * @return  boolean 
  175.      * @access  public
  176.      */
  177.     function isMolecule($obj{
  178.         return  (is_object($obj&& 
  179.                  (strtolower(strtolower(get_class($obj))) == strtolower("Science_Chemistry_Molecule"||
  180.                   is_subclass_of($objstrtolower("Science_Chemistry_Molecule")))
  181.                 );
  182.     }
  183.  
  184.     /**
  185.      * Returns a string representation of the molecule  as a XYZ-format file
  186.      * Alias of toXYZ()
  187.      *
  188.      * @return  string 
  189.      * @access  public
  190.      * @see toXYZ()
  191.      */
  192.     function toString({
  193.         return $this->toXYZ();
  194.     }
  195.  
  196.     /**
  197.      * Returns a string representation of the molecule  as a XYZ-format file
  198.      *
  199.      * @return  string 
  200.      * @access  public
  201.      * @see toString()
  202.      */
  203.     function toXYZ({
  204.         if (!$this->atoms)
  205.             return false;
  206.         $out[$this->num_atoms;
  207.         $out[$this->name;
  208.         reset($this->atoms);
  209.         for ($i=0; $i<$this->num_atoms$i++)
  210.             $out[$this->atoms[$i]->toString();
  211.         return implode("\n",$out)."\n";
  212.     }
  213.  
  214.     /**
  215.      * Returns a CML representation of the molecule
  216.      * Accepts an optional id, and a flag to signal
  217.      * printing of the connection table
  218.      *
  219.      * @param   optional    string  $id
  220.      * @param   optional    boolean $connect
  221.      * @return  string 
  222.      * @access  public
  223.      */
  224.     function toCML($title="molecule"$id="mol1"$connect=false{
  225.         $out = " <molecule title=\"$title\" id=\"$id\">\n";
  226.         $out .= "  <string title=\"name\">".$this->name."</string>\n";
  227.         $out .= "  <list title=\"atoms\">\n";
  228.         for ($i=0; $i<$this->num_atoms$i++)
  229.             $out .= $this->atoms[$i]->toCML($i+1);
  230.         $out .= "  </list>\n";
  231.         if ($connect{
  232.             // calculate the connection table if needed
  233.             // and short-circuit if we cannot do that
  234.             if (empty($this->conn_table)) 
  235.                 if (!$this->calcConnectionTable()) {
  236.                     $out .= " </molecule>\n";
  237.                     return $out;
  238.                 }
  239.             $out .= "  <list title=\"connections\">\n";
  240.             for ($i=0; $i count($this->conn_table)$i++{
  241.                 $tmp = array();
  242.                 foreach ($this->conn_table[$ias $atomid=>$flag{
  243.                     if ($flag)
  244.                         $tmp[$atomid + 1;
  245.                 }
  246.                 if (!empty($tmp)) {
  247.                     $out .= "   <list title=\"connect\" id=\"".($i + 1)."\">";
  248.                     $out .= implode(" "$tmp)."</list>\n";
  249.                 }
  250.             }
  251.             $out .= "  </list>\n";
  252.         }
  253.         $out .= " </molecule>\n";
  254.         return $out;
  255.     }
  256.  
  257.     
  258.     /**
  259.      * Sets the distance cutoff for bond determination
  260.      *
  261.      * @param   float   $cutoff 
  262.      * @return  boolean 
  263.      * @access  public
  264.      * @see     $BONDCUTOFF
  265.      * @see     getBondCutoff()
  266.      * @see     calcConnectionTable()
  267.      */
  268.     function setBondCutoff($cutoff{
  269.         if ((float)$cutoff > 0.0{
  270.             $this->BONDCUTOFF = (float)$cutoff;
  271.             return true;
  272.         else {
  273.             return false;
  274.         }
  275.     }
  276.  
  277.     /**
  278.      * Returns the bond cutoff uses to determine bonds
  279.      * 
  280.      * @return  float 
  281.      * @access  public
  282.      * @see     $BONDCUTOFF
  283.      * @see     setBondCutoff()
  284.      * @see     calcConnectionTable()
  285.      */
  286.     function getBondCutoff({
  287.         return $this->BONDCUTOFF;
  288.     }
  289.  
  290.     /**
  291.      * Calculates the atom-atom distance matrix in Angstroms
  292.      *
  293.      * @return  boolean 
  294.      * @access  public
  295.      */
  296.     function calcDistanceMatrix({
  297.         if (empty($this->atoms))
  298.             return false;
  299.         $this->dist_matrix = array();
  300.         for ($i=0; $i $this->num_atoms$i++)
  301.             for ($j=0; $j $this->num_atoms$j++{
  302.                 if ($i == $j{
  303.                     $this->dist_matrix[$i][$j= 0.0;
  304.                 elseif ($i $j{
  305.                     $this->dist_matrix[$i][$j$this->atoms[$i]->distance($this->atoms[$j]);
  306.                 }
  307.             }
  308.         return true;
  309.     }
  310.  
  311.     /**
  312.      * Prints the atom-atom distance matrix
  313.      *
  314.      * @return  string 
  315.      * @access  public
  316.      */
  317.     function printDistanceMatrix({
  318.         if (empty($this->dist_matrix))
  319.             if(!$this->calcDistanceMatrix())
  320.                 return false;
  321.         $dmat &$this->dist_matrix;
  322.         echo "# Atom-Atom Distance Matrix:\n";
  323.         for ($i=0; $i $this->num_atoms$i++)
  324.             echo "\t".($i+1);
  325.         for ($i=0; $i $this->num_atoms$i++{
  326.             echo "\n".($i+1);
  327.             for ($j=0; $j $this->num_atoms$j++)
  328.                 if (!isset($dmat[$i][$j])) {
  329.                     echo "\t";
  330.                 else {
  331.                     printf("\t%.4f",$dmat[$i][$j]);
  332.                 }
  333.         }
  334.         echo "\n";
  335.         return true;
  336.     }
  337.  
  338.     /**
  339.      * Returns the atom-atom distance matrix
  340.      *
  341.      * @return  array 
  342.      * @access  public
  343.      */
  344.     function getDistanceMatrix({
  345.         if (empty($this->dist_matrix))
  346.             if (!$this->calcDistanceMatrix())
  347.                 return false;
  348.         return $this->dist_matrix;
  349.     }
  350.  
  351.     /**
  352.      * Calculates the connection table for the molecule
  353.      *
  354.      * @return  boolean 
  355.      * @access  public
  356.      */
  357.     function calcConnectionTable(){
  358.         if (empty($this->dist_matrix))
  359.             if (!$this->calcDistanceMatrix())
  360.                 return false;
  361.         $dmat &$this->dist_matrix;
  362.         for ($i=0; $i $this->num_atoms$i++)
  363.             for ($j=($i+1)$j $this->num_atoms$j++)
  364.                 $this->conn_table[$i][$j($dmat[$i][$j<= $this->BONDCUTOFF);
  365.         return true;
  366.     }
  367.  
  368.     /**
  369.      * Prints the molecule's connection table
  370.      *
  371.      * @return  boolean 
  372.      * @access  public
  373.      */
  374.     function printConnectionTable({
  375.         if (empty($this->conn_table))
  376.             if (!$this->calcConnectionTable())
  377.                 return false;
  378.         printf("# Connection Table: (cutoff = %.4f Angstroms)\n"$this->BONDCUTOFF);
  379.         for ($i=0; $i $this->num_atoms$i++)
  380.             for ($j=($i+1)$j $this->num_atoms$j++)
  381.                 if ($this->conn_table[$i][$j]{
  382.                     echo $this->atoms[$i]->element.($i+1)."\t";
  383.                     echo $this->atoms[$j]->element.($j+1)."\n";
  384.                 }
  385.         return true;
  386.     }
  387.  
  388.     /**
  389.      * Returns an array of connected atoms and their bond distance
  390.      * e.g. array ( array ($atomobj1, $atomobj2, $distance ), ... )
  391.      * 
  392.      * @return  array 
  393.      * @access  public
  394.      */
  395.     function getConnectionTable({
  396.         if (empty($this->conn_table))
  397.             if (!$this->calcConnectionTable())
  398.                 return false;
  399.         $ct = 0; $ctable=array();
  400.         for ($i=0; $i $this->num_atoms$i++)
  401.             for ($j=($i+1)$j $this->num_atoms$j++)
  402.                 if ($this->conn_table[$i][$j]{
  403.                     $ctable[$ct= array ($this->atoms[$i]$this->atoms[$j],
  404.                                     $this->dist_matrix[$i][$j]);
  405.                     $ct++;
  406.                 }
  407.        return $ctable
  408.     }
  409.  
  410. // end of class Science_Chemistry_Molecule
  411.  
  412.  
  413. // vim: expandtab: ts=4: sw=4
  414. ?>

Documentation generated on Mon, 11 Mar 2019 15:48:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.