From 9504457f60b83ede07ef66c1fecfb0713dedc5cb Mon Sep 17 00:00:00 2001 From: Jenny HY Lin Date: Mon, 29 Jan 2024 11:47:58 +0800 Subject: [PATCH 1/8] Added max_phase as an option --- src/chembl_downloader/queries.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/chembl_downloader/queries.py b/src/chembl_downloader/queries.py index 3a3a6e2..2a7f10d 100644 --- a/src/chembl_downloader/queries.py +++ b/src/chembl_downloader/queries.py @@ -96,6 +96,7 @@ def get_target_sql( standard_relation: Optional[str] = None, standard_type: Optional[str] = None, tax_id: Optional[str] = None, + max_phase: Optional[str] = None, ) -> str: """Get the SQL for all chemicals inhibiting the target.""" ar = ( @@ -106,6 +107,7 @@ def get_target_sql( st = "" if standard_relation is None else f"AND ACTIVITIES.standard_type = '{standard_type}'" tt = "" if target_type is None else f"AND TARGET_DICTIONARY.target_type = '{target_type}'" tax = "" if tax_id is None else f"AND TARGET_DICTIONARY.tax_id = '{tax_id}'" + mp = "" if max_phase is None else f"AND MOLECULE_DICTIONARY.max_phase = '{max_phase}'" return dedent( f"""\ SELECT @@ -127,6 +129,7 @@ def get_target_sql( {ar} {st} {tax} + {mp} """ # noqa: S608 ) From a0fca2f3ed9896c2940ba2c689d18fcf677d5d7f Mon Sep 17 00:00:00 2001 From: Jenny HY Lin Date: Mon, 29 Jan 2024 20:38:51 +0800 Subject: [PATCH 2/8] Changed max_phase to boolean --- src/chembl_downloader/queries.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/chembl_downloader/queries.py b/src/chembl_downloader/queries.py index 2a7f10d..b0811ed 100644 --- a/src/chembl_downloader/queries.py +++ b/src/chembl_downloader/queries.py @@ -96,7 +96,7 @@ def get_target_sql( standard_relation: Optional[str] = None, standard_type: Optional[str] = None, tax_id: Optional[str] = None, - max_phase: Optional[str] = None, + max_phase: bool = False, ) -> str: """Get the SQL for all chemicals inhibiting the target.""" ar = ( @@ -107,7 +107,7 @@ def get_target_sql( st = "" if standard_relation is None else f"AND ACTIVITIES.standard_type = '{standard_type}'" tt = "" if target_type is None else f"AND TARGET_DICTIONARY.target_type = '{target_type}'" tax = "" if tax_id is None else f"AND TARGET_DICTIONARY.tax_id = '{tax_id}'" - mp = "" if max_phase is None else f"AND MOLECULE_DICTIONARY.max_phase = '{max_phase}'" + mp = "" if max_phase is False else f"MOLECULE_DICTIONARY.max_phase" return dedent( f"""\ SELECT @@ -117,7 +117,8 @@ def get_target_sql( COMPOUND_STRUCTURES.canonical_smiles, MOLECULE_DICTIONARY.chembl_id AS molecule_chembl_id, ACTIVITIES.standard_type, - ACTIVITIES.pchembl_value + ACTIVITIES.pchembl_value, + {mp} FROM TARGET_DICTIONARY JOIN ASSAYS ON TARGET_DICTIONARY.tid == ASSAYS.tid JOIN ACTIVITIES ON ASSAYS.assay_id == ACTIVITIES.assay_id @@ -129,7 +130,6 @@ def get_target_sql( {ar} {st} {tax} - {mp} """ # noqa: S608 ) From 72cd48a9a47af696432db4c718ad1759b899b21d Mon Sep 17 00:00:00 2001 From: Charles Tapley Hoyt Date: Mon, 29 Jan 2024 14:03:59 +0100 Subject: [PATCH 3/8] Minor refactor 1. Just check bools directly in if/else 2. No need for f-string 3. Stick the format in the middle to get around the trailing comma issue. --- src/chembl_downloader/queries.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chembl_downloader/queries.py b/src/chembl_downloader/queries.py index b0811ed..2406711 100644 --- a/src/chembl_downloader/queries.py +++ b/src/chembl_downloader/queries.py @@ -107,7 +107,7 @@ def get_target_sql( st = "" if standard_relation is None else f"AND ACTIVITIES.standard_type = '{standard_type}'" tt = "" if target_type is None else f"AND TARGET_DICTIONARY.target_type = '{target_type}'" tax = "" if tax_id is None else f"AND TARGET_DICTIONARY.tax_id = '{tax_id}'" - mp = "" if max_phase is False else f"MOLECULE_DICTIONARY.max_phase" + mp = "MOLECULE_DICTIONARY.max_phase," if max_phase else "" return dedent( f"""\ SELECT @@ -116,9 +116,9 @@ def get_target_sql( TARGET_DICTIONARY.tax_id, COMPOUND_STRUCTURES.canonical_smiles, MOLECULE_DICTIONARY.chembl_id AS molecule_chembl_id, - ACTIVITIES.standard_type, - ACTIVITIES.pchembl_value, {mp} + ACTIVITIES.standard_type, + ACTIVITIES.pchembl_value FROM TARGET_DICTIONARY JOIN ASSAYS ON TARGET_DICTIONARY.tid == ASSAYS.tid JOIN ACTIVITIES ON ASSAYS.assay_id == ACTIVITIES.assay_id From 51120b56c29d8b13fa7495a152f925cdd0c710d3 Mon Sep 17 00:00:00 2001 From: Jenny HY Lin Date: Tue, 30 Jan 2024 12:22:31 +0800 Subject: [PATCH 4/8] Removed f-string + rearranged in if/else and moved max_phase sql up --- .vscode/settings.json | 11 +++++++++++ tests/test_queries.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 tests/test_queries.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e9e6a80 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "python.testing.unittestArgs": [ + "-v", + "-s", + "./tests", + "-p", + "test_*.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true +} \ No newline at end of file diff --git a/tests/test_queries.py b/tests/test_queries.py new file mode 100644 index 0000000..0f58aaa --- /dev/null +++ b/tests/test_queries.py @@ -0,0 +1,17 @@ +"""Tests for querying the target.""" + +import unittest + +import chembl_downloader + + +class TestQueries(unittest.TestCase): + """Tests for the target.""" + + def test_get_target_sql(self): + """Test getting the target sql.""" + target = chembl_downloader.queries.get_target_sql() + self.assertIsInstance(target, str) + +# if __name__ == '__main__': +# unittest.main() \ No newline at end of file From 848a4bf211b6e27e7674857ba68197df653029de Mon Sep 17 00:00:00 2001 From: Jenny HY Lin Date: Tue, 30 Jan 2024 12:26:45 +0800 Subject: [PATCH 5/8] Removed files --- tests/test_queries.py | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 tests/test_queries.py diff --git a/tests/test_queries.py b/tests/test_queries.py deleted file mode 100644 index 0f58aaa..0000000 --- a/tests/test_queries.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Tests for querying the target.""" - -import unittest - -import chembl_downloader - - -class TestQueries(unittest.TestCase): - """Tests for the target.""" - - def test_get_target_sql(self): - """Test getting the target sql.""" - target = chembl_downloader.queries.get_target_sql() - self.assertIsInstance(target, str) - -# if __name__ == '__main__': -# unittest.main() \ No newline at end of file From 9513d43fb0f41c7d0b220e141c9364d9e3c0c822 Mon Sep 17 00:00:00 2001 From: Jenny HY Lin Date: Tue, 30 Jan 2024 13:44:23 +0800 Subject: [PATCH 6/8] Tested mp --- tests/test_queries.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/test_queries.py diff --git a/tests/test_queries.py b/tests/test_queries.py new file mode 100644 index 0000000..0f58aaa --- /dev/null +++ b/tests/test_queries.py @@ -0,0 +1,17 @@ +"""Tests for querying the target.""" + +import unittest + +import chembl_downloader + + +class TestQueries(unittest.TestCase): + """Tests for the target.""" + + def test_get_target_sql(self): + """Test getting the target sql.""" + target = chembl_downloader.queries.get_target_sql() + self.assertIsInstance(target, str) + +# if __name__ == '__main__': +# unittest.main() \ No newline at end of file From b35046d41773e59831252b5010e79719f759bcc6 Mon Sep 17 00:00:00 2001 From: Charles Tapley Hoyt Date: Tue, 30 Jan 2024 20:51:48 +0100 Subject: [PATCH 7/8] Update tests --- .vscode/settings.json | 11 ------- src/chembl_downloader/queries.py | 7 ++--- tests/test_queries.py | 50 +++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 19 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index e9e6a80..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "python.testing.unittestArgs": [ - "-v", - "-s", - "./tests", - "-p", - "test_*.py" - ], - "python.testing.pytestEnabled": false, - "python.testing.unittestEnabled": true -} \ No newline at end of file diff --git a/src/chembl_downloader/queries.py b/src/chembl_downloader/queries.py index 2406711..3174a9c 100644 --- a/src/chembl_downloader/queries.py +++ b/src/chembl_downloader/queries.py @@ -107,7 +107,7 @@ def get_target_sql( st = "" if standard_relation is None else f"AND ACTIVITIES.standard_type = '{standard_type}'" tt = "" if target_type is None else f"AND TARGET_DICTIONARY.target_type = '{target_type}'" tax = "" if tax_id is None else f"AND TARGET_DICTIONARY.tax_id = '{tax_id}'" - mp = "MOLECULE_DICTIONARY.max_phase," if max_phase else "" + mp = "\n MOLECULE_DICTIONARY.max_phase," if max_phase else "" return dedent( f"""\ SELECT @@ -115,8 +115,7 @@ def get_target_sql( TARGET_DICTIONARY.target_type, TARGET_DICTIONARY.tax_id, COMPOUND_STRUCTURES.canonical_smiles, - MOLECULE_DICTIONARY.chembl_id AS molecule_chembl_id, - {mp} + MOLECULE_DICTIONARY.chembl_id AS molecule_chembl_id,{mp} ACTIVITIES.standard_type, ACTIVITIES.pchembl_value FROM TARGET_DICTIONARY @@ -131,7 +130,7 @@ def get_target_sql( {st} {tax} """ # noqa: S608 - ) + ).strip() DRUG_INDICATIONS_SQL = """\ diff --git a/tests/test_queries.py b/tests/test_queries.py index 0f58aaa..377e698 100644 --- a/tests/test_queries.py +++ b/tests/test_queries.py @@ -1,6 +1,7 @@ """Tests for querying the target.""" import unittest +from textwrap import dedent import chembl_downloader @@ -10,8 +11,49 @@ class TestQueries(unittest.TestCase): def test_get_target_sql(self): """Test getting the target sql.""" - target = chembl_downloader.queries.get_target_sql() - self.assertIsInstance(target, str) + target = chembl_downloader.queries.get_target_sql("CHEMBL3467") + expected = dedent( + """\ + SELECT + ASSAYS.chembl_id AS assay_chembl_id, + TARGET_DICTIONARY.target_type, + TARGET_DICTIONARY.tax_id, + COMPOUND_STRUCTURES.canonical_smiles, + MOLECULE_DICTIONARY.chembl_id AS molecule_chembl_id, + ACTIVITIES.standard_type, + ACTIVITIES.pchembl_value + FROM TARGET_DICTIONARY + JOIN ASSAYS ON TARGET_DICTIONARY.tid == ASSAYS.tid + JOIN ACTIVITIES ON ASSAYS.assay_id == ACTIVITIES.assay_id + JOIN MOLECULE_DICTIONARY ON MOLECULE_DICTIONARY.molregno == ACTIVITIES.molregno + JOIN COMPOUND_STRUCTURES ON MOLECULE_DICTIONARY.molregno == COMPOUND_STRUCTURES.molregno + WHERE TARGET_DICTIONARY.chembl_id = 'CHEMBL3467' + AND ACTIVITIES.pchembl_value IS NOT NULL + """ + ).strip() + self.assertEqual(expected, target) -# if __name__ == '__main__': -# unittest.main() \ No newline at end of file + def test_get_target_with_max_phase(self): + """Test getting target sql with a max phase.""" + target = chembl_downloader.queries.get_target_sql("CHEMBL3467", max_phase=True) + expected = dedent( + """\ + SELECT + ASSAYS.chembl_id AS assay_chembl_id, + TARGET_DICTIONARY.target_type, + TARGET_DICTIONARY.tax_id, + COMPOUND_STRUCTURES.canonical_smiles, + MOLECULE_DICTIONARY.chembl_id AS molecule_chembl_id, + MOLECULE_DICTIONARY.max_phase, + ACTIVITIES.standard_type, + ACTIVITIES.pchembl_value + FROM TARGET_DICTIONARY + JOIN ASSAYS ON TARGET_DICTIONARY.tid == ASSAYS.tid + JOIN ACTIVITIES ON ASSAYS.assay_id == ACTIVITIES.assay_id + JOIN MOLECULE_DICTIONARY ON MOLECULE_DICTIONARY.molregno == ACTIVITIES.molregno + JOIN COMPOUND_STRUCTURES ON MOLECULE_DICTIONARY.molregno == COMPOUND_STRUCTURES.molregno + WHERE TARGET_DICTIONARY.chembl_id = 'CHEMBL3467' + AND ACTIVITIES.pchembl_value IS NOT NULL + """ + ).strip() + self.assertEqual(expected, target) From 98de12c646dda29fe90683daf43ca740b200926e Mon Sep 17 00:00:00 2001 From: Charles Tapley Hoyt Date: Tue, 30 Jan 2024 20:52:48 +0100 Subject: [PATCH 8/8] Reduce files --- tests/test_api.py | 50 ++++++++++++++++++++++++++++++++++++ tests/test_queries.py | 59 ------------------------------------------- 2 files changed, 50 insertions(+), 59 deletions(-) delete mode 100644 tests/test_queries.py diff --git a/tests/test_api.py b/tests/test_api.py index 95415e7..c60b9f5 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,6 +1,7 @@ """Tests for the API.""" import unittest +from textwrap import dedent import chembl_downloader @@ -12,3 +13,52 @@ def test_latest_version(self): """Test getting the latest version.""" latest_version = chembl_downloader.latest() self.assertIsInstance(latest_version, str) + + def test_get_target_sql(self): + """Test getting the target sql.""" + target = chembl_downloader.queries.get_target_sql("CHEMBL3467") + expected = dedent( + """\ + SELECT + ASSAYS.chembl_id AS assay_chembl_id, + TARGET_DICTIONARY.target_type, + TARGET_DICTIONARY.tax_id, + COMPOUND_STRUCTURES.canonical_smiles, + MOLECULE_DICTIONARY.chembl_id AS molecule_chembl_id, + ACTIVITIES.standard_type, + ACTIVITIES.pchembl_value + FROM TARGET_DICTIONARY + JOIN ASSAYS ON TARGET_DICTIONARY.tid == ASSAYS.tid + JOIN ACTIVITIES ON ASSAYS.assay_id == ACTIVITIES.assay_id + JOIN MOLECULE_DICTIONARY ON MOLECULE_DICTIONARY.molregno == ACTIVITIES.molregno + JOIN COMPOUND_STRUCTURES ON MOLECULE_DICTIONARY.molregno == COMPOUND_STRUCTURES.molregno + WHERE TARGET_DICTIONARY.chembl_id = 'CHEMBL3467' + AND ACTIVITIES.pchembl_value IS NOT NULL + """ + ).strip() + self.assertEqual(expected, target) + + def test_get_target_with_max_phase(self): + """Test getting target sql with a max phase.""" + target = chembl_downloader.queries.get_target_sql("CHEMBL3467", max_phase=True) + expected = dedent( + """\ + SELECT + ASSAYS.chembl_id AS assay_chembl_id, + TARGET_DICTIONARY.target_type, + TARGET_DICTIONARY.tax_id, + COMPOUND_STRUCTURES.canonical_smiles, + MOLECULE_DICTIONARY.chembl_id AS molecule_chembl_id, + MOLECULE_DICTIONARY.max_phase, + ACTIVITIES.standard_type, + ACTIVITIES.pchembl_value + FROM TARGET_DICTIONARY + JOIN ASSAYS ON TARGET_DICTIONARY.tid == ASSAYS.tid + JOIN ACTIVITIES ON ASSAYS.assay_id == ACTIVITIES.assay_id + JOIN MOLECULE_DICTIONARY ON MOLECULE_DICTIONARY.molregno == ACTIVITIES.molregno + JOIN COMPOUND_STRUCTURES ON MOLECULE_DICTIONARY.molregno == COMPOUND_STRUCTURES.molregno + WHERE TARGET_DICTIONARY.chembl_id = 'CHEMBL3467' + AND ACTIVITIES.pchembl_value IS NOT NULL + """ + ).strip() + self.assertEqual(expected, target) diff --git a/tests/test_queries.py b/tests/test_queries.py deleted file mode 100644 index 377e698..0000000 --- a/tests/test_queries.py +++ /dev/null @@ -1,59 +0,0 @@ -"""Tests for querying the target.""" - -import unittest -from textwrap import dedent - -import chembl_downloader - - -class TestQueries(unittest.TestCase): - """Tests for the target.""" - - def test_get_target_sql(self): - """Test getting the target sql.""" - target = chembl_downloader.queries.get_target_sql("CHEMBL3467") - expected = dedent( - """\ - SELECT - ASSAYS.chembl_id AS assay_chembl_id, - TARGET_DICTIONARY.target_type, - TARGET_DICTIONARY.tax_id, - COMPOUND_STRUCTURES.canonical_smiles, - MOLECULE_DICTIONARY.chembl_id AS molecule_chembl_id, - ACTIVITIES.standard_type, - ACTIVITIES.pchembl_value - FROM TARGET_DICTIONARY - JOIN ASSAYS ON TARGET_DICTIONARY.tid == ASSAYS.tid - JOIN ACTIVITIES ON ASSAYS.assay_id == ACTIVITIES.assay_id - JOIN MOLECULE_DICTIONARY ON MOLECULE_DICTIONARY.molregno == ACTIVITIES.molregno - JOIN COMPOUND_STRUCTURES ON MOLECULE_DICTIONARY.molregno == COMPOUND_STRUCTURES.molregno - WHERE TARGET_DICTIONARY.chembl_id = 'CHEMBL3467' - AND ACTIVITIES.pchembl_value IS NOT NULL - """ - ).strip() - self.assertEqual(expected, target) - - def test_get_target_with_max_phase(self): - """Test getting target sql with a max phase.""" - target = chembl_downloader.queries.get_target_sql("CHEMBL3467", max_phase=True) - expected = dedent( - """\ - SELECT - ASSAYS.chembl_id AS assay_chembl_id, - TARGET_DICTIONARY.target_type, - TARGET_DICTIONARY.tax_id, - COMPOUND_STRUCTURES.canonical_smiles, - MOLECULE_DICTIONARY.chembl_id AS molecule_chembl_id, - MOLECULE_DICTIONARY.max_phase, - ACTIVITIES.standard_type, - ACTIVITIES.pchembl_value - FROM TARGET_DICTIONARY - JOIN ASSAYS ON TARGET_DICTIONARY.tid == ASSAYS.tid - JOIN ACTIVITIES ON ASSAYS.assay_id == ACTIVITIES.assay_id - JOIN MOLECULE_DICTIONARY ON MOLECULE_DICTIONARY.molregno == ACTIVITIES.molregno - JOIN COMPOUND_STRUCTURES ON MOLECULE_DICTIONARY.molregno == COMPOUND_STRUCTURES.molregno - WHERE TARGET_DICTIONARY.chembl_id = 'CHEMBL3467' - AND ACTIVITIES.pchembl_value IS NOT NULL - """ - ).strip() - self.assertEqual(expected, target)