forked from Chuqiaoo/foamtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetData.js
161 lines (113 loc) · 4.36 KB
/
getData.js
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
/**
* Created by Chuqiao on 28/02/19.
*/
function getData(data) {
// json object container
var groups = [];
// save data as key => value pair, use dbId as key for per pairs
var humanDataStId = {};
$.each(data.nodes, function (key, val) {
humanDataStId[val.dbId] = val.stId;
});
var humanDataName = {};
$.each(data.nodes, function (key, val) {
humanDataName[val.dbId] = val.name;
});
var humanDataRatio = {};
$.each(data.nodes, function (key, val) {
if (val.ratio != 0) {
humanDataRatio[val.dbId] = val.ratio * 1000;
} else {
humanDataRatio[val.dbId] = (val.ratio + 0.009) * 1000;
}
});
var humanDataUrl = {};
$.each(data.nodes, function (key, val) {
humanDataUrl[val.dbId] = "https://dev.reactome.org/PathwayBrowser/#/" + val.stId;
});
//get all nested children
function getNestedChildren(arr, parentId) {
// create a new object to store the result
var out = [];
// for each item in a
for (var i = 0; i < arr.length; i++) {
// find all children of parentId
if (arr[i].from == parentId) {
// recursively find children for each children of parentId
var grandGroups = getNestedChildren(arr, arr[i].to);
if (grandGroups.length) {
arr[i].groups = grandGroups
}
out.push(arr[i]);
}
}
return out
}
// add all Nested children to level 1 parent
function addAllNestedToParent(arr, parentID) {
var allNestedChild = getNestedChildren(arr, parentID);
var parent = {to: parentID, groups: []};
parent.groups = Object.assign(parent.groups, allNestedChild);
return parent
}
getFullLeveldbId();
function getFullLeveldbId() {
$.ajax({
//warning here
async: false,
//human sapiens top data
url: "https://dev.reactome.org/ContentService/data/pathways/top/9606",
dataType: "json",
success: function (topData) {
topData.forEach(function (item) {
var each = addAllNestedToParent(data.edges, item.dbId);
groups.push(each);
});
}
});
return groups
};
addValue(groups);
function addValue(groups) {
for (var k = 0; k < groups.length; k++) {
if (humanDataStId[groups[k].to]) {
groups[k] = Object.assign(groups[k], {'stId': humanDataStId[groups[k].to]});
}
if (humanDataName[groups[k].to]) {
groups[k] = Object.assign(groups[k], {'label': humanDataName[groups[k].to]});
}
if (humanDataRatio[groups[k].to]) {
groups[k] = Object.assign(groups[k], {'weight': humanDataRatio[groups[k].to]});
}
if (humanDataUrl[groups[k].to]) {
groups[k] = Object.assign(groups[k], {'url': humanDataUrl[groups[k].to]});
}
delete groups[k].to;
}
}
groups.forEach(addValueToChildren);
function addValueToChildren(group) {
if (group.groups && group.groups.length >= 0) {
group.groups.forEach(addValueToChildren);
for (var i = 0; i < group.groups.length; i++) {
// delete form (parent key) in arrary
delete group.groups[i].from;
if (humanDataStId[group.groups[i].to]) {
group.groups[i] = Object.assign(group.groups[i], {'stId': humanDataStId[group.groups[i].to]});
}
if (humanDataName[group.groups[i].to]) {
group.groups[i] = Object.assign(group.groups[i], {'label': humanDataName[group.groups[i].to]});
}
if (humanDataRatio[group.groups[i].to]) {
group.groups[i] = Object.assign(group.groups[i], {'weight': humanDataRatio[group.groups[i].to]});
}
if (humanDataUrl[group.groups[i].to]) {
group.groups[i] = Object.assign(group.groups[i], {'url': humanDataUrl[group.groups[i].to]});
}
// delete to (dbId) in arrary
delete group.groups[i].to;
}
}
}
return groups
};