moshi4 / pyCirclize

Circular visualization in Python (Circos Plot, Chord Diagram, Radar Chart)
https://moshi4.github.io/pyCirclize/
MIT License
715 stars 42 forks source link

DIC object Not callable #39

Closed DEV-SRB closed 1 year ago

DEV-SRB commented 1 year ago

while defining the new dictionary for c map in below code, the error can be seen in label_kws and link_kws color_dict = {}

for i in np.array(rows_df['Rows']):
    color_dict[i] = "blue"
for i in np.array(cols_df['Columns']):
    color_dict[i] = "red"

circos = Circos.initialize_from_matrix(
    matrix_df,
    space=2,
    cmap='tab10',  # Use color_dict as the colormap
    label_kws = dict(size=8, orientation='vertical'),
    link_kws=dict(ec='black', lw=0.5, direction=1, hatch="//"),
)
moshi4 commented 1 year ago

The partial error output and code shown does not help me determine the cause of the error.

Could you please provide example code and data so that I can reproduce this error?


You may have misunderstood how to use cmap dictionary in Circos.initialize_from_matrix(), so here is a code example.

from pycirclize import Circos
import pandas as pd

# Create matrix dataframe (3 x 6)
row_names = ["S1", "S2", "S3"]
col_names = ["E1", "E2", "E3", "E4", "E5", "E6"]
matrix_data = [
    [4, 14, 13, 17, 5, 2],
    [7, 1, 6, 8, 12, 15],
    [9, 10, 3, 16, 11, 18],
]
matrix_df = pd.DataFrame(matrix_data, index=row_names, columns=col_names)

# Initialize from matrix (Can also directly load tsv matrix file)
circos = Circos.initialize_from_matrix(
    matrix_df,
    space=5,
    r_lim=(93, 100),
    # cmap="tab10",
    cmap=dict(S1="red", S2="blue", S3="green", E1="grey", E2="grey", E3="grey", E4="grey", E5="grey", E6="grey"),
    label_kws=dict(r=94, size=12, color="white"),
    link_kws=dict(ec="black", lw=0.5),
)
circos.savefig("result.png")

result.png

result

DEV-SRB commented 1 year ago

Thank you For your Response, Is it possible to give different colors to labels?

moshi4 commented 1 year ago

I can't reproduce the error without the data file.

Could you also upload Gene.xlsx? If it is not possible to upload the research data as is, dummy data with appropriate numerical changes is fine.

DEV-SRB commented 1 year ago

I apologize for the confusion, but i solved the error thanks to your example, but I wanted to change the label colors to differentiate between row and column values. I am sharing my example data and the code here below. And I appreciate your quick responses to my queries.

Here below is my code,

import pandas as pd  
import numpy as np  
from pycirclize import Circos  

color_dict={}
def filter_data_and_create_matrix(fname, col1, col2, filter_col, threshold):
    file_path = fname
    df = pd.read_excel(file_path)

    #  Filtering based on the specified threshold
    df = df[df[filter_col] > threshold]

    # Extracting unique values from the specified columns
    column1_values = df[col1].unique()
    column2_values = df[col2].str.split(',').explode().str.strip().unique()

    # Zero Matrix
    matrix = np.zeros((len(column1_values), len(column2_values)))

    # Matrix based on column values & filtering criteria
    for i, value1 in enumerate(column1_values):
        for j, value2 in enumerate(column2_values):
            if any((df[col1] == value1) & (df[col2].str.contains(value2))):
                matrix[i][j] = 1

    # DataFrames for rows and columns
    rows_df = pd.DataFrame(column1_values, columns=['Rows'])
    cols_df = pd.DataFrame(column2_values, columns=['Columns'])

    # DataFrame for the matrix
    matrix_df = pd.DataFrame(matrix, columns=cols_df['Columns'], index=rows_df['Rows'])
    # for i in np.array(rows_df['Rows']):
    #     color_dict[i] = "blue"
    # for i in np.array(cols_df['Columns']):
    #     color_dict[i] = "red"
    return matrix_df
#ciros plot 
def create_circos_plot(matrix_df, space_size, svg_file_name):
    circos = Circos.initialize_from_matrix(
        matrix_df,
        space=space_size,
        cmap='tab10', #Can use dic 'color_dict'
        label_kws=dict(size=8, orientation='vertical'),
        link_kws=dict(ec="black", lw=0.5, direction=1, hatch="//"),
    )
    circos.savefig(svg_file_name)

filtered_matrix_df = filter_data_and_create_matrix('Gene.xlsx', 'term_id', 'intersections', 'intersection_size', 2)
create_circos_plot(filtered_matrix_df, 2, 'example04.svg')

Gene.xlsx

moshi4 commented 1 year ago

It is possible to change the label color as shown in the code example below.

from pycirclize import Circos
import pandas as pd

# Create matrix dataframe (3 x 6)
row_names = ["S1", "S2", "S3"]
col_names = ["E1", "E2", "E3", "E4", "E5", "E6"]
matrix_data = [
    [4, 14, 13, 17, 5, 2],
    [7, 1, 6, 8, 12, 15],
    [9, 10, 3, 16, 11, 18],
]
matrix_df = pd.DataFrame(matrix_data, index=row_names, columns=col_names)

# Initialize from matrix (Can also directly load tsv matrix file)
cmap = dict(S1="red", S2="blue", S3="green", E1="grey", E2="grey", E3="grey", E4="grey", E5="grey", E6="grey")
circos = Circos.initialize_from_matrix(
    matrix_df,
    space=5,
    r_lim=(93, 100),
    cmap=cmap,
    label_kws=dict(size=0, color="none"), # Disable plotting label automatically
    link_kws=dict(ec="black", lw=0.5),
)
# Manually plot label
for sector in circos.sectors:
    label_color = cmap[sector.name]
    sector.text(sector.name, r=105, color=label_color, size=12)

circos.savefig("result.png")

result.png

result

DEV-SRB commented 1 year ago

Thank you