NetAnalyzer 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,145 @@
1
+ require 'gv'
2
+
3
+ #For javascrip plotting
4
+ require 'erb'
5
+ require 'base64'
6
+ require 'json'
7
+ require 'zlib'
8
+
9
+ TEMPLATES = File.join(File.dirname(__FILE__), 'templates')
10
+
11
+
12
+ class Net_plotter
13
+ def initialize(net_data, options = {})
14
+ @group_nodes = net_data[:group_nodes]
15
+ @reference_nodes = net_data[:reference_nodes]
16
+ @nodes = net_data[:nodes]
17
+ @edges = net_data[:edges]
18
+ @layers = net_data[:layers]
19
+
20
+ if options[:method] == 'graphviz'
21
+ plot_dot(options)
22
+ elsif options[:method] == 'cyt_app'
23
+ plot_cyt_app(options)
24
+ else
25
+ if options[:method] == 'elgrapho'
26
+ template = 'el_grapho'
27
+ elsif options[:method] == 'cytoscape'
28
+ template = 'cytoscape'
29
+ elsif options[:method] == 'sigma'
30
+ template = 'sigma'
31
+ end
32
+ renderered_template = ERB.new(File.open(File.join(TEMPLATES, template + '.erb')).read).result(binding)
33
+ File.open(options[:output_file] + '.html', 'w'){|f| f.puts renderered_template}
34
+ end
35
+ end
36
+
37
+ def plot_cyt_app(user_options = {})
38
+ options = {}
39
+ options = options.merge(user_options)
40
+
41
+ node_cyt_ids = {}
42
+ nodes = []
43
+ count = 0
44
+ group_nodes = {}
45
+ @group_nodes.each do |groupID, gNodes|
46
+ gNodes.each do |gNode|
47
+ group_nodes[gNode] = groupID
48
+ end
49
+ end
50
+ @nodes.each do |id, node|
51
+ cyt_app_add_node(nodes, count, node, group_nodes)
52
+ node_cyt_ids[id] = count.to_s
53
+ count += 1
54
+ end
55
+ edges = cyt_app_add_edges(node_cyt_ids, count)
56
+ cys_net = {
57
+ 'elements' => {
58
+ 'nodes' => nodes,
59
+ 'edges' => edges
60
+ }
61
+ }
62
+ File.open(options[:output_file]+ '.cyjs', 'w'){|f| f.print JSON.pretty_generate(cys_net)}
63
+ end
64
+
65
+ def cyt_app_add_node(nodes, count, node, group_nodes)
66
+ id = node.id
67
+ cyt_node = {
68
+ 'data' => {
69
+ 'id' => count.to_s,
70
+ 'name' => id
71
+ }
72
+ }
73
+ cyt_node['data']['type'] = node.type
74
+ if !@reference_nodes.empty?
75
+ ref = @reference_nodes.include?(id) ? 'y' : 'n'
76
+ cyt_node['data']['ref'] = ref
77
+ end
78
+ if !group_nodes.empty?
79
+ query = group_nodes[id]
80
+ cyt_node['data']['group'] = query if !query.nil?
81
+ end
82
+ nodes << cyt_node
83
+ end
84
+
85
+ def cyt_app_add_edges(node_ids, count)
86
+ edges = []
87
+ plotted_edges = {}
88
+ @edges.each do |nodeID, associatedIDs|
89
+ associatedIDs.each do |associatedID|
90
+ pair = [nodeID, associatedID].sort.join('_').to_sym
91
+ if !plotted_edges[pair]
92
+ edges << {
93
+ 'data' => {
94
+ 'id' => count.to_s,
95
+ 'source' => node_ids[nodeID],
96
+ 'target' => node_ids[associatedID],
97
+ "interaction" => "-",
98
+ "weight" => 1.0
99
+ }
100
+ }
101
+ count +=1
102
+ plotted_edges[pair] = true
103
+ end
104
+ end
105
+ end
106
+ return edges
107
+ end
108
+
109
+ def plot_dot(user_options = {}) # input keys: layout
110
+ options = {layout: "sfdp"}
111
+ options = options.merge(user_options)
112
+ graphviz_colors = %w[lightsteelblue1 lightyellow1 lightgray orchid2]
113
+ palette = {}
114
+ @layers.each do |layer|
115
+ palette[layer] = graphviz_colors.shift
116
+ end
117
+ graph = GV::Graph.open('g', type = :undirected)
118
+ plotted_edges = {}
119
+ @edges.each do |nodeID, associatedIDs|
120
+ associatedIDs.each do |associatedID|
121
+ pair = [nodeID, associatedID].sort.join('_').to_sym
122
+ if !plotted_edges[pair]
123
+ graph.edge 'e',
124
+ graph.node(nodeID, label: '', style: 'filled', fillcolor: palette[@nodes[nodeID].type]),
125
+ graph.node(associatedID, label: '', style: 'filled' , fillcolor: palette[@nodes[associatedID].type])
126
+ plotted_edges[pair] = true
127
+ end
128
+ end
129
+ end
130
+ @reference_nodes.each do |nodeID|
131
+ graph.node(nodeID, style: 'filled', fillcolor: 'firebrick1', label: '')
132
+ end
133
+ graphviz_border_colors = %w[blue darkorange red olivedrab4]
134
+ @group_nodes.each do |groupID, gNodes|
135
+ border_color = graphviz_border_colors.shift
136
+ gNodes.each do |nodeID|
137
+ graph.node(nodeID, color: border_color, penwidth: '10', label: '')
138
+ end
139
+ end
140
+ graph[:overlap] = false
141
+ STDERR.puts 'Save graph'
142
+ graph.save(options[:output_file] + '.png', format='png', layout=options[:layout])
143
+ end
144
+
145
+ end