9
3

More than 1 year has passed since last update.

pyCircosで綺麗な円形グラフをplotする#1

Last updated at Posted at 2023-01-26

pyCircosとは

ゲノム情報の可視化ソフトCircospythonmatplotlibベースで書くことができる可視化パッケージです。比較的簡単にきれいなCircosを書くことができます。
Document example
image.png

1.install

#python 3.9
#anaconnda環境
pip install python-circos

1-1.exapmle notebookのclone

git clone https://github.com/ponnhide/pyCircos-examples.git

1-2.ディレクトリ構成

.
├── mycircos
│   ├── 01.ipynb
│   └── sample_data
└── pyCircos-examples
    ├── README.md
    └── example_notebooks

2.tutorial

2-1.簡単な円形グラフプロット tickplot

mycircos/01.ipynb
from pycircos import Garc, Gcircle #この二つのクラスが大事
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline

GarcGcircleが重要なクラスで、Garcにグラフの情報を追加していき、Gcircleはグラフのmatplotでいう所のfigureの役割を果たしています。

mycircos/01.ipynb
data_file = 'sample_data/example_data_chromosome_general.csv'
data_df = pd.read_csv(data_file)
data_df.head()

image.png

mycircos/01.ipynb
#figureの定義とサイズの指定
circle = Gcircle(figsize=(4,4))

#データのリスト作成
names = data_df['chr'].tolist()
values = data_df['end'].tolist()

#circleにデータ一つずつnameとvalueを格納していきます。
for name, value in zip(names, values):

    arc = Garc(arc_id=name, size=value, interspace=2, raxis_range=(900,985))
    '''
    arc_id : データ一つ一つのID(arc)
    size : arcの長さ
    interspace : arc同士の間隔
    raxis_range : arcの幅、太さ 900から985まで
    '''
    circle.add_garc(arc) #circleにarc情報を格納


circle.set_garcs(0,270)

for arc_id in circle.garc_dict:
    circle.tickplot(
        arc_id,
        raxis_range=(985,990),
        tickinterval=20000000, #tickの間隔
        )
circle.figure #notebookでのグラフの表示

2-2.画像保存方法

circle.figure.savefig(fname='filenamae')

3.labelの位置とサイズ

labelの設定方法はDocumentでは以下のようになっています。

Document
label : str, optional
            Label of the arc section. The default is None.

labelposition : int, optional
    Relative label height from the center of the arc section.
    The default is 0.
    #arcの真ん中を0として配置するデフォルトは0

labelsize : int, optional
    Font size of the label. The default is 10.

ここでlabelpositionarcの中心が0の相対座標で、Garcraxis_rangeはcircleの中心が0の絶対座標なので注意が必要です。下図参照
画像3.png

常にarcの30だけ外側にlabelを配置するには以下のように設定します

circle = Gcircle(figsize=(8,8))
m = 985
n = 880
names = data_df['chr'].tolist()
values = data_df['end'].tolist()
for name, value in zip(names, values):
    arc = Garc(
        arc_id=name,
        size=value,
        interspace=2,
        raxis_range=(n,m),
        label_visible=True,
        labelposition=int(30 + (m-n)/2), #30は任意、どのぐらい外側に配置するか
        labelsize=10
        )
    circle.add_garc(arc)

4. valueの大きさによってスケーリング

これまでのことを組み合わせてこのようなこともできる。

from sklearn.preprocessing import MinMaxScaler
m = 985
n = 600
scaler = MinMaxScaler((n,m)) #600から985の間にスケーリングする
arr = np.array(values).reshape(-1,1)
values_scaled = scaler.fit_transform(arr)
values_scaled = values_scaled.reshape(-1).tolist()

circle = Gcircle(figsize=(8,8))

names = data_df['chr'].tolist()
values = data_df['end'].tolist()
for name, value, scaled_value in zip(names, values, values_scaled):
    arc = Garc(
        arc_id=name,
        size=value,
        interspace=5,
        raxis_range=(n,scaled_value),
        label_visible=True,
        labelposition=int(30 + (scaled_value-n)/2), #labelpositionもスケールに連動させる
        labelsize=10
        )
    circle.add_garc(arc)
    
circle.set_garcs(0,360)
for arc_id in circle.garc_dict:
    circle.tickplot(
        arc_id,
        raxis_range=(985,985),
        tickinterval=20000000,

06.png

次の記事

いろいろなplotを組み合わせる

9
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3