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

Source for file PDBParser.php

Documentation is available at PDBParser.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. /**
  23.  * A self-contained class to parse a PDB file into an array of residues
  24.  * each containing an array of atoms
  25.  * <br>
  26.  * Useful when dealing with big PDB files, where using the Science_Chemistry_PDBFile
  27.  * class will generate out of memory errors.
  28.  *
  29.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  30.  * @version 1.0
  31.  * @access  public
  32.  * @package Science_Chemistry
  33.  * @see     Science_Chemistry_PDBFile
  34.  */
  35.  
  36.     /**
  37.      * PDB ID
  38.      *
  39.      * @var     string 
  40.      * @access   private
  41.      */
  42.     var $pdb;
  43.  
  44.     /**
  45.      * Full path to PDB file
  46.      *
  47.      * @var     string 
  48.      * @access  private
  49.      */
  50.     var $file;
  51.  
  52.     /**
  53.      * PDB file's date
  54.      *
  55.      * @var     date 
  56.      * @access  private
  57.      */
  58.     var $date;
  59.  
  60.     /**
  61.      * PDB macromolecule(s) class
  62.      *
  63.      * @var     string 
  64.      * @access  private
  65.      */
  66.     var $class;
  67.  
  68.     /**
  69.      * Array of macromolecules
  70.      *
  71.      * @var     array 
  72.      * @access  private
  73.      */
  74.     var $macromolecules;
  75.  
  76.     /**
  77.      * Number of macromolecules
  78.      *
  79.      * @var     int 
  80.      * @access  private
  81.      */
  82.     var $num_macromolecules;
  83.  
  84.     /**
  85.      * Constructor for the class, requires a PDB filename
  86.      * 
  87.      * @param   string  $filename   PDB filename
  88.      * @param   boolean $multi  whether to parse all models in a multi-model file
  89.      * @param   boolean $meta   whether to store the PDB file meta information
  90.      * @param   boolean $full   whether to store the full set of fields per atom
  91.      * @return  object  PDBParser 
  92.      * @access  public
  93.      * @see     parseResidues()
  94.      */
  95.     function Science_Chemistry_PDBParser($filename$multi=false$meta=false$full=false{
  96.         if (!file_exists($filename))
  97.             return null;
  98.         list($pdb,explode(".",basename($filename));
  99.         $this->pdb $pdb;
  100.         $this->file realpath($filename);
  101.         $arr file($filename);
  102.         // parsing the PDB file
  103.         $month = array (
  104.                 "JAN" => "01""FEB" => "02""MAR" => "03",
  105.                 "APR" => "04""MAY" => "05""JUN" => "06",
  106.                 "JUL" => "07""AUG" => "08""SEP" => "09",
  107.                 "OCT" => "10""NOV" => "11""DEC" => "12"
  108.                 );
  109.         $header_re "/^HEADER[[:space:]]+(([^[:space:]]+ )+)[[:space:]]+";
  110.         $header_re .= "([0-9]{2}-[A-Z]{3}-[0-9]{2,4})[[:space:]]+[A-Z0-9]{4}/";
  111.  
  112.         if (preg_match($header_re$arr[0]$regs)) {
  113.             $this->class trim($regs[1]);
  114.             // put date in a more standard format
  115.             $tmp explode("-"$regs[3]);
  116.             if ($tmp[2<= 23)
  117.                 $year = 2000 + (int)$tmp[2];
  118.             else
  119.                 $year = 1900 + (int)$tmp[2];
  120.             $this->date $year."-".$month[$tmp[1]]."-".$tmp[0];
  121.         }
  122.         
  123.         $flag "nomodel";
  124.         $tmparr = array();
  125.         for ($i=0; $i count($arr)$i++{
  126.             if (!trim($arr[$i]))
  127.                 continue;
  128.             $rectype strtok($arr[$i]," ");
  129.             
  130.             // check if we have multi-model file
  131.             if ($rectype == "MODEL")
  132.                 continue;
  133.  
  134.             // did we get a multi-model file and are parsing the end
  135.             // of a model, if so, end parsing altogether
  136.             if ($rectype == "ENDMDL"{
  137.                 if ($multi{
  138.                     $this->macromolecules[$this->parseResidues($tmparr$full);
  139.                     $this->num_macromolecules++; // = count($this->macromolecules);
  140.                     $tmparr = array();
  141.                 else {
  142.                     break;
  143.                 }
  144.                 continue;
  145.             }
  146.  
  147.             // accumulate atom records, put the rest into the meta array
  148.             if ($rectype == "ATOM" || $rectype == "HETATM")
  149.                 $tmparr[$arr[$i];
  150.             elseif ($meta)
  151.                 $this->meta[$rectype][$arr[$i];
  152.         }
  153.         if (!empty($tmparr)) {
  154.             $this->macromolecules[$this->parseResidues($tmparr$full);
  155.             $this->num_macromolecules++; // = count($this->macromolecules);
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Makes the array of residues in the macromolecule
  161.      *
  162.      * @param   array   $records 
  163.      * @param   boolean $full   whether to store the full set of fields per atom
  164.      * @see     parseFile()
  165.      * @see     parseAtom()
  166.      */
  167.     function parseResidues($records$full{
  168.         $curr_res_id "";
  169.         $residues = array();
  170.         $res_atoms = array();
  171.         for ($i=0; $icount($records)$i++{
  172.             $atomrec =$records[$i];
  173.             $res_name trim(substr($atomrec,17,3));
  174.             $chain trim(substr($atomrec,21,1));
  175.             $seq_num = (int) trim(substr($atomrec,22,4));
  176.             $res_id $res_name.":".$seq_num.":".$chain;
  177.             
  178.             //if ($i == 0)
  179.               //  $curr_res_id = $res_id;
  180.  
  181.             if ($res_id == $curr_res_id{
  182.                 $res_atoms[$atomrec;
  183.                 if ($i != (count($records- 1))
  184.                     continue;
  185.             }
  186.  
  187.             if (($res_id != $curr_res_id|| 
  188.                 ($i == (count($records- 1))
  189.                 {
  190.                 if (!empty($res_atoms)) {
  191.                     for ($j=0; $j count($res_atoms)$j++{
  192.                         $temp $this->parseAtom($res_atoms[$j]$full$atomname);
  193.                         $residues[$curr_res_id][$atomname$temp;
  194.                     }
  195.                 }
  196.                 $curr_res_id $res_id;
  197.                 $res_atoms = array($atomrec);
  198.             }
  199.         }
  200.         return $residues;
  201.     }
  202.  
  203.     /**
  204.      * Parses an atom record into an associative array
  205.      *
  206.      * @param   string  $atomrec    PDB atom record
  207.      * @param   boolean $full   whether to store the full set of fields per atom
  208.      * @see     parseResidues()
  209.      */
  210.     function parseAtom($atomrec$full$atomname{
  211.         $atom = array();
  212.         // process PDB atom record
  213.         // no error checking, assumes correct and standard record
  214.         $atom["RecName"trim(substr($atomrec,0,6));
  215.         $atom["SerNum"= (int) trim(substr($atomrec,6,5));
  216.         $atom["AtomName"trim(substr($atomrec,12,4));
  217.         $atomname $atom["AtomName"];
  218.         if ($full{
  219.             $atom["AltLoc"trim(substr($atomrec,16,1));
  220.             $atom["ResName"trim(substr($atomrec,17,3));
  221.             $atom["ChainID"trim(substr($atomrec,21,1));
  222.             $atom["ResSeqNum"= (int) trim(substr($atomrec,22,4));
  223.             $atom["InsCode"trim(substr($atomrec,26,1));
  224.             $atom["Occupancy"= (float) trim(substr($atomrec,54,6));
  225.             $atom["TempFactor"= (float) trim(substr($atomrec,60,6));
  226.             $atom["SegmentID"trim(substr($atomrec,72,4));
  227.             $atom["Charge"= (float)trim(substr($atomrec,78,2));
  228.             $atom["Element"trim(substr($atomrec,76,2));
  229.         }
  230.         $atom["X"= (double) trim(substr($atomrec,30,8));
  231.         $atom["Y"= (double) trim(substr($atomrec,38,8));
  232.         $atom["Z"= (double) trim(substr($atomrec,46,8));
  233.         return $atom;
  234.     }
  235.  
  236.     /**
  237.      * Returns an array of residues with a particular name
  238.      * from the indicated macromolecule index
  239.      *
  240.      * @param   integer $macromol   Index of the macromolecule in the $macromolecules array
  241.      * @param   string  $resnam     Residue name, e.g. HIS, CYS, etc.
  242.      * @return  array   list of residues with the requested name
  243.      * @access  public
  244.      * @see     $macromolecules
  245.      */
  246.     function getResidueList ($macromol$resname{
  247.         $mol =$this->macromolecules[$macromol];
  248.         $reslist = array();
  249.         if (!$mol)
  250.             return $reslist;
  251.         foreach($mol as $resid=>$atoms{
  252.             list($curr_res_name,,explode(":",$resid);
  253.         //    echo $curr_res_name."***\n";
  254.             if ($curr_res_name == $resname)
  255.                 $reslist[$resid$atoms;
  256.             else
  257.                 continue;
  258.             
  259.         }
  260.         return $reslist;
  261.     }
  262.  
  263. // end of PDBParser
  264.  
  265. // vim: expandtab: ts=4: sw=4
  266. ?>

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