home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / PDBFile.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  5.8 KB  |  197 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: PDBFile.php,v 1.5 2003/05/13 01:18:17 jmcastagnetto Exp $
  20. //
  21.  
  22. require_once "Science/Chemistry/Macromolecule_PDB.php";
  23.  
  24. /**
  25.  * Represents a PDB file, composed of one or more Science_Chemistry_Macromolecule_PDB objects
  26.  *
  27.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  28.  * @version 1.0
  29.  * @access  public
  30.  * @package Science_Chemistry
  31.  * @see     Science_Chemistry_PDBParser
  32.  */
  33. class Science_Chemistry_PDBFile {
  34.  
  35.     /**
  36.      * PDB ID
  37.      *
  38.      * @var     string
  39.      * @access   private
  40.      */
  41.     var $pdb;
  42.  
  43.     /**
  44.      * Full path to PDB file
  45.      *
  46.      * @var     string
  47.      * @access  private
  48.      */
  49.     var $file;
  50.  
  51.     /**
  52.      * PDB file's date
  53.      *
  54.      * @var     string
  55.      * @access  private
  56.      */
  57.     var $date;
  58.  
  59.     /**
  60.      * PDB macromolecule(s) class
  61.      *
  62.      * @var     string
  63.      * @access  private
  64.      */
  65.     var $class;
  66.  
  67.     /**
  68.      * Array of meta records
  69.      *
  70.      * @var     array
  71.      * @access  private
  72.      */
  73.     var $meta;
  74.  
  75.     /**
  76.      * Array of macromolecular objects
  77.      *
  78.      * @var     array
  79.      * @access  private
  80.      */
  81.     var $macromolecules;
  82.  
  83.     /**
  84.      * Number of molecules/subunits
  85.      *
  86.      * @var     int
  87.      * @access  private
  88.      */
  89.     var $num_macromolecules;
  90.  
  91.     /**
  92.      * Constructor for the class, requires a PDB filename
  93.      * 
  94.      * @param   string  $filename
  95.      * @return  object  PDBFile;
  96.      * @access  public
  97.      * @see     $pdb
  98.      * @see     $file
  99.      * @see     mkArrays()
  100.      */
  101.     function Science_Chemistry_PDBFile($filename, $usemeta=false) {
  102.         if (!file_exists($filename))
  103.             return null;
  104.         list($pdb,) = explode(".",basename($filename));
  105.         $this->pdb = $pdb;
  106.         $this->file = realpath($filename);
  107.         $this->parseFile(file($filename), $usemeta);
  108.     }
  109.  
  110.     /**
  111.      * Makes the arrays of all present PDB record types
  112.      *
  113.      * @param   array   $arr    array of lines
  114.      * @access  private
  115.      * @see     Science_Chemistry_Macromolecule_PDB()
  116.      */
  117.     function parseFile($arr, $usemeta) {
  118.         $month = array (
  119.                 "JAN" => "01", "FEB" => "02", "MAR" => "03",
  120.                 "APR" => "04", "MAY" => "05", "JUN" => "06",
  121.                 "JUL" => "07", "AUG" => "08", "SEP" => "09",
  122.                 "OCT" => "10", "NOV" => "11", "DEC" => "12"
  123.                 );
  124.         $header_re = "/^HEADER[[:space:]]+(([^[:space:]]+ )+)[[:space:]]+";
  125.         $header_re .= "([0-9]{2}-[A-Z]{3}-[0-9]{2,4})[[:space:]]+[A-Z0-9]{4}/";
  126.  
  127.         if (preg_match($header_re, $arr[0], &$regs)) {
  128.             $this->class = trim($regs[1]);
  129.             // put date in a more standard format
  130.             $tmp = explode("-", $regs[3]);
  131.             if ($tmp[2] <= 23)
  132.                 $year = 2000 + (int)$tmp[2];
  133.             else
  134.                 $year = 1900 + (int)$tmp[2];
  135.             $this->date = $year."-".$month[$tmp[1]]."-".$tmp[0];
  136.         }
  137.         
  138.         $flag = "nomodel";
  139.         $tmparr = array();
  140.         for ($i=0; $i < count($arr); $i++) {
  141.             if (!trim($arr[$i]))
  142.                 continue;
  143.             $rectype = trim(strtok($arr[$i]," "));
  144.             
  145.             // check if we have multi-model file
  146.             if ($rectype == "MODEL") {
  147.                 $flag = "model";
  148.                 continue;
  149.             }
  150.  
  151.             // create the meta array and accumulate the atom records
  152.             if ($rectype != "ATOM" && $rectype != "HETATM") {
  153.                 if ($usemeta) {
  154.                     $this->meta[$rectype][] = trim($arr[$i]);
  155.                 } else {
  156.                     continue;
  157.                 }
  158.             } else {
  159.                 $tmparr[] = $arr[$i];
  160.             }
  161.  
  162.             // did we get a multi-model file and are parsing the end
  163.             // of a model, if so, create new macromolecule and change
  164.             // the flag
  165.             if ($rectype == "ENDMDL") {
  166.                 $this->macromolecules[] = new Science_Chemistry_Macromolecule_PDB($this->pdb, 
  167.                                                 $tmparr, &$this);
  168.                 $this->num_macromolecules++;
  169.                 $flag = "endmodel";
  170.                 $tmparr = array();
  171.             }
  172.         }
  173.         // if we got to the end without hitting a MODEL ... ENDMDL pair
  174.         // add the only macromolecule in this file to the array
  175.         if ($flag == "nomodel") {
  176.             $this->macromolecules[] = new Science_Chemistry_Macromolecule_PDB(&$this->pdb, 
  177.                                             &$tmparr, &$this);
  178.             $this->num_macromolecules++;
  179.         }
  180.     }
  181.  
  182.     /**
  183.      * Returns a CML representation of the PDB file
  184.      * TODO
  185.      *
  186.      * @return  string
  187.      * @access  public
  188.      */
  189.     function toCML() {
  190.         // TODO
  191.     }
  192.     
  193. } // end of PDBFile
  194.  
  195. // vim: expandtab: ts=4: sw=4
  196. ?>
  197.