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

  1. #! /usr/local/bin/php -q
  2. <?php
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP Version 4                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2003 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/2_02.txt.                                 |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // Sample script that uses the Chemistry classes to convert a XYZ file to CML
  21. //
  22. // $Id: xyz2cml.php,v 1.3 2003/01/04 11:56:25 mj Exp $
  23.  
  24. require_once "Science/Chemistry.php";
  25. require_once "Science/Chemistry/Molecule_XYZ.php";
  26.  
  27. // save the old value of "track_errors"
  28. $oldtrack = ini_get("track_errors");
  29. // set it so $php_errormsg will be used
  30. ini_set("track_errors", 1);
  31.  
  32. // Use a local or the original DTD
  33. //$DTDPATH = "http://www.xml-cml.org/cml_10.dtd";
  34. // change the local path
  35. $DTDPATH = "/usr/local/dtd/cml.dtd";
  36.  
  37. function usage ($extra) {
  38.     global $argv;
  39.     echo $extra;
  40.     echo "Usage:\n\t".basename($argv[0])." xyzfile cmlfile [title] \n\n";
  41.     // restore old "track_errors" value
  42.     ini_set("track_errors", $oldtrack);
  43.     exit -1;
  44. }
  45.  
  46. if ($argc < 3)
  47.     usage("*ERROR* Wrong number of parameters\n");
  48.  
  49. // assume that any parameter after the cmlfile name 
  50. // is part of the title
  51. if ($argc > 3)
  52.     for ($i=3; $i < $argc; $i++)
  53.         $title .= $argv[$i]." ";
  54. else
  55.     $title = "molecule";
  56.  
  57. $date = date("Y-m-d H:i:s T");
  58. $title = trim($title);
  59. $xyz = realpath($argv[1]);
  60. $id = str_replace("/","_", $xyz);
  61. list($moltitle,) = explode(".",basename($xyz));
  62. $cml = $argv[2];
  63.  
  64. echo "Converting ".$argv[1]." to $cml... ";
  65. // create the molecule object
  66. $mol = new Science_Chemistry_Molecule_XYZ($xyz);
  67.  
  68. // prepare output
  69. $out = "<?xml version=\"1.0\">
  70. <!DOCTYPE document SYSTEM \"$DTDPATH\" [
  71. <!ELEMENT document ANY>
  72. <!ELEMENT cml ANY>
  73. <!ATTLIST cml
  74.     title CDATA #IMPLIED
  75.     id CDATA #IMPLIED
  76. >
  77. ]>
  78. <!-- 
  79.   -- CML document  
  80.   -- converted from: $xyz 
  81.   -- on: $date
  82.   -->\n";
  83. $out .= "<document>\n<cml title=\"$title\" id=\"$id\">\n";
  84. $out .= $mol->toCML($moltitle, $moltitle."1", true);
  85. $out .= "</cml>\n</document>";
  86.  
  87. // write the output
  88. $fp = @fopen($cml, "w");
  89. if ($fp) {
  90.     fwrite($fp, $out);
  91.     flush();
  92.     fclose($fp);
  93.     echo " finished!\n\n";
  94. } else {
  95.     usage("\n*ERROR* while writing to $cml\n--> $php_errormsg\n");
  96. }
  97.  
  98. // restore old "track_errors" value
  99. ini_set("track_errors", $oldtrack);
  100. ?>
  101.