-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessValidation.php
330 lines (269 loc) · 10.3 KB
/
processValidation.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
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
<?php
require "../phplib/genlibraries.php";
redirectOutside();
$resp = Array (
'fileName' => "",
'fileId' => "",
'msg' => "",
'state' => 0,
);
if(!$_REQUEST['op']){
$resp['msg']="Internal error: Operation not received. Restart form<br/>";
print json_encode($resp);
die();
}
if(!$_REQUEST['fn']){
$resp['msg']="Internal error: Select the file to validate.</br>";
print json_encode($resp);
die();
}elseif(is_array($_REQUEST['fn'])){
$resp['msg']="Internal error: A list of files given. Expecting only one.</br>";
print json_encode($resp);
die();
}
$fn = $_REQUEST['fn'];
$fnPath = getAttr_fromGSFileId($fn,'path');
$rfn = $GLOBALS['dataDir']."/".$fnPath;
$resp['fileId'] = $fn;
$resp['fileName'] = basename($fnPath);
if (! is_file($rfn)){
$resp['msg'] ="Error: Cannot find file '".basename($fnPath)."' . Not stored in disk anymore.</br>";
print json_encode($resp);
die();
}
$fileData = $GLOBALS['filesCol']->findOne(array('_id' => $fn, 'owner' => $_SESSION['User']['id']));
$fileMeta = $GLOBALS['filesMetaCol']->findOne(array('_id' => $file));
if (empty ($fileData) ){
$resp['msg'] ="Error: Cannot validate '".basename($fnPath)."'. File do not belong to the user currently logged.</br>";
print json_encode($resp);
die();
}
// define operations
switch ($_REQUEST['op']) {
case '1':
// check obligatory fields and build validation action list ($_SESSION['validation'])
// returns file state and validation/error info
// 0 = ERROR - $_SESSION['errorData'] is set
// 1 = VALIDATED - $_SESSION['validation'] is empty
// 2 = READY - $_SESSION['validation'] has pending actions
unset($_SESSION['errorData']);
// restart validation action list
if (isset($_SESSION['validation'][$fn]))
unset($_SESSION['validation'][$fn]);
// check compulsory fields
if (!isset($_REQUEST['format'])){
$resp['msg']="Missing compulsory fields. Please, especify file format.</br>";
break;
}
$_SESSION['validation'][$fn]['format']=$_REQUEST['format'];
switch ( $_REQUEST['format'] ) {
case 'BAM':
if (!isset($_REQUEST['refGenome']) || !isset($_REQUEST['paired']) || !isset($_REQUEST['sorted'])){
$resp['msg']="Missing compulsory fields. Please, especify: reference genome, sorted/unsorted and paired/single.</br>";
$resp['state'] = 0;
break;
}
if (($_REQUEST['sorted']!= "sorted" && $_REQUEST['sorted']!= 1) && (!isset($fileMeta['sorted']) || $fileMeta['sorted']=="unsorted")){
$resp['msg'] = "The BAM file will be sorted and indexed.</br>";
$_SESSION['validation'][$fn]['action']["sort"]=0;
$_SESSION['validation'][$fn]['action']["index"]=0;
$resp['state'] = 2;
}else{
if (!is_file($rfn.".bai") ){
$resp['msg'] = "The BAM file will be indexed.</br>";
$_SESSION['validation'][$fn]['action']["index"]=0;
$resp['state'] = 2;
}else{
$resp['msg'] = "BAM file already indexed.</br>";
$resp['state'] = 1;
}
}
break;
case 'BEDGRAPH';
case 'WIG':
case 'BED':
if (!isset($_REQUEST['refGenome'])){
$resp['msg']="Missing compulsory fields. Please, especify reference genome.</br>";
$resp['state'] = 0;
break;
}
//$resp['msg'] = "File will be converted to BW";
//$_SESSION['validation'][$fn]['action']["convert"]=0;
break;
case 'GFF':
case 'GFF3':
if (!isset($_REQUEST['refGenome'])){
$resp['msg']="Missing compulsory fields. Please, especify reference genome.</br>";
$resp['state'] = 0;
break;
}
break;
default:
# other formats accepted as uploaded
$resp['msg'] = "Metadata file is valid</br>";
$resp['state'] = 1;
break;
}
// check formats and chrs names
if ( $resp['state'] != 0 && $_REQUEST['refGenome']){
$valid = validateUPLOAD($fn,$rfn,$_REQUEST['refGenome'],$_REQUEST['format']);
// add function error msgs to resp
if (! $valid){
$resp['msg'] .= printErrorData();
$resp['msg'] .= "File '".basename($fnPath)."' <b>not validated</b>. Please, mend your warnings/errors or upload the file again<br/>";
$resp['state'] = 0;
// translate pending accions into nice msgs
}else{
if (!isset($_SESSION['validation'][$fn]['action'])){
$resp['msg'] .= "Genomic coordinates successfully mapped against ".$_REQUEST['refGenome']." genome.<br/>";
$resp['state'] = 1;
}else{
$resp['state'] = 2;
if (isset($_SESSION['validation'][$fn]['action']['substitutions']) ){
foreach ($_SESSION['validation'][$fn]['action']['substitutions'] as $sub => $r){
$resp['msg'] .= $_REQUEST['format']." chromosome name not in reference sequence. The following transformation will be performed: $sub<br/>";
}
}
if (isset($_SESSION['validation'][$fn]['action']['enqueue_chrNames']) ){
$resp['msg'] .= "Chromosome names in BAM will be validated. If they do not match the names of reference genome, your BAM will modified to do so (i.e. 'Chr2' => 'ChrII'). If no equivalent name can be found, the validation will fail.</br>";
}
}
}
}
// set file state according to SESSION['errorData'] and SESSION['validation']
if ( isset($_SESSION['validation'][$fn]['action']))
$resp['state']= 2;
if (isset($_SESSION['errorData']))
$resp['state']= 0;
// save metadata if file already validated
if ($resp['state'] == 1 ){
$ok = saveMetadataUpload($fn,$_REQUEST,1);
if (!$ok){
$resp['msg'] .= printErrorData();
$resp['state']= 0;
}else{
$resp['msg'] .= basename($fnPath). " successfully validated<br/>";
}
}
break;
case 'uncompress':
break;
case '2':
// execute validation action list (reading $_SESSION['validation'])
// returns file state and validation/error info
// 0 = ERROR
// 1 = READY
if (!isset($_SESSION['validation'][$fn]) ){
$resp['msg'] = "Nothing else to do for '".basename($fnPath)."'! File will we set as valid.<br/>";
$resp['state'] = 1;
print json_encode($resp);
}
$format = $_SESSION['validation'][$fn]['format'];
switch($format){
case 'BAM':
$subs = get_seds_fromChrNameValidation($fn);
$sort = (isset($_SESSION['validation'][$fn]['action']['sort'] )? true:false);
$index = (isset($_SESSION['validation'][$fn]['action']['index'] )? true:false);
$chrs = (isset($_SESSION['validation'][$fn]['action']['enqueue_chrNames'])? true:false); #TODO !!!
if (count($subs) || $sort || $index || $chrs){
$bamFn = $rfn;
$dirFn = str_replace("/".basename($fnPath),"",$fnPath);
$dirRfn = $GLOBALS['dataDir']."/".$dirFn;
$dirTmp = $GLOBALS['dataDir']."/".$_SESSION['User']['id']."/.tmp";
if (! is_dir($dirTmp)){
if(!mkdir($dirTmp, 0775, true)) {
$_SESSION['errorData']['error'][]="Cannot create temporal file $dirTmp . Please, try it later.";
$resp['state']=0;
break;
}
}
$toolId = "BAMval";
$tool = $GLOBALS['toolsCol']->findOne(array('_id' => $toolId));
if (empty($tool)){
$_SESSION['errorData']['Error'] = "Cannot submit job of type: $toolId. Tool not set.";
$resp['state']=0;
$resp['msg'] .= printErrorData();
break;
}
$shName = queueBAMvalidation($fn,$dirTmp,$dirRfn,$bamFn,$_REQUEST['paired'],$sort,$subs,$index,1);
$pid = execJob($dirTmp,"$dirRfn/$shName",$tool);
if ($pid){
$resp['state'] = 3;
$logName = str_replace(".sh",".log",$shName);
$_REQUEST['sorted'] = "sorted";
$REQUEST['logPath'] = "$dirFn/$logName";
$REQUEST['shPath'] = "$dirFn/$shName";
$jobInfo = Array('_id' => $pid,
'title' => basename($fnPath)." validation",
'description' => "BAM will be sorted and indexed",
'inPaths' => Array($fnPath),
'outPaths' => Array($fnPath,"$fnPath.bai"),
'logPath' => "$dirFn/$logName",
'shPath' => "$dirFn/$shName",
'outDir' => $dirFn,
'tool' => $toolId,
'metaData' => Array(prepMetadataUpload($_REQUEST,$resp['state']),Array("visible"=>0)) #foreach outPaths
);
addUserJob($_SESSION['User']['_id'],$jobInfo,$pid);
unset($_SESSION['validation'][$fn]);
}else{
$resp['state']=0;
$_SESSION['errorData']['Error'][]="Cannot submit BAM preprocessing to the queue. Try it later, sorry.";
$resp['msg'] .= printErrorData();
break;
}
}else{
$resp['state']= 1;
unset($_SESSION['validation'][$fn]);
$_REQUEST['sorted'] = "sorted";
}
break;
case 'BEDGRAPH':
case 'WIG':
case 'BED':
case 'GFF':
case 'GFF3':
$ok = processUPLOAD($file);
if (!$ok){
$resp['msg'] .= printErrorData();
$resp['state']= 0;
}else{
unset($_SESSION['validation'][$fn]);
$resp['msg'] .= basename($fnPath). " processed<br/>";
$resp['state']= 1;
}
break;
default:
$resp['msg'] .= "Nothing to do in '".basename($fnPath). "'.<br/>";
$resp['state']= 1;
}
// set file state according to SESSION['errorData'] and SESSION['validation']
if ( isset($_SESSION['validation'][$fn]['action']))
$resp['state']= 2;
if (isset($_SESSION['errorData']))
$resp['state']= 0;
// save metadata if file already validated or is enqueued
if ($resp['state'] == 1 ){
$ok = saveMetadataUpload($fn,$_REQUEST,1);
if (!$ok){
$resp['msg'] .= printErrorData();
$resp['state']= 0;
}else{
$resp['msg'] .= basename($fnPath). " successfully validated<br/>";
}
}elseif ($resp['state'] == 3){
$ok = saveMetadataUpload($fn,$_REQUEST,3);
if (!$ok){
$resp['msg'] .= printErrorData();
$resp['state']= 0;
}else{
$resp['msg'] .= basename($fnPath). " validation process has being submited to the server. The task could take some time to run. Return to 'User Workspace' for monitoring it.<br/>";
}
}
break;
//no format
default:
break;
}
print json_encode($resp);
?>