Search code examples
ubuntugoogle-colaboratorycytoscape

How do I install Cytoscape on Google Colab?


I'd like to use Cytoscape.js (https://js.cytoscape.org/) on Google Colab. I found here (https://py2cytoscape.readthedocs.io/en/latest/) that this can be done in Jupyter Notebooks, but the documentation isn't very thorough.

Here is the code I ran to try to set up Cytoscape and CyREST (paste each block of code here into a separate cell in Google Colab):

%%shell
# Install dependencies
pip install py2cytoscape
pip install dash dash-html-components
pip install dash-cytoscape
apt install g++ make libxml2-dev python-dev python3-dev zlib1g-dev

# Clone Cytoscape from Git and install
cd /usr/local/envs/cytoscape
wget https://github.com/cytoscape/cytoscape/releases/download/3.8.0/Cytoscape_3_8_0_unix.sh
chmod u+x  Cytoscape_3_8_0_unix.sh
sudo sh Cytoscape_3_8_0_unix.sh -q

# Run Cytoscape to check that everything was installed correctly
Cytoscape &
# Start Cytoscape
!Cytoscape &

from py2cytoscape.data.cyrest_client import CyRestClient

cy = CyRestClient()
network = cy.network.create(name='My Network', collection='My network collection')
print(network.get_id())

This gave me the following error:

---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/urllib3/connection.py in _new_conn(self)
    158             conn = connection.create_connection(
--> 159                 (self._dns_host, self.port), self.timeout, **extra_kw)
    160 

23 frames
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

NewConnectionError                        Traceback (most recent call last)
NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f822bd80860>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

MaxRetryError                             Traceback (most recent call last)
MaxRetryError: HTTPConnectionPool(host='localhost', port=1234): Max retries exceeded with url: /v1/styles/visualproperties (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f822bd80860>: Failed to establish a new connection: [Errno 111] Connection refused',))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    514                 raise SSLError(e, request=request)
    515 
--> 516             raise ConnectionError(e, request=request)
    517 
    518         except ClosedPoolError as e:

ConnectionError: HTTPConnectionPool(host='localhost', port=1234): Max retries exceeded with url: /v1/styles/visualproperties (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f822bd80860>: Failed to establish a new connection: [Errno 111] Connection refused',))

I found this piece of code somewhere that is supposed to help with starting CyREST, but running it gave me the exact same error as above (which I don't really understand)

REST_PORT = '1234' # Set to whatever rest.port is in Cytoscape preferences
REST_ENDPOINT = "http://localhost:{}/v1".format(REST_PORT)
import requests
import json
import os
from urllib.request import urlretrieve
from IPython.display import Image, display, HTML

response = requests.get(REST_ENDPOINT)
js_resp = response.json()
assert js_resp['apiVersion'] == 'v1', \
    "This notebook uses CyREST API v1, but version {} was found.".format(js_resp['apiVersion'])

print("CyREST v1 is running!")

My guess is that this is a problem with the ports in the Google Colab Ubuntu environment, but I'm not sure how to fix it.


Solution

  • For anyone else who runs into this issue, I learned that instead of using this approach, an easier one is to use ipycytoscape, which makes it much easier to get everything running.

    However, the problem with both this approach and the one I described in the question is that Cytoscape relies on some ipywidgets for output, and these aren't supported in Google Colab yet.

    You can follow the progress of this issue here: https://github.com/googlecolab/colabtools/issues/60