Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

46 expand tests #47

Merged
merged 42 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4959994
updated vignette with new panacea loaders
vicpaton Aug 5, 2024
bd88b77
fixed bug
vicpaton Aug 5, 2024
7686798
removed moon reg function
vicpaton Aug 5, 2024
40bc78d
renamed import
vicpaton Aug 6, 2024
44115aa
removed support for undirected graphs
vicpaton Aug 6, 2024
802d8c1
disabled lazy import
vicpaton Aug 6, 2024
1c1d69b
removed superfluous import
vicpaton Aug 6, 2024
021d75e
added EOL spacing
vicpaton Aug 6, 2024
ea3be70
expanded tests for moon
vicpaton Aug 6, 2024
52f019f
added tests for omics utils
vicpaton Aug 6, 2024
27271ec
added tests for prior knowledge
vicpaton Aug 6, 2024
cffc045
added tests
vicpaton Aug 6, 2024
3505620
removed unused import
vicpaton Aug 6, 2024
c524633
added tests
vicpaton Aug 6, 2024
d9fbc71
added reset index
vicpaton Aug 6, 2024
d57d06a
fixed bug
vicpaton Aug 6, 2024
18815c6
added tests for cptac_datasets
vicpaton Aug 6, 2024
fc0fd31
removed type_checking since it was preventing successful import
vicpaton Aug 6, 2024
a98cfba
remove rounding
vicpaton Aug 6, 2024
b8bb253
expanded tests for deseq2
vicpaton Aug 6, 2024
47683e5
removed descryptm raw-file dowload (not in nc anyways)
vicpaton Aug 6, 2024
0e540c3
added tests
vicpaton Aug 6, 2024
db69387
added tests
vicpaton Aug 6, 2024
068e82f
renamed nci60 data
vicpaton Aug 6, 2024
a32c712
added tests for panacea
vicpaton Aug 6, 2024
fc81761
added fast tests for scperturb
vicpaton Aug 6, 2024
87d239e
removed support for undirected graphs
vicpaton Aug 7, 2024
e46c33b
linted code
vicpaton Aug 7, 2024
f810485
added tests for metrics
vicpaton Aug 7, 2024
c8a2801
redid try/except chunk
vicpaton Aug 8, 2024
ea01ea6
added tests
vicpaton Aug 8, 2024
b28da56
added error capture for invalid types
vicpaton Aug 8, 2024
822222b
reformulated ifelse chunk
vicpaton Aug 8, 2024
ad80399
added tests for error capturing
vicpaton Aug 8, 2024
a84b052
captured error and added log
vicpaton Aug 8, 2024
f5b7236
added tests
vicpaton Aug 8, 2024
0400567
removed build_moon_regulons
vicpaton Aug 8, 2024
3377bf1
removed unused function
vicpaton Aug 8, 2024
0fafc07
added tests for _commons module
vicpaton Aug 8, 2024
c0d392b
removed multigraph support from networkx
vicpaton Aug 8, 2024
ca3310b
redid ifelse statement for graph check
vicpaton Aug 8, 2024
c175674
added tests
vicpaton Aug 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 102 additions & 100 deletions docs/src/vignettes/1_simple_example.ipynb

Large diffs are not rendered by default.

35 changes: 21 additions & 14 deletions networkcommons/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,31 @@ def edge_attrs_from_corneto(graph: cn.Graph) -> pd.DataFrame:
concat_df.rename(columns={0: 'node'}, inplace=True)



def to_cornetograph(graph):
"""
Convert a networkx graph to a corneto graph, if needed.

Args:
graph (nx.Graph or nx.DiGraph): The corneto graph.
graph (nx.DiGraph): The corneto graph.

Returns:
cn.Graph: The corneto graph.
"""
if isinstance(graph, cn._graph.Graph):
if isinstance(graph, nx.MultiDiGraph):
raise NotImplementedError("Only nx.DiGraph graphs and corneto graphs are supported.")
elif isinstance(graph, cn.Graph):
corneto_graph = graph
elif isinstance(graph, (nx.Graph, nx.DiGraph)):
elif isinstance(graph, nx.DiGraph):
# substitute 'sign' for 'interaction' in the graph
nx_graph = graph.copy()
for u, v, data in nx_graph.edges(data=True):
data['interaction'] = data.pop('sign')

corneto_graph = cn_nx.networkx_to_corneto_graph(nx_graph)
elif isinstance(graph, nx.Graph):
raise NotImplementedError("Only nx.DiGraph graphs and corneto graphs are supported.")
else:
raise NotImplementedError("Only nx.DiGraph graphs and corneto graphs are supported.")

