Analyse coexpression with MCODE

Open In Colab

Yasir Demirtaş, Kozo Nishida, Barry Demchak

py4cytoscape 0.0.11

Prerequisites

In addition to this package (py4cytoscape), you will need:

  • Cytoscape 3.8 or greater, which can be downloaded from https://cytoscape.org/download.html. Simply follow the installation instructions on screen.

  • Complete installation wizard

  • Launch Cytoscape

  • If your Cytoscape is 3.8.2 or earlier, install FileTransfer App (Follow here to do it.)

NOTE: To run this notebook, you must manually start Cytoscape first – don’t proceed until you have started Cytoscape.

Setup required only in a remote notebook environment

If you’re using a remote Jupyter Notebook environment such as Google Colab, run the cell below. (If you’re running a local Jupyter Notebook server on the desktop machine same with Cytoscape, you don’t need to do that.)

(Note that to use the current py4cytoscape release (instead of v0.0.11), remove the _PY4CYTOSCAPE= line in the snippet below.)

[1]:
_PY4CYTOSCAPE = 'git+https://github.com/cytoscape/py4cytoscape@0.0.11'
import requests
exec(requests.get("https://raw.githubusercontent.com/cytoscape/jupyter-bridge/master/client/p4c_init.py").text)
IPython.display.Javascript(_PY4CYTOSCAPE_BROWSER_CLIENT_JS) # Start browser client
Collecting git+https://github.com/cytoscape/py4cytoscape@0.0.11
  Cloning https://github.com/cytoscape/py4cytoscape (to revision 0.0.11) to /tmp/pip-req-build-3g5792n9
  Running command git clone -q https://github.com/cytoscape/py4cytoscape /tmp/pip-req-build-3g5792n9
  Running command git checkout -b 0.0.11 --track origin/0.0.11
  Switched to a new branch '0.0.11'
  Branch '0.0.11' set up to track remote branch '0.0.11' from 'origin'.
Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from py4cytoscape==0.0.11) (1.1.5)
Requirement already satisfied: networkx in /usr/local/lib/python3.7/dist-packages (from py4cytoscape==0.0.11) (2.6.2)
Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from py4cytoscape==0.0.11) (2.23.0)
Collecting python-igraph
  Downloading python_igraph-0.9.6-cp37-cp37m-manylinux2010_x86_64.whl (3.2 MB)
Collecting colorbrewer
  Downloading colorbrewer-0.2.0-py3-none-any.whl (9.4 kB)
Requirement already satisfied: chardet in /usr/local/lib/python3.7/dist-packages (from py4cytoscape==0.0.11) (3.0.4)
Requirement already satisfied: decorator in /usr/local/lib/python3.7/dist-packages (from py4cytoscape==0.0.11) (4.4.2)
Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from colorbrewer->py4cytoscape==0.0.11) (1.15.0)
Requirement already satisfied: numpy>=1.15.4 in /usr/local/lib/python3.7/dist-packages (from pandas->py4cytoscape==0.0.11) (1.19.5)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas->py4cytoscape==0.0.11) (2.8.2)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas->py4cytoscape==0.0.11) (2018.9)
Collecting texttable>=1.6.2
  Downloading texttable-1.6.4-py2.py3-none-any.whl (10 kB)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->py4cytoscape==0.0.11) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->py4cytoscape==0.0.11) (2021.5.30)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->py4cytoscape==0.0.11) (1.24.3)
Building wheels for collected packages: py4cytoscape
  Building wheel for py4cytoscape (setup.py): started
  Building wheel for py4cytoscape (setup.py): finished with status 'done'
  Created wheel for py4cytoscape: filename=py4cytoscape-0.0.11-py3-none-any.whl size=159396 sha256=22027916c1648ecce03f7711cfc08ad5cb65eac3041aa3fc88e1eb11aefb7015
  Stored in directory: /tmp/pip-ephem-wheel-cache-5fw2l66k/wheels/07/82/a1/94122065caf1c697891340d02661b7b47a82e2d8af2477dd84
Successfully built py4cytoscape
Installing collected packages: texttable, python-igraph, colorbrewer, py4cytoscape
Successfully installed colorbrewer-0.2.0 py4cytoscape-0.0.11 python-igraph-0.9.6 texttable-1.6.4
Loading Javascript client ... 8028b9f5-f88d-4e08-9596-a107d4b31c3a on https://jupyter-bridge.cytoscape.org
ADVICE: WHEN RUNNING UNDER COLAB, DO NOT RE-RUN THIS CELL WITHOUT MANUALLY EXECUTING Runtime | Factory Reset Runtime FROM THE COLAB MENU FIRST.
[1]:
[2]:
!pip install concurrent-log-handler
!pip install openpyxl
Collecting concurrent-log-handler
  Downloading concurrent_log_handler-0.9.19-py2.py3-none-any.whl (18 kB)
