cytoscape / py2cytoscape

Python utilities for Cytoscape and Cytoscape.js
https://py2cytoscape.readthedocs.io
MIT License
178 stars 45 forks source link

how to set node width #91

Open mtill opened 5 years ago

mtill commented 5 years ago

Hello, how can I resize the nodes of a graph depending on the length of their labels? I.e., a node with a "long" label should be wider than a node with a "short" label.

Thank you very much for your help -- and for this great library! Best regads mtill

Cytoscape:3.7.1 Java:1.8

jorgeboucas commented 5 years ago

you can create a column with the size of node, upload the table:

cytoscape.table.loadTableData(<dataframe>, df_key=<df_key>, table_key_column=< table_key_column >)

and then use that to define NODE_SIZE:

NODE_SIZE=cytoscape.vizmap.mapVisualProperty(visualProperty="NODE_SIZE",\
                                                   mappingType="passthrough",\
                                                   mappingColumn="word_size")
cytoscape.vizmap.update_style("dataStyle",mappings=[NODE_SIZE])
g-simmons commented 5 years ago

Is cytoscape in the above example supposed to be a CyRestClient object?

If it is, I get an AttributeError when trying to access cytoscape.vizmap

Here are some alternate instructions that worked for me, assuming that the labels for the nodes are already in the node table for your network.

#imports
from py2cytoscape.data.cyrest_client import CyRestClient
import pandas as pd

cy = CyRestClient()

#load some data
data = pd.read_csv('../data/<your_data_file.csv>')

#initialize the network
net = cy.network.create_from_dataframe(data,name="<network_name>",collection="<network_collection>")

# get the node table
node_table = net.get_node_table()

# get the label lengths
node_table['size'] = node_table.<label_column>.apply(len)

# update the node table
net.update_node_table(df=table,network_key_col="SUID",data_key_col="SUID")

# choose your favorite default style or skip if you already have a style object
my_style = cy.style.create('Marquee')

# create a mapping from the column value to NODE_SIZE and apply the style
my_style.create_passthrough_mapping(column='size',vp='NODE_SIZE',
            col_type='Integer')
cy.style.apply(my_style, net)

This is more verbose than the above, but in the above answer it's not really clear (at least to me) what the cytoscape object is.