-
Notifications
You must be signed in to change notification settings - Fork 3
/
display_genotype_class.php
472 lines (445 loc) · 20.4 KB
/
display_genotype_class.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
/**
* Report for a single genotyping experiment.
*
* PHP version 5.3
*
* @author Clay Birkett <[email protected]>
* @license http://triticeaetoolbox.org/wheat/docs/LICENSE Berkeley-based
* @link http://triticeaetoolbox.org/wheat/display_genotype.php
*
* dem 23mar12 Handle large dataset downloads. Output one row at a time
* instead of catenating the whole thing into $output first.
* J.Lee 5/9/2011 Fix problem with query while restricting mmaf and max missing
* values, prevent download operation when 0 markers match condition.
* J.Lee 8/17/2010 Modify alelle download to work in Linux and Solaris
*/
/** Using a PHP class to implement the report feature
*
* @author Clay Birkett <[email protected]>
* @license http://triticeaetoolbox.org/wheat/docs/LICENSE Berkeley-based
* @link http://triticeaetoolbox.org/wheat/display_genotype.php
**/
class ShowData
{
public $delimiter = "\t";
/**
* Using the class's constructor to decide which action to perform
*
* @param string $function action to perform
*/
public function __construct($function = null)
{
switch ($function) {
case 'typeTabDelimiter':
$this->type_Tab_Delimiter(); /* Displaying in tab delimited fashion */
break;
case 'typeTabDelimiterGBS':
$this->type_Tab_Delimiter_GBS();
break;
case 'select_lines':
$this->typeSelectLines();
break;
case 'select_markers':
$this->typeSelectMarkers();
break;
default:
$this->typeData();
break;
}
}
/**
* Store the lines from this experiment in a session variable, and jump to Select by Properties.
*
* @return null
*/
private function typeSelectLines()
{
$_SESSION[selected_lines] = explode(",", $_POST[linelist]);
echo "<meta http-equiv=\"refresh\" content=\"0;url=".$config['base_url']."pedigree/line_properties.php\">";
}
/** Store the genotype experiment selection in a session variable, and jump to genotype experiments list*/
private function typeSelectMarkers()
{
global $mysqli;
$_SESSION[selected_lines] = explode(",", $_POST[linelist]);
$exps_str = $_POST[genoexp];
$experiments = explode(',', $exps_str);
$_SESSION['geno_exps'] = $experiments;
$sql = "select count(marker_uid) from allele_bymarker_exp_101 where experiment_uid in ($exps_str)";
if ($stmt = mysqli_prepare($mysqli, "select count(marker_uid) from allele_bymarker_exp_101 where experiment_uid in (?)")) {
mysqli_stmt_bind_param($stmt, "s", $exps_str);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $countMarkers);
if (mysqli_stmt_fetch($stmt)) {
$_SESSION['geno_exps_cnt'] = $countMarkers;
echo "<meta http-equiv=\"refresh\" content=\"0;url=".$config['base_url']."genotyping/genotype_selection.php\">";
} else {
echo "Error: no markers could be found";
}
mysqli_stmt_close($stmt);
} else {
echo "Error: " . mysqli_error($mysqli);
}
}
// The wrapper action for the type1 download. Handles outputting the header
// and footer and calls the first real action of the type1 download.
private function typeData()
{
global $config;
include $config['root_dir'].'theme/admin_header.php';
$trial_code = strip_tags($_GET['trial_code']);
echo " <h2>Genotyping experiment ".$trial_code. "</h2>";
$this->type_DataInformation($trial_code);
$footer_div = 1;
include $config['root_dir'].'theme/footer.php';
?>
<script type="text/javascript" src="display_genotype01.js"></script>
<?php
}
private function type_DataInformation($trial_code)
{
global $mysqli;
$line_ids = array();
$sql = "SELECT CAPdata_programs_uid, experiment_type_uid, experiment_uid, experiment_short_name FROM experiments where trial_code = ?";
if (!authenticate(array(USER_TYPE_PARTICIPANT,
USER_TYPE_CURATOR,
USER_TYPE_ADMINISTRATOR))) {
$sql .= " and data_public_flag > 0";
}
if ($stmt = mysqli_prepare($mysqli, $sql)) {
mysqli_stmt_bind_param($stmt, "s", $trial_code);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $CAPdata_programs_uid, $experiment_type_uid, $experiment_uid, $experiment_short_name);
if (mysqli_stmt_fetch($stmt)) {
mysqli_stmt_close($stmt);
$sql_data_code = "SELECT data_program_code, data_program_name FROM CAPdata_programs where CAPdata_programs_uid = '".$CAPdata_programs_uid."' ";
$res_data_code = mysqli_query($mysqli, $sql_data_code) or die("Error: unable to retrieve CAP data info from data prog id.<br>".mysqli_error($mysqli));
$row_data_code = mysqli_fetch_assoc($res_data_code);
$data_program_code = $row_data_code['data_program_code'];
$data_program_name = $row_data_code['data_program_name'];
} else {
mysqli_stmt_close($stmt);
echo "Error: no experiment found";
return;
}
} else {
echo "Error: " . mysqli_error($mysqli);
}
$sql_lines = "select line_record_uid from tht_base where experiment_uid = $experiment_uid";
$res_lines = mysqli_query($mysqli, $sql_lines) or die("Error: unable to retrieve lines for this experiment.<br>" . mysqli_error($mysqli));
while ($line = mysqli_fetch_row($res_lines)) {
$line_ids[] = $line[0];
}
$line_total = count($line_ids);
$line_list = implode(",", $line_ids);
$sql_lines = "select line_index from allele_bymarker_expidx where experiment_uid = $experiment_uid";
$res_lines = mysqli_query($mysqli, $sql_lines) or die("Error: unable to retrieve lines for this experiment.<br>" . mysqli_error($mysqli));
if ($line = mysqli_fetch_row($res_lines)) {
$gbs_exp = "yes";
$tmp = $line[0];
$line_ids = json_decode($tmp, true);
$line_total = count($line_ids);
$line_list = implode(",", $line_ids);
}
$sql_Gen_Info = "SELECT * FROM genotype_experiment_info where experiment_uid = '".$experiment_uid."' ";
$res_Gen_Info = mysqli_query($mysqli, $sql_Gen_Info) or die("Error: No experiment information for genotype experiment $trial_code..<br> " .mysqli_error($mysqli));
$row_Gen_Info = mysqli_fetch_assoc($res_Gen_Info);
$manifest_file_name = $row_Gen_Info['manifest_file_name'];
$cluster_file_name = $row_Gen_Info['cluster_file_name'];
$OPA_name = $row_Gen_Info['OPA_name'];
$sample_sheet_filename = $row_Gen_Info['sample_sheet_filename'];
$raw_datafile_archive = $row_Gen_Info['raw_datafile_archive'];
$genotype_experiment_info_uid = $row_Gen_Info['genotype_experiment_info_uid'];
$comments = $row_Gen_Info['comments'];
$platform_uid = $row_Gen_Info['platform_uid'];
$sql = "SELECT platform_name from platform where platform_uid = $platform_uid";
$res = mysqli_query($mysqli, $sql) or die("Error: No platform information for genotype experiment $trial_code..<br> " .mysqli_error($mysqli));
$row = mysqli_fetch_assoc($res);
$platform_name = $row['platform_name'];
//$dataset = mysql_grab("select datasets_uid from datasets_experiments where experiment_uid = $experiment_uid");
$sql_DS = "select datasets_uid from datasets_experiments where experiment_uid = $experiment_uid";
$res_DS = mysqli_query($mysqli, $sql_DS) or die(mysqli_error($mysqli));
while ($row_DS = mysqli_fetch_assoc($res_DS)) {
$dataset = $row_DS['datasets_uid'];
$sql_BP = "select cp.data_program_name, cp.data_program_code
from datasets ds, CAPdata_programs cp
where ds.datasets_uid = $dataset
and ds.CAPdata_programs_uid = cp.CAPdata_programs_uid";
$res_BP = mysqli_query($mysqli, $sql_BP) or die(mysqli_error($mysqli));
$row_BP = mysqli_fetch_assoc($res_BP);
if ($breeding_program_name == "") {
$breeding_program_name = $row_BP['data_program_name'] . " (" . $row_BP['data_program_code'] . ")";
} else {
$breeding_program_name .= ", " . $row_BP['data_program_name'] . " (" . $row_BP['data_program_code'] . ")";
}
}
if (isset($_GET['mm']) && is_numeric($_GET['mm'])) {
$max_missing = $_GET['mm'];
} else {
$max_missing = 10; //IN PERCENT
}
if ($max_missing > 100) {
$max_missing = 100;
} elseif ($max_missing < 0) {
$max_missing = 0;
}
if (isset($_GET['mmaf']) && is_numeric($_GET['mm'])) {
$min_maf = $_GET['mmaf'];
} else {
$min_maf = 5; //IN PERCENT
}
if ($min_maf > 100) {
$min_maf = 100;
} elseif ($min_maf < 0) {
$min_maf = 0;
}
$max_miss_line = 10;
if (isset($_GET['mml']) && !empty($_GET['mml']) && is_numeric($_GET['mml'])) {
$max_miss_line = $_GET['mml'];
}
$sql = "select marker_count from genotype_experiment_info where experiment_uid = $experiment_uid";
$res = mysqli_query($mysqli, $sql) or
die("Error: Unable to sum allele frequency values.<br>".mysqli_error($mysqli));
if ($row = mysqli_fetch_row($res)) {
$num_mark = $row[0];
}
$num_maf = $num_miss = 0;
echo "<h3>Description</h3><p>";
echo "<table>";
echo "<tr> <td>Experiment Short Name</td><td>".$experiment_short_name."</td></tr>";
echo "<tr> <td>Platform</td><td>".$platform_name."</td></tr>";
echo "<tr> <td>Data Program</td><td>".$data_program_name." (".$data_program_code.")</td></tr>";
echo "<tr> <td>Breeding Program</td><td>".$breeding_program_name."</td></tr>";
echo "<tr> <td>OPA Name</td><td>".$row_Gen_Info['OPA_name']."</td></tr>";
echo "<tr> <td>Processing Date</td><td>".$row_Gen_Info['processing_date']."</td></tr>";
echo "<tr> <td>Software</td><td>".$row_Gen_Info['analysis_software']."</td></tr>";
echo "<tr> <td>Software version</td><td>".$row_Gen_Info['BGST_version_number']."</td></tr>";
echo "<tr> <td>Comments</td><td>".$row_Gen_Info['comments']."</td></tr>";
echo "</table><p>";
?>
<h3>Download</h3>
<b><?php echo ($num_mark) ?></b> markers were assayed for <b><?php echo ($line_total) ?></b> lines.
<form method=POST action="<?php echo $SERVER[PHP_SELF] ?>">
<input type=hidden name=function value=select_lines>
<input type=hidden name=linelist value=<?php echo "\"$line_list\""; ?>>
<input type="submit" value="Select lines" style="color:blue">
</form>
<form method=POST action="<?php echo $SERVER[PHP_SELF] ?>">
<input type=hidden name=function value=select_markers>
<input type=hidden name=linelist value=<?php echo "\"$line_list\""; ?>>
<input type=hidden name=genoexp value=<?php echo "\"$experiment_uid\""; ?>>
<input type="submit" value="Select experiment" style="color:blue"> (lines and markers)
</form>
<br>
<?php
if ($gbs_exp == "yes") {
calculate_afe($experiment_uid, $min_maf, $max_missing, $max_miss_line);
} else {
calculate_af($line_ids, $min_maf, $max_missing, $max_miss_line);
}
?>
<p>
Maximum Missing Data: <input type="text" name="mm" id="mm" size="2" value="<?php echo ($max_missing) ?>" />%
Minimum MAF: <input type="text" name="mmaf" id="mmaf" size="1" value="<?php echo ($min_maf) ?>" />%
<input type="button" value="Refresh" onclick="javascript:mrefresh('<?php echo $trial_code ?>');return false;" /><br>
<div id="status"></div>
<div id="results">
<img alt="creating download file" id="spinner" src="images/ajax-loader.gif" style="display:none;">
<?php
if ($gbs_exp == "yes") {
?>
<input type="button" value="Download allele data" onclick="javascript:load_tab_delimiter_GBS('<?php echo $experiment_uid ?>','<?php echo $max_missing ?>','<?php echo $min_maf ?>');"/>
<?php
} else {
?>
<input type="button" value="Download allele data" onclick="javascript:load_tab_delimiter('<?php echo $experiment_uid ?>','<?php echo $max_missing ?>','<?php echo $min_maf ?>');"/>
<?php
}
$url = "genotyping/display_markers.php?geno_exp=" . $experiment_uid;
?>
<button onclick="location.href='<?php echo $url ?>'">Download marker data</button><br>
<?php
echo "</div><p><br>";
echo "<h3>Additional files available</h3><p>";
echo "<table>";
echo "<tr> <td>Samples (germplasm lines)</td><td><a href='".$config['base_url']."raw/genotype/".$row_Gen_Info['sample_sheet_filename']."'>".$row_Gen_Info['sample_sheet_filename']."</a></td></tr>";
echo "<tr> <td>Manifest (markers used)</td><td><a href='".$config['base_url']."raw/genotype/".$row_Gen_Info['manifest_file_name']."'>". $row_Gen_Info['manifest_file_name']." </a></td></tr>";
echo "<tr> <td>Cluster File</td><td><a href='".$config['base_url']."raw/genotype/".$row_Gen_Info['cluster_file_name']."'>".$row_Gen_Info['cluster_file_name']."</a></td></tr>";
echo "<tr> <td>Raw data</td><td><a href='".$config['base_url']."raw/genotype/".$row_Gen_Info['raw_datafile_archive']."'>".$row_Gen_Info['raw_datafile_archive']."</a></td></tr>";
echo "</table>";
} /* End of function type_DataInformation*/
private function type_Tab_Delimiter_GBS()
{
$dtype = "";
$experiment_uid = $_GET['expuid'];
$max_missing = 99.9;//IN PERCENT
if (isset($_GET['mm']) && !empty($_GET['mm']) && is_numeric($_GET['mm'])) {
$max_missing = $_GET['mm'];
}
if ($max_missing > 100)
$max_missing = 100;
elseif ($max_missing < 0)
$max_missing = 0;
$min_maf = 0.01;//IN PERCENT
if (isset($_GET['mmaf']) && !is_null($_GET['mmaf']) && is_numeric($_GET['mmaf']))
$min_maf = $_GET['mmaf'];
if ($min_maf > 100)
$min_maf = 100;
elseif ($min_maf < 0)
$min_maf = 0;
$unique_str = chr(rand(65, 90)) .chr(rand(65, 90)) .chr(rand(65, 90)) .chr(rand(65, 90));
$filename = "download_" . $unique_str;
mkdir("/tmp/tht/$filename");
$filename = "selection_parameters.txt";
$h = fopen("/tmp/tht/download_$unique_str/$filename", "w");
fwrite($h, "Minimum MAF = $min_maf\n");
fwrite($h, "Maximum Missing = $max_missing\n");
fclose($h);
$filename = "genotype.hmp.txt";
$h = fopen("/tmp/tht/download_$unique_str/$filename", "w");
$output = type4BuildMarkersDownload($experiment_uid, $min_maf, $max_missing, $dtype, $h);
fclose($h);
$filename = "/tmp/tht/download_" . $unique_str . ".zip";
exec("cd /tmp/tht; /usr/bin/zip -r $filename download_$unique_str");
?>
<input type="button" value="Download Zip file of results" onclick="javascript:window.open('<?php echo "$filename"; ?>');" />
<?php
}
private function type_Tab_Delimiter() {
global $mysqli;
$experiment_uid = $_GET['expuid'];
$max_missing = 99.9;//IN PERCENT
if (isset($_GET['mm']) && !empty($_GET['mm']) && is_numeric($_GET['mm']))
$max_missing = $_GET['mm'];
if ($max_missing > 100)
$max_missing = 100;
elseif ($max_missing < 0)
$max_missing = 0;
$min_maf = 0.01;//IN PERCENT
if (isset($_GET['mmaf']) && !is_null($_GET['mmaf']) && is_numeric($_GET['mmaf']))
$min_maf = $_GET['mmaf'];
if ($min_maf > 100)
$min_maf = 100;
elseif ($min_maf < 0)
$min_maf = 0;
$outputheader = '';
$output = '';
$doneheader = false;
$delimiter ="\t";
//get lines and filter to get a list of markers which meet the criteria selected by the user
$sql = "SELECT af.marker_uid as marker, m.marker_name as name, SUM(af.aa_cnt) as sumaa, SUM(af.missing)as summis, SUM(af.bb_cnt) as sumbb,
SUM(af.total) as total, SUM(af.ab_cnt) AS sumab
FROM allele_frequencies AS af, markers as m
WHERE m.marker_uid = af.marker_uid
AND af.experiment_uid = ?
group by af.marker_uid";
if ($stmt = mysqli_prepare($mysqli, $sql)) {
mysqli_stmt_bind_param($stmt, "i", $experiment_uid);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $marker, $name, $sumaa, $summis, $sumbb, $total, $sumab);
$num_mark = mysqli_stmt_num_rows($stmt);
$num_maf = $num_miss = 0;
while (mysqli_stmt_fetch($stmt)) {
$maf = round(100*min((2*$sumaa+$sumab)/(2*$total),($sumab+2*$sumbb)/(2*$total)),1);
$miss = round(100*$summis/$total,1);
if (($maf >= $min_maf) AND ($miss <= $max_missing)) {
$marker_names[] = $name;
$outputheader .= $delimiter.$name;
$marker_uid[] = $marker;
}
}
}
//get a list of GBS markers used to convert format
$query = "SELECT marker_uid, marker_type_name, A_allele, B_allele from markers, marker_types
where markers.marker_type_uid = marker_types.marker_type_uid and marker_type_name = 'GBS'";
$resource = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
while ($row = mysqli_fetch_assoc($resource)) {
$uid = $row['marker_uid'];
$a_allele = $row['A_allele'];
$b_allele = $row['B_allele'];
$lookupGBS[$uid] = array(
'AA' => $a_allele,
'BB' => $b_allele,
'--' => 'N',
);
}
// Begin output to file.
// Prepend HTML header to trigger browser's "Open or Save?" dialog.
$date = date("m-d-Y-His");
$name = "THT-allele_query-$date.txt";
// JLee force url context change
header('Cache-Control:');
header('Pragma:');
header('Content-type: text/plain');
header("Content-Disposition: attachment; filename=$name");
header('Pragma: no-cache');
header('Expires: 0');
echo $outputheader."\n";
$nelem = count($marker_uid);
if ($nelem > 0) {
sort($marker_uid,SORT_NUMERIC);
$marker_uid = implode(",",$marker_uid);
} else {
error(1, "There are no markers matching the current conditions, try again with different set of criteria.");
exit("<input type=\"Button\" value=\"Return\" onClick=\"history.go(-1); return;\">");
}
$lookup = array(
'AA' => 'AA',
'BB' => 'BB',
'--' => '-',
'AB' => 'AB'
);
// make an empty line with the markers as array keys, set default value
// to the default missing value for either qtlminer or tassel
// places where the lines may have different values
$empty = array_combine($marker_names,array_fill(0,$nelem,'NA'));
$sql = "SELECT line_record_name, marker_name AS name, alleles AS value, marker_uid
FROM allele_cache
WHERE marker_uid IN ($marker_uid)
AND experiment_uid =$experiment_uid
ORDER BY line_record_name, marker_uid";
$last_line = "some really silly name that no one would call a plant";
$res = mysqli_query($mysqli, $sql) or die("Error:allele output dataset<br>". mysqli_error($mysqli));
$outarray = $empty;
$cnt = $num_lines = 0;
while ($row = mysqli_fetch_array($res)) {
//first time through loop
$uid = $row['marker_uid'];
if ($cnt == 0)
$last_line = $row['line_record_name'];
if ($last_line != $row['line_record_name']) {
// Close out the last line
$output .= "$last_line\t";
$outarray = implode($delimiter,$outarray);
$output .= $outarray."\n";
echo $output;
$output = "";
//reset output arrays for the next line
$outarray = $empty;
$mname = $row['name'];
if (isset($lookupGBS[$uid])) {
$outarray[$mname] = $lookupGBS[$uid][$row['value']];
} else {
$outarray[$mname] = $lookup[$row['value']];
}
$last_line = $row['line_record_name'];
$num_lines++;
} else {
$mname = $row['name'];
if (isset($lookupGBS[$uid])) {
$outarray[$mname] = $lookupGBS[$uid][$row['value']];
} else {
$outarray[$mname] = $lookup[$row['value']];
}
}
$cnt++;
}
//save data from the last line
$output .= "$last_line$delimiter";
$outarray = implode($delimiter, $outarray);
$output .= $outarray."\n";
$num_lines++;
echo $output;
}
}