-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.inc.php
328 lines (285 loc) · 10.9 KB
/
utilities.inc.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
<?php
// system call in a subprocess
function subprocess($cmd, &$stdout=null, &$stderr=null,$cwd=null) {
$proc = proc_open($cmd,[
1 => ['pipe','w'],
2 => ['pipe','w'],
],$pipes,$cwd,null);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
return proc_close($proc);
}
// define JBrowse tracktype from file format
function format2trackType($format,$fn=NULL){
if (!isset ($format) ){
return FALSE;
}
$program = NULL;
if ($fn){
if (preg_match('/^([A-Z]+)_/',basename($fn),$m) ){
$program = $m[1];
}
}
switch ($format){
case "BAM":
$type = "BAM";
break;
case "GTF":
case "GFF":
case "GFF3":
if ($program && $program == "ND"){
$type = "GFF_ND";
}elseif($program && $program == "NR"){
$type = "GFF_NR";
}elseif($program && $program == "TSS"){
$type = "GFF_TX";
}elseif($program && $program == "P"){
$type = "GFF_P";
}elseif($program && $program == "NFR"){
$type = "GFF_NFR";
}elseif($program && $program == "STF"){
$type = "GFF_GAU";
}else{
$type = "GFF";
}
break;
case "BW":
//case "BEDGRAPH":
//case "WIG":
if ($program && $program == "P"){
$type = "BW_P";
}else{
$type = "BW";
}
break;
default:
$type = 0;
}
return $type;
}
function createLink ($source, $target){
if (is_file($source))
unlink($source);
if (is_file($target) || is_link($target))
unlink($target);
touch($source);
symlink($source,$target);
}
//return html from
//SESSION['errorData'] = Array( 'seccionA' => Array( 'error msg A1', 'error msg A2'))
function printErrorData($targetSeccion=0){
$txt="";
foreach ($_SESSION['errorData'] as $seccion =>$lines) {
if ($targetSeccion && $targetSeccion != $seccion )
continue;
$txt .="<b>$seccion</b></br>";
if (!is_array($lines))
$lines[0] = $lines;
$txt .= "<span style=\"margin-left:45px;\"></span>";
$txt .= join("<br/><span style=\"margin-left:45px;\"></span>",$lines);
$txt .= "<br/>";
}
unset($_SESSION['errorData']);
return $txt;
}
function fromPrefix2Program($prefix){
$tools = $GLOBALS['toolsCol']->find(array('prefix' => array('$exists'=> true)));
if (empty($tools)){
$_SESSION['errorData']['Error'][]="Internal Error. Cannot extract any prefix from 'tools' collection";
return 0;
}
foreach ( $tools as $toolId => $d ){
if ($d['prefix'] == $prefix)
return $d['title'];
}
$_SESSION['errorData']['Warning'][]=" Prefix '$prefix"."_' not registered. File descriptions may not be complete.";
return 0;
}
// build text description for running jobs in datatables
function getJobDescription($descrip0,$jobSGE,$lastjobs){
$descrip = ($descrip0?$descrip0."<br/>":"");
if ($jobSGE['state'] == "RUNNING"){
$descrip = "<b>Job in course<b/><br/>".$descrip;
}elseif($jobSGE['state'] == "HOLD"){
$descrip .= "<br><strong>Job waiting</strong>";
# get info for dependent jobs from lastjobs
if (isset($jobSGE['jid_predecessor_list'])){
$depText = "";
$depPids = explode(",",$jobSGE['jid_predecessor_list']);
foreach ($depPids as $depPid){
if (isset($lastjobs[$depPid])){
$depText.=basename($lastjobs[$depPid]['out'][0])." ";
}
}
if ($depText)
$descrip .= " for predecessor analyses to finish: $depText";
}
}elseif($jobSGE['state'] == "ERROR"){
$descrip .= " Job in error.";
if (isset($jobSGE['error reason 1'])){
$descrip .= "<br/>".$jobSGE['error reason 1'];
}
}
return $descrip;
}
// build text description from systematic execution file names
function getDescriptionFromFN ($fn,$prefix=0){
$descr = "";
$ext = strtoupper(pathinfo($fn,PATHINFO_EXTENSION));
if (!$prefix){
if (preg_match('/([A-Z]+)_.+\.(\w+)$/',$fn,$m)){
$prefix = $m[1];
$ext = strtoupper($m[2]);
}
}
if ($ext){
switch ($ext){
case "SH":
$descr="Execution file";
break;
case "LOG":
$descr="Log file";
break;
case "GFF":
case "BW":
$descr="Result file";
break;
case "PNG":
$descr="Image file";
break;
case "BAM":
$descr="BAM file";
if (!$prefix)
$descr.=" is being sorted (if needed), indexed, and preprocessed for running Nucleosome Dynamics ";
break;
default:
$descr="$ext file";
}
if (preg_match('/E\d+/',$ext)){
$descr= "ERROR file";
}
}
if ($prefix){
$program = fromPrefix2Program($pre);
if ($program)
$descr.= " from $program";
}
return $descr;
}
//adds regular metadata
function saveMetadataUpload($fn,$request,$validationState){
// filters known metadata fields
$insertMeta = prepMetadataUpload($request,$validationState);
// save to mongo
$r = modifyMetadataBNS($fn,$insertMeta);
return $r;
}
// filters uploadForm2 request and formats mongo file metadata
function prepMetadataUpload($request,$validationState=0){
$fnPath = getAttr_fromGSFileId($fn,'path');
$format = (isset($request['format'])?$request['format']:"UNK");
$validated = $validationState;
$tracktype = format2trackType($format,$fnPath);
$visible = (isset($insertMeta['visible'])?$insertMeta['visible']:"1");
// compulsory metadata
$insertMeta=array(
'format' => $format,
'validated' => $validated,
'trackType' => $tracktype,
'visible' => $visible,
);
// GFF, BAM, BW,.. metadata
if (isset($request['refGenome'])) {$insertMeta['refGenome'] = $request['refGenome'];}
// BAM metadata
if (isset($request['paired'])) {$insertMeta['paired'] = $request['paired'];}
if (isset($request['sorted'])) {$insertMeta['sorted'] = $request['sorted'];}
if (isset($request['description'])) {$insertMeta['description']= $request['description'];}
// results metadata
if (isset($request['shPath'])) {$insertMeta['shPath'] = $request['shFile'];}
if (isset($request['logPath'])) {$insertMeta['logPath'] = $request['logFile'];}
if (isset($request['inPaths'])) {$insertMeta['inPaths'] = $request['inPaths'];}
if (isset($request['outPaths'])) {$insertMeta['outPaths'] = $request['outPaths'];}
return $insertMeta;
}
//formats and completes mongo file metadata from $meta and $lastjob
function prepMetadataResult($meta,$fnPath=0,$lastjob=Array() ){
if ($fnPath){
$extension = pathinfo("$fnPath",PATHINFO_EXTENSION);
$extension = preg_replace('/_\d+$/',"",$extension);
if (preg_match('/^E\d+$/',strtoupper($extension)) )
$extension="ERR";
}
if (!isset($meta['format']) && $fnPath)
$meta['format']= strtoupper($extension);
if (!isset($meta['tracktype']) && $fnPath )
$meta['tracktype']=format2trackType($meta['format'],basename($fnPath));
if (!isset($meta['inPaths']) && isset($lastjob['inPaths']) )
$meta['inPaths']=$lastjob['inPaths'];
if (!isset($meta['shPath']) && isset($lastjob['shPath']) )
$meta['shPath']=$lastjob['shPath'];
if (!isset($meta['logPath']) && isset($lastjob['logPath']) )
$meta['logPath']=$lastjob['logPath'];
if (!isset($meta['tool']) && isset($lastjob['tool']))
$meta['tool']=$lastjob['tool'];
if (!isset($meta['refGenome']) && in_array($meta['format'],array("BAM","GFF","GFF3","BW")) ){
if (isset($meta['inPaths']) ){
$inp = $meta['inPaths'][0];
$inpObj = $GLOBALS['filesMetaCol']->findOne(array('path' => $inp));
if (!empty($inpObj) && isset($inpObj['refGenome']) ){
$meta['refGenome']= $inpObj['refGenome'];
}
}
if (!isset($meta['refGenome']) && $fnPath ){
$fnCore = "";
$refGenome= "";
$ext = $meta['format'];
if (preg_match("/^[A-Z]+_\(\w+\)(.+)-\(\w+\)(.+)\.$ext/i",basename($fnPath),$m)) {
$fnCore = $m[1];
}elseif (preg_match("/^[A-Z]+_\(\w+\)(.+)-/",basename($fnPath),$m)) {
$fnCore = $m[1];
}elseif (preg_match("/^[A-Z]+_(.+)\.$ext/i",basename($fnPath),$m)) {
$fnCore = $m[1];
}elseif (preg_match("/^[A-Z]+_(.+)\./i",basename($fnPath),$m)) {
$fnCore = $m[1];
}else{
$fnCore = preg_replace("/.$ext/i","",basename($fnPath));
$fnCore = preg_replace("/^.*_/","",$fnCore);
}
$reObj = new MongoRegex("/".$_SESSION['User']['id'].".*".$fnCore."/i");
$relatedBAMS = $GLOBALS['filesMetaCol']->find(array('path' => $reObj));
if (!empty($relatedBAMS)){
$relatedBAMS->next();
$BAM = $relatedBAMS->current();
if (!empty($BAM))
$meta['refGenome'] = $BAM['refGenome'];
}
}
}
if (!isset($meta['description']) ){
$prefix = 0;
if ($meta['tool']){
$tool = $GLOBALS['toolsCol']->findOne(array('_id' => $meta['tool']));
$prefix = $tool['prefix'];
}
$meta['description'] = getDescriptionFromFN(basename($fnPath),$prefix);
}
if (!isset($meta['validated'])){
$meta['validated']=1;
}
if (!isset($meta['visible']) && $fnPath){
$meta['visible']=((in_array($extension,$GLOBALS['internalResults']))?0:1);
}
return $meta;
}
//completes $meta for log files based on expected outfile
function prepMetadataLog($metaOutfile,$logPath=0,$format="LOG"){
$metaLog = $metaOutfile;
$metaLog['format'] = $format;
$metaLog['tracktype'] = format2trackType($metaLog['format'],$logPath);
$metaLog['validated'] = 1;
$metaLog['visible'] = 1;
return $metaLog;
}
?>