home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Macromolecule.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  5.7 KB  |  196 lines

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