In this section, you can learn how to import network data from various types of file format, for example local file, URLs, web services, and your creating data.
In py2cytoscape, there are some supported formats that you can import.
In this section, we load data from various type of format and data type. In addition to supported formats that we menthion above, we will import some python's data format data and update network table.
In [5]:
# import
from py2cytoscape.data.cyrest_client import CyRestClient
# Create REST client for Cytoscape
cy = CyRestClient()
# Reset current session for fresh start
cy.session.delete()
# Empty network
empty1 = cy.network.create()
# With name
empty2 = cy.network.create(name='Created in Jupyter Notebook')
# With name and collection name
empty3 = cy.network.create(name='Also created in Jupyter', collection='New network collection')
In [6]:
# import data from url
from py2cytoscape.data.cyrest_client import CyRestClient
import pandas as pd
from IPython.display import Image
# Create REST client for Cytoscape
cy = CyRestClient()
# Reset current session for fresh start
cy.session.delete()
# Load a sample network
#network1 = cy.network.create_from('http://chianti.ucsd.edu/~kono/data/galFiltered.sif')
# Load a single local file
network2 = cy.network.create_from('../sampleData/galFiltered.json')
network3 = cy.network.create_from('../sampleData/sample_yeast_network.xgmml', collection='My Collection')
network4 = cy.network.create_from('../sampleData/galFiltered.gml', collection='My Collection')
# Load from multiple locations
network_locations = [
# File1
'../sampleData/galFiltered.json',
# File2
'../sampleData/sample_yeast_network.xgmml',
# File3
'../sampleData/galFiltered.gml'
]
# This requrns Series
networks = cy.network.create_from(network_locations)
pd.DataFrame(networks, columns=['CyNetwork'])
Out[6]:
In [3]:
# Apply layout to the cytoscape network object
cy.layout.apply(network = network2)
# Show it!!
Image(network2.get_png(height=400))
Out[3]:
In [3]:
# import
from py2cytoscape.data.cyrest_client import CyRestClient
import py2cytoscape.util.cytoscapejs as cyjs
from IPython.display import Image
# Create REST client for Cytoscape
cy = CyRestClient()
# Reset current session for fresh start
cy.session.delete()
# Cytoscape.js JSON
network = cy.network.create(data=cyjs.get_empty_network(), name='Created from Cytoscape.js JSON')
# You can get empty network
Image(network.get_png(height=400))
Out[3]:
In [84]:
# import
from py2cytoscape.data.cyrest_client import CyRestClient
import networkx as nx
from IPython.display import Image
# Create REST client for Cytoscape
cy = CyRestClient()
# Reset current session for fresh start
cy.session.delete()
# Create NetworkX data object
nx_graph = nx.scale_free_graph(100)
# Generate cytoscape network object from networkx
scale_free100 = cy.network.create_from_networkx(nx_graph, collection='Generated by NetworkX')
# Apply layout to the cytoscape network object
cy.layout.apply(network = scale_free100)
# Show it!!
Image(scale_free100.get_png(height=400))
Out[84]:
In [3]:
# import
from py2cytoscape.data.cyrest_client import CyRestClient
import pandas as pd
from IPython.display import Image
# Create REST client for Cytoscape
cy = CyRestClient()
# Reset current session for fresh start
cy.session.delete()
# Import Pandas DataFrame from a simple text table
df_from_sif = pd.read_csv('../sampleData/galFiltered.sif', names=['source', 'interaction', 'target'], sep=' ')
# Get the network object
network1 = cy.network.create_from_dataframe(df_from_sif)
# Apply layout
cy.layout.apply(network = network1)
# Show it!!
Image(network1.get_png(height=400))
Out[3]:
In [35]:
# import
from py2cytoscape.data.cyrest_client import CyRestClient
import igraph as ig
from IPython.display import Image
# Create REST client for Cytoscape
cy = CyRestClient()
# Reset current session for fresh start
cy.session.delete()
# igraph
ig_data = ig.Graph.Tree(400, 5)
# Get the network object from igraph data
network_igraph = cy.network.create_from_igraph(ig_data)
# Apply layout to network
cy.layout.apply(network = network_igraph)
# Show it!!
Image(network_igraph.get_png(height=400))
Out[35]:
In [29]:
# Import
from py2cytoscape.data.cyrest_client import CyRestClient
import numpy as np
from IPython.display import Image
# Create REST client for Cytoscape
cy = CyRestClient()
# Reset current session for fresh start
cy.session.delete()
# Prepare ndarray data
matrix1 = np.array([
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
])
# Generate cytoscape network obejct from ndarray
net1 = cy.network.create_from_ndarray(matrix1, name='binary sample')
# Apply layout
cy.layout.apply(network=net1)
cy.layout.fit(network=net1)
# Show it!!
Image(net1.get_png(height=400))
Out[29]:
In [2]:
# Small utility function to convert ID sets
from py2cytoscape.data.cyrest_client import CyRestClient
from util.util_uniprot import *
import pandas as pd
from IPython.display import Image
# Create REST client for Cytoscape
cy = CyRestClient()
# Reset current session for fresh start
cy.session.delete()
# Load data from a simple text table as pandas data format
df_from_sif = pd.read_csv('../sampleData/galFiltered.sif', names=['source', 'interaction', 'target'], sep=' ')
# Create network from pandas data frame
network = cy.network.create_from_dataframe(df_from_sif, name='Yeast network created from pandas DataFrame')
# Apply layout
cy.layout.apply(network=network)
#Fit an existing network view to current window.
cy.layout.fit(network=network)
# Show it!!
Image(network.get_png(height=400))
network
Out[2]:
In [22]:
# Get node table from Cytoscape
network_node_table = network.get_node_table()
# Show it
network_node_table.head()
Out[22]:
In [18]:
# From KEGG ID to UniprotKB ID
query1 = ' '.join(network_node_table['name'].map(lambda gene_id: 'sce:' + gene_id).values)
id_map_kegg2uniprot = uniprot_id_mapping_service(query1, from_id='KEGG_ID', to_id='ID')
id_map_kegg2uniprot.columns = ['kegg', 'uniprot']
# Show it
id_map_kegg2uniprot.head()
Out[18]:
In [19]:
# From UniprotKB to SGD
query2 = ' '.join(id_map_kegg2uniprot['uniprot'].values)
id_map_uniprot2sgd = uniprot_id_mapping_service(query2, from_id='ID', to_id='SGD_ID')
id_map_uniprot2sgd.columns = ['uniprot', 'sgd']
# Show it
id_map_uniprot2sgd.head()
Out[19]:
In [20]:
# From UniprotKB to Entrez Gene ID
query3 = ' '.join(id_map_kegg2uniprot['uniprot'].values)
id_map_uniprot2ncbi = uniprot_id_mapping_service(query3, from_id='ID', to_id='P_ENTREZGENEID')
id_map_uniprot2ncbi.columns = ['uniprot', 'entrez']
# Show it
id_map_uniprot2ncbi.head()
Out[20]:
In [21]:
# Merge them
merged = pd.merge(id_map_kegg2uniprot, id_map_uniprot2sgd, on='uniprot')
merged = pd.merge(merged, id_map_uniprot2ncbi, on='uniprot')
# Show it
merged.head()
Out[21]:
In [23]:
# Add key column by removing prefix
merged['name'] = merged['kegg'].map(lambda kegg_id : kegg_id[4:])
# Show it
merged.head()
Out[23]:
In [26]:
# Now update existing node table with the data frame above.
network.update_node_table(merged, network_key_col='name', data_key_col='name')
# Check the table is actually updated
network.get_node_table().head()
Out[26]: