diff --git a/pori_python/graphkb/util.py b/pori_python/graphkb/util.py index bbc6776..772cfae 100644 --- a/pori_python/graphkb/util.py +++ b/pori_python/graphkb/util.py @@ -98,7 +98,7 @@ def cache_key(request_body) -> str: class GraphKBConnection: def __init__( self, - url: str = os.environ.get("GRAPHKB_URL"), + url: str = os.environ.get("GRAPHKB_URL", ""), username: str = "", password: str = "", use_global_cache: bool = True, @@ -143,6 +143,8 @@ def request(self, endpoint: str, method: str = "GET", **kwargs) -> Dict: Returns: dict: the json response as a python dict """ + if not self.url: + raise ValueError("no GraphKBConnection url set - cannot make a login demo") url = join_url(self.url, endpoint) self.request_count += 1 connect_timeout = 7 @@ -222,6 +224,8 @@ def login_demo(self) -> None: 1. get a first token from KeyCloak using username and password; self.login_demo() 2. get a second token from the GraphKB API using keyCloakToken; self.login() """ + if not self.url: + raise ValueError("no GraphKBConnection url set - cannot make a login demo") url_parts = urlsplit(self.url) base_url = f"{url_parts.scheme}://{url_parts.netloc}" @@ -250,8 +254,11 @@ def login(self, username: str, password: str, pori_demo: bool = False) -> None: connect_timeout = 7 read_timeout = 61 - # KBDEV-1328. Alt. GraphKB login for GSC's PORI online demo - if pori_demo or "pori-demo" in self.url: + if not self.url: + raise ValueError("no GraphKBConnection url set - cannot login") + elif pori_demo or "pori-demo" in self.url: + # KBDEV-1328. Alt. GraphKB login for GSC's PORI online demo + logger.warning("login demo") self.login_demo() # use requests package directly to avoid recursion loop on login failure diff --git a/pori_python/ipr/inputs.py b/pori_python/ipr/inputs.py index 3cfe526..4feb12f 100644 --- a/pori_python/ipr/inputs.py +++ b/pori_python/ipr/inputs.py @@ -26,8 +26,8 @@ from .constants import ( COSMIC_SIGNATURE_VARIANT_TYPE, HLA_SIGNATURE_VARIANT_TYPE, - MSI_MAPPING, HRD_MAPPING, + MSI_MAPPING, TMB_SIGNATURE, TMB_SIGNATURE_VARIANT_TYPE, ) diff --git a/pori_python/ipr/ipr.py b/pori_python/ipr/ipr.py index d606de2..742a5a7 100644 --- a/pori_python/ipr/ipr.py +++ b/pori_python/ipr/ipr.py @@ -168,8 +168,7 @@ def convert_statements_to_alterations( diseases = [c for c in statement["conditions"] if c["@class"] == "Disease"] disease_match = len(diseases) == 1 and diseases[0]["@rid"] in disease_matches reference = ";".join([e["displayName"] for e in statement["evidence"]]) - - if statement['relevance']['name'] == 'eligibility': + if statement["relevance"]["name"] == "eligibility": reference = ";".join([e["sourceId"] for e in statement["evidence"]]) ipr_section = gkb_statement.categorize_relevance( @@ -296,10 +295,10 @@ def create_key_alterations( counts: Dict[str, Set] = {v: set() for v in type_mapping.values()} skipped_variant_types = [] - included_kbvariant_ids = list(set([item['kbVariantId'] for item in included_kb_matches])) + included_kbvariant_ids = list(set([item["kbVariantId"] for item in included_kb_matches])) for kb_match in kb_matches: - if kb_match['kbVariantId'] not in included_kbvariant_ids: + if kb_match["kbVariantId"] not in included_kbvariant_ids: continue variant_type = kb_match["variantType"] variant_key = kb_match["variant"] @@ -647,13 +646,13 @@ def get_kb_matches_sections( unique_kb_variant_ids = list( set( [ - item['kbVariantId'] + item["kbVariantId"] for conditionSet in kb_statement_matched_conditions - for item in conditionSet['matchedConditions'] + for item in conditionSet["matchedConditions"] ] ) ) - kb_variants = [item for item in kb_variants if item['kbVariantId'] in unique_kb_variant_ids] + kb_variants = [item for item in kb_variants if item["kbVariantId"] in unique_kb_variant_ids] return { "kbMatches": kb_variants, diff --git a/pori_python/ipr/main.py b/pori_python/ipr/main.py index 3194779..b0b0782 100644 --- a/pori_python/ipr/main.py +++ b/pori_python/ipr/main.py @@ -31,8 +31,8 @@ preprocess_cosmic, preprocess_expression_variants, preprocess_hla, - preprocess_msi, preprocess_hrd, + preprocess_msi, preprocess_signature_variants, preprocess_small_mutations, preprocess_structural_variants, @@ -294,7 +294,7 @@ def ipr_report( username: str, password: str, content: Dict, - ipr_url: str = '', + ipr_url: str = "", log_level: str = "info", output_json_path: str = "", always_write_output_json: bool = False, @@ -363,17 +363,17 @@ def ipr_report( if ipr_url: ipr_conn = IprConnection(username, password, ipr_url) else: - logger.warning("No ipr_url given") + logger.error("No ipr_url given with no IPR_URL environment variable") if validate_json: if not ipr_conn: - raise ValueError("ipr_url required to validate json") + raise ValueError("ipr_url required to validate_json") ipr_result = ipr_conn.validate_json(content) return ipr_result if upload_json: if not ipr_conn: - raise ValueError("ipr_url required to upload json") + raise ValueError("ipr_url required to upload_json") ipr_result = ipr_conn.upload_report( content, mins_to_wait, async_upload, ignore_extra_fields ) @@ -419,14 +419,15 @@ def ipr_report( ) # GKB CONNECTION + gkb_user = graphkb_username if graphkb_username else username + gkb_pass = graphkb_password if graphkb_password else password + graphkb_url = graphkb_url if graphkb_url else os.environ.get("GRAPHKB_URL", "") if graphkb_url: logger.info(f"connecting to graphkb: {graphkb_url}") graphkb_conn = GraphKBConnection(graphkb_url) else: - graphkb_conn = GraphKBConnection() - - gkb_user = graphkb_username if graphkb_username else username - gkb_pass = graphkb_password if graphkb_password else password + # graphkb_conn = GraphKBConnection() # This will just error on trying to login + raise ValueError("graphkb_url is required") graphkb_conn.login(gkb_user, gkb_pass) @@ -502,7 +503,7 @@ def ipr_report( if include_ipr_variant_text: if not ipr_conn: - raise ValueError("ipr_url required to to include ipr variant text") + raise ValueError("ipr_url required to include_ipr_variant_text") ipr_comments = get_ipr_analyst_comments( ipr_conn, gkb_matches, @@ -524,7 +525,7 @@ def ipr_report( # KEY ALTERATIONS key_alterations, variant_counts = create_key_alterations( - gkb_matches, all_variants, kb_matched_sections['kbMatches'] + gkb_matches, all_variants, kb_matched_sections["kbMatches"] ) # OUTPUT CONTENT @@ -563,10 +564,10 @@ def ipr_report( # if input includes hrdScore field, that is ok to pass to db # but prefer the 'hrd' field if it exists - if output.get('hrd'): - if output.get('hrd').get('score'): - output['hrdScore'] = output['hrd']['score'] - output.pop('hrd') # kbmatches have already been made + if output.get("hrd"): + if output.get("hrd").get("score"): + output["hrdScore"] = output["hrd"]["score"] + output.pop("hrd") # kbmatches have already been made ipr_result = {} upload_error = None diff --git a/tests/test_ipr/test_inputs.py b/tests/test_ipr/test_inputs.py index e99d170..da9b1dd 100644 --- a/tests/test_ipr/test_inputs.py +++ b/tests/test_ipr/test_inputs.py @@ -7,8 +7,8 @@ from pori_python.graphkb.match import INPUT_COPY_CATEGORIES from pori_python.ipr.constants import ( - MSI_MAPPING, HRD_MAPPING, + MSI_MAPPING, TMB_SIGNATURE, TMB_SIGNATURE_HIGH_THRESHOLD, ) @@ -21,8 +21,8 @@ preprocess_cosmic, preprocess_expression_variants, preprocess_hla, - preprocess_msi, preprocess_hrd, + preprocess_msi, preprocess_signature_variants, preprocess_small_mutations, preprocess_structural_variants, diff --git a/tests/test_ipr/test_ipr.py b/tests/test_ipr/test_ipr.py index 9994cf4..e7c68df 100644 --- a/tests/test_ipr/test_ipr.py +++ b/tests/test_ipr/test_ipr.py @@ -5,13 +5,13 @@ from pori_python.graphkb import vocab as gkb_vocab from pori_python.ipr.ipr import ( convert_statements_to_alterations, + create_key_alterations, germline_kb_matches, get_kb_disease_matches, get_kb_matched_statements, + get_kb_matches_sections, get_kb_statement_matched_conditions, get_kb_variants, - get_kb_matches_sections, - create_key_alterations, ) from pori_python.types import Statement @@ -497,10 +497,10 @@ def test_germline_kb_matches(self): ] ALL_VARIANTS = [ - {"variant": "var1", "key": '1', "variantType": 'mut'}, - {"variant": "var2", "key": '2', "variantType": 'mut'}, - {"variant": "var3", "key": '3', "variantType": 'mut'}, - {"variant": "var4", "key": '4', "variantType": 'mut'}, + {"variant": "var1", "key": "1", "variantType": "mut"}, + {"variant": "var2", "key": "2", "variantType": "mut"}, + {"variant": "var3", "key": "3", "variantType": "mut"}, + {"variant": "var4", "key": "4", "variantType": "mut"}, ] BASIC_GKB_MATCH = { @@ -709,8 +709,8 @@ def test_partial_matches_omitted(self): gkb_matches = create_gkb_matches(input_fields) sections = get_kb_matches_sections(gkb_matches, allow_partial_matches=False) - stmts = sections['kbMatchedStatements'] - kbcs = sections['kbStatementMatchedConditions'] + stmts = sections["kbMatchedStatements"] + kbcs = sections["kbStatementMatchedConditions"] assert len(stmts) == 2 assert len(kbcs) == 1 # X only assert kbcs[0]["kbStatementId"] == "X" @@ -796,14 +796,14 @@ def test_kbvariants_removed_from_set_when_not_part_of_full_conditionset_match(se item["kbVariant"] = "test" gkb_matches = create_gkb_matches(input_fields) sections1 = get_kb_matches_sections(gkb_matches, allow_partial_matches=False) - kbcs1 = sections1['kbStatementMatchedConditions'] - kbvars1 = sections1['kbMatches'] + kbcs1 = sections1["kbStatementMatchedConditions"] + kbvars1 = sections1["kbMatches"] assert len(kbcs1) == 1 # only fully matched condition sets included assert len(kbvars1) == 2 # therefore, kbvars associated with stmt X are pruned sections2 = get_kb_matches_sections(gkb_matches, allow_partial_matches=True) - kbcs2 = sections2['kbStatementMatchedConditions'] - kbvars2 = sections2['kbMatches'] + kbcs2 = sections2["kbStatementMatchedConditions"] + kbvars2 = sections2["kbMatches"] assert len(kbcs2) == 2 # all condition sets included assert len(kbvars2) == 3 # therefore, no pruning @@ -844,12 +844,12 @@ def test_create_key_alterations_includes_only_pruned_kbmatches(self): sections1 = get_kb_matches_sections(gkb_matches, allow_partial_matches=False) key_alts1, counts1 = create_key_alterations( - gkb_matches, ALL_VARIANTS, sections1['kbMatches'] + gkb_matches, ALL_VARIANTS, sections1["kbMatches"] ) sections2 = get_kb_matches_sections(gkb_matches, allow_partial_matches=True) key_alts2, counts2 = create_key_alterations( - gkb_matches, ALL_VARIANTS, sections2['kbMatches'] + gkb_matches, ALL_VARIANTS, sections2["kbMatches"] ) # check partial-match-only variants are not included in key alterations when