-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetCommonPheno.php
148 lines (127 loc) · 4.69 KB
/
GetCommonPheno.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
<?php
//include_once("../includes/db_access/db_connect_sswap.php");
include_once("../../dev/includes/db_access/db_connect_sswap.php"); //for dev box
function outputCSV($data) {
$outstream = fopen("php://output", "w");
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals); // add parameters if you want
}
array_walk($data, "__outputCSV", $outstream);
fclose($outstream);
}
if (isset($_GET['tid'])) {
$escapedId = pg_escape_string(trim($_GET['tid']));
}
$idArr = explode(",", $escapedId);
$tgdrArr = array();
$dtreeArr = array();
foreach($idArr as $tid) {
if(strpos($tid, 'TGDR') !== false) {
//true
$tgdrArr[] = $tid;
// var_dump($tgdrArr);
} else {
$dtreeArr[] = $tid;
}
}
//need to throw in case if they are disparate datasets, but for now unioning tgdr and inv_*
$q = "
SELECT
t.tgdr_accession || '-' || s.id as tree_identifier,
tgdr_phenotypes_per_individual.phenotype_name AS metric_description,
tgdr_phenotypes_per_individual.value AS metric_value
FROM
tgdr_data_availability_mv t,
tgdr_samples s
LEFT JOIN tgdr_phenotypes_per_individual ON s.id = tgdr_phenotypes_per_individual.tgdr_samples_id
WHERE
t.tgdr_accession || '-' || s.id IN ('".implode("','",$idArr)."')
GROUP BY
tree_identifier,
tgdr_phenotypes_per_individual.phenotype_name,
metric_value
UNION
SELECT
inv_samples.sample_barcode AS tree_identifier,
inv_sample_metrics.descriptive_name as metric_description,
inv_samples_sample_metrics.measurement_value as metric_value
FROM inv_samples
INNER JOIN inv_sample_sources ON inv_samples.inv_sample_sources_id = inv_sample_sources.id
INNER JOIN inv_samples_sample_metrics ON inv_samples.id = inv_samples_sample_metrics.inv_samples_id
INNER JOIN inv_sample_metrics ON inv_samples_sample_metrics.inv_sample_metrics_id = inv_sample_metrics.id
WHERE inv_sample_metrics.metric_type = 'Phenotype' AND sample_barcode IN ( '".implode("','",$idArr)."' )
UNION
SELECT
cast(ecp_trydb.ecp_trydb_gps_groups_id as text) as tree_identifier,
ecp_trydb.trait as metric_description,
string_agg(cast(ecp_trydb.obsdataid as text),',') as metric_value
FROM ecp_trydb
WHERE cast(ecp_trydb.ecp_trydb_gps_groups_id as text) IN ( '".implode("','",$idArr)."' )
GROUP BY ecp_trydb.trait, ecp_trydb.ecp_trydb_gps_groups_id
ORDER BY tree_identifier ASC, metric_description ASC;";
$res = DBQuery($q);
$numrows = pg_num_rows($res);
$numsamples = count($idArr);
$numpheno = $numrows / $numsamples;
## CSV STUFF HERE
if(isset($_GET['csv'])) {
header("Cache-control: private");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
//header("Content-Type: application/octet-stream");
header("Content-Type: text/csv; charset=ansi");
header("Content-disposition: attachment; filename=\"commonphenotype_spreadsheet.csv\"");
header("Expires: 0");
//echo "NumRows:$numrows NumPheno:$numpheno NumSamples:$numsamples<br>";
if ($numrows > 0) {
$tree_data_string = array();
$col_flag = true;
$col_names = array('"TreeID"');
$prev_identifer = '';
while ($r = pg_fetch_assoc($res)) {
$identifier = $r["tree_identifier"];
$metric_description = $r["metric_description"];
$measurement_value = $r["metric_value"];
if($prev_identifier == ''){
$prev_identifier = $identifier;
}
if($identifier != $prev_identifier){
if($col_flag){
echo implode(',',$col_names)."\n";
$col_flag = false;
}
echo "$prev_identifier,".implode(',',$tree_data_string)."\n";
$prev_identifier = $identifier;
$tree_data_string = array();
}
if($col_flag){
$col_names[] = '"'.$metric_description.'"';
}
$tree_data_string[] = '"'.$measurement_value.'"';
}
if($col_flag){
echo implode(',',$col_names)."\n";
$col_flag = false;
}
echo "$prev_identifier,".implode(',',$tree_data_string)."\n";
}
}//csv
else {// echo to populate slickgrid
if($numrows > 0) {
$phenos = array(
"phenotypes" => array(),
"tree_ids" => array(),
);
$tree_id_prev = '';
while($row = pg_fetch_assoc($res)){
if($tree_id_prev !== $row["tree_identifier"]){
$tree_id_prev = $row["tree_identifier"]; //make new array for each new tree_id
$phenos["tree_ids"][$tree_id_prev] = array();
}
array_push($phenos["phenotypes"],$row["metric_description"]); //populate column array
array_push($phenos["tree_ids"][$tree_id_prev],array("phenotype" => $row["metric_description"], "value" => $row["metric_value"])); //populate tree id array
}
echo json_encode($phenos);
}
}
?>