1<?php
2
3// ChemDoodle server using JmolData.jar in silent restricted mode
4// requires JmolData.jar version 12.3.22
5// Bob Hanson hansonr@stolaf.edu 4/8/2012-4/13/2012
6
7$cmd = '/home/mscs/common/bin32/java -Djava.awt.headless=true -jar JmolData.jar -iRJ ';
8
9
10function getValueSimple($json, $key, $default) {
11
12 // just do a crude check for "key"..."value"  -- nothing more than that;
13 // only for very simple key/value pairs; mostly because we don't have the JSON
14 // module set up for our server.
15
16 list($junk,$info) = explode('"'.$key.'"', $json, 2);
17 list($junk,$val) = explode('"', $info, 3);
18 if ($val == "") {
19	$val = str_replace('"','_',$_REQUEST[$key]);
20 }
21 if ($val == "") {
22   $val = $default;
23 }
24 return $val;
25}
26
27$values= file_get_contents("php://input");
28$call = getValueSimple($values, "call", "getMoleculeFromDatabase");
29$query = getValueSimple($values, "query", "morphine");
30$database = getValueSimple($values, "database", "$");
31$postLoad = getValueSimple($values, "script","");
32
33if ($database == '$') {
34	$database = '\\$';  // NCI query
35}
36
37if (substr($query, 0, 1) == '$') {
38	$query = '\\'.$query;  // 2D query
39}
40
41$imagedata = "";
42$output = "";
43
44if ($call == "getInfoFromDatabase") {
45	if ($database == '=') {
46		$restQueryUrl = "http://www.pdb.org/pdb/rest/search";
47		$restReportUrl = "http://www.pdb.org/pdb/rest/customReport";
48		$xml = "<orgPdbQuery><queryType>org.pdb.query.simple.AdvancedKeywordQuery</queryType><description>Text Search</description><keywords>$query</keywords></orgPdbQuery>";
49		$context = stream_context_create(array('http' => array(
50			'method' => 'POST',
51			'header' => 'Content-Type: application/x-www-form-urlencoded',
52			'content' => $xml))
53		);
54		$output = file_get_contents($restQueryUrl, false, $context);
55		$n = strlen($output)/5;
56		if ($n == 0) {
57			$output = "ERROR: \"$query\" not found";
58		} else {
59			if (strlen($query) == 4 && $n != 1) {
60				$QQQQ = strtoupper($query);
61				if (strpos("123456789", substr($QQQQ, 0, 1)) == 0 && strpos($output, $QQQQ) > 0) {
62					$output = "$QQQQ\n".$output.str_replace("$QQQQ\n", "",$output);
63				}
64			}
65			if ($n > 50) {
66				$output = substr($output, 0, 250);
67			}
68			$output = str_replace("\n",",",$output);
69			//http://www.rcsb.org/pdb/rest/customReport?pdbids=1crn,1d66,1blu,&customReportColumns=structureId,structureTitle
70			$output = $restReportUrl."?pdbids=".$output."&customReportColumns=structureId,structureTitle";
71			$output = "<result query=\"$query\" count=\"$n\">".file_get_contents($output)."</result>";
72		}
73	}
74}
75
76if ($call == "getInfoFromDatabase") {
77	// done
78} else if ($call == "getImageForFileLoad" || $call == "getImageFromDatabase") {
79
80	// max size for image is 1000x1000
81
82	$imagefile = "/tmp/img".rand().".png";
83	$width = $_REQUEST[width];
84	$height = $_REQUEST[height];
85
86	if (!$width || $width > 1000) {
87		$width = 300;
88	}
89	if (!$height || $height > 1000) {
90		$height = 300;
91	}
92	if ($call == "getImageFromDatabase") {
93		$cmd = $cmd.'"load \\"'.$database.$query.'\\"'.$postLoad.'" -g'.$width.'x'.$height.' -wPNG:'.$imagefile;
94	} else {
95		$file = $_REQUEST[file];
96		$params = $_REQUEST[params];
97		$cmd = $cmd.'"load \\"'.$file.'\\" '.$params.'" -g'.$width.'x'.$height.' -wPNG:'.$imagefile;
98	}
99	exec($cmd, $v);
100	$imagedata = file_get_contents($imagefile);
101
102} else if ($call == "getRawDataFromDatabase") {
103	if ($database == '\\$') { // NCI -- needs post load
104		if (substr($query, 0, 2) == '\\$')
105			$cmd = $cmd.'"print load(\\"'.$database.$query.'\\")"';
106		else
107			$cmd = $cmd.'"load \\"'.$database.$query.'\\"'.$postLoad.';write MOL"';
108		exec($cmd, $result);
109		$output = implode("\n",$result);
110	} else if ($database == ":") { // PubChem -- just get file, including charge data
111		$cmd = $cmd.'"print load(\\"'.$database.$query.'\\")"';
112		exec($cmd, $result);
113		$output = implode("\n",$result);
114	} else {
115		if ($database != "_")
116			$query = $database.$query;
117		if (strpos($query, '?POST?') > 0) {
118			list($query,$data) = explode('?POST?', $query, 2);
119			$context = stream_context_create(array('http' => array(
120				'method' => 'POST',
121				'header' => 'Content-Type: application/x-www-form-urlencoded',
122				'content' => $data))
123			);
124			$output = file_get_contents($query, false, $context);
125		} else {
126			$output = file_get_contents($query);
127		}
128	}
129} else if ($call == "getMoleculeFromDatabase") {
130
131  // note: ChemDoodle coordinates in JSON are factored by [20, -20, 20]
132  // we use select molecule=1 because some salts are retrieved by NCI, and we want to ignore them
133  // it's not perfect.
134
135	$query = getValueSimple($values, "query", "morphine");
136	$cmd = $cmd.'"load \\"'.$database.$query.'\\"'.$postLoad.';write CD"';
137	exec($cmd, $result);
138	$result = implode($result);
139	$output = "{\"jmolcd_message\":\"$call - $query\",\"values\":$values,\"content\":$result}";
140
141} else {
142
143  // just the test result -- penicillin
144	$result = '{"mol":{"a":[{"x":78.474,"y":18.444,"z":3.67},{"x":64.958,"y":64.212006,"z":7.6419997},{"x":103.462,"y":26.569998,"z":-4.9280005},{"x":89.946,"y":72.338,"z":-0.956},{"x":109.19601,"y":53.517998,"z":-7.2460003},{"x":31.98,"y":28.406,"z":19.326},{"x":-84.273994,"y":-16.376001,"z":51.858},{"x":-115.051994,"y":-3.2080002,"z":14.070001},{"l":"H","x":-18.426,"y":13.716001,"z":17.006},{"x":59.22,"y":37.264,"z":9.952001},{"l":"O","x":-99.78,"y":-50.052002,"z":-24.666002},{"l":"O","x":-25.512001,"y":-33.281998,"z":-38.72},{"l":"O","x":22.08,"y":29.476002,"z":-26.811998},{"l":"O","x":-92.08,"y":-69.404,"z":14.316},{"x":-88.61001,"y":-49.4,"z":-3.246},{"x":13.762001,"y":25.081999,"z":-4.454},{"x":-70.782,"y":-26.126001,"z":3.75},{"x":-29.484001,"y":14.0,"z":-23.994},{"x":-37.694,"y":-14.437999,"z":-29.506},{"l":"H","x":-103.7,"y":-83.898,"z":8.932},{"l":"N","x":-11.774,"y":17.23,"z":-0.8759999},{"x":-59.156,"y":16.960001,"z":-17.647999},{"x":-85.96,"y":-6.886,"z":22.816},{"l":"S","x":-66.38,"y":23.898,"z":17.618},{"l":"N","x":-63.18,"y":-11.977999,"z":-20.772},{"l":"H","x":-52.845997,"y":-33.494,"z":13.710001},{"l":"H","x":-70.414,"y":29.387997,"z":-31.578003},{"l":"H","x":-93.138,"y":-36.222,"z":53.544},{"l":"H","x":-95.102005,"y":-2.47,"z":64.688},{"l":"H","x":-63.384,"y":-17.2,"z":58.034},{"l":"H","x":-115.588,"y":5.1380005,"z":-6.062},{"l":"H","x":-125.116005,"y":10.374001,"z":27.836},{"l":"H","x":-125.17799,"y":-22.512,"z":14.205999},{"l":"H","x":-22.886,"y":25.046001,"z":-41.592003},{"l":"H","x":23.692,"y":43.415993,"z":32.788},{"l":"H","x":33.742,"y":9.392,"z":29.842},{"l":"H","x":74.024,"y":-2.606,"z":5.568},{"l":"H","x":49.914,"y":78.914,"z":12.550001},{"l":"H","x":118.50201,"y":11.865999,"z":-9.842},{"l":"H","x":94.43,"y":93.39,"z":-2.7519999},{"l":"H","x":128.714,"y":59.865997,"z":-13.978}],"b":[{"b":10,"e":14,"o":2},{"b":13,"e":14},{"b":14,"e":16},{"b":16,"e":24},{"b":16,"e":22},{"b":21,"e":24},{"b":18,"e":24},{"b":6,"e":22},{"b":7,"e":22},{"b":22,"e":23},{"b":21,"e":23},{"b":17,"e":21},{"b":17,"e":18},{"b":11,"e":18,"o":2},{"b":17,"e":20},{"b":15,"e":20},{"b":5,"e":15},{"b":12,"e":15,"o":2},{"b":5,"e":9},{"b":0,"e":9,"o":2},{"b":1,"e":9},{"b":0,"e":2},{"b":1,"e":3,"o":2},{"b":2,"e":4,"o":2},{"b":3,"e":4},{"b":13,"e":19},{"b":16,"e":25},{"b":21,"e":26},{"b":6,"e":27},{"b":6,"e":28},{"b":6,"e":29},{"b":7,"e":30},{"b":7,"e":31},{"b":7,"e":32},{"b":17,"e":33},{"b":8,"e":20},{"b":5,"e":34},{"b":5,"e":35},{"b":0,"e":36},{"b":1,"e":37},{"b":2,"e":38},{"b":3,"e":39},{"b":4,"e":40}]}}';
145	$result = implode($result);
146	$output = "{\"message\":\"$call - $query - $cmd\",\"values\":$values,\"content\":$result}";
147
148}
149
150ob_start();
151 if ($imagedata != "") {
152	$output = $imagedata;
153	header('Content-Type: image/png');
154 } else {
155	header('Access-Control-Allow-Origin: *');
156	header('Content-Type: application/json');
157 }
158 header('Last-Modified: '.date('r'));
159 header('Accept-Ranges: bytes');
160 header('Content-Length: '.strlen($output));
161 print($output);
162ob_end_flush();
163
164?>
165
166