forked from luponzo86/rhapsody-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit_job.php
305 lines (277 loc) · 9.14 KB
/
submit_job.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
include 'src/php/utils.php';
function endsWith($haystack, $needle) {
$length = strlen($needle);
if ($length == 0) return true;
return (substr($haystack, -$length) === $needle);
}
function save2file($fname, $value) {
$file = fopen($fname, "w");
fwrite($file, $value);
fclose($file);
}
$scratch_dir = "./workspace";
// determine submission type
$subm_type = '';
if ( isset($_POST["sm_query"]) ) {
$subm_type = 'sat_mutagen';
}
elseif ( isset($_POST["bq_radios"]) ) {
$subm_type = 'batch_query';
}
// check for errors and print error messages, if any
$errors = [];
if ( $subm_type == 'sat_mutagen' ) {
$back_link = 'sat_mutagen.php';
// check input data
$query = str_replace(array(" ", "\n", "\r"), '', $_POST["sm_query"]);
// allow empty query only on default options
if ( empty($query) ) {
if ( isset($_POST["customPDB_checkbox"]) ) {
$errors[] =
'"Uniprot ID" field is empty.
When assigning a custom PDB structure, please make sure that the Uniprot
sequence is sufficiently similar to the amino acid sequence in the PDB,
since only matching amino acids will be analyzed.';
}
else if ( isset($_POST["EVmutation_checkbox"]) ||
isset($_POST["environment_checkbox"]) ) {
$errors[] =
'"Uniprot ID" field is empty.
Advanced options cannot be selected when running test cases.';
}
else {
$subm_type = "example-sm";
}
}
else {
$parsed_query = explode(" ", trim($_POST["sm_query"]));
if (sizeof($parsed_query) > 2) {
$errors[] =
'Query must be a single Uniprot identifier (e.g. "P01112") or a
Uniprot ID plus a specific position on the sequence (e.g. "P01112 100").';
}
if (!ctype_alnum($query))
$errors[] = 'Query must contain letters, numbers and spaces only.';
}
if ( isset($_POST["customPDB_checkbox"]) ) {
$radio_value = $_POST["customPDB_radios"];
if ( $radio_value == "PDBID" ) {
$PDBID = str_replace(' ', '', $_POST["customPDBID"]);
if ( ! ctype_alnum($PDBID) || strlen($PDBID)!=4 )
$errors[] = 'PDB code must be a 4-letter alphanumeric code.';
}
else if ( $radio_value == "PDBfile" ) {
# check for correct file extensions
$fname = $_FILES["customPDBFile"]["name"];
if ( ! (endsWith($fname, ".pdb") || endsWith($fname, ".pdb.gz")) )
$errors[] = 'Only files with extensions ".pdb" or ".pdb.gz" are accepted.';
if ( $_FILES["customPDBFile"]["size"] > 2000000 )
$errors[] = 'PDB file is too large (>2MB).';
}
else {
$errors[] = 'Internal error: Invalid custom PDB selection.';
}
}
}
elseif ( $subm_type == 'batch_query' ) {
$back_link = 'batch_query.php';
// check input data
$radio_value = $_POST["bq_radios"];
if ( $radio_value == "bq_text" ) {
$text = $_POST["bq_text"];
$text = str_replace(array(" ", "_", "\n", "\r"), '', $text);
if ( empty($text) ) {
// allow empty query only on default options
if ( isset($_POST["EVmutation_checkbox"]) ) {
$errors[] =
'"Uniprot ID" field is empty.
Advanced options cannot be selected when running test cases.';
}
else {
$subm_type = "example-bq";
}
}
elseif ( !ctype_alnum($text) ) {
$errors[] = 'SAV coordinates can only contain ' .
'alphanumeric characters and underscores.';
}
elseif ( strlen($text) > 500 ) {
$errors[] = 'input text is too long.';
}
}
else if ( $radio_value == "bq_file" ) {
if ( $_FILES["bq_file"]["size"] == 0 )
$errors[] = 'Empty file';
elseif ( $_FILES["bq_file"]["size"] > 150000 )
$errors[] = 'Uploaded file is too large (>150KB).';
}
else {
$errors[] = 'Internal error: Invalid batch query.';
}
}
else {
$errors[] = 'Internal error: Invalid submission type.';
$back_link = 'index.php';
}
if ( isset($_POST["email"]) && $_POST["email"]!="" &&
!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email address.';
}
if (!empty($errors)) {
$err_msg = "";
foreach($errors as $err) {
$err_msg .= $err . ' ';
}
// // DEBUG: append info to error message
// $err_msg .= "Set variables: <br>";
// $err_msg .= print_r($_POST, true);
// $err_msg .= "<br>Uploaded files: <br>";
// $err_msg .= print_r($_FILES, true);
// exit to error page
$arr = ["err_msg" => $err_msg, "back_link" => $back_link];
redirect("error.php", $arr);
}
// create a unique job ID and directory
$orig_dir = getcwd();
$jobid = "";
$jobdir = "";
while ( $jobid=="" || file_exists($jobdir) ) {
$salt = time();
$jobid = substr(md5($salt), 0, 10);
$jobdir = $scratch_dir . "/job_" . $jobid;
}
// store test examples in a special directory for the first time only
if ( substr($subm_type, 0, 8) == "example-") {
$exdir = $scratch_dir . "/job_" . $subm_type;
if ( !file_exists($exdir) ) {
$jobid = $subm_type;
$jobdir = $exdir;
}
}
mkdir($jobdir);
// write data to file
chdir($jobdir);
if ( $subm_type == 'sat_mutagen' ) {
$config = "submission type: saturation mutagenesis \n";
save2file("input-sm_query.txt", $_POST["sm_query"]);
if ( isset($_POST["customPDB_checkbox"]) ) {
$radio_value = $_POST["customPDB_radios"];
if ( $radio_value == "PDBID") {
$config .= "custom PDB: PDBID \n";
save2file("input-PDBID.txt", $_POST["customPDBID"]);
}
else {
$config .= "custom PDB: PDB file \n";
$orig_fname = $_FILES["customPDBFile"]["name"];
$temp_fname = $_FILES["customPDBFile"]["tmp_name"];
if ( endsWith($orig_fname, ".pdb") )
$new_fname = "input-PDB.pdb";
else
$new_fname = "input-PDB.pdb.gz";
if (! move_uploaded_file($temp_fname, $new_fname)) {
// exit to error page
$err_msg = "Sorry, there was an error uploading your file";
$arr = ["err_msg" => $err_msg, "back_link" => $back_link];
redirect("error.php", $arr);
}
}
}
else {
$config .= "custom PDB: None \n";
}
if ( isset($_POST["EVmutation_checkbox"]) ) {
$config .= "include EVmutation feature: True \n";
}
else {
$config .= "include EVmutation feature: False \n";
}
if ( isset($_POST["environment_checkbox"]) ) {
$config .= "include environmental effects: True \n";
}
else {
$config .= "include environmental effects: False \n";
}
}
elseif ( $subm_type == 'batch_query' ) {
$config = "submission type: batch query \n";
$radio_value = $_POST["bq_radios"];
if ( $radio_value == "bq_text" ) {
save2file("input-batch_query.txt", $_POST["bq_text"]);
}
else {
$tmp_fname = $_FILES["bq_file"]["tmp_name"];
if (! move_uploaded_file($tmp_fname, "input-batch_query.txt")) {
// exit to error page
$err_msg = "Sorry, there was an error uploading your file";
$arr = ["err_msg" => $err_msg, "back_link" => $back_link];
redirect("error.php", $arr);
}
}
if ( isset($_POST["environment_checkbox"]) ) {
$config .= "include environmental effects: True \n";
}
else {
$config .= "include environmental effects: False \n";
}
}
elseif ( substr($subm_type, 0, 8) == "example-") {
// try to reuse PolyPhen-2 files
$pph2file = "../job_" . $subm_type . "/pph2-full.txt";
if ( file_exists($pph2file) ) {
copy($pph2file, './pph2-full.txt');
}
// print test query to file
if ( $subm_type == "example-sm" ) {
$test_query = "P01112";
save2file("input-sm_query.txt", $test_query);
$config = "submission type: saturation mutagenesis \n" .
"include EVmutation feature: False \n" .
"include environmental effects: False \n";
}
elseif ( $subm_type == "example-bq" ) {
$test_query = "P01112 99 Q R\nEGFR_HUMAN 300 V A\n";
save2file("input-batch_query.txt", $test_query);
$config = "submission type: batch query \n" .
"include EVmutation feature: False \n";
}
}
else {
// exit to error page
$err_msg = "Internal error: invalid submission type";
$arr = ["err_msg" => $err_msg, "back_link" => $back_link];
redirect("error.php", $arr);
}
// print configuration file
save2file("config.txt", $config);
// print email to file
if (! empty($_POST["email"]) ) {
save2file("input-email.txt", $_POST["email"]);
}
chdir($orig_dir);
if (false) {
// DEBUG: instead of launching the job, save variables to file and exit
$output = print_r($_POST, true);
echo '<pre>'; print_r($_POST); echo '</pre>';
file_put_contents("${jobdir}/input.log", $output);
$output = print_r($_FILES, true);
echo '<pre>'; print_r($_FILES); echo '</pre>';
file_put_contents("${jobdir}/input.log", $output, FILE_APPEND);
}
else {
// launch job in the background
$pyscript = "${orig_dir}/src/python/rhapsody_interface.py";
$pid_file = "${jobdir}/PID.tmp";
exec("nohup src/bin/launch_job.sh $jobdir $pyscript < /dev/null " .
">> ${scratch_dir}/launch_job.err 2>&1 & echo $! > $pid_file");
// clean workspace from old jobs
exec("nohup src/bin/clean_workspace.sh $scratch_dir < /dev/null " .
">> ${scratch_dir}/old_jobs.log 2>> ${scratch_dir}/old_jobs.err &");
// go to the progress page
$page = "status.php?id=${jobid}";
echo "<script type='text/javascript'> window.location.href='$page';</script>";
// I don't understand why redirect() does not work here...
// redirect("status.php", ["id" => $jobid]);
}
exit();
?>