Class: Net_plotter

Inherits:
Object
  • Object
show all
Defined in:
lib/NetAnalyzer/net_plotter.rb

Instance Method Summary collapse

Constructor Details

#initialize(net_data, options = {}) ⇒ Net_plotter

Returns a new instance of Net_plotter.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/NetAnalyzer/net_plotter.rb', line 13

def initialize(net_data, options = {})
	@group_nodes = net_data[:group_nodes]
	@reference_nodes = net_data[:reference_nodes]
	@nodes = net_data[:nodes]
	@edges = net_data[:edges]
	@layers = net_data[:layers]

	if options[:method] == 'graphviz'
		plot_dot(options)
	elsif options[:method] == 'cyt_app'
		plot_cyt_app(options)
	else
		if options[:method] == 'elgrapho'
			template = 'el_grapho'
		elsif options[:method] == 'cytoscape'
			template = 'cytoscape'
		elsif options[:method] == 'sigma'
			template = 'sigma'
		end
		renderered_template = ERB.new(File.open(File.join(TEMPLATES, template + '.erb')).read).result(binding)
		File.open(options[:output_file] + '.html', 'w'){|f| f.puts renderered_template}
	end	
end

Instance Method Details

#cyt_app_add_edges(node_ids, count) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/NetAnalyzer/net_plotter.rb', line 85

def cyt_app_add_edges(node_ids, count)
	edges = []
	plotted_edges = {}
	@edges.each do |nodeID, associatedIDs|
		associatedIDs.each do |associatedID|
			pair = [nodeID, associatedID].sort.join('_').to_sym
			if !plotted_edges[pair]
				edges << {
					'data' => {
						'id' => count.to_s,
						'source' => node_ids[nodeID],
						'target' => node_ids[associatedID],
						"interaction" => "-",
						"weight" => 1.0
					}
				}
				count +=1
				plotted_edges[pair] = true
			end
		end
	end
	return edges
end

#cyt_app_add_node(nodes, count, node, group_nodes) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/NetAnalyzer/net_plotter.rb', line 65

def cyt_app_add_node(nodes, count, node, group_nodes)
	id = node.id
	cyt_node = {
		'data' => {
			'id' => count.to_s,
			'name' => id
		}
	}
	cyt_node['data']['type'] = node.type
	if !@reference_nodes.empty?
		ref = @reference_nodes.include?(id) ? 'y' : 'n'
		cyt_node['data']['ref'] = ref
	end
	if !group_nodes.empty?
		query = group_nodes[id]
		cyt_node['data']['group'] = query if !query.nil?			
	end
	nodes << cyt_node
end

#plot_cyt_app(user_options = {}) ⇒ Object



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
# File 'lib/NetAnalyzer/net_plotter.rb', line 37

def plot_cyt_app(user_options = {})
	options = {}
	options = options.merge(user_options)

	node_cyt_ids = {}
	nodes = []
	count = 0
	group_nodes = {}
	@group_nodes.each do |groupID, gNodes|
		gNodes.each do |gNode|
			group_nodes[gNode] = groupID
		end
	end
	@nodes.each do |id, node|
		cyt_app_add_node(nodes, count, node, group_nodes)
		node_cyt_ids[id] = count.to_s
		count += 1
	end
	edges = cyt_app_add_edges(node_cyt_ids, count)
	cys_net = {
		'elements' => {
			'nodes' => nodes,
			'edges' => edges
		}
	}
	File.open(options[:output_file]+ '.cyjs', 'w'){|f| f.print JSON.pretty_generate(cys_net)}
end

#plot_dot(user_options = {}) ⇒ Object

input keys: layout



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
# File 'lib/NetAnalyzer/net_plotter.rb', line 109

def plot_dot(user_options = {}) # input keys: layout
	options = {layout: "sfdp"}
	options = options.merge(user_options)
	graphviz_colors = %w[lightsteelblue1 lightyellow1 lightgray orchid2]
	palette = {}
	@layers.each do |layer|
		palette[layer] = graphviz_colors.shift
	end
	graph = GV::Graph.open('g', type = :undirected)
	plotted_edges = {}
	@edges.each do |nodeID, associatedIDs|
		associatedIDs.each do |associatedID|
			pair = [nodeID, associatedID].sort.join('_').to_sym
			if !plotted_edges[pair]
				graph.edge 'e', 
					graph.node(nodeID, label: '', style: 'filled', fillcolor: palette[@nodes[nodeID].type]), 
					graph.node(associatedID, label: '', style: 'filled' , fillcolor: palette[@nodes[associatedID].type])
				plotted_edges[pair] = true
			end
		end
	end
	@reference_nodes.each do |nodeID|
		graph.node(nodeID, style: 'filled', fillcolor: 'firebrick1', label: '')
	end
	graphviz_border_colors = %w[blue darkorange red olivedrab4]
	@group_nodes.each do |groupID, gNodes|
		border_color = graphviz_border_colors.shift
		gNodes.each do |nodeID|
				graph.node(nodeID, color: border_color, penwidth: '10', label: '')
		end
	end
	graph[:overlap] = false
	STDERR.puts 'Save graph'
	graph.save(options[:output_file] + '.png', format='png', layout=options[:layout])
end