In [1]:
require 'nyaplot'
require 'bionya'
Out[1]:
Out[1]:
Out[1]:
In [2]:
arr = []
10.times do |i|
arr.push({group: 'group' + i.to_s ,df: Nyaplot::DataFrame.new({axis: ['a', 'b', 'c'], val: [2, 3, 4]})})
end
df = Nyaplot::DataFrame.new(arr)
Out[2]:
In [3]:
plot = Nyaplot::CircularPlot.new(df, :group, :df)
plot.to_json
Out[3]:
In [4]:
plot.add(1, :arc, :axis, :val)
Out[4]:
In [5]:
plot.show
Out[5]:
In [6]:
df = Nyaplot::DataFrame.from_csv('./data/circular/category.csv')
Out[6]:
In [7]:
df2 = Nyaplot::DataFrame.from_csv('./data/circular/hgmd.tsv', {col_sep: "\t"})
Out[7]:
In [8]:
df3 = Nyaplot::DataFrame.from_csv('./data/circular/genes_hgmd.tsv', {col_sep: "\t"})
Out[8]:
In [9]:
hash2 = {}
df3.each_row do |row|
chr_name = "chr" + row[:gene_name].match(/hs(.+)/)[1]
hash2[chr_name] ||= []
hash2[chr_name].push({locale: row[:start], name: row[:name]})
end
""
Out[9]:
In [10]:
hash = {}
df2.each_row do |row|
chr_name = "chr" + row[:gene_name].match(/hs(.+)/)[1]
hash[chr_name] ||= []
hash[chr_name].push({start: row[:start], val: row[:num2]})
end
""
Out[10]:
In [11]:
bin_size = df.size.max/50
chr_name = df.column(:name).to_a
nested = df.column(:size).to_a.map.with_index do |size, i|
vals = hash[chr_name[i]]
names = hash2[chr_name[i]]
vals = [] if vals.nil?
names = [] if names.nil?
raw = Array.new(size/bin_size, 0).map.with_index {|val, i|
val = vals.reduce(0){|memo, v| next memo + v[:val] if v[:start] > i*bin_size && v[:start] < (i+1)*bin_size; memo}
name = names.select {|name| name[:locale] > i*bin_size && name[:locale] < (i+1)*bin_size}
{axis: i*bin_size, val: val, name: (name.length==0 ? '' : name[0][:name])}
}
Nyaplot::DataFrame.new(raw)
end
df.df = nested
df
Out[11]:
In [12]:
df.name = df.column(:name).to_a.map{|name| name.match(/chr(.+)/)[1]}
df
Out[12]:
In [13]:
color = Nyaplot::Colors.qual
Out[13]:
In [14]:
plot2 = Nyaplot::CircularPlot.new(df, :name, :df)
arc = plot2.add(1, :arc, :axis, :val)
arc.color(["rgb(56,108,176)"])
labels = plot2.add(2, :labels, :axis, :name)
labels.text_size("0.3em")
plot2.color(color)
plot2.text_size("0.5em")
plot2.text_color("#000")
plot2.show
Out[14]:
In [15]:
from=[]; to=[]
df.each_row do |row|
chr_name = row[:name]
axis = row[:df].axis.to_a
2.times do |i|
num = axis.sample
from.push(chr_name + '.' + num.to_s)
num = axis.sample
to.push(chr_name + '.' + num.to_s)
end
end
connector_df = Nyaplot::DataFrame.new({from: from.shuffle,to: to.shuffle})
connector_df
Out[15]:
In [16]:
plot2.add_connector_with_df(connector_df, :from, :to)
plot2.show
Out[16]: