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

Source for file Macromolecule.php

Documentation is available at Macromolecule.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. require_once "Science/Chemistry/Molecule.php";
  25.  
  26. /**
  27.  * Represents a macromolecule, composed of several
  28.  * Science_Chemistry_Molecule objects
  29.  *
  30.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  31.  * @version 1.0
  32.  * @access  public
  33.  * @package Science_Chemistry
  34.  */
  35.  
  36.     /**
  37.      * Macromolecule's name
  38.      *
  39.      * @var     string 
  40.      * @access   private
  41.      */
  42.     var $name;
  43.  
  44.     /**
  45.      * Array of molecular objects
  46.      *
  47.      * @var     array 
  48.      * @access  private
  49.      */
  50.     var $molecules;
  51.  
  52.     /**
  53.      * Number of molecules/subunits
  54.      *
  55.      * @var     int 
  56.      * @access  private
  57.      */
  58.     var $num_molecules;
  59.  
  60.     /**
  61.      * Constructor for the class, requires a macromolecule name
  62.      * and an optional array of Science_Chemistry_Molecule objects
  63.      * 
  64.      * @param   string  $name 
  65.      * @param   optional    array   $molecules
  66.      * @return  object  Science_Chemistry_Macromolecule 
  67.      * @access  public
  68.      * @see     $name
  69.      * @see     initMacromolecule()
  70.      */
  71.     function Science_Chemistry_Macromolecule($name$molecules=""{
  72.         $this->name $name;
  73.         if (!empty($molecules))
  74.             if (!$this->initMacromolecule($molecules))
  75.                 return null;
  76.     }
  77.     
  78.  
  79.     /**
  80.      * Initializes the array of Science_Chemistry_Molecule objects
  81.      * 
  82.      * @param   array   $molecules 
  83.      * @return  boolean 
  84.      * @access  public
  85.      * @see     $num_molecules
  86.      * @see     $molecules
  87.      * @see     addMolecule()
  88.      */
  89.     function initMacromolecule($molecules{
  90.         if (!is_array($molecules)) {
  91.             return false;
  92.         }
  93.  
  94.         for ($i=0; $i count($molecules)$i++{
  95.             if (!$this->addMolecule($molecules[$i])) {
  96.                 return false;
  97.             }
  98.         }
  99.  
  100.         return true;
  101.     }
  102.  
  103.     /**
  104.      * Adds a Science_Chemistry_Molecule object to the list of molecules in the macromolecule
  105.      * 
  106.      * @param   object  Science_Chemistry_Molecule   $mol 
  107.      * @return  boolean 
  108.      * @access  public
  109.      * @see     initMacromolecule()
  110.      */
  111.     function addMolecule($mol{
  112.         if (Science_Chemistry_Molecule::isMolecule($mol)) {
  113.             $this->molecules[$mol;
  114.             $this->num_molecules++;
  115.             return true;
  116.         else {
  117.             return false;
  118.         }
  119.     }
  120.  
  121.     /**
  122.      * Returns an array of Science_Chemistry_Molecule objects
  123.      *
  124.      * @return  array 
  125.      * @access  public
  126.      * @see     $molecules
  127.      */
  128.     function getMolecules({
  129.         return $this->molecules;
  130.     }
  131.  
  132.     /**
  133.      * Checks if the object is an instance of Science_Chemistry_Macromolecule
  134.      *
  135.      * @param   object  Science_Chemistry_Macromolecule $obj 
  136.      * @return  boolean 
  137.      * @access  public
  138.      */
  139.     function isMacromolecule($obj{
  140.         return  (is_object($obj&& 
  141.                  (strtolower(strtolower(get_class($obj))) == strtolower("Science_Chemistry_Macromolecule"||
  142.                   is_subclass_of($objstrtolower("Science_Chemistry_Macromolecule")))
  143.                 );
  144.     }
  145.  
  146.  
  147.     /**
  148.      * Returns a string representation of the macromolecule
  149.      * as a multiple molecule XYZ-format file
  150.      *
  151.      * @return  string 
  152.      * @access  public
  153.      * @see toString()
  154.      */
  155.     function toXYZ({
  156.         $out "# Number of molecules: ".$this->num_molecules."\n";
  157.         for ($i=0; $i $this->num_molecules$i++)
  158.             $out .= "# Molecule ".($i+1)."\n".$this->molecules[$i]->toString()."\n";
  159.         return $out;
  160.     }
  161.  
  162.     /**
  163.      * Returns a string representation of the macromolecule
  164.      * as a multiple molecule XYZ-format file
  165.      * Alias of toXYZ()
  166.      *
  167.      * @return  string 
  168.      * @access  public
  169.      * @see toString()
  170.      */
  171.     function toString({
  172.         return $this->toXYZ();
  173.     }
  174.  
  175.     /**
  176.      * Returns a CML representation of the molecule
  177.      * Accepts an optional id, and a flag to signal
  178.      * printing of the connection table
  179.      *
  180.      * @param   optional    string  $id
  181.      * @param   optional    boolean $connect
  182.      * @return  string 
  183.      * @access  public
  184.      */
  185.     function toCML($title="macromolecule"$id="macromol1"$connect=false{
  186.         $out = "<molecule title=\"$title\" id=\"$id\">\n";
  187.         $out .= "<list title=\"molecules\">\n";
  188.         for ($i=0; $i $this->num_molecules$i++{
  189.             $mol =$this->molecules[$i];
  190.             $out .= $mol->toCML($mol->name($i+1)$connect);
  191.         }
  192.         $out .= "</list>\n</molecule>\n";
  193.         return $out;
  194.     }
  195.  
  196. // end of Science_Chemistry_Macromolecule
  197.  
  198. // vim: expandtab: ts=4: sw=4
  199. ?>

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