-
Notifications
You must be signed in to change notification settings - Fork 2
/
webgiviAPI.php
executable file
·580 lines (517 loc) · 20.4 KB
/
webgiviAPI.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
<?php
//**************************************************
// API 1: submit all IDs to WebGIVI API (filter Table)
// 2015-03-22
//**************************************************
session_start();
require_once ('display_fns.php');
display_header();
?>
<script>
function loadTable(fileName, flag_egift, conceptName) {
var currentData;
var tooltip;
var rowLength = 4;
var fileName_txt = '/var/www/data/'+fileName+'.txt';
d3.text(fileName_txt, function (geneItems) {
//var datas = geneItems.split("\r\n");
geneItems = geneItems.replace(/\r\n/g, '\n');
geneItems = geneItems.replace(/\r/g, '\n');
//console.log(geneItems);
if (geneItems.indexOf('\t') == -1) {
alert('Warning, your data format is wrong or your input IDs are too many, if not, please contact us!');
location.href = "index.php";
return;
}
var datas = geneItems.split("\n");
var geneItemData = {};
geneItemData.genes = [];
geneItemData.Items = [];
for (var i = 0; i < datas.length; ++i) {
if (datas[i] == "")
continue;
var pairs = datas[i].split("\t");
if (pairs.length == 2){
geneItemData.genes.push(pairs[0]);
geneItemData.Items.push(pairs[1]);
}else{
alert('Warning, the line of your input data is not tab delimited:' + datas[i]);
}
}
if (geneItemData.genes.length == 0 || geneItemData.Items.length == 0) {
alert('The eGIFT result of your input data is empyt OR your input data format is not correct OR Your input data is too big!');
location.href = "index.php";
return;
}
currentData = partData(geneItemData); //0 genes 1 items
tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("fill", "#333")
.style("font-size", "12px")
.style("background", "#eee")
.style("box-shadow", "0 0 5px #999999")
.style("position", "absolute")
.style("z-index", "10");
refreshTable(currentData);
});
var dataSelect = []; //save all highlighted iTerms
function refreshTable(Data) { //Data saves all the existing items in current views //yongnan
d3.select("#delete").on("click", function (d) {
d3.selectAll('.checkBox')[0].forEach(function (d, i) {
if (d.checked) {
var currentdata = d3.select("#" + d.id).datum();
var index = Data.indexOf(currentdata);
if (index > -1) {
Data.splice(index, 1);
}
}
});
//rewrite data to the gene iterm file???????????????????????????????????????????????????
var saveString = "";
for (var i = 0; i < Data.length; ++i) {
for (var j = 0; j < Data[i].genes.length - 1; ++j) {
if (Data[i].genes[j] !== "") {
saveString += Data[i].genes[j];
saveString += "\t";
saveString += Data[i].keys
saveString += "\n";
}
}
saveString += Data[i].genes[Data[i].genes.length - 1];
saveString += "\t";
saveString += Data[i].keys
saveString += "\n";
}
$.ajax({
url: 'update.php',
type: "POST",
data: {
txt: saveString,
fileName: fileName
},
dataType: "text",
beforeSend: function () {
$('#wait').show();
},
success: function () {
$('#wait').hide();
},
error: function () {
$('#wait').hide();
alert("failure");
}
});
refreshTable(Data);
});
//highlight all iterms with the frequency users defined
function isNumber(obj) {
return !isNaN(parseFloat(obj))
};
// define cutoff to highlight all iTerms
$("#select_freq").click(function () {
var input_freq = $("#input_freq").val();
if (!isNumber(input_freq)) {
alert("Please your input data type should be number!");
return;
}
//console.log(Data);
for (var i = 0; i < Data.length; ++i) {
if (Data[i].sum <= input_freq) {
d3.select("#" + Data[i].id).property('checked', true);
d3.select("#cellTd" + Data[i].id).attr("class", "highlight");
dataSelect.push(Data[i].id);
}
}
});
// sort based on alphebetical or frequency.
$('#sortTable').change(function () {
if (this.value == 'freq') {
//console.log('click freq');
//sort Data based on freq
sortObjByFreq(Data);
refreshTable(Data);
}
else {
//console.log('click alpha');
//sort based on alphabetical order of iTerms
sortObjByAlpha(Data);
refreshTable(Data);
}
});
function sortObjByFreq(Data) {
var swapped;
do {
swapped = false;
for (var i = 0; i < Data.length - 1; ++i) {
if (Data[i].sum > Data[i + 1].sum) {
var tmp = Data[i];
Data[i] = Data[i + 1];
Data[i + 1] = tmp;
swapped = true;
}
}
} while (swapped)
}
function sortObjByAlpha(Data) {
var swapped;
do {
swapped = false;
for (var i = 0; i < Data.length - 1; ++i) {
if (Data[i].keys > Data[i + 1].keys) {
var tmp = Data[i];
Data[i] = Data[i + 1];
Data[i + 1] = tmp;
swapped = true;
}
}
} while (swapped)
}
$("#clear").off().click(function () {
for (var i = 0; i < Data.length; ++i) {
d3.select("#" + Data[i].id).property('checked', false);
d3.select("#cellTd" + Data[i].id).attr("class", "normal");
}
dataSelect = [];
});
//make sure data is rewrite successfully, and then we can use visualize the data
var fileNameDir = '/var/www/data/'+fileName+'.txt';
$("#view").off().click(function () {
window.open(fileNameDir);
});
$("#cytoscape").off().click(function () {
window.open('cytoscape.php?fileName=' + fileName, '_blank');
});
$('#conceptMap').off().click(function () {
window.open('conceptMap.php?fileName=' + fileName + '&flag_egift=' + flag_egift + '&conceptName=' + conceptName, '_blank');
});
$('#download').off().click(function () {
window.location.href = "download.php?path=" + fileNameDir;
});
var margin = {top: 20, right: 10, bottom: 30, left: 10},
width = 800 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
if (d3.select('#d3table').select('table')) {
d3.select('#d3table').select('table').remove();
}
var table = d3.select('#d3table').append("table")
//.attr("width", width + margin.left + margin.right);
.attr("height", height + margin.top + margin.bottom);
table.append("tbody");
var matrix = [];
var currentData = clone(Data);
//click visualize tool, then save the data into usrID folder
while (currentData.length) {
matrix.push(currentData.splice(0, rowLength));
}
// ***********************how to understand clone function********************************
// function clone(obj){
// var o;
// switch(typeof obj){
// case 'undefined': break;
// case 'string' : o = obj +' ';break;
// case 'number' : o = obj - 0;break;
// case 'boolean' : o = obj;break;
// case 'object' :
// if(obj === null){
// o = null;
// }else{
// if(obj instanceof Array){
// o = [];
// for(var i = 0, len = obj.length; i < len; i++){
// o.push(clone(obj[i]));
// }
// }else{
// o = {};
// for(var k in obj){
// o[k] = clone(obj[k]);
// }
// }
// }
// break;
// default:
// o = obj;break;
// }
// return o;
// }
function clone(obj) { //
if (null == obj || "object" != typeof obj)
return obj;
var copy = obj.constructor(); //copy
for (var attr in obj) {
if (obj.hasOwnProperty(attr))
copy[attr] = obj[attr];//copy
}
return copy;
}
// fill the table
// create rows
var tr = d3.select("tbody")
.selectAll("tr")
.data(matrix);
tr.enter().append("tr");
// create cells
var td = tr.selectAll("td")
.data(function (d) {
return d;
}).attr("width", 80);
//var cellTd = td.enter().append("td");
//cellTd.on("click", function(d,i) {
//if(!d3.select("#"+d3.select(this).datum().id).node().checked )
//{
// d3.select(this).attr("class","highlight");
// d3.select("#"+d3.select(this).datum().id).property('checked', true);
//}
//else
//{
// d3.select(this).attr("class","normal");
// d3.select("#"+d3.select(this).datum().id).property('checked', false);
//}
//});
var cellTd = td.enter().append("td");
cellTd.attr("id",function (d, i) {
return "cellTd" + d.id;
}).on("click", function (d, i) {
if (!d3.select("#" + d3.select(this).datum().id).node().checked) {
d3.select(this).attr("class", "highlight");
d3.select("#" + d3.select(this).datum().id).property('checked', true);
dataSelect.push(d.id);
}
else {
d3.select(this).attr("class", "normal");
d3.select("#" + d3.select(this).datum().id).property('checked', false);
dataSelect = d3.set(dataSelect).values();
var index = dataSelect.indexOf(d.id);
if (index > -1) {
dataSelect.splice(index, 1);
}
}
});
function format_name(d) {
var name = '<b>';
name += d.genes.join("</b><br/><b>");
name += '</b><br/>';
return name;
}
{
var maxCount = d3.max(matrix, function (array) {
return d3.max(array, function (d, i) {
return d.sum;
});
});
if (maxCount > 0) {
cellTd.append("svg").attr("left", 0)
.attr("class", "cellCount")
.attr("height", 10)
.append("rect")
.attr("height", 10)
.attr("width",
function (d) {
if (d.sum !== undefined)
return d.sum / maxCount * 80;
else
return 0;
});
cellTd.on("mouseover", function (d, i) {
tooltip.html(function () {
var name = format_name(d);
return name;
});
return tooltip.transition()
.duration(50)
.style("opacity", 0.9);
})
.on("mousemove", function (d) {
return tooltip
.style("top", (d3.event.pageY - 10) + "px")
.style("left", (d3.event.pageX + 10) + "px");
})
.on("mouseout", function () {
tooltip.style("opacity", 0);
});
}
}
//var cellDiv = cellTd.append('div');
//cellDiv.append('input').attr("class","checkBox")
// .attr("id", function(d,i){
// return d.id;
// })
// .attr('type','checkbox');
var cellDiv = cellTd.append('div'); //table ??checkbox
cellDiv.append('input').attr("class", "checkBox")
.attr("id", function (d, i) {
return d.id;
})
.attr('type', 'checkbox')
.on("click", function (d, i) {
if (!this.checked) {
d3.select("cellTd" + d.id).attr("class", "highlight");
this.checked = true;
dataSelect.push(d.id);
}
else {
d3.select("cellTd" + d.id).attr("class", "normal");
this.checked = false;
dataSelect = d3.set(dataSelect).values();
var index = dataSelect.indexOf(d.id);
if (index > -1) {
dataSelect.splice(index, 1);
}
}
});
cellDiv.attr("class", "Bcell")
.append("span")
.text(function (d) {
return d.keys;
});
// add hight by checking the array
dataSelect = d3.set(dataSelect).values();
for (var i = 0; i < dataSelect.length; i++) {
d3.select("#" + dataSelect[i]).property('checked', true);
d3.select("#cellTd" + dataSelect[i]).attr("class", "highlight");
}
}
function partData(data) {
var sData = {}; //0 genes 1 items
var genes = [];
var items = [];
sData.keys = [
d3.set(data.genes.map(function (d) {
if (d !== "")
return d;
return d;
})).values().sort(function (a, b) {
return ( a < b ? -1 : a > b ? 1 : 0);
}),
d3.set(data.Items.map(function (d) {
if (d !== "")
return d;
return d;
})).values().sort(function (a, b) {
return ( a < b ? -1 : a > b ? 1 : 0);
})
];
//initialize the relationship
//*************************************
sData.data = [
sData.keys[0].map(
function (d) {
return sData.keys[1].map(function (v) {
return 0;
});
}),
sData.keys[1].map(function (d) {
return sData.keys[0].map(function (v) {
return 0;
});
})
];
for (var i = 0; i < data.genes.length; ++i) {
var gene = data.genes[i];
var item = data.Items[i];
sData.data[0][sData.keys[0].indexOf(gene)][sData.keys[1].indexOf(item)] = 1; // The relationship from left to right
sData.data[1][sData.keys[1].indexOf(item)][sData.keys[0].indexOf(gene)] = 1; // The relationship from right to left
}
for (var i = 0; i < sData.keys[1].length; ++i) {
var obj = {};
obj.data = sData.data[1][i];
obj.genes = [];
for (var j = 0; j < obj.data.length; ++j) {
if (obj.data[j] !== 0)
obj.genes.push(sData.keys[0][j]);
}
obj.keys = sData.keys[1][i];
obj.id = "idItem" + i;
obj.sum = d3.sum(sData.data[1][i]);
items.push(obj);
}
return items;
}
}
</script>
<?php
$data_string=array();
#Webgivi api without usrID folder which have the full permission
if(preg_match("/[A-Za-z0-9\_]+/", $_GET['type']) == TRUE){
$type = test_input($_GET['type']);
}
#get the similar php function to process the data and filter out category
#1.categories
echo "</br><div id='main'>";
echo "<b>Analysis Result:</b></br>";
echo "<div>Suggestion: if your filtered data is larger than 1000 lines, please use concept map to visuallize!</div></br>";
echo '<div id="wait"><!-- loading animation --></div>';
echo "<div>";
echo "Sort: <select id='sortTable'><option value='alph'>alphabetical</option><option value='freq'>frequency</option></select> ";
echo "Frequency: <= <input id='input_freq' type='text' size=3 value='0' > <button id='select_freq'>Select</button> ";
echo "Selection: <button id='clear'>Clear</button> ";
echo "</br></br>";
echo "<button id='delete'>Delete</button> ";
echo "<button id='view'>View</button> ";
echo "<button id='download'>Download</button> ";
echo "<button id='conceptMap'>Concept_Map View</button> ";
if($type == 'entrez'||$type == 'gene-iterm'){
echo "<button id='cytoscape'>Cytoscape View</button> ";
}
echo "</br></br>";
echo '<div style="height: 700px; width: 1250px; overflow: scroll;">';
echo "<div id='d3table'></div>";
echo '</div>';
echo "</div>";
echo "</br></br></br>";
$date=new DateTime();
$d=$date->getTimestamp();
$ran=md5(uniqid());
$filename="/var/www/data/id_$d"."_$ran.txt";
//if we find filename is the same, try to assign a new file name untile it's different.
while(file_exists($filename)){
$ran=md5(uniqid());
$filename="/var/www/data/id_$d"."_$ran.txt";
}
$fileID = "id_$d"."_$ran";
$filePass="/var/www/data/id_$d"."_$ran";
#print out all ID and iterms
if($type=='entrez'){
$gene=implode(",", $_SESSION['missingID']);
$username="liang";
//This is password for you.
$password="AnalysisForLiang";
//$gene="650,651,652";
$result = file_get_contents("https://biotm.cis.udel.edu/udelafc/getGeneAnalysisResults.php?user=$username&pass=$password&entrezids=$gene");
$fh=fopen($filename,'w') or die ("cannot open this file");
fwrite($fh,$result);
fclose($fh);
//echo 'perl eGIFT_parser.pl'.' '.$filePass;
echo exec(escapeshellcmd('perl eGIFT_parser.pl'.' '.$filePass));
echo "</br>";
$outfile=$filePass.'_gene_iterm.txt';
$outfile_post = $fileID.'_gene_iterm';
?>
<script>
var fileName = <?php echo "'".$outfile_post."'"; ?>;
loadTable(fileName,true,'gene2iTerm');
</script>
<?php
}else{
$outfile=$filePass.'_custome.txt';
$outfile_post = $fileID.'_custome';
$list='';
$pathway2symbol = unserialize($_SESSION['symbol2pathway']);
foreach($pathway2symbol as $pair){
//echo $pair[0].'*'.$pair[1];
$list = $list.implode("\t", $pair)."\n";
//$list = $list.$line;
}
//echo $list;
$fh=fopen($outfile,'w') or die ("cannot open this file");
fwrite($fh,$list);
fclose($fh);
?>
<script>
var fileName = <?php echo "'".$outfile_post."'"; ?>;
var conceptName = <?php echo "'".$conceptName."'"; ?>;
loadTable(fileName,false,'gene2pathway');
</script>
<?php
}
?>