In [1]:
import random
import hetnetpy.readwrite
import hetnetpy.pathtools
import hetnetpy.stats
In [2]:
# Read Hetionet v1.0
url = 'https://github.com/hetio/hetionet/raw/{}/{}'.format(
'00bf0b6f8886821d91cfdf00eadad145a7a1b6da',
'hetnet/json/hetionet-v1.0.json.bz2',
)
graph = hetnetpy.readwrite.read_graph(url)
metagraph = graph.metagraph
In [3]:
# Specify compound and disease
compound_id = 'Compound', 'DB01156' # Bupropion
disease_id = 'Disease', 'DOID:0050742' # nicotine dependences
metapath = metagraph.metapath_from_abbrev('CbGpPWpGaD')
In [4]:
# Extract walks from compound to disease
paths = hetnetpy.pathtools.paths_between(
graph,
source=graph.node_dict[compound_id],
target=graph.node_dict[disease_id],
metapath=metapath,
duplicates=True,
)
In [5]:
metaedges = set(metapath)
nodes = set()
for path in paths:
nodes.update(path.get_nodes())
for edge in path:
# Add incidental nodes along paths to enable correct DWWC/DWPC computations
# for the CbGpPWpGaD metapath
nodes.update(e.target for e in edge.source.get_edges(edge.metaedge))
nodes.update(e.target for e in edge.target.get_edges(edge.metaedge.inverse))
# Add Gene-interacts-Gene metaedge (not essential but may be useful)
metaedges.add(metagraph.metapath_from_abbrev('GiG')[0])
# Get subgraph
subgraph = graph.get_subgraph(metaedges=metaedges, nodes=nodes)
In [6]:
# Metagraph size
subgraph.metagraph.n_nodes, subgraph.metagraph.n_edges
Out[6]:
In [7]:
# Graph size
subgraph.n_nodes, subgraph.n_edges
Out[7]:
In [8]:
# Metanode info
hetnetpy.stats.get_metanode_df(subgraph)
Out[8]:
In [9]:
# Metaedge info
hetnetpy.stats.get_metaedge_df(subgraph)
Out[9]:
In [10]:
# Export as JSON
hetnetpy.readwrite.write_graph(subgraph, 'bupropion-CbGpPWpGaD-subgraph.json.xz')
In [11]:
metanode_to_nodes = graph.get_metanode_to_nodes()
n_nodes = 100
node_subset = list()
random.seed(0, version=2)
for metanode, nodes in sorted(metanode_to_nodes.items()):
nodes = sorted(nodes)
nodes = random.sample(nodes, n_nodes)
node_subset.extend(nodes)
n_nodes += 1
In [12]:
# Get subgraph
subgraph = graph.get_subgraph(nodes=node_subset)
In [13]:
# Metagraph size
subgraph.metagraph.n_nodes, subgraph.metagraph.n_edges
Out[13]:
In [14]:
# Graph size
subgraph.n_nodes, subgraph.n_edges
Out[14]:
In [15]:
# Metanode info
hetnetpy.stats.get_metanode_df(subgraph)
Out[15]:
In [16]:
# Metaedge info
hetnetpy.stats.get_metaedge_df(subgraph)
Out[16]:
In [17]:
# Export as JSON
hetnetpy.readwrite.write_graph(subgraph, 'random-subgraph.json.xz')