<jupyter_start><jupyter_text>Group Nodes <NAME> and <NAME> 2020-12-17 This vignette will show you how to use node grouping functions to manipulate graphs in Cytoscape.  InstallationThe following chunk of code installs the `py4cytoscape` module.<jupyter_code>%%capture
!python3 -m pip install python-igraph requests pandas networkx
!python3 -m pip install py4cytoscape<jupyter_output><empty_output><jupyter_text>PrerequisitesIn addition to this package (py4cytoscape version 0.0.7), you will need:* Latest version of Cytoscape, which can be downloaded from https://cytoscape.org/download.html. Simply follow the installation instructions on screen.* Complete installation wizard* Launch CytoscapeYou can also install app inside Python notebook by running "py4cytoscape.install_app('Your App')"  Import the required package<jupyter_code>import os
import sys
import pandas as pd
import py4cytoscape as p4c<jupyter_output><empty_output><jupyter_text>Setup Cytoscape<jupyter_code>p4c.cytoscape_version_info()<jupyter_output><empty_output><jupyter_text>BackgroundThe ability to group nodes together into “metanodes” and collapse them to a single node in a graph is useful for simplifying views of a complex network.The example in this vignette describes application of node grouping functions to data that includes protein-protein interactions and clustered correlations of protein post-translational modifications (Grimes, et al., 2018). This vignette plots five proteins and their modifications, and uses the node grouping functions to manipulate the graph in Cytoscape.  ExampleFirst we set up the node and edge data frames.<jupyter_code>net_nodes = ["ALK", "ALK p Y1078", "ALK p Y1096", "ALK p Y1586", "CTNND1", "CTNND1 p Y193", "CTNND1 p Y217", "CTNND1 p Y228", "CTNND1 p Y241", "CTNND1 p Y248", "CTNND1 p Y302", "CTNND1 p Y904", "CTTN", "CTTN ack K107", "CTTN ack K124", "CTTN ack K147", "CTTN ack K161", "CTTN ack K235", "CTTN ack K390", "CTTN ack K87", "CTTN p S113", "CTTN p S224", "CTTN p Y104", "CTTN p Y154", "CTTN p Y162", "CTTN p Y228", "CTTN p Y334", "CTTN p Y421", "IRS1", "IRS1 p Y632", "IRS1 p Y941", "IRS1 p Y989", "NPM1", "NPM1 ack K154", "NPM1 ack K223", "NPM1 p S214", "NPM1 p S218"]
parent = ["", "ALK", "ALK", "ALK", "", "CTNND1", "CTNND1", "CTNND1", "CTNND1", "CTNND1", "CTNND1", "CTNND1", "", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "", "IRS1", "IRS1", "IRS1", "", "NPM1", "NPM1", "NPM1", "NPM1"]
nodeType = ["protein", "modification", "modification", "modification", "protein", "modification", "modification", "modification", "modification", "modification", "modification", "modification", "protein", "modification", "modification", "modification", "modification", "modification", "modification", "modification", "modification", "modification", "modification", "modification", "modification", "modification", "modification", "modification", "protein", "modification", "modification", "modification", "protein", "modification", "modification", "modification", "modification"]
net_genes = []
for n in net_nodes:
    s = n.split()[0]
    net_genes.append(s)
