rbbt-rest 1.7.19 → 1.7.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -49,7 +49,6 @@ rbbt.page.list = function(){
49
49
  var parts = rbbt.page.path_parts()
50
50
  var type = parts[1]
51
51
  var list = parts[3]
52
- console.log(list)
53
52
  return new EntityList({id: list, type: type})
54
53
  }
55
54
 
@@ -0,0 +1,195 @@
1
+ rbbt.plots.aes = {}
2
+
3
+ rbbt.plots.aes.map_aesthetic = function(aes, mapper, map_obj){
4
+ switch(mapper){
5
+ case false:
6
+ case undefined:
7
+ case null:
8
+ case 'direct':
9
+ return aes;
10
+ case 'gradient':
11
+ if (map_obj){
12
+ if (typeof aes == 'object')
13
+ aes = aes.map(function(value){ return map_obj[value] })
14
+ else
15
+ aes = map_obj[aes]
16
+ }
17
+ return get_gradient(aes, 'red', 'green')
18
+ case 'sign-gradient':
19
+ if (map_obj){
20
+ if (typeof aes == 'object')
21
+ aes = aes.map(function(value){ return map_obj[value] })
22
+ else
23
+ aes = map_obj[aes]
24
+ }
25
+ return get_sign_gradient(aes, 'red', 'grey', 'green')
26
+ case 'map':
27
+ if (typeof aes == 'object')
28
+ return aes.map(function(value){ return map_obj[value.toString()] })
29
+ else
30
+ return map_obj[aes]
31
+ case 'function':
32
+ var res = []
33
+ if (typeof aes == 'object')
34
+ for (i in aes) res.push(map_obj(aes[i]))
35
+ else
36
+ res = map_obj(aes)
37
+ return res
38
+ case 'list_function':
39
+ return map_obj(aes)
40
+ }
41
+ }
42
+ rbbt.plots.aes.get_properties = function(list, rules){
43
+ if (undefined === rules) rules == []
44
+ if (undefined === list.properties) list.properties = {}
45
+ var promises = []
46
+ forArray(rules, function(rule){
47
+ var name = rule.name
48
+ var extract = rule.extract
49
+ if (typeof extract == 'string'){
50
+ eval('extract='+extract)
51
+ }
52
+
53
+ if (rule.entity_type && rule.entity_type != list.type) return
54
+
55
+ if (rule.info){
56
+ var property = rule.property
57
+ var deferred = m.deferred()
58
+ var entry = rule.info
59
+ if (undefined === name) name = entry
60
+
61
+ var value = list.info[entry]
62
+ if (undefined === value && entry == 'type') value = list.type
63
+ if (undefined === value && entry == 'code') value = list.codes
64
+
65
+ deferred.resolve(value)
66
+ promise = deferred.promise
67
+ }
68
+
69
+ if (rule.value){
70
+ var property = rule.property
71
+ var deferred = m.deferred()
72
+ deferred.resolve(rule.value)
73
+ promise = deferred.promise
74
+ }
75
+
76
+ if (rule.property){
77
+ var property = rule.property
78
+ var args = rule.args
79
+ if (undefined === name) name = property
80
+ promise = rbbt.entity_array.property(list.codes, list.type, list.info, property, args)
81
+ if (extract){ promise = promise.then(function(res){ return res.map(extract)}) }
82
+ }
83
+
84
+ if (rule.workflow){
85
+ var workflow = rule.workflow
86
+ var task = rule.task
87
+ var args = rule.args
88
+ if (undefined === name) name = task
89
+ promise = rbbt.job(workflow, task, args, true)
90
+ if (extract){ promise = promise.then(function(res){ return list.codes.map(function(code){return extract.call(null, res, code)}) } )}
91
+ }
92
+
93
+ if (rule.parents || rule.children){
94
+ var kb_type
95
+ var database
96
+ if (rule.parents){
97
+ kb_type = 'parent'
98
+ var database = rule.parents
99
+ if (undefined === name) name = database
100
+
101
+ promise = rbbt.entity_array.parents(list.codes, list.type, database)
102
+ }else{
103
+ kb_type = 'children'
104
+ var database = rule.children
105
+ if (undefined === name) name = database
106
+
107
+ promise = rbbt.entity_array.children(list.codes, list.type, database)
108
+ }
109
+
110
+ if (rule.field){
111
+ promise = m.sync([promise,rbbt.knowledge_base.database_info(database)]).then(function(res){
112
+ var data = res[0]
113
+ var db_info = res[1]
114
+ var fields = db_info.fields
115
+ var field_pos = fields.indexOf(rule.field)
116
+
117
+ if (field_pos < 0){
118
+ var msg = "Field not found: " + rule.field + ". Options: " + fields.join(", ")
119
+ throw new Error(msg)
120
+ }
121
+
122
+ var matches = {}
123
+ forHash(data, function(key, values){
124
+ var source = values[0]
125
+ var target = values[1]
126
+ if (matches[source] === undefined) matches[source] = {}
127
+ matches[source][target] = values[field_pos+2]
128
+ })
129
+ return matches
130
+ })
131
+ }else{
132
+ promise = promise.then(function(data){
133
+ var matches = {}
134
+ forHash(data, function(key, values){
135
+ var source,target
136
+ if (rule.parents){
137
+ source = values[1]
138
+ target = values[0]
139
+ }else{
140
+ source = values[0]
141
+ target = values[1]
142
+ }
143
+ if (matches[source] === undefined) matches[source] = {}
144
+ matches[source][target] = values.slice(2)
145
+ })
146
+ return matches
147
+ })
148
+ }
149
+
150
+ if (extract){
151
+ promise = promise.then(function(matches){
152
+ return list.codes.map(function(e){
153
+ return extract.call(null, matches, e)
154
+ })
155
+ })
156
+ }
157
+ else{
158
+ promise = promise.then(function(matches){
159
+ return list.codes.map(function(e){ return matches[e] })
160
+ })
161
+ }
162
+ }
163
+
164
+ promises.push(promise.then(function(res){list.properties[name] = res}))
165
+ })
166
+
167
+ return m.sync(promises)
168
+ }
169
+
170
+ rbbt.plots.aes.set_aes = function(list, aes_rules){
171
+ if (undefined === list.aes) list.aes = {}
172
+ forArray(aes_rules, function(rule){
173
+
174
+ if (rule.entity_type && rule.entity_type != list.type) return
175
+
176
+
177
+ var name = rule.name
178
+ var aes = rule.aes
179
+ var mapper = rule.mapper
180
+ var mapper_obj = rule.mapper_obj
181
+ var property = list.properties[name]
182
+
183
+ if (rule.value){
184
+ list.aes[aes] = rule.value
185
+ return
186
+ }
187
+
188
+ if (undefined === property && list.info[name]) property = list.info[name]
189
+ if (undefined === property && name == 'type') property = list.type
190
+ if (undefined === property && name == 'code') property = list.codes
191
+ if (undefined === property) return
192
+
193
+ list.aes[aes] = rbbt.plots.aes.map_aesthetic(property, mapper, mapper_obj)
194
+ })
195
+ }
@@ -0,0 +1,2 @@
1
+ rbbt.plots = {}
2
+
@@ -0,0 +1,216 @@
1
+ rbbt.plots.graph.build_d3 = function(graph_model){
2
+
3
+ var model = rbbt.plots.graph.consolidate(graph_model)
4
+
5
+ var node_index = {}
6
+ for (i=0; i< model.nodes.length; i++) node_index[model.nodes[i].code] = i
7
+
8
+ for (i=0; i< model.edges.length; i++){
9
+ var edge = model.edges[i]
10
+ edge.source = node_index[edge.source]
11
+ edge.target = node_index[edge.target]
12
+ }
13
+
14
+ model.links = model.edges
15
+ model.edges = undefined
16
+ model.node_index = node_index
17
+
18
+ return model
19
+ }
20
+
21
+ rbbt.plots.graph.build_cytoscape = function(graph_model){
22
+ var model = rbbt.plots.graph.consolidate(graph_model)
23
+
24
+ var dataSchema = {nodes: [], edges:[]}
25
+
26
+ var node_vars = {}
27
+ for (i in model.nodes){
28
+ for (p in model.nodes[i]){
29
+ if (undefined !== model.nodes[i][p])
30
+ node_vars[p] = typeof model.nodes[i][p]
31
+ }
32
+ }
33
+ for (p in node_vars){
34
+ dataSchema.nodes.push({name: p, type: node_vars[p]})
35
+ }
36
+
37
+ node_vars = {}
38
+ for (i in model.edges){
39
+ for (p in model.edges[i]){
40
+ if (undefined !== model.edges[i][p])
41
+ node_vars[p] = typeof model.edges[i][p]
42
+ }
43
+ }
44
+ for (p in node_vars){
45
+ dataSchema.edges.push({name: p, type: node_vars[p]})
46
+ }
47
+
48
+ var cy_model = {}
49
+ cy_model.dataSchema = dataSchema
50
+ cy_model.data = model
51
+
52
+ return cy_model
53
+ }
54
+
55
+ rbbt.plots.graph.build_cytoscapejs = function(graph_model){
56
+ var model = rbbt.plots.graph.consolidate(graph_model)
57
+
58
+ var nodes = []
59
+ forArray(model.nodes, function(node){
60
+ var clean = clean_hash(node)
61
+ if (undefined === clean.id) clean.id = clean.code
62
+ nodes.push({data: clean})
63
+ })
64
+
65
+ var edges = []
66
+ forArray(model.edges, function(edge){
67
+ var clean = clean_hash(edge)
68
+ if (undefined === clean.id) clean.id = clean.code
69
+ edges.push({data: clean})
70
+ })
71
+
72
+ var cy_model = {}
73
+ cy_model.elements = {nodes: nodes, edges: edges}
74
+
75
+ return cy_model
76
+ }
77
+
78
+ rbbt.plots.graph.view_cytoscapejs = function(graph_model, elem, style, layout, extra){
79
+
80
+ var default_style = [ // the stylesheet for the graph
81
+ {
82
+ selector: 'node',
83
+ style: { 'background-color': 'blue', 'label': 'data(id)' }
84
+ },
85
+
86
+ {
87
+ selector: 'node[label]',
88
+ style: {'label': 'data(label)' }
89
+ },
90
+
91
+ {
92
+ selector: 'node[color]',
93
+ style: { 'background-color': 'data(color)' }
94
+ },
95
+
96
+ {
97
+ selector: 'edge',
98
+ style: { 'width': 1, 'line-color': 'grey', 'target-arrow-color': '#ccc', 'target-arrow-shape': 'triangle' }
99
+ },
100
+ {
101
+ selector: 'edge[color]',
102
+ style: { 'line-color': 'data(color)'}
103
+ }
104
+ ]
105
+
106
+ var default_layout = { name: 'cose' }
107
+
108
+ if (undefined === style) style = default_style
109
+ if (undefined === layout) layout = default_layout
110
+
111
+ var deferred = m.deferred()
112
+
113
+ console.log(graph_model)
114
+ rbbt.plots.graph.update(graph_model).then(function(updated_model){
115
+ var cy_model = rbbt.plots.graph.build_cytoscapejs(updated_model)
116
+
117
+ require_js(['/plugins/cytoscapejs/cytoscape.js'], function(){
118
+ var cy_params = {
119
+ container: elem,
120
+ elements: cy_model.elements,
121
+ style: style,
122
+ layout: layout,
123
+ }
124
+
125
+ if (undefined !== extra) forHash(extra,function(k,v){ cy_params[k,v] })
126
+
127
+ var cy = cytoscape(cy_params)
128
+
129
+ deferred.resolve(cy)
130
+ })
131
+ },rbbt.exception.report)
132
+
133
+ return deferred.promise
134
+ }
135
+
136
+ rbbt.plots.graph.view_cytoscape = function(graph_model, elem, style, layout, extra){
137
+ rbbt.plots.graph.update(graph_model).then(function(updated_model){
138
+ var dataset = rbbt.plots.graph.build_cytoscape(updated_model)
139
+
140
+ require_js(['/js/cytoscape/js/src/AC_OETags.js', '/js/cytoscape/js/src/cytoscapeweb.js', '/js/cytoscape'], function(){
141
+ var tool = $('#plot').cytoscape_tool({
142
+ knowledge_base: 'user',
143
+ namespace: 'Hsa/feb2014',
144
+ entities: dataset.nodes,
145
+ network: dataset,
146
+ aesthetics: {},
147
+
148
+ node_click: function(event){
149
+ var target = event.target;
150
+
151
+ for (var i in target.data){
152
+ var variable_name = i;
153
+ var variable_value = target.data[i];
154
+ }
155
+
156
+ for (var i in target.data) {
157
+ var variable_name = i;
158
+ var variable_value = target.data[i];
159
+ }
160
+
161
+ var url = target.data.url;
162
+
163
+ rbbt.modal.controller.show_url(url)
164
+ return(false)
165
+ },
166
+
167
+ edge_click: function(event){
168
+ var target = event.target;
169
+ for (var i in target.data){
170
+ var variable_name = i;
171
+ var variable_value = target.data[i];
172
+ }
173
+
174
+ var pair = [target.data.source, target.data.target].join("~")
175
+ tool.cytoscape_tool('show_info', "user", target.data.database, pair);
176
+
177
+ return(false)
178
+ }
179
+
180
+ });
181
+
182
+ require_js('/js/controls/context_menu', function(){
183
+ cytoscape_context_menu(tool)
184
+ })
185
+
186
+ require_js('/js/controls/placement', function(){
187
+ cytoscape_placement(tool)
188
+ })
189
+
190
+ require_js('/js/controls/save', function(){
191
+ cytoscape_save(tool)
192
+ })
193
+ tool.cytoscape_tool('draw');
194
+ })
195
+ })
196
+ }
197
+
198
+ rbbt.plots.graph.view_d3js_graph = function(graph_model, elem, node_obj){
199
+ rbbt.plots.graph.update(graph_model).then(function(updated_model){
200
+ console.log(updated_model)
201
+ var dataset = rbbt.plots.graph.build_d3(updated_model)
202
+
203
+ if (undefined === node_obj){
204
+ node_obj = function(node){
205
+ var g = node.append('g').attr('class', function(d){ if(undefined === d.shape) d.shape = 'circle'; return "node " + d.shape})
206
+ d3.selectAll('.node.circle').append('circle').attr('fill',function(d){return d.color}).attr('r', 20)
207
+ d3.selectAll('.node.rect').append('rect').attr('fill',function(d){return d.color}).attr('width', 40).attr('height', 40).attr('x', -20).attr('y', -20)
208
+ g.append('text').attr('x',-20).attr('y',-20).text(function(d){if(undefined === d.label) d.label = d.code; return d.label}).fill('black')
209
+ return g
210
+ }
211
+ }
212
+
213
+ rbbt.plots.d3js_graph(dataset, elem, node_obj)
214
+
215
+ })
216
+ }
@@ -0,0 +1,138 @@
1
+ rbbt.plots.graph = {}
2
+
3
+ rbbt.plots.graph.get_entities = function(graph_model){
4
+
5
+ if (undefined !== graph_model.entities) return graph_model
6
+
7
+ var entities = {}
8
+ forHash(graph_model.associations, function(database, list){
9
+ var source_type = list.source_type
10
+ var source_entities = {}
11
+ source_entities.codes = unique(list.info.source)
12
+ source_entities.info = list.source_info
13
+ source_entities.type = list.source_type
14
+
15
+ var target_type = list.target_type
16
+ var target_entities = {}
17
+ target_entities.codes = unique(list.info.target)
18
+ target_entities.info = list.target_info
19
+ target_entities.type = list.target_type
20
+
21
+ if (entities[source_entities.type])
22
+ entities[source_entities.type].codes = unique(entities[source_entities.type].codes.concat(source_entities.codes))
23
+ else
24
+ entities[source_entities.type] = source_entities
25
+
26
+ if (entities[target_entities.type])
27
+ entities[target_entities.type].codes = unique(entities[target_entities.type].codes.concat(target_entities.codes))
28
+ else
29
+ entities[target_entities.type] = target_entities
30
+ })
31
+
32
+ graph_model.entities = entities
33
+
34
+ return graph_model
35
+ }
36
+
37
+ rbbt.plots.graph.update_aes = function(graph_model){
38
+ var data_promises = []
39
+ forHash(graph_model.entities, function(type, list){
40
+ data_promises.push(
41
+
42
+ rbbt.plots.aes.get_properties(list, graph_model.rules).
43
+ then(function(){
44
+ rbbt.plots.aes.set_aes(list, graph_model.aes_rules)
45
+ })
46
+
47
+ )
48
+ })
49
+
50
+ forHash(graph_model.associations, function(db, list){
51
+ data_promises.push(
52
+
53
+ rbbt.plots.aes.get_properties(list, graph_model.edge_rules).
54
+ then(function(){
55
+ rbbt.plots.aes.set_aes(list, graph_model.edge_aes_rules)
56
+ })
57
+
58
+ )
59
+ })
60
+ return m.sync(data_promises).then(function(){return graph_model})
61
+ }
62
+
63
+ //{{{ CONSOLIDATION
64
+
65
+ rbbt.plots.graph.consolidate_list = function(list){
66
+ var nodes = []
67
+
68
+ var codes = list.codes
69
+ var info = list.info
70
+ var aes = list.aes
71
+ var properties = list.properties
72
+
73
+ for (i in codes){
74
+ var node = {}
75
+ node.code = codes[i]
76
+ node.id = codes[i]
77
+ forHash(aes, function(name, values){
78
+ var value
79
+ if (typeof values == 'object') value = values[i]
80
+ else value = values
81
+ node[name] = value
82
+ })
83
+ nodes.push(node)
84
+ }
85
+
86
+ return nodes
87
+ }
88
+
89
+ rbbt.plots.graph.consolidate_associations = function(list){
90
+ var nodes = []
91
+
92
+ var codes = list.codes
93
+ var info = list.info
94
+ var aes = list.aes
95
+ var properties = list.properties
96
+ var values = list.properties
97
+
98
+ for (i in codes){
99
+ var node = {}
100
+ node.code = codes[i]
101
+ forHash(aes, function(name, values){
102
+ var value
103
+ if (typeof values == 'object') value = values[i]
104
+ else value = values
105
+ node[name] = value
106
+ })
107
+ nodes.push(node)
108
+ }
109
+
110
+ return nodes
111
+ }
112
+
113
+ rbbt.plots.graph.consolidate = function(graph_model){
114
+ var model = {}
115
+
116
+ var nodes = []
117
+
118
+ forHash(graph_model.entities, function(type, list){
119
+ var list_nodes = rbbt.plots.graph.consolidate_list(list)
120
+ for (i in list_nodes) nodes.push(list_nodes[i])
121
+ })
122
+
123
+ var edges = []
124
+ forHash(graph_model.associations, function(database, list){
125
+ var list_edges = rbbt.plots.graph.consolidate_associations(list,database)
126
+ for (i in list_edges) edges.push(list_edges[i])
127
+ })
128
+
129
+ model.nodes = nodes
130
+ model.edges = edges
131
+
132
+ return model
133
+ }
134
+
135
+ rbbt.plots.graph.update = function(graph_model){
136
+ graph_model = rbbt.plots.graph.get_entities(graph_model)
137
+ return rbbt.plots.graph.update_aes(graph_model)
138
+ }