PageRenderTime 36ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wwwfiles/admin/editdata.php

https://github.com/annulen/MolDB
PHP | 2749 lines | 2429 code | 191 blank | 129 comment | 442 complexity | ca2d3d8ecb2b91b928e09ce230c18800 MD5 | raw file
  1. <?php
  2. // admin/editdata.php Norbert Haider, University of Vienna, 2009-2010
  3. // part of MolDB5R last change: 2010-06-14
  4. $myname = $_SERVER['PHP_SELF'];
  5. @include("moldb5conf.php"); // if moldb5conf.php is in the PHP include path
  6. @include("../moldb5conf.php"); // if moldb5conf.php is where it should *not* be...
  7. require_once("../functions.php");
  8. require_once("../rxnfunct.php");
  9. require_once("dbfunct.php");
  10. $debug = 1; // 0: remain silent, higher values: be more verbose
  11. // odd numbers: output as HTML comments,
  12. // even numbers: output as clear-text messages
  13. if (config_quickcheck() > 0) { die(); }
  14. if (!isset($sitename) || ($sitename == "")) {
  15. $sitename = "MolDB5R demo";
  16. }
  17. $db_id = $_REQUEST['db'];
  18. $datatype = $_REQUEST['datatype'];
  19. $action = $_REQUEST['action'];
  20. $smiles = $_POST['smiles'];
  21. $jme = $_POST['jme'];
  22. $mol = $_POST['mol'];
  23. $ostype = getostype();
  24. $link = mysql_pconnect($hostname,"$rw_user", "$rw_password")
  25. or die("Could not connect to database server!");
  26. mysql_select_db($database)
  27. or die("Could not select database!");
  28. $db_id = check_db($db_id);
  29. if ($db_id < 0) {
  30. $db_id = $default_db;
  31. $db_id = check_db_all($db_id);
  32. if ($db_id < 0) {
  33. $db_id = get_lowestdbid();
  34. }
  35. }
  36. $mydb = get_dbproperties($db_id);
  37. $dbtype = $mydb['type'];
  38. if (($dbtype == 1) && ($datatype != 1)) { $datatype = 1; }
  39. if (($dbtype == 2) && ($datatype != 2)) { $datatype = 2; }
  40. $ip = $_SERVER['REMOTE_ADDR'];
  41. $trusted = is_trustedIP($ip);
  42. if ($trusted == false) {
  43. $trusted = is_db_trustedIP($db_id,$ip); // try if IP is trusted for this db
  44. }
  45. ?>
  46. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  47. <html>
  48. <head>
  49. <link href="../moldb.css" rel="stylesheet" type="text/css">
  50. <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  51. <meta name="author" content="Norbert Haider, University of Vienna">
  52. <title><?php echo "$sitename (administration page)"; ?></title>
  53. </head>
  54. <body>
  55. <?php
  56. echo "<!-- MolDB5R, OS type $ostype --!>\n";
  57. echo "<h1>$sitename: data input/editing</h1>\n";
  58. echo "On this page, you can add new MolDB5R entries and erase or edit existing ones.<br />\n";
  59. echo "<hr />\n";
  60. echo "<small>selected data collection: $db_id</small><p />\n";
  61. $dbprefix = $prefix . "db" . $db_id . "_";
  62. $moldatatable = $dbprefix . $moldatasuffix;
  63. $molfgbtable = $dbprefix . $molfgbsuffix;
  64. $molstattable = $dbprefix . $molstatsuffix;
  65. $molstructable = $dbprefix . $molstrucsuffix;
  66. $molcfptable = $dbprefix . $molcfpsuffix;
  67. $pic2dtable = $dbprefix . $pic2dsuffix;
  68. $rxnstructable = $dbprefix . $rxnstrucsuffix;
  69. $rxndatatable = $dbprefix . $rxndatasuffix;
  70. $rxnfgbtable = $dbprefix . $rxnfgbsuffix;
  71. $rxncfptable = $dbprefix . $rxncfpsuffix;
  72. $mydb = get_dbproperties($db_id);
  73. $db = $mydb['db_id'];
  74. $access = $mydb['access'];
  75. $usemem = $mydb['usemem'];
  76. $dbtype = $mydb['type'];
  77. if ($dbtype == 1) {
  78. $structable = $molstructable;
  79. $idname = "mol_id";
  80. $namename = "mol_name";
  81. } elseif ($dbtype == 2) {
  82. $structable = $rxnstructable;
  83. $idname = "rxn_id";
  84. $namename = "rxn_name";
  85. }
  86. if (($access < 2) && ($trusted == FALSE)) {
  87. echo "Your client IP is not authorized to perform the requested operation!<br />\n";
  88. //echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  89. echo "</body></html>\n";
  90. die();
  91. }
  92. if ($action == "addstruc1") {
  93. if ($dbtype == 1) { $what = "structure"; } elseif ($dbtype == 2) { $what = "reaction"; }
  94. echo "<h3>Add a new $what to data collection $db_id</h3>\n";
  95. echo "<table>\n<tr>\n<td valign=\"top\">\n";
  96. if ($dbtype == 1) {
  97. echo "<small>draw the molecule in the applet window....</small><br />\n";
  98. echo "<applet name=\"JME\" code=\"JME.class\" \n";
  99. echo "archive=\"../JME.jar\" width=\"350\" height=\"290\">\n";
  100. echo "<param name=\"jme\" value=\"$jme\">\n";
  101. //echo "<param name=\"options\" value=\"xbutton, query, hydrogens\">\n";
  102. echo "<param name=\"options\" value=\"xbutton, hydrogens, multipart\">\n";
  103. echo "You have to enable Java in your browser.\n";
  104. echo "</applet>\n";
  105. } elseif ($dbtype == 2) {
  106. echo "<small>draw the reaction in the applet window....</small><br />\n";
  107. echo "<applet name='JME' code='JME.class' \n";
  108. echo "archive='../JME.jar' width=450 height=288>\n";
  109. if (strlen($jme) > 0) {
  110. echo "<param name=\"jme\" value=\"$jme\">\n";
  111. }
  112. echo "<param name=\"options\" value=\"xbutton, hydrogens, reaction, multipart\">\n";
  113. echo "You have to enable Java in your browser.\n";
  114. echo "</applet>\n";
  115. }
  116. ?>
  117. <script>
  118. function check_ss() {
  119. var smiles = document.JME.smiles();
  120. var jme = document.JME.jmeFile();
  121. var mol = document.JME.molFile();
  122. if (smiles.length < 1) {
  123. alert("No molecule!");
  124. }
  125. else {
  126. document.form.smiles.value = smiles;
  127. document.form.jme.value = jme;
  128. document.form.mol.value = mol;
  129. document.form.submit();
  130. }
  131. }
  132. </script>
  133. <p />
  134. <form name="form" action="<?php echo $myname; ?>" method="post">
  135. <input type="button" value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Submit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" onClick="check_ss()">
  136. <input type="hidden" name="smiles">
  137. <input type="hidden" name="jme">
  138. <input type="hidden" name="mol">
  139. <input type="hidden" name="action" value="addstruc2">
  140. <input type="hidden" name="db" value="<?php echo "$db_id";?>">
  141. </form>
  142. <?php
  143. echo "</td>\n<td valign=\"top\">\n";
  144. if ($dbtype == 1) {
  145. echo "<small>...or paste it in MDL (V2000) molfile format into the text window</small><br />\n";
  146. } elseif ($dbtype == 2) {
  147. echo "<small>...or paste it in MDL (V2000) rxnfile format into the text window</small><br />\n";
  148. }
  149. echo "<form name=\"form2\" action=\"$myname\" method=\"post\">\n";
  150. echo "<textarea name=\"mol\" cols=\"70\" rows=\"17\"></textarea>\n";
  151. echo "<input type=\"hidden\" name=\"action\" value=\"addstruc2\">\n";
  152. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\"><p />\n";
  153. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Submit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\">\n";
  154. echo "</form>\n";
  155. echo "</td></tr>\n</table>\n";
  156. echo "<p /><a href=\"$myname?db=$db_id\">Continue without saving</a>\n";
  157. echo "</body></html>\n";
  158. die();
  159. }
  160. if ($action == "addstruc2") {
  161. if ($dbtype == 1) {
  162. $what = "structure";
  163. $reserved_mol_id = get_next_mol_id($db_id);
  164. } elseif ($dbtype == 2) {
  165. $what = "reaction";
  166. $reserved_mol_id = get_next_rxn_id($db_id);
  167. }
  168. // find out the next available mol_id
  169. // get next available mol_id
  170. // check if we got a valid molfile (either from JME or from the text area)
  171. if (is_validmol($mol) == FALSE) {
  172. echo "<h3>ERROR: this is not a valid input MDL (V2000) molfile!</h3>\n";
  173. echo "<pre>$safemol </pre>\n";
  174. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  175. echo "</body></html>\n";
  176. die();
  177. }
  178. // if in reaction mode, check if we have the rxn file header (if not, add it)
  179. if ($dbtype == 2) {
  180. if (strpos($mol, '$RXN') === FALSE) {
  181. $mol = "\$RXN\n\n\n\n 0 1\n\$MOL\n" . $mol;
  182. }
  183. }
  184. echo "<h3>Do you really want to add this $what to data collection $db_id?</h3>\n";
  185. // JME needs MDL molfiles with the "|" character instead of linebreaks
  186. $jmehitmol = strtr($mol,"\n","|");
  187. echo "<applet name=\"JME\" code=\"JME.class\" archive=\"../JME.jar\" \n";
  188. if ($dbtype == 1) {
  189. echo "width=\"250\" height=\"220\">";
  190. } elseif ($dbtype == 2) {
  191. echo "width=\"450\" height=\"220\">";
  192. }
  193. echo "<param name=\"options\" value=\"depict\"> \n";
  194. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  195. echo "</applet>\n";
  196. ?>
  197. <script>
  198. function submit1_ss() {
  199. var smiles = document.JME.smiles();
  200. var jme = document.JME.jmeFile();
  201. var mol = document.JME.molFile();
  202. if (smiles.length < 1) {
  203. alert("No molecule!");
  204. }
  205. else {
  206. document.addstruc1.smiles.value = smiles;
  207. document.addstruc1.jme.value = jme;
  208. document.addstruc1.mol.value = mol;
  209. document.addstruc1.submit();
  210. }
  211. }
  212. </script>
  213. <script>
  214. function submit2_ss() {
  215. var smiles = document.JME.smiles();
  216. var jme = document.JME.jmeFile();
  217. var mol = document.JME.molFile();
  218. if (smiles.length < 1) {
  219. alert("No molecule!");
  220. }
  221. else {
  222. document.addstruc2.smiles.value = smiles;
  223. document.addstruc2.jme.value = jme;
  224. document.addstruc2.mol.value = mol;
  225. document.addstruc2.submit();
  226. }
  227. }
  228. </script>
  229. <?php
  230. echo "<form name=\"addstruc1\" action=\"$myname\" method=\"post\">\n";
  231. echo "<input type=\"hidden\" name=\"action\" value=\"addstruc3\">\n";
  232. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  233. echo "<input type=\"hidden\" name=\"nextid\" value=\"$reserved_mol_id\">\n";
  234. echo "<input type=\"hidden\" name=\"mol\" value=\"$mol\">\n";
  235. echo "<input type=\"hidden\" name=\"jme\" value=\"$jme\">\n";
  236. echo "<input type=\"hidden\" name=\"smiles\" value=\"$smiles\">\n";
  237. // echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;Yes, add new structure!\">\n";
  238. echo "<input type=\"button\" value=\"&nbsp;Yes, add new structure!\" onClick=\"submit1_ss()\">\n";
  239. echo "</form><p />\n";
  240. echo "<form name=\"addstruc2\" action=\"$myname\" method=\"post\">\n";
  241. echo "<input type=\"hidden\" name=\"action\" value=\"addstruc1\">\n";
  242. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  243. echo "<input type=\"hidden\" name=\"mol\" value=\"$mol\">\n";
  244. echo "<input type=\"hidden\" name=\"jme\" value=\"$jme\">\n";
  245. echo "<input type=\"hidden\" name=\"smiles\" value=\"$smiles\">\n";
  246. // echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;No, edit again\">\n";
  247. echo "<input type=\"button\" value=\"&nbsp;No, edit again\" onClick=\"submit2_ss()\">\n";
  248. echo "</form><p />\n";
  249. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  250. echo "</body></html>\n";
  251. die();
  252. }
  253. if (($action == "addstruc3") && ($dbtype == 1)) { // structure input
  254. $errorcount = 0;
  255. $proposed_mol_id = $_POST['nextid'];
  256. if ($use_cmmmsrv == 'y') {
  257. /* create a TCP/IP socket */
  258. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  259. if ($socket < 0) {
  260. //echo "socket_create() failed.\nreason: " . socket_strerror ($socket) . "\n";
  261. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  262. $use_cmmmsrv = "n";
  263. }
  264. $sockresult = socket_connect ($socket, $cmmmsrv_addr, $cmmmsrv_port);
  265. if ($sockresult < 0) {
  266. //echo "socket_connect() failed.\nreason: ($sockresult) " . socket_strerror($sockresult) . "\n";
  267. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  268. $use_cmmmsrv = "n";
  269. }
  270. }
  271. if ($use_cmmmsrv == 'y') {
  272. $a = socket_read($socket, 250, PHP_NORMAL_READ);
  273. //echo "$a\n";
  274. $pos = strpos($a,"READY");
  275. if ($pos === false) {
  276. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  277. $use_cmmmsrv = "n";
  278. }
  279. $pos = 0;
  280. }
  281. // remove CR if present (IE, Mozilla et al.) and add it again (for Opera)
  282. $mol = str_replace("\r\n","\n",$mol);
  283. $mol = str_replace("\n","\r\n",$mol);
  284. //$safemol = escapeshellcmd($mol);
  285. $safemol = str_replace(";"," ",$mol);
  286. $safemol = str_replace("\""," ",$safemol);
  287. $safemol = str_replace("\'"," ",$safemol);
  288. $safemol = str_replace("\�"," ",$safemol);
  289. $safemol = str_replace("\`"," ",$safemol);
  290. $safemol = str_replace("\|"," ",$safemol);
  291. // first, tweak the molfile (if enabled in config file)
  292. if ($tweakmolfiles == "y") {
  293. if ($use_cmmmsrv == 'y') {
  294. $safemol = filterthroughcmmm($safemol,"#### checkmol:m");
  295. } else {
  296. if ($ostype == 1) {$safemol = filterthroughcmd($safemol,"$CHECKMOL -m - "); }
  297. if ($ostype == 2) {$safemol = filterthroughcmd2($safemol,"$CHECKMOL -m - "); }
  298. }
  299. }
  300. // check if we (still) have a valid MDL molfile
  301. if (is_validmol($safemol) == FALSE) {
  302. echo "<h3>ERROR: this is not a valid input MDL (V2000) molfile!</h3>\n";
  303. echo "<pre>$safemol </pre>\n";
  304. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  305. echo "</body></html>\n";
  306. die();
  307. }
  308. // next step: get the molecular statistics of the input structure
  309. // by piping it through the checkmol program
  310. //echo "adding this structure:\<br />\n<pre>$safemol</pre>\n";
  311. // get next available mol_id
  312. $result = mysql_query("SELECT COUNT(mol_id) AS molcount FROM $molstructable")
  313. or die("Query failed! (1c3)");
  314. $line = mysql_fetch_row($result);
  315. mysql_free_result($result);
  316. $molcount = $line[0];
  317. if ($molcount == 0) {
  318. $next_mol_id = 1;
  319. } else {
  320. $result = mysql_query("SELECT MAX(mol_id) AS molcount FROM $molstructable")
  321. or die("Query failed! (1c4)");
  322. $line = mysql_fetch_row($result);
  323. mysql_free_result($result);
  324. $molcount = $line[0];
  325. $next_mol_id = $molcount + 1;
  326. }
  327. if ($proposed_mol_id != $next_mol_id) {
  328. echo "This entry exists already!<br />\n";
  329. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  330. echo "</body></html>\n";
  331. die();
  332. }
  333. // add structure to molstruc
  334. $qstr = "INSERT INTO $molstructable (`mol_id`, `struc`) VALUES ($next_mol_id, \"$safemol\" )";
  335. if ($debug > 0) { debug_output("adding structure as no. $next_mol_id to data collection $db_id <br />\n"); }
  336. $result = mysql_query($qstr);
  337. $err = 0;
  338. $err = mysql_errno();
  339. if ($err != 0) {
  340. echo "<br />Action failed (#1/$err: " . mysql_error() . ")<br />\n";
  341. $errorcount++;
  342. } #else { echo "."; }
  343. if ($use_cmmmsrv == 'y') {
  344. $chkresult = filterthroughcmmm("$safemol", "#### checkmol:aXbH");
  345. } else {
  346. if ($ostype == 1) { $chkresult = filterthroughcmd("$safemol", "$CHECKMOL -aXbH - "); }
  347. if ($ostype == 2) {
  348. $safemol = str_replace("\n","\r\n",$safemol);
  349. $chkresult = filterthroughcmd2("$safemol", "$CHECKMOL -aXbH - ");
  350. }
  351. }
  352. #echo "<pre>$chkresult</pre>\n";
  353. if (strlen($chkresult) < 2) {
  354. echo "no response from checkmol (maybe a server configuration problem?)\n</body></html>\n";
  355. exit;
  356. }
  357. $cr = explode("\n", $chkresult);
  358. $molstat = trim($cr[0]);
  359. $molfgb = trim($cr[1]);
  360. $molfgb = str_replace(";",",",$molfgb);
  361. $molhfp = trim($cr[2]);
  362. $molhfp = str_replace(";",",",$molhfp);
  363. $qstr = "INSERT INTO $molstattable VALUES ( $next_mol_id,$molstat )";
  364. $result = mysql_query($qstr);
  365. $err = 0;
  366. $err = mysql_errno();
  367. if ($err != 0) {
  368. echo "<br />Action failed (#2/$err: " . mysql_error() . ")<br />\n";
  369. $errorcount++;
  370. } #else { echo "."; }
  371. $qstr = "INSERT INTO $molfgbtable VALUES ($next_mol_id,$molfgb )";
  372. $result = mysql_query($qstr);
  373. $err = 0;
  374. $err = mysql_errno();
  375. if ($err != 0) {
  376. echo "<br />Action failed (#3/$err: " . mysql_error() . ")<br />\n";
  377. $errorcount++;
  378. } #else { echo "."; }
  379. // get the fingerprint dictionary
  380. $fpdefqstr = "SELECT fp_id, fpdef FROM $fpdeftable;";
  381. $fpdefresult = mysql_query($fpdefqstr)
  382. or die("Could not get fingerprint definition!");
  383. $i = -1;
  384. $n_dict = 0;
  385. $fpdef = array();
  386. while ($fpdefline = mysql_fetch_array($fpdefresult, MYSQL_ASSOC)) {
  387. $i++;
  388. $n_dict++;
  389. $fpdef[$i] = $fpdefline["fpdef"];
  390. }
  391. mysql_free_result($fpdefresult);
  392. //create the dictionary-based fingerprints
  393. $moldfp = "";
  394. for ($k = 0; $k < $n_dict; $k++) {
  395. $dict = $fpdef[$k];
  396. if ($ostype == 1) { $cand = $safemol . "\n" . '$$$$' ."\n" . $dict; }
  397. if ($ostype == 2) {
  398. $dict = str_replace("\r","",$dict);
  399. $dict = str_replace("\n","\r\n",$dict);
  400. $cand = $safemol . "\r\n" . '$$$$' ."\r\n" . $dict;
  401. }
  402. if ($use_cmmmsrv == 'y') {
  403. $dfpstr = filterthroughcmmm($cand,"#### MATCHMOL:F");
  404. } else {
  405. $cand = str_replace("\$","\\\$",$cand);
  406. if ($ostype == 1) { $dfpstr = filterthroughcmd($cand,"$MATCHMOL -F - "); }
  407. if ($ostype == 2) { $dfpstr = filterthroughcmd2($cand,"$MATCHMOL -F - "); }
  408. }
  409. $dfpstr = trim($dfpstr);
  410. if ($k > 0) { $moldfp .= ","; }
  411. $moldfp .= " " . $dfpstr;
  412. } // for..
  413. //now insert dictionary-based and hash-based fingerprints into molcfptable
  414. $qstr = "INSERT INTO $molcfptable VALUES ($next_mol_id, $moldfp, $molhfp )";
  415. //echo "$qstr<br />\n";
  416. //echo "adding combined fingerprints for no. $next_mol_id to table $molcfptable.... ";
  417. $result = mysql_query($qstr);
  418. $err = 0;
  419. $err = mysql_errno();
  420. if ($err != 0) {
  421. echo "<br />Action failed (#4i/$err: " . mysql_error() . ")<br />\n";
  422. $errorcount++;
  423. } #else { echo "."; }
  424. // add new record to pic2dtable
  425. $qstr = "INSERT INTO $pic2dtable VALUES ($next_mol_id, \"1\", \"3\" )"; // 0 = does not exist, 1 = OK, 2 = OK, but do not show, 3 = to be created/updated, 4 = to be deleted
  426. //echo "$qstr<br />\n";
  427. //echo "adding 2D depiction information for no. $next_mol_id to table $pic2dtable.... ";
  428. $result = mysql_query($qstr);
  429. $err = 0;
  430. $err = mysql_errno();
  431. if ($err != 0) {
  432. echo "<br />Action failed (#5/$err: " . mysql_error() . ")<br />\n";
  433. $errorcount++;
  434. } #else { echo "."; }
  435. // add new record to moldatatable
  436. $qstr = "INSERT INTO $moldatatable (`mol_id`) VALUES (\"$next_mol_id\")";
  437. //echo "$qstr<br />\n";
  438. //echo "adding mol data for no. $next_mol_id to table $moldatatable.... ";
  439. $result = mysql_query($qstr);
  440. $err = 0;
  441. $err = mysql_errno();
  442. if ($err != 0) {
  443. echo "<br />Action failed (#6/$err: " . mysql_error() . ")<br />\n";
  444. $errorcount++;
  445. } #else { echo "."; }
  446. // MySQL syntax for table description, including comments:
  447. // SHOW FULL COLUMNS FROM db3_pic2d;
  448. // MySQL syntax for changing just a comment:
  449. // ALTER TABLE `db3_pic2d` CHANGE `status` `status` TINYINT( 4 ) NOT NULL DEFAULT '0'
  450. // COMMENT '0 = does not exist, 1 = OK, 2 = OK, but do not show, 3 = to be created/updated, 4 = to be deleted, 5 = in progress'
  451. // finished.....
  452. if ($use_cmmmsrv == 'y') {
  453. socket_write($socket,'#### bye');
  454. socket_close($socket);
  455. }
  456. // for now, disable use of memory-based tables
  457. set_memstatus_dirty($db_id);
  458. if ($debug > 0) { debug_output("Finished with $errorcount errors.<br />\n"); }
  459. if ($err == 0) {
  460. // JME needs MDL molfiles with the "|" character instead of linebreaks
  461. $jmehitmol = strtr($safemol,"\n","|");
  462. echo "<applet code=\"JME.class\" archive=\"../JME.jar\" \n";
  463. echo "width=\"250\" height=\"120\">";
  464. echo "<param name=\"options\" value=\"depict\"> \n";
  465. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  466. echo "</applet>\n";
  467. echo "<p />\n";
  468. mk_dataeditform($next_mol_id,1);
  469. } else {
  470. echo "something went wrong....<br />\n";
  471. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  472. }
  473. echo "</body></html>\n";
  474. die();
  475. } // action=addstruc3
  476. if (($action == "addstruc3") && ($dbtype == 2)) { // reaction input
  477. $errorcount = 0;
  478. $proposed_rxn_id = $_POST['nextid'];
  479. if (strpos($mol,"\$RXN") === FALSE) {
  480. echo "this is not a valid reaction file!\n";
  481. echo "</body></html>\n";
  482. die();
  483. }
  484. $errorcount = 0;
  485. $proposed_rxn_id = $_POST['nextid'];
  486. if ($use_cmmmsrv == 'y') {
  487. /* create a TCP/IP socket */
  488. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  489. if ($socket < 0) {
  490. //echo "socket_create() failed.\nreason: " . socket_strerror ($socket) . "\n";
  491. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  492. $use_cmmmsrv = "n";
  493. }
  494. $sockresult = socket_connect ($socket, $cmmmsrv_addr, $cmmmsrv_port);
  495. if ($sockresult < 0) {
  496. //echo "socket_connect() failed.\nreason: ($sockresult) " . socket_strerror($sockresult) . "\n";
  497. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  498. $use_cmmmsrv = "n";
  499. }
  500. }
  501. if ($use_cmmmsrv == 'y') {
  502. $a = socket_read($socket, 250, PHP_NORMAL_READ);
  503. //echo "$a\n";
  504. $pos = strpos($a,"READY");
  505. if ($pos === false) {
  506. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  507. $use_cmmmsrv = "n";
  508. }
  509. $pos = 0;
  510. }
  511. // remove CR if present (IE, Mozilla et al.) and add it again (for Opera)
  512. $mol = str_replace("\r\n","\n",$mol);
  513. $mol = str_replace("\n","\r\n",$mol);
  514. //$saferxn = escapeshellcmd($mol);
  515. $saferxn = str_replace(";"," ",$mol);
  516. $saferxn = str_replace("\""," ",$saferxn);
  517. $saferxn = str_replace("\'"," ",$saferxn);
  518. $saferxn = str_replace("\�"," ",$saferxn);
  519. $saferxn = str_replace("\`"," ",$saferxn);
  520. $saferxn = str_replace("\|"," ",$saferxn);
  521. $rxndescr = analyze_rxnfile($saferxn);
  522. $nrmol = get_nrmol($rxndescr);
  523. $npmol = get_npmol($rxndescr);
  524. //echo "there are $nrmol reactants and $npmol products<br>\n";
  525. if (($nrmol == 0) || ($npmol == 0)) {
  526. echo "incomplete reaction file!\n";
  527. echo "</body></html>\n";
  528. die();
  529. }
  530. $allmol = array();
  531. $rmol = array();
  532. $pmol = array();
  533. $label_list = array();
  534. $map_list = array();
  535. $n_labels = 0;
  536. $n_maps = 0;
  537. $mapstr = "";
  538. echo "<pre>\n";
  539. $allmol = explode("\$MOL\r\n",$saferxn);
  540. $header = $allmol[0];
  541. // get the fingerprint dictionary
  542. $fpdefqstr = "SELECT fp_id, fpdef FROM $fpdeftable;";
  543. $fpdefresult = mysql_query($fpdefqstr)
  544. or die("Could not get fingerprint definition!");
  545. $i = -1;
  546. $n_dict = 0;
  547. $fpdef = array();
  548. while ($fpdefline = mysql_fetch_array($fpdefresult, MYSQL_ASSOC)) {
  549. $i++;
  550. $n_dict++;
  551. $fpdef[$i] = $fpdefline["fpdef"];
  552. }
  553. mysql_free_result($fpdefresult);
  554. // get next available rxn_id
  555. $result = mysql_query("SELECT COUNT(rxn_id) AS molcount FROM $rxnstructable")
  556. or die("Query failed! (1c3a)");
  557. $line = mysql_fetch_row($result);
  558. mysql_free_result($result);
  559. $rxncount = $line[0];
  560. if ($rxncount == 0) {
  561. $next_rxn_id = 1;
  562. } else {
  563. $result = mysql_query("SELECT MAX(rxn_id) AS molcount FROM $rxnstructable")
  564. or die("Query failed! (1c4a)");
  565. $line = mysql_fetch_row($result);
  566. mysql_free_result($result);
  567. $rxncount = $line[0];
  568. $next_rxn_id = $rxncount + 1;
  569. }
  570. if ($proposed_rxn_id != $next_rxn_id) {
  571. echo "This entry exists already!<br />\n";
  572. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  573. echo "</body></html>\n";
  574. die();
  575. }
  576. $mapstr = get_maps($saferxn);
  577. if ($nrmol > 0) {
  578. $moldfpsum = "";
  579. $molhfpsum = "";
  580. $molfgbsum = "";
  581. for ($i = 0; $i < $nrmol; $i++) {
  582. $rmol[$i] = $allmol[($i+1)];
  583. $mnum = $i + 1;
  584. //echo "processing reactant no. $mnum ...";
  585. $labels = get_atomlabels($rmol[$i]);
  586. $mid = "r" . $mnum;
  587. if (strlen($labels) > 0) {
  588. add_labels($mid,$labels);
  589. }
  590. $safemol = $rmol[$i];
  591. // now tweak each molecule
  592. if ($tweakmolfiles == "y") {
  593. if ($use_cmmmsrv == 'y') {
  594. $safemol = filterthroughcmmm($safemol,"#### checkmol:m");
  595. } else {
  596. if ($ostype == 1) { $safemol = filterthroughcmd($safemol,"$CHECKMOL -m - "); }
  597. if ($ostype == 2) {
  598. $safemol = filterthroughcmd2($safemol,"$CHECKMOL -m - ");
  599. $safemol = str_replace("\n","\r\n",$safemol);
  600. }
  601. }
  602. $rmol[$i] = $safemol;
  603. } // end tweakmolefiles = y
  604. //create the dictionary-based fingerprints
  605. $moldfp = "";
  606. for ($k = 0; $k < $n_dict; $k++) {
  607. $dict = $fpdef[$k];
  608. if ($ostype == 1) { $cand = $safemol . "\n" . '$$$$' ."\n" . $dict; }
  609. if ($ostype == 2) {
  610. $dict = str_replace("\r","",$dict);
  611. $dict = str_replace("\n","\r\n",$dict);
  612. $cand = $safemol . "\r\n" . '$$$$' ."\r\n" . $dict;
  613. }
  614. if ($use_cmmmsrv == 'y') {
  615. $dfpstr = filterthroughcmmm($cand,"#### MATCHMOL:F");
  616. } else {
  617. $cand = str_replace("\$","\\\$",$cand);
  618. if ($ostype == 1) { $dfpstr = filterthroughcmd($cand,"$MATCHMOL -F - "); }
  619. if ($ostype == 2) { $dfpstr = filterthroughcmd2($cand,"$MATCHMOL -F - "); }
  620. }
  621. $dfpstr = trim($dfpstr);
  622. if ($k > 0) { $moldfp .= ","; }
  623. $moldfp .= $dfpstr;
  624. //echo "dictionary-based fingerprints for reactant $i + dictionary $k\n$dfpstr\n";
  625. } // for..
  626. //echo "moldfp: $moldfp\n";
  627. $moldfpsum = add_molfp($moldfpsum,$moldfp);
  628. // create the hash-based fingerprints
  629. if ($use_cmmmsrv == 'y') {
  630. $chkresult = filterthroughcmmm($safemol,"#### checkmol:bH");
  631. } else {
  632. if ($ostype == 1) {$chkresult = filterthroughcmd($safemol,"$CHECKMOL -bH - "); }
  633. if ($ostype == 2) {$chkresult = filterthroughcmd2($safemol,"$CHECKMOL -bH - "); }
  634. }
  635. if (strlen($chkresult) < 2) {
  636. echo "no response from checkmol (maybe a server configuration problem?)\n</body></html>\n";
  637. exit;
  638. }
  639. $cr = explode("\n", $chkresult);
  640. $molfgb = trim($cr[0]);
  641. $fgbarr = explode(";",$molfgb); // cut off the n1bits value
  642. $molfgb = $fgbarr[0];
  643. $molhfp = trim($cr[1]);
  644. $hfparr = explode(";",$molhfp); // cut off the n1bits value
  645. $molhfp = $hfparr[0];
  646. //echo "molhfp: $molhfp\n";
  647. $molhfpsum = add_molfp($molhfpsum,$molhfp);
  648. $molfgbsum = add_molfp($molfgbsum,$molfgb);
  649. } // end for ($i = 0; $i < $nrmol; $i++) ...
  650. //echo "added moldfp: $moldfpsum\n";
  651. //echo "added molhfp: $molhfpsum\n";
  652. // insert combined reaction fingerprints for reactant(s)
  653. $qstr = "INSERT INTO $rxncfptable VALUES ($next_rxn_id,'R',$moldfpsum,$molhfpsum,0)";
  654. //echo "adding combined fingerprints for no. $next_rxn_id to table $rxncfptable.... ";
  655. $result = mysql_query($qstr);
  656. $err = 0;
  657. $err = mysql_errno();
  658. if ($err != 0) {
  659. echo "<br />Action failed (#4a/$err: " . mysql_error() . ")<br />\n";
  660. $errorcount++;
  661. } // else { echo "done\n"; }
  662. // insert combined functional group bitstring for reactant(s)
  663. $qstr = "INSERT INTO $rxnfgbtable VALUES ($next_rxn_id,'R',$molfgbsum,0)";
  664. //echo "adding combined reactant functional group codes for no. $next_rxn_id to table $rxncfptable.... ";
  665. $result = mysql_query($qstr);
  666. $err = 0;
  667. $err = mysql_errno();
  668. if ($err != 0) {
  669. echo "<br />Action failed (#4b/$err: " . mysql_error() . ")<br />\n";
  670. $errorcount++;
  671. } // else { echo "done\n"; }
  672. }
  673. if ($npmol > 0) {
  674. $moldfpsum = "";
  675. $molhfpsum = "";
  676. $molfgbsum = "";
  677. for ($i = 0; $i < $npmol; $i++) {
  678. $pmol[$i] = $allmol[($i+1+$nrmol)];
  679. $mnum = $i + 1;
  680. //echo "processing product no. $mnum ...";
  681. $labels = get_atomlabels($pmol[$i]);
  682. $mid = "p" . $mnum;
  683. if (strlen($labels) > 0) {
  684. add_labels($mid,$labels);
  685. }
  686. $safemol = $pmol[$i];
  687. // tweak the molfile
  688. if ($tweakmolfiles == "y") {
  689. if ($use_cmmmsrv == 'y') {
  690. $safemol = filterthroughcmmm($safemol,"#### checkmol:m");
  691. } else {
  692. if ($ostype == 1) { $safemol = filterthroughcmd($safemol,"$CHECKMOL -m - "); }
  693. if ($ostype == 2) {
  694. $safemol = filterthroughcmd2($safemol,"$CHECKMOL -m - ");
  695. $safemol = str_replace("\n","\r\n",$safemol);
  696. }
  697. }
  698. $pmol[$i] = $safemol;
  699. } // end tweakmolefiles = y
  700. //create the dictionary-based fingerprints
  701. $moldfp = "";
  702. for ($k = 0; $k < $n_dict; $k++) {
  703. $dict = $fpdef[$k];
  704. if ($ostype == 1) { $cand = $safemol . "\n" . '$$$$' ."\n" . $dict; }
  705. if ($ostype == 2) {
  706. $dict = str_replace("\r","",$dict);
  707. $dict = str_replace("\n","\r\n",$dict);
  708. $cand = $safemol . "\r\n" . '$$$$' ."\r\n" . $dict;
  709. }
  710. if ($use_cmmmsrv == 'y') {
  711. $dfpstr = filterthroughcmmm($cand,"#### MATCHMOL:F");
  712. } else {
  713. $cand = str_replace("\$","\\\$",$cand);
  714. if ($ostype == 1) { $dfpstr = filterthroughcmd($cand,"$MATCHMOL -F - "); }
  715. if ($ostype == 2) { $dfpstr = filterthroughcmd2($cand,"$MATCHMOL -F - "); }
  716. }
  717. $dfpstr = trim($dfpstr);
  718. if ($k > 0) { $moldfp .= ","; }
  719. $moldfp .= $dfpstr;
  720. //echo "dictionary-based fingerprints for product $i + dictionary $k\n$dfpstr\n";
  721. } // for..
  722. //echo "moldfp: $moldfp\n";
  723. $moldfpsum = add_molfp($moldfpsum,$moldfp);
  724. // create the hash-based fingerprints
  725. if ($use_cmmmsrv == 'y') {
  726. $chkresult = filterthroughcmmm($safemol,"#### checkmol:bH");
  727. } else {
  728. if ($ostype == 1) {$chkresult = filterthroughcmd($safemol,"$CHECKMOL -bH - "); }
  729. if ($ostype == 2) {$chkresult = filterthroughcmd2($safemol,"$CHECKMOL -bH - "); }
  730. }
  731. if (strlen($chkresult) < 2) {
  732. echo "no response from checkmol (maybe a server configuration problem?)\n</body></html>\n";
  733. exit;
  734. }
  735. $cr = explode("\n", $chkresult);
  736. $molfgb = trim($cr[0]);
  737. $fgbarr = explode(";",$molfgb); // cut off the n1bits value
  738. $molfgb = $fgbarr[0];
  739. $molhfp = trim($cr[1]);
  740. $hfparr = explode(";",$molhfp); // cut off the n1bits value
  741. $molhfp = $hfparr[0];
  742. //echo "molhfp: $molhfp\n";
  743. $molhfpsum = add_molfp($molhfpsum,$molhfp);
  744. $molfgbsum = add_molfp($molfgbsum,$molfgb);
  745. } // end for ($i = 0; $i < $npmol; $i++) ...
  746. //echo "added moldfp: $moldfpsum\n";
  747. //echo "added molhfp: $molhfpsum\n";
  748. // insert combined reaction fingerprints for product(s)
  749. $qstr = "INSERT INTO $rxncfptable VALUES ($next_rxn_id,'P',$moldfpsum,$molhfpsum,0)";
  750. //echo "adding combined fingerprints for no. $next_rxn_id to table $rxncfptable.... ";
  751. $result = mysql_query($qstr);
  752. $err = 0;
  753. $err = mysql_errno();
  754. if ($err != 0) {
  755. echo "<br />Action failed (#4c/$err: " . mysql_error() . ")<br />\n";
  756. $errorcount++;
  757. } // else { echo "done"; }
  758. // insert combined functional group bitstring for product(s)
  759. $qstr = "INSERT INTO $rxnfgbtable VALUES ($next_rxn_id,'P',$molfgbsum,0)";
  760. //echo "adding combined product functional group codes for no. $next_rxn_id to table $rxncfptable.... ";
  761. $result = mysql_query($qstr);
  762. $err = 0;
  763. $err = mysql_errno();
  764. if ($err != 0) {
  765. echo "<br />Action failed (#4d/$err: " . mysql_error() . ")<br />\n";
  766. $errorcount++;
  767. } // else { echo "done\n"; }
  768. }
  769. // re-apply the reaction maps after tweaking (checkmol strips the atom labels)
  770. if ($tweakmolfiles == "y") {
  771. $saferxn = $header;
  772. for ($nm = 0; $nm < $nrmol; $nm++) {
  773. $saferxn .= "\$MOL\r\n" . $rmol[$nm];
  774. }
  775. for ($nm = 0; $nm < $npmol; $nm++) {
  776. $saferxn .= "\$MOL\r\n" . $pmol[$nm];
  777. }
  778. $saferxn = apply_maps($saferxn,$mapstr);
  779. }
  780. $qstr = "INSERT INTO $rxnstructable (`rxn_id`, `struc`, `map`) VALUES ($next_rxn_id, \"$saferxn\", \"$mapstr\" )";
  781. //echo "adding reaction as no. $next_rxn_id to data collection $db_id ...";
  782. //echo "<pre>$qstr</pre>\n";
  783. $result = mysql_query($qstr);
  784. $err = 0;
  785. $err = mysql_errno();
  786. if ($err != 0) {
  787. echo "<br />Action failed (#1/$err: " . mysql_error() . ")<br />\n";
  788. $errorcount++;
  789. } // else { echo "done\n"; }
  790. // add new record to rxndatatable
  791. $qstr = "INSERT INTO $rxndatatable (`rxn_id`) VALUES (\"$next_rxn_id\")";
  792. //echo "adding reaction data for no. $next_rxn_id ...";
  793. $result = mysql_query($qstr);
  794. $err = 0;
  795. $err = mysql_errno();
  796. if ($err != 0) {
  797. echo "<br />Action failed (#6/$err: " . mysql_error() . ")<br />\n";
  798. $errorcount++;
  799. } // else { echo "done\n"; }
  800. echo "</pre>\n";
  801. if ($debug > 0) { debug_output("Action finished with $errorcount errors.<br />\n"); }
  802. if ($err == 0) {
  803. // JME needs MDL molfiles with the "|" character instead of linebreaks
  804. $jmehitmol = strtr($saferxn,"\n","|");
  805. echo "<applet code=\"JME.class\" archive=\"../JME.jar\" \n";
  806. echo "width=\"250\" height=\"120\">";
  807. echo "<param name=\"options\" value=\"depict\"> \n";
  808. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  809. echo "</applet>\n";
  810. echo "<p />\n";
  811. mk_dataeditform($next_rxn_id,2);
  812. } else {
  813. echo "something went wrong....<br />\n";
  814. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  815. }
  816. echo "</body></html>\n";
  817. die();
  818. }
  819. if ($action == "savedata") {
  820. global $db_id;
  821. global $datatype;
  822. global $moldatatable;
  823. global $rxndatatable;
  824. $item_id = $_POST["id"];
  825. if ($datatype == 2) {
  826. $idname = "rxn_id";
  827. $datatable = $rxndatatable;
  828. } else {
  829. $idname = "mol_id";
  830. $datatable = $moldatatable;
  831. }
  832. echo "<h3>Saving data for entry no. $item_id in data collection $db_id</h3>\n";
  833. echo "<table bgcolor=\"#eeeeee\">\n";
  834. echo "<tr align=\"left\"><th>Field:</th><th>Value:</th></tr>\n";
  835. $i = 0;
  836. $qstr = "SHOW FULL COLUMNS FROM $datatable";
  837. $result = mysql_query($qstr)
  838. or die("Query failed! (savedata)");
  839. $updstr = "UPDATE $datatable SET ";
  840. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  841. $field = $line["Field"];
  842. $type = $line["Type"];
  843. $comment = $line["Comment"];
  844. if ($field != $idname) {
  845. $i++;
  846. $newval = ""; //preliminary....
  847. $f = "f" . $i;
  848. $newval = $_POST[$f];
  849. echo "<tr><td>$field</td><td>$newval</td></tr>\n";
  850. if ($i > 1) { $updstr .= ","; }
  851. $updstr .= " $field = \"$newval\"";
  852. } // if...
  853. } // while
  854. $updstr .= " WHERE $idname = $item_id";
  855. echo "</table>\n";
  856. $result = mysql_query($updstr)
  857. or die("Save data failed!");
  858. echo "<h3>Done.</h3>\n";
  859. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  860. echo "</body></html>\n";
  861. die();
  862. } // action = savedata
  863. if ($action == "duplcopy") {
  864. $item_id = $_REQUEST["id"];
  865. if ($datatype == 2) {
  866. $idname = "rxn_id";
  867. $datatable = $rxndatatable;
  868. $structable = $rxnstructable;
  869. $dtstr = "RD";
  870. $what = "reaction";
  871. } else {
  872. $idname = "mol_id";
  873. $datatable = $moldatatable;
  874. $structable = $molstructable;
  875. $dtstr = "SD";
  876. $what = "structure";
  877. }
  878. // sanity checks...
  879. if (!is_numeric($item_id)) {
  880. echo "invalid input!</body></html>";
  881. } else {
  882. $qstr = "SELECT COUNT($idname) AS itemcount FROM $structable WHERE $idname = $item_id";
  883. $result = mysql_query($qstr) or die("Query failed! (struc 1)");
  884. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  885. $itemcount = $line["itemcount"];
  886. }
  887. mysql_free_result($result);
  888. if ($itemcount < 1) {
  889. echo "<h2>Entry no. $item_id in data collection $db_id ($dtstr)</h2>\n<hr />\n";
  890. echo "This entry does not exist!<br />\n";
  891. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  892. echo "</body></html>\n";
  893. die();
  894. }
  895. echo "<h2>Entry no. $item_id in data collection $db_id ($dtstr)</h2>\n<hr />\n";
  896. $molstruc = ""; // can be MOL or RXN
  897. $qstr = "SELECT struc FROM $structable WHERE $idname = $item_id";
  898. $result = mysql_query($qstr) or die("Query failed! (struc 2)");
  899. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  900. $molstruc = $line["struc"];
  901. }
  902. mysql_free_result($result);
  903. if ($molstruc == "") {
  904. echo "empty structure<br />\n";
  905. } else {
  906. // JME needs MDL molfiles with the "|" character instead of linebreaks
  907. $jmehitmol = strtr($molstruc,"\n","|");
  908. if ($datatype == 1) {
  909. $what1 = "Structure:";
  910. $what2 = "structure";
  911. $jmewidth = 250;
  912. } elseif ($datatype == 2) {
  913. $what1 = "Reaction:";
  914. $what2 = "reaction";
  915. $jmewidth = 450;
  916. }
  917. echo "<h3>$what1</h3>\n";
  918. echo "<applet code=\"JME.class\" archive=\"../JME.jar\" \n";
  919. echo "width=\"$jmewidth\" height=\"120\">";
  920. echo "<param name=\"options\" value=\"depict\"> \n";
  921. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  922. echo "</applet>\n";
  923. }
  924. echo "<p />\n";
  925. // show data fields
  926. $result4 = mysql_query("SELECT * FROM $datatable WHERE $idname = $item_id")
  927. or die("Query failed! (duplcopy)");
  928. $y = mysql_num_fields($result4);
  929. echo "<table bgcolor=\"#eeeeee\">\n";
  930. while ($line4 = mysql_fetch_array($result4, MYSQL_BOTH)) {
  931. for ($x = 0; $x < $y; $x++) {
  932. $fieldname = mysql_field_name($result4, $x);
  933. //$fieldtype = mysql_field_type($result4, $x);
  934. if (($fieldname != $idname) && ($line4[$fieldname] != "")) {
  935. //echo "<b>$fieldname:</b> \t$line4[$fieldname] <br />\n";
  936. echo "<tr>\n";
  937. echo " <td><b>$fieldname</b></td><td>$line4[$fieldname]</td>\n";
  938. echo "</tr>\n";
  939. }
  940. }
  941. echo "<br />\n";
  942. }
  943. echo "</table>\n";
  944. echo "<p />\n<hr />\n";
  945. // and now the buttons for duplicate and copy
  946. echo "<form name=\"edform1\" action=\"$myname\" method=post>\n";
  947. echo "<input type=\"hidden\" name=\"action\" value=\"duplicate\">\n";
  948. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  949. echo "<input type=\"hidden\" name=\"datatype\" value=\"$datatype\">\n";
  950. echo "<input type=\"hidden\" name=\"id\" value=\"$item_id\">\n";
  951. echo "<input type=\"Submit\" name=\"select\" value=\"Duplicate\"> this $what + all data fields in current data collection ($db_id)\n";
  952. echo "</form><p />\n";
  953. // now, get number of available data collections
  954. $db_list = array();
  955. $n_db = 0;
  956. $qstr5 = "SELECT db_id FROM $metatable WHERE (type = $datatype) AND (db_id != $db_id) ORDER BY db_id";
  957. $result5 = mysql_query($qstr5)
  958. or die("Query failed! (duplcopy 2)");
  959. while ($line5 = mysql_fetch_array($result5, MYSQL_BOTH)) {
  960. $db_list[$n_db] = $line5["db_id"];
  961. $n_db++;
  962. }
  963. if ($n_db > 0) {
  964. echo "<form name=\"edform1\" action=\"$myname\" method=post>\n";
  965. echo "<input type=\"hidden\" name=\"action\" value=\"copy\">\n";
  966. echo "<input type=\"hidden\" name=\"sourcedb\" value=\"$db_id\">\n";
  967. echo "<input type=\"hidden\" name=\"datatype\" value=\"$datatype\">\n";
  968. echo "<input type=\"hidden\" name=\"id\" value=\"$item_id\">\n";
  969. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Copy&nbsp;&nbsp;&nbsp;\"> this $what + name to data collection ";
  970. echo "<select size=\"1\" name=\"db\">\n";
  971. for ($i = 0; $i < $n_db; $i++) {
  972. $db = $db_list[$i];
  973. echo "<option value=\"$db\"";
  974. //if ($db == $db_id) { echo " selected"; }
  975. echo ">$db</option>\n";
  976. }
  977. echo "</select>\n";
  978. echo "</form><p />\n";
  979. } else { echo "no other data collections available for copying..."; }
  980. } // end if is_numeric(item_id)
  981. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  982. echo "</body></html>\n";
  983. die();
  984. }
  985. if (($action == "editdata") || ($action == "duplicate") || ($action == "copy")) {
  986. $item_id = $_REQUEST["id"];
  987. $sourcedb_id = $db_id;
  988. $targetdb_id = $db_id;
  989. if ($datatype == 2) {
  990. $idname = "rxn_id";
  991. $datatable = $rxndatatable;
  992. $datasuffix = $rxndatasuffix;
  993. $structable = $rxnstructable;
  994. $strucsuffix = $rxnstrucsuffix;
  995. $dtstr = "RD";
  996. } else {
  997. $idname = "mol_id";
  998. $datatable = $moldatatable;
  999. $datasuffix = $moldatasuffix;
  1000. $structable = $molstructable;
  1001. $strucsuffix = $molstrucsuffix;
  1002. $dtstr = "SD";
  1003. }
  1004. // sanity checks...
  1005. if (!is_numeric($item_id)) {
  1006. echo "invalid input!</body></html>";
  1007. } else {
  1008. if ($action == "copy") {
  1009. $sourcedb_id = $_REQUEST['sourcedb'];
  1010. $db_id = $sourcedb_id;
  1011. //echo "db_id: $db_id<br>sourcedb_id: $sourcedb_id<br>targetdb_id: $targetdb_id<br>";
  1012. }
  1013. $structable = $prefix . "db" . $db_id . "_" . $strucsuffix;
  1014. $qstr = "SELECT COUNT($idname) AS itemcount FROM $structable WHERE $idname = $item_id";
  1015. $result = mysql_query($qstr) or die("Query failed! (struc 1)");
  1016. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  1017. $itemcount = $line["itemcount"];
  1018. }
  1019. mysql_free_result($result);
  1020. if ($itemcount < 1) {
  1021. echo "<h2>Entry no. $item_id in data collection $db_id ($dtstr)</h2>\n<hr />\n";
  1022. echo "This entry does not exist!<br />\n";
  1023. $db_id = $targetdb_id;
  1024. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1025. echo "</body></html>\n";
  1026. die();
  1027. }
  1028. if ($action == "duplicate") {
  1029. if ($datatype == 1) {
  1030. $new_item_id = copy_mol($db_id,$item_id,$db_id);
  1031. $item_id = $new_item_id;
  1032. } elseif ($datatype == 2) {
  1033. $new_item_id = copy_rxn($db_id,$item_id,$db_id);
  1034. $item_id = $new_item_id;
  1035. }
  1036. }
  1037. if ($action == "copy") {
  1038. if ($datatype == 1) {
  1039. $new_item_id = copy_mol($db_id,$item_id,$targetdb_id);
  1040. if ($new_item_id > 0) {
  1041. $item_id = $new_item_id;
  1042. $db_id = $targetdb_id;
  1043. }
  1044. } elseif ($datatype == 2) {
  1045. $new_item_id = copy_rxn($db_id,$item_id,$targetdb_id);
  1046. if ($new_item_id > 0) {
  1047. $item_id = $new_item_id;
  1048. $db_id = $targetdb_id;
  1049. }
  1050. }
  1051. $structable = $prefix . "db" . $db_id . "_" . $strucsuffix;
  1052. }
  1053. echo "<h2>Entry no. $item_id in data collection $db_id ($dtstr)</h2>\n<hr />\n";
  1054. $molstruc = ""; // can be MOL or RXN
  1055. $qstr = "SELECT struc FROM $structable WHERE $idname = $item_id";
  1056. $result = mysql_query($qstr) or die("Query failed! (struc 2)");
  1057. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  1058. $molstruc = $line["struc"];
  1059. }
  1060. mysql_free_result($result);
  1061. if ($molstruc == "") {
  1062. echo "empty structure<br />\n";
  1063. } else {
  1064. // JME needs MDL molfiles with the "|" character instead of linebreaks
  1065. $jmehitmol = strtr($molstruc,"\n","|");
  1066. if ($datatype == 1) {
  1067. $what1 = "Structure:";
  1068. $what2 = "structure";
  1069. $jmewidth = 250;
  1070. } elseif ($datatype == 2) {
  1071. $what1 = "Reaction:";
  1072. $what2 = "reaction";
  1073. $jmewidth = 450;
  1074. }
  1075. echo "<h3>$what1</h3>\n";
  1076. echo "<applet code=\"JME.class\" archive=\"../JME.jar\" \n";
  1077. echo "width=\"$jmewidth\" height=\"120\">";
  1078. echo "<param name=\"options\" value=\"depict\"> \n";
  1079. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  1080. echo "</applet>\n";
  1081. }
  1082. echo "<form name=\"strucform\" action=\"$myname\" method=\"post\">\n";
  1083. echo "<input type=\"hidden\" name=\"action\" value=\"editstruc\">\n";
  1084. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  1085. echo "<input type=\"hidden\" name=\"datatype\" value=\"$datatype\">\n";
  1086. echo "<input type=\"hidden\" name=\"id\" value=\"$item_id\">\n";
  1087. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Edit $what2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\">\n";
  1088. echo "</form>";
  1089. if ($datatype == 2) {
  1090. echo "<form name=\"strucform\" action=\"$myname\" method=\"post\">\n";
  1091. echo "<input type=\"hidden\" name=\"action\" value=\"extract\">\n";
  1092. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  1093. echo "<input type=\"hidden\" name=\"datatype\" value=\"$datatype\">\n";
  1094. echo "<input type=\"hidden\" name=\"id\" value=\"$item_id\">\n";
  1095. echo "<input type=\"Submit\" name=\"select\" value=\"Extract molecules\">\n";
  1096. echo "</form>";
  1097. }
  1098. if ($datatype == 1) {
  1099. echo "&nbsp;&nbsp;<small><a href=\"../showmol.php?mol=${item_id}&db=${db_id}&mode=txt\"";
  1100. echo " target=\"blank\">display molfile</a></small><p />\n";
  1101. } elseif ($datatype == 2) {
  1102. echo "&nbsp;&nbsp;<small><a href=\"../showmol.php?rxn=${item_id}&db=${db_id}&mode=txt\"";
  1103. echo " target=\"blank\">display rxnfile</a></small><p />\n";
  1104. }
  1105. echo "<p />\n<hr />\n";
  1106. mk_dataeditform($item_id,$datatype);
  1107. }
  1108. echo "<p /><a href=\"$myname?db=$sourcedb_id\">Continue without saving</a>\n";
  1109. echo "</body></html>\n";
  1110. die();
  1111. }
  1112. if (($action == "extract") && ($datatype == 2)) {
  1113. $item_id = $_REQUEST["id"];
  1114. $idname = "rxn_id";
  1115. $datatable = $rxndatatable;
  1116. $structable = $rxnstructable;
  1117. $dtstr = "RD";
  1118. // sanity checks...
  1119. if (!is_numeric($item_id)) {
  1120. echo "invalid input!</body></html>";
  1121. } else {
  1122. $qstr = "SELECT COUNT($idname) AS itemcount FROM $structable WHERE $idname = $item_id";
  1123. $result = mysql_query($qstr) or die("Query failed! (extract 1)");
  1124. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  1125. $itemcount = $line["itemcount"];
  1126. }
  1127. mysql_free_result($result);
  1128. if ($itemcount < 1) {
  1129. echo "<h2>Entry no. $item_id in data collection $db_id ($dtstr)</h2>\n<hr />\n";
  1130. echo "This entry does not exist!<br />\n";
  1131. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1132. echo "</body></html>\n";
  1133. die();
  1134. }
  1135. echo "<h2>Entry no. $item_id in data collection $db_id ($dtstr)</h2>\n<hr />\n";
  1136. $molstruc = ""; // can be MOL or RXN
  1137. $qstr = "SELECT struc FROM $structable WHERE $idname = $item_id";
  1138. $result = mysql_query($qstr) or die("Query failed! (extract 2)");
  1139. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  1140. $rxnstruc = $line["struc"];
  1141. }
  1142. mysql_free_result($result);
  1143. if ($rxnstruc == "") {
  1144. echo "empty reaction<br />\n";
  1145. } else {
  1146. // JME needs MDL molfiles with the "|" character instead of linebreaks
  1147. $jmehitmol = strtr($rxnstruc,"\n","|");
  1148. $what1 = "Reaction:";
  1149. $what2 = "reaction";
  1150. $jmewidth = 450;
  1151. echo "<h3>$what1</h3>\n";
  1152. echo "<applet code=\"JME.class\" archive=\"../JME.jar\" \n";
  1153. echo "width=\"$jmewidth\" height=\"120\">";
  1154. echo "<param name=\"options\" value=\"depict\"> \n";
  1155. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  1156. echo "</applet>\n";
  1157. }
  1158. echo "<hr />\n";
  1159. // get a list of all structure databases
  1160. $db_list = array();
  1161. $result1 = mysql_query("SELECT db_id FROM $metatable WHERE (type = 1) ORDER BY db_id")
  1162. or die("Query failed! (extract 3)");
  1163. $ndb = mysql_num_rows($result1);
  1164. $i = 0;
  1165. while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)) {
  1166. $db = $line["db_id"];
  1167. $db_list[$i] = $db;
  1168. $i++;
  1169. }
  1170. mysql_free_result($result1);
  1171. // now split the rxnfile into pieces
  1172. $rxnstruc = str_replace("\n","\r\n",$rxnstruc);
  1173. $rxnstruc = str_replace("\r\r\n","\r\n",$rxnstruc);
  1174. $rxnstruc = strip_labels($rxnstruc);
  1175. $rxndescr = analyze_rxnfile($rxnstruc);
  1176. $nrmol = get_nrmol($rxndescr);
  1177. $npmol = get_npmol($rxndescr);
  1178. $allmol = array();
  1179. $allmol = explode("\$MOL\r\n",$rxnstruc);
  1180. $header = $allmol[0];
  1181. if ($nrmol > 0) {
  1182. echo "<h3>Reactants:</h3>\n";
  1183. for ($i = 0; $i < $nrmol; $i++) {
  1184. $rmol[$i] = $allmol[($i+1)];
  1185. $mnum = $i + 1;
  1186. echo " reactant no. $mnum<br>\n";
  1187. $jmehitmol = strtr($rmol[$i],"\n","|");
  1188. echo "<table cellpadding=\"2\" cellspacing=\"2\" border=\"0\" width=\"100%\">\n";
  1189. echo "<tr align=\"left\">\n";
  1190. echo "<td width=\"20%\">\n";
  1191. echo "<applet code=\"JME.class\" archive=\"../JME.jar\" \n";
  1192. echo "width=\"250\" height=\"120\">";
  1193. echo "<param name=\"options\" value=\"depict\"> \n";
  1194. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  1195. echo "</applet>\n";
  1196. echo "</td>\n<td align=\"left\">\n";
  1197. echo "<br />\n";
  1198. if ($ndb > 0) {
  1199. echo "<form name=\"searchform\" action=\"../moldbsss.php\" method=\"post\">\n";
  1200. echo "<input type=\"hidden\" name=\"mode\" value=\"1\">\n";
  1201. echo "<input type=\"hidden\" name=\"mol\" value=\"$rmol[$i]\">\n";
  1202. //echo "<input type=\"hidden\" name=\"db\" value=\"2\">\n";
  1203. echo "<input type=\"Submit\" name=\"select\" value=\"Search\"> in data collection ";
  1204. echo "<select size=\"1\" name=\"db\">\n";
  1205. $j = 0;
  1206. foreach ($db_list as $db) {
  1207. echo "<option value=\"$db\"";
  1208. if ($j == 0) { echo " selected"; }
  1209. echo "> $db </option>\n";
  1210. $j++;
  1211. }
  1212. echo "</select>\n";
  1213. echo "</form>\n";
  1214. //echo "<pre>$rmol[$i]</pre>";
  1215. echo "<form name=\"copyform\" action=\"$myname\" method=\"post\">\n";
  1216. echo "<input type=\"hidden\" name=\"action\" value=\"addstruc2\">\n";
  1217. echo "<input type=\"hidden\" name=\"mol\" value=\"$rmol[$i]\">\n";
  1218. //echo "<input type=\"hidden\" name=\"db\" value=\"2\">\n";
  1219. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;&nbsp;Copy&nbsp;&nbsp;\"> to data collection ";
  1220. echo "<select size=\"1\" name=\"db\">\n";
  1221. $i = 0;
  1222. foreach ($db_list as $db) {
  1223. echo "<option value=\"$db\"";
  1224. if ($i == 0) { echo " selected"; }
  1225. echo "> $db </option>\n";
  1226. $i++;
  1227. }
  1228. echo "</select>\n";
  1229. echo "</form><br /></td>";
  1230. } // end if ndb > 0
  1231. echo "</tr>\n</table>\n";
  1232. }
  1233. echo "<hr />\n";
  1234. } // end if nrmol > 0
  1235. if ($npmol > 0) {
  1236. echo "<h3>Products:</h3>\n";
  1237. for ($i = 0; $i < $npmol; $i++) {
  1238. $pmol[$i] = $allmol[($i+1+$nrmol)];
  1239. $mnum = $i + 1;
  1240. echo " product no. $mnum<br>\n";
  1241. $jmehitmol = strtr($pmol[$i],"\n","|");
  1242. echo "<table cellpadding=\"2\" cellspacing=\"2\" border=\"0\" width=\"100%\">\n";
  1243. echo "<tr align=\"left\">\n";
  1244. echo "<td width=\"20%\">\n";
  1245. echo "<applet code=\"JME.class\" archive=\"../JME.jar\" \n";
  1246. echo "width=\"250\" height=\"120\">";
  1247. echo "<param name=\"options\" value=\"depict\"> \n";
  1248. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  1249. echo "</applet>\n";
  1250. echo "</td>\n<td align=\"left\">\n";
  1251. echo "<br />\n";
  1252. if ($ndb > 0) {
  1253. echo "<form name=\"searchform\" action=\"../moldbsss.php\" method=\"post\">\n";
  1254. echo "<input type=\"hidden\" name=\"mode\" value=\"1\">\n";
  1255. echo "<input type=\"hidden\" name=\"mol\" value=\"$pmol[$i]\">\n";
  1256. //echo "<input type=\"hidden\" name=\"db\" value=\"2\">\n";
  1257. echo "<input type=\"Submit\" name=\"select\" value=\"Search\"> in data collection ";
  1258. echo "<select size=\"1\" name=\"db\">\n";
  1259. $j = 0;
  1260. foreach ($db_list as $db) {
  1261. echo "<option value=\"$db\"";
  1262. if ($j == 0) { echo " selected"; }
  1263. echo "> $db </option>\n";
  1264. $j++;
  1265. }
  1266. echo "</select>\n";
  1267. echo "</form>\n";
  1268. echo "<form name=\"copyform\" action=\"$myname\" method=\"post\">\n";
  1269. echo "<input type=\"hidden\" name=\"action\" value=\"addstruc2\">\n";
  1270. echo "<input type=\"hidden\" name=\"mol\" value=\"$pmol[$i]\">\n";
  1271. //echo "<input type=\"hidden\" name=\"db\" value=\"2\">\n";
  1272. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;&nbsp;Copy&nbsp;&nbsp;\"> to data collection ";
  1273. echo "<select size=\"1\" name=\"db\">\n";
  1274. $i = 0;
  1275. foreach ($db_list as $db) {
  1276. echo "<option value=\"$db\"";
  1277. if ($i == 0) { echo " selected"; }
  1278. echo "> $db </option>\n";
  1279. $i++;
  1280. }
  1281. echo "</select>\n";
  1282. echo "</form><br /></td>";
  1283. }
  1284. echo "</tr>\n</table>\n";
  1285. }
  1286. echo "<hr />\n";
  1287. } // end if npmol > 0
  1288. }
  1289. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1290. echo "</body></html>\n";
  1291. die();
  1292. }
  1293. if ($action == "editstruc") {
  1294. $item_id = $_POST["id"];
  1295. if ($datatype == 1) {
  1296. $idname = "mol_id";
  1297. $structable = $molstructable;
  1298. $jmewidth = 350;
  1299. $jmeheigth = 288;
  1300. $jmeopt = "xbutton, hydrogens, multipart";
  1301. $btnlabel = "Save structure";
  1302. } elseif ($datatype == 2) {
  1303. $idname = "rxn_id";
  1304. $structable = $rxnstructable;
  1305. $jmewidth = 640;
  1306. $jmeheigth = 480;
  1307. $jmeopt = "xbutton, hydrogens, reaction, multipart";
  1308. $btnlabel = "Save reaction";
  1309. }
  1310. echo "<h2>Entry no. $item_id in data collection $db_id</h2>\n";
  1311. // sanity checks...
  1312. if (!is_numeric($item_id)) {
  1313. echo "invalid input!";
  1314. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1315. echo "</body></html>\n";
  1316. die();
  1317. } else {
  1318. $qstr = "SELECT COUNT($idname) AS itemcount FROM $structable WHERE $idname = $item_id";
  1319. $result = mysql_query($qstr) or die("Query failed! (struc 3)");
  1320. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  1321. $itemcount = $line["itemcount"];
  1322. }
  1323. mysql_free_result($result);
  1324. if ($itemcount < 1) {
  1325. echo "This entry does not exist!<br />\n";
  1326. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1327. echo "</body></html>\n";
  1328. die();
  1329. }
  1330. $molstruc = "";
  1331. $qstr = "SELECT struc FROM $structable WHERE $idname = $item_id";
  1332. $result = mysql_query($qstr) or die("Query failed! (struc 4)");
  1333. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  1334. $molstruc = $line["struc"];
  1335. }
  1336. mysql_free_result($result);
  1337. // JME needs MDL molfiles with the "|" character instead of linebreaks
  1338. $jmehitmol = strtr($molstruc,"\n","|");
  1339. echo "<applet name =\"JME\" code=\"JME.class\" archive=\"../JME.jar\" \n";
  1340. echo "width=\"$jmewidth\" height=\"$jmeheigth\">\n";
  1341. echo "<param name=\"options\" value=\"$jmeopt\"> \n";
  1342. if ($molstruc != "") {
  1343. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  1344. }
  1345. echo "</applet>\n";
  1346. ?>
  1347. <script>
  1348. function check_ss() {
  1349. var smiles = document.JME.smiles();
  1350. var jme = document.JME.jmeFile();
  1351. var mol = document.JME.molFile();
  1352. if (smiles.length < 1) {
  1353. alert("No molecule!");
  1354. }
  1355. else {
  1356. document.form.smiles.value = smiles;
  1357. document.form.jme.value = jme;
  1358. document.form.mol.value = mol;
  1359. document.form.submit();
  1360. }
  1361. }
  1362. </script>
  1363. <p />
  1364. <form name="form" action="<?php echo $myname; ?>" method="post">
  1365. <input type="button" value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo "$btnlabel"; ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" onClick="check_ss()">
  1366. <input type="hidden" name="smiles">
  1367. <input type="hidden" name="jme">
  1368. <input type="hidden" name="mol">
  1369. <input type="hidden" name="action" value="savestruc">
  1370. <input type="hidden" name="db" value="<?php echo "$db_id";?>">
  1371. <input type="hidden" name="datatype" value="<?php echo "$datatype";?>">
  1372. <input type="hidden" name="id" value="<?php echo "$item_id";?>">
  1373. </form>
  1374. <form name="cancel" action="<?php echo "$myname"; ?>" method="post">
  1375. <input type="hidden" name="smiles">
  1376. <input type="hidden" name="jme">
  1377. <input type="hidden" name="mol">
  1378. <input type="hidden" name="action" value="editdata">
  1379. <input type="hidden" name="db" value="<?php echo "$db_id";?>">
  1380. <input type="hidden" name="datatype" value="<?php echo "$datatype";?>">
  1381. <input type="hidden" name="id" value="<?php echo "$item_id";?>">
  1382. <input type="Submit" name="select" value="&nbsp;&nbsp;&nbsp;&nbsp;Cancel&nbsp;&nbsp;&nbsp;&nbsp;">
  1383. </form>
  1384. <?php
  1385. echo "<p />\n";
  1386. echo "</body></html>\n";
  1387. exit;
  1388. }
  1389. }
  1390. if (($action == "savestruc") && ($datatype == 1)) {
  1391. $errorcount = 0;
  1392. $mol_id = $_POST['id'];
  1393. if (!is_numeric($mol_id)) {
  1394. echo "invalid input!";
  1395. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1396. echo "</body></html>\n";
  1397. die();
  1398. }
  1399. $qstr = "SELECT COUNT(mol_id) AS molcount FROM $molstructable WHERE mol_id = $mol_id";
  1400. //echo "$qstr<br />\n";
  1401. $result = mysql_query($qstr) or die("Query failed! (struc 5)");
  1402. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  1403. $molcount = $line["molcount"];
  1404. }
  1405. mysql_free_result($result);
  1406. if ($molcount < 1) {
  1407. echo "This entry does not exist!<br />\n";
  1408. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1409. echo "</body></html>\n";
  1410. die();
  1411. }
  1412. if ($mol == "") {
  1413. echo "empty structure, nothing saved!<br />\n";
  1414. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1415. echo "</body></html>\n";
  1416. die();
  1417. }
  1418. if ($use_cmmmsrv == 'y') {
  1419. /* create a TCP/IP socket */
  1420. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  1421. if ($socket < 0) {
  1422. //echo "socket_create() failed.\nreason: " . socket_strerror ($socket) . "\n";
  1423. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  1424. $use_cmmmsrv = "n";
  1425. }
  1426. $sockresult = socket_connect ($socket, $cmmmsrv_addr, $cmmmsrv_port);
  1427. if ($sockresult < 0) {
  1428. //echo "socket_connect() failed.\nreason: ($sockresult) " . socket_strerror($sockresult) . "\n";
  1429. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  1430. $use_cmmmsrv = "n";
  1431. }
  1432. }
  1433. if ($use_cmmmsrv == 'y') {
  1434. $a = socket_read($socket, 250, PHP_NORMAL_READ);
  1435. //echo "$a\n";
  1436. $pos = strpos($a,"READY");
  1437. if ($pos === false) {
  1438. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  1439. $use_cmmmsrv = "n";
  1440. }
  1441. $pos = 0;
  1442. }
  1443. $safemol = str_replace(";"," ",$mol);
  1444. $safemol = str_replace("\""," ",$safemol);
  1445. $safemol = str_replace("\'"," ",$safemol);
  1446. $safemol = str_replace("\�"," ",$safemol);
  1447. $safemol = str_replace("\`"," ",$safemol);
  1448. $safemol = str_replace("\|"," ",$safemol);
  1449. // first, tweak the molfile
  1450. if ($use_cmmmsrv == 'y') {
  1451. $safemol = filterthroughcmmm($safemol,"#### checkmol:m");
  1452. } else {
  1453. if ($ostype == 1) { $safemol = filterthroughcmd($safemol,"$CHECKMOL -m - "); }
  1454. if ($ostype == 2) {
  1455. $safemol = filterthroughcmd2($safemol,"$CHECKMOL -m - ");
  1456. #$safemol = str_replace("\n","\r\n",$safemol);
  1457. }
  1458. }
  1459. // next step: get the molecular statistics of the input structure
  1460. // by piping it through the checkmol program, then do a search
  1461. // in molstat and the fingerprint table(s) ==> this gives a list of candidates
  1462. // update structure in molstruc
  1463. $qstr = "UPDATE $molstructable SET struc =\"$safemol\" WHERE mol_id = $mol_id";
  1464. echo "<h2>Entry no. $mol_id in data collection $db_id</h2><hr />\n";
  1465. //echo "<small>updating structure no. $mol_id in data collection $db_id ";
  1466. $result = mysql_query($qstr);
  1467. $err = 0;
  1468. $err = mysql_errno();
  1469. if ($err != 0) {
  1470. echo "<br />Action failed (#7/$err: " . mysql_error() . ")<br />\n";
  1471. $errorcount++;
  1472. } #else { echo "."; }
  1473. if ($use_cmmmsrv == 'y') {
  1474. $chkresult = filterthroughcmmm("$safemol", "#### checkmol:aXbH");
  1475. } else {
  1476. if ($ostype == 1) { $chkresult = filterthroughcmd("$safemol", "$CHECKMOL -aXbH - "); }
  1477. if ($ostype == 2) {
  1478. $safemol = str_replace("\n","\r\n",$safemol);
  1479. $chkresult = filterthroughcmd2("$safemol", "$CHECKMOL -aXbH - ");
  1480. }
  1481. }
  1482. //echo "<pre>$chkresult</pre>\n";
  1483. if (strlen($chkresult) < 2) {
  1484. echo "no response from checkmol (maybe a server configuration problem?)\n</body></html>\n";
  1485. exit;
  1486. }
  1487. $cr = explode("\n", $chkresult);
  1488. $molstat = trim($cr[0]);
  1489. $molfgb = trim($cr[1]);
  1490. $molfgb = str_replace(";",",",$molfgb);
  1491. $molhfp = trim($cr[2]);
  1492. $molhfp = str_replace(";",",",$molhfp);
  1493. //echo "molstat ($next_mol_id): $molstat<br />\n";
  1494. //echo "molfgb ($next_mol_id): $molfgb<br />\n";
  1495. //echo "molhfp ($next_mol_id): $molhfp<br />\n";
  1496. // delete old molstat record first
  1497. $qstr = "DELETE FROM $molstattable WHERE mol_id = $mol_id";
  1498. $result = mysql_query($qstr);
  1499. $err = mysql_errno();
  1500. if ($err != 0) {
  1501. echo "<br />Action failed (#8/$err: " . mysql_error() . ")<br />\n";
  1502. $errorcount++;
  1503. } #else { echo "."; }
  1504. // add new molstat record
  1505. $qstr = "INSERT INTO $molstattable VALUES ( $mol_id,$molstat )";
  1506. $result = mysql_query($qstr);
  1507. $err = mysql_errno();
  1508. if ($err != 0) {
  1509. echo "<br />Action failed (#9/$err: " . mysql_error() . ")<br />\n";
  1510. $errorcount++;
  1511. } #else { echo "."; }
  1512. // delete old molfgb record first
  1513. $qstr = "DELETE FROM $molfgbtable WHERE mol_id = $mol_id";
  1514. $result = mysql_query($qstr);
  1515. $err = mysql_errno();
  1516. if ($err != 0) {
  1517. echo "<br />Action failed (#10/$err: " . mysql_error() . ")<br />\n";
  1518. $errorcount++;
  1519. } #else { echo "."; }
  1520. // add new molfgb record
  1521. $qstr = "INSERT INTO $molfgbtable VALUES ($mol_id,$molfgb )";
  1522. $result = mysql_query($qstr);
  1523. $err = mysql_errno();
  1524. if ($err != 0) {
  1525. echo "<br />Action failed (#11/$err: " . mysql_error() . ")<br />\n";
  1526. $errorcount++;
  1527. } #else { echo "."; }
  1528. // delete old molcfp record first
  1529. $qstr = "DELETE FROM $molcfptable WHERE mol_id = $mol_id";
  1530. $result = mysql_query($qstr);
  1531. $err = mysql_errno();
  1532. if ($err != 0) {
  1533. echo "<br />Action failed (#12/$err: " . mysql_error() . ")<br />\n";
  1534. $errorcount++;
  1535. } #else { echo "."; }
  1536. // add new molcfp record
  1537. // get the fingerprint dictionary
  1538. $fpdefqstr = "SELECT fp_id, fpdef FROM $fpdeftable;";
  1539. $fpdefresult = mysql_query($fpdefqstr)
  1540. or die("Could not get fingerprint definition!");
  1541. $i = -1;
  1542. $n_dict = 0;
  1543. $fpdef = array();
  1544. while ($fpdefline = mysql_fetch_array($fpdefresult, MYSQL_ASSOC)) {
  1545. $i++;
  1546. $n_dict++;
  1547. $fpdef[$i] = $fpdefline["fpdef"];
  1548. }
  1549. mysql_free_result($fpdefresult);
  1550. //create the dictionary-based fingerprints
  1551. $moldfp = "";
  1552. for ($k = 0; $k < $n_dict; $k++) {
  1553. $dict = $fpdef[$k];
  1554. if ($ostype == 1) { $cand = $safemol . "\n" . '$$$$' ."\n" . $dict; }
  1555. if ($ostype == 2) {
  1556. $dict = str_replace("\r","",$dict);
  1557. $dict = str_replace("\n","\r\n",$dict);
  1558. $cand = $safemol . "\r\n" . '$$$$' ."\r\n" . $dict;
  1559. }
  1560. if ($use_cmmmsrv == 'y') {
  1561. $dfpstr = filterthroughcmmm($cand,"#### MATCHMOL:F");
  1562. } else {
  1563. $cand = str_replace("\$","\\\$",$cand);
  1564. if ($ostype == 1) { $dfpstr = filterthroughcmd($cand,"$MATCHMOL -F - "); }
  1565. if ($ostype == 2) { $dfpstr = filterthroughcmd2($cand,"$MATCHMOL -F - "); }
  1566. }
  1567. $dfpstr = trim($dfpstr);
  1568. if ($k > 0) { $moldfp .= ","; }
  1569. $moldfp .= " " . $dfpstr;
  1570. } // for..
  1571. //now insert dictionary-based and hash-based fingerprints into molcfptable
  1572. $qstr = "INSERT INTO $molcfptable VALUES ($mol_id, $moldfp, $molhfp )";
  1573. //echo "adding combined fingerprints for no. $mol_id to table $molcfptable.... ";
  1574. $result = mysql_query($qstr);
  1575. $err = 0;
  1576. $err = mysql_errno();
  1577. if ($err != 0) {
  1578. echo "<br />Action failed (#13/$err: " . mysql_error() . ")<br />\n";
  1579. $errorcount++;
  1580. } #else { echo "."; }
  1581. // update record in pic2dtable
  1582. $qstr = "UPDATE $pic2dtable SET status = \"3\" WHERE mol_id = $mol_id"; // 0 = does not exist, 1 = OK, 2 = OK, but do not show, 3 = to be created/updated, 4 = to be deleted
  1583. $result = mysql_query($qstr);
  1584. $err = mysql_errno();
  1585. if ($err != 0) {
  1586. echo "<br />Action failed (#14/$err: " . mysql_error() . ")<br />\n";
  1587. $errorcount++;
  1588. } #else { echo "."; }
  1589. // finished.....
  1590. if ($use_cmmmsrv == 'y') {
  1591. socket_write($socket,'#### bye');
  1592. socket_close($socket);
  1593. }
  1594. if ($debug > 0) { debug_output("Action finished with $errorcount errors.<br />\n"); }
  1595. if ($err == 0) {
  1596. echo "<h3>Structure:</h3>\n";
  1597. // JME needs MDL molfiles with the "|" character instead of linebreaks
  1598. $jmehitmol = strtr($safemol,"\n","|");
  1599. echo "<applet code=\"JME.class\" archive=\"../JME.jar\" \n";
  1600. echo "width=\"250\" height=\"120\">";
  1601. echo "<param name=\"options\" value=\"depict\"> \n";
  1602. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  1603. echo "</applet>\n";
  1604. echo "<p />\n";
  1605. echo "<form name=\"strucform\" action=\"$myname\" method=\"post\">\n";
  1606. echo "<input type=\"hidden\" name=\"action\" value=\"editstruc\">\n";
  1607. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  1608. echo "<input type=\"hidden\" name=\"datatype\" value=\"1\">\n";
  1609. echo "<input type=\"hidden\" name=\"id\" value=\"$mol_id\">\n";
  1610. echo "<input type=\"Submit\" name=\"select\" value=\"Edit structure\">\n";
  1611. echo "</form><p />\n";
  1612. echo "<p />\n<hr />\n";
  1613. mk_dataeditform($mol_id,1);
  1614. } else {
  1615. echo "something went wrong....<br />\n";
  1616. }
  1617. set_memstatus_dirty($db_id);
  1618. echo "<p /><a href=\"$myname?db=$db_id\">Continue without saving</a>\n";
  1619. echo "</body></html>\n";
  1620. die();
  1621. }
  1622. if (($action == "savestruc") && ($datatype == 2)) {
  1623. $errorcount = 0;
  1624. $rxn_id = $_POST['id'];
  1625. if (!is_numeric($rxn_id)) {
  1626. echo "invalid input!";
  1627. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1628. echo "</body></html>\n";
  1629. die();
  1630. }
  1631. $qstr = "SELECT COUNT(rxn_id) AS rxncount FROM $rxnstructable WHERE rxn_id = $rxn_id";
  1632. $result = mysql_query($qstr) or die("Query failed! (struc 6)");
  1633. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  1634. $rxncount = $line["rxncount"];
  1635. }
  1636. mysql_free_result($result);
  1637. if ($rxncount < 1) {
  1638. echo "This entry does not exist!<br />\n";
  1639. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1640. echo "</body></html>\n";
  1641. die();
  1642. }
  1643. if ($mol == "") {
  1644. echo "empty reaction, nothing saved!<br />\n";
  1645. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1646. echo "</body></html>\n";
  1647. die();
  1648. }
  1649. if (strpos($mol,"\$RXN") === FALSE) {
  1650. echo "this is not a valid reaction file!\n";
  1651. echo "</body></html>\n";
  1652. die();
  1653. }
  1654. if ($use_cmmmsrv == 'y') {
  1655. /* create a TCP/IP socket */
  1656. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  1657. if ($socket < 0) {
  1658. //echo "socket_create() failed.\nreason: " . socket_strerror ($socket) . "\n";
  1659. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  1660. $use_cmmmsrv = "n";
  1661. }
  1662. $sockresult = socket_connect ($socket, $cmmmsrv_addr, $cmmmsrv_port);
  1663. if ($sockresult < 0) {
  1664. //echo "socket_connect() failed.\nreason: ($sockresult) " . socket_strerror($sockresult) . "\n";
  1665. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  1666. $use_cmmmsrv = "n";
  1667. }
  1668. }
  1669. if ($use_cmmmsrv == 'y') {
  1670. $a = socket_read($socket, 250, PHP_NORMAL_READ);
  1671. //echo "$a\n";
  1672. $pos = strpos($a,"READY");
  1673. if ($pos === false) {
  1674. echo "<!-- could not connect to cmmmsrv - reverting to checkmol/matchmol --!>\n";
  1675. $use_cmmmsrv = "n";
  1676. }
  1677. $pos = 0;
  1678. }
  1679. // remove CR if present (IE, Mozilla et al.) and add it again (for Opera)
  1680. $mol = str_replace("\r\n","\n",$mol);
  1681. $mol = str_replace("\n","\r\n",$mol);
  1682. //$saferxn = escapeshellcmd($mol);
  1683. $saferxn = str_replace(";"," ",$mol);
  1684. $saferxn = str_replace("\""," ",$saferxn);
  1685. $saferxn = str_replace("\'"," ",$saferxn);
  1686. $saferxn = str_replace("\�"," ",$saferxn);
  1687. $saferxn = str_replace("\`"," ",$saferxn);
  1688. $saferxn = str_replace("\|"," ",$saferxn);
  1689. $rxndescr = analyze_rxnfile($saferxn);
  1690. $nrmol = get_nrmol($rxndescr);
  1691. $npmol = get_npmol($rxndescr);
  1692. //echo "there are $nrmol reactants and $npmol products<br>\n";
  1693. if (($nrmol == 0) || ($npmol == 0)) {
  1694. echo "incomplete reaction file!\n";
  1695. echo "</body></html>\n";
  1696. die();
  1697. }
  1698. $allmol = array();
  1699. $rmol = array();
  1700. $pmol = array();
  1701. $label_list = array();
  1702. $map_list = array();
  1703. $n_labels = 0;
  1704. $n_maps = 0;
  1705. $mapstr = "";
  1706. $allmol = explode("\$MOL\r\n",$saferxn);
  1707. $header = $allmol[0];
  1708. // get the fingerprint dictionary
  1709. $fpdefqstr = "SELECT fp_id, fpdef FROM $fpdeftable;";
  1710. $fpdefresult = mysql_query($fpdefqstr)
  1711. or die("Could not get fingerprint definition!");
  1712. $i = -1;
  1713. $n_dict = 0;
  1714. $fpdef = array();
  1715. while ($fpdefline = mysql_fetch_array($fpdefresult, MYSQL_ASSOC)) {
  1716. $i++;
  1717. $n_dict++;
  1718. $fpdef[$i] = $fpdefline["fpdef"];
  1719. }
  1720. mysql_free_result($fpdefresult);
  1721. $mapstr = get_maps($saferxn);
  1722. // delete old rxnfgb record first
  1723. $qstr = "DELETE FROM $rxnfgbtable WHERE rxn_id = $rxn_id";
  1724. $result = mysql_query($qstr);
  1725. $err = mysql_errno();
  1726. if ($err != 0) {
  1727. echo "<br />Action failed (#11/$err: " . mysql_error() . ")<br />\n";
  1728. $errorcount++;
  1729. } #else { echo "."; }
  1730. // delete old rxncfp record first
  1731. $qstr = "DELETE FROM $rxncfptable WHERE rxn_id = $rxn_id";
  1732. $result = mysql_query($qstr);
  1733. $err = mysql_errno();
  1734. if ($err != 0) {
  1735. echo "<br />Action failed (#12/$err: " . mysql_error() . ")<br />\n";
  1736. $errorcount++;
  1737. } #else { echo "done\n"; }
  1738. if ($nrmol > 0) {
  1739. $moldfpsum = "";
  1740. $molhfpsum = "";
  1741. $molfgbsum = "";
  1742. for ($i = 0; $i < $nrmol; $i++) {
  1743. $rmol[$i] = $allmol[($i+1)];
  1744. $mnum = $i + 1;
  1745. #echo "processing reactant no. $mnum ...";
  1746. $labels = get_atomlabels($rmol[$i]);
  1747. $mid = "r" . $mnum;
  1748. if (strlen($labels) > 0) {
  1749. add_labels($mid,$labels);
  1750. }
  1751. $safemol = $rmol[$i];
  1752. // now tweak each molecule
  1753. if ($tweakmolfiles == "y") {
  1754. if ($use_cmmmsrv == 'y') {
  1755. $safemol = filterthroughcmmm($safemol,"#### checkmol:m");
  1756. } else {
  1757. if ($ostype == 1) { $safemol = filterthroughcmd($safemol,"$CHECKMOL -m - "); }
  1758. if ($ostype == 2) {
  1759. $safemol = filterthroughcmd2($safemol,"$CHECKMOL -m - ");
  1760. $safemol = str_replace("\n","\r\n",$safemol);
  1761. }
  1762. }
  1763. $rmol[$i] = $safemol;
  1764. } // end tweakmolefiles = y
  1765. //create the dictionary-based fingerprints
  1766. $moldfp = "";
  1767. for ($k = 0; $k < $n_dict; $k++) {
  1768. $dict = $fpdef[$k];
  1769. if ($ostype == 1) { $cand = $safemol . "\n" . '$$$$' ."\n" . $dict; }
  1770. if ($ostype == 2) {
  1771. $dict = str_replace("\r","",$dict);
  1772. $dict = str_replace("\n","\r\n",$dict);
  1773. $cand = $safemol . "\r\n" . '$$$$' ."\r\n" . $dict;
  1774. }
  1775. if ($use_cmmmsrv == 'y') {
  1776. $dfpstr = filterthroughcmmm($cand,"#### MATCHMOL:F");
  1777. } else {
  1778. $cand = str_replace("\$","\\\$",$cand);
  1779. if ($ostype == 1) { $dfpstr = filterthroughcmd($cand,"$MATCHMOL -F - "); }
  1780. if ($ostype == 2) { $dfpstr = filterthroughcmd2($cand,"$MATCHMOL -F - "); }
  1781. }
  1782. $dfpstr = trim($dfpstr);
  1783. if ($k > 0) { $moldfp .= ","; }
  1784. $moldfp .= $dfpstr;
  1785. //echo "dictionary-based fingerprints for reactant $i + dictionary $k\n$dfpstr\n";
  1786. } // for..
  1787. $moldfpsum = add_molfp($moldfpsum,$moldfp);
  1788. // create the hash-based fingerprints
  1789. if ($use_cmmmsrv == 'y') {
  1790. $chkresult = filterthroughcmmm($safemol,"#### checkmol:bH");
  1791. } else {
  1792. if ($ostype == 1) {$chkresult = filterthroughcmd($safemol,"$CHECKMOL -bH - "); }
  1793. if ($ostype == 2) {$chkresult = filterthroughcmd2($safemol,"$CHECKMOL -bH - "); }
  1794. }
  1795. if (strlen($chkresult) < 2) {
  1796. echo "no response from checkmol (maybe a server configuration problem?)\n</body></html>\n";
  1797. exit;
  1798. }
  1799. $cr = explode("\n", $chkresult);
  1800. $molfgb = trim($cr[0]);
  1801. $fgbarr = explode(";",$molfgb); // cut off the n1bits value
  1802. $molfgb = $fgbarr[0];
  1803. $molhfp = trim($cr[1]);
  1804. $hfparr = explode(";",$molhfp); // cut off the n1bits value
  1805. $molhfp = $hfparr[0];
  1806. //echo "molhfp: $molhfp\n";
  1807. $molhfpsum = add_molfp($molhfpsum,$molhfp);
  1808. $molfgbsum = add_molfp($molfgbsum,$molfgb);
  1809. } // end for ($i = 0; $i < $nrmol; $i++) ...
  1810. //echo "added moldfp: $moldfpsum\n";
  1811. //echo "added molhfp: $molhfpsum\n";
  1812. // insert combined reaction fingerprints for reactant(s)
  1813. $qstr = "INSERT INTO $rxncfptable VALUES ($rxn_id,'R',$moldfpsum,$molhfpsum,0)";
  1814. #echo "adding combined fingerprints (reactants) for no. $next_rxn_id to table $rxncfptable.... ";
  1815. $result = mysql_query($qstr);
  1816. $err = 0;
  1817. $err = mysql_errno();
  1818. if ($err != 0) {
  1819. echo "<br />Action failed (#4e/$err: " . mysql_error() . ")<br />\n";
  1820. $errorcount++;
  1821. } #else { echo "done\n"; }
  1822. // insert combined functional group bitstring for reactant(s)
  1823. $qstr = "INSERT INTO $rxnfgbtable VALUES ($rxn_id,'R',$molfgbsum,0)";
  1824. #echo "adding combined reactant functional group codes for no. $next_rxn_id to table $rxncfptable.... ";
  1825. $result = mysql_query($qstr);
  1826. $err = 0;
  1827. $err = mysql_errno();
  1828. if ($err != 0) {
  1829. echo "<br />Action failed (#4f/$err: " . mysql_error() . ")<br />\n";
  1830. $errorcount++;
  1831. } #else { echo "done\n"; }
  1832. }
  1833. if ($npmol > 0) {
  1834. $moldfpsum = "";
  1835. $molhfpsum = "";
  1836. $molfgbsum = "";
  1837. for ($i = 0; $i < $npmol; $i++) {
  1838. $pmol[$i] = $allmol[($i+1+$nrmol)];
  1839. $mnum = $i + 1;
  1840. #echo "processing product no. $mnum ...";
  1841. $labels = get_atomlabels($pmol[$i]);
  1842. $mid = "p" . $mnum;
  1843. if (strlen($labels) > 0) {
  1844. add_labels($mid,$labels);
  1845. }
  1846. $safemol = $pmol[$i];
  1847. // tweak the molfile
  1848. if ($tweakmolfiles == "y") {
  1849. if ($use_cmmmsrv == 'y') {
  1850. $safemol = filterthroughcmmm($safemol,"#### checkmol:m");
  1851. } else {
  1852. if ($ostype == 1) { $safemol = filterthroughcmd($safemol,"$CHECKMOL -m - "); }
  1853. if ($ostype == 2) {
  1854. $safemol = filterthroughcmd2($safemol,"$CHECKMOL -m - ");
  1855. $safemol = str_replace("\n","\r\n",$safemol);
  1856. }
  1857. }
  1858. $pmol[$i] = $safemol;
  1859. } // end tweakmolefiles = y
  1860. //create the dictionary-based fingerprints
  1861. $moldfp = "";
  1862. for ($k = 0; $k < $n_dict; $k++) {
  1863. $dict = $fpdef[$k];
  1864. if ($ostype == 1) { $cand = $safemol . "\n" . '$$$$' ."\n" . $dict; }
  1865. if ($ostype == 2) {
  1866. $dict = str_replace("\r","",$dict);
  1867. $dict = str_replace("\n","\r\n",$dict);
  1868. $cand = $safemol . "\r\n" . '$$$$' ."\r\n" . $dict;
  1869. }
  1870. if ($use_cmmmsrv == 'y') {
  1871. $dfpstr = filterthroughcmmm($cand,"#### MATCHMOL:F");
  1872. } else {
  1873. $cand = str_replace("\$","\\\$",$cand);
  1874. if ($ostype == 1) { $dfpstr = filterthroughcmd($cand,"$MATCHMOL -F - "); }
  1875. if ($ostype == 2) { $dfpstr = filterthroughcmd2($cand,"$MATCHMOL -F - "); }
  1876. }
  1877. $dfpstr = trim($dfpstr);
  1878. if ($k > 0) { $moldfp .= ","; }
  1879. $moldfp .= $dfpstr;
  1880. //echo "dictionary-based fingerprints for product $i + dictionary $k\n$dfpstr\n";
  1881. } // for..
  1882. $moldfpsum = add_molfp($moldfpsum,$moldfp);
  1883. // create the hash-based fingerprints
  1884. if ($use_cmmmsrv == 'y') {
  1885. $chkresult = filterthroughcmmm($safemol,"#### checkmol:bH");
  1886. } else {
  1887. if ($ostype == 1) {$chkresult = filterthroughcmd($safemol,"$CHECKMOL -bH - "); }
  1888. if ($ostype == 2) {$chkresult = filterthroughcmd2($safemol,"$CHECKMOL -bH - "); }
  1889. }
  1890. if (strlen($chkresult) < 2) {
  1891. echo "no response from checkmol (maybe a server configuration problem?)\n</body></html>\n";
  1892. exit;
  1893. }
  1894. $cr = explode("\n", $chkresult);
  1895. $molfgb = trim($cr[0]);
  1896. $fgbarr = explode(";",$molfgb); // cut off the n1bits value
  1897. $molfgb = $fgbarr[0];
  1898. $molhfp = trim($cr[1]);
  1899. $hfparr = explode(";",$molhfp); // cut off the n1bits value
  1900. $molhfp = $hfparr[0];
  1901. //echo "molhfp: $molhfp\n";
  1902. $molhfpsum = add_molfp($molhfpsum,$molhfp);
  1903. $molfgbsum = add_molfp($molfgbsum,$molfgb);
  1904. } // end for ($i = 0; $i < $npmol; $i++) ...
  1905. //echo "added moldfp: $moldfpsum\n";
  1906. //echo "added molhfp: $molhfpsum\n";
  1907. // insert combined reaction fingerprints for product(s)
  1908. $qstr = "INSERT INTO $rxncfptable VALUES ($rxn_id,'P',$moldfpsum,$molhfpsum,0)";
  1909. //echo "adding combined fingerprints for no. $next_rxn_id to table $rxncfptable.... ";
  1910. $result = mysql_query($qstr);
  1911. $err = 0;
  1912. $err = mysql_errno();
  1913. if ($err != 0) {
  1914. echo "<br />Action failed (#4g/$err: " . mysql_error() . ")<br />\n";
  1915. $errorcount++;
  1916. } #else { echo "done"; }
  1917. // insert combined functional group bitstring for product(s)
  1918. $qstr = "INSERT INTO $rxnfgbtable VALUES ($rxn_id,'P',$molfgbsum,0)";
  1919. //echo "adding combined product functional group codes for no. $next_rxn_id to table $rxncfptable.... ";
  1920. $result = mysql_query($qstr);
  1921. $err = 0;
  1922. $err = mysql_errno();
  1923. if ($err != 0) {
  1924. echo "<br />Action failed (#4h/$err: " . mysql_error() . ")<br />\n";
  1925. $errorcount++;
  1926. } #else { echo "done\n"; }
  1927. }
  1928. // re-apply the reaction maps after tweaking (checkmol strips the atom labels)
  1929. if ($tweakmolfiles == "y") {
  1930. $saferxn = $header;
  1931. for ($nm = 0; $nm < $nrmol; $nm++) {
  1932. $saferxn .= "\$MOL\r\n" . $rmol[$nm];
  1933. }
  1934. for ($nm = 0; $nm < $npmol; $nm++) {
  1935. $saferxn .= "\$MOL\r\n" . $pmol[$nm];
  1936. }
  1937. $saferxn = apply_maps($saferxn,$mapstr);
  1938. }
  1939. // update structure in rxnstruc
  1940. $qstr = "UPDATE $rxnstructable SET struc =\"$saferxn\", map = \"$mapstr\" WHERE rxn_id = $rxn_id";
  1941. //echo "updating reaction no. $rxn_id in data collection $db_id ...";
  1942. $result = mysql_query($qstr);
  1943. $err = 0;
  1944. $err = mysql_errno();
  1945. if ($err != 0) {
  1946. echo "<br />Action failed (#1/$err: " . mysql_error() . ")<br />\n";
  1947. $errorcount++;
  1948. } #else { echo "done\n"; }
  1949. // finished.....
  1950. if ($use_cmmmsrv == 'y') {
  1951. socket_write($socket,'#### bye');
  1952. socket_close($socket);
  1953. }
  1954. if ($debug > 0) { debug_output("Action finished with $errorcount errors.<br />\n"); }
  1955. if ($err == 0) {
  1956. echo "<h3>Reaction:</h3>\n";
  1957. // JME needs MDL molfiles with the "|" character instead of linebreaks
  1958. $jmehitmol = strtr($saferxn,"\n","|");
  1959. echo "<applet code=\"JME.class\" archive=\"../JME.jar\" \n";
  1960. echo "width=\"350\" height=\"120\">";
  1961. echo "<param name=\"options\" value=\"depict\"> \n";
  1962. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  1963. echo "</applet>\n";
  1964. echo "<p />\n";
  1965. echo "<form name=\"strucform\" action=\"$myname\" method=\"post\">\n";
  1966. echo "<input type=\"hidden\" name=\"action\" value=\"editstruc\">\n";
  1967. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  1968. echo "<input type=\"hidden\" name=\"datatype\" value=\"2\">\n";
  1969. echo "<input type=\"hidden\" name=\"id\" value=\"$rxn_id\">\n";
  1970. echo "<input type=\"Submit\" name=\"select\" value=\"Edit reaction\">\n";
  1971. echo "</form><p />\n";
  1972. echo "<p />\n<hr />\n";
  1973. mk_dataeditform($rxn_id,2);
  1974. } else {
  1975. echo "something went wrong....<br />\n";
  1976. }
  1977. echo "<p /><a href=\"$myname?db=$db_id\">Continue without saving</a>\n";
  1978. echo "</body></html>\n";
  1979. die();
  1980. } // end action = savestruc && datatype = 2
  1981. if ($action == "eraserecord") {
  1982. if ($datatype == 1) {
  1983. $idname = "mol_id";
  1984. $structable = $molstructable;
  1985. $datatable = $moldatatable;
  1986. $jmewidth = 250;
  1987. } elseif ($datatype == 2) {
  1988. $idname = "rxn_id";
  1989. $structable = $rxnstructable;
  1990. $datatable = $rxndatatable;
  1991. $jmewidth = 350;
  1992. }
  1993. $item_id = $_POST['id'];
  1994. if (!is_numeric($item_id)) {
  1995. echo "invalid input!";
  1996. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  1997. echo "</body></html>\n";
  1998. die();
  1999. }
  2000. $qstr = "SELECT COUNT($idname) AS itemcount FROM $structable WHERE $idname = $item_id";
  2001. $result = mysql_query($qstr) or die("Query failed! (struc 7)");
  2002. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  2003. $itemcount = $line["itemcount"];
  2004. }
  2005. mysql_free_result($result);
  2006. if ($itemcount < 1) {
  2007. echo "This entry does not exist!<br />\n";
  2008. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  2009. echo "</body></html>\n";
  2010. die();
  2011. }
  2012. $molstruc = "";
  2013. $md5str = "";
  2014. $qstr = "SELECT struc FROM $structable WHERE $idname = $item_id";
  2015. $result = mysql_query($qstr) or die("Query failed! (struc 8)");
  2016. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  2017. $molstruc = $line["struc"];
  2018. }
  2019. mysql_free_result($result);
  2020. if ($molstruc == "") {
  2021. echo "empty structure<br />\n";
  2022. } else {
  2023. $md5str = md5($molstruc);
  2024. }
  2025. echo "<h3>Do you really want to erase entry no. $item_id from data collection $db_id?</h3>\n";
  2026. // JME needs MDL molfiles with the "|" character instead of linebreaks
  2027. $jmehitmol = strtr($molstruc,"\n","|");
  2028. echo "<applet code=\"JME.class\" archive=\"../JME.jar\" \n";
  2029. echo "width=\"$jmewidth\" height=\"120\">";
  2030. echo "<param name=\"options\" value=\"depict\"> \n";
  2031. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  2032. echo "</applet>\n";
  2033. // show data fields
  2034. echo "<table bgcolor=\"#eeeeee\">\n";
  2035. echo "<tr align=\"left\"><th>Field:</th><th>Value:</th></tr>\n";
  2036. $i = 0;
  2037. $qstr = "SHOW FULL COLUMNS FROM $datatable";
  2038. $result = mysql_query($qstr)
  2039. or die("Query failed! (1c5)");
  2040. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  2041. $field = $line["Field"];
  2042. $type = $line["Type"];
  2043. $comment = $line["Comment"];
  2044. if ($field != $idname) {
  2045. $i++;
  2046. $oldval = ""; //preliminary....
  2047. $qstr2 = "SELECT $field FROM $datatable WHERE $idname = $item_id";
  2048. $result2 = mysql_query($qstr2)
  2049. or die("Query failed! (oldval)");
  2050. $line2 = mysql_fetch_row($result2);
  2051. mysql_free_result($result2);
  2052. $oldval = $line2[0];
  2053. echo "<tr><td>$field</td><td>$oldval</td></tr>\n";
  2054. } // if...
  2055. }
  2056. echo "</table><p />\n";
  2057. // show yes/no buttons
  2058. echo "<table>\n<tr>\n<td>\n";
  2059. echo "<form name=\"erase\" action=\"$myname\" method=\"post\">\n";
  2060. echo "<input type=\"hidden\" name=\"action\" value=\"eraseconfirm\">\n";
  2061. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  2062. echo "<input type=\"hidden\" name=\"id\" value=\"$item_id\">\n";
  2063. echo "<input type=\"hidden\" name=\"token\" value=\"$md5str\">\n";
  2064. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;Yes, erase it!\">\n";
  2065. echo "</form><p />\n";
  2066. echo "</td>\n<td>\n";
  2067. echo "<form name=\"goback\" action=\"$myname\" method=\"post\">\n";
  2068. echo "<input type=\"hidden\" name=\"action\" value=\"\">\n";
  2069. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  2070. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;No, keep it!\">\n";
  2071. echo "</form><p />\n";
  2072. echo "</td>\n</tr>\n</table>\n";
  2073. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  2074. echo "</body></html>\n";
  2075. die();
  2076. }
  2077. if ($action == "eraseconfirm") {
  2078. if (($access < 3) && ($trusted == false)) {
  2079. echo "Your client IP is not authorized to perform the requested operation!<br />\n";
  2080. //echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  2081. echo "</body></html>\n";
  2082. die();
  2083. }
  2084. if ($datatype == 1) {
  2085. $idname = "mol_id";
  2086. $structable = $molstructable;
  2087. $datatable = $moldatatable;
  2088. $cfptable = $molcfptable;
  2089. $fgbtable = $molfgbtable;
  2090. $jmewidth = 250;
  2091. } elseif ($datatype == 2) {
  2092. $idname = "rxn_id";
  2093. $structable = $rxnstructable;
  2094. $datatable = $rxndatatable;
  2095. $cfptable = $rxncfptable;
  2096. $fgbtable = $rxnfgbtable;
  2097. $jmewidth = 350;
  2098. }
  2099. $item_id = $_POST['id'];
  2100. $md5str = $_POST['token'];
  2101. if (!is_numeric($item_id)) {
  2102. echo "invalid input!";
  2103. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  2104. echo "</body></html>\n";
  2105. die();
  2106. }
  2107. $qstr = "SELECT COUNT($idname) AS itemcount FROM $structable WHERE $idname = $item_id";
  2108. $result = mysql_query($qstr) or die("Query failed! (struc 9)");
  2109. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  2110. $itemcount = $line["itemcount"];
  2111. }
  2112. mysql_free_result($result);
  2113. if ($itemcount < 1) {
  2114. echo "This entry does not exist!<br />\n";
  2115. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  2116. echo "</body></html>\n";
  2117. die();
  2118. }
  2119. $molstruc = "";
  2120. $mymd5str = "";
  2121. $qstr = "SELECT struc FROM $structable WHERE $idname = $item_id";
  2122. $result = mysql_query($qstr) or die("Query failed! (struc 10)");
  2123. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  2124. $molstruc = $line["struc"];
  2125. }
  2126. mysql_free_result($result);
  2127. if ($molstruc == "") {
  2128. echo "empty structure<br />\n";
  2129. } else {
  2130. $mymd5str = md5($molstruc);
  2131. }
  2132. echo "<h3>Erasing entry $item_id from data collection $db_id...</h3>\n";
  2133. //echo "hash value (requested): $md5str<br />\n";
  2134. //echo "hash value (calculated): $mymd5str<br />\n";
  2135. if ($md5str == $mymd5str) {
  2136. // erase everything
  2137. // delete old molstruc record
  2138. $qstr = "DELETE FROM $structable WHERE $idname = $item_id";
  2139. $result = mysql_query($qstr);
  2140. $err = mysql_errno();
  2141. if ($err != 0) { $errorcount++; }
  2142. // delete old moldata record
  2143. $qstr = "DELETE FROM $datatable WHERE $idname = $item_id";
  2144. $result = mysql_query($qstr);
  2145. $err = mysql_errno();
  2146. if ($err != 0) { $errorcount++; }
  2147. // delete old molcfp record
  2148. $qstr = "DELETE FROM $cfptable WHERE $idname = $item_id";
  2149. $result = mysql_query($qstr);
  2150. $err = mysql_errno();
  2151. if ($err != 0) { $errorcount++; }
  2152. // delete old molfgb record
  2153. $qstr = "DELETE FROM $fgbtable WHERE $idname = $item_id";
  2154. $result = mysql_query($qstr);
  2155. $err = mysql_errno();
  2156. if ($err != 0) { $errorcount++; }
  2157. if ($datatype == 1) {
  2158. // delete old molstat record
  2159. $qstr = "DELETE FROM $molstattable WHERE mol_id = $item_id";
  2160. $result = mysql_query($qstr);
  2161. $err = mysql_errno();
  2162. if ($err != 0) { $errorcount++; }
  2163. // delete old pic2d record
  2164. $qstr = "DELETE FROM $pic2dtable WHERE mol_id = $item_id";
  2165. $result = mysql_query($qstr);
  2166. $err = mysql_errno();
  2167. if ($err != 0) { $errorcount++; }
  2168. if ($usemem == 'T') {
  2169. $molstattable = str_replace($memsuffix, "", $molstattable);
  2170. $molcfptable = str_replace($memsuffix, "", $molcfptable);
  2171. // delete old molststat record first
  2172. $qstr = "DELETE FROM $molstattable WHERE mol_id = $item_id";
  2173. $result = mysql_query($qstr);
  2174. $err = mysql_errno();
  2175. if ($err != 0) { $errorcount++; }
  2176. // delete old molcfp record first
  2177. $qstr = "DELETE FROM $molcfptable WHERE mol_id = $item_id";
  2178. $result = mysql_query($qstr);
  2179. $err = mysql_errno();
  2180. if ($err != 0) { $errorcount++; }
  2181. }
  2182. }
  2183. echo "<h3>Done.</h3>\n";
  2184. }
  2185. if ($debug > 0) { debug_output("Action finished with $errorcount errors.<br />\n"); }
  2186. $errorcount = 0;
  2187. echo "<p /><a href=\"$myname?db=$db_id\">Continue</a>\n";
  2188. echo "</body></html>\n";
  2189. die();
  2190. }
  2191. function mk_inputline($iname,$idefault) {
  2192. echo "<input type=\"text\" name=\"$iname\" size=\"106\" value=\"$idefault\">";
  2193. }
  2194. function mk_inputarea($iname,$idefault) {
  2195. echo "<textarea name=\"$iname\" cols=\"80\" rows=\"4\">$idefault</textarea>\n";
  2196. }
  2197. function mk_inputselect($iname,$itype,$idefault) {
  2198. $lpos = strpos($itype,"(");
  2199. $itype = substr($itype,($lpos+1));
  2200. $rpos = strrpos($itype,")");
  2201. $list1 = substr($itype,0,$rpos);
  2202. $aitems = explode(",",$list1);
  2203. echo "<select size=\"1\" name=\"$iname\">\n";
  2204. foreach ($aitems as $item) {
  2205. $ditem = str_replace("'","",$item);
  2206. echo "<option value=\"$ditem\""; if ($idefault == $ditem) {echo " selected";} echo ">&nbsp;$ditem&nbsp;</option>\n";
  2207. } // foreach
  2208. echo "</select>";
  2209. }
  2210. function mk_dataeditform($item_id,$formtype) {
  2211. global $db_id;
  2212. global $moldatatable;
  2213. global $rxndatatable;
  2214. global $myname;
  2215. if ($formtype == 1) {
  2216. $idname = "mol_id";
  2217. $datatable = $moldatatable;
  2218. } elseif ($formtype == 2) {
  2219. $idname = "rxn_id";
  2220. $datatable = $rxndatatable;
  2221. }
  2222. echo "<h3>Edit textual data for entry no. $item_id in data collection $db_id</h3>\n";
  2223. echo "<form name=\"dataform\" action=\"$myname\" method=post>\n";
  2224. echo "<table bgcolor=\"#eeeeee\">\n";
  2225. echo "<tr align=\"left\"><th>Field:</th><th>Value:</th></tr>\n";
  2226. $i = 0;
  2227. $qstr = "SHOW FULL COLUMNS FROM $datatable";
  2228. $result = mysql_query($qstr)
  2229. or die("Query failed! (1c6)");
  2230. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  2231. $field = $line["Field"];
  2232. $type = $line["Type"];
  2233. $comment = $line["Comment"];
  2234. if ($field != $idname) {
  2235. $i++;
  2236. $oldval = ""; //preliminary....
  2237. $qstr2 = "SELECT $field FROM $datatable WHERE $idname = $item_id";
  2238. $result2 = mysql_query($qstr2)
  2239. or die("Query failed! (oldval)");
  2240. $line2 = mysql_fetch_row($result2);
  2241. mysql_free_result($result2);
  2242. $oldval = $line2[0];
  2243. $ifmt = "line";
  2244. if (strlen($comment)>4) {
  2245. $pos = strpos($comment, ">>>>");
  2246. if ($pos !== false) {
  2247. if ($pos == 0) {
  2248. $comment = str_replace(">>>>","",$comment);
  2249. $acomment = explode("<",$comment);
  2250. $label = $acomment[0];
  2251. $nformat = $acomment[1];
  2252. if ($nformat == 0) { $ifmt = "line"; }
  2253. if ($nformat == 1) { $ifmt = "line"; }
  2254. if ($nformat == 2) { $ifmt = "text"; }
  2255. if ($nformat == 3) { $ifmt = "line"; }
  2256. }
  2257. }
  2258. }
  2259. if(stristr($type, 'text') != FALSE) { $ifmt = "text"; }
  2260. if(stristr($type, 'enum') != FALSE) { $ifmt = "select"; }
  2261. if(stristr($type, 'set') != FALSE) { $ifmt = "select"; }
  2262. echo "<tr><td>$field</td><td>";
  2263. if ($ifmt == "line") { mk_inputline("f${i}",$oldval); }
  2264. if ($ifmt == "text") { mk_inputarea("f${i}",$oldval); }
  2265. if ($ifmt == "select") { mk_inputselect("f${i}",$type,$oldval); }
  2266. echo "</td></tr>\n";
  2267. } // if...
  2268. }
  2269. echo "</table><p />\n";
  2270. echo "<input type=\"hidden\" name=\"action\" value=\"savedata\">\n";
  2271. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  2272. echo "<input type=\"hidden\" name=\"datatype\" value=\"$formtype\">\n";
  2273. echo "<input type=\"hidden\" name=\"id\" value=\"$item_id\">\n";
  2274. echo "<input type=\"hidden\" name=\"nf\" value=\"$i\">\n";
  2275. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;&nbsp;Save data&nbsp;&nbsp;\">\n";
  2276. echo "</form><p />\n";
  2277. mysql_free_result($result);
  2278. }
  2279. function copy_mol($db_id,$mol_id,$targetdb_id) {
  2280. global $prefix;
  2281. global $molstrucsuffix;
  2282. global $moldatasuffix;
  2283. global $molfgbsuffix;
  2284. global $molstatsuffix;
  2285. global $molcfpsuffix;
  2286. global $pic2dsuffix;
  2287. global $memsuffix;
  2288. global $metatable;
  2289. $dbprefix = $prefix . "db" . $db_id . "_";
  2290. $targetdbprefix = $prefix . "db" . $targetdb_id . "_";
  2291. $moldatatable = $dbprefix . $moldatasuffix;
  2292. $molfgbtable = $dbprefix . $molfgbsuffix;
  2293. $molstattable = $dbprefix . $molstatsuffix;
  2294. $molstructable = $dbprefix . $molstrucsuffix;
  2295. $molcfptable = $dbprefix . $molcfpsuffix;
  2296. $pic2dtable = $dbprefix . $pic2dsuffix;
  2297. $next_mol_id = get_next_mol_id($targetdb_id);
  2298. if (intval($db_id) == intval($targetdb_id)) {
  2299. $table_list = array($moldatatable,$molfgbtable,$molstattable,$molstructable,$molcfptable);
  2300. } else {
  2301. $table_list = array($molfgbtable,$molstattable,$molstructable,$molcfptable);
  2302. }
  2303. $field_name = array();
  2304. $field_val = array();
  2305. foreach ($table_list as $table) {
  2306. $targettable = str_replace($dbprefix,$targetdbprefix,$table);
  2307. $qstr1 = "DESCRIBE $table";
  2308. $result1 = mysql_query($qstr1) or die("Query failed! (copy_mol #1)");
  2309. $i = 0;
  2310. while ($line1 = mysql_fetch_array($result1, MYSQL_ASSOC)) {
  2311. $field_name[$i] = $line1["Field"];
  2312. $i++;
  2313. }
  2314. $n_fields = $i;
  2315. #mysql_free_result($result1);
  2316. $qstr2 = "SELECT * FROM $table WHERE mol_id = $mol_id";
  2317. $result2 = mysql_query($qstr2) or die("Query failed! (copy_mol #2)");
  2318. while ($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
  2319. for ($i = 0; $i < $n_fields; $i++) {
  2320. $field = $field_name[$i];
  2321. $field_val[$i] = $line2[$field];
  2322. if ($field == "mol_id") {
  2323. $field_val[$i] = $next_mol_id;
  2324. }
  2325. } // end for ...
  2326. } // end while ...
  2327. $valstr = "";
  2328. for ($i = 0; $i < $n_fields; $i++) {
  2329. $val = $field_val[$i];
  2330. if (strlen($valstr) > 0) { $valstr .= ","; }
  2331. $valstr .= "\"" . $val . "\"";
  2332. }
  2333. $qstr3 = "INSERT INTO $targettable VALUES (" . $valstr . ")";
  2334. $result3 = mysql_query($qstr3);
  2335. $err = 0;
  2336. $err = mysql_errno();
  2337. if ($err != 0) {
  2338. echo "<br />Action failed (duplicate_mol #3/$err: " . mysql_error() . ")<br />\n";
  2339. } #else { echo "."; }
  2340. } // end foreach table ...
  2341. // if sourcedb and targetdb are different, copy only mol_name in moldatatable
  2342. if (intval($db_id) != intval($targetdb_id)) {
  2343. $qstr4 = "SELECT mol_name FROM $moldatatable WHERE mol_id = $mol_id";
  2344. $result4 = mysql_query($qstr4) or die("Query failed! (copy_mol #4)");
  2345. while ($line4 = mysql_fetch_array($result4, MYSQL_ASSOC)) {
  2346. $mol_name = $line4["mol_name"];
  2347. }
  2348. $qstr5 = "INSERT INTO " . $targetdbprefix . $moldatasuffix . "(mol_id,mol_name) ";
  2349. $qstr5 .= "VALUES (" . $next_mol_id . ",'" . $mol_name . "')";
  2350. $result5 = mysql_query($qstr5);
  2351. $err = 0;
  2352. $err = mysql_errno();
  2353. if ($err != 0) {
  2354. echo "<br />Action failed (copy_mol #5/$err: " . mysql_error() . ")<br />\n";
  2355. } #else { echo "."; }
  2356. }
  2357. // handle pic2dtable separately...
  2358. $qstr6 = "INSERT INTO " . $targetdbprefix . $pic2dsuffix . " VALUES ($next_mol_id, \"1\", \"3\" )"; // 0 = does not exist, 1 = OK, 2 = OK, but do not show, 3 = to be created/updated, 4 = to be deleted
  2359. $result6 = mysql_query($qstr6);
  2360. $err = 0;
  2361. $err = mysql_errno();
  2362. if ($err != 0) {
  2363. echo "<br />Action failed (copy_mol #6/$err: " . mysql_error() . ")<br />\n";
  2364. } #else { echo "."; }
  2365. set_memstatus_dirty($db_id);
  2366. return($next_mol_id);
  2367. }
  2368. function copy_rxn($db_id,$rxn_id,$targetdb_id) {
  2369. global $prefix;
  2370. global $rxnstrucsuffix;
  2371. global $rxndatasuffix;
  2372. global $rxnfgbsuffix;
  2373. global $rxncfpsuffix;
  2374. $dbprefix = $prefix . "db" . $db_id . "_";
  2375. $targetdbprefix = $prefix . "db" . $targetdb_id . "_";
  2376. $rxndatatable = $dbprefix . $rxndatasuffix;
  2377. $rxnfgbtable = $dbprefix . $rxnfgbsuffix;
  2378. $rxnstructable = $dbprefix . $rxnstrucsuffix;
  2379. $rxncfptable = $dbprefix . $rxncfpsuffix;
  2380. $next_rxn_id = get_next_rxn_id($targetdb_id);
  2381. if (intval($db_id) == intval($targetdb_id)) {
  2382. $table_list = array($rxndatatable,$rxnfgbtable,$rxnstructable,$rxncfptable);
  2383. } else {
  2384. $table_list = array($rxnfgbtable,$rxnstructable,$rxncfptable);
  2385. }
  2386. $field_name = array();
  2387. $field_val = array();
  2388. foreach ($table_list as $table) {
  2389. $targettable = str_replace($dbprefix,$targetdbprefix,$table);
  2390. $qstr1 = "DESCRIBE $table";
  2391. $result1 = mysql_query($qstr1) or die("Query failed! (copy_rxn #1)");
  2392. $i = 0;
  2393. while ($line1 = mysql_fetch_array($result1, MYSQL_ASSOC)) {
  2394. $field_name[$i] = $line1["Field"];
  2395. $i++;
  2396. }
  2397. $n_fields = $i;
  2398. $qstr2 = "SELECT * FROM $table WHERE rxn_id = $rxn_id";
  2399. $result2 = mysql_query($qstr2) or die("Query failed! (copy_rxn #2)");
  2400. while ($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
  2401. for ($i = 0; $i < $n_fields; $i++) {
  2402. $field = $field_name[$i];
  2403. $field_val[$i] = $line2[$field];
  2404. if ($field == "rxn_id") {
  2405. $field_val[$i] = $next_rxn_id;
  2406. }
  2407. } // end for ...
  2408. } // end while ...
  2409. $valstr = "";
  2410. for ($i = 0; $i < $n_fields; $i++) {
  2411. $val = $field_val[$i];
  2412. if (strlen($valstr) > 0) { $valstr .= ","; }
  2413. $valstr .= "\"" . $val . "\"";
  2414. }
  2415. $qstr3 = "INSERT INTO $targettable VALUES (" . $valstr . ")";
  2416. $result3 = mysql_query($qstr3);
  2417. $err = 0;
  2418. $err = mysql_errno();
  2419. if ($err != 0) {
  2420. echo "<br />Action failed (copy_rxn #3/$err: " . mysql_error() . ")<br />\n";
  2421. } #else { echo "."; }
  2422. } // end foreach table ...
  2423. if (intval($db_id) != intval($targetdb_id)) {
  2424. $qstr4 = "SELECT rxn_name FROM $rxndatatable WHERE rxn_id = $rxn_id";
  2425. $result4 = mysql_query($qstr4) or die("Query failed! (copy_rxn #4)");
  2426. while ($line4 = mysql_fetch_array($result4, MYSQL_ASSOC)) {
  2427. $rxn_name = $line4["rxn_name"];
  2428. }
  2429. $qstr5 = "INSERT INTO " . $targetdbprefix . $rxndatasuffix . "(rxn_id,rxn_name) ";
  2430. $qstr5 .= "VALUES (" . $next_rxn_id . ",'" . $rxn_name . "')";
  2431. $result5 = mysql_query($qstr5);
  2432. $err = 0;
  2433. $err = mysql_errno();
  2434. if ($err != 0) {
  2435. echo "<br />Action failed (copy_rxn #5/$err: " . mysql_error() . ")<br />\n";
  2436. } #else { echo "."; }
  2437. }
  2438. return($next_rxn_id);
  2439. }
  2440. $result = mysql_query("SELECT COUNT($idname) AS molcount, MAX($idname) AS max_mol_id FROM $structable")
  2441. or die("Query failed! (1c7)");
  2442. $line = mysql_fetch_row($result);
  2443. mysql_free_result($result);
  2444. $molcount = $line[0];
  2445. $max_mol_id = $line[1];
  2446. if ($molcount == 0) { $max_mol_id = 0; }
  2447. if ($molcount > 0) {
  2448. echo "<p><small>current entries: $molcount, highest entry no.: $max_mol_id</small></p>\n";
  2449. echo "<form name=\"edform1\" action=\"$myname\" method=post>\n";
  2450. echo "<input type=\"hidden\" name=\"action\" value=\"editdata\">\n";
  2451. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  2452. echo "<input type=\"hidden\" name=\"datatype\" value=\"$formtype\">\n";
  2453. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Edit entry no.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\">\n";
  2454. echo "<input type=\"text\" name=\"id\" size=\"12\"><br />\n";
  2455. echo "</form><p />\n";
  2456. echo "<form name=\"edform1\" action=\"$myname\" method=post>\n";
  2457. echo "<input type=\"hidden\" name=\"action\" value=\"duplcopy\">\n";
  2458. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  2459. echo "<input type=\"hidden\" name=\"datatype\" value=\"$formtype\">\n";
  2460. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;Copy entry no.&nbsp;&nbsp;&nbsp;&nbsp;\">\n";
  2461. echo "<input type=\"text\" name=\"id\" size=\"12\"><br />\n";
  2462. echo "</form><p />\n";
  2463. echo "<form name=\"edform1\" action=\"$myname\" method=post>\n";
  2464. echo "<input type=\"hidden\" name=\"action\" value=\"eraserecord\">\n";
  2465. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  2466. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;Erase entry no.&nbsp;&nbsp;&nbsp;\">\n";
  2467. echo "<input type=\"text\" name=\"id\" size=\"12\"><br />\n";
  2468. echo "</form><p />\n";
  2469. }
  2470. echo "<form name=\"edform2\" action=\"$myname\" method=\"post\">\n";
  2471. echo "<input type=\"hidden\" name=\"action\" value=\"addstruc1\">\n";
  2472. echo "<input type=\"hidden\" name=\"db\" value=\"$db_id\">\n";
  2473. echo "<input type=\"Submit\" name=\"select\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add new entry&nbsp;&nbsp;&nbsp;&nbsp;\">\n";
  2474. echo "</form><p />\n";
  2475. echo "Back to <a href=\"./?db=$db_id\">database administration</a>\n";
  2476. ?>
  2477. <br />
  2478. <hr />
  2479. <br />
  2480. </body>
  2481. </html>