Collecting portalocker>=1.4.0
  Downloading portalocker-2.3.2-py2.py3-none-any.whl (15 kB)
Installing collected packages: portalocker, concurrent-log-handler
Successfully installed concurrent-log-handler-0.9.19 portalocker-2.3.2
Requirement already satisfied: openpyxl in /usr/local/lib/python3.7/dist-packages (2.5.9)
Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.7/dist-packages (from openpyxl) (1.1.0)
Requirement already satisfied: jdcal in /usr/local/lib/python3.7/dist-packages (from openpyxl) (1.4.1)
[61]:
import py4cytoscape as p4c
import pandas as pd
import os
import uuid
from logging import getLogger, INFO
from concurrent_log_handler import ConcurrentRotatingFileHandler
from IPython.display import Image
from time import sleep
%config InlineBackend.figure_format = 'svg'
[62]:
p4c.cytoscape_version_info()
[62]:
{'apiVersion': 'v1',
 'cytoscapeVersion': '3.8.2',
 'automationAPIVersion': '1.3.0',
 'py4cytoscapeVersion': '0.0.11'}
[63]:
p4c.install_app('MCODE')
{}
[63]:
{}

Clear all networks at the begining

[66]:
p4c.session.close_session(False)
[66]:
{}

Import Co-Exp Data and Rename columns

[67]:
results = pd.read_excel('https://github.com/cytoscape/py4cytoscape/raw/0.0.11/doc/tutorials/input_data/co_exp_result.xlsx')
[68]:
results
[68]:
gene1 gene2 score
0 ACSM3 COL6A2 -0.518721
1 ACSM3 DPYSL3 -0.504664
2 ACSM3 THY1 -0.494217
3 ADAM28 AL928768.3 /// Ig alpha 1-[alpha]2m /// IGH 0.553792
4 ADH1B BUB1B -0.565257
... ... ... ...
1117 THBS2 THY1 0.691359
1118 THBS2 VCAN 0.630551
1119 THBS4 THY1 0.530471
1120 THY1 VCAN 0.510101
1121 TOP2A UBE2C 0.656060

1122 rows × 3 columns

[69]:
results = results.rename(columns={'gene1': 'source', 'gene2': 'target'})
[70]:
results
[70]:
source target score
0 ACSM3 COL6A2 -0.518721
1 ACSM3 DPYSL3 -0.504664
2 ACSM3 THY1 -0.494217
3 ADAM28 AL928768.3 /// Ig alpha 1-[alpha]2m /// IGH 0.553792
4 ADH1B BUB1B -0.565257
... ... ... ...
1117 THBS2 THY1 0.691359
1118 THBS2 VCAN 0.630551
1119 THBS4 THY1 0.530471
1120 THY1 VCAN 0.510101
1121 TOP2A UBE2C 0.656060

1122 rows × 3 columns

Create network with co-exp data and give random network name

[71]:
network_name = uuid.uuid4().hex
network_suid = p4c.networks.create_network_from_data_frames(
    edges=results,
    title= network_name,
    collection= network_name + '_collection'
)
Applying default style...
Applying preferred layout
[72]:
p4c.notebook_export_show_image()
[72]:
../_images/tutorials_analyse_coexp_with_mcode_15_0.png

Run analyze command so your network will be one way directed

[73]:
p4c.commands.commands_post('analyzer/version')
[73]:
{'version': '4.4.8'}
[74]:
analyze = p4c.commands.commands_post('analyzer/analyze')
[75]:
analyze
[75]:
{'networkTitle': 'd91589510bdd4df785c3d0c72d1dc166 (undirected)',
 'nodeCount': '197',
 'edgeCount': '1122',
 'avNeighbors': '11.86096256684492',
 'diameter': '8',
 'radius': '5',
 'avSpl': '3.271634753608188',
 'cc': '0.5249889291420875',
 'density': '0.06376861595077914',
 'heterogeneity': '0.8353654292197396',
 'centralization': '0.16378959604766055',
 'ncc': '4',
 'time': '0.03'}

Run mcode command with options (you can change network settings)

[76]:
p4c.commands.commands_help('mcode')
[76]:
['cluster', 'view']
[77]:
p4c.commands.commands_help('mcode cluster')
[77]:
['degreeCutoff',
 'fluff',
 'fluffNodeDensityCutoff',
 'haircut',
 'includeLoops',
 'kCore',
 'maxDepthFromStart',
 'network',
 'nodeScoreCutoff',
 'scope']
[78]:
mcode = p4c.commands.commands_post('mcode cluster degreeCutoff=2 fluff=false fluffNodeDensityCutoff=0.1 haircut=true includeLoops=false kCore=2 maxDepthFromStart=100 network=current nodeScoreCutoff=0.2 scope=NETWORK')
sleep(5)
[79]:
mcode
[79]:
{'id': 1,
 'parameters': {'scope': 'NETWORK',
  'includeLoops': False,
  'degreeCutoff': 2,
  'kCore': 2,
  'maxDepthFromStart': 100,
  'nodeScoreCutoff': 0.2,
  'haircut': True,
  'fluff': False,
  'fluffNodeDensityCutoff': 0.1,
  'selectedNodes': []},
 'clusters': [{'rank': 0,
   'name': 'Cluster 0',
   'score': 17.263157894736842,
   'seedNode': 3963,
   'nodes': [3906,
    3908,
    4004,
    3941,
    3911,
    3914,
    3915,
    3918,
    3920,
    3889,
    3921,
    3923,
    3924,
    3925,
    4021,
    3927,
    3928,
    3930,
    3931,
    3963]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 9.916666666666666,
   'seedNode': 3968,
   'nodes': [3904,
    3968,
    3905,
    3907,
    3909,
    3912,
    3913,
    3916,
    3917,
    3919,
    3929,
    3933,
    3997,
    3998,
    3999,
    4001,
    4002,
    3939,
    4003,
    3887,
    3951,
    3893,
    3894,
    3895,
    3899]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 8.842105263157894,
   'seedNode': 4005,
   'nodes': [3969,
    3970,
    3971,
    3972,
    3973,
    4005,
    3974,
    3975,
    3976,
    3978,
    4042,
    3980,
    3981,
    3982,
    3983,
    3984,
    3986,
    3987,
    4055,
    3898]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 5.666666666666667,
   'seedNode': 4052,
   'nodes': [3977,
    4052,
    4022,
    3959,
    4023,
    4024,
    3961,
    4025,
    3962,
    4026,
    4058,
    4027,
    3900]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 5.666666666666667,
   'seedNode': 3948,
   'nodes': [3937, 3943, 3946, 3948, 3949, 3950, 3935]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 5.0,
   'seedNode': 4067,
   'nodes': [4040, 4012, 3958, 4038, 4039]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 5.0,
   'seedNode': 3967,
   'nodes': [3891, 3964, 3965, 3966, 3967]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 4.0,
   'seedNode': 3940,
   'nodes': [3888, 3922, 3940, 3926, 3942, 4007, 3996]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 4.0,
   'seedNode': 3988,
   'nodes': [3988, 3989, 3990, 3991]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 3.0,
   'seedNode': 4077,
   'nodes': [4077, 4074, 4015]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 3.0,
   'seedNode': 4031,
   'nodes': [4033, 4034, 4031]}],
 'stale': False,
 'lock': {}}

Mcode created clusters, we need big clusters which is node count bigger than 20 (you can change threshold)

[80]:
big_clusters = [i for i in range(len(mcode['clusters'])) if len(mcode['clusters'][i]['nodes']) >= 20]
[81]:
big_clusters
[81]:
[0, 1, 2]

Create empty list and append cluster tables to in it

Create cluster images as a png and network file as a .cyjs so you are able to use in cytoscape.js

[82]:
p4c.get_table_column_names()
[82]:
['SUID',
 'shared name',
 'name',
 'selected',
 'id',
 'AverageShortestPathLength',
 'ClusteringCoefficient',
 'ClosenessCentrality',
 'IsSingleNode',
 'PartnerOfMultiEdgedNodePairs',
 'SelfLoops',
 'Eccentricity',
 'Stress',
 'Degree',
 'BetweennessCentrality',
 'NeighborhoodConnectivity',
 'NumberOfDirectedEdges',
 'NumberOfUndirectedEdges',
 'Radiality',
 'TopologicalCoefficient',
 'MCODE::Score (1)',
 'MCODE::Node Status (1)',
 'MCODE::Clusters (1)']
[83]:
clusters = []
img_names = []
for i in range(len(big_clusters)):
    view_id = p4c.commands.commands_post('mcode view id=1 rank=' + str(i+1) )
    clusters.append(p4c.tables.get_table_columns() \
                  .drop(columns=['id','name','selected','MCODE::Clusters (1)','SUID']) \
                  .sort_values(by=['MCODE::Score (1)'], ascending=False) \
                 .reset_index(drop=True) )

    p4c.network_views.get_network_view_suid()
    img_name = uuid.uuid4().hex
    p4c.export_image(str(img_name))
    p4c.export_network(img_name,'cyjs')
    img_names.append(img_name+'.png')
[84]:
img_names
[84]:
['adf62ca4886248228abdcca71e0477d4.png',
 '8b97eebbcf874104b009b27af429ee7c.png',
 'c04e8386ba944ab7bb2364227dbb6eea.png']
[85]:
p4c.notebook_show_image(img_names[0])
[85]:
../_images/tutorials_analyse_coexp_with_mcode_32_0.png
[86]:
p4c.notebook_show_image(img_names[1])
[86]:
../_images/tutorials_analyse_coexp_with_mcode_33_0.png
[87]:
p4c.notebook_show_image(img_names[2])
[87]:
../_images/tutorials_analyse_coexp_with_mcode_34_0.png
[23]: