<jupyter_start><jupyter_text>Advanced cancer networks and data <NAME> and <NAME> 2021-01-05  Installation The following chunk of code installs the `py4cytoscape` module.<jupyter_code>%%capture
!python3 -m pip install python-igraph requests pandas networkx
!python3 -m pip install wget
!python3 -m pip install py4cytoscape<jupyter_output><empty_output><jupyter_text>Import the required packages<jupyter_code>import os
import sys
from time import sleep
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import py4cytoscape as p4c
import wget
from IPython.display import Image
%matplotlib inline
pd.options.mode.chained_assignment = None<jupyter_output><empty_output><jupyter_text>Setup Cytoscape* Launch Cytoscape on your local machine. If you haven't already installed Cytoscape, then download the latest version from http://cytoscape.org.* Install the STRING app, if you haven't already: http://apps.cytoscape.org/apps/stringapp* Install the filetransfer app from https://apps.cytoscape.org/apps/filetransfer* Leave Cytoscape running in the background during the remainder of the tutorial.* Check cytoscape connection.You can also install app inside Python notebook by running "py4cytoscape.install_app('Your App')"<jupyter_code>p4c.cytoscape_version_info()<jupyter_output><empty_output><jupyter_text>Step 1: Getting Disease NetworksUse Cytoscape to query the STRING database for networks of genes associated with breast cancer and ovarian cancer. Query STRING database by disease to generate networks Breast cancer<jupyter_code>string_cmd_list = ['string disease query','disease="breast cancer"', 'species="Homo sapiens"', 'limit=150', 'cutoff=0.9']
string_cmd = " ".join(string_cmd_list)
p4c.commands.commands_run(string_cmd)<jupyter_output><empty_output><jupyter_text>Ovarian cancer<jupyter_code>string_cmd_list = ['string disease query','disease="ovarian cancer"', 'species="Homo sapiens"', 'limit=150', 'cutoff=0.9']
string_cmd = " ".join(string_cmd_list)
p4c.commands.commands_run(string_cmd)<jupyter_output><empty_output><jupyter_text>Step 2: Interacting with Cytoscape  Get list of networks<jupyter_code>p4c.get_network_list()
p4c.get_network_suid('String Network - ovarian cancer')
p4c.get_network_suid('String Network - breast cancer')<jupyter_output><empty_output><jupyter_text>Layout network<jupyter_code>p4c.layout_network('circular', 'String Network - ovarian cancer')<jupyter_output><empty_output><jupyter_text>List of layout algorithms available<jupyter_code>p4c.commands.cyrest_get('apply/layouts')
p4c.layout_network('force-directed', 'String Network - ovarian cancer')<jupyter_output><empty_output><jupyter_text>Layout with parameters!<jupyter_code>p4c.set_layout_properties('force-directed', {'defaultSpringLength': 5, 'defaultSpringCoefficient': .000004})
p4c.get_layout_property_names('force-directed')<jupyter_output><empty_output><jupyter_text>These *command* calls will allow you to interrogate the layouts and parameters available in your active Cytoscape session. This includes any layouts added by apps you've installed! Get table data from networkNow, let's look at the tablular data associated with our STRING networks...<jupyter_code>p4c.get_table_column_names()<jupyter_output><empty_output><jupyter_text>Retrieve disease scores We can retrieve any set of columns from Cytoscape and store them as a data frame.<jupyter_code>disease_score_table = p4c.get_table_columns(columns='stringdb::disease score')
disease_score_table.head()
sns.set_style("white")
sns.kdeplot(data=disease_score_table["stringdb::disease score"].astype(float) )
plt.xlabel("disease score")
plt.ylabel("frequency")<jupyter_output><empty_output><jupyter_text>Generate subnetworksIn order to reflect your exploration back onto the network, let's generate subnetworks......from top quartile of 'disease score'<jupyter_code>top_quart=disease_score_table["stringdb::disease score"].quantile(0.75)
top_nodes=disease_score_table[disease_score_table["stringdb::disease score"]>top_quart].index.tolist()
top_nodes = [str(n) for n in top_nodes]
p4c.create_subnetwork(nodes=top_nodes, subnetwork_name="top disease quartile")<jupyter_output><empty_output><jupyter_text>...of connected nodes only<jupyter_code>p4c.create_subnetwork(nodes="selected", edges="all", subnetwork_name="top disease quartile connected")<jupyter_output><empty_output><jupyter_text>...from first neighbors of top 3 genes, using the network connectivity together with the data to direct discovery.<jupyter_code>p4c.set_current_network("String Network - ovarian cancer")
top_nodes=disease_score_table.sort_values(by=["stringdb::disease score"],\
                                         ascending=False)
