Skip to content
Browse files

Preparing for release of version 1.1.0

- Added toXYZ() methods to Atom, Molecule and Macromolecule
- Made toString() aliases to toXYZ()
- Improved the parsing algorithm of atoms in a residue/molecule
- Minor modifications for better handling of missing fields


git-svn-id: http://svn.php.net/repository/pear/packages/Science_Chemistry/trunk@126719 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent eef402c commit 434b3f37b0b80712cb570edc48cecc5183e6e3fd @jmcastagnetto jmcastagnetto committed May 13, 2003
View
13 Chemistry/Atom.php
@@ -140,11 +140,24 @@ function isAtom($obj) {
/**
* Returns a string representation of the Science_Chemistry_Atom object
+ * Alias of toXYZ()
*
* @return string
* @access public
+ * @see toXYZ()
*/
function toString() {
+ return $this->toXYZ();
+ }
+
+ /**
+ * Returns a XYZ representation of the Science_Chemistry_Atom object
+ *
+ * @return string
+ * @access public
+ * @see toString()
+ */
+ function toXYZ() {
if ($this->element && $this->xyz)
return sprintf("%2s",$this->element)." ".$this->xyz->toString();
}
View
3 Chemistry/Atom_PDB.php
@@ -182,7 +182,8 @@ function Science_Chemistry_Atom_PDB($atomrec, $residue="") {
$z = (double) trim(substr($atomrec,46,8));
$this->xyz = new Science_Chemistry_Coordinates(array($x, $y, $z));
$element = trim(substr($atomrec,76,2));
- $this->element = $element;
+ // if no element is present, use the atom_name
+ $this->element = (preg_match('/^[A-Z]{1,2}/', $element)) ? $element : $this->atom_name;
}
function getField($field) {
View
16 Chemistry/Macromolecule.php
@@ -146,15 +146,29 @@ function isMacromolecule($obj) {
*
* @return string
* @access public
+ * @see toString()
*/
- function toString() {
+ function toXYZ() {
$out = "# Number of molecules: ".$this->num_molecules."\n";
for ($i=0; $i < $this->num_molecules; $i++)
$out .= "# Molecule ".($i+1)."\n".$this->molecules[$i]->toString()."\n";
return $out;
}
/**
+ * Returns a string representation of the macromolecule
+ * as a multiple molecule XYZ-format file
+ * Alias of toXYZ()
+ *
+ * @return string
+ * @access public
+ * @see toString()
+ */
+ function toString() {
+ return $this->toXYZ();
+ }
+
+ /**
* Returns a CML representation of the molecule
* Accepts an optional id, and a flag to signal
* printing of the connection table
View
25 Chemistry/Macromolecule_PDB.php
@@ -58,27 +58,24 @@ function Science_Chemistry_Macromolecule_PDB($pdb, $records, $pdbfile="") {
* @see Science_Chemistry_Macromolecule_PDB()
*/
function parseResidues($records) {
- $curr_res_id = "";
+ $curr_res_id = '';
$res_atoms = array();
- for ($i=0; $i< count($records); $i++) {
- $atomrec =& $records[$i];
+ $nrecs = count($records);
+ for ($i=0; $i< $nrecs; $i++) {
+ $atomrec = $records[$i];
$res_name = trim(substr($atomrec,17,3));
$chain = trim(substr($atomrec,21,1));
$seq_num = (int) trim(substr($atomrec,22,4));
$res_id = $res_name.":".$seq_num.":".$chain;
+ $res_atoms[$res_id][] = $atomrec;
+ }
- if ($res_id == $curr_res_id) {
- $res_atoms[] = $atomrec;
- } else {
- if (!empty($res_atoms)) {
- $this->molecules[] = new Science_Chemistry_Residue_PDB(&$this->pdb,
- &$res_atoms, &$this);
- $this->num_molecules++;
- }
- $curr_res_id = $res_id;
- $res_atoms = array($atomrec);
- }
+ foreach ($res_atoms as $mol_id => $atoms_list) {
+ $this->molecules[] =& new Science_Chemistry_Residue_PDB(&$this->pdb,
+ &$atoms_list, &$this);
+ $this->num_molecules++;
}
+ return true;
}
} // end of Science_Chemistry_Macromolecule_PDB
View
20 Chemistry/Molecule.php
@@ -183,13 +183,25 @@ function isMolecule($obj) {
}
/**
- * Returns a string representation of the molecule
- * as a XYZ-format file
+ * Returns a string representation of the molecule as a XYZ-format file
+ * Alias of toXYZ()
*
* @return string
* @access public
+ * @see toXYZ()
*/
function toString() {
+ return $this->toXYZ();
+ }
+
+ /**
+ * Returns a string representation of the molecule as a XYZ-format file
+ *
+ * @return string
+ * @access public
+ * @see toString()
+ */
+ function toXYZ() {
if (!$this->atoms)
return false;
$out[] = $this->num_atoms;
@@ -308,7 +320,7 @@ function printDistanceMatrix() {
if(!$this->calcDistanceMatrix())
return false;
$dmat = &$this->dist_matrix;
- echo "Atom-Atom Distance Matrix:\n";
+ echo "# Atom-Atom Distance Matrix:\n";
for ($i=0; $i < $this->num_atoms; $i++)
echo "\t".($i+1);
for ($i=0; $i < $this->num_atoms; $i++) {
@@ -364,7 +376,7 @@ function printConnectionTable() {
if (empty($this->conn_table))
if (!$this->calcConnectionTable())
return false;
- printf("Connection Table: (cutoff = %.4f Angstroms)\n", $this->BONDCUTOFF);
+ printf("# Connection Table: (cutoff = %.4f Angstroms)\n", $this->BONDCUTOFF);
for ($i=0; $i < $this->num_atoms; $i++)
for ($j=($i+1); $j < $this->num_atoms; $j++)
if ($this->conn_table[$i][$j]) {
View
12 Chemistry/PDBFile.php
@@ -98,13 +98,13 @@ class Science_Chemistry_PDBFile {
* @see $file
* @see mkArrays()
*/
- function Science_Chemistry_PDBFile($filename) {
+ function Science_Chemistry_PDBFile($filename, $usemeta=false) {
if (!file_exists($filename))
return null;
list($pdb,) = explode(".",basename($filename));
$this->pdb = $pdb;
$this->file = realpath($filename);
- $this->parseFile(file($filename));
+ $this->parseFile(file($filename), $usemeta);
}
/**
@@ -114,7 +114,7 @@ function Science_Chemistry_PDBFile($filename) {
* @access private
* @see Science_Chemistry_Macromolecule_PDB()
*/
- function parseFile($arr) {
+ function parseFile($arr, $usemeta) {
$month = array (
"JAN" => "01", "FEB" => "02", "MAR" => "03",
"APR" => "04", "MAY" => "05", "JUN" => "06",
@@ -150,7 +150,11 @@ function parseFile($arr) {
// create the meta array and accumulate the atom records
if ($rectype != "ATOM" && $rectype != "HETATM") {
- //$this->meta[$rectype][] = trim($arr[$i]);
+ if ($usemeta) {
+ $this->meta[$rectype][] = trim($arr[$i]);
+ } else {
+ continue;
+ }
} else {
$tmparr[] = $arr[$i];
}
View
21 Chemistry/test/pdb2xyz.php
@@ -0,0 +1,21 @@
+<?php
+// Example of converting from PDB for XYZ
+set_time_limit(0);
+
+require_once "Science/Chemistry/PDBFile.php";
+$file = new Science_Chemistry_PDBFile($argv[1]);
+for ($j=0; $j < $file->num_macromolecules; $j++) {
+ $macromol =& $file->macromolecules[$j];
+ // use the builtin method for the macromolecule
+ echo $macromol->toXYZ();
+ // for more control on the printing
+ /*
+ foreach ($macromol->molecules as $mol) {
+ echo $mol->toXYZ();
+ $mol->printConnectionTable();
+ $mol->printDistanceMatrix();
+ }
+ */
+}
+
+?>

0 comments on commit 434b3f3

Please sign in to comment.
Something went wrong with that request. Please try again.