In [1]:
require 'nyaplot'
require 'bionya'
Out[1]:
Out[1]:
Out[1]:
true
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]:
| group | df | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| group0 |
| ||||||||
| group1 |
| ||||||||
| group2 |
| ||||||||
| group3 |
| ||||||||
| group4 |
| ||||||||
| group5 |
| ||||||||
| group6 |
| ||||||||
| group7 |
| ||||||||
| group8 |
| ||||||||
| group9 |
|
In [3]:
plot = Nyaplot::CircularPlot.new(df, :group, :df)
plot.to_json
Out[3]:
"{\"diagrams\":[],\"options\":{\"zoom\":true,\"width\":800,\"height\":800,\"axis_extra_options\":{\"group_by\":\"group\",\"color\":[\"#253494\"],\"inner_num\":0,\"outer_num\":1,\"df_id\":\"3b9f7deb-7b35-43fc-b92e-05716f486799\"}},\"axis_extra_options\":{\"group_by\":\"group\",\"color\":[\"#253494\"],\"inner_num\":0,\"outer_num\":1,\"df_id\":\"3b9f7deb-7b35-43fc-b92e-05716f486799\"},\"extension\":\"Bionya\"}"
In [4]:
plot.add(1, :arc, :axis, :val)
Out[4]:
#<Nyaplot::Diagram:0xb7ff154c @properties={:type=>:arc, :options=>{:x=>:axis, :y=>:val, :range=>[0, 4], :layer=>1}, :data=>"3b9f7deb-7b35-43fc-b92e-05716f486799"}>
In [5]:
plot.show
Out[5]:
In [6]:
df = Nyaplot::DataFrame.from_csv('./data/circular/category.csv')
Out[6]:
| name | size |
|---|---|
| chr1 | 249250621 |
| chr2 | 243199373 |
| chr3 | 198022430 |
| chr4 | 191154276 |
| chr5 | 180915260 |
| chr6 | 171115067 |
| chr7 | 159138663 |
| chr8 | 146364022 |
| chr9 | 141213431 |
| chr10 | 135534747 |
| chr11 | 135006516 |
| chr12 | 133851895 |
| chr13 | 115169878 |
| chr14 | 107349540 |
| chr15 | 102531392 |
| chr16 | 90354753 |
| ... | ... |
| chry | 59373566 |
In [7]:
df2 = Nyaplot::DataFrame.from_csv('./data/circular/hgmd.tsv', {col_sep: "\t"})
Out[7]:
| gene_name | start | end | num1 | num2 |
|---|---|---|---|---|
| hs1 | 182554557 | 187554557 | 0.0 | 12.0 |
| hs1 | 223284528 | 228284528 | 0.0 | 15.0 |
| hs1 | 43804340 | 48804340 | 0.0 | 1.0 |
| hs1 | 20972111 | 25972111 | 0.0 | 2.0 |
| hs1 | 1650845 | 6650845 | 0.0 | 21.0 |
| hs1 | 223284444 | 228284444 | 0.0 | 1.0 |
| hs1 | 12252955 | 17252955 | 0.0 | 10.0 |
| hs1 | 45797505 | 50797505 | 0.0 | 3.0 |
| hs1 | 226555302 | 231555302 | 0.0 | 9.0 |
| hs1 | 20977000 | 25977000 | 0.0 | 14.0 |
| hs1 | 182551337 | 187551337 | 0.0 | 17.0 |
| hs1 | 8030994 | 13030994 | 0.0 | 1.0 |
| hs1 | 55638075 | 60638075 | 0.0 | 20.0 |
| hs1 | 223285200 | 228285200 | 0.0 | 3.0 |
| hs1 | 223284599 | 228284599 | 0.0 | 7.0 |
| hs10 | 72195439 | 77195439 | 0.0 | 10.0 |
| ... | ... | ... | ... | ... |
| hsX | 153284192 | 158284192 | 0.0 | 12.0 |
In [8]:
df3 = Nyaplot::DataFrame.from_csv('./data/circular/genes_hgmd.tsv', {col_sep: "\t"})
Out[8]:
| gene_name | start | end | name |
|---|---|---|---|
| hs1 | 182554557 | 182555057 | RNASEL |
| hs1 | 223284528 | 223285028 | TLR5 |
| hs1 | 43804340 | 43804840 | MPL |
| hs1 | 20972111 | 20972611 | PINK1 |
| hs1 | 1650845 | 1651345 | CDK11A |
| hs1 | 223284444 | 223284944 | TLR5 |
| hs1 | 12252955 | 12253455 | TNFRSF1B |
| hs1 | 45797505 | 45798005 | MUTYH |
| hs1 | 226555302 | 226555802 | PARP1 |
| hs1 | 20977000 | 20977500 | PINK1 |
| hs1 | 182551337 | 182551837 | RNASEL |
| hs1 | 8030994 | 8031494 | PARK7 |
| hs1 | 55638075 | 55638575 | USP24 |
| hs1 | 223285200 | 223285700 | TLR5 |
| hs1 | 223284599 | 223285099 | TLR5 |
| hs10 | 72195439 | 72195939 | NODAL |
| ... | ... | ... | ... |
| hsX | 153284192 | 153284692 | IRAK1 |
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]:
| name | size | df | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| chr1 | 249250621 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr2 | 243199373 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr3 | 198022430 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr4 | 191154276 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr5 | 180915260 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr6 | 171115067 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr7 | 159138663 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr8 | 146364022 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr9 | 141213431 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr10 | 135534747 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr11 | 135006516 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr12 | 133851895 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr13 | 115169878 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr14 | 107349540 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr15 | 102531392 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chr16 | 90354753 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ... | ... | ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chry | 59373566 |
|
In [12]:
df.name = df.column(:name).to_a.map{|name| name.match(/chr(.+)/)[1]}
df
Out[12]:
| name | size | df | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 249250621 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 243199373 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 198022430 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 191154276 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 180915260 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 171115067 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 159138663 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 146364022 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 141213431 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 135534747 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 135006516 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 133851895 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 115169878 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 107349540 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 102531392 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 90354753 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ... | ... | ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| y | 59373566 |
|
In [13]:
color = Nyaplot::Colors.qual
Out[13]:
| rgb(102,194,165) | rgb(252,141,98) | rgb(141,160,203) | rgb(231,138,195) | rgb(166,216,84) | rgb(255,217,47) | rgb(229,196,148) | rgb(179,179,179) |
|---|---|---|---|---|---|---|---|
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]:
| from | to |
|---|---|
| 18.24925060 | 13.104685252 |
| 17.64805156 | 7.139580336 |
| 18.64805156 | 15.69790168 |
| 2.34895084 | 9.0 |
| 14.64805156 | 14.74775180 |
| 4.179460432 | 4.154535372 |
| 5.39880096 | 22.24925060 |
| 8.129610312 | x.149550360 |
| 19.49850120 | 5.134595324 |
| 3.174475420 | 12.44865108 |
| 5.24925060 | 20.29910072 |
| 20.24925060 | 3.89730216 |
| 10.49850120 | 21.19940048 |
| 2.199400480 | 1.124625300 |
| 4.119640288 | 19.4985012 |
| 9.39880096 | x.39880096 |
| ... | ... |
| 22.24925060 | 22.39880096 |
In [16]:
plot2.add_connector_with_df(connector_df, :from, :to)
plot2.show
Out[16]: