Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
elewis2 committed Jul 15, 2024
1 parent 1eb256d commit c0cf636
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 35 deletions.
9 changes: 7 additions & 2 deletions pori_python/graphkb/genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ def get_genes_from_variant_types(
return result


def get_preferred_gene_source_rid(conn: GraphKBConnection, preferred_source_name=PREFERRED_GENE_SOURCE_NAME):
def get_preferred_gene_source_rid(
conn: GraphKBConnection, preferred_source_name=PREFERRED_GENE_SOURCE_NAME
):
"""Get the rid of the preferred gene source.
Args:
Expand All @@ -196,7 +198,10 @@ def get_preferred_gene_source_rid(conn: GraphKBConnection, preferred_source_name
"""
result = conn.query(
{"target": {"target": "Source", "filters": {"name": preferred_source_name}}, "queryType": "similarTo"},
{
"target": {"target": "Source", "filters": {"name": preferred_source_name}},
"queryType": "similarTo",
},
)[0]["@rid"]
return result

Expand Down
10 changes: 7 additions & 3 deletions pori_python/ipr/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@


class IprConnection:
def __init__(self, username: str, password: str, url: str = os.environ.get("IPR_URL", DEFAULT_URL)):
def __init__(
self, username: str, password: str, url: str = os.environ.get("IPR_URL", DEFAULT_URL)
):
self.token = None
self.url = url
self.username = username
Expand All @@ -26,7 +28,9 @@ def __init__(self, username: str, password: str, url: str = os.environ.get("IPR_
self.cache: Dict[str, List[Dict]] = {}
self.request_count = 0

def request(self, endpoint: str, method: str = "GET", custom_headers: Dict = None, **kwargs) -> Dict:
def request(
self, endpoint: str, method: str = "GET", custom_headers: Dict = None, **kwargs
) -> Dict:
"""Request wrapper to handle adding common headers and logging
Args:
Expand Down Expand Up @@ -59,7 +63,7 @@ def request(self, endpoint: str, method: str = "GET", custom_headers: Dict = Non
pass

raise requests.exceptions.HTTPError(message)
if resp.status_code == 204: # TODO: address this in api
if resp.status_code == 204: # TODO: address this in api
return {'status_code': 204}
return resp.json()

Expand Down
8 changes: 6 additions & 2 deletions pori_python/ipr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ def command_interface() -> None:
)

parser.add_argument('--ipr_url', default=os.environ.get("IPR_URL", DEFAULT_URL))
parser.add_argument('--graphkb_username', help='username to use connecting to graphkb if different from ipr')
parser.add_argument('--graphkb_password', help='password to use connecting to graphkb if different from ipr')
parser.add_argument(
'--graphkb_username', help='username to use connecting to graphkb if different from ipr'
)
parser.add_argument(
'--graphkb_password', help='password to use connecting to graphkb if different from ipr'
)
parser.add_argument('--graphkb_url', default=os.environ.get("GRAPHKB_URL", None))
parser.add_argument('--log_level', default='info', choices=LOG_LEVELS.keys())
parser.add_argument(
Expand Down
36 changes: 29 additions & 7 deletions tests/test_graphkb/test_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def conn():
conn.login(os.environ["GRAPHKB_USER"], os.environ["GRAPHKB_PASS"])
return conn


@pytest.mark.skipif(EXCLUDE_ONCOKB_TESTS, reason="excluding tests that depend on oncokb data")
def test_oncogene(conn):
result = get_oncokb_oncogenes(conn)
Expand All @@ -121,6 +122,7 @@ def test_oncogene(conn):
for gene in CANONICAL_CG:
assert gene not in names


@pytest.mark.skipif(EXCLUDE_ONCOKB_TESTS, reason="excluding tests that depend on oncokb data")
def test_tumour_supressors(conn):
result = get_oncokb_tumour_supressors(conn)
Expand All @@ -132,8 +134,11 @@ def test_tumour_supressors(conn):
for gene in CANONICAL_CG:
assert gene not in names


@pytest.mark.skipif(EXCLUDE_ONCOKB_TESTS, reason="excluding tests that depend on oncokb data")
@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (tso500 not available)")
@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (tso500 not available)"
)
def test_cancer_genes(conn):
result = get_cancer_genes(conn)
names = {row["name"] for row in result}
Expand All @@ -144,7 +149,10 @@ def test_cancer_genes(conn):
for gene in CANONICAL_ONCOGENES:
assert gene not in names

@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires CGL loader))")

@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires CGL loader))"
)
def test_get_pharmacogenomic_info(conn):
genes, matches = get_pharmacogenomic_info(conn)
for gene in PHARMACOGENOMIC_INITIAL_GENES:
Expand All @@ -158,7 +166,10 @@ def test_get_pharmacogenomic_info(conn):
continue
assert False, f"No rid found for a pharmacogenomic with {gene}"

@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires CGL loader))")

@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires CGL loader))"
)
def test_get_gene_linked_pharmacogenomic_info(conn):
genes, matches = get_gene_linked_pharmacogenomic_info(conn)
for gene in PHARMACOGENOMIC_INITIAL_GENES:
Expand All @@ -172,15 +183,19 @@ def test_get_gene_linked_pharmacogenomic_info(conn):


@pytest.mark.skipif(EXCLUDE_INTEGRATION_TESTS, reason="excluding long running integration tests")
@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires CGL loader))")
@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires CGL loader))"
)
def test_get_cancer_predisposition_info(conn):
genes, matches = get_cancer_predisposition_info(conn)
for gene in CANCER_PREDISP_INITIAL_GENES:
assert gene in genes, f"{gene} not found in get_cancer_predisposition_info"


@pytest.mark.skipif(EXCLUDE_INTEGRATION_TESTS, reason="excluding long running integration tests")
@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires CGL loader))")
@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires CGL loader))"
)
def test_get_gene_linked_cancer_predisposition_info(conn):
genes, matches = get_gene_linked_cancer_predisposition_info(conn)
for gene in CANCER_PREDISP_INITIAL_GENES:
Expand All @@ -197,7 +212,9 @@ def test_get_preferred_gene_name_kras(alt_rep, conn):
), f"Expected KRAS as preferred gene name for {alt_rep}, not '{gene_name}'"


@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires CGL loader))")
@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires CGL loader))"
)
@pytest.mark.skipif(EXCLUDE_INTEGRATION_TESTS, reason="excluding long running integration tests")
def test_find_genes_by_variant_type_structural_variant(conn):
result = get_genes_from_variant_types(conn, ["structural variant"])
Expand All @@ -214,6 +231,7 @@ def test_find_no_genes_by_variant_type_with_nonmatching_source_record_id(conn):
)
assert not result


@pytest.mark.skipif(EXCLUDE_INTEGRATION_TESTS, reason="excluding long running integration tests")
def test_get_therapeutic_associated_genes(conn):
gene_list = get_therapeutic_associated_genes(graphkb_conn=conn)
Expand All @@ -225,7 +243,11 @@ def test_get_therapeutic_associated_genes(conn):
for gene in CANNONICAL_THERAPY_GENES + CANONICAL_ONCOGENES + CANONICAL_TS:
assert gene in names, f"{gene} not found by get_therapeutic_associated_genes"

@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests (requires oncokb and other loaders))")

@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS,
reason="excluding BCGSC-specific tests (requires oncokb and other loaders))",
)
@pytest.mark.skipif(EXCLUDE_INTEGRATION_TESTS, reason="excluding long running integration tests")
def test_get_gene_information(conn):
gene_info = get_gene_information(
Expand Down
24 changes: 19 additions & 5 deletions tests/test_graphkb/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ def conn() -> GraphKBConnection:
def kras(conn):
return [f["displayName"] for f in match.get_equivalent_features(conn, "kras")]


"""
version found in the db for ENSG00000133703 will vary depending on which
version of ensembl was loaded. checking for any . version
"""
kras_ensg_version = r'ENSG00000133703\..*'


class TestGetEquivalentFeatures:
def test_kras_has_self(self, kras):
assert "KRAS" in kras
Expand Down Expand Up @@ -109,7 +111,10 @@ def test_bad_gene_name(self, conn):
with pytest.raises(FeatureNotFoundError):
match.match_copy_variant(conn, "not a real gene name", match.INPUT_COPY_CATEGORIES.AMP)

@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests - no copy loss variants in other data")
@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS,
reason="excluding BCGSC-specific tests - no copy loss variants in other data",
)
def test_known_loss(self, conn):
matches = match.match_copy_variant(conn, "CDKN2A", match.INPUT_COPY_CATEGORIES.ANY_LOSS)
assert matches
Expand All @@ -126,7 +131,10 @@ def test_known_loss(self, conn):
for variant_type in types_selected:
assert not has_prefix(variant_type, INCREASE_PREFIXES)

@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests - no copy loss variants in other data")
@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS,
reason="excluding BCGSC-specific tests - no copy loss variants in other data",
)
def test_known_loss_zygosity_filtered(self, conn):
matches = match.match_copy_variant(
conn, "CDKN2A", match.INPUT_COPY_CATEGORIES.ANY_LOSS, True
Expand Down Expand Up @@ -224,7 +232,9 @@ def test_known_reduced_expression(self, conn):
for variant_type in types_selected:
assert not has_prefix(variant_type, INCREASE_PREFIXES)

@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests - no applicable variants")
@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason="excluding BCGSC-specific tests - no applicable variants"
)
def test_known_reduced_expression_gene_id(self, conn):
gene_id = conn.query({"target": "Feature", "filters": [{"name": "PTEN"}]})[0]["@rid"]
matches = match.match_expression_variant(
Expand Down Expand Up @@ -413,7 +423,9 @@ def test_match_explicit_references(self, conn):
["EGFR:p.E746_S752delinsI", ["EGFR mutation"], ["EGFR copy variant"]],
],
)
@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason='TODO: fix loader for vars ending in X, p.?, copy variant')
@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason='TODO: fix loader for vars ending in X, p.?, copy variant'
)
def test_known_variants(self, conn, known_variant, related_variants, unrelated_variants):
matches = match.match_positional_variant(conn, known_variant)
names = {m["displayName"] for m in matches}
Expand All @@ -424,7 +436,9 @@ def test_known_variants(self, conn, known_variant, related_variants, unrelated_v
for variant in unrelated_variants:
assert variant not in names

@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="TODO: add nonIPRKB fusion tests; source for these is IPRKB")
@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason="TODO: add nonIPRKB fusion tests; source for these is IPRKB"
)
@pytest.mark.parametrize(
"known_variant,related_variants",
[
Expand Down
5 changes: 4 additions & 1 deletion tests/test_graphkb/test_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ def test_custom_categories(self, graphkb_conn):
)
assert category == "blargh"

@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason='db-specific rid; requires Inferred Functional Annotation source')

@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason='db-specific rid; requires Inferred Functional Annotation source'
)
@pytest.mark.skipif(EXCLUDE_INTEGRATION_TESTS, reason="excluding long running integration tests")
class TestStatementMatch:
def test_truncating_categories(self, conn): # noqa - pytest fixture, not redefinition
Expand Down
38 changes: 24 additions & 14 deletions tests/test_ipr/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
EXCLUDE_BCGSC_TESTS = os.environ.get("EXCLUDE_BCGSC_TESTS") == "1"
EXCLUDE_ONCOKB_TESTS = os.environ.get("EXCLUDE_ONCOKB_TESTS") == "1"


def get_test_spec():
ipr_spec = {"components": {"schemas": {"genesCreate": {"properties": {}}}}}
ipr_gene_keys = IprGene.__required_keys__ | IprGene.__optional_keys__
Expand Down Expand Up @@ -43,20 +44,27 @@ def report_upload_content(tmp_path_factory) -> Dict:
],
'patientId': 'PATIENT001',
'project': 'TEST',
'expressionVariants': json.loads(pd.read_csv(
get_test_file('expression.short.tab'), sep='\t'
).to_json(orient='records')),
'smallMutations': json.loads(pd.read_csv(
get_test_file('small_mutations.short.tab'), sep='\t'
).to_json(orient='records')),
'copyVariants': json.loads(pd.read_csv(
get_test_file('copy_variants.short.tab'), sep='\t'
).to_json(orient='records')),
'structuralVariants': json.loads(pd.read_csv(get_test_file('fusions.tab'), sep='\t').to_json(
orient='records'
)),
'expressionVariants': json.loads(
pd.read_csv(get_test_file('expression.short.tab'), sep='\t').to_json(
orient='records'
)
),
'smallMutations': json.loads(
pd.read_csv(get_test_file('small_mutations.short.tab'), sep='\t').to_json(
orient='records'
)
),
'copyVariants': json.loads(
pd.read_csv(get_test_file('copy_variants.short.tab'), sep='\t').to_json(
orient='records'
)
),
'structuralVariants': json.loads(
pd.read_csv(get_test_file('fusions.tab'), sep='\t').to_json(orient='records')
),
'kbDiseaseMatch': 'colorectal cancer',
}, allow_nan=False
},
allow_nan=False,
)
)
with patch.object(
Expand Down Expand Up @@ -108,7 +116,9 @@ def test_main_sections_present(self, report_upload_content: Dict) -> None:

def test_kept_low_quality_fusion(self, report_upload_content: Dict) -> None:
fusions = [(sv['gene1'], sv['gene2']) for sv in report_upload_content['structuralVariants']]
if EXCLUDE_BCGSC_TESTS: # may be missing statements assoc with SUZ12 if no access to bcgsc data
if (
EXCLUDE_BCGSC_TESTS
): # may be missing statements assoc with SUZ12 if no access to bcgsc data
assert ('SARM1', 'CDKL2') in fusions
else:
assert ('SARM1', 'SUZ12') in fusions
Expand Down
4 changes: 3 additions & 1 deletion tests/test_ipr/test_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class TestCreateReport:
def test_found_probe_small_mutations(self, probe_upload_content: Dict) -> None:
assert probe_upload_content["smallMutations"]

@pytest.mark.skipif(EXCLUDE_BCGSC_TESTS, reason="excluding tests that depend on BCGSC-specific data")
@pytest.mark.skipif(
EXCLUDE_BCGSC_TESTS, reason="excluding tests that depend on BCGSC-specific data"
)
def test_found_probe_small_mutations_match(self, probe_upload_content: Dict) -> None:
# verify each probe had a KB match
for sm_probe in probe_upload_content["smallMutations"]:
Expand Down

0 comments on commit c0cf636

Please sign in to comment.