return corneto_graph

Expand All @@ -71,15 +76,21 @@ def to_networkx(graph, skip_unsupported_edges=True):
Returns:
nx.Graph: The networkx graph.
"""
if isinstance(graph, nx.Graph) or isinstance(graph, nx.DiGraph):
if isinstance(graph, nx.MultiDiGraph):
raise NotImplementedError("Only nx.DiGraph graphs and corneto graphs are supported.")
elif isinstance(graph, nx.DiGraph):
networkx_graph = graph
elif isinstance(graph, cn._graph.Graph):
elif isinstance(graph, cn.Graph):
networkx_graph = cn_nx.corneto_graph_to_networkx(
graph,
skip_unsupported_edges=skip_unsupported_edges)
# rename interaction for sign
for u, v, data in networkx_graph.edges(data=True):
data['sign'] = data.pop('interaction')
elif isinstance(graph, nx.Graph):
raise NotImplementedError("Only nx.DiGraph graphs and corneto graphs are supported.")
else:
raise NotImplementedError("Only nx.DiGraph graphs and corneto graphs are supported.")

return networkx_graph

Expand Down Expand Up @@ -116,8 +127,7 @@ def read_network_from_file(file_path,
def network_from_df(network_df,
source_col='source',
target_col='target',
directed=True,
multigraph=False):
directed=True):
"""
Create a network from a DataFrame.

