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
|
<meta charset="utf-8">
<script src="./scripts/d3.v4.min.js"></script>
<script src="./scripts/create_table.js"></script>
<script>
<?php
//Going to modify this so it just writes the uploaded data to a file.
//Standardization and determining other factors will be performed in
// a Matlab/Octave script.
//<link rel="stylesheet" href="./scripts/table.css" type="text/css">
include("header_new.inc");
include("header_batchsearch.inc");
include("runtime_check.php");
include("input_validate.php");
$searchID="";
$UploadValue="NO";
$TextFile=$_FILES["MyFile"]["name"];
/////////////Generate a random key/////////////////////
$alphas=array();
$alphas = array_merge(range('A', 'Z'), range('a', 'z'));
$al1=rand(0,51);
$al2=rand(0,51);
$al3=rand(0,51);
$alpha="$alphas[$al1]"."$alphas[$al2]"."$alphas[$al3]";
$keyval=$alpha;
if($_POST["My_key"]!="")
$keyval=$_POST["My_key"];
$keyval=valid_keyval($keyval);
$sid=$keyval."continuous_input";
//$dir="/var/lib/genenet/bnw/";
//$dir="/var/lib/genenet/bnw/";
$dir="/var/lib/genenet/bnw/";
//$input_table_file="/var/lib/genenet/bnw/".$keyval."input_table.txt";
$input_table_file=$dir.$keyval."input_table.txt";
$TextinFile=$dir.$sid."_orig.txt";
if(isset($_POST["searchkey"]))
{
$searchID=$_POST["searchkey"];
$searchID = str_replace(',', "\t", $searchID);
$searchID = str_replace(';', "\n", $searchID);
}
// GeneNetwork (GN2) posts its data directly: commas are column
// separators, semicolons are row separators. Convert server-side --
// the previous release echoed the raw payload into a JavaScript
// string, which is an XSS hole on public deployments.
// NOTE: GN2's bnw_page.html posts the field as "My_Genenet" while
// earlier integrations used "My_Gennenet" -- accept both spellings.
$gn2_payload = NULL;
foreach (array("My_Gennenet", "My_Genenet") as $gn2_field)
if(isset($_POST[$gn2_field]))
$gn2_payload = $_POST[$gn2_field];
if(!is_null($gn2_payload))
{
// BNW's file format does not allow spaces in variable names
// (readInputData.m: "First line has labels for each of the nodes.
// There cannot be spaces"), but GN2 trait display names often do
// ("Age at death"). The octave parsers split on WHITESPACE
// (strtok), so a spaced name corrupts the header -- the variable
// count no longer matches the labels and structure learning dies
// with "A(0,_): subscripts must be ... integers" in mk_adj_mat.
// Sanitize the NAME ROW (everything before the first semicolon)
// BEFORE the separator conversion: whitespace inside a name becomes
// an underscore.
$name_row_end = strpos($gn2_payload, ";");
if ($name_row_end !== false) {
$name_row = substr($gn2_payload, 0, $name_row_end);
$gn2_payload = preg_replace('/\\s+/', '_', $name_row)
. substr($gn2_payload, $name_row_end);
}
// normalize line endings FIRST (a Windows-style payload must not
// carry \r into the data files), then convert separators
$gn2_payload = str_replace(array("\r\n", "\r"), "\n", $gn2_payload);
$searchID = str_replace(',', "\t", $gn2_payload);
$searchID = str_replace(';', "\n", $searchID);
// GN2 terminates every row with ';' and may ALSO send newlines --
// after conversion that leaves empty lines, and octave's line-based
// parsers treat those as corrupt rows (prep then produces an
// incomplete file set and the page dies). Squeeze them.
$searchID = preg_replace("/\n+/", "\n", trim($searchID));
// --- GN2 payload validation -----------------------------------------
// A malformed payload used to flow through prep and die deep inside
// octave with a cryptic error (e.g. "Could not open
// <key>continuous_input.txt" after GeneNetwork sent a header with NO
// data rows because the selected traits had no sample values
// loaded). Fail fast, in plain English, before any file is written.
$problems = array();
$gn2_notes = array();
$lines = preg_split("/\n/", str_replace("\r\n", "\n", $searchID));
while (count($lines) > 0 && trim($lines[count($lines)-1]) === "")
array_pop($lines);
if (count($lines) < 1 || trim($lines[0]) === "") {
$problems[] = "The posted data is empty -- GeneNetwork sent no variable names and no values.";
} else {
$header_cells = preg_split("/\t/", trim($lines[0]));
// a header of pure numbers means the variable-name row is missing
$all_numeric = true;
foreach ($header_cells as $c)
if (!is_numeric(trim($c))) { $all_numeric = false; break; }
if ($all_numeric)
$problems[] = "The first line contains numbers, not variable names -- the header row is missing. GeneNetwork should send the variable names first (comma separated), then the data rows (semicolon terminated).";
$names = array();
foreach ($header_cells as $i => $c) {
$n = trim($c);
if ($n === "")
$problems[] = "Empty variable name in column ".($i+1)." of the header row.";
$names[] = $n;
}
$ncols = count($names);
if ($ncols < 2)
$problems[] = "Only $ncols variable was found in the header row. Network learning needs at least 2 variables.";
// duplicates: the same trait name can appear twice when the trait
// was measured in more than one data set -- rename the later
// copies so the octave name lookups stay unambiguous
$seen = array();
$renamed = array();
foreach ($names as $i => $n) {
if ($n !== "" && isset($seen[$n])) {
$k = 2;
while (isset($seen[$n."_".$k])) $k++;
$renamed[] = $n." (column ".($i+1).") renamed to ".$n."_".$k;
$names[$i] = $n."_".$k;
}
if ($n !== "") $seen[$names[$i]] = 1;
}
// data rows
$nrows = 0; $rows_seen = 0; $ragged = array(); $bad_cell = array();
$col_all_na = array();
$rows_with_na = 0;
foreach (array_slice($lines, 1) as $ri => $line) {
if (trim($line) === "") continue;
$cells = preg_split("/\t/", $line);
// tolerate a trailing separator ("9.0,2.1,3.4;" etc.)
while (count($cells) > 0 && trim($cells[count($cells)-1]) === "")
array_pop($cells);
if (count($cells) === 0) continue;
$rows_seen++;
$has_value = false;
$row_has_na = false;
foreach ($cells as $ci => $c) {
$t = trim($c);
if ($t === "") { $row_has_na = true; continue; }
if (in_array(strtolower($t), array("na", "n/a", "nan"))) {
$row_has_na = true;
continue;
}
$has_value = true;
if (!is_numeric($t) && !in_array(strtolower($t), array("inf","-inf")))
$bad_cell[] = array($ri + 2, $ci + 1, $t);
}
if (!$has_value) continue; // an all-NA row: prep removes it
$nrows++;
if ($row_has_na) $rows_with_na++;
if (count($cells) !== $ncols)
$ragged[] = array($ri + 2, count($cells));
// track per-column NA coverage
if (!isset($col_na_count)) $col_na_count = array_fill(0, $ncols, 0);
foreach ($cells as $ci => $c) {
$t = trim($c);
if ($ci < $ncols
&& ($t === "" || in_array(strtolower($t), array("na","n/a","nan"))))
$col_na_count[$ci]++;
}
}
// a variable whose every entry is NA/empty: prep crashes on it
// ("Could not open <key>continuous_input.txt")
if (isset($col_na_count) && $nrows > 0)
foreach ($col_na_count as $ci => $cnt)
if ($cnt >= $nrows && isset($names[$ci]) && $names[$ci] !== "")
$problems[] = "Variable '$names[$ci]' has no measured values (every entry is NA or empty). Remove it from the data and try again.";
// if no row survives prep's removal of incomplete rows, the
// learning step dies with "fscanf: invalid stream number = -1"
$usable = $nrows - $rows_with_na; // rows without any NA/empty
if ($nrows > 0 && $usable === 0)
$problems[] = "Every sample row contains at least one missing value (NA). After removing incomplete rows no data would remain -- provide rows without missing values.";
if ($nrows === 0) {
if ($rows_seen > 0)
$problems[] = "All $rows_seen data rows contain only missing values (NA) -- no usable data remains.";
else
$problems[] = "No sample data rows were received -- GeneNetwork sent only the variable names (0 cases). This usually means the selected traits had no sample values loaded in GeneNetwork.";
}
else if ($nrows === 1)
$problems[] = "Only 1 usable sample row was received. Network learning needs at least 2 samples (and works best with many more).";
else if ($usable === 1)
$problems[] = "Only 1 complete sample row -- the other rows have missing values and are removed. Network learning needs at least 2 complete samples.";
if (count($ragged)) {
$p = $ragged[0];
$problems[] = "Row $p[0] has $p[1] value(s) but the header names $ncols variable(s)".(count($ragged) > 1 ? " -- first mismatch of ".count($ragged)." ragged rows" : "").". Every data row needs one value per variable.";
}
if (count($bad_cell)) {
$p = $bad_cell[0];
$problems[] = "Value '$p[2]' in row $p[0], column $p[1] is not a number".(count($bad_cell) > 1 ? " (first of ".count($bad_cell)." such values)" : "").". GeneNetwork should send numeric expression values (or NA for missing).";
}
if ($ncols > 30)
$gn2_notes[] = "Large data set: $ncols variables. Structure learning may take a long time.";
if (count($renamed))
$gn2_notes[] = "Duplicate variable name(s) renamed: ".implode("; ", $renamed).". The same trait can appear twice when it was measured in more than one data set.";
if (!count($problems) && count($renamed)) {
$lines[0] = implode("\t", $names);
$searchID = implode("\n", $lines);
}
}
if (count($problems)) {
?>
<ul class="navbar2">
<li><a href="help.php#file_format" target="_blank">Data formatting guidelines</a>
<li><a href="home.php">Home</a>
</ul>
<div id="outernew">
<br>
<h2>The data could not be used for network learning</h2>
<ul>
<?php foreach ($problems as $p) print("<li style='color:brown'>$p</li>\n"); ?>
</ul>
<p>Please check the data on the GeneNetwork side and try again, or see the
<a href="help.php#file_format" target="_blank">data formatting guidelines</a>.</p>
<a class=button3 href="home.php">Back to the data upload page</a>
</div>
</body>
</html>
<?php
exit();
}
}
if($searchID=="")
{
?>
<!-- Site navigation menu -->
<ul class="navbar2">
<li><a href="sample_input.php" target="_blank">View sample input file</a>
<li><a href="help.php#file_format" target="_blank">Data formatting guidelines</a>
<li><a href="about_this_page.php#file_upload" target="_blank">About this page</a>
<li><a href="help.php" target="_blank">Help</a>
<li><a href="home.php">Home</a>
</ul>
<?php
}
if(isset($_POST["MyUpload"]))
{
$UploadValue=$_POST["MyUpload"];
if ($UploadValue=="YES")
{
if($TextFile!="")
{
// $TextFile = valid_input($TextFile);
$sta=move_uploaded_file($_FILES['MyFile']['tmp_name'],$TextinFile);
if(!$sta)
{
echo "<script type='text/javascript'> window.alert ('Sorry, error uploading $TextFile.')</script>";
flush();
exit();
}
else
{
$searchID=file_get_contents("$TextinFile");
//unlink($TextinFile);
}
}
else
{
echo "<script type='text/javascript'> window.alert ('Sorry, please select upload file.')</script>";
}
}
}
?>
<div id="outernew">
<br>
<FORM name="key_search" id="in_form" enctype="multipart/form-data" ACTION="bn_genenet.php" METHOD=POST>
<INPUT style="background-color:#FFFFFF;" type="file" name="MyFile" id="MyFile_id" class="inputfile" onchange="upload_submit()" >
<label for="MyFile_id"> Loading data from GeneNetwork </label>
<INPUT TYPE="hidden" NAME="My_key" value=<?php print($keyval) ?> >
<INPUT TYPE="hidden" name="MyUpload" value="NO">
<textarea name="searchkey" style="display:none;"><?PHP print($searchID)?> </textarea>
</FORM>
</div>
<script>
function upload_submit() {
Upload();
document.getElementById("in_form").submit();
}
</script>
<?php
if($searchID!="")
{
if ($UploadValue=="NO")
{
$fpdata = fopen($dir.$keyval."continuous_input_orig.txt","w");
if ($fpdata !== false) {
fwrite($fpdata,$searchID);
fclose($fpdata);
}
}
$keyval = valid_keyval($keyval);
shell_exec(__DIR__.'/run_scripts/run_prep_input '.$keyval);
$parent_number=4;
$k_number=1;
$runtime=exe_time($keyval,$parent_number,$k_number);
?>
<ul class="navbar2">
<li><a href="about_this_page.php#file_upload" target="_blank">About this page</a>
<li><a href="help.php" target="_blank">Help</a>
<li><a href="home.php">Home</a>
</ul>
<div id="outernew_load">
<br>
<?php if (isset($gn2_notes) && count($gn2_notes)) { ?>
<ul>
<?php foreach ($gn2_notes as $gn2_note) print("<li style='color:darkgreen'>$gn2_note</li>\n"); ?>
</ul>
<br>
<?php } ?>
<a class=button3 href="executionprogress_default.php?My_key=<?php print($keyval);?>">Perform Bayesian network modeling using default settings. Estimated runtime: <?php print($runtime);?> seconds</a>
<br>
<a class=button3 href="create_tiers_gom_part1.php?My_key=<?php print($keyval);?>">Go to structure learning settings and the BNW structural constraint interface</a>
<br>
<a class=button3 href="remove_variables.php?My_key=<?php print($keyval);?>">Remove variables from data set</a>
<br>
<br>
<p><h3>The uploaded data file has the following properties:
<br></h3>
<?php
$table_text = json_encode(file("file://".$input_table_file));
?>
<div class="d3_table" id="table_div1">
<script type="text/javascript">
d3.text("", function(error, raw_temp){
var dsv = d3.dsvFormat("\t")
var data1 = <?php echo $table_text?>
var data2 = data1.join("")
var data = dsv.parse(data2)
var caption_text = data.pop()
if (error) throw error;
tabulate_caption("#table_div1",data,caption_text.Variable);
});
</script>
</div>
<br><br><br></h3>
</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>
<?php
}
?>
</body>
</html>
|