Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Commit

Permalink
Code Refactor and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mvento committed Jan 15, 2021
1 parent b8cdfa0 commit 4eb22a2
Show file tree
Hide file tree
Showing 51 changed files with 2,932 additions and 6,469 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Output files
/api_interfaces/python/input_data/
/api_interfaces/python/out/
/out/
/out/*
!/out/.gitkeep
/in/*
!/in/.gitkeep
!/in/example_data/
Expand Down
5 changes: 5 additions & 0 deletions cellphonedb/src/core/exceptions/NoInteractionsFound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NoInteractionsFound(Exception):
def __init__(self, description: str = None, hint: str = None):
super(NoInteractionsFound, self).__init__('No CellPhoneDB interacions found in this input.')
self.description = description
self.hint = hint
321 changes: 61 additions & 260 deletions cellphonedb/src/core/methods/cpdb_analysis_complex_method.py

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions cellphonedb/src/core/methods/cpdb_analysis_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def percent_analysis(clusters: dict,
cluster_interactions: list,
base_result: pd.DataFrame,
separator: str,
suffixes: tuple = ('_1', '_2'),
counts_data: str = 'ensembl') -> pd.DataFrame:
) -> pd.DataFrame:
result = base_result.copy()
percents = {}
for cluster_name in clusters['names']:
Expand All @@ -20,8 +19,7 @@ def percent_analysis(clusters: dict,
for cluster_interaction in cluster_interactions:
cluster_interaction_string = '{}{}{}'.format(cluster_interaction[0], separator, cluster_interaction[1])

interaction_percent = cluster_interaction_percent(cluster_interaction, interaction, percents, suffixes,
counts_data=counts_data)
interaction_percent = cluster_interaction_percent(cluster_interaction, interaction, percents)

result.at[interaction_index, cluster_interaction_string] = interaction_percent

Expand All @@ -42,14 +40,14 @@ def counts_percent(counts: pd.Series,
def cluster_interaction_percent(cluster_interaction: tuple,
interaction: pd.Series,
clusters_percents: dict,
suffixes: tuple = ('_1', '_2'),
counts_data: str = 'ensembl'
) -> int:
percent_cluster_receptors = clusters_percents[cluster_interaction[0]]
percent_cluster_ligands = clusters_percents[cluster_interaction[1]]

percent_receptor = percent_cluster_receptors[interaction['{}{}'.format(counts_data, suffixes[0])]]
percent_ligand = percent_cluster_ligands[interaction['{}{}'.format(counts_data, suffixes[1])]]
receptor = interaction['multidata_1_id']
percent_receptor = percent_cluster_receptors.loc[receptor]
ligand = interaction['multidata_2_id']
percent_ligand = percent_cluster_ligands.loc[ligand]

if percent_receptor and percent_ligand:
interaction_percent = 1
Expand Down
50 changes: 12 additions & 38 deletions cellphonedb/src/core/methods/cpdb_analysis_method.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pandas as pd

from cellphonedb.src.core.exceptions.EmptyResultException import EmptyResultException
from cellphonedb.src.core.exceptions.NoComplexException import NoComplexException
from cellphonedb.src.core.methods import cpdb_analysis_simple_method, cpdb_analysis_complex_method
from cellphonedb.src.core.methods import cpdb_analysis_complex_method


def call(meta: pd.DataFrame,
Expand All @@ -15,41 +13,17 @@ def call(meta: pd.DataFrame,
separator: str,
threshold: float = 0.1,
result_precision: int = 3) -> (pd.DataFrame, pd.DataFrame, pd.DataFrame):
means_simple, significant_means_simple, deconvoluted_simple = \
cpdb_analysis_simple_method.call(meta.copy(),
counts.copy(),
counts_data,
interactions.copy(),
separator,
threshold,
result_precision)

try:
means_complex, significant_means_complex, deconvoluted_complex = \
cpdb_analysis_complex_method.call(meta.copy(),
counts.copy(),
counts_data,
interactions.copy(),
genes,
complexes,
complex_compositions,
separator,
threshold,
result_precision)
means = means_simple.append(means_complex, sort=False)
significant_means = significant_means_simple.append(significant_means_complex, sort=False)
deconvoluted = deconvoluted_simple.append(deconvoluted_complex, sort=False)

except NoComplexException:
means = means_simple
significant_means = significant_means_simple
deconvoluted = deconvoluted_simple

deconvoluted.drop_duplicates(inplace=True)

if means.empty:
raise EmptyResultException

means, significant_means, deconvoluted = \
cpdb_analysis_complex_method.call(meta.copy(),
counts.copy(),
counts_data,
interactions.copy(),
genes,
complexes,
complex_compositions,
separator,
threshold,
result_precision)
max_rank = significant_means['rank'].max()
significant_means['rank'] = significant_means['rank'].apply(lambda rank: rank if rank != 0 else (1 + max_rank))
significant_means.sort_values('rank', inplace=True)
Expand Down
189 changes: 0 additions & 189 deletions cellphonedb/src/core/methods/cpdb_analysis_simple_method.py

This file was deleted.

Loading

0 comments on commit 4eb22a2

Please sign in to comment.