-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_visualization.php
More file actions
146 lines (129 loc) · 4.32 KB
/
build_visualization.php
File metadata and controls
146 lines (129 loc) · 4.32 KB
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
<?php
if (!defined('PATH_TO_IS_MANAGER')) {
define('PATH_TO_IS_MANAGER', '');
}
require PATH_TO_IS_MANAGER . 'inc/init.php';
$wsi_filename = $_GET["filename"];
$fileDir = "/" . fm_clean_path($_GET["directory"]);
$relativeFileDir = fm_clean_path($_GET["relativeDirectory"]);
if (!$wsi_filename || !$fileDir || !$relativeFileDir) {
fm_set_msg('Invalid file!', 'error');
?>
<script>
window.history.back();
</script>
<?php
die();
}
function scan_json_def($full_path, $filename, $wsi_filename_text, $relativeFileDir, &$output)
{
$fname_text = pathinfo($filename, PATHINFO_FILENAME);
//called twice to make sure, so
$wsi_fname_text = strtok($wsi_filename_text, ".");
if (preg_match("/$wsi_fname_text.*\.json/", $filename)) {
try {
$output[$fname_text] = json_decode(file_get_contents($full_path));
$output[$fname_text]->file = "$relativeFileDir/{$output[$fname_text]->file}";
} catch (Exception $e) {
//pass
}
} else if (preg_match("/$wsi_fname_text.*\.tiff?/", $filename)) {
if (!isset($output[$fname_text])) {
$output[$fname_text] = (object)array(
"file" => "$relativeFileDir/$filename",
"order" => 0,
"default" => false
);
}
}
}
$objects = is_readable($fileDir) ? scandir($fileDir) : array();
$specs = array();
$wsi_text = pathinfo($wsi_filename, PATHINFO_FILENAME);
$default_vis = array();
if (is_array($objects)) {
$root_specs = array();
$specs[] = $root_specs;
foreach ($objects as $file) {
if ($file == '.' || $file == '..') continue;
$fpath = "$fileDir/$file";
if (is_dir($fpath)) {
$nested = is_readable($fpath) ? scandir($fpath) : array();
if (is_array($nested)) {
$child_specs = array();
foreach ($nested as $nestedFile) {
if ($nestedFile == '.' || $nestedFile == '..') continue;
scan_json_def("$fpath/$nestedFile", $nestedFile, $wsi_text,
"$relativeFileDir/$file", $child_specs);
}
$specs[] = $child_specs;
}
} else if (is_file($fpath) && $file !== $wsi_filename) {
if ($file === "default.json") {
//override default visualisation of heatmap with default.json if present
try {
$default_vis = json_decode(file_get_contents($fpath));
} catch (Exception $e) {
//pass
}
} else {
scan_json_def($fpath, $file, $wsi_text, $relativeFileDir, $root_specs);
}
}
}
}
foreach ($specs as $goal) {
$i = 0;
usort($goal, function ($a, $b) {
return $a->order - $b->order;
});
foreach ($goal as $name => $spec) {
if (isset($spec->default) && $spec->default === false) {
if ($i < count($default_vis)) {
$spec->default = $default_vis[$i];
} else {
$spec->default = "heatmap";
}
}
$i++;
}
}
$shader_data = json_encode($specs);
$viewer_url = FM_XOPAT_URL;
$js_path = _FM_JS_PATH;
$user_id = FM_USER_ID;
$wsi_meta_api = FM_WSI_IMPORTER_API;
global $browser_relative_root;
echo <<<EOF
<head>
<script type="text/javascript" src="$js_path/viewerConfig.js">
</script>
</head><body>
<span id='$relativeFileDir/$wsi_filename-meta' style='display: none' data-microns-x='{$_GET["microns"]}' data-microns-y=''></span>
<script>
const data = $shader_data;
const tissuePath = '$relativeFileDir/$wsi_filename';
window.viewerConfig = new ViewerConfig({
windowName: 'viewerConfig',
viewerUrl: '$viewer_url',
importerMetaEndpoint: '$wsi_meta_api',
urlRoot: '$browser_relative_root',
data: '',
});
viewerConfig.setTissue(tissuePath);
let run = false;
for (let goal of data) {
if (goal.length < 1) continue;
//just first set visualised for now, config cannot handle multiple :/
for (let key in goal) {
const spec = goal[key];
viewerConfig.setShaderFor(spec.file, spec.default);
}
run = true;
viewerConfig.open();
break;
}
if (!run) viewerConfig.open();
</script>
</body>
EOF;