d = {'id': net_nodes, 'Gene.name':net_genes, 'parent': parent, 'nodeType':nodeType}
netnodes_df = pd.DataFrame(data=d)
netnodes_df.head()
# Define edge data
source_nodes = ["ALK", "ALK", "ALK", "CTNND1", "CTNND1", "CTNND1", "CTNND1", "CTNND1", "CTNND1", "CTNND1", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "CTTN", "IRS1", "IRS1", "IRS1", "NPM1", "NPM1", "NPM1", "NPM1", "ALK p Y1096", "CTNND1 p Y193", "CTNND1 p Y193", "CTNND1 p Y228", "CTNND1 p Y904", "CTNND1 p Y217", "CTNND1 p Y241", "CTNND1 p Y248", "ALK p Y1078", "ALK p Y1096", "ALK p Y1586", "IRS1 p Y941", "CTTN ack K147", "CTTN ack K107", "CTTN ack K235", "CTTN ack K87", "CTTN ack K147", "CTTN ack K124", "CTTN ack K147", "CTTN ack K235", "CTTN ack K161", "CTTN ack K390", "NPM1 ack K223", "NPM1 ack K154", "NPM1 ack K223", "ALK", "CTNND1", "CTNND1", "CTTN", "IRS1"]
target_nodes = ["ALK p Y1078", "ALK p Y1096", "ALK p Y1586", "CTNND1 p Y193", "CTNND1 p Y217", "CTNND1 p Y228", "CTNND1 p Y241", "CTNND1 p Y248", "CTNND1 p Y302", "CTNND1 p Y904", "CTTN ack K107", "CTTN ack K124", "CTTN ack K147", "CTTN ack K161", "CTTN ack K235", "CTTN ack K390", "CTTN ack K87", "CTTN p S113", "CTTN p S224", "CTTN p Y104", "CTTN p Y154", "CTTN p Y162", "CTTN p Y228", "CTTN p Y334", "CTTN p Y421", "IRS1 p Y632", "IRS1 p Y941", "IRS1 p Y989", "NPM1 ack K154", "NPM1 ack K223", "NPM1 p S214", "NPM1 p S218", "ALK p Y1586", "CTNND1 p Y228", "CTNND1 p Y302", "CTNND1 p Y302", "CTTN p Y154", "CTTN p Y162", "CTTN p Y162", "CTTN p Y334", "IRS1 p Y632", "IRS1 p Y989", "IRS1 p Y989", "IRS1 p Y989", "CTTN p S113", "CTTN p S224", "CTTN p S224", "CTTN p S224", "CTTN p Y104", "CTTN p Y228", "CTTN p Y228", "CTTN p Y228", "CTTN p Y421", "CTTN p Y421", "NPM1 p S214", "NPM1 p S218", "NPM1 p S218", "IRS1", "CTTN", "IRS1", "NPM1", "NPM1"]
Weight = [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 0.8060606, 0.7575758, 0.7454545, 0.9393939, 0.8949096, 0.7329699, 0.7553845, 0.7866191, 0.775, 0.6969697, 0.7818182, 0.8424242, -0.7714286, -0.8385965, -0.5017544, -0.7473684, -0.5252838, -0.9428571, -0.8285714, -0.6713287, -0.5508772, -0.9428571, -0.8857143, -0.6310881, -0.8285714, 0.6123365, 2.115272, 0.002461723, 0.3354451, 0.5661711]
d = {'source': source_nodes, 'target':target_nodes, 'Weight': Weight}
netedges_df = pd.DataFrame(data=d)
netedges_df.head()
#create network from data frames
net_suid = p4c.create_network_from_data_frames(netnodes_df, netedges_df, title="Group Nodes Test", collection = "Py4cytoscape Vignettes")
p4c.layout_network('force-directed')<jupyter_output><empty_output><jupyter_text>Note that for convenience the data frame has defined whether a node is a protein or a modification, and also defined the parent node for each modification.The function selectNodes looks by default for the node SUID, which can be retrieved by getTableColumns. Alternatively, the data frame can be used to distinguish proteins and modifications.<jupyter_code>nodedata = p4c.get_table_columns(table='node')
edgedata = p4c.get_table_columns(table='edge')
nodedata.head()
genes = netnodes_df[netnodes_df['nodeType'].str.contains("protein")]['id']
genes = pd.DataFrame(genes)
genes
#select by gene SUIDs
geneSUIDs = nodedata[nodedata['nodeType'].str.contains("protein")]['SUID']
geneSUIDs = pd.DataFrame(geneSUIDs)
p4c.select_nodes(nodes=list(geneSUIDs['SUID']), preserve_current_selection=False)
# or by names in the "id" column
p4c.select_nodes(nodes=["ALK","IRS1"],by_col='id', preserve_current_selection=False)
# or by names based on dataframe subsetting
modifications = netnodes_df[netnodes_df['nodeType'].str.contains("modification")]['id']
p4c.select_nodes(nodes=list(modifications),by_col='id', preserve_current_selection=False)
# Now select one protein and all its modifications
deltacatnodes = netnodes_df[netnodes_df['Gene.name'].str.contains("CTNND1")]['id']
p4c.select_nodes(nodes=list(deltacatnodes),by_col='id', preserve_current_selection=False)<jupyter_output><empty_output><jupyter_text>Let’s create a new group of the selected nodes and collapse it into one node…<jupyter_code>p4c.create_group(group_name='delta catenin group')
p4c.collapse_group(groups='delta catenin group')<jupyter_output><empty_output><jupyter_text>…then expand it again.<jupyter_code>group_number = p4c.expand_group(groups='delta catenin group')
group_number['groups']<jupyter_output><empty_output><jupyter_text>For these data, we can create groups of all proteins together with their modifications. Here we name the groups by their gene names.<jupyter_code>p4c.delete_group(groups=group_number['groups'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15521821631878557, 'fraction_numerical': 0.10246679316888045, 'mean_word_length': 6.472589792060491, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '440869', 'license': 'CC0-1.0', 'n_tokens_mistral': 3621, 'n_tokens_neox': 2974, 'n_words': 908, 'path': 'for-scripters/Python/group-nodes.ipynb', 'repo_name': 'kozo2/cytoscape-automation', 'size': '7912'}