/
action_gbs_analysis.php
438 lines (337 loc) · 18.9 KB
/
action_gbs_analysis.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
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
<?php
require basename("..").'/php_header.php'; // Require the PHP header housing required PHP functions
#############################################
# VERIFY SESSION
#############################################
if (!isset($_SESSION["query_db"], $_SESSION["query_group"]) || $_SESSION["query_db"] == "" || $_SESSION["query_group"] == "") {
gbs_query_page_redirect();
}
#############################################
# VERIFY REQUIRED POSTS
#############################################
if (!isset($_POST["family"]) || !isset($_POST[preg_replace("/\s/", "_", $_POST["family"])."analysis_type"])) {
gbs_query_page_redirect("insufficient_data");
}
#############################################
# CAPTURE THE SUBMITTED VALUES
#############################################
$_SESSION["gbs_analysis_type"] = htmlspecialchars($_POST[preg_replace("/\s/", "_", $_POST["family"])."analysis_type"], ENT_QUOTES, 'UTF-8');
$_SESSION["gbs_family"] = htmlspecialchars($_POST["family"], ENT_QUOTES, 'UTF-8');
// If a numeric greater than copy number has been submitted, update the global value - for analysis types that don't display this parameter in the query form, the downstream functions will ignore the value so there is no need to reset it, keeping it set means it will be saved for rerunning analyses that do use it
if (!in_array($_SESSION["gbs_analysis_type"], array("rohmer", "svfusions")) && isset($_POST["cngreaterthan"]) && is_numeric($_POST["cngreaterthan"])) {
$_SESSION["gbs_cngreaterthan"] = htmlspecialchars($_POST["cngreaterthan"], ENT_QUOTES, 'UTF-8');
}
// If a numeric less than copy number has been submitted, update the global value - for analysis types that don't display this parameter in the query form, the downstream functions will ignore the value so there is no need to reset it, keeping it set means it will be saved for rerunning analyses that do use it
if (!in_array($_SESSION["gbs_analysis_type"], array("rohmer", "svfusions")) && isset($_POST["cnlessthan"]) && is_numeric($_POST["cnlessthan"])) {
$_SESSION["gbs_cnlessthan"] = htmlspecialchars($_POST["cnlessthan"], ENT_QUOTES, 'UTF-8');
}
// If a numeric minimum block size has been submitted, update the global value, otherwise reset to default - for analysis types that don't display this parameter in the query form, the downstream functions will ignore the value so there is no need to reset it, keeping it set means it will be saved for rerunning analyses that do use it
if (isset($_POST["minblocksize"]) && is_numeric($_POST["minblocksize"])) {
$_SESSION["gbs_minblocksize"] = $_POST["minblocksize"];
}
// If a exclude failed variants filter has been submitted, that means it was selected as true, so update the global value to the true value
if (isset($_POST["exclude_failed_variants"])) {
$_SESSION["gbs_exclude_failed_variants"] = "1";
// If the analysis type is not ROHmer, which doesn't use the exclude failed variants filter, then no POST value means the box was unticked so the value should be set to the false value
} elseif ($_SESSION["gbs_analysis_type"] != "rohmer") {
$_SESSION["gbs_exclude_failed_variants"] = "2";
}
#############################################
# EXTRACT FAMILIAL INFORMATION FOR THE DB
#############################################
$family_info = extract_familial_information($_SESSION["query_group"]."/".$_SESSION["query_db"]);
if ($family_info === false) {
gbs_query_page_redirect("cant_fetch_family_information");
}
#############################################
# DETERMINE WHICH OF THE SAMPLES TO BE QUERIED ARE PRESENT IN THE GBS
#############################################
$samples_to_query_gbs = array();
// If the entire database is to be queried, save all samples
if ($_SESSION["gbs_family"] == "entiredatabase") {
// Go through each family and save all the samples in each one
foreach (array_keys($family_info) as $family) {
foreach (array_keys($family_info[$family]) as $sample) {
array_push($samples_to_query_gbs, $sample);
}
}
// If a family was specified to be queried
} elseif (isset($family_info[$_SESSION["gbs_family"]])) {
foreach (array_keys($family_info[$_SESSION["gbs_family"]]) as $sample) {
array_push($samples_to_query_gbs, $sample);
}
} else {
gbs_query_page_redirect("invalid_family");
}
$GBS_presence = fetch_gbs_samples_presence($samples_to_query_gbs);
//$GBS_presence[<sample name>]["methods"] = array(<method1>, <method2>)
if ($GBS_presence === false) {
return false;
}
#############################################
# CREATE ARRAYS OF SAMPLES IN THE GBS BY PHENOTYPE
#############################################
$samples_to_query = array();
$affected_samples_to_query = array();
$unaffected_samples_to_query = array();
$svfusions_samples_to_query = array();
$families_to_query = array();
// If the entire dataset is to be queried, fetch all samples
if ($_SESSION["gbs_family"] == "entiredatabase") {
$families_to_query = array_keys($family_info);
// If a family was specified to be queried
} elseif (isset($family_info[$_SESSION["gbs_family"]])) {
$families_to_query = array($_SESSION["gbs_family"]);
} else {
gbs_query_page_redirect("invalid_family");
}
// Go through each family and sample and pull out samples relevant to different analyses
foreach ($families_to_query as $family) {
foreach (array_keys($family_info[$family]) as $sample_name) {
// If the sample is in the GBS, save it
if (isset($GBS_presence[$sample_name])) {
// Save the sample to the overall samples to query array
array_push($samples_to_query, $sample_name);
// Check the affected status and save the sample to separate affected status arrays
if (is_affected($family_info[$family][$sample_name]["phenotype"])) {
array_push($affected_samples_to_query, $sample_name);
} elseif (is_unaffected($family_info[$family][$sample_name]["phenotype"])) {
array_push($unaffected_samples_to_query, $sample_name);
}
// If the sample has a BND/INV event, save it for SV Fusions analysis
if (in_array("BND", array_keys($GBS_presence[$sample_name]["event_types"])) || in_array("inversion", array_keys($GBS_presence[$sample_name]["event_types"]))) {
array_push($svfusions_samples_to_query, $sample_name);
}
}
}
}
#############################################
# IF A GENE LIST WAS SUBMITTED FOR AN ANALYSIS TYPE THAT ALLOWS THEM
#############################################
if (in_array($_SESSION["gbs_analysis_type"], array("svfusions", "gene_lists"))) {
// Store the lists selected in the multi-select box so they can be restored on the query page (only used for this)
if (isset($_POST["gbs_gene_list_selection"])) {
$_SESSION["gbs_gene_list_selection"] = $_POST["gbs_gene_list_selection"];
// If no list was selected reset to nothing (in case some were selected before)
} else {
$_SESSION["gbs_gene_list_selection"] = "";
}
// Store the manual entry gene list submitted
if (isset($_POST["genes"])) {
$_SESSION["gbs_gene_list"] = htmlspecialchars($_POST["genes"], ENT_QUOTES, 'UTF-8');
} else {
$_SESSION["gbs_gene_list"] = "";
}
// Store the PanelApp panel selected so it can be restored on the query page (only used for this)
if (isset($_POST["gbs_panelapp_panel_selection"]) && $_POST["gbs_panelapp_panel_selection"] != "None") {
$_SESSION["gbs_panelapp_panel_selection"] = $_POST["gbs_panelapp_panel_selection"];
// If no panel was selected reset to nothing (in case one was selected before)
} else {
$_SESSION["gbs_panelapp_panel_selection"] = "";
}
// If no gene lists to search were selected, no PanelApp panel was selected and nothing was typed in the manual entry box
if ($_SESSION["gbs_gene_list_selection"] == "" && $_SESSION["gbs_gene_list"] == "" && $_SESSION["gbs_panelapp_panel_selection"] == "") {;
if ($_SESSION["gbs_analysis_type"] == "svfusions") {
// Empty array of genes to search (means all will be searched)
$gene_list_to_search = array();
} elseif ($_SESSION["gbs_analysis_type"] == "gene_lists") {
gbs_query_page_redirect("no_genes_specified");
}
// If genes were specified
} else {
$gene_list_to_search = determine_gene_list($_POST["gbs_gene_list_selection"], $_SESSION["gbs_gene_list"], $_SESSION["gbs_panelapp_panel_selection"]);
if ($gene_list_to_search === false) {
gbs_query_page_redirect("cant_determine_gene_list");
}
// If no genes were parsed out of the gene lists selected and manual list submitted
if (count($gene_list_to_search) == 0) {
if ($_SESSION["gbs_analysis_type"] == "svfusions") {
log_results_page_error("No genes were extracted from the gene list(s) you specified. Returning results for all genes in the genome.");
} elseif ($_SESSION["gbs_analysis_type"] == "gene_lists") {
gbs_query_page_redirect("no_genes_specified");
}
} else {
// Determine which genes (if any) are not in the GBS gene name -> co-ordinate table, these are genes that will not be searched for
$missing_genes = validate_gene_list_gbs($gene_list_to_search);
if ($missing_genes === false) {
gbs_query_page_redirect("cant_determine_missing_genes");
}
// If missing genes were found
if (count($missing_genes) > 0) {
// Save any genes that were not found to a string to display to the user
log_results_page_error("Some genes do not have coordinates stored in the GBS for block searching, no blocks will be returned for these genes: ".implode(", ", $missing_genes)."\n");
// Subtract the missing genes from the genes to search
foreach ($gene_list_to_search as $gene) {
// If the gene is not in the GBS
if (array_search($gene, $missing_genes) !== false) { // array_search returns an ID which can evaluate to true/false, the function will return false when it doesn't find a match, check this with === or !==
// Delete it from the gene list to search
unset($gene_list_to_search[array_search($gene, $gene_list_to_search)]);
}
}
// Reindex the array
$gene_list_to_search = array_values($gene_list_to_search);
// Check that genes are remaining to search
if (count($gene_list_to_search) == 0) {
// Allow the query to go through with a warning for SV Fusions
if ($_SESSION["gbs_analysis_type"] == "svfusions") {
log_results_page_error("No valid genes were extracted from the gene list(s) you specified. Returning results for all genes in the genome.");
// Stop the query going through for Gene Lists as you need at least 1 gene
} elseif ($_SESSION["gbs_analysis_type"] == "gene_lists") {
gbs_query_page_redirect("no_genes_remaining_to_query");
}
}
}
}
}
}
#############################################
# ANALYSIS TYPE SAMPLE OVERLAPPING BLOCKS
#############################################
if ($_SESSION["gbs_analysis_type"] == "sample_overlaps") {
$all_methods_with_samples_in_gbs = array();
$methods_with_potential_sample_overlaps = array();
$samples_with_methods_with_potential_overlaps = array();
// Create a single array of all methods for all samples in the GBS (i.e. with repeating values where there are more than 1)
foreach ($samples_to_query as $sample_name) {
$all_methods_with_samples_in_gbs = array_merge($all_methods_with_samples_in_gbs, array_keys($GBS_presence[$sample_name]["methods"]));
}
// Count the number of occurrences of each method
$all_methods_with_samples_in_gbs = array_count_values($all_methods_with_samples_in_gbs);
// Save all methods that occur more than once to a separate array
foreach (array_keys($all_methods_with_samples_in_gbs) as $method_name) {
if ($all_methods_with_samples_in_gbs[$method_name] > 1) {
array_push($methods_with_potential_sample_overlaps, $method_name);
}
}
// Go through each method per sample and save all samples where one of their methods has 2+ samples
foreach ($samples_to_query as $sample_name) {
foreach (array_keys($GBS_presence[$sample_name]["methods"]) as $method_name) {
if (in_array($method_name, $methods_with_potential_sample_overlaps)) {
array_push($samples_with_methods_with_potential_overlaps, $sample_name);
}
}
}
$overlapping_blocks = analysis_type_sample_overlaps_gbs($samples_with_methods_with_potential_overlaps, $methods_with_potential_sample_overlaps);
if ($overlapping_blocks === false) {
gbs_query_page_redirect("cant_fetch_overlapping_blocks");
}
log_website_event("GBS query for sample overlaps, samples '".implode(", ", $samples_with_methods_with_potential_overlaps)."', methods '".implode(",", $methods_with_potential_sample_overlaps)."'");
output_results_to_tsv_and_redirect($overlapping_blocks);
#############################################
# ANALYSIS TYPE METHOD OVERLAPPING BLOCKS
#############################################
} elseif ($_SESSION["gbs_analysis_type"] == "method_overlaps") {
$samples_with_potential_method_overlaps = array();
// Save all samples with 2 or more methods, ignore the rest
foreach ($samples_to_query as $sample_name) {
if (count($GBS_presence[$sample_name]["methods"]) > 1) {
array_push($samples_with_potential_method_overlaps, $sample_name);
}
}
$overlapping_blocks = analysis_type_method_overlaps_gbs($samples_with_potential_method_overlaps);
if ($overlapping_blocks === false) {
gbs_query_page_redirect("cant_fetch_overlapping_blocks");
}
log_website_event("GBS query for method overlaps, samples '".implode(", ", $samples_with_potential_method_overlaps)."'");
output_results_to_tsv_and_redirect($overlapping_blocks);
#############################################
# ANALYSIS TYPE METHOD ROHMER
#############################################
} elseif ($_SESSION["gbs_analysis_type"] == "rohmer") {
$rohmer_blocks = analysis_type_rohmer_gbs($affected_samples_to_query, $unaffected_samples_to_query);
if ($rohmer_blocks === false) {
gbs_query_page_redirect("cant_fetch_rohmer_blocks");
}
log_website_event("GBS query for ROHmer, affected samples '".implode(", ", $affected_samples_to_query)."', unaffected samples '".implode(", ", $unaffected_samples_to_query)."'");
output_results_to_tsv_and_redirect($rohmer_blocks);
#############################################
# ANALYSIS TYPE METHOD SV FUSIONS
#############################################
} elseif ($_SESSION["gbs_analysis_type"] == "svfusions") {
$svfusions_blocks = analysis_type_svfusions_gbs($svfusions_samples_to_query, $gene_list_to_search);
if ($svfusions_blocks === false) {
gbs_query_page_redirect("cant_fetch_svfusions_blocks");
}
log_website_event("GBS query for SV Fusions, samples '".implode(", ", $svfusions_samples_to_query)."', gene(s) to search '".implode(", ", $gene_list_to_search)."'");
output_results_to_tsv_and_redirect($svfusions_blocks);
#############################################
# ANALYSIS TYPE GENE LIST(S)
#############################################
} elseif ($_SESSION["gbs_analysis_type"] == "gene_lists") {
// Run the analysis for blocks
$gene_lists_blocks_output = analysis_type_gene_lists_gbs($gene_list_to_search, $samples_to_query);
if ($gene_lists_blocks_output === false) {
gbs_query_page_redirect("cant_fetch_gene_lists_blocks");
}
log_website_event("GBS query for gene lists, samples '".implode(", ", $samples_to_query)."', gene(s) to search '".implode(", ", $gene_list_to_search)."'");
output_results_to_tsv_and_redirect($gene_lists_blocks_output);
#############################################
# ANALYSIS TYPE GENOMIC COORDINATES
#############################################
} elseif ($_SESSION["gbs_analysis_type"] == "genomic_coordinates") {
// Check for submitted regions
if (!isset($_POST["regions"]) || $_POST["regions"] == "") {
gbs_query_page_redirect("no_regions_submitted");
}
// Parse the submitted regions for correct formatting
$parse_regions_result = parse_submitted_regions(htmlspecialchars($_POST["regions"], ENT_QUOTES, 'UTF-8'), "gbs_regions");
if ($parse_regions_result !== true) {
gbs_query_page_redirect("invalid_search_regions");
}
$genomic_coordinates_blocks_output = analysis_type_genomic_coordinates_gbs($samples_to_query);
if ($genomic_coordinates_blocks_output === false) {
gbs_query_page_redirect("cant_fetch_genomic_coordinates_blocks");
}
log_website_event("GBS query for genomic coordinates, samples '".implode(", ", $samples_to_query)."', regions to search '".$_POST["regions"]."'");
output_results_to_tsv_and_redirect($genomic_coordinates_blocks_output);
}
#############################################
// Redirect to the query page for db query
gbs_query_page_redirect();
#############################################
# PAGE FUNCTIONS
#############################################
// Function to redirect to query page
function gbs_query_page_redirect($session_variable_name = NULL) {
if (isset($session_variable_name)) {
$_SESSION["gbs_query_".$session_variable_name] = 1;
}
// If a non-fatal error occurred, clear it as a fatal error occurred for this function to be called
unset($_SESSION["gbs_query_error"]);
header("Location: ".basename("..")."/gbs_query");
exit;
}
// Function to log query error without a redirect to the results page
function log_results_page_error($error_description) {
if (isset($_SESSION["gbs_query_error"])) {
$_SESSION["gbs_query_error"] .= $error_description;
} else {
$_SESSION["gbs_query_error"] = $error_description;
}
}
// Function to output any non-fatal errors to an err file
function output_errors_to_err_file($output_filename) {
if (isset($_SESSION["gbs_query_error"])) {
$error_output_full_path = basename("..")."/temp/".$output_filename.".err"; // Save the full path where the output file will be for opening it
$output = fopen($error_output_full_path, "w") or gbs_query_page_redirect("cant_create_output_file");
fwrite($output, $_SESSION["gbs_query_error"]);
fclose($output);
unset($_SESSION["gbs_query_error"]);
}
}
// Function to take an array of output line strings and write them to a results file and then redirect to it
function output_results_to_tsv_and_redirect($output_lines_array) {
date_default_timezone_set('Australia/Sydney');
$output_filename = date("Y_m_d_H_i_s",time())."_".rand_string("5"); // Generate an output filename with the current time and a random string for uniqueness/security
$output_full_path = basename("..")."/temp/".$output_filename.".tsv"; // Save the full path where the output file will be for opening it
$output = fopen($output_full_path, "w") or gbs_query_page_redirect("cant_create_output_file");
// Output all lines to the file
foreach ($output_lines_array as $row) {
fwrite($output, $row."\n");
}
fclose($output);
output_errors_to_err_file($output_filename);
header("Location: ".basename("..")."/gbs_results?query=".$output_filename);
exit;
}
?>