top_nodes=top_nodes.index.tolist()[:3]
print(top_nodes)
p4c.select_nodes(nodes=top_nodes)
p4c.select_first_neighbors()
p4c.create_subnetwork(nodes="selected", subnetwork_name="top disease neighbors")
p4c.layout_network('force-directed', 'top disease neighbors')<jupyter_output><empty_output><jupyter_text>...from diffusion algorithm starting with top 3 genes, using the network connectivity in a more subtle way than just first-degree neighbors.<jupyter_code>p4c.set_current_network("String Network - ovarian cancer")
p4c.select_nodes(nodes=top_nodes)
p4c.diffusion_basic()
p4c.create_subnetwork(nodes="selected", subnetwork_name="top disease diffusion")
p4c.layout_network('force-directed', 'top disease diffusion')<jupyter_output><empty_output><jupyter_text>Step 3: Visualizing data on networks Load datasetsDownloaded TCGA data from https://portal.gdc.cancer.gov/. Also available via each TCGA publication, e.g.: * Breast: https://tcga-data.nci.nih.gov/docs/publications/brca_2012/* Ovarian: https://tcga-data.nci.nih.gov/docs/publications/ov_2011/Here we download TCGA data from our GitHub repo.<jupyter_code>wget.download('https://raw.githubusercontent.com/cytoscape/cytoscape-automation/master/for-scripters/Python/data/brc.exp.tsv')
wget.download('https://raw.githubusercontent.com/cytoscape/cytoscape-automation/master/for-scripters/Python/data/brc.mut.tsv')
brc_expr=pd.read_table("./brc.exp.tsv")
brc_mut=pd.read_table("./brc.mut.tsv")<jupyter_output><empty_output><jupyter_text>Breast Cancer DatsetThese datasets are similar to the data frames you normarlly encounter in R. For diversity, one using row.names to store corresponding gene names and the other uses the first column. Both are easy to import into Cytoscape.<jupyter_code>brc_expr.head()
brc_mut.head()<jupyter_output><empty_output><jupyter_text>Let's return to the Breast Cancer network...<jupyter_code>p4c.set_current_network("String Network - breast cancer")
p4c.set_layout_properties('force-directed', {'defaultSpringLength': 5, 'defaultSpringCoefficient': .000004})<jupyter_output><empty_output><jupyter_text>...and use the helper function from py4cytoscape called *load_table_data*<jupyter_code>p4c.load_table_data(brc_expr, table_key_column='display name')
p4c.load_table_data(brc_mut, data_key_column='Hugo_Symbol', table_key_column='display name')<jupyter_output><empty_output><jupyter_text>Visual stylesLet's create a new style to visualize our imported data...starting with the basics, we will specify a few defaults and obvious mappings in a custom style all our own.<jupyter_code>defaults = {'NODE_SHAPE': 'ellipse', 'NODE_SIZE': 60, 'NODE_FILL_COLOR':"#AAAAAA", 'EDGE_TRANSPARENCY': 120}
node_labels = p4c.map_visual_property('node label', 'display name', 'p')
p4c.create_visual_style('dataStyle', defaults=defaults, mappings=[node_labels])
p4c.set_visual_style('dataStyle')<jupyter_output><empty_output><jupyter_text>Now let's update the style with a mapping for mean expression. The first step is to grab the column data from Cytoscape and pull out the min and max to define our data mapping range of values.<jupyter_code>brc_expr_network = p4c.get_table_columns(columns=['expr.mean'])
min_brc_expr = min(brc_expr_network.dropna()['expr.mean'].tolist())
max_brc_expr = max(brc_expr_network.dropna()['expr.mean'].tolist())

min_brc_expr=abs(min_brc_expr)
max_brc_expr=abs(max_brc_expr)

max_brc_expr=max([min_brc_expr,max_brc_expr])
min_brc_expr=max_brc_expr*-1.0<jupyter_output><empty_output><jupyter_text>In python we check matplotlib's color maps to help us pick good colors to pair with our data values: https://matplotlib.org/examples/color/colormaps_reference.html<jupyter_code>cmap = matplotlib.cm.get_cmap("bwr")
norm = matplotlib.colors.Normalize(vmin=min_brc_expr, vmax=max_brc_expr)
min_color=matplotlib.colors.rgb2hex(cmap(norm(min_brc_expr)))
center_color=matplotlib.colors.rgb2hex(cmap(norm(0)))
max_color=matplotlib.colors.rgb2hex(cmap(norm(max_brc_expr)))  
print(min_color, center_color, max_color)
NODE_FILL_COLOR = p4c.map_visual_property('node fill color', 'expr.mean', 'c', [min_brc_expr, 0.0, max_brc_expr], [min_color, center_color,max_color])
p4c.update_style_mapping('dataStyle', NODE_FILL_COLOR)
p4c.set_visual_style('dataStyle')<jupyter_output><empty_output><jupyter_text>Pro-tip: depending on your data, it may be better to balance your color range over negative and positive values bounded by the largest min or max data value, so that color intensity scales similarly in both directions.  Visualize mutation dataOK, now let's update with a mapping for mutation. Here are all the same steps, but this time mapping mutation counts to *both* node border width and color.<jupyter_code>brc_mut_network = p4c.get_table_columns(columns=['mut_count'])
min_brc_mut = min(brc_mut_network.dropna()['mut_count'].tolist())
max_brc_mut = max(brc_mut_network.dropna()['mut_count'].tolist())

cent_brc_mut=20.0

