Skip to content

Commit

Permalink
lint - isort black - line-len 100
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinbleile committed Jul 12, 2024
1 parent 7e67692 commit db7c199
Show file tree
Hide file tree
Showing 28 changed files with 235 additions and 835 deletions.
73 changes: 14 additions & 59 deletions pori_python/graphkb/genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@


def _get_tumourigenesis_genes_list(
conn: GraphKBConnection,
relevance: str,
sources: List[str],
ignore_cache: bool = False,
conn: GraphKBConnection, relevance: str, sources: List[str], ignore_cache: bool = False
) -> List[Ontology]:
statements = cast(
List[Statement],
Expand All @@ -37,17 +34,10 @@ def _get_tumourigenesis_genes_list(
"filters": {
"AND": [
{"source": {"target": "Source", "filters": {"name": sources}}},
{
"relevance": {
"target": "Vocabulary",
"filters": {"name": relevance},
}
},
{"relevance": {"target": "Vocabulary", "filters": {"name": relevance}}},
]
},
"returnProperties": [
f"subject.{prop}" for prop in GENE_RETURN_PROPERTIES
],
"returnProperties": [f"subject.{prop}" for prop in GENE_RETURN_PROPERTIES],
},
ignore_cache=ignore_cache,
),
Expand Down Expand Up @@ -84,9 +74,7 @@ def get_oncokb_tumour_supressors(conn: GraphKBConnection) -> List[Ontology]:
Returns:
gene (Feature) records
"""
return _get_tumourigenesis_genes_list(
conn, TUMOUR_SUPPRESSIVE, [ONCOKB_SOURCE_NAME]
)
return _get_tumourigenesis_genes_list(conn, TUMOUR_SUPPRESSIVE, [ONCOKB_SOURCE_NAME])


def get_cancer_genes(conn: GraphKBConnection) -> List[Ontology]:
Expand Down Expand Up @@ -159,12 +147,7 @@ def get_genes_from_variant_types(
filters: List[Dict[str, Any]] = []
if types:
filters.append(
{
"type": {
"target": "Vocabulary",
"filters": {"name": types, "operator": "IN"},
}
}
{"type": {"target": "Vocabulary", "filters": {"name": types, "operator": "IN"}}}
)

variants = cast(
Expand Down Expand Up @@ -194,11 +177,7 @@ def get_genes_from_variant_types(
result = cast(
List[Ontology],
conn.query(
{
"target": list(genes),
"returnProperties": GENE_RETURN_PROPERTIES,
"filters": filters,
},
{"target": list(genes), "returnProperties": GENE_RETURN_PROPERTIES, "filters": filters},
ignore_cache=ignore_cache,
),
)
Expand Down Expand Up @@ -294,12 +273,7 @@ def get_gene_linked_cancer_predisposition_info(
"filters": {"@rid": get_rid(conn, "Source", "CGL")},
}
},
{
"relevance": {
"target": "Vocabulary",
"filters": {"@rid": relevance_rids},
}
},
{"relevance": {"target": "Vocabulary", "filters": {"@rid": relevance_rids}}},
]
},
"returnProperties": [
Expand Down Expand Up @@ -333,10 +307,7 @@ def get_gene_linked_cancer_predisposition_info(
logger.error(
f"Non-gene cancer predisposition {biotype}: {name} for {condition['displayName']}"
)
variants[condition["@rid"]] = [
condition["displayName"],
assoc_gene_list,
]
variants[condition["@rid"]] = [condition["displayName"], assoc_gene_list]

for gene, name, biotype in infer_genes:
logger.debug(f"Found gene '{gene}' for '{name}' ({biotype})")
Expand Down Expand Up @@ -388,12 +359,7 @@ def get_gene_linked_pharmacogenomic_info(
{
"target": "Statement",
"filters": [
{
"relevance": {
"target": "Vocabulary",
"filters": {"@rid": relevance_rids},
}
}
{"relevance": {"target": "Vocabulary", "filters": {"@rid": relevance_rids}}}
],
"returnProperties": [
"conditions.@class",
Expand Down Expand Up @@ -431,10 +397,7 @@ def get_gene_linked_pharmacogenomic_info(
logger.error(
f"Non-gene pharmacogenomic {biotype}: {name} for {condition['displayName']}"
)
variants[condition["@rid"]] = [
condition["displayName"],
assoc_gene_list,
]
variants[condition["@rid"]] = [condition["displayName"], assoc_gene_list]
for gene, name, biotype in infer_genes:
logger.debug(f"Found gene '{gene}' for '{name}' ({biotype})")
genes.add(gene)
Expand Down Expand Up @@ -486,9 +449,7 @@ def get_gene_information(

gene_names = sorted(set(gene_names))
statements = graphkb_conn.query(body)
statements = [
s for s in statements if s.get("reviewStatus") != FAILED_REVIEW_STATUS
]
statements = [s for s in statements if s.get("reviewStatus") != FAILED_REVIEW_STATUS]

gene_flags: Dict[str, Set[str]] = {
"kbStatementRelated": set(),
Expand All @@ -511,13 +472,9 @@ def get_gene_information(
logger.info("fetching oncogenes list")
gene_flags["oncogene"] = convert_to_rid_set(get_oncokb_oncogenes(graphkb_conn))
logger.info("fetching tumour supressors list")
gene_flags["tumourSuppressor"] = convert_to_rid_set(
get_oncokb_tumour_supressors(graphkb_conn)
)
gene_flags["tumourSuppressor"] = convert_to_rid_set(get_oncokb_tumour_supressors(graphkb_conn))
logger.info("fetching cancerGeneListMatch list")
gene_flags["cancerGeneListMatch"] = convert_to_rid_set(
get_cancer_genes(graphkb_conn)
)
gene_flags["cancerGeneListMatch"] = convert_to_rid_set(get_cancer_genes(graphkb_conn))

logger.info("fetching therapeutic associated genes lists")
gene_flags["therapeuticAssociated"] = convert_to_rid_set(
Expand All @@ -527,9 +484,7 @@ def get_gene_information(
logger.info(f"Setting gene_info flags on {len(gene_names)} genes")
result = []
for gene_name in gene_names:
equivalent = convert_to_rid_set(
get_equivalent_features(graphkb_conn, gene_name)
)
equivalent = convert_to_rid_set(get_equivalent_features(graphkb_conn, gene_name))
row = {"name": gene_name}
flagged = False
for flag in gene_flags:
Expand Down
90 changes: 17 additions & 73 deletions pori_python/graphkb/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
STRUCTURAL_VARIANT_TYPES,
VARIANT_RETURN_PROPERTIES,
)
from .types import (
BasicPosition,
Ontology,
ParsedVariant,
PositionalVariant,
Record,
Variant,
)
from .types import BasicPosition, Ontology, ParsedVariant, PositionalVariant, Record, Variant
from .util import (
FeatureNotFoundError,
convert_to_rid_list,
Expand Down Expand Up @@ -70,8 +63,7 @@ def get_equivalent_features(
return cast(
List[Ontology],
conn.query(
{"target": [gene_name], "queryType": "similarTo"},
ignore_cache=ignore_cache,
{"target": [gene_name], "queryType": "similarTo"}, ignore_cache=ignore_cache
),
)

Expand All @@ -90,27 +82,17 @@ def get_equivalent_features(
filters.append({"sourceId": gene_name})
if source_id_version:
filters.append(
{
"OR": [
{"sourceIdVersion": source_id_version},
{"sourceIdVersion": None},
]
}
{"OR": [{"sourceIdVersion": source_id_version}, {"sourceIdVersion": None}]}
)
elif (
FEATURES_CACHE and gene_name.lower() not in FEATURES_CACHE and not ignore_cache
):
elif FEATURES_CACHE and gene_name.lower() not in FEATURES_CACHE and not ignore_cache:
return []
else:
filters.append({"OR": [{"sourceId": gene_name}, {"name": gene_name}]})

return cast(
List[Ontology],
conn.query(
{
"target": {"target": "Feature", "filters": filters},
"queryType": "similarTo",
},
{"target": {"target": "Feature", "filters": filters}, "queryType": "similarTo"},
ignore_cache=ignore_cache,
),
)
Expand All @@ -123,13 +105,7 @@ def cache_missing_features(conn: GraphKBConnection) -> None:
"""
genes = cast(
List[Ontology],
conn.query(
{
"target": "Feature",
"returnProperties": ["name", "sourceId"],
"neighbors": 0,
}
),
conn.query({"target": "Feature", "returnProperties": ["name", "sourceId"], "neighbors": 0}),
)
for gene in genes:
if gene["name"]:
Expand Down Expand Up @@ -184,9 +160,7 @@ def match_category_variant(
)

