1.前回#1
2.今回の内容barplot
scatterplot
lineplot
heatmap
chordplot
2-1.使用するデータ
from pycircos import Gcircle,Garc
from collections import defaultdict
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
general_data = './sample_data/example_data_chromosome_general.csv'
cytoband_data = './sample_data/example_data_chromosome_cytoband.csv'
point_data = './sample_data/example_data_point.csv'
bar_data = './sample_data/example_data_barplot.csv'
gradual_data = './sample_data/example_data_rect_gradual.csv'
link_data = './sample_data/example_data_links.csv'
general_data | cytoband_data |
point_data | bar_data |
gradual_data | link_data |
"> | "> |
2-2.arcの基本情報の設定
この過程はどのプロットをするときにも使用します。
circle = Gcircle(figsize=(8,8))
circle.set_garcs() #デフォルトは0°から360°
#gengeral file からGarcの設定
with open(general_data) as f:
f.readline()
for line in f:
line = line.rstrip().split(',') #csvなのでこのようにすれば分割できる
name = line[0]
length = int(line[2])
arc = Garc(
arc_id=name,
size=length, #arcの長さをlengthに設定
interspace=2, #arcの間隔
raxis_range=(950,1000), #arcの太さ
labelposition=60, #labelの位置 arcの中心から60外側
label_visible=True #labelを表示 Falseで非表示
)
circle.add_garc(arc) #arcを追加
circle.set_garcs() #デフォルトは0°から360°
2-3.barplot
barplot
するためのデータを格納した辞書を作成
#dataを格納する辞書の作成
arcdata_dict = defaultdict(dict)
color_dict = {"gneg":"#FFFFFF00", "gpos25":"#EEEEEE", "gpos50":"#BBBBBB", "gpos75":"#777777", "gpos100":"#000000", "gvar":"#FFFFFF00", "stalk":"#C01E27", "acen":"#D82322"}
with open(cytoband_data) as cf:
cf.readline()
for line in cf:
line = line.rstrip().split(',')
name = line[0]
start = int(line[1])-1
width = int(line[2]) - start
if name not in arcdata_dict:
arcdata_dict[name]['positions'] = []
arcdata_dict[name]['width'] = []
arcdata_dict[name]['colors'] = []
arcdata_dict[name]['positions'].append(start)
arcdata_dict[name]['width'].append(width)
arcdata_dict[name]['colors'].append(color_dict[line[4]])
最後にデータをもとにbarplot
していきます
for key in arcdata_dict:
circle.barplot(
garc_id = key,
data = [1]*len(arcdata_dict[key]['positions']),#barの高さ今回はすべて同じ高さ,最大値がraxis_rangeの幅と同じになる
width = arcdata_dict[key]['width'],#barの太さ
raxis_range=[950,1000],#barを描画する範囲, arcの外に配置することも可能
facecolor = arcdata_dict[key]['colors'], #barの色
positions = arcdata_dict[key]['positions'] #それぞれのbarの位置
)
circle.figure #描画
2-4.scatterplot
2-3のcircleにscatterplot
を追加します。
values_all = []
arcdata_dict = defaultdict(dict)
with open(scatter_data) as f:
f.readline()
for line in f:
line = line.rstrip().split(',')
name = line[0]
start = int(line[1]) -1
end = int(line[2])
mid = (start+end)/2
value = float(line[3])
values_all.append(value)
if name not in arcdata_dict:
arcdata_dict[name]['positions'] = []
arcdata_dict[name]['values'] = []
arcdata_dict[name]['positions'].append(mid)
arcdata_dict[name]['values'].append(value)
vmin, vmax = min(values_all), max(values_all)
for key in arcdata_dict:
circle.scatterplot(
garc_id=key,
data=arcdata_dict[key]['values'],
positions=arcdata_dict[key]['positions'],
rlim=[vmin-0.05*abs(vmin), vmax+0.05*abs(vmax)],
raxis_range=(640,690),
facecolor='orangered',
spine=True #枠線の有無
)
2-4.lineplot
for key in arcdata_dict:
circle.lineplot(
key,
data=arcdata_dict[key]["values"],
positions=arcdata_dict[key]["positions"],
rlim=[vmin-0.05*abs(vmin), vmax+0.05*abs(vmax)],
raxis_range=(570,620),
linecolor="royalblue",
spine=False
)
2-5.barplot
values_all = []
arcdata_dict = defaultdict(dict)
with open(bar_data) as f:
f.readline()
for line in f:
line = line.rstrip().split(",")
name = line[0]
start = int(line[1])-1
end = int(line[2])
width = end-start
if name not in arcdata_dict:
arcdata_dict[name]["positions"] = []
arcdata_dict[name]["widths"] = []
arcdata_dict[name]["values"] = []
arcdata_dict[name]["positions"].append(start)
arcdata_dict[name]["widths"].append(width)
arcdata_dict[name]["values"].append(float(line[-1]))
values_all.append(float(line[-1]))
vmin, vmax = min(values_all), max(values_all)
for key in arcdata_dict:
circle.barplot(
key,
data=arcdata_dict[key]["values"],
positions=arcdata_dict[key]["positions"],
width=arcdata_dict[key]["widths"],
base_value=0.0,
rlim=[vmin-0.05*abs(vmin), vmax+0.05*abs(vmax)],
raxis_range=(500,550),
facecolor="y", #yellow
spine=True
)
2-6.heatmap
values_all = []
arcdata_dict = defaultdict(dict)
with open(gradual_data) as f:
f.readline()
for line in f:
line = line.rstrip().split(",")
name = line[0]
start = int(line[1])-1
end = int(line[2])
width = end-start
if name not in arcdata_dict:
arcdata_dict[name]["positions"] = []
arcdata_dict[name]["widths"] = []
arcdata_dict[name]["values"] = []
arcdata_dict[name]["positions"].append(start)
arcdata_dict[name]["widths"].append(width)
arcdata_dict[name]["values"].append(float(line[-1]))
values_all.append(float(line[-1]))
vmin, vmax = min(values_all), max(values_all)
for key in arcdata_dict:
circle.heatmap(key,
data=arcdata_dict[key]["values"],
positions=arcdata_dict[key]["positions"],
width=arcdata_dict[key]["widths"],
raxis_range=(430,480),
vmin=vmin, vmax=vmax,
cmap=plt.cm.viridis
)
2-7 chordplot
values_all = []
arcdata_dict = defaultdict(dict)
with open(link_data) as f:
f.readline()
for line in f:
line = line.rstrip().split(",")
name1 = line[0]
start1 = int(line[1])-1
end1 = int(line[2])
name2 = line[3]
start2 = int(line[4])-1
end2 = int(line[5])
source = (name1, start1, end1, 425) #繋ぐもと
destination = (name2, start2, end2, 425)#繋ぐ先
circle.chord_plot(
source,
destination,
facecolor=circle.garc_dict[name1].facecolor
)