-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetGenoData.php
334 lines (290 loc) · 11.3 KB
/
GetGenoData.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
<?php
//testurl
//http://dendrome.ucdavis.edu/_dev/havasquezgross/cartogratree/GetGenoData.php?tid=GRI0001,GRI0002
ini_set('max_execution_time', 300);
include_once("../includes/db_access/db_connect_sswap.php");
function sortByChromAndPos($dataArray) {
ksort($dataArray);
foreach($dataArray as $id => &$value) {
ksort(&$value);
}
return($dataArray);
}
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);
}
function printHeader($array) {
$output_string = "rs#\talleles\tchrom\tpos\tstrand\tassembly#\tcenter\tprotLSID\tassayLSID\tpanelLSID\tQCcode";
foreach($array as $value){
$output_string = $output_string."\t$value";
}
echo $output_string."\n";
}
function printTable($dataArray, $idArray) {
$output_string = "";
foreach($dataArray as $chrom => $posArray) {
foreach($posArray as $pos => $tidArray) {
//print "$chrom\t$pos\t$tidArray\n";
$snpid = "SNP_".$chrom."-".$pos;
$output_string = $output_string."$snpid\tN/N\t$chrom\t$pos\t+\tdendrome\tcenter\tProtocol\tAssay\tPanel\tQC";
foreach($idArray as $identifier){
if(array_key_exists ($identifier, $dataArray[$chrom][$pos])) {
$genotype = $dataArray[$chrom][$pos][$identifier];
$genotype = str_replace(":","",$genotype);
$output_string = $output_string."\t".$genotype;
} else {
$genotype = "NA";
}
}
$output_string = $output_string."\n";
}
}
echo $output_string;
}
if (isset($_GET['tid'])) {
$escapedId = pg_escape_string(trim($_GET['tid']));
} else {
}
$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;
}
}
if(count($dtreeArr) > 0) {
$snpSearch = array();
$snp_list = array();
foreach($dtreeArr as $snpId) {
$snpSearch[] = "(
SELECT snp_accessions.snp_accession
FROM inv_samples AS ins
LEFT JOIN inv_genotyping_data_genotype_results AS igdgr ON igdgr.inv_samples_id = ins.id
LEFT JOIN snps ON snps.id = igdgr.snps_id
LEFT JOIN snp_accessions ON snp_accessions.id = snps.snp_accessions_id
WHERE
ins.sample_barcode = '$snpId'
AND snp_accessions.snp_accession IS NOT NULL
AND snp_accessions.snp_accession <> 'SNP_NULL'
)";
$snp_list[] = "'$snpId'";
}
$q = "
SELECT
ins.sample_barcode AS identifier,
s2.species,
CASE
WHEN iss.gps_latitude IS NOT NULL AND strpos(CAST(iss.gps_latitude AS text), '.') > 0 AND substr(rtrim(CAST(iss.gps_latitude AS text),'0'), length(rtrim(CAST(iss.gps_latitude AS text),'0')), 1) = '.'
THEN substr(CAST(iss.gps_latitude AS text), 1, strpos(CAST(iss.gps_latitude AS text), '.') - 1)
WHEN iss.gps_latitude IS NOT NULL AND strpos(CAST(iss.gps_latitude AS text), '.') > 0
THEN rtrim(CAST(iss.gps_latitude AS text), '0')
WHEN iss.gps_latitude IS NOT NULL
THEN CAST(iss.gps_latitude AS text)
ELSE ''
END AS latitude,
CASE
WHEN iss.gps_longitude IS NOT NULL AND strpos(CAST(iss.gps_longitude AS text), '.') > 0 AND substr(rtrim(CAST(iss.gps_longitude AS text),'0'), length(rtrim(CAST(iss.gps_longitude AS text),'0')), 1) = '.'
THEN substr(CAST(iss.gps_longitude AS text), 1, strpos(CAST(iss.gps_longitude AS text), '.') - 1)
WHEN iss.gps_longitude IS NOT NULL AND strpos(CAST(iss.gps_longitude AS text), '.') > 0
THEN rtrim(CAST(iss.gps_longitude AS text), '0')
WHEN iss.gps_longitude IS NOT NULL
THEN CAST(iss.gps_longitude AS text)
ELSE ''
END AS longitude,
igdgr.allele1,
igdgr.allele2,
snp_accessions.snp_accession,
snp_accessions.sequence_id,
snps.genotype,
physical_pos
FROM species AS s2
LEFT JOIN inv_sample_sources AS iss ON s2.species_id = iss.species_id
LEFT JOIN inv_samples ins ON iss.id = ins.inv_sample_sources_id
LEFT JOIN inv_genotyping_data_genotype_results AS igdgr ON igdgr.inv_samples_id = ins.id
LEFT JOIN snps ON snps.id = igdgr.snps_id LEFT JOIN snp_accessions ON snp_accessions.id = snps.snp_accessions_id
WHERE
ins.sample_barcode IN ( ".implode(',',$snp_list)." )
AND snp_accessions.snp_accession IS NOT NULL
AND snp_accessions.snp_accession <> 'SNP_NULL'
AND snp_accessions.snp_accession IN (
SELECT * FROM (
".implode(" INTERSECT ",$snpSearch)."
) AS tmp
)
ORDER BY snp_accession ASC, identifier ASC;";
}
if(count($tgdrArr) > 0) {
$tid_list = array();
foreach($tgdrArr as $tid) {
$tid_list[] = "'$tid'";
}
$queryTidString = implode(",", $tid_list);
foreach($tgdrArr as $tid) {
$q = "SELECT
t.tgdr_accession || '-' || s.id as identifier,
tgdr_genotypes.genotype_value,
tgdr_genotypes.genetic_marker_name as snp_accession,
'tgdr' AS data_source
FROM
tgdr_data_availability_mv t,
tgdr_samples s
LEFT JOIN tgdr_genotypes ON s.id = tgdr_genotypes.tgdr_samples_id
WHERE
t.tgdr_accession || '-' || s.id IN ($queryTidString)
ORDER BY snp_accession ASC, identifier ASC;";
}
}
header("Content-Type: text/plain; charset=ansi");
//echo $q;
//exit();
// var_dump($q);
$res = DBQuery($q);
$numrows = pg_num_rows($res);
$numsamples = count($idArr);
$numsnps = $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: text/csv; charset=ansi");
//header("Content-disposition: attachment; filename=\"commonsnp_spreadsheet.csv\"");
//header("Expires: 0");
//echo "NumRows:$numrows NumSNPS:$numsnps 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["identifier"];
$allele1 = $r["allele1"];
$allele2 = $r["allele2"];
$snp_accession = $r["snp_accession"];
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[] = '"'.$snp_accession.'"';
}
$tree_data_string[] = '"'.$allele1.$allele2.'"';
}
if($col_flag){
echo implode(',',$col_names)."\n";
$col_flag = false;
}
echo "$prev_identifier,".implode(',',$tree_data_string)."\n";
}
} //csv
//elseif(isset($_GET['hapmap'])) {
else{
header("Content-Type: text/plain; charset=ansi");
if ($numrows > 0) {
$tree_data_string = array();
$prev_identifer = '';
$begin_id = NULL;
$idArr = array();
$dataArray = array();
while ($r = pg_fetch_assoc($res)) {
$identifier = $r["identifier"];
$idArr[] = $identifier;
}
$idArr = array_unique($idArr);
//echo var_dump($idArr);
printHeader($idArr);
//Reset pointer
pg_result_seek($res, 0);
$count = 0;
$first_treeid = "";
$output_string = "";
$minorAllele = "";
$majorAllele = "";
$finalpart_string = "";
$saved_accession = "";
$dataArray = array();
//$prev_identifer = '';
if(count($dtreeArr) > 0) {
while ($r = pg_fetch_assoc($res)) {
$identifier = $r["identifier"];
$allele1 = $r["allele1"];
$allele2 = $r["allele2"];
$snp_accession = $r["snp_accession"];
$seqid = $r["sequence_id"];
//$genotype = $r["genotype"];
$physical_pos = $r["physical_pos"];
if($count == 0) {
$first_treeid = $identifier;
}
if($allele2 != $allele1) {
$minorAllele = $allele2;
$majorAllele = $allele1;
}
if($identifier == $first_treeid && $count != 0) {
$output_string = "$saved_accession\t$majorAllele/$minorAllele\t$seqid\t$physical_pos\t+\tdendrome\tcenter\tProtocol\tAssay\tPanel\tQC".$finalpart_string;
//$output_string = "$saved_accession\t$majorAllele/$minorAllele\t$seqid\t$physical_pos\t+\tdendrome\tcenter\turn:dendrome.ucdavis.edu:Protocol\turn:dendrome.ucdavis.edu:Assay\turn:dendrome.ucdavis.edu:Panel\tQC+".$finalpart_string;
//$output_string = "$saved_accession\t / \t$seqid\t$physical_pos\t+\tdendrome\tcenter\turn:dendrome.ucdavis.edu:Protocol\turn:dendrome.ucdavis.edu:Assay\turn:dendrome.ucdavis.edu:Panel\tQC+".$finalpart_string;
//echo "HAPMAP:$output_string\n";
///////echo "$output_string\n";
//$unique_id = "$seqid:$physical_pos";
//$dataArray[$unique_id] = $output_string;
$dataArray[$seqid][$physical_pos] = $output_string;
$finalpart_string = "";
$minorAllele = "";
$majorAllele = "";
}
if($saved_accession != $snp_accession) {
$saved_accession = $snp_accession;
}
//echo "TEST:$snp_accession\t$allele1$allele2\t$identifier\n";
$finalpart_string = $finalpart_string."\t$allele1$allele2";
$count += 1;
}
ksort($dataArray);
foreach($dataArray as $id => &$value) {
//print "$id\t$value\n";
ksort(&$value);
}
//var_dump($dataArray);
foreach($dataArray as $id => &$value) {
foreach($value as $k => $v) {
//print "$id\t$k\n";
print "$v\n";
}
}
} // ifdtree
else {
while ($r = pg_fetch_assoc($res)) {
$identifier = $r["identifier"];
$genotype = $r["genotype_value"];
$snp_accession = $r["snp_accession"];
$splitArr = explode("-",$snp_accession);
$chrom = $splitArr[0];
$pos = $splitArr[1];
$dataArray[$chrom][$pos][$identifier] = $genotype;
}
$dataArray = sortByChromAndPos($dataArray);
// echo var_dump($dataArray);
printTable($dataArray, $idArr);
} //iftgdr
}
}
?>