if not terms:
raise ValueError(
f"unable to find the term/category ({category}) or any equivalent"
)
raise ValueError(f"unable to find the term/category ({category}) or any equivalent")

# find the variant list
return cast(
Expand All @@ -201,12 +175,7 @@ def match_category_variant(
],
},
"queryType": "similarTo",
"edges": [
"AliasOf",
"DeprecatedBy",
"CrossReferenceOf",
"GeneralizationOf",
],
"edges": ["AliasOf", "DeprecatedBy", "CrossReferenceOf", "GeneralizationOf"],
"treeEdges": ["Infers"],
"returnProperties": VARIANT_RETURN_PROPERTIES,
},
Expand All @@ -216,11 +185,7 @@ def match_category_variant(


def match_copy_variant(
conn: GraphKBConnection,
gene_name: str,
category: str,
drop_homozygous: bool = False,
**kwargs,
conn: GraphKBConnection, gene_name: str, category: str, drop_homozygous: bool = False, **kwargs
) -> List[Variant]:
"""
Returns a list of variants matching the input variant
Expand Down Expand Up @@ -261,9 +226,7 @@ def match_expression_variant(


def positions_overlap(
pos_record: BasicPosition,
range_start: BasicPosition,
range_end: Optional[BasicPosition] = None,
pos_record: BasicPosition, range_start: BasicPosition, range_end: Optional[BasicPosition] = None
) -> bool:
"""
Check if 2 Position records from GraphKB indicate an overlap
Expand Down Expand Up @@ -387,14 +350,9 @@ def compare_positional_variants(
reference_variant["untemplatedSeq"] not in AMBIGUOUS_AA
and variant["untemplatedSeq"] not in AMBIGUOUS_AA
):
if (
reference_variant["untemplatedSeq"].lower()
!= variant["untemplatedSeq"].lower()
):
if reference_variant["untemplatedSeq"].lower() != variant["untemplatedSeq"].lower():
return False
elif len(variant["untemplatedSeq"]) != len(
reference_variant["untemplatedSeq"]
):
elif len(variant["untemplatedSeq"]) != len(reference_variant["untemplatedSeq"]):
return False

# If both variants have a reference sequence,
Expand Down Expand Up @@ -478,9 +436,7 @@ def type_screening(
if parsed.get("reference2", None):
return parsed["type"]
prefix = parsed.get("prefix", "g")
if (
prefix == "y"
): # Assuming all variations using cytoband coordiantes meet the size threshold
if prefix == "y": # Assuming all variations using cytoband coordiantes meet the size threshold
return parsed["type"]

# When size cannot be determined: exonic and intronic coordinates
Expand Down Expand Up @@ -575,11 +531,7 @@ def match_positional_variant(
gene1 = parsed["reference1"]

gene1_features = get_equivalent_features(
conn,
gene1,
source=gene_source,
is_source_id=gene_is_source_id,
ignore_cache=ignore_cache,
conn, gene1, source=gene_source, is_source_id=gene_is_source_id, ignore_cache=ignore_cache
)
features = convert_to_rid_list(gene1_features)

Expand Down Expand Up @@ -630,15 +582,12 @@ def match_positional_variant(
]

filtered_similarOnly: List[Record] = [] # For post filter match use
filtered_similarAndGeneric: List[Record] = (
[]
) # To be added to the matches at the very end
filtered_similarAndGeneric: List[Record] = [] # To be added to the matches at the very end

for row in cast(
List[Record],
conn.query(
{"target": "PositionalVariant", "filters": query_filters},
ignore_cache=ignore_cache,
{"target": "PositionalVariant", "filters": query_filters}, ignore_cache=ignore_cache
),
):
# TODO: Check if variant and reference_variant should be interchanged
Expand All @@ -661,12 +610,7 @@ def match_positional_variant(
{
"target": convert_to_rid_list(filtered_similarOnly),
"queryType": "similarTo",
"edges": [
"AliasOf",
"DeprecatedBy",
"CrossReferenceOf",
"GeneralizationOf",
],
"edges": ["AliasOf", "DeprecatedBy", "CrossReferenceOf", "GeneralizationOf"],
"treeEdges": ["Infers"],
"returnProperties": POS_VARIANT_RETURN_PROPERTIES,
},
Expand Down
19 changes: 4 additions & 15 deletions pori_python/graphkb/statement.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from typing import List, cast

from . import GraphKBConnection
from .constants import (
FAILED_REVIEW_STATUS,
RELEVANCE_BASE_TERMS,
STATEMENT_RETURN_PROPERTIES,
)
from .constants import FAILED_REVIEW_STATUS, RELEVANCE_BASE_TERMS, STATEMENT_RETURN_PROPERTIES
from .types import CategoryBaseTermMapping, Statement, Variant
from .util import convert_to_rid_list
from .vocab import get_terms_set
Expand All @@ -27,9 +23,7 @@ def categorize_relevance(


def get_statements_from_variants(
graphkb_conn: GraphKBConnection,
variants: List[Variant],
failed_review: bool = False,
graphkb_conn: GraphKBConnection, variants: List[Variant], failed_review: bool = False
) -> List[Statement]:
"""Given a list of variant records from GraphKB, return related statements.
Expand All @@ -44,15 +38,10 @@ def get_statements_from_variants(
statements = graphkb_conn.query(
{
"target": "Statement",
"filters": {
"conditions": convert_to_rid_list(variants),
"operator": "CONTAINSANY",
},
"filters": {"conditions": convert_to_rid_list(variants), "operator": "CONTAINSANY"},
"returnProperties": STATEMENT_RETURN_PROPERTIES,
}
)
if not failed_review:
statements = [
s for s in statements if s.get("reviewStatus") != FAILED_REVIEW_STATUS
]
statements = [s for s in statements if s.get("reviewStatus") != FAILED_REVIEW_STATUS]
return [cast(Statement, s) for s in statements]
Loading

0 comments on commit db7c199

Please sign in to comment.