-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgensel_class.php
2459 lines (2326 loc) · 98.9 KB
/
gensel_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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace T3;
/**
* 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 Downloads
{
/**
* Delimiter used for output files
*/
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 'genomic_prediction':
$this->genomic_prediction();
break;
case 'run_histo':
$this->run_histo();
break;
case 'run_gwa':
$this->run_gwa();
break;
case 'run_gwa2':
$this->run_gwa2();
break;
case 'run_rscript':
$this->run_rscript();
break;
case 'run_rscript2':
$this->run_rscript2();
break;
case 'download_session_v2':
$this->type1_session(V2);
break;
case 'download_session_v3':
$this->type1_session(V3);
break;
case 'download_session_v4':
$this->type1_session(V4);
break;
case 'refreshtitle':
$this->refreshTitle();
break;
case 'gwas_status':
$this->status_gwas();
break;
case 'pred_status':
$this->status_pred();
break;
case 'filter_lines':
$this->filterLines();
break;
case 'web':
$this->type1Select();
break;
}
}
/**
* Load header and footer then check session to use existing data selection
*/
private function type1Select()
{
global $config;
global $mysqli;
require_once $config['root_dir'].'theme/normal_header.php';
$phenotype = "";
$lines = "";
$markers = "";
$saved_session = "";
$this->type1Checksession();
require_once 'downloads/select-map.php';
require_once $config['root_dir'].'theme/footer.php';
}
/**
* Checks the session variable, if there is lines data saved then go directly to the lines menu
*/
private function type1Checksession()
{
global $mysqli;
?>
<style type="text/css">
th {background: #5B53A6 !important; color: white !important; border-left: 2px solid #5B53A6}
table {background: none; border-collapse: collapse}
td {border: 1px solid #eee !important;}
h3 {border-left: 4px solid #5B53A6; padding-left: .5em;}
</style>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script type="text/javascript" src="downloads/download_gs04.js"></script>
<script type="text/javascript" src="downloads/downloadsjq03.js"></script>
<div id="title">
<?php
$phenotype = "";
$lines = "";
$markers = "";
$saved_session = "";
$message1 = $message2 = "";
if (isset($_SESSION['phenotype'])) {
$tmp = count($_SESSION['phenotype']);
if ($tmp==1) {
$saved_session = "$tmp phenotype ";
} else {
$saved_session = "$tmp phenotypes ";
}
$message2 = "download phenotype and genotype data";
$phenotype = $_SESSION['phenotype'];
} else {
$message1 = "0 phenotypes";
$message2 = " download genotype data";
}
if (isset($_SESSION['selected_lines'])) {
$countLines = count($_SESSION['selected_lines']);
if ($saved_session == "") {
$saved_session = "$countLines lines";
} else {
$saved_session = $saved_session . ", $countLines lines";
}
$lines = $_SESSION['selected_lines'];
}
if (isset($_SESSION['clicked_buttons'])) {
$tmp = count($_SESSION['clicked_buttons']);
$saved_session = $saved_session . ", $tmp markers";
$markers = $_SESSION['clicked_buttons'];
} else {
if ($message2 == "") {
$message1 = "0 markers ";
$message2 = "for all markers.";
} else {
$message1 = $message1 . ", 0 markers ";
$message2 = $message2 . " for all markers";
}
}
$this->refreshTitle();
if (empty($_SESSION['phenotype'])) {
echo "<font color=red>Select a set of traits and phenotype trials</font><br><br>";
} elseif (empty($_SESSION['selected_lines'])) {
echo "<br>Select validation set containing trait measurements to plot prediction vs observed. ";
echo "<a href=";
echo $config['base_url'];
echo "downloads/select_all.php>Wizard</a><br>";
echo "Select prediction set without trait measurements to predict the traits. ";
echo "<a href=";
echo $config['base_url'];
echo "pedigree/line_properties.php>Lines by Properties</a>, ";
echo "<a href=";
echo $config['base_url'];
echo "downloads/select_genotype.php>Lines by Genotype Experiment</a><br>";
} elseif (empty($_SESSION['phenotype']) && empty($_SESSION['training_traits'])) {
echo "Please select traits before using this feature.<br><br>";
echo "<a href=";
echo $config['base_url'];
echo "phenotype/phenotype_selection.php>Select Traits</a><br><br>";
echo "<a href=";
echo $config['base_url'];
echo "downloads/select_all.php>Wizard (Lines, Traits, Trials)</a>";
} elseif (empty($_SESSION['selected_map'])) {
if (isset($_SESSION['geno_exps'])) {
$geno_exp = $_SESSION['geno_exps'];
$geno_str = $geno_exp[0];
$sql = "select marker_uid from allele_bymarker_exp_101 where experiment_uid = $geno_str and pos is not null limit 10";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli) . $sql);
if ($row = mysqli_fetch_array($res)) {
} else {
echo "<font color=red>Select a genetic map.</font>";
echo "<input type=button value=\"Genetic map\" onclick=\"javascript: select_map()\"><br>";
}
} else {
echo "<font color=red>Select a genetic map.</font>";
echo "<input type=button value=\"Genetic map\" onclick=\"javascript: select_map()\"><br>";
}
}
if (!empty($_SESSION['training_lines']) && !empty($_SESSION['selected_lines'])) {
if (empty($_SESSION['selected_trials'])) {
echo "<tr><td>Prediction<td>";
} else {
echo "<tr><td>Validation<td>";
$tmp = $_SESSION['selected_trials'];
$e_uid = implode(",", $tmp);
$sql = "select trial_code from experiments where experiment_uid IN ($e_uid)";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli) . $sql);
while ($row = mysqli_fetch_array($res)) {
echo "$row[0]<br>";
}
}
$count = count($_SESSION['selected_lines']);
$markers = $_SESSION['filtered_markers'];
$estimate = count($markers) + count($lines);
echo "<td>$count";
?>
<td>
<form method="LINK" action="gensel.php">
<input type="hidden" value="step1gensel" name="function">
<input type="hidden" value="clear_p" name="cmd">
<input type="submit" value="Clear Selection">
</form>
<?php
//check if these are unique
$count = 0;
$count_dup = 0;
$tmp1 = $_SESSION['training_lines'];
$tmp2 = $_SESSION['selected_lines'];
$count_t = count($tmp2);
foreach ($tmp2 as $uid) {
if (in_array($uid, $tmp1)) {
$count_dup++;
} else {
$count++;
}
}
if ($count < 5) {
echo " <font color=red>(Error - $count unique lines in prediction set)";
}
}
echo "</table>";
if ($count_dup > 0) {
if (empty($_SESSION['selected_trials'])) {
echo " Warning - $count_dup lines removed from prediction set because they are in training set";
} else {
echo " Warning - $count_dup lines removed from validation set because they are in training set";
}
}
$min_maf = 5;
$max_missing = 10;
$max_miss_line = 10;
$unique_str = chr(rand(65, 80)).chr(rand(65, 80)).chr(rand(65, 80)).chr(rand(65, 80));
?>
</div>
<?php
if (!empty($_SESSION['training_lines']) && !empty($_SESSION['selected_lines'])) {
$min_maf = 5;
$max_missing = 10;
$max_miss_line = 10;
$unique_str = chr(rand(65, 80)).chr(rand(65, 80)).chr(rand(65, 80)).chr(rand(65, 80));
?>
<p>Minimum MAF ≥ <input type="text" name="mmaf" id="mmaf" size="2" value="<?php echo ($min_maf) ?>" />%
Remove markers missing > <input type="text" name="mmm" id="mmm" size="2" value="<?php echo ($max_missing) ?>" />% of data
Remove lines missing > <input type="text" name="mml" id="mml" size="2" value="<?php echo ($max_miss_line) ?>" />% of data
<div id="filter" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%"></div>
<div id="step1" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%">
<img alt="spinner" id="spinner" src="images/ajax-loader.gif" style="display:none;" /></div>
<div id="step2" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%">
<table>
<!--tr><td><td>fixed effect (trial is always included)-->
<tr><td><input type="button" value="G-BLUP Analysis" onclick="javascript:load_genomic_prediction(<?php echo $estimate; ?>)">
<!-td-->
</table><br>
</div>
<div id="step3" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%"></div>
<div id="step4" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%"></div>
<div id="step5" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%">
<?php
echo "</div>";
}
echo "</div>";
}
/**
* filters markers and lines based on settings
*/
private function filterLines()
{
if (isset($_GET['maf'])) {
$min_maf = $_GET['maf'];
} else {
$min_maf = 5;
}
if (isset($_GET['mmm'])) {
$max_missing = $_GET['mmm'];
} else {
$max_missing = 10;
}
if (isset($_GET['mml'])) {
$max_miss_line = $_GET['mml'];
} else {
$max_miss_line = 10;
}
$lines = $_SESSION['selected_lines'];
if (isset($_SESSION['training_lines'])) {
$training_lines = $_SESSION['training_lines'];
} else {
$training_lines = "";
}
if (isset($_SESSION['geno_exps'])) {
$experiment_uid = $_SESSION['geno_exps'][0];
calculate_afe($experiment_uid, $min_maf, $max_missing, $max_miss_line);
findCommonLines($lines);
} elseif ($training_lines == "") {
calculate_af($lines, $min_maf, $max_missing, $max_miss_line);
} else {
calculate_af($training_lines, $min_maf, $max_missing, $max_miss_line);
}
?>
<img alt="spinner" id="spinner" src="images/ajax-loader.gif" style="display:none;" />
<?php
}
/**
* 1. display a spinning activity image when a slow function is running
* 2. show button to clear sessin data
* 3. show button to save current selection
*/
private function refreshTitle()
{
global $mysqli;
$command = (isset($_GET['cmd']) && !empty($_GET['cmd'])) ? $_GET['cmd'] : null;
echo "<h2>Genomic Association and Prediction</h2>";
if (!empty($_SESSION['training_traits'])) {
$tmp = $_SESSION['training_traits'];
$tmp = $tmp[0];
$sql = "select phenotypes_name from phenotypes where phenotype_uid = '$tmp'";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_array($res);
echo "<h3>Trait: $row[0]</h3>";
}
if ($command == "save_t") {
if (!empty($_SESSION['selected_traits'])) {
$_SESSION['training_traits'] = $_SESSION['selected_traits'];
$_SESSION['training_trials'] = $_SESSION['selected_trials'];
$_SESSION['training_lines'] = $_SESSION['selected_lines'];
unset($_SESSION['selected_trials']);
unset($_SESSION['selected_lines']);
unset($_SESSION['filtered_lines']);
unset($_SESSION['filtered_markers']);
unset($_SESSION['clicked_buttons']);
} else {
echo "error - no selection found";
}
} elseif ($command == "save_p") {
$_SESSION['predict_traits'] = $_SESSION['selected_traits'];
$_SESSION['predict_trials'] = $_SESSION['selected_trials'];
$_SESSION['predict_lines'] = $_SESSION['selected_lines'];
} elseif ($command == "clear") {
unset($_SESSION['selected_traits']);
unset($_SESSION['selected_trials']);
unset($_SESSION['selected_lines']);
unset($_SESSION['training_traits']);
unset($_SESSION['training_trials']);
unset($_SESSION['training_lines']);
unset($_SESSION['filtered_lines']);
unset($_SESSION['phenotype']);
} elseif ($command== "clear_p") {
unset($_SESSION['selected_traits']);
unset($_SESSION['selected_trials']);
unset($_SESSION['selected_lines']);
}
if (empty($_SESSION['selected_lines']) || empty($_SESSION['training_lines'])) {
?>
<table>
<tr><td><b>Genome Wide Association (consensus genotype)</b><br>
1. Select a <a href="downloads/select_all.php">set of lines, trait, and trials</a> (one trait).<br>
2. Select the <a href="maps/select_map.php">genetic map</a> which has the best coverage for this set.<br>
3. Return to this page and select model options then GWAS Analysis<br>
<td><b>Genome Wide Association (single genotype experiment)</b><br>
1. Select a <a href="downloads/select_genotype.php">set of lines by genotype experiment</a>.<br>
2. Select a <a href="phenotype/phenotype_selection.php">trait and phenotype trial</a>.<br>
3. Select the <a href="maps/select_map.php">genetic map</a> which has the best coverage for this set.<br>
4. Return to this page and select model options then GWAS Analysis<br>
<tr><td colspan=2><b>Genomic Prediction</b><br>
1. Select a <a href="downloads/select_all.php">set of lines, trait, and trials</a> (one trait).<br>
2. Return to this page and select G-BLUP Analysis for cross-validation of the training set. Then save Training Set.<br>
3. To select a validation set, select a new set of lines using a different trial, then return to this page for analysis.<br>
4. To select a prediction set, select a new set of lines without phenotype measurements, then return to this page for analysis.<br>
</table>
<p><a href="downloads/genomic-tools.php">Additional notes on GWAS and G-BLUP methods</a><br>
<?php
}
if (!empty($_SESSION['training_traits']) && !empty($_SESSION['training_trials'])) {
echo "<table>";
echo "<tr><td>Set<td>Trials<td>Lines<td>";
$p_uid = $_SESSION['training_traits'];
$p_uid = $p_uid[0];
$sql = "select phenotypes_name from phenotypes where phenotype_uid = $p_uid";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_array($res);
echo "<tr><td>Training<td>";
if (!empty($_SESSION['training_trials'])) {
$tmp = $_SESSION['training_trials'];
$e_uid = implode(",",$tmp);
$sql = "select trial_code from experiments where experiment_uid IN ($e_uid)";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
while ($row = mysqli_fetch_array($res)) {
echo "$row[0]<br>";
}
}
echo "<td>";
if (count($_SESSION['training_lines']) > 0) {
$selectedlines = implode(",", $_SESSION['training_lines']);
$sql_option = " AND lr.line_record_uid IN ($selectedlines)";
} else {
$sql_option = "";
}
$sql = "SELECT count(DISTINCT lr.line_record_uid)
FROM tht_base as tb, phenotype_data as pd, phenotypes as p, line_records as lr
WHERE pd.tht_base_uid = tb.tht_base_uid
$sql_option
AND p.phenotype_uid = pd.phenotype_uid
AND lr.line_record_uid = tb.line_record_uid
AND pd.phenotype_uid = $p_uid
AND tb.experiment_uid IN ($e_uid)";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_array($res);
echo "$row[0]";
?>
<td>
<form method="LINK" action="gensel.php">
<input type="hidden" value="step1gensel" name="function">
<input type="hidden" value="clear" name="cmd">
<input type="submit" value="Clear Selection">
</form>
<?php
if (empty($_SESSION['selected_lines'])) {
echo "</table>";
}
} elseif (!empty($_SESSION['phenotype']) && !empty($_SESSION['selected_trials']) ) {
?>
<table>
<tr><td>Traits<td>Trials<td>Lines<td>Genetic Map
<tr><td>
<?php
$traits = $_SESSION['phenotype'];
$map = $_SESSION['selected_map'];
//$traits= implode(",",$tmp); use when I get this working for multiple traits
$sql = "select phenotypes_name from phenotypes where phenotype_uid IN ($traits)";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
while ($row = mysqli_fetch_array($res)) {
echo "$row[0]<br>";
}
echo "<td>";
$tmp = $_SESSION['selected_trials'];
$e_uid = implode(",",$tmp);
$sql = "select trial_code from experiments where experiment_uid IN ($e_uid)";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
while ($row = mysqli_fetch_array($res)) {
echo "$row[0]<br>";
}
echo "<td>";
$count = count($_SESSION['selected_lines']);
echo "$count<td>";
if (isset($_SESSION['geno_exps'])) {
$geno_exp = $_SESSION['geno_exps'];
$geno_str = $geno_exp[0];
$sql = "select marker_uid from allele_bymarker_exp_101 where experiment_uid = $geno_str and pos is not null limit 10";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
if ($row = mysqli_fetch_array($res)) {
$sql = "select trial_code from experiments where experiment_uid = $geno_str";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_array($res);
$name = $row[0];
echo "using map from genotype experiment<br>$name";
} elseif (isset($_SESSION['selected_map'])) {
$sql = "select mapset_name from mapset where mapset_uid = $map";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_assoc($res);
$map_name = $row['mapset_name'];
echo "$map_name";
}
} elseif (isset($_SESSION['selected_map'])) {
$sql = "select mapset_name from mapset where mapset_uid = $map";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_assoc($res);
$map_name = $row['mapset_name'];
echo "$map_name";
}
echo "</table>";
if ($count < 10) {
echo "<font color=red>Warning: analysis may fail with only $count lines selected</font><td>";
}
$min_maf = 5;
$max_missing = 10;
$max_miss_line = 10;
$lines = $_SESSION['selected_lines'];
$count_markers = calculate_db($lines, $min_maf, $max_missing, $max_miss_line);
$count_lines = count($lines);
$estimate = ($count_markers * $count_lines) / 10000;
if ($count > 0) {
?>
<br><table><tr><td>Minimum MAF ≥ <input type="text" name="mmaf" id="mmaf" size="2" value="<?php echo ($min_maf) ?>" /><br>
Remove markers missing > <input type="text" name="mmm" id="mmm" size="2" value="<?php echo ($max_missing) ?>" />% of data<br>
<?php
if (!isset($_SESSION['geno_exps'])) {
?>
Remove lines missing > <input type="text" name="mml" id="mml" size="2" value="<?php echo ($max_miss_line) ?>" />% of data<br>
<?php
} else {
?>
<input type="hidden" name="mml" id="mml">
<?php
}
if (isset($_SESSION['outliers'])) {
?>
Remove trait outliers <input type="checkbox" id="removeOutlier" value="Y">
<?php
}
?>
<td><input type="button" value="Filter Lines and Markers" onclick="javascript:filter_lines();"/>
</table>
</div>
<div id="filter" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%">
<img alt="spinner" id="spinner" src="images/ajax-loader.gif" style="display:none;" /></div>
<div id="step1" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%"></div>
<div id="step2" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%">
<table border=0>
<tr><td rowspan=2>
<input type="button" value="Analyze" onclick="javascript:load_genomic_gwas(<?php echo $estimate; ?>)"> GWAS
<td>principal components
<td><select name="model2" onchange="javascript: update_fixed(this.value)">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<tr><td>method
<td><input type="radio" name="P3D" checked value="TRUE">EMMAX (faster but can underestimate significance)<br>
<input type="radio" name="P3D" value="FALSE">EMMA with REML
<tr><td><input type="button" value="Analyze" onclick="javascript:load_genomic_prediction('<?php echo $estimate; ?>')"> G-BLUP
<td>
</table><br>
<form action="gensel.php">
<input type="hidden" value="step1gensel" name="function">
<input type="hidden" value="save_t" name="cmd">
<input type="submit" value="Save Training Set">
then continue to select prediction set
</form>
</div>
<div id="step3" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%"></div>
<div id="step4" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%"></div>
<div id="step5" style="clear: both; float: left; margin-bottom: 1.5em; width: 100%">
<?php
} else {
echo "<font color=red>Warning, not a valid combination of traits, trials, and lines</font>";
}
}
?>
</p>
<?php
}
/**
* setup results page for R stat analysis
*/
private function genomic_prediction() {
?>
<h2>Genomic Selection</h2>
<img alt="spinner" id="spinner" src="images/ajax-loader.gif" style="display:none;" />
<?php
}
/**
* call R program for displaying histograms
*/
private function run_histo() {
global $mysqli;
$unique_str = $_GET['unq'];
$dir = '/tmp/tht/';
$filename1 = 'THTdownload_hapmap_' . $unique_str . '.txt';
$filename2 = 'THTdownload_traits_' . $unique_str . '.txt';
$filename3 = 'THTdownload_histo_' . $unique_str . '.R';
$filename4 = 'THTdownload_histo_' . $unique_str . '.png';
$filename5 = 'process_error_histo_' . $unique_str . '.txt';
if (isset($_SESSION['training_traits'])) {
$phenotype = $_SESSION['training_traits'];
$phenotype = $phenotype[0];
} elseif (isset($_SESSION['selected_traits'])) {
$phenotype = $_SESSION['selected_traits'];
$phenotype = $phenotype[0];
}
$sql = "select phenotypes_name, unit_name from phenotypes, units
where phenotypes.unit_uid = units.unit_uid
and phenotype_uid = $phenotype";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_array($res);
$phenolabel = $row[0];
$phenounit = $row[1];
$ntrials = 0;
$triallabel = "";
if (isset($_SESSION['selected_trials'])) {
$trials = $_SESSION['selected_trials'];
foreach ($trials as $uid) {
$sql = "select trial_code from experiments where experiment_uid = $uid";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
if ($row = mysqli_fetch_array($res)) {
$trial = $row[0];
}
if ($triallabel == "") {
$triallabel = "triallabel <- list()\n";
}
$triallabel .= "triallabel[$uid] <- \"$trial\"\n";
$ntrials++;
}
}
if (isset($_SESSION['training_trials'])) {
$trials = $_SESSION['training_trials'];
foreach ($trials as $uid) {
$sql = "select trial_code from experiments where experiment_uid = $uid";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
if ($row = mysqli_fetch_array($res)) {
$trial = $row[0];
}
if ($triallabel == "") {
$triallabel= "triallabel <- list()\n";
}
$triallabel .= "triallabel[$uid] <- \"$trial\"\n";
$ntrials++;
}
}
$histo_width = 800;
if ($ntrials > 3) {
$histo_width = 800 + ($ntrials - 3) * 200;
}
if(!file_exists($dir.$filename3)){
$h = fopen($dir.$filename3, "w+");
$png = "png(\"$dir$filename4\", width=$histo_width, height=300)\n";
$cmd1 = "phenoData <- as.matrix(read.delim(\"$dir$filename2\", header=TRUE, na.strings=\"-999\", stringsAsFactors=FALSE, sep=\"\\t\", row.names=1))\n";
$cmd1 = "phenoData <- read.table(\"$dir$filename2\", header=TRUE, na.strings=\"-999\", stringsAsFactors=FALSE, sep=\"\\t\", row.names=NULL)\n";
$cmd2 = "phenolabel <- \"$phenolabel\"\n";
$cmd3 = "phenounit <- \"$phenounit\"\n";
$cmd4 = $triallabel;
fwrite($h, $png);
fwrite($h, $cmd1);
fwrite($h, $cmd2);
fwrite($h, $cmd3);
fwrite($h, $cmd4);
fclose($h);
}
exec("cat /tmp/tht/$filename3 R/GShisto.R | R --vanilla > /dev/null 2> /tmp/tht/$filename5");
if (file_exists("/tmp/tht/$filename5")) {
$h = fopen("/tmp/tht/$filename5", "r");
while ($line=fgets($h)) {
echo "$line<br>\n";
}
fclose($h);
}
if (file_exists("/tmp/tht/$filename4")) {
print "<img src=\"/tmp/tht/$filename4\" /><br>";
} else {
echo "Error in R script R/GShisto.R<br>\n";
}
}
private function display_gwas_hits($h) {
global $mysqli;
echo "Top five marker scores from GWAS analysis<br>";
echo "<table><tr><td>marker<td>chrom<td>pos<td>value<td>external link (resource name)";
$line= fgetcsv($h);
while ($line= fgetcsv($h)) {
$link = "";
$sql = "select value, name_annotation, linkout_string_for_annotation
from markers, marker_annotations, marker_annotation_types
where markers.marker_uid = marker_annotations.marker_uid
and marker_annotations.marker_annotation_type_uid = marker_annotation_types.marker_annotation_type_uid
and marker_name = \"$line[1]\"";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
while ($row = mysqli_fetch_assoc($res)) {
$reg_pattern = '/XXXX/';
$replace_string = $row['value'];
$name = $row['name_annotation'];
$source_string = $row['linkout_string_for_annotation'];
$linkString = preg_replace($reg_pattern, $replace_string, $source_string);
if ($link == "") {
if ($linkString != "") {
$link = "<a href=\"$linkString\" target=\"_new\">$replace_string</a> ($name)";
}
} else {
if ($linkString != "") {
$link .= "<br><a href=\"$linkString\" target=\"_new\">$replace_string</a> ($name)";
}
}
}
if ($count < 5) {
$markerlink = "<a href=$config[base_url]view.php?table=markers&name=$line[1]>$line[1]</a>";
echo "<tr><td>$markerlink<td>$line[2]<td>$line[3]<td>$line[4]<td>$link\n";
}
$count++;
}
fclose($h);
echo "</table>";
}
/**
* display gwas results
*/
private function status_gwas() {
$unique_str = $_GET['unq'];
$dir = '/tmp/tht/';
$found = 1;
$filename9 = 'THTdownload_hmp_' . $unique_str. '.txt';
$filename2 = 'THTdownload_traits_' . $unique_str . '.txt';
$filename3 = 'THTdownload_gwa_' . $unique_str . '.R';
$filename4 = 'THTdownload_gwa1_' . $unique_str . '.png';
$filename7 = 'THTdownload_gwa2_' . $unique_str . '.png';
$filename10 = 'THTdownload_gwa3_' . $unique_str . '.png';
$filename5 = 'process_error_gwa_' . $unique_str . '.txt';
$filename6 = 'R_error_gwa_' . $unique_str . '.txt';
$filename1 = 'THT_result_' . $unique_str . '.csv';
$filenameK = 'Kinship_matrix_' . $unique_str . '.csv';
if (file_exists("/tmp/tht/$filename7")) {
} else {
//echo "$filename7 not ready<br>\n";
$found = 0;
}
if (file_exists("/tmp/tht/$filename10")) {
} else {
//echo "$filename10 not ready<br>\n";
$found = 0;
}
if (file_exists("/tmp/tht/$filename4")) {
} else {
//echo "$filename4 not ready<br>\n";
$found = 0;
}
if (file_exists("/tmp/tht/$filename5")) {
$h = fopen("/tmp/tht/$filename5", "r");
while ($line=fgets($h)) {
echo "$line<br>\n";
}
fclose($h);
}
if (file_exists("/tmp/tht/$filename3")) {
// Extract the Trait name from the .R file.
$h = fopen("/tmp/tht/$filename3", "r");
while ($line=fgets($h)) {
if (strpos($line, 'phenolabel') !== FALSE) {
$traitname = preg_replace('/phenolabel <- "(.*)"/', '$1', $line);
}
}
fclose($h);
}
if ($found) {
print "<img src=\"/tmp/tht/$filename7\" width=\"800\"/><br>";
print "<img src=\"/tmp/tht/$filename10\" width=\"800\"/><br>";
print "<img src=\"/tmp/tht/$filename4\" width=\"800\" /><br>";
print "Trait: <b>$traitname</b><p>";
print "<a href=/tmp/tht/$filename1 target=\"_blank\" type=\"text/csv\">Export GWAS results to CSV file</a> ";
print "with columns for marker name, chromosome, position, marker score<br><br>";
print "<a href=/tmp/tht/$filenameK target=\"_blank\" type=\"text/csv\">Export Kinship matrix</a><br><br>";
$count = 0;
$h = fopen("/tmp/tht/$filename1", "r");
if ($h) {
$this->display_gwas_hits($h);
}
} else {
if (isset($_SESSION['filtered_ines'])) {
$lines = $_SESSION['filtered_lines'];
} else {
$lines = $_SESSION['selected_lines'];
}
if (isset($_SESSION['filtered_markers'])) {
$markers = $_SESSION['filtered_markers'];
} else {
$markers = $_SESSION['geno_exps_cnt'];
}
$estimate = count($lines) * count($markers);
$estimate = round($estimate/6000000,1);
echo "Results not ready yet. Estimated analysis time is $estimate minutes using default options.<br>";
?>
<font color=red>Select the "Check Results" button to retrieve results.<br>
<input type="button" value="Check Results" onclick="javascript: run_status('<?php echo $unique_str; ?>');"/>
</font>
<?php
}
}
/**
* display genomic prediction results
*/
private function status_pred() {
$unique_str = $_GET['unq'];
$dir = '/tmp/tht/';
$found = 1;
$filename1 = 'THTdownload_hapmap_' . $unique_str . '.txt';
$filename2 = 'THTdownload_traits_' . $unique_str . '.txt';
$filename3 = 'THTdownload_gensel_' . $unique_str . '.R';
$filename10 = 'THTdownload_gensel2_' . $unique_str . '.png';
$filename4 = 'THTdownload_gensel_' . $unique_str . '.png';
$filename5 = 'THT_process_error_' . $unique_str . '.txt';
$filename6 = 'THT_R_error_' . $unique_str . '.txt';
$filename7 = 'THT_result_' . $unique_str . '.csv';
if (file_exists("/tmp/tht/$filename10")) {
print "<img src=\"/tmp/tht/$filename10\" /><br>";
} else {
$found = 0;
}
if (file_exists("/tmp/tht/$filename4")) {
print "<img src=\"/tmp/tht/$filename4\" /><br>";
if (isset($_SESSION['selected_trials'])) {
print "<a href=/tmp/tht/$filename7 target=\"_blank\" type=\"text/csv\">Export prediction to CSV file</a><br><br>";
} else {
print "Cross-validation of training set using 5 folds and 2 repeats.<br>\n";
print "<a href=/tmp/tht/$filename7 target=\"_blank\" type=\"text/csv\">Export Cross-validated prediction to CSV file</a><br><br>";
}
} else {
$found = 0;
}
if (file_exists("/tmp/tht/$filename5")) {
$h = fopen("/tmp/tht/$filename5", "r");
while ($line=fgets($h)) {
echo "$line<br>\n";
}
fclose($h);
}
if (file_exists("/tmp/tht/$filename6")) {
$h = fopen("/tmp/tht/$filename6", "r");
while ($line=fgets($h)) {
echo "$line<br>\n";
}
fclose($h);
}
if ($found == 0) {
$lines = $_SESSION['filtered_lines'];
$markers = $_SESSION['filtered_markers'];
$estimate = count($lines) + count($markers);
$estimate = round($estimate/700,1);
echo "Results not ready yet. Estimated analysis time is $estimate minutes.<br>";
?>
<font color=red>Select the "Check Results" button to retrieve results.<br>
<input type="button" value="Check Results" onclick="javascript: run_status('<?php echo $unique_str; ?>');"/>
</font>
<?php
}
}
/**
* display results from GWAS
*/
private function run_gwa() {
global $mysqli;
$unique_str = $_GET['unq'];
$model_opt = $_GET['fixed2'];
$p3d = $_GET['p3d'];
if (isset($_SESSION['training_traits'])) {
$phenotype = $_SESSION['training_traits'];
$phenotype = $phenotype[0];
//} elseif (isset($_SESSION['selected_traits'])) { use when multiple traits is working
} elseif (isset($_SESSION['phenotype'])) {
$phenotype = $_SESSION['phenotype'];
}
$sql = "select phenotypes_name, unit_name from phenotypes, units
where phenotypes.unit_uid = units.unit_uid
and phenotype_uid = $phenotype";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_array($res);
$phenolabel = $row[0];
//$unique_fld = chr(rand(65,80)).chr(rand(65,80)).chr(rand(65,80)).chr(rand(65,80));
//mkdir("/tmp/tht/$unique_fld"); it would be better to put all files in directory
$dir = '/tmp/tht/';
$filename9 = 'THTdownload_hmp_' . $unique_str. '.txt';
$filename2 = 'THTdownload_traits_' . $unique_str . '.txt';
$filename3 = 'THTdownload_gwa_' . $unique_str . '.R';
$filename4 = 'THTdownload_gwa1_' . $unique_str . '.png';
$filename7 = 'THTdownload_gwa2_' . $unique_str . '.png';
$filename10 = 'THTdownload_gwa3_' . $unique_str . '.png';
$filename5 = 'process_error_gwa_' . $unique_str . '.txt';
$filename6 = 'R_error_gwa_' . $unique_str . '.txt';
$filename1 = 'THT_result_' . $unique_str . '.csv';
$filenameK = 'Kinship_matrix_' . $unique_str . '.csv';
if(!file_exists($dir.$filename3)){
$h = fopen($dir.$filename3, "w+");
$png1 = "png(\"$dir$filename4\", width=1200, height=400)\n";
$png2 = "png(\"$dir$filename7\", width=1200, height=400)\n";
$png3 = "png(\"$dir$filename10\", width=1200, height=400)\n";
$png4 = "dev.set(3)\n";
$cmd3 = "phenoData <- read.table(\"$dir$filename2\", header=TRUE, na.strings=\"-999\", stringsAsFactors=FALSE, sep=\"\\t\", row.names=NULL)\n";
$cmd4 = "hmpData <- read.table(\"$dir$filename9\", header=TRUE, stringsAsFactors=FALSE, sep=\"\\t\", check.names = FALSE)\n";
$cmd5 = "phenolabel <- \"$phenolabel\"\n";
$cmd6 = "fileerr <- \"$dir$filename6\"\n";
$cmd7 = "fileout <- \"$filename1\"\n";
$cmd8 = "model_opt <- \"$model_opt\"\n";
$cmd9 = "fileK <- \"$filenameK\"\n";
fwrite($h, $png1);
fwrite($h, $png2);
fwrite($h, $png3);
fwrite($h, $png4);
fwrite($h, $cmd3);
fwrite($h, $cmd4);
fwrite($h, $cmd5);
fwrite($h, $cmd6);
fwrite($h, $cmd7);
fwrite($h, $cmd8);
fwrite($h, $cmd9);
fwrite($h, "p3d <- $p3d\n");
fwrite($h, "setwd(\"/tmp/tht/\")\n");
fclose($h);
}
exec("cat /tmp/tht/$filename3 R/GSforGWA.R | R --vanilla > /dev/null 2> /tmp/tht/$filename5");
if (file_exists("/tmp/tht/$filename7")) {
print "<img src=\"/tmp/tht/$filename7\" width=\"800\" /><br>";
} else {
echo "Error in R script<br>\n";
echo "cat /tmp/tht/$filename3 R/GSforT3.R | R --vanilla <br>";
}
if (file_exists("/tmp/tht/$filename10")) {
print "<img src=\"/tmp/tht/$filename10\" width=\"800\"/><br>";
}
if (file_exists("/tmp/tht/$filename4")) {
print "<img src=\"/tmp/tht/$filename4\" width=\"800\" /><br>";
print "Trait: $phenolabel<p>";
print "<a href=/tmp/tht/$filename1 target=\"_blank\" type=\"text/csv\">Export GWAS results to CSV file</a> ";
print "with columns for marker name, chromosome, position, marker score<br><br>";
print "<a href=/tmp/tht/$filenameK target=\"_blank\" type=\"text/csv\">Export Kinship matrix</a><br><br>";
$count = 0;
$h = fopen("/tmp/tht/$filename1", "r");
if($h) {
$this->display_gwas_hits($h);
} else {
echo "error - could not open $filename1\n";
}
}
if (file_exists("/tmp/tht/$filename5")) {
$h = fopen("/tmp/tht/$filename5", "r");
while ($line=fgets($h)) {
echo "$line<br>\n";
}
fclose($h);
}
}
/**
* run GWAS results in background and notify when complete
*/
private function run_gwa2() {
global $config;
global $mysqli;
$unique_str = $_GET['unq'];
$model_opt = $_GET['fixed2'];
$p3d = $_GET['p3d'];
if (isset($_SESSION['training_traits'])) {
$phenotype = $_SESSION['training_traits'];
$phenotype = $phenotype[0];
//} elseif (isset($_SESSION['selected_traits'])) { use when multiple traits is working
} elseif (isset($_SESSION['phenotype'])) {
$phenotype = $_SESSION['phenotype'];
}
$sql = "select phenotypes_name, unit_name from phenotypes, units
where phenotypes.unit_uid = units.unit_uid
and phenotype_uid = $phenotype";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_array($res);
$phenolabel = $row[0];
//$unique_fld = chr(rand(65,80)).chr(rand(65,80)).chr(rand(65,80)).chr(rand(65,80));
//mkdir("/tmp/tht/$unique_fld"); it would be better to put all files in directory
$dir = '/tmp/tht/';
$filename9 = 'THTdownload_hmp_' . $unique_str. '.txt';
$filename2 = 'THTdownload_traits_' . $unique_str . '.txt';
$filename3 = 'THTdownload_gwa_' . $unique_str . '.R';
$filename4 = 'THTdownload_gwa1_' . $unique_str . '.png';
$filename7 = 'THTdownload_gwa2_' . $unique_str . '.png';
$filename10 = 'THTdownload_gwa3_' . $unique_str . '.png';
$filename5 = 'process_error_gwa_' . $unique_str . '.txt';
$filename6 = 'R_error_gwa_' . $unique_str . '.txt';
$filename1 = 'THT_result_' . $unique_str . '.csv';
$filenameK = 'Kinship_matrix_' . $unique_str . '.csv';
if(!file_exists($dir.$filename3)){
$h = fopen($dir.$filename3, "w+");
$png1 = "png(\"$dir$filename4\", width=1200, height=400)\n";
$png2 = "png(\"$dir$filename7\", width=1200, height=400)\n";
$png3 = "png(\"$dir$filename10\", width=1200, height=400)\n";
$png4 = "dev.set(3)\n";
$cmd3 = "phenoData <- read.table(\"$dir$filename2\", header=TRUE, na.strings=\"-999\", stringsAsFactors=FALSE, sep=\"\\t\", row.names=NULL)\n";
$cmd4 = "hmpData <- read.table(\"$dir$filename9\", header=TRUE, stringsAsFactors=FALSE, sep=\"\\t\", check.names = FALSE)\n";
$cmd5 = "phenolabel <- \"$phenolabel\"\n";
$cmd6 = "fileerr <- \"$dir$filename6\"\n";
$cmd7 = "fileout <- \"$filename1\"\n";
$cmd8 = "model_opt <- \"$model_opt\"\n";
$cmd9 = "fileK <- \"$filenameK\"\n";
if (isset($_SESSION['username'])) {
$emailAddr = $_SESSION['username'];
$emailAddr = "email <- \"$emailAddr\"\n";