Skip to content

Commit

Permalink
Falke8 code
Browse files Browse the repository at this point in the history
  • Loading branch information
PauBadiaM committed Jun 20, 2024
1 parent 2092219 commit 06bae40
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 66 deletions.
6 changes: 3 additions & 3 deletions decoupler/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ def _benchmark(mat, obs, net, perturb, sign, metrics=['auroc', 'auprc'], groupby
return df


def benchmark(mat, obs, net, perturb, sign, metrics=['auroc', 'auprc', 'mcauroc', 'mcauprc', 'rank', 'nrank', 'recall'], groupby=None,
by='experiment', f_expr=True, f_srcs=False, use_pval=None, min_exp=5, pi0=0.5, n_iter=1000, seed=42,
verbose=True, use_raw=True, decouple_kws={}):
def benchmark(mat, obs, net, perturb, sign, metrics=['auroc', 'auprc', 'mcauroc', 'mcauprc', 'rank', 'nrank', 'recall'],
groupby=None, by='experiment', f_expr=True, f_srcs=False, use_pval=None, min_exp=5, pi0=0.5, n_iter=1000,
seed=42, verbose=True, use_raw=True, decouple_kws={}):
"""
Benchmark methods or networks on a given set of perturbation experiments using activity inference with decoupler.
Expand Down
1 change: 0 additions & 1 deletion decoupler/method_aucell.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from .pre import extract, rename_net, filt_min_n, return_data

from anndata import AnnData
import numba as nb


Expand Down
1 change: 0 additions & 1 deletion decoupler/method_gsea.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from .pre import extract, rename_net, filt_min_n, return_data
from .utils import p_adjust_fdr

from anndata import AnnData
from tqdm import tqdm

import numba as nb
Expand Down
1 change: 0 additions & 1 deletion decoupler/method_gsva.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from .pre import extract, rename_net, filt_min_n, return_data
from .method_gsea import std

from anndata import AnnData
from tqdm import tqdm

import numba as nb
Expand Down
1 change: 0 additions & 1 deletion decoupler/method_mdt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data

from anndata import AnnData
from tqdm import tqdm


Expand Down
1 change: 0 additions & 1 deletion decoupler/method_mlm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data

from anndata import AnnData
from scipy import stats

from tqdm import tqdm
Expand Down
1 change: 0 additions & 1 deletion decoupler/method_ora.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from .pre import extract, rename_net, filt_min_n, return_data
from .utils import p_adjust_fdr

from anndata import AnnData
from tqdm import tqdm

import numba as nb
Expand Down
1 change: 0 additions & 1 deletion decoupler/method_udt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data

from anndata import AnnData
from tqdm import tqdm


Expand Down
1 change: 0 additions & 1 deletion decoupler/method_ulm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data

from anndata import AnnData
from tqdm import tqdm


Expand Down
1 change: 0 additions & 1 deletion decoupler/method_viper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data

from anndata import AnnData
from tqdm import tqdm

import numba as nb
Expand Down
1 change: 0 additions & 1 deletion decoupler/method_wmean.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data
from .method_gsea import std

from anndata import AnnData
from tqdm import tqdm

import numba as nb
Expand Down
1 change: 0 additions & 1 deletion decoupler/method_wsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data
from .method_gsea import std

from anndata import AnnData
from tqdm import tqdm

import numba as nb
Expand Down
101 changes: 51 additions & 50 deletions decoupler/omnip.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,32 @@ def _check_if_omnipath() -> ModuleType:

def _check_if_pypath() -> None:

def ver(v):
return tuple(map(int, v.split('.')))

# Check if pypath is installed
try:

def ver(v):
return tuple(map(int, v.split('.')))

import pypath

if (
getattr(pypath, '__version__', None) and
ver(pypath.__version__) < ver(PYPATH_MIN_VERSION)
):

msg = (
'The installed version of pypath-omnipath is too old, '
f'the oldest compatible version is {PYPATH_MIN_VERSION}.'
)
_misc.log_traceback(msg)
raise RuntimeError(msg)

except Exception:

msg = (
'pypath-omnipath is not installed. Please install it with: '
'pip install git+https://github.com/saezlab/pypath.git'
'pip install pypath-omnipath'
)
_misc.log_traceback(msg)
raise ImportError(msg)

if (getattr(pypath, '__version__', None) and ver(pypath.__version__) < ver(PYPATH_MIN_VERSION)):
msg = (
'The installed version of pypath-omnipath is too old, '
f'the oldest compatible version is {PYPATH_MIN_VERSION}.'
)
_misc.log_traceback(msg)
raise ImportError(msg)
elif not callable(getattr(pypath, "disclaimer", False)):
msg = (
'pypath is installed instead of pypath-omnipath. Please, remove'
'pypath (pip uninstall pypath) and install pypath-omnipath (pip install pypath-omnipath)'
)
_misc.log_traceback(msg)
raise ImportError(msg)
Expand Down Expand Up @@ -193,7 +194,7 @@ def get_progeny(
None
) = None,
**kwargs
) -> pd.DataFrame:
) -> pd.DataFrame:
"""
Pathway RespOnsive GENes for activity inference (PROGENy).
Expand Down Expand Up @@ -244,7 +245,7 @@ def get_progeny(
p = p.unstack('label').droplevel(axis=1, level=0)
p.columns = np.array(p.columns)
p = p.reset_index()
p = p.drop('record_id', axis = 1)
p = p.drop('record_id', axis=1)
p.columns.name = None
p = _annotation_identifiers(p, organism, genesymbol_resource)
p = p[['pathway', 'genesymbol', 'weight', 'p_value']]
Expand All @@ -257,7 +258,7 @@ def get_progeny(
groupby('pathway').
head(top).
sort_values(['pathway', 'p_value']).
reset_index(drop = True)
reset_index(drop=True)
)
p.columns = ['source', 'target', 'weight', 'p_value']
p = op._misc.dtypes.auto_dtype(p)
Expand Down Expand Up @@ -613,11 +614,11 @@ def get_collectri(
if genesymbol_resource and genesymbol_resource != 'uniprot':

mirna = add_genesymbols(
net = mirna,
column = 'source',
target_column = 'source_genesymbol',
organism = organism,
resource = genesymbol_resource,
net=mirna,
column='source',
target_column='source_genesymbol',
organism=organism,
resource=genesymbol_resource,
)

elif genesymbol_resource is False:
Expand Down Expand Up @@ -658,7 +659,7 @@ def get_collectri(

# Select and rename columns
ct = ct.rename(
columns = {
columns={
'source_genesymbol': 'source',
'target_genesymbol': 'target',
'references_stripped': 'PMID',
Expand Down Expand Up @@ -780,7 +781,7 @@ def add_genesymbols(
Literal['uniprot', 'ensembl'] |
dict[str, set[str]]
) = 'uniprot',
) -> pd.DataFrame:
) -> pd.DataFrame:
"""
Add or update Gene Symbols in data frame.
Expand Down Expand Up @@ -819,15 +820,15 @@ def add_genesymbols(
raise ValueError('Resource must be either "uniprot" or "ensembl".')

mapping_def = mapping_cls(
id_type_a = 'genesymbol',
id_type_b = 'uniprot',
ncbi_tax_id = _organism,
id_type_a='genesymbol',
id_type_b='uniprot',
ncbi_tax_id=_organism,
)

resource = mapping.MapReader(
mapping_def,
load_b_to_a = True,
load_a_to_b = False,
load_b_to_a=True,
load_a_to_b=False,
).b_to_a

mapping_table = pd.DataFrame(
Expand All @@ -837,19 +838,19 @@ def add_genesymbols(
resource.items()
for g in gs
),
columns = (column, target_column),
columns=(column, target_column),
)

if target_column in net.columns:

col_idx = net.columns.get_loc(target_column)
net = net.drop(target_column, axis = 1)
net = net.drop(target_column, axis=1)

else:

col_idx = net.columns.get_loc(column) + 1

net = net.merge(mapping_table, how = 'left', on = column)
net = net.merge(mapping_table, how='left', on=column)
net = net[
list(net.columns[0:col_idx]) +
[target_column] +
Expand All @@ -868,7 +869,7 @@ def _network_identifiers(
bool |
None
) = None,
) -> pd.DataFrame:
) -> pd.DataFrame:

_organism = _the_organism(organism)

Expand All @@ -887,11 +888,11 @@ def _network_identifiers(
for side in ('source', 'target'):

net = add_genesymbols(
net = net,
column = side,
target_column = f'{side}_genesymbol',
organism = organism,
resource = genesymbol_resource,
net=net,
column=side,
target_column=f'{side}_genesymbol',
organism=organism,
resource=genesymbol_resource,
)

elif genesymbol_resource is False:
Expand All @@ -911,7 +912,7 @@ def _annotation_identifiers(
bool |
None
) = None,
) -> pd.DataFrame:
) -> pd.DataFrame:

if not _is_human(organism):

Expand All @@ -932,19 +933,19 @@ def _annotation_identifiers(
genesymbol_resource = genesymbol_resource or 'uniprot'

net = add_genesymbols(
net = net,
column = 'uniprot',
target_column = 'genesymbol',
organism = organism,
resource = genesymbol_resource,
net=net,
column='uniprot',
target_column='genesymbol',
organism=organism,
resource=genesymbol_resource,
)

return net


def get_ksn_omnipath(
organism: str | int = 'human',
) -> pd.DataFrame:
) -> pd.DataFrame:
"""
OmniPath kinase-substrate network
Expand All @@ -964,7 +965,7 @@ def get_ksn_omnipath(
_organism = _the_organism(organism)

# Load Kinase-Substrate Network
ksn = op.requests.Enzsub.get(genesymbols=True, organism = _organism)
ksn = op.requests.Enzsub.get(genesymbols=True, organism=_organism)

# Filter by phosphorilation
cols = ['enzyme_genesymbol', 'substrate_genesymbol', 'residue_type',
Expand Down
2 changes: 1 addition & 1 deletion decoupler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def p_adjust_fdr(p):
"""

# Code adapted from: https://stackoverflow.com/a/33532498/8395875
p = np.asarray(p, dtype = 'float64')
p = np.asarray(p, dtype='float64')
by_descend = p.argsort()[::-1]
by_orig = by_descend.argsort()
steps = float(len(p)) / np.arange(len(p), 0, -1)
Expand Down
2 changes: 1 addition & 1 deletion decoupler/utils_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from numpy.random import default_rng
import pandas as pd
import scipy
from scipy.sparse import csr_matrix, issparse
from scipy.sparse import issparse

from .utils import get_toy_data
from .pre import match
Expand Down

0 comments on commit 06bae40

Please sign in to comment.