-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsnp_panel.php
224 lines (193 loc) · 7.03 KB
/
snp_panel.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
<?php
function curlCladeFinder($snps)
{
$fields_string = "";
$fields = array(
'input' => urlencode($snps),
'json' => urlencode('phyloeq,downstream,products,score,panels')
);
$url = "https://cladefinder.yseq.net/snp_panel_json.php";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function addChildObj(&$json_obj, $childClade, $childObj)
{
if (array_key_exists("downstream", $json_obj)) {
$count = 0;
foreach($json_obj->{'downstream'} as $obj) {
if ($obj->{'clade'} == $childClade) {
$json_obj->{'downstream'}[$count] = $childObj;
return;
}
addChildObj($json_obj->{'downstream'}[$count], $childClade, $childObj);
$count++;
}
}
}
$levels = 0;
if(isset($_POST['reset'])) {
$levels = $_POST['levels'];
$json_obj = json_decode(str_replace("'", "\"", $_POST['json']));
$thebranch = $json_obj->{'clade'};
$json = curlCladeFinder($thebranch . "--2");
$json_obj = json_decode($json)[0];
}
if(isset($_POST['json']) and !isset($_POST['reset'])) {
$json_obj = json_decode(str_replace("'", "\"", $_POST['json']));
}
if(isset($_POST['json']) == False and isset($_GET['branch'])) {
$branch = $_GET['branch'];
$levels = $_GET['levels'];
$json = curlCladeFinder($branch . "--" . $levels);
$json_obj = json_decode($json)[0];
#echo $json;
}
if(isset($_POST['add'])) {
$addedClade = $_POST['add'];
$addedObj = json_decode(curlCladeFinder($addedClade . "--3"))[0];
addChildObj($json_obj, $addedClade, $addedObj);
} else {
if (isset($_POST['new'])) {
$newClade = $_POST['new'];
$json_obj = json_decode(curlCladeFinder($newClade . "--3"));
}
}
if(isset($_POST['expandAll']) or isset($_POST['expandAll2']) or isset($_POST['expandAll3'])) {
$deflevels = 2;
if (isset($_POST['expandAll'])) {
$deflevels = 2;
}
if (isset($_POST['expandAll2'])) {
$deflevels = 3;
}
if (isset($_POST['expandAll3'])) {
$deflevels = 4;
}
$unexpandedNodes = getLeafNodesWithChildren($json_obj);
$expandedJSON = curlCladeFinder(join(",",$unexpandedNodes) . "--" . $deflevels);
$addedObjects = json_decode($expandedJSON);
foreach($addedObjects as $addedObject) {
addChildObj($json_obj, $addedObject->{'clade'}, $addedObject);
}
}
function getLeafNodesWithChildren($obj) {
$thechildren = array();
if (array_key_exists("downstream", $obj)) {
foreach ($obj->{"downstream"} as $child) {
$thedownstr = getLeafNodesWithChildren($child);
$thechildren = array_merge($thechildren, $thedownstr);
}
}
if (array_key_exists("children", $obj)) {
array_push($thechildren, $obj->{"clade"});
}
return $thechildren;
}
function getPhyloeqHTMLOutput($phyloeq_obj) {
$phyloeq_str = "";
foreach($phyloeq_obj as $key=>$value) {
$color = "blue";
$bold = True;
$phyloeq_str = $phyloeq_str . ' <b><font color="' . $color . '">';
if ($phyloeq_obj->{$key}->{"product"}) {
$phyloeq_str = $phyloeq_str . '<a href="https://www.yseq.net/product_info.php?products_id=' . $phyloeq_obj->{$key}->{"product"} . '" target="_">'.$key.'</a></font></b>';
}
}
return $phyloeq_str;
}
function getIndent($branchesBelow) {
if ($branchesBelow == "") {
return "";
}
$indent = "";
$array = str_split($branchesBelow);
for($i = 0; $i < count($array) - 1; $i++) {
if ($array[$i] == "y") {
$indent = $indent . "┃ ";
} else {
if ($array[$i] != "") {
$indent = $indent . " ";
}
}
}
$arraylen = count($array);
if ($arraylen > 0) {
if ($array[$arraylen - 1] == "y") {
$indent = $indent . "┣";
} else {
$indent = $indent . "┗";
}
}
return $indent . "━";
}
function getSpanOrName($json_obj, $level) {
$clade = $json_obj->{"clade"};
# if ($level == 0) {
# return '<a href="https://www.yfull.com/tree/' . $clade . '" target="_"><b>' . $clade . '</b></a>';
# }
return "<b>" . $clade . "</b>";
}
function getExpandButton($json_obj) {
$clade = $json_obj->{"clade"};
if (array_key_exists("children", $json_obj)) {
return '<button name="add" class="text" type="submit" style="padding:0px;font-size:10px;height:20px;width:20px" value="' . $clade .'">' . '+' . $json_obj->{"children"} . '</button>';
}
return "";
}
function getIconsTableHTML($clade) {
$icons = array('<a href="https://www.yfull.com/tree/' . $clade . '" target="_"><img border="0" alt="Link to ' . $clade . ' on YFull" title="' . $clade . ' on YFull" src="yfull.png" width="16" height="16"></a>',
'<a href="https://hras.yseq.net/hras.php?dna_type=y&map_type=alpha&hg=' . $clade . '" target="_"><img border="0" alt="View maps, migrations and statistics on HRAS" title="' . $clade . ' maps, migrations and statistics on HRAS" src="heat.png" width="16" height="16"></a>',
'<a href="https://www.phylogeographer.com/snp-lookup?' . $clade . '" target="_"><img border="0" alt="View theoretical migration on PhyloGeographer" title="' . $clade . ' on PhyloGeographer" src="phylogeographer.png" width="16" height="16"></a>',
'<a href="https://phylogeographer.com/what-do-all-these-codes-mean/" target="_"><img src="questionmark.png" height="16" width="16"/></a>',
'<button name="expandAll" type="submit">+1</button>',
'<button name="expandAll2" type="submit">+2</submit>',
'<button name="expandAll3" type="submit">+3</submit>',
'<button name="reset" type="submit">↺</submit>');
return '<table border = "1px solid black"><tr><td>' . implode('</td><td>', $icons) . '</td></tr></table>';
}
function recursivelyBuildTree($json_obj, $level, $branchesBelow) {
echo '<tr><td nowrap align="top">' . getIndent($branchesBelow) . getSpanOrName($json_obj, $level) . " " . getExpandButton($json_obj) . '</td><td>' . getPhyloeqHTMLOutput($json_obj->{"phyloeq"}) . "</td></tr>";
if (array_key_exists("downstream", $json_obj)) {
$len = count($json_obj->{"downstream"});
$count = 0;
foreach($json_obj->{"downstream"} as $child) {
$hasBelow = "y";
if ($count == $len - 1) {
$hasBelow = "n";
}
recursivelyBuildTree($child, $level + 1, $branchesBelow . $hasBelow);
$count++;
}
}
}
?>
<html><head></head><body>
<div style="font-family: monospace;">
<form name="interactive_tree" method="post">
<input type="hidden" name="levels" value="<?php echo $levels; ?>">
<input type="hidden" name="json" value="<?php echo str_replace("\"", "'", json_encode($json_obj)); ?>">
<?php
if (array_key_exists("clade", $json_obj)) {
$clade = $json_obj->{"clade"};
echo 'Most specific position on the <a href="https://www.yfull.com" target="_">YFull</a> <a href="https://www.yfull.com/tree/" target="_">YTree</a> is ' . $clade . getIconsTableHTML($clade);
echo '<table style="font-size:10;">';
recursivelyBuildTree($json_obj, 0, "");
echo '</table>';
} else {
if (array_key_exists("error", $json_obj)) {
echo $json_obj->{"error"};
} else {
echo "error: malformed backend response";
}
}
?>
</div>
</body></html>