cmap = matplotlib.cm.get_cmap("Reds")
norm = matplotlib.colors.Normalize(vmin=min_brc_mut, vmax=max_brc_mut)
min_color=matplotlib.colors.rgb2hex(cmap(norm(min_brc_mut)))
center_color=matplotlib.colors.rgb2hex(cmap(norm(cent_brc_mut)))
max_color=matplotlib.colors.rgb2hex(cmap(norm(max_brc_mut)))  
NODE_BORDER_PAINT = p4c.map_visual_property('node border paint', 'mut_count', 'c', [min_brc_mut, 20, max_brc_mut], [min_color, center_color, max_color])
p4c.update_style_mapping('dataStyle', NODE_BORDER_PAINT)
p4c.set_visual_style('dataStyle')
NODE_BORDER_WIDTH = p4c.map_visual_property('node border width', 'mut_count', 'c', [min_brc_mut, 20, max_brc_mut], [2, 4, 8])
p4c.update_style_mapping('dataStyle', NODE_BORDER_WIDTH)
p4c.set_visual_style('dataStyle')<jupyter_output><empty_output><jupyter_text>Subnetwork based on diffusion from heavily mutated nodesNow, let's pull in what we learned about subnetwork selection and apply it here...<jupyter_code>top_mut=brc_mut_network.dropna().sort_values(by=["mut_count"],ascending=False)
top_mut=top_mut.index.tolist()[:2]
p4c.select_nodes(nodes=top_nodes)
p4c.diffusion_basic()
p4c.create_subnetwork(nodes="selected", subnetwork_name="top mutated diffusion")
p4c.layout_network('force-directed', 'top disease diffusion')<jupyter_output><empty_output><jupyter_text>The top mutated genes are based on TCGA data and the diffusion algorithm is operating based on the network connectivity from STRING data, leading to a focused subnetwork view of critical Breast Cancer genes with mean patient expression data mapped to fill color. Now *that's* data integration!**Pro-tip: You can generate a legend for this in Cytoscape Style tab > Options > Create style...   Ovarian Cancer DatsetBut what about the other network and datasets? Do we have to repeat *all* of those steps again?  Actually, no!First, let's switch back over to the Ovarian Cancer network and load our data.<jupyter_code>wget.download('https://raw.githubusercontent.com/cytoscape/cytoscape-automation/master/for-scripters/Python/data/ovc.expr.tsv')
wget.download('https://raw.githubusercontent.com/cytoscape/cytoscape-automation/master/for-scripters/Python/data/ovc.mut.tsv')
ovc_expr=pd.read_table("./ovc.expr.tsv")
ovc_mut=pd.read_table("./ovc.mut.tsv")
p4c.set_current_network("String Network - ovarian cancer")
p4c.load_table_data(ovc_expr, table_key_column='display name')
p4c.load_table_data(ovc_mut, data_key_column='Hugo_Symbol', table_key_column='display name')<jupyter_output><empty_output><jupyter_text>**Because we used the same column names in our original data frames, now we can simply apply the *same* visual style created above!**<jupyter_code>p4c.set_visual_style('dataStyle')
p4c.clear_selection(type='both')<jupyter_output><empty_output><jupyter_text>Reusing the same style for both breast and ovarian cancers, we can compare the relative expression and mutation counts across the two datasets. For example, notice in the case of ovarian cancer: **decreased** range of mean expression and **fewer** mega-mutated genes.  Step 4: Saving, sharing and publishing Saving a Cytoscape session fileSession files save *everything*. As with most project software, we recommend saving often! **Note: If you don't specify a complete path, the files will be save relative to your Cytoscape installation directory, e.g., /Applications/Cytoscape_v3.6.0/... or somewhere you don't have write permissions. So, it's probably better to specify one...**<jupyter_code>p4c.save_session('cytoscape') # Save current session as cytoscape.cys<jupyter_output><empty_output><jupyter_text>Exporting high resolution image filesYou can export extremely high resolution images, including vector graphic formats.<jupyter_code>p4c.clear_selection(type='both')
p4c.fit_content()
p4c.export_image('cytoscape_image', type='PDF')<jupyter_output><empty_output><jupyter_text>Exporting interactive web pages via cytoscape.jsYou can export the network model in multiple standard formats. And if you want to get really fancy, you can export the model and style in cytoscape.js formats and host interactive views of your network analysis and visualization results online!<jupyter_code>p4c.export_network('cytoscape_network','cyjs')
p4c.export_visual_styles('cytoscape_style', type='json') # export the current style in cytoscape.js format<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10481099656357389, 'fraction_numerical': 0.01052405498281787, 'mean_word_length': 8.035575679172057, 'pattern_counts': {'":': 0, '<': 119, '<?xml version=': 0, '>': 122, 'https://': 9, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '29', 'dir': 'jupyter-structured-clean-dedup', 'id': '334558', 'license': 'CC0-1.0', 'n_tokens_mistral': 4743, 'n_tokens_neox': 4527, 'n_words': 1083, 'path': 'for-scripters/Python/advanced-cancer-networks-and-data-new.ipynb', 'repo_name': 'kozo2/cytoscape-automation', 'size': '13975'}