-
Notifications
You must be signed in to change notification settings - Fork 9
/
files.php
executable file
·389 lines (352 loc) · 16.8 KB
/
files.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
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
<?php
##########################################################################
# Copyright 2013, Philip Ewels ([email protected]) #
# #
# This file is part of Labrador. #
# #
# Labrador is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# Labrador is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with Labrador. If not, see <http://www.gnu.org/licenses/>. #
##########################################################################
include('includes/start.php');
if(isset($_GET['id']) && is_numeric($_GET['id'])){
$project_id = $_GET['id'];
$projects = mysqli_query($dblink, "SELECT * FROM `projects` WHERE `id` = '".$project_id."'");
if(mysqli_num_rows($projects) == 1){
$project = mysqli_fetch_array($projects);
} else {
header("Location: index.php");
}
} else {
header("Location: index.php");
}
if(isset($_POST['java_download_paths']) && $_POST['java_download_paths'] == 'Download Checked Files With Java Applet'){
$i = 1;
$paths = array();
while(isset($_POST["path_$i"])){
if(isset($_POST["check_$i"]) && $_POST["check_$i"] == 'on'){
$paths[] = $project['name'].$_POST["path_$i"];
}
$i++;
}
// no checked boxes - download all
if(count($paths) == 0){
$i = 1;
while(isset($_POST["path_$i"])){
$paths[] = $project['name'].$_POST["path_$i"];
$i++;
}
}
$_SESSION['files'] = $paths;
}
include('includes/header.php'); ?>
<div class="sidebar-nav">
<h3 id="sidebar_project_title">
<?php echo '<a href="project.php?id='.$project_id.'">'.$project['name'].'</a>'; ?></h3>
<ul class="project-tabs">
<li>
<a href="project.php?id=<?php echo $project_id; ?>">Project Details</a>
</li>
<li>
<a href="datasets.php?id=<?php echo $project_id; ?>">Datasets</a>
</li>
<li>
<a href="reports.php?id=<?php echo $project_id; ?>">Reports</a>
</li>
<li class="active">
<a href="files.php?id=<?php echo $project_id; ?>">Files</a>
</li>
</ul>
</div>
<div class="sidebar-mainpage project-mainpage">
<a class="labrador_help_toggle pull-right" href="#labrador_help" title="Help"><i class="icon-question-sign"></i></a>
<?php project_header($project); ?>
<?php if(isset($_POST['java_download_paths']) && $_POST['java_download_paths'] == 'Download Checked Files With Java Applet'){ ?>
<div class="labrador_help" style="display:none;">
<div class="well">
<h3>Downloads with the Java Applet</h3>
<p>The Java applet below allows many large files to be downloaded from the server to your computer in succession. This is useful, as trying to download a lot of data through your browser
can fail and choke the network. If you are downloading many files, the applet is better as it maintains the directory structure found on the server, so keeping your files organised and not over-writing anything.</p>
<p>To use the applet, choose the folder that you would like the files to be downloaded to by clicking 'Browse'. Select the files to be downloaded (these should be those selected in the previos page) and click Download.</p>
<p>You can read the full <a href="<?php echo $labrador_url; ?>/documentation/">Labrador documenation here</a>.</p>
</div>
</div>
<applet code="biz.jupload.jdownload.Manager" archive="includes/jdownload/jdownload.jar" width="100%" height="500px" name="JDownload" mayscript="mayscript" alt="JDownload by www.jupload.biz">
<!-- Java Plug-In Options -->
<param name="progressbar" value="true">
<param name="boxmessage" value="Loading JDownload Applet ...">
<!-- URL pointing to the data structure containing the list of files and folders to download -->
<param name="dataURL" value="ajax/java_download_xml.php">
<!-- Show or Hide the controls. If hidden (set all to 'false'), remote control the applet using JavaScript buttons -->
<param name="showExplorer" value="true">
<param name="showControls" value="true">
<param name="showBrowser" value="true">
<param name="showStatus" value="true">
<!-- Error message for browsers not supporting Java applets -->
Your browser does not support applets, or you have disabled applets in your options.
To use this applet, please update your Java. You can get it from <a href="http://www.java.com/">java.com</a>
</applet>
<?php } else { ?>
<div class="labrador_help" style="display:none;">
<div class="well">
<h3>Files Page</h3>
<p>You can use this page to download data from the Bioinformatics server to your computer.</p>
<p>File size and Genome is dynamically generated from the files themselves - BAM/SAM and SeqMonk files record the genome that they use within their file headers, which is extracted below.</p>
<p>Files within the project directory will be matched up to datasets, those associated with the datasets selected on the previous page will be shown below, along with any not matched to a dataset.
You can filter the table for files with extensions commonly used for Aligned data, Raw data, Processing Reports and everything else. You can also use the text box to filter the file names. The table can be sorted by column heading.</p>
<p>There are two ways to download files: either <strong>click the file name</strong> to download the file through your browser, or select the files you wish to download and press <strong>Download Checked Files With Java Applet</strong>.
The Java applet is best for downloading large files and maintains directory struture, useful if downloading many files.</p>
<p>You can read the full <a href="<?php echo $labrador_url; ?>/documentation/">Labrador documenation here</a>.</p>
</div>
</div>
<form action="files.php" method="get" class="form-horizontal">
<input type="hidden" name="id" value="<?php echo $project_id; ?>">
<div class="well">
<!-- <input type="submit" name="java_download_paths" class="btn btn-primary pull-right" value="Download Checked Files With Java Applet"> -->
Filter files:
<div class="btn-group">
<button class="btn" id="filter_projects">Projects</button>
<button class="btn" id="filter_aligned">Aligned</button>
<button class="btn" id="filter_raw">Raw</button>
<button class="btn" id="filter_reports">Reports</button>
<button class="btn" id="filter_other">Other</button>
</div>
<input type="text" class="input-small filter_text" id="name" placeholder="Filename">
<select name="ds" id="filter_dataset" class="span4">
<option value="all">All Datasets</option>
<option value="none" <?php if(isset($_GET['ds']) && $_GET['ds'] == 'none') { echo 'selected="selected"'; } ?>>Unmatched</option>
<?php
$dataset_query = mysqli_query($dblink, "SELECT * FROM `datasets` WHERE `project_id` = '$project_id'");
if(mysqli_num_rows($dataset_query) > 0){
while($dataset = mysqli_fetch_array($dataset_query)){
echo '<option value="'.$dataset['id'].'"';
if(isset($_GET['ds']) && $_GET['ds'] == $dataset['id']){
echo ' selected="selected"';
}
echo '>'.$dataset['name'].'</option>';
}
}
?>
</select>
<span class="help-block" style="margin:10px 0 0;"><?php echo $download_instructions; ?></span>
</div>
</form>
<form action="files.php?id=<?php echo $project_id; ?>" method="post">
<?php
// Check directory exists
if(file_exists($data_root.$project['name'])){
// Get dataset details for filename search needles
$datasets = array();
$orphans = array();
$sql = "SELECT * FROM `datasets` WHERE `project_id` = '$project_id'";
if(isset($_GET['ds']) && is_numeric($_GET['ds'])){
$sql .= " AND `id` = '".$_GET['ds']."'";
}
$dataset_query = mysqli_query($dblink, $sql);
if(mysqli_num_rows($dataset_query) > 0){
while ($dataset = mysqli_fetch_array($dataset_query)){
$id = $dataset['id'];
$datasets[$id] = $dataset;
$datasets[$id]['files'] = array();
}
}
// Loop through files and match to datasets
$num_paths = 0;
$dir = $data_root.$project['name'];
$it = new RecursiveDirectoryIterator($dir);
foreach(new RecursiveIteratorIterator($it) as $file) {
$path = $file->getPathname();
if($file->isLink()){
# File is a symlink - getSize() will throw a fatal error.
$size = "0";
} else {
$size = $file->getSize();
}
$matched = false;
if(substr($path, -1) !== '~' && substr(basename($path), 0, 1) !== '.' && !stripos($path, 'fastqc/')){
$num_paths++;
// Look for SRA accessions first
foreach ($datasets as $id => $dataset){
$accessions = explode(" ", $dataset['accession_sra']);
foreach($accessions as $accession){
if(stripos($path, $accession)){
$datasets[$id]['paths'][$path] = $size;
$matched = true;
break 2;
}
}
}
// Look for GEO accessions
if(!$matched){
foreach ($datasets as $id => $dataset){
$accessions = explode(" ", $dataset['accession_geo']);
foreach($accessions as $accession){
if(stripos($path, $accession)){
$datasets[$id]['paths'][$path] = $size;
$matched = true;
break;
}
}
}
}
// Still nothing - look for names
if(!$matched){
foreach ($datasets as $id => $dataset){
if(stripos($path, $dataset['name'])){
$datasets[$id]['paths'][$path] = $size;
$matched = true;
break;
}
}
}
// Can't find this one - an orphan
if(!$matched && (!isset($_GET['ds']) || $_GET['ds'] == 'all' || $_GET['ds'] == 'none')){
$orphans[$path] = $size;
}
}
}
// Kill matched dataset paths if filtering for unmatched
if(isset($_GET['ds']) && $_GET['ds'] == 'none'){
$datasets = array();
}
?>
<table class="table table-condensed table-bordered table-striped sortable download_table">
<thead>
<tr>
<th class="select" style="width:20px;"><input type="checkbox" class="select-all"></th>
<th data-sort="string-ins" style="width:30%;">Dataset Name</th>
<th data-sort="int" style="width:10%;">File Size</th>
<th data-sort="string-ins" style="width:15%;">Genome</th>
<th data-sort="string-ins">Filename</th>
<th data-sort="string-ins" style="width:7.5%;">Links</th>
</tr>
</thead>
<tbody>
<?php
function find_genome($path){
if(!isset($samtools_path)){
$samtools_path = '';
}
$genome = '';
// Find genome from BAM or SAM files
if(substr($path, -4) == '.bam' || substr($path, -4) == '.bam'){
$bam_header = shell_exec (escapeshellcmd ($samtools_path.'samtools view -H '.$path));
$bam_headers = explode("\n", $bam_header);
foreach($bam_headers as $header){
if(stripos($header, 'Genomes/')){
$genomes = explode(" ", substr($header, stripos($header, 'Genomes/') + 8));
$genomes2 = preg_split("/", $genomes[0]);
$genome = $genomes2[0].' - '.$genomes2[1];
}
}
}
// Find genome from SeqMonk projects
if(substr($path, -4) == '.smk' || substr($path, -7) == '.smk.gq'){
$type = shell_exec (escapeshellcmd ('file '.$path));
$types = explode(": ", $type);
$type = $types[1];
if(substr($type,0,4) == 'gzip'){
$header = shell_exec ('gunzip -c '.$path.' | head');
} else {
$header = shell_exec (escapeshellcmd ('head '.$path));
}
$headers = explode("\n", $header);
$genomes = explode("\t", $headers[1]);
$genome = $genomes[1].' - '.$genomes[2];
}
return $genome;
}
function find_parameters($path){
// BAM and SAM files
if(substr($path, -4) == '.bam' || substr($path, -4) == '.bam'){
$bam_header = shell_exec (escapeshellcmd ($samtools_path.'samtools view -H '.$path));
$bam_headers = explode("\n", $bam_header);
foreach($bam_headers as $header){
if(stripos($header, 'Genomes/')){
return '<i class="icon-info-sign" title="'.htmlspecialchars ($header).'"></i>';
}
}
}
}
function make_url_alias($labrador_url,$dataset_name,$data_alias,$path){
if(isset($data_alias)){
$url_split = parse_url($labrador_url,PHP_URL_HOST);
$url_short = $url_split. '/'.$data_alias . '/' . $dataset_name . $path;
return $url_short;
}
else {
return ($labrador_url.$dataset_name.$path); }
}
function make_webstartlink($url_short,$genome){
// BAM files
$bam_web_link = "";
if(substr($url_short, -4) == '.bam'){
if(!isset($genome)){ $genome = 'hg38'; }
$bam_web_link = '<BR><a href="http://www.broadinstitute.org/igv/projects/current/igv.php?sessionURL=http://' . $url_short . '&genome='.$genome.'">[Launch IGV]</a><FONT style="font-size:6pt">(Broken in Chrome)</FONT>';
} // if BAM
return $bam_web_link;
}
$j = 0;
foreach($datasets as $dataset){
$paths = $dataset['paths'];
ksort($paths);
foreach($paths as $raw_path => $size){
if($size > 750){
$j++;
$path = substr($raw_path, strlen($dir)); ?>
<tr>
<td class="select"><input type="checkbox" class="select-row" id="check_<?php echo $j; ?>" name="check_<?php echo $j; ?>"><input type="hidden" name="path_<?php echo $j; ?>" value="<?php echo $path; ?>"></td>
<td><?php echo $dataset['name']; ?></td>
<td data-sort-value="<?php echo $size; ?>"><?php echo human_filesize($size); ?></td>
<td><?php echo find_genome($raw_path); ?> <?php echo find_parameters($raw_path); ?></td>
<td class="path"><a href="download_file.php?fn=<?php echo substr($raw_path, strlen($data_root)); ?>"><?php echo $path; ?></a></td>
<td><a href="http://<?php echo make_url_alias($labrador_url,$dataset['name'],$data_alias,$path); ?>">[Hard Link]</a>
<?php echo make_webstartlink(make_url_alias($labrador_url,$dataset['name'],$data_alias,$path)); ?></td>
</tr>
<?php
} // human_readable if statement
} //foreach path
} // foreach dataset
ksort($orphans);
foreach ($orphans as $raw_path => $size){
$j++;
$path = substr($raw_path, strlen($dir)); ?>
<tr>
<td class="select"><input type="checkbox" class="select-row" id="check_<?php echo $j; ?>" name="check_<?php echo $j; ?>"><input type="hidden" name="path_<?php echo $j; ?>" value="<?php echo $path; ?>"></td>
<td><em>Not matched to any datasets</em></td>
<td data-sort-value="<?php echo $size; ?>"><?php echo human_filesize($size); ?></td>
<td><?php echo find_genome($raw_path); ?></td>
<td class="path"><a href="download_file.php?fn=<?php echo substr($raw_path, strlen($data_root)); ?>"><?php echo $path; ?></a></td>
<td><a href="http://<?php echo make_url_alias($labrador_url,"",$data_alias,$path); ?>">[Hard Link]</a>
<?php echo make_webstartlink(make_url_alias($labrador_url,"",$data_alias,$path)); ?></td>
</tr>
<?php } // foreach orhpans ?></tbody>
</table>
<div class="form-actions">
<input type="submit" class="btn btn-primary btn-large" name="java_download_paths" id="java_download_paths" value="Download Checked Files With Java Applet">
</div>
</form>
<?php } // directory existence check
else { ?>
<p>No directory found on the server.</p>
<?php }
} // java applet check ?>
</div>
<?php include('includes/javascript.php'); ?>
<script type="text/javascript">
var project_filename_filters = new Array("<?php echo implode('", "', $project_filename_filters); ?>");
var raw_filename_filters = new Array("<?php echo implode('", "', $raw_filename_filters); ?>");
var aligned_filename_filters = new Array("<?php echo implode('", "', $aligned_filename_filters); ?>");
var reports_filename_filters = new Array("<?php echo implode('", "', $reports_filename_filters); ?>");
</script>
<script src="js/files.js" type="text/javascript"></script>
<?php include('includes/footer.php'); ?>