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]:
groupdf
group0
axisval
a2
b3
c4
group1
axisval
a2
b3
c4
group2
axisval
a2
b3
c4
group3
axisval
a2
b3
c4
group4
axisval
a2
b3
c4
group5
axisval
a2
b3
c4
group6
axisval
a2
b3
c4
group7
axisval
a2
b3
c4
group8
axisval
a2
b3
c4
group9
axisval
a2
b3
c4
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]:
namesize
chr1249250621
chr2243199373
chr3198022430
chr4191154276
chr5180915260
chr6171115067
chr7159138663
chr8146364022
chr9141213431
chr10135534747
chr11135006516
chr12133851895
chr13115169878
chr14107349540
chr15102531392
chr1690354753
......
chry59373566
In [7]:
df2 = Nyaplot::DataFrame.from_csv('./data/circular/hgmd.tsv', {col_sep: "\t"})
Out[7]:
gene_namestartendnum1num2
hs11825545571875545570.012.0
hs12232845282282845280.015.0
hs143804340488043400.01.0
hs120972111259721110.02.0
hs1165084566508450.021.0
hs12232844442282844440.01.0
hs112252955172529550.010.0
hs145797505507975050.03.0
hs12265553022315553020.09.0
hs120977000259770000.014.0
hs11825513371875513370.017.0
hs18030994130309940.01.0
hs155638075606380750.020.0
hs12232852002282852000.03.0
hs12232845992282845990.07.0
hs1072195439771954390.010.0
...............
hsX1532841921582841920.012.0
In [8]:
df3 = Nyaplot::DataFrame.from_csv('./data/circular/genes_hgmd.tsv', {col_sep: "\t"})
Out[8]:
gene_namestartendname
hs1182554557182555057RNASEL
hs1223284528223285028TLR5
hs14380434043804840MPL
hs12097211120972611PINK1
hs116508451651345CDK11A
hs1223284444223284944TLR5
hs11225295512253455TNFRSF1B
hs14579750545798005MUTYH
hs1226555302226555802PARP1
hs12097700020977500PINK1
hs1182551337182551837RNASEL
hs180309948031494PARK7
hs15563807555638575USP24
hs1223285200223285700TLR5
hs1223284599223285099TLR5
hs107219543972195939NODAL
............
hsX153284192153284692IRAK1
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]:
namesizedf
chr1249250621
axisvalname
021.0CDK11A
49850121.0PARK7
997002410.0TNFRSF1B
149550360
1994004816.0PINK1
249250600
299100720
348950840
398800961.0MPL
448651083.0MUTYH
498501200
5483513220.0USP24
598201440
648051560
697901680
747751800
.........
2442655880
chr2243199373
axisvalname
00
49850120
997002411.0ROCK2
149550360
199400480
249250600
299100720
348950840
398800960
448651085.0MSH6
498501200
548351320
598201440
648051560
697901680
747751800
.........
2342955640
chr3198022430
axisvalname
00
49850120
997002412.0XPC
149550360
199400480
249250600
299100720
3489508410.0MLH1
398800960
4486510811.0MST1
498501200
548351320
598201440
648051560
697901680
747751800
.........
1894304560
chr4191154276
axisvalname
017.0GRK4
49850120
99700240
149550360
1994004815.0PPARGC1A
249250600
299100720
3489508410.0TLR1
398800960
448651080
498501200
5483513213.0KDR
598201440
648051560
697901680
7477518010.0BTC
.........
18444544433.0CASP3
chr5180915260
axisvalname
00
49850120
99700241.0DNAH5
149550360
199400480
249250600
299100720
348950840
398800960
448651080
498501204.0ITGA2
548351320
598201440
648051568.0PIK3R1
697901680
7477518010.0THBS4
.........
1744754200
chr6171115067
axisvalname
00
49850120
99700240
149550360
199400480
249250600
2991007230.0EHMT2
348950840
398800961.0SLC29A1
448651080
498501204.0PKHD1
548351320
598201440
648051560
697901680
747751800
.........
1645053960
chr7159138663
axisvalname
01.0MAD1L1
49850120
99700240
149550360
199400480
249250600
299100720
348950840
398800962.0GLI3
4486510811.0IGFBP1
498501200
5483513211.0EGFR
598201440
648051560
697901680
747751800
.........
1495503600
chr8146364022
axisvalname
00
49850120
99700241.0BLK
149550360
199400487.0TNFRSF10A
249250600
2991007211.0WRN
348950840
398800960
448651080
498501200
548351320
598201440
648051560
697901680
747751800
.........
1395803360
chr9141213431
axisvalname
00
49850129.0KDM4C
997002418.0CER1
149550360
199400480
249250600
299100720
348950840
398800960
448651080
498501200
548351320
598201440
648051560
697901680
747751800
.........
13459532412.0CARD9
chr10135534747
axisvalname
00
49850120
99700240
149550362.0CUBN
1994004810.0PIP4K2A
249250600
299100720
348950840
3988009613.0RET
448651080
498501200
548351320
598201440
6480515610.0CTNNA3
6979016810.0NODAL
747751800
.........
12961031216.0MGMT
chr11135006516
axisvalname
00
49850120
99700240
149550360
199400480
249250600
299100720
348950840
398800960
4486510810.0PTPRJ
498501200
548351320
598201440
6480515621.0GSTP1
697901680
747751800
.........
1296103120
chr12133851895
axisvalname
01.0FGF23
49850120
99700245.0CDKN1B
1495503613.0PIK3C2G
199400480
249250600
299100720
348950840
3988009610.0LRRK2
448651080
498501201.0ATF1
5483513217.0GLI1
598201440
648051560
697901680
747751800
.........
1246253000
chr13115169878
axisvalname
00
49850120
99700240
149550360
199400480
249250600
2991007218.0BRCA2
348950840
398800960
448651080
4985012015.0SETDB2
548351320
598201440
648051560
697901680
747751800
.........
10967026412.0COL4A1
chr14107349540
axisvalname
00
49850120
99700240
149550360
199400489.0RPGRIP1
249250600
299100720
348950840
398800960
448651080
4985012012.0BMP4
548351320
598201447.0PRKCH
648051560
697901681.0PSEN1
747751801.0MLH3
.........
997002400
chr15102531392
axisvalname
00
49850120
99700240
149550360
199400480
249250600
299100720
348950840
3988009633.0TP53BP1
448651080
498501205.0TRPM7
548351320
598201440
648051560
697901680
747751800
.........
947152280
chr1690354753
axisvalname
03.0NTHL1
49850120
99700241.0ERCC4
149550360
199400487.0PALB2
249250600
299100720
348950840
398800960
448651080
498501201.0RPGRIP1L
548351320
598201440
648051560
697901680
747751800
.........
847452040
.........
chry59373566
axisvalname
00
49850120
99700240
149550360
199400480
249250600
299100720
348950840
398800960
448651080
498501200
In [12]:
df.name = df.column(:name).to_a.map{|name| name.match(/chr(.+)/)[1]}
df
Out[12]:
namesizedf
1249250621
axisvalname
021.0CDK11A
49850121.0PARK7
997002410.0TNFRSF1B
149550360
1994004816.0PINK1
249250600
299100720
348950840
398800961.0MPL
448651083.0MUTYH
498501200
5483513220.0USP24
598201440
648051560
697901680
747751800
.........
2442655880
2243199373
axisvalname
00
49850120
997002411.0ROCK2
149550360
199400480
249250600
299100720
348950840
398800960
448651085.0MSH6
498501200
548351320
598201440
648051560
697901680
747751800
.........
2342955640
3198022430
axisvalname
00
49850120
997002412.0XPC
149550360
199400480
249250600
299100720
3489508410.0MLH1
398800960
4486510811.0MST1
498501200
548351320
598201440
648051560
697901680
747751800
.........
1894304560
4191154276
axisvalname
017.0GRK4
49850120
99700240
149550360
1994004815.0PPARGC1A
249250600
299100720
3489508410.0TLR1
398800960
448651080
498501200
5483513213.0KDR
598201440
648051560
697901680
7477518010.0BTC
.........
18444544433.0CASP3
5180915260
axisvalname
00
49850120
99700241.0DNAH5
149550360
199400480
249250600
299100720
348950840
398800960
448651080
498501204.0ITGA2
548351320
598201440
648051568.0PIK3R1
697901680
7477518010.0THBS4
.........
1744754200
6171115067
axisvalname
00
49850120
99700240
149550360
199400480
249250600
2991007230.0EHMT2
348950840
398800961.0SLC29A1
448651080
498501204.0PKHD1
548351320
598201440
648051560
697901680
747751800
.........
1645053960
7159138663
axisvalname
01.0MAD1L1
49850120
99700240
149550360
199400480
249250600
299100720
348950840
398800962.0GLI3
4486510811.0IGFBP1
498501200
5483513211.0EGFR
598201440
648051560
697901680
747751800
.........
1495503600
8146364022
axisvalname
00
49850120
99700241.0BLK
149550360
199400487.0TNFRSF10A
249250600
2991007211.0WRN
348950840
398800960
448651080
498501200
548351320
598201440
648051560
697901680
747751800
.........
1395803360
9141213431
axisvalname
00
49850129.0KDM4C
997002418.0CER1
149550360
199400480
249250600
299100720
348950840
398800960
448651080
498501200
548351320
598201440
648051560
697901680
747751800
.........
13459532412.0CARD9
10135534747
axisvalname
00
49850120
99700240
149550362.0CUBN
1994004810.0PIP4K2A
249250600
299100720
348950840
3988009613.0RET
448651080
498501200
548351320
598201440
6480515610.0CTNNA3
6979016810.0NODAL
747751800
.........
12961031216.0MGMT
11135006516
axisvalname
00
49850120
99700240
149550360
199400480
249250600
299100720
348950840
398800960
4486510810.0PTPRJ
498501200
548351320
598201440
6480515621.0GSTP1
697901680
747751800
.........
1296103120
12133851895
axisvalname
01.0FGF23
49850120
99700245.0CDKN1B
1495503613.0PIK3C2G
199400480
249250600
299100720
348950840
3988009610.0LRRK2
448651080
498501201.0ATF1
5483513217.0GLI1
598201440
648051560
697901680
747751800
.........
1246253000
13115169878
axisvalname
00
49850120
99700240
149550360
199400480
249250600
2991007218.0BRCA2
348950840
398800960
448651080
4985012015.0SETDB2
548351320
598201440
648051560
697901680
747751800
.........
10967026412.0COL4A1
14107349540
axisvalname
00
49850120
99700240
149550360
199400489.0RPGRIP1
249250600
299100720
348950840
398800960
448651080
4985012012.0BMP4
548351320
598201447.0PRKCH
648051560
697901681.0PSEN1
747751801.0MLH3
.........
997002400
15102531392
axisvalname
00
49850120
99700240
149550360
199400480
249250600
299100720
348950840
3988009633.0TP53BP1
448651080
498501205.0TRPM7
548351320
598201440
648051560
697901680
747751800
.........
947152280
1690354753
axisvalname
03.0NTHL1
49850120
99700241.0ERCC4
149550360
199400487.0PALB2
249250600
299100720
348950840
398800960
448651080
498501201.0RPGRIP1L
548351320
598201440
648051560
697901680
747751800
.........
847452040
.........
y59373566
axisvalname
00
49850120
99700240
149550360
199400480
249250600
299100720
348950840
398800960
448651080
498501200
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]:
fromto
18.2492506013.104685252
17.648051567.139580336
18.6480515615.69790168
2.348950849.0
14.6480515614.74775180
4.1794604324.154535372
5.3988009622.24925060
8.129610312x.149550360
19.498501205.134595324
3.17447542012.44865108
5.2492506020.29910072
20.249250603.89730216
10.4985012021.19940048
2.1994004801.124625300
4.11964028819.4985012
9.39880096x.39880096
......
22.2492506022.39880096
In [16]:
plot2.add_connector_with_df(connector_df, :from, :to)
plot2.show
Out[16]: