Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve order of tsv rows during rendering #28

Closed
naturepoker opened this issue Jul 2, 2023 · 2 comments
Closed

Preserve order of tsv rows during rendering #28

naturepoker opened this issue Jul 2, 2023 · 2 comments
Labels
question Further information is requested

Comments

@naturepoker
Copy link

Hi,

Thanks for the wonderful package - I've been having a lot of fun with it.

I'm trying to produce a linked circle plot with an imported tsv file (three column consisting of from/too/value) - everything seems to work okay, except rendered image re-orders the first column into alphabetical order.
Is there a way to suppress that behavior and preserve the order in the input tsv file?

Attaching current code for reference:

from pycirclize import Circos
from pycirclize.parser import Matrix
import pandas as pd

fromto_table_df = pd.read_csv("sorted.tsv",sep="\t")

matrix = Matrix.parse_fromto_table(fromto_table_df)

circos = Circos.initialize_from_matrix(
	matrix,
	space=1,
	cmap="tab10",
	label_kws=dict(size=3, orientation="vertical"),
	link_kws=dict(ec="white", lw=0),
)

fig = circos.savefig('sorted.png', dpi=500)

Thank you!

@moshi4
Copy link
Owner

moshi4 commented Jul 3, 2023

Hi @naturepoker,

pyCirclize does not provide a way to preserve the row order when converting from a tsv file to a matrix.
However, users can freely change the node order in Chord Diagram by using the order option of Circos.initialize_from_matrix().

The following is an example code using the order option.

from pycirclize import Circos
from pycirclize.parser import Matrix
import pandas as pd

fromto_table_df = pd.DataFrame(
    [
        ["A", "B", 10],
        ["A", "C", 5],
        ["A", "D", 15],
        ["A", "E", 20],
        ["A", "F", 3],
        ["B", "A", 3],
        ["B", "G", 15],
        ["F", "D", 13],
        ["F", "E", 2],
        ["E", "A", 20],
        ["E", "D", 6],
    ],
    columns=["from", "to", "value"],
)
matrix = Matrix.parse_fromto_table(fromto_table_df)

circos = Circos.initialize_from_matrix(
    matrix,
    space=3,
    order=["C", "D", "E", "F", "B", "A", "G"], # Set user-defined order
    label_kws=dict(size=12, r=105),
    link_kws=dict(direction=1, ec="black", lw=0.5),
)

circos.savefig("result.png")

result

@naturepoker
Copy link
Author

This worked perfectly, thank you!

@moshi4 moshi4 added the question Further information is requested label May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants