Analyse coexpression with MCODE

Open In Colab

Yasir Demirtaş, Kozo Nishida, Barry Demchak

py4cytoscape 0.0.10

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.10), remove the _PY4CYTOSCAPE= line in the snippet below.)

[ ]:
_PY4CYTOSCAPE = 'git+https://github.com/cytoscape/py4cytoscape@0.0.10'
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
[5]:
!pip install concurrent-log-handler
!pip install openpyxl
Requirement already satisfied: concurrent-log-handler in c:\users\hoge\miniforge3\lib\site-packages (0.9.19)
Requirement already satisfied: portalocker>=1.4.0 in c:\users\hoge\miniforge3\lib\site-packages (from concurrent-log-handler) (1.7.0)
Requirement already satisfied: pywin32!=226 in c:\users\hoge\miniforge3\lib\site-packages (from portalocker>=1.4.0->concurrent-log-handler) (301)
[1]:
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'
[16]:
p4c.install_app('MCODE')
{}
[16]:
{}

Clear all networks at the begining

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

Import Co-Exp Data and Rename columns

[2]:
results = pd.read_excel('https://github.com/cytoscape/py4cytoscape/raw/0.0.10/doc/tutorials/input_data/co_exp_result.xlsx')
[3]:
results
[3]:
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

[4]:
results = results.rename(columns={'gene1': 'source', 'gene2': 'target'})
[5]:
results
[5]:
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

[6]:
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
[7]:
p4c.notebook_show_image()
[7]:
../_images/tutorials_analyse_coexp_with_mcode_14_0.png

Run analyze command so your network will be one way directed

[8]:
analyze = p4c.commands.commands_post('analyzer/analyze')
[9]:
analyze
[9]:
{'networkTitle': 'a4e2797fa98546b6b878e4f5099e1fdc (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.082'}

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

[10]:
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)
[11]:
mcode
[11]:
{'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': 138,
   'nodes': [64,
    96,
    98,
    99,
    100,
    196,
    102,
    103,
    105,
    106,
    138,
    81,
    83,
    179,
    116,
    86,
    89,
    90,
    93,
    95]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 9.916666666666666,
   'seedNode': 143,
   'nodes': [68,
    69,
    70,
    74,
    79,
    143,
    80,
    82,
    84,
    87,
    88,
    91,
    92,
    94,
    104,
    108,
    172,
    173,
    174,
    176,
    177,
    114,
    178,
    62,
    126]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 8.842105263157894,
   'seedNode': 180,
   'nodes': [161,
    162,
    230,
    73,
    144,
    145,
    146,
    147,
    148,
    180,
    149,
    150,
    151,
    153,
    217,
    155,
    156,
    157,
    158,
    159]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 5.666666666666667,
   'seedNode': 227,
   'nodes': [227, 197, 134, 198, 199, 136, 200, 137, 201, 233, 202, 75, 152]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 5.666666666666667,
   'seedNode': 123,
   'nodes': [112, 118, 121, 123, 124, 125, 110]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 5.0,
   'seedNode': 242,
   'nodes': [187, 133, 213, 214, 215]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 5.0,
   'seedNode': 142,
   'nodes': [66, 139, 140, 141, 142]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 4.0,
   'seedNode': 115,
   'nodes': [97, 115, 101, 117, 182, 171, 63]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 4.0,
   'seedNode': 163,
   'nodes': [163, 164, 165, 166]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 3.0,
   'seedNode': 252,
   'nodes': [252, 249, 190]},
  {'rank': 0,
   'name': 'Cluster 0',
   'score': 3.0,
   'seedNode': 206,
   'nodes': [208, 209, 206]}],
 'stale': False,
 'lock': {}}

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

[12]:
big_clusters = [i for i in range(len(mcode['clusters'])) if len(mcode['clusters'][i]['nodes']) >= 20]
[13]:
big_clusters
[13]:
[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

[14]:
p4c.get_table_column_names()
[14]:
['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)']
[15]:
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')
[17]:
img_names
[17]:
['a2683fbd40324fcdb6c1fbab9b931a06.png',
 '59e2d4909be74f938fb045c1bec2b6c7.png',
 'a10d252d6cd346f5a948d526a29cf1bf.png']
[20]:
p4c.notebook_show_image(img_names[0])
[20]:
../_images/tutorials_analyse_coexp_with_mcode_28_0.png
[21]:
p4c.notebook_show_image(img_names[1])
[21]:
../_images/tutorials_analyse_coexp_with_mcode_29_0.png
[22]:
p4c.notebook_show_image(img_names[2])
[22]:
../_images/tutorials_analyse_coexp_with_mcode_30_0.png
[ ]: