Repository navigation
Expand file tree
/
Copy pathinput.tools.lib.php
More file actions
455 lines (329 loc) · 13.5 KB
/
Copy pathinput.tools.lib.php
File metadata and controls
455 lines (329 loc) · 13.5 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<?php
// UTILITIES
//
// get files from $fileList matching with given file type
function matchFormat_File($type, $fileList) {
$output = [];
// from tool, return empty and select from modal
if(empty($fileList)) return "";
// from ws / rerun, match format file with format tool field (type)
foreach ($fileList as $file) {
if(isset($file["fn"])) {
if(preg_grep("/".$file['format']."/i" , $type)) {
$p = explode("/", $file['path']);
$proj = getProject($p[1]);
$a[0] = $proj['name']. " / $p[2] / $p[3]";
$a[1] = $file['fn'];
$output[] = $a;
//return $output;
}
}
}
return $output;
}
// format PHP array to JS array
function getArrayJS($array) {
return preg_replace('/"/', '\'', json_encode($array));
}
// HEADER FUNCTIONS
//
// check if data request is correct
function InputTool_checkRequest($request) {
if (!isset($request['fn']) && !isset($request['rerunDir']) && !isset($request['op'])){
$_SESSION['errorData']['Error'][]="Please, before running this tool, select the correct files from the workspace or launch tool from the side menu.";
redirect($GLOBALS["BASEURL"].'workspace/');
}
}
// get if user is coming from ws, rerun or tool
function InputTool_getOrigin($request) {
$from = "";
// coming from workspace
if (isset($request['fn'])){
$from = "workspace";
}
// coming from rerun
if (isset($request['rerunDir'])){
$from = "rerun";
}
// coming from tool
if (isset($request['op'])){
$from = "tool";
}
return $from;
}
function InputTool_getPathsAndRerun($request) {
$output = [];
$output[0] = [];
$output[1] = [];
if ($request['rerunDir']){
$dirMeta = $GLOBALS['filesMetaCol']->findOne(array('_id' => $request['rerunDir']));
if (!is_array($dirMeta['input_files']) && !isset($dirMeta['arguments'])){
$_SESSION['errorData']['Error'][]="Cannot rerun job ".$request['rerunDir'].". Some folder metadata is missing.";
redirect($GLOBALS["BASEURL"].'workspace/');
}
if (is_array($dirMeta['input_files'][0])){
$_SESSION['errorData']['Internal'][]="Cannot rerun job ".$request['rerunDir'].". New directory metadata not implemeted yet.";
redirect($GLOBALS["BASEURL"].'workspace/');
}
foreach ($dirMeta['input_files'] as $fn){
$file['path'] = getAttr_fromGSFileId($fn,'path');
$file['fn'] = $fn;
$file['format'] = getAttr_fromGSFileId($fn,'format');
array_push($output[1],$file);
}
$output[0] = $dirMeta['arguments'];
}else{
if (!is_array($request['fn']))
$request['fn'][]=$request['fn'];
foreach($request['fn'] as $fn){
$file['path'] = getAttr_fromGSFileId($fn,'path');
$file['fn'] = $fn;
$file['format'] = getAttr_fromGSFileId($fn,'format');
array_push($output[1],$file);
}
//array_push($output[1],getAttr_fromGSFileId($fn,'path'));
}
return $output;
}
function InputTool_getDefExName() {
// default execution name
$dirNum="000";
$dataDirPath = getAttr_fromGSFileId($_SESSION['User']['dataDir'],"path");
$reObj = new MongoRegex("/^".$dataDirPath."\\/run\d\d\d$/i");
$prevs = $GLOBALS['filesCol']->find(array('path' => $reObj, 'owner' => $_SESSION['User']['id']));
if ($prevs->count() > 0){
$prevs->sort(array('path' => -1));
$prevs->next();
$previous = $prevs->current();
if (preg_match('/(\d+)$/',$previous["path"],$m) ){
$dirNum= sprintf("%03d",$m[1]+1);
}
}
$dirName="run".$dirNum;
$prevs = $GLOBALS['filesCol']->find(array('path' => $dataDirPath."/$dirName", 'owner' => $_SESSION['User']['id']));
if ($prevs->count() > 0){
$dirName="run".rand(100, 999);
}
return $dirName;
}
// PROJECTS
//
// get list of projects
function InputTool_getSelectProjects() {
$projects = getProjects_byOwner();
$output = '<select class="form-control" id="select_project" name="project">';
foreach ($projects as $project_id => $project){
$selected="";
$project_code = basename($project['path']);
if ($project_code == $_SESSION['User']['activeProject'])
$selected = " selected ";
$output.="<option value=\"$project_code\" $selected >".$project['name']."</option>";
}
$output .='</select>';
echo $output;
}
// FORM GENERIC FUNCTIONS
//
// print select file(s)
function InputTool_printSelectFile($input, $rerun, $ff, $multiple, $required) {
$req = "field_not_required";
if($required) $req = "field_required";
if(!$multiple) {
$labelopt = (!$required) ? " (optional)" : "";
$output = '<div class="form-group">
<label class="control-label">'.$input['description'].$labelopt.' <i class="icon-question tooltips" data-container="body" data-html="true" data-placement="right" data-original-title="<p align=\'left\' style=\'margin:0\'>'.$input['help'].'</p>"></i></label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file"></i></span>
<input type="text"
name="visible_'.$input['name'].'"
class="form-control form-field-enabled '.$req.'"
placeholder="'.$GLOBALS['placeholder_input'].'"
value="'.$ff[0].'"
readonly>
<input type="hidden" class="form-field-enabled" name="input_files['.$input['name'].']" value="'.$ff[1].'">
<span class="input-group-btn input-tool">
<a href="javascript:cleanInput(\'visible_'.$input['name'].'\', \'input_files['.$input['name'].']\', 0);" class="clean-input"><i class="fa fa-times-circle"></i></a>
<button class="btn green" type="button"
onclick="toolModal(\'visible_'.$input['name'].'\', \'input_files['.$input['name'].']\', '.getArrayJS($input['data_type']).', '.getArrayJS($input['file_type']).', false);"><i class="fa fa-check-square-o"></i> Select</button>
</span>
</div>
</div>';
} else {
/*
* TODO!!
* $output = '<div class="form-group">
<label class="control-label">MNase-seq data <i class="icon-question tooltips" data-container="body" data-html="true" data-placement="right" data-original-title="<p align='left' style='margin:0'>Input MNase-seq data used to compute nucleosome positioning and the subsequent analyses.</p>"></i></label>
<div class="input-group">
<span class="input-group-addon" style="vertical-align: top; padding-top: 10px;"><i class="fa fa-file"></i></span>
<textarea
name="visible_MNaseSeq"
class="form-control form-field-enabled params_input"
style="height:<?php echo ($r != 0 ? $r*34 : 34); ?>px"
placeholder="<?php echo $GLOBALS['placeholder_textarea']; ?>"
readonly><?php echo implode("\n", $p); ?></textarea>
<div id="hidden_visible_MNaseSeq">
<?php foreach($ff as $fi) { ?>
<input type="hidden" name="input_files[MNaseSeq][]" value="<?php echo $fi[1]; ?>">
<?php } ?>
</div>
<span class="input-group-btn input-tool">
<button class="btn green" type="button"
onclick="toolModal('visible_MNaseSeq', 'input_files[MNaseSeq][]', <?php echo getArrayJS($tool['input_files']['condition1']['data_type']); ?>, true);"><i class="fa fa-check-square-o"></i> Select</button>
</span>
</div>
</div>';*/
}
echo $output;
}
// print list of files (in select)
function InputTool_printListOfFiles($input, $rerun, $required) {
$req = "field_not_required";
if($required) $req = "field_required";
$output = '<div class="form-group">
<label class="control-label">'.$input['description'].' <i class="icon-question tooltips" data-container="body" data-html="true" data-placement="right" data-original-title="<p align=\'left\' style=\'margin:0\'>'.$input['help'].'</p>"></i></label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file"></i></span>
<select name="input_files_public_dir['.$input['name'].']" class="form-control '.$req.'">
<option value="">Select the '.$input['description'].'</option>';
$tool_options = $input['enum_items'];
for ($i=0; $i<count($tool_options['name']); $i++){
if($tool_options['name'][$i] == $rerun) $sel = "selected";
else $sel = "";
$output .= '<option value="'.$tool_options['name'][$i].'" '.$sel.'>'.$tool_options['description'][$i].'</option>';
}
$output .= '</select>
</div>
</div>';
echo $output;
}
// print input
function InputTool_printInput($input, $type) {
$req = "field_not_required";
if($input["required"]) $req = "field_required";
$max = $input["maximum"];
$min = $input["minimum"];
if(isset($max) && isset($min)) {
$range = 'min="'.$min.'" max="'.$max.'"';
}
if($type == "number") {
if(isset($max) && isset($min)) {
if(($max - $min) > 10) $st = 1;
if(($max - $min) < 10 && ($max - $min) > 1) $st = 0.1;
if(($max - $min) < 1) $st = 0.01;
} else {
$st = "any";
}
$step = 'step="'.$st.'"';
}
if(($input['default'] !== null) && ($input['default'] !== "null")) $value = $input['default'];
else $value = "";
$output = '<div class="form-group">
<label class="control-label">'.$input['description'].' <i class="icon-question tooltips" data-container="body" data-html="true" data-placement="right" data-original-title="<p align=\'left\' style=\'margin:0\'>'.$input['help'].'</p>"></i></label>
<input type="'.$type.'" '.$range.' '.$step.' name="arguments['.$input['name'].']" id="'.str_replace(":", "_", $input['name']).'" class="form-control form-field-enabled '.$req.'" value="'.$value.'">
</div>';
return $output;
}
// print input
function InputTool_printInputHidden($input, $type) {
$req = "field_not_required";
if($input["required"]) $req = "field_required";
if(($input['value'] !== null) && ($input['value'] !== "null")) $value = $input['value'];
else $value = "";
$output = '<input type="hidden" name="arguments['.$input['name'].']" id="'.str_replace(":", "_", $input['name']).'" class="form-control '.$req.'" value="'.$value.'">';
return $output;
}
// print select
function InputTool_printSelect($input) {
$req = "field_not_required";
if($input["required"]) $req = "field_required";
if(($input['default'] !== null) && ($input['default'] !== "null")) $default = $input['default'];
else $default = "";
if($default === "false") $default = [0];
if($default === "true") $default = [1];
if($input["type"] == "boolean") {
$tool_options["name"] = [1, 0];
$tool_options["description"] = ["True", "False"];
} else {
$tool_options = $input['enum_items'];
}
$output = '<div class="form-group">
<label class="control-label">'.$input['description'].' <i class="icon-question tooltips" data-container="body" data-html="true" data-placement="right" data-original-title="<p align=\'left\' style=\'margin:0\'>'.$input['help'].'</p>"></i></label>
<select name="arguments['.$input['name'].']" id="'.str_replace(":", "_", $input['name']).'" class="form-control '.$req.'">';
$sel = "";
for ($i=0; $i<count($tool_options['name']); $i++) {
if(($default != "") && in_array($tool_options['name'][$i], $default)) $sel = "selected";
else $sel = "";
$output .= '<option value="'.$tool_options['name'][$i].'" '.$sel.'>'.$tool_options['description'][$i].'</option>';
}
$output .= '</select>
</div>';
return $output;
}
// print select multiple
function InputTool_printSelectMultiple($input) {
$req = "field_not_required";
if($input["required"]) $req = "field_required";
if(($input['default'] !== null) && ($input['default'] !== "null") && ($input['default'] !== "")) $default = array_values($input['default']);
else $default = "";
$tool_options = $input['enum_items'];
$output = '<div class="form-group">
<label class="control-label">'.$input['description'].' <i class="icon-question tooltips" data-container="body" data-html="true" data-placement="right" data-original-title="<p align=\'left\' style=\'margin:0\'>'.$input['help'].'</p>"></i></label>
<select name="arguments['.$input['name'].'][]" id="'.str_replace(":", "_", $input['name']).'" class="form-control '.$req.'" multiple="multiple">';
$sel = "";
for ($i=0; $i<count($tool_options['name']); $i++) {
if(($default != "") && in_array($tool_options['name'][$i], $default)) $sel = "selected";
else $sel = "";
$output .= '<option value="'.$tool_options['name'][$i].'" '.$sel.'>'.$tool_options['description'][$i].'</option>';
}
$output .= '</select>
</div>';
return $output;
}
// print field
function InputTool_printField($input, $rerun) {
if($input["required"]) $req = "field_required";
switch($input["type"]) {
case 'string': $field = "input";
$type = "text";
break;
case 'enum':
case 'boolean': $field = "select";
break;
case 'enum_multiple': $field = "select_multiple";
break;
case 'integer':
case 'number': $field = "input";
$type = "number";
break;
case 'hidden': $field = "input";
$type = "hidden";
break;
}
switch($field) {
case "input": if($rerun) $input["default"] = $rerun;
if($type == "hidden") $output = InputTool_printInputHidden($input, $type);
else $output = InputTool_printInput($input, $type);
break;
case "select": if($rerun) $input["default"] = [$rerun];
$output = InputTool_printSelect($input);
break;
case "select_multiple": if($rerun) $input["default"] = $rerun;
$output = InputTool_printSelectMultiple($input);
break;
}
return $output;
}
// print the whole form for standard tools
function InputTool_printSettings($arguments, $rerun) {
$output = '';
$c = 0;
foreach($arguments as $arg) {
if(($c % 2) == 0) $output .= '<div class="row">';
$output .= '<div class="col-md-6">';
$output .= InputTool_printField($arg, $rerun[$arg['name']]);
$output .= '</div>';
if(($c % 2) != 0) $output .= '</div>';
$c ++;
}
echo $output;
}