Expand All @@ -132,9 +142,6 @@ def network_from_df(network_df,
"""
network_type = nx.DiGraph if directed else nx.Graph

if multigraph:
network_type = nx.MultiDiGraph if directed else nx.MultiGraph

if list(network_df.columns) == list([source_col, target_col]):
network = nx.from_pandas_edgelist(network_df,
source=source_col,
Expand Down Expand Up @@ -183,8 +190,8 @@ def decoupler_formatter(df,
Format dataframe to be used by decoupler.

Parameters:
df (DataFrame): A pandas DataFrame.
column (str): The column to be used as index.
df (DataFrame): A pandas DataFrame. Index should be populated
column (str): The columns to be subsetted.

Returns:
A formatted DataFrame.
Expand All @@ -211,7 +218,7 @@ def targetlayer_formatter(df, n_elements=25):

# Sort the DataFrame by the absolute value of the
# 'sign' column and get top n elements
df = df.sort_values(by='sign', key=lambda x: abs(x))
df = df.sort_values(by='sign', key=lambda x: abs(x), ascending=False)

df = df.head(n_elements)

Expand Down
8 changes: 4 additions & 4 deletions networkcommons/data/network/_liana.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

__all__ = ['get_lianaplus']

import lazy_import
# import lazy_import

liana = lazy_import.lazy_module('liana')
# liana = lazy_import.lazy_module('liana')

import liana


def get_lianaplus(resource='Consensus'):
Expand All @@ -37,8 +39,6 @@ def get_lianaplus(resource='Consensus'):
pandas.DataFrame: Liana+ network with source, target, and sign columns.
"""

import liana

network = liana.resource.select_resource(resource).drop_duplicates()
network.columns = ['source', 'target']
network['sign'] = 1
Expand Down
51 changes: 1 addition & 50 deletions networkcommons/data/network/_moon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Prior knowledge network used by MOON.
"""

__all__ = ['build_moon_regulons', 'get_cosmos_pkn']
__all__ = ['get_cosmos_pkn']

import lazy_import
import numpy as np
Expand Down Expand Up @@ -59,52 +59,3 @@ def get_cosmos_pkn(update: bool = False):
file_legend = pd.read_pickle(path)

return file_legend




def build_moon_regulons(include_liana=False):

dorothea_df = dc.get_collectri()

TFs = np.unique(dorothea_df['source'])

full_pkn = _omnipath.get_omnipath(genesymbols=True, directed_signed=True)

if include_liana:

ligrec_resource = _liana.get_lianaplus()

full_pkn = pd.concat([full_pkn, ligrec_resource])
full_pkn['edgeID'] = full_pkn['source'] + '_' + full_pkn['target']

# This prioritises edges coming from OP
full_pkn = full_pkn.drop_duplicates(subset='edgeID')
full_pkn = full_pkn.drop(columns='edgeID')

kinTF_regulons = full_pkn[full_pkn['target'].isin(TFs)].copy()
kinTF_regulons.columns = ['source', 'target', 'mor']
kinTF_regulons = kinTF_regulons.drop_duplicates()

kinTF_regulons = kinTF_regulons.groupby(['source', 'target']).mean() \
.reset_index()

layer_2 = {}
activation_pkn = full_pkn[full_pkn['sign'] == 1].copy()

pkn_graph = _utils.network_from_df(activation_pkn, directed=True)

relevant_nodes = list(activation_pkn['source'].unique())
relevant_nodes = [node for node in relevant_nodes if node in list(kinTF_regulons['source'])]

for node in relevant_nodes:
intermediates = activation_pkn[activation_pkn['source'] == node]['target'].tolist()
targets = [n for i in intermediates for n in pkn_graph.neighbors(i)]
targets = np.unique([n for n in targets if n in TFs])
layer_2[node] = targets

layer_2_df = pd.concat([pd.DataFrame({'source': k, 'target': v, 'mor': 0.25}) for k, v in layer_2.items()], ignore_index=True)
kinTF_regulons = pd.concat([kinTF_regulons, layer_2_df])
kinTF_regulons = kinTF_regulons.groupby(['source', 'target']).sum().reset_index()

return kinTF_regulons
2 changes: 1 addition & 1 deletion networkcommons/data/omics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
from ._deseq2 import *
from ._panacea import *
from ._scperturb import *
from ._moon import *
from ._nci60 import *
from ._cptac import *
5 changes: 0 additions & 5 deletions networkcommons/data/omics/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ def _commons_url(dataset: str, **kwargs) -> str:
return urllib.parse.urljoin(baseurl, path)


def _dataset(key: str) -> dict | None:

return _datasets()['datasets'].get(key.lower(), None)


def _requests_session() -> requests.Session:

ses = requests.Session()
Expand Down
1 change: 1 addition & 0 deletions networkcommons/data/omics/_cptac.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,6 @@ def cptac_extend_dataframe(df):

extended_df.drop(['Tumor', 'Normal'], axis=1, inplace=True)
extended_df.rename(columns={'idx': 'sample_ID'}, inplace=True)
extended_df.reset_index(inplace=True, drop=True)

return extended_df
54 changes: 0 additions & 54 deletions networkcommons/data/omics/_decryptm_ebi.py

This file was deleted.

19 changes: 3 additions & 16 deletions networkcommons/data/omics/_deseq2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@

__all__ = ['deseq2']

from typing import TYPE_CHECKING
import multiprocessing
import importlib

if TYPE_CHECKING:
import pandas as pd

import pandas as pd

import lazy_import
from pypath_common import _misc as _ppcommon

#for _mod in ('default_inference', 'dds', 'ds'):
# for _mod in ('default_inference', 'dds', 'ds'):

# globals()[f'_deseq2_{_mod}'] = lazy_import.lazy_module(f'pydeseq2.{_mod}')

Expand All @@ -50,8 +45,7 @@ def deseq2(
ref_group: str,
sample_col: str = 'sample_ID',
feature_col: str = 'gene_symbol',
covariates: list | None = None,
round_values: bool = False
covariates: list | None = None
) -> pd.DataFrame:
"""
Runs DESeq2 analysis on the given counts and metadata.
Expand All @@ -67,8 +61,6 @@ def deseq2(
Defaults to 'gene_symbol'.
covariates (list, optional): List of covariates to include in the analysis.
Defaults to None.
round_values (bool, optional): Whether to round the counts to integers. Otherwise, the
counts will be left as floats and the function will fail. Defaults to False.


Returns:
Expand All @@ -86,11 +78,6 @@ def deseq2(
if '_' in ref_group:
ref_group = ref_group.replace('_', '-')

if round_values and not counts.select_dtypes(include=['float64', 'float32']).empty:
counts = counts.round(0)
_log('Float values found. Rounded counts to integers.')


n_cpus = _conf.get('cpu_count', multiprocessing.cpu_count())
inference = _deseq2_default_inference.DefaultInference(n_cpus = n_cpus)

Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion networkcommons/data/omics/_scperturb.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from typing import Any
import json

import bs4
import anndata as ad

from . import _common
Expand Down
4 changes: 3 additions & 1 deletion networkcommons/eval/_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def get_graph_metrics(network, target_dict):

metrics.reset_index(inplace=True, drop=True)

elif isinstance(network, (nx.Graph, nx.DiGraph)):
elif isinstance(network, nx.DiGraph):
metrics = pd.DataFrame({
'Number of nodes': get_number_nodes(network),
'Number of edges': get_number_edges(network),
Expand All @@ -198,6 +198,8 @@ def get_graph_metrics(network, target_dict):
'Mean closeness': get_mean_closeness(network),
'Connected targets': get_connected_targets(network, target_dict)
}, index=[0])
else:
raise TypeError("The network must be a networkx graph or a dictionary of networkx graphs.")

return metrics

Expand Down
Loading