-
Notifications
You must be signed in to change notification settings - Fork 3
/
haplotype_search.php
617 lines (591 loc) · 22.7 KB
/
haplotype_search.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
<?php
/**
* select lines by haplotype
*
* @author Clay Birkett <[email protected]>
* @license http://triticeaetoolbox.org/wheat/docs/LICENSE Berkeley-based
* @link http://triticeaetoolbox.org/wheat/andvanced_search.php
*/
require 'config.php';
require $config['root_dir'].'includes/bootstrap.inc';
$mysqli = connecti();
new Haplotype($_POST['function']);
/** Using a PHP class to implement the "Download Gateway" feature
*
* @author Clay Birkett <[email protected]>
* @license http://triticeaetoolbox.org/wheat/docs/LICENSE Berkeley-based
* @link http://triticeaetoolbox.org/wheat/downloads/downloads.php
**/
class Haplotype
{
public function __construct($function = null)
{
switch ($function) {
case 'type1':
$this->type1();
break;
case 'step1':
$this->dispHaplo();
break;
case 'step2':
$this->step2();
break;
case 'step3':
$this->step3();
break;
case 'selLines':
$this->step2_combine();
break;
default:
$this->disp_framework();
break;
}
}
private function combinations($num_markers, $marker_idx, $marker_list, $cross, $cross_c)
{
global $dispMissing;
global $mysqli;
$sub = $num_markers - 1; /* which column of marker_idx to increment */
$i = 0;
while ($i < $num_markers) {
$markers[$marker_list[$i]] = 1;
$i++;
}
foreach ($_POST as $k => $v) {
if (strpos(strtolower($k), "marker") !== false) {
$check_list[$k] = 1;
}
}
if (isset($_POST['dispMissing'])) {
$dispMissing = 1;
} else {
$dispMissing = 0;
}
$marker_instr=" E.marker_uid in (".implode(",", array_keys($markers)).")";
//$in_these_lines = str_replace("line_records.", "E.", $in_these_lines);
$query_str="select E.line_record_name, E.line_record_uid, E.marker_uid, E.alleles from allele_cache as E where
$marker_instr $in_these_lines";
//print $query_str;
$result=mysqli_query($mysqli, $query_str) or die(mysqli_error($mysqli));
$lines = array();
$line_uids=array();
$line_names=array();
while ($row=mysqli_fetch_assoc($result)) {
$linename=$row['line_record_name'];
$lineuid=$row['line_record_uid'];
$mkruid=$row['marker_uid'];
$alleleval=$row['alleles'];
$line_uids[$linename]=$lineuid;
$line_names[$lineuid]=$linename;
if (!isset($lines[$linename])) {
$lines[$linename]=array();
}
if (!isset($lines[$linename][$mkruid])) {
$lines[$linename][$mkruid]=$alleleval;
}
}
$i = 0;
$markers = array();
if ($num_markers > 1) {
$loop1 = 4;
} else {
$loop1 = 1;
}
while ($i < $loop1) {
$j = 0;
$marker_idx[$sub] = 0;
while ($j < 4) {
$k = 0;
$tmp1 = "";
while ($k < $num_markers) {
$alleles = $marker_idx[$k];
if ($tmp1 == "") {
$tmp1 = $marker_list[$k] . "_" . $cross[$k][$alleles];
$tmp2 = $cross_c[$k][$alleles];
$tmp3 = $cross[$k][$alleles];
} else {
$tmp1 = $tmp1 . "_" . $marker_list[$k] . "_" . $cross[$k][$alleles];
$tmp2 = $tmp2 . "<td>" . $cross_c[$k][$alleles];
$tmp3 = $tmp3 . $cross[$k][$alleles];
}
$markers[$marker_list[$k]] = $cross[$k][$alleles];
$k++;
}
$unique = "marker_" . $tmp3;
if (isset($check_list[$unique])) {
$checked = "checked";
} else {
$checked = "";
}
$selLines=array();
foreach ($lines as $lnm=>$lmks) {
$flag=0;
foreach ($markers as $mkr=>$val) {
if (strtolower($lmks[$mkr])==strtolower($val)) {
} else {
//print strtolower($lmks[$mkr])."***".strtolower($val)."<br>";
$flag++;
}
}
if ($flag==0) {
//print $lnm."<br>";
array_push($selLines, $line_uids[$lnm]);
}
}
$count_lines = count($selLines);
if (count($selLines) > 0) {
if ($dispMissing || (!preg_match('/--/',$tmp2))) {
echo "<tr><td><input type='checkbox' name='marker_$tmp3' value = '$tmp1' $checked><td>$tmp2<td>" . count($selLines) . "\n";
}
}
$marker_idx[$sub] = $marker_idx[$sub] + 1;
$j++;
}
$marker_idx[$sub-1] = $marker_idx[$sub-1] + 1;
$i++;
}
}
function disp_framework() {
global $config;
include($config['root_dir'].'theme/normal_header.php');
?>
<div id="title">
<?php
$this->check_session();
?>
</div>
<div id="step1" class="box">
<?php
$this->dispHaplo();
?>
</div>
<div id="step2"></div></div>
<?php
include($config['root_dir'].'theme/footer.php');
}
function check_session() {
$tmp = count($_SESSION['clicked_buttons']);
if ($tmp > 5) {
echo "$tmp markers selected<br>\n";
echo "Error - Please select no more than 5 markers<br>";
echo "<p><a href=genotyping/marker_selection.php>Select markers</a></p>";
return;
}
if (isset($_POST['dispMissing'])) {
$dispMissing = 1;
} else {
$dispMissing = 0;
}
}
/* for GBS data convert calls to ACTG */
function dispHaplo() {
global $mysqli;
if (isset($_POST['dispMissing'])) {
$dispMissing = 1;
} else {
$dispMissing = 0;
}
?><form action="haplotype_search.php" method="post" name="myForm">
<script type="text/javascript" src="downloads/downloads.js"></script>
<h2>View Haplotypes</h2>
<h3>Select haplotype combination</h3>
<div class="boxContent">
<table id="haplotypeSelTab" class="tableclass1" cellpadding=0
cellspacing=0>
<thead>
<tr>
<th>Marker</th>
<?php
//clicked_buttons = the uids of the markers in the
//current selection list.
if(isset($_SESSION['clicked_buttons']) && count($_SESSION['clicked_buttons']) > 0) {
$i = 0;
$cross = array();
$crossc = array();
foreach($_SESSION['clicked_buttons'] as $marker) {
// Show Marker Name
$sql = "SELECT marker_name, A_allele, B_allele, marker_type_name FROM markers, marker_types
WHERE markers.marker_type_uid = marker_types.marker_type_uid
and marker_uid = ?";
if ($stmt = mysqli_prepare($mysqli, $sql)) {
mysqli_stmt_bind_param($stmt, "i", $marker);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $marker_name, $a_allele, $b_allele, $mkrtyp);
mysqli_stmt_fetch($stmt);
echo "<th>$marker_name</th>";
$marker_ab=$a_allele . $b_allele;
mysqli_stmt_close($stmt);
}
$marker_list[$i] = $marker;
$marker_idx[$i] = 0;
// Show Alleles corresponding to the marker.
$sql = "SELECT DISTINCT allele_1, allele_2
FROM alleles, genotyping_data
WHERE genotyping_data.marker_uid = ?
AND genotyping_data.genotyping_data_uid = alleles.genotyping_data_uid
ORDER BY allele_1 ASC";
if ($stmt = mysqli_prepare($mysqli, $sql)) {
mysqli_stmt_bind_param($stmt, "i", $marker);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $allele_1, $allele_2);
$j = 0;
while (mysqli_stmt_fetch($stmt)) {
$alleles = $allele_1.$allele_2;
$alleles_c = $alleles;
//if (($mkrtyp == "GBS") || ($mkrtyp == "DArT Marker")) {
if ($alleles=='AA') {
$alleles_c = substr($marker_ab,0,1) . substr($marker_ab,0,1);
} elseif ($alleles=='BB') {
$alleles_c = substr($marker_ab,1,1) . substr($marker_ab,1,1);
} elseif ($alleles=='AB') {
$alleles_c = substr($marker_ab,0,1) . substr($marker_ab,1,1);
} elseif ($alleles=='BA') {
$alleles_c = substr($marker_ab,1,1) . substr($marker_ab,0,1);
}
//}
$cross[$i][$j] = $alleles;
$crossc[$i][$j] = $alleles_c;
$j++;
}
mysqli_stmt_close($stmt);
$num_alleles[$i] = $j;
$i++;
} else {
echo "No Data Available $row[marker_name]";
}
echo "</td>\n";
}
$num_markers = $i;
if ($num_markers > 0) {
// calculate the number of times to call combinations function
$i = 0;
$total = 1;
while ($i < ($num_markers - 2)) {
$total = $total * 4;
$i++;
}
echo "<th>Number Lines\n";
$i = 0;
$current = $num_markers - 1;
$current2 = $num_markers - 3;
//echo "total $total";
while ($i < $total) {
$this->combinations($num_markers,$marker_idx,$marker_list,$cross,$crossc);
$marker_idx[$current2]++;
$j = $current2;
while ($j > 0) {
if ($marker_idx[$j] == 4) {
$marker_idx[$j] = 0;
$marker_idx[$j-1]++;
}
$j--;
}
$i++;
}
} else {
echo "<th>The current marker selection does not have genotyping data";
}
?>
</tbody>
</table>
<?php
if ($dispMissing) {
echo "<input type='submit' name='hideMissing' value='Hide missing'> Hide haplotypes with missing data";
} else {
echo "<input type='submit' name='dispMissing' value='Show missing'> Show haplotypes with missing data";
}
?>
<p>
<input type="button" name="haplotype" value="Save line selection"
onclick="javascript: haplotype_step2();" /> Combine selected
haplotype with currently selected lines
</p></form>
<?php
}
else {
echo "<td>No markers selected";
echo "</table><p><a href=genotyping/marker_selection.php>Select markers</a></p>";
}
echo "</div>";
}
function step2() {
global $mysqli;
print "<h2>Results of Select Haplotypes</h2>";
$marker_query = "";
foreach($_SESSION['clicked_buttons'] as $marker) {
if (preg_match('/[A-Za-z0-9]+/',$marker)) {
if ($marker_query == "") {
$marker_query = $marker;
} else {
$marker_query = $marker_query . ",$marker";
}
}
}
//$marker_instr=" and D.marker_uid in (".implode("," , array_keys($markers)).")";
$marker_instr=" and D.marker_uid in (".$marker_query.")";
$in_these_lines = str_replace("line_records.", "A.", $in_these_lines);
$query_str="select A.line_record_name, A.line_record_uid, D.marker_uid, E.allele_1, E.allele_2
from line_records as A, tht_base as B, genotyping_data as C, markers as D, alleles as E
where A.line_record_uid=B.line_record_uid and B.tht_base_uid=C.tht_base_uid and
C.marker_uid=D.marker_uid and C.genotyping_data_uid=E.genotyping_data_uid
$marker_instr $in_these_lines";
//print $query_str;
$result=mysqli_query($mysqli, $query_str) or die(mysqli_error($mysqli));
//print "Number of rows = ". mysql_num_rows($result) . "\n";
$lines = array();
$line_uids=array();
$line_names=array();
while ($row=mysqli_fetch_assoc($result)) {
$linename=$row['line_record_name'];
$lineuid=$row['line_record_uid'];
$mkruid=$row['marker_uid'];
$alleleval=$row['allele_1'].$row['allele_2'];
$line_uids[$linename]=$lineuid;
$line_names[$lineuid]=$linename;
if (! isset($lines[$linename])) $lines[$linename]=array();
if (! isset($lines[$linename][$mkruid])) $lines[$linename][$mkruid]=$alleleval;
}
$selLines=array();
foreach($_POST as $k=>$v) {
//print "$k $v<br>";
if (strpos(strtolower($k), "marker") !== FALSE) {
$tm = explode("_", $v);
$i = 0;
while ($i < count($tm)) {
$markers[$tm[$i]] = $tm[$i+1];
$i = $i + 2;
}
foreach ($lines as $lnm=>$lmks) {
$flag=0;
foreach ($markers as $mkr=>$val) {
if (strtolower($lmks[$mkr])==strtolower($val)) {
} else {
$flag++;
}
}
if ($flag==0) {
//print $lnm."<br>";
array_push($selLines, $line_uids[$lnm]);
}
}
} else {
continue;
}
}
if (count($selLines) > 0) {
if (count($_SESSION['selected_lines']) == 0) {
$selected_lines = array();
foreach($selLines as $line_uid) {
if (!in_array($line_uid, $selected_lines)) {
array_push($selected_lines, $line_uid);
}
$_SESSION['selected_lines'] = $selected_lines;
}
print "Currently selected Lines " . count($selLines) . "<br>\n";
print "Currently Selected Markers: " . count($_SESSION['clicked_buttons']) . "<br><br>\n";
//* <form action="pedigree/pedigree_markers.php" method="post">
//* <input type="submit"
//* value="Display Haplotype Data for Selected Lines and Markers">
//* </form>
//*<?php */
} else {
print "<table><tr><td>Currently Selected Lines:" . count($_SESSION['selected_lines']) . "<td>";
print "Lines found:" . count($selLines) . "\n";
print "<tr><td>";
?>
<select name="lines" multiple="multiple" style="height: 12em;">
<?php
$selected_lines = $_SESSION['selected_lines'];
foreach ($selected_lines as $line) {
$sql = "SELECT line_record_uid as id, line_record_name as name from line_records where line_record_uid = $line";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_assoc($res);
$temp = $row['id'];
$lines_list[$temp] = 1;
echo "<option disabled=\"disabled\" value=\"" . $row['id'] . "\">" . $row['name'] . "</option>\n";
}
?>
</select>
<select name="lines" multiple="multiple" style="height: 12em;" onchange="javascript: update_phenotype_lines(this.options)">
<?php
foreach ($selLines as $line) {
$sql = "SELECT line_record_uid as id, line_record_name as name from line_records where line_record_uid = $line";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_assoc($res);
$temp = $row['id'];
$lines_list[$temp] = 1;
?>
<option disabled="disabled" value="<?php echo $row['id'] ?>">
<?php echo $row['name'] ?>
</option>
<?php
}
?>
</select>
</table><br>
<form name='lines' id='selectLines' action='haplotype_search.php' method='post'>
<?php
$lines_str = implode(",",$selLines);
?>
<input type="hidden" name="selLines" value="<?php echo $lines_str?>">
<p>
Combine with <font color=blue>currently selected lines</font>:<br> <input
type="radio" name="selectWithin" value="Replace" checked>Replace<br>
<input type="radio" name="selectWithin" value="Add">Add (OR)<br>
<input type="radio" name="selectWithin" value="Yes">Intersect (AND)<br>
<!--input type="submit" value="Combine" style='color: blue'-->
<input type="button" name="haplotype" value="Submit" onclick="javascript: haplotype_step2_combine()">
</form>
<?php
}
} else {
echo "<p>Sorry, no records found<p>";
$this->dispHaplo();
//* print_r($_POST); */
}
}
function step2_combine() {
$lines_str = $_POST['selLines'];
$selLines = explode(",",$lines_str);
//* print "Currently selected Lines " . count($selLines) . "<br>\n";
//* print "Currently Selected Markers: " . count($_SESSION['clicked_buttons']) . "<br><br>\n"; */
//* <form action="pedigree/pedigree_markers.php" method="post">
//* <input type="submit"
//* value="Display Haplotype Data for Selected Lines and Markers">
//* </form>
//* <?php
//* $selLines = $_POST['selLines']; */
if ($_POST['selectWithin'] == "Replace") {
$selected_lines = array();
foreach($selLines as $line_uid) {
array_push($selected_lines, $line_uid);
}
$_SESSION['selected_lines'] = $selected_lines;
} elseif ($_POST['selectWithin'] == "Yes")
$_SESSION['selected_lines'] = array_intersect($_SESSION['selected_lines'], $selLines);
else { // Add.
$selected_lines = $_SESSION['selected_lines'];
if (!isset($selected_lines))
$selected_lines = array();
foreach($selLines as $line_uid) {
if (!in_array($line_uid, $selected_lines))
array_push($selected_lines, $line_uid);
}
$_SESSION['selected_lines'] = $selected_lines;
}
print "Currently Selected Lines: " . count($_SESSION['selected_lines']) . "<br>\n";
print "Currently Selected Markers: " . count($_SESSION['clicked_buttons']) . "<br><br>\n";
}
/*
* identify lines with particular phenotype values
*/
function phenoSearch() {
global $mysqli;
// Find all lines associated with the given phenotype data.
print "<h2>Results of Search by Phenotype</h2>";
$phenotype = $_POST['phenotype'];
$selLines = $_POST['selLines'];
$in_these_lines = "AND line_records.line_record_uid IN ( $selLines ) ";
if (! isset($phenotype) || strlen($phenotype)<1) {
print "<p><a href=\"".$_SERVER['PHP_SELF']."\">Go Back</a></p>";
error(1,"Phenotype not set");
die();
}
if(isset($_POST['na_value']) && $_POST['na_value'] != "") { // no range specified, single value
$value = $_POST['na_value'] == "" ? " " : $_POST['na_value'];
$search_str="SELECT line_records.line_record_uid, line_record_name FROM line_records, tht_base, phenotype_data
WHERE value REGEXP '$value'
AND line_records.line_record_uid = tht_base.line_record_uid
AND tht_base.tht_base_uid = phenotype_data.tht_base_uid
AND phenotype_data.phenotype_uid = '$phenotype' $in_these_lines";
//echo "<pre>$search_str\n\n\n\n\n\n</pre>";
$search = mysqli_query($mysqli, $search_str) or die(mysqli_error($mysqli));
$compare = "na_value=$value";
}
else {
$_SESSION['phenotype'] = $phenotype;
$first = $_POST['first_value'] == "" ? getMaxMinPhenotype("min", $phenotype) : $_POST['first_value'];
$last = $_POST['last_value'] == "" ? getMaxMinPhenotype("max", $phenotype) : $_POST['last_value'];
$search_str="SELECT line_records.line_record_uid, line_record_name FROM line_records, tht_base, phenotype_data
WHERE value BETWEEN $first AND $last
AND line_records.line_record_uid = tht_base.line_record_uid
AND tht_base.tht_base_uid = phenotype_data.tht_base_uid
AND phenotype_data.phenotype_uid = '$phenotype' $in_these_lines";
//echo "<pre>$search_str\n\n\n\n\n\n</pre>";
$search = mysqli_query($mysqli, $search_str) or die(mysqli_error($mysqli));
$compare = "first_value=$first&last_value=$last";
}
$selLines = array();
$linenames= array();
if(mysqli_num_rows($search) > 0) {
while($line = mysqli_fetch_assoc($search)) {
array_push($selLines, $line['line_record_uid']);
$linenames[$line['line_record_uid']] =$line['line_record_name'];
}
}
if(count($selLines) > 0) {
sort($selLines);
$_SESSION['selected_lines']=array();
foreach ($selLines as $sline) {
if (! in_array($sline, $_SESSION['selected_lines'])) array_push($_SESSION['selected_lines'], $sline);
}
$selLines=$_SESSION['selected_lines'];
print "Lines filtered by phenotype: " . count($selLines) . "<br><br>\n";
print "<table class='tableclass1' style=\"float: left;\"><thead><tr><th><b>Lines found</b></th></tr></thead><tbody>";
foreach ($selLines as $luid) {
print "<tr><td>";
print "<a href=\"pedigree/show_pedigree.php?line=$luid\">".$linenames[$luid]."</a>";
print "</td></tr>";
}
print "</tbody></table>";
//print "<div style='float: left; margin-left: 10px;'>";
//print "<p><a href=\"pedigree/pedigree_markers.php\">Compare the alleles for all selected markers for these lines</a></p>";
//print "<p><a href=\"phenotype/compare.php?phenotype=$phenotype&$compare\">Narrow your results by phenotype</a></p>";
//print "<p><a href=\"advanced_search.php?searchtype=idMkrs\">Identify Markers that are identical for these lines</a></p>";
// print "<div id='ajaxMsg'></div>";
// print "<input type=\"button\" id=\"storeLineButton\" value=\"Store Line Names\" onclick=\"callAjaxFunc('ajaxSessionVariableFunc','&action=store&svkey=selected_lines',this.id)\" >";
//print "</div><div style='clear: left;'></div>";
}
else {
echo "<p>Sorry, no records found in lines with selected phenotype value<p>";
}
}
function step3() {
$lines_str = $_POST['selLines'];
$selLines = explode(",",$lines_str);
?>
<form action="haplotype_search.php" method="post">
<h3> Optionally, also select a trait </h3>
<input type="hidden" name=selLines value=" <?php echo $lines_str ?> ">
<div id="phenotypeSel">
<table id="phenotypeSelTab" class="tableclass1" cellspacing=0; cellpadding=0;>
<thead>
<tr>
<th>Category</th>
<th>Trait</th>
<th>Experiment</th>
</tr>
</thead>
<tbody>
<tr class="nohover">
<td>
<select name='phenotypecategory' size=10 onfocus="DispPhenoSel(this.value, 'Category')" onchange="DispPhenoSel(this.value, 'Category')">
<?php showTableOptions("phenotype_category"); ?>
</select>
</td>
<td><p>Select a trait category from the left most column</p>
</td>
<td></td>
</tr>
<tr>
<td> </td><td></td><td></td>
</tr>
</tbody>
</table>
</div>
</form>
</div>
</div>
<?php
}
}