This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from Teichlab/develop
Develop
- Loading branch information
Showing
15 changed files
with
212 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,6 @@ | |
./docker/ | ||
.dockerignore | ||
.git | ||
./venv | ||
./venv | ||
./dist | ||
./cpdb-venv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 26 additions & 8 deletions
34
...db/src/api_endpoints/terminal_api/query_terminal_api_endpoints/query_terminal_commands.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,40 @@ | ||
from click._unicodefun import click | ||
from typing import Callable | ||
|
||
import click | ||
|
||
from cellphonedb.src.api_endpoints.terminal_api.util.choose_database import choose_database | ||
from cellphonedb.src.app import cpdb_app | ||
from cellphonedb.src.local_launchers.local_query_launcher import LocalQueryLauncher | ||
from cellphonedb.src.app.cellphonedb_app import cellphonedb_app | ||
|
||
|
||
def common_options(f: Callable) -> Callable: | ||
options = [ | ||
click.option('--verbose/--quiet', default=True, help='Print or hide cellphonedb logs [verbose]'), | ||
click.option('--database', default='latest', callback=choose_database), | ||
] | ||
|
||
for option in reversed(options): | ||
f = option(f) | ||
|
||
return f | ||
|
||
|
||
@click.command() | ||
@click.argument('element') | ||
def find_interactions_by_element(element: str): | ||
LocalQueryLauncher(cellphonedb_app).find_interactions_by_element(element) | ||
@common_options | ||
def find_interactions_by_element(element: str, verbose: bool, database: str): | ||
LocalQueryLauncher(cpdb_app.create_app(verbose, database)).find_interactions_by_element(element) | ||
|
||
|
||
@click.command() | ||
@click.option('--columns', default=None, help='Columns to set in the result') | ||
def get_interaction_gene(columns: str): | ||
LocalQueryLauncher(cellphonedb_app).get_interaction_gene(columns) | ||
@common_options | ||
def get_interaction_gene(columns: str, verbose: bool, database: str): | ||
LocalQueryLauncher(cpdb_app.create_app(verbose, database)).get_interaction_gene(columns) | ||
|
||
|
||
@click.command() | ||
@click.argument('partial_element') | ||
def autocomplete(partial_element: str) -> None: | ||
LocalQueryLauncher(cellphonedb_app).autocomplete_element(partial_element) | ||
@common_options | ||
def autocomplete(partial_element: str, verbose: bool, database: str) -> None: | ||
LocalQueryLauncher(cpdb_app.create_app(verbose, database)).autocomplete_element(partial_element) |
Empty file.
9 changes: 9 additions & 0 deletions
9
cellphonedb/src/api_endpoints/terminal_api/util/choose_database.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from typing import Optional | ||
|
||
from click import Context, Argument | ||
|
||
from cellphonedb.src.database.manager import DatabaseVersionManager | ||
|
||
|
||
def choose_database(ctx: Context, argument: Argument, value: str) -> Optional[str]: | ||
return DatabaseVersionManager.find_database_for(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
import pandas as pd | ||
import re | ||
|
||
import pandas as pd | ||
|
||
|
||
def autocomplete_query(genes: pd.DataFrame, multidatas: pd.DataFrame, partial_element: pd.DataFrame) -> pd.DataFrame: | ||
values = genes[genes['ensembl'].str.contains(partial_element, flags=re.IGNORECASE)]['ensembl'] | ||
values = values.append( | ||
genes[genes['protein_name'].str.contains(partial_element, flags=re.IGNORECASE)]['protein_name'], | ||
ignore_index=True) | ||
values = values.append( | ||
genes[genes['gene_name'].str.contains(partial_element, flags=re.IGNORECASE)]['gene_name'], | ||
ignore_index=True) | ||
values = values.append( | ||
genes[genes['hgnc_symbol'].str.contains(partial_element, flags=re.IGNORECASE)]['hgnc_symbol'], | ||
ignore_index=True) | ||
values = values.append( | ||
multidatas[multidatas['name'].str.contains(partial_element, flags=re.IGNORECASE)]['name'], | ||
ignore_index=True) | ||
result = pd.DataFrame(data=values, columns=['value']) | ||
values = _partial_filter(genes, 'ensembl', partial_element) | ||
|
||
by_protein_name = _partial_filter(genes, 'protein_name', partial_element) | ||
by_gene_name = _partial_filter(genes, 'gene_name', partial_element) | ||
|
||
with_hgnc_symbol = genes.dropna(subset=['hgnc_symbol']) | ||
by_hgnc_symbol = _partial_filter(with_hgnc_symbol, 'hgnc_symbol', partial_element) | ||
|
||
by_name = _partial_filter(multidatas, 'name', partial_element) | ||
|
||
values = values.append(by_protein_name, ignore_index=True) | ||
values = values.append(by_gene_name, ignore_index=True) | ||
values = values.append(by_hgnc_symbol, ignore_index=True) | ||
values = values.append(by_name, ignore_index=True) | ||
|
||
result = pd.DataFrame(data=values, columns=['value']).drop_duplicates() | ||
|
||
return result | ||
|
||
|
||
def _partial_filter(input_data, name, partial_element): | ||
matching = input_data[input_data[name].str.contains(partial_element, flags=re.IGNORECASE)][name] | ||
return matching |
Empty file.
56 changes: 56 additions & 0 deletions
56
cellphonedb/src/core/tests/queries/test_autocomplete_queries.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import random | ||
from unittest import TestCase | ||
|
||
from cellphonedb.src.app.app_config import AppConfig | ||
from cellphonedb.src.core.CellphonedbSqlalchemy import CellphonedbSqlalchemy | ||
|
||
|
||
class TestAutocompleteQueries(TestCase): | ||
|
||
def setUp(self) -> None: | ||
self.cellphone = CellphonedbSqlalchemy(AppConfig().get_cellphone_core_config()) | ||
gene_repository = self.cellphone.database_manager.get_repository('gene') | ||
self.all_genes = gene_repository.get_all_expanded().to_dict(orient='records') | ||
|
||
def test_find_elements_by_gene_name(self): | ||
self._test_find_elements_by_('gene_name') | ||
|
||
def test_find_elements_by_protein_name(self): | ||
self._test_find_elements_by_('protein_name') | ||
|
||
def test_find_elements_by_hgnc_symbol(self): | ||
self._test_find_elements_by_('hgnc_symbol') | ||
|
||
def test_find_elements_by_ensembl(self): | ||
self._test_find_elements_by_('ensembl') | ||
|
||
def test_find_elements_by_name(self): | ||
self._test_find_elements_by_('name') | ||
|
||
def _test_find_elements_by_(self, field): | ||
random_gene = random.choice(self.all_genes) | ||
whole_input = random_gene[field] | ||
|
||
whole_query_result = self.cellphone.query.autocomplete_launcher(whole_input) | ||
|
||
whole_results = whole_query_result['value'].tolist() | ||
self.assertIn(whole_input, whole_results) | ||
|
||
partial_input = self._random_substring(whole_input) | ||
|
||
partial_query_result = self.cellphone.query.autocomplete_launcher(partial_input) | ||
|
||
partial_results = partial_query_result['value'].tolist() | ||
self.assertIn(whole_input, partial_results) | ||
|
||
self.assertGreaterEqual(len(partial_results), len(whole_results)) | ||
|
||
def _random_substring(self, whole_input): | ||
start_index = self._random_position_to_half(whole_input) | ||
end_index = self._random_position_to_half(whole_input) | ||
|
||
return whole_input[start_index:-end_index if end_index else None] | ||
|
||
@staticmethod | ||
def _random_position_to_half(string): | ||
return random.randint(0, int((len(string)) / 2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import logging | ||
import sys | ||
|
||
|
||
class RabbitLogger: | ||
def __init__(self): | ||
self._logger = logging.getLogger(__name__) | ||
formatter = logging.Formatter('[ ][QUEUE][%(asctime)s][%(levelname)s] %(message)s', "%d/%m/%y-%H:%M:%S") | ||
handler = logging.StreamHandler(sys.stdout) | ||
handler.setFormatter(formatter) | ||
self._logger.addHandler(handler) | ||
self._logger.setLevel(logging.INFO) | ||
|
||
def __getattr__(self, item): | ||
return getattr(self._logger, item) | ||
|
||
|
||
class RabbitAdapter(logging.LoggerAdapter): | ||
def process(self, msg, kwargs): | ||
return '[{}] {}'.format(self.extra['job_id'], msg), kwargs | ||
|
||
@classmethod | ||
def logger_for(cls, logger, job_id): | ||
return cls(logger, {'job_id': job_id}) |
Oops, something went wrong.