def get_xqtl_endpoints( endpoint: models.TopicViewEndpoints, xqtl_mode: XqtlMode = XqtlMode.multi_snp_mr, exposure_gene: Optional[str] = None, outcome_trait: Optional[str] = None, variant: Optional[str] = None, mr_method: XqtlMrMethod = XqtlMrMethod.ivw, qtl_type: XqtlQtlType = XqtlQtlType.eqtl, pval_threshold: float = Query(1e-5, ge=0.0, le=1.0), rels_limit: int = rels_limit, overwrite: bool = False, ): log_args(api=f"/xqtl/{endpoint.value}", kwargs=locals()) validate_input(locals()) processor = XqtlQueryProcessor( params={ "xqtl_mode": xqtl_mode.value, "exposure_gene": exposure_gene, "outcome_trait": outcome_trait, "variant": variant, "mr_method": mr_method.value, "qtl_type": qtl_type.value, "pval_threshold": pval_threshold, }) res = None if endpoint.value == "table": res = processor.get_table_data(overwrite=overwrite) elif endpoint.value == "network-plot": res = processor.get_network_plot_data(rels_limit=rels_limit, overwrite=overwrite) elif endpoint.value == "query": res = processor.get_query_data(overwrite=overwrite) elif endpoint.value == "query-diagram": res = processor.get_query_diagram_data() return res
def get_xqtl_list( meta_node: XqtlListMetaNodeInput, pval_threshold: float = Query(1e-5, ge=0.0, le=1.0), qtl_type: QtlTypeInput = QtlTypeInput.eqtl, ): log_args(api="/xqtl/single-snp-mr/list", kwargs=locals()) if meta_node.value == "GeneGwas": query = xqtl_queries.XqtlSingleSnpMrList.gene_gwas.format( qtl_type=qtl_type.value, pval_threshold=pval_threshold ) elif meta_node.value == "Gwas": query = xqtl_queries.XqtlSingleSnpMrList.gwas.format( qtl_type=qtl_type.value, pval_threshold=pval_threshold ) else: query = xqtl_queries.XqtlSingleSnpMrList.gene.format( qtl_type=qtl_type.value, pval_threshold=pval_threshold ) response = epigraphdb.run_query(query) res = ( pd.json_normalize(response["results"]) .drop_duplicates() .to_dict(orient="records") ) # TODO: formalise this res = {"metadata": [], "results": res} return res
def get_mr_endpoints( endpoint: models.TopicViewEndpoints, exposure_trait: Optional[str] = None, outcome_trait: Optional[str] = None, pval_threshold: float = Query(1e-5, ge=0.0, le=1.0), rels_limit: int = rels_limit, overwrite: bool = False, ): log_args(api=f"/mr/{endpoint.value}", kwargs=locals()) validate_input(locals()) processor = MRQueryProcessor( params={ "exposure_trait": exposure_trait, "outcome_trait": outcome_trait, "pval_threshold": pval_threshold, }) res = None if endpoint.value == "table": res = processor.get_table_data(overwrite=overwrite) elif endpoint.value == "network-plot": res = processor.get_network_plot_data(rels_limit=rels_limit, overwrite=overwrite) elif endpoint.value == "query": res = processor.get_query_data(overwrite=overwrite) elif endpoint.value == "query-diagram": res = processor.get_query_diagram_data() return res
def get_literature_trait_endpoints( endpoint: models.TopicViewEndpoints, trait: str, semmed_predicate: Optional[str] = None, pval_threshold: float = 1e-5, limit: int = 500, rels_limit: int = rels_limit, overwrite: bool = False, ): log_args(api=f"/literature_trait/{endpoint.value}", kwargs=locals()) semmed_predicates = ([semmed_predicate] if semmed_predicate is not None else None) processor = LiteratureTraitQueryProcessor( params={ "trait": trait, "semmed_predicates": semmed_predicates, "pval_threshold": pval_threshold, "limit": limit, }) res = None if endpoint.value == "table": res = processor.get_table_data(overwrite=overwrite) elif endpoint.value == "network-plot": res = processor.get_network_plot_data(rels_limit=rels_limit, overwrite=overwrite) elif endpoint.value == "query": res = processor.get_query_data(overwrite=overwrite) elif endpoint.value == "query-diagram": res = processor.get_query_diagram_data() return res
def get_mr( exposure_trait: Optional[str] = None, outcome_trait: Optional[str] = None, pval_threshold: float = Query(1e-5, ge=0.0, le=1.0), ): """ Return information related to Mendelian randomisation Specify at least one of `exposure_trait` and `outcome_trait` or both. """ log_args(api="/mr", kwargs=locals()) validate_at_least_one_not_none( dict(exposure_trait=exposure_trait, outcome_trait=outcome_trait)) if exposure_trait is not None and outcome_trait is not None: query = MRQueries.pair.format( exposure_trait=exposure_trait, outcome_trait=outcome_trait, pval_threshold=pval_threshold, ) res = epigraphdb.run_query(query) elif exposure_trait is not None: query = MRQueries.exp.format(exposure_trait=exposure_trait, pval_threshold=pval_threshold) res = epigraphdb.run_query(query) elif outcome_trait is not None: query = MRQueries.out.format(outcome_trait=outcome_trait, pval_threshold=pval_threshold) res = epigraphdb.run_query(query) return res
def get_confounder_endpoints( endpoint: models.TopicViewEndpoints, exposure_trait: str, outcome_trait: str, confounder_type: ConfounderType = ConfounderType.confounder, pval_threshold: float = Query(1e-5, ge=0.0, le=1.0), rels_limit: int = rels_limit, overwrite: bool = False, ): log_args(api=f"/confounder/{endpoint}", kwargs=locals()) processor = ConfounderQueryProcessor( params={ "exposure_trait": exposure_trait, "outcome_trait": outcome_trait, "type": confounder_type.value, "pval_threshold": pval_threshold, }, type=confounder_type.value, ) res = None if endpoint.value == "table": res = processor.get_table_data(overwrite=overwrite) elif endpoint.value == "network-plot": res = processor.get_network_plot_data(rels_limit=rels_limit, overwrite=overwrite) elif endpoint.value == "query": res = processor.get_query_data(overwrite=overwrite) elif endpoint.value == "query-diagram": res = processor.get_query_diagram_data() return res
def get_xqtl( xqtl_mode: XqtlMode = XqtlMode.multi_snp_mr, exposure_gene: Optional[str] = None, outcome_trait: Optional[str] = None, variant: Optional[str] = None, mr_method: XqtlMrMethod = XqtlMrMethod.ivw, qtl_type: XqtlQtlType = XqtlQtlType.eqtl, pval_threshold: float = Query(1e-5, ge=0.0, le=1.0), overwrite: bool = False, ) -> bool: """This is the master processor. For actual data use sub-apis""" log_args(api="/xqtl", kwargs=locals()) validate_input(locals()) processor = XqtlQueryProcessor( params={ "xqtl_mode": xqtl_mode.value, "exposure_gene": exposure_gene, "outcome_trait": outcome_trait, "variant": variant, "mr_method": mr_method.value, "qtl_type": qtl_type.value, "pval_threshold": pval_threshold, }) res = processor.process_master(overwrite=overwrite) return res
def get_desc_stats( query: str, db: EpigraphdbGraphsExtended = EpigraphdbGraphsExtended.epigraphdb, hostname: Optional[str] = None, bolt_port: Optional[str] = None, user: Optional[str] = None, password: Optional[str] = None, ): """Converts query response to a pandas df, then returns summary stats from df """ log_args(api="/analysis/descriptive-status", kwargs=locals()) url = f"{api_url}/raw_cypher/" r = requests.get( url, params={ "query": query, "db": db.value, "hostname": hostname, "bolt_port": bolt_port, "user": user, "password": password, }, headers=api_request_headers, ) r.raise_for_status() df = pd.json_normalize(r.json()["results"]) res = generate_df_descriptive_stats(df) print(res) return res
def get_ontology_trait_disease_endpoints( endpoint: models.TopicViewEndpoints, trait: Optional[str] = None, efo_term: Optional[str] = None, disease_label: Optional[str] = None, rels_limit: int = rels_limit, overwrite: bool = False, ): log_args(api=f"/ontology_trait_disease/{endpoint.value}", kwargs=locals()) processor = OntologyTraitDiseaseQueryProcessor( params={ "trait": trait, "efo_term": efo_term, "disease_label": disease_label, "score_threshold": 0.7, } ) res = None if endpoint.value == "table": res = processor.get_table_data(overwrite=overwrite) elif endpoint.value == "network-plot": res = processor.get_network_plot_data( rels_limit=rels_limit, overwrite=overwrite ) elif endpoint.value == "query": res = processor.get_query_data(overwrite=overwrite) elif endpoint.value == "query-diagram": res = processor.get_query_diagram_data() return res
def get_ontology_disease_efo( disease_label: Optional[str] = None, efo_term: Optional[str] = None, fuzzy: bool = True, ): """ Map Disease label to EFO term, via `MONDO_MAP_EFO` """ log_args(api="/ontology/disease-efo", kwargs=locals()) validate_at_least_one_not_none( dict(disease_label=disease_label, efo_term=efo_term)) validate_char_length(dict(disease_label=disease_label, efo_term=efo_term)) eq_symbol = "=" if fuzzy: disease_label = cypher_fuzzify(disease_label) efo_term = cypher_fuzzify(efo_term) eq_symbol = "=~" if disease_label is not None and efo_term is not None: query = queries.DiseaseEfo.disease_efo.format( disease_label=disease_label, efo_term=efo_term, eq_symbol=eq_symbol) elif disease_label is not None: query = queries.DiseaseEfo.disease.format(disease_label=disease_label, eq_symbol=eq_symbol) elif efo_term is not None: query = queries.DiseaseEfo.efo.format(efo_term=efo_term, eq_symbol=eq_symbol) res = epigraphdb.run_query(query) return res
def get_confounder_ac_index(overwrite: bool = False) -> bool: log_args(api="/confounder/ac/index", kwargs=locals()) return index_ac_name_from_cypher( cypher_query=ac_mr_trait["query"], index_name=ac_mr_trait["index_name"], es_client=es_client, overwrite=overwrite, )
def post_xqtl_gene_by_variant(data: GeneByVariantRequest): """Get the list of genes associated by an instrument SNP, nested per SNP""" log_args(api="/xqtl/single-snp-mr/gene-by-variant", kwargs=locals()) query = xqtl_queries.GeneByVariant.query.format( variant_list=str(data.variant_list), qtl_type=data.qtl_type ) res = epigraphdb.run_query(query) return res
def get_prs_ac_index(overwrite: bool = False) -> bool: log_args(api="/prs/ac/index", kwargs=locals()) return index_ac_name_from_cypher( cypher_query=ac_query_trait, index_name=ac_index_trait, es_client=es_client, overwrite=overwrite, )
def get_gene_drugs(gene_name: str): """ Get the aasociated drugs for a gene. """ log_args(api="/gene/drugs", kwargs=locals()) query = queries.Drugs.query.format(gene_name=gene_name) res = epigraphdb.run_query(query) return res
def get_prs_ac_trait(query: str, size: int = 20) -> List[str]: log_args(api="/prs/ac/trait", kwargs=locals()) if not es_client.indices.exists(index=ac_index_trait): get_prs_ac_index() res = query_ac_name(query=query, index_name=ac_index_trait, es_client=es_client, size=size) return res
def get_list_gwas(entity: models.CovidXqtlList): log_args(api=f"/covid-19/ctda/list/{entity}", kwargs=locals()) r = requests.get( f"{api_url}/covid-19/ctda/list/{entity.value}", headers=api_request_headers, ) r.raise_for_status() res = r.json()["results"] return res
def post_api(data: ApiRequest): log_args(api="/api", kwargs=locals()) url = f"{api_url}{data.endpoint}" if data.method == "GET": r = requests.get(url, params=data.params, headers=api_request_headers) elif data.method == "POST": r = requests.post(url, json=data.params, headers=api_request_headers) r.raise_for_status() return r.json()
def get_gene_druggability_ppi(gene_name: str): """ For a gene, search for its associated druggable genes via protein-protein-interaction (INTACT and STRING) """ log_args(api="/gene/druggability/ppi", kwargs=locals()) query = queries.Druggability.ppi.format(gene_name=gene_name) res = epigraphdb.run_query(query) return res
def get_schema_plot(): log_args(api="/api", kwargs=locals()) url = f"{api_url}/meta/schema" r = requests.get( url, params={"graphviz": True, "plot": True}, headers=api_request_headers, ) r.raise_for_status() return StreamingResponse(io.BytesIO(r.content), media_type="image/png")
def get_pathway(trait: str, pval_threshold: float = Query(1e-5, ge=0.0, le=1.0)): """ Pathway-based stratification of instruments """ log_args(api="/pathway", kwargs=locals()) query = PathwayQueries.pathway.format(trait=trait, pval_threshold=pval_threshold) response = epigraphdb.run_query(query) return response
def get_gene_literature(gene_name: str, object_name: str): """ For a gene, search for its literature evidence related to a SemMedDB term """ log_args(api="/gene/literature", kwargs=locals()) query = queries.Literature.query.format(gene_name=gene_name, object_name=object_name) res = epigraphdb.run_query(query) return res
def get_pqtl_list(flag: ListFlagInput): """Returns either the list of all proteins or traits in the database "searchable_entities": {"flag": "Search for 'outcomes' or 'exposures'"} """ log_args(api="/pqtl/list/", kwargs=locals()) data = pQTL_list(flag.value) return data
def get_drugs_risk_factors(trait: str = None, pval_threshold: float = Query(1e-8, ge=0.0, le=1.0)): """ Drugs for common risk factors of diseases """ log_args(api="/drugs/risk-factors", kwargs=locals()) query = DrugsQueries.risk_factors.format(trait=trait, pval_threshold=pval_threshold) response = epigraphdb.run_query(query) return response
def nodes_search_neighbour( meta_node: str, id: Optional[str], limit: int = Query(50, ge=1, le=200) ): """Search the neighbour nodes adjacent to the query node.""" log_args(api="/meta/nodes/{meta_node}/search-neighbour", kwargs=locals()) query = nodes_neighbour_query_builder( meta_node=meta_node, id=id, limit=limit ) logger.info(query) response = epigraphdb.run_query(query) return response
def get_confounder_ac_trait(query: str, size: int = 20) -> List[str]: log_args(api="/confounder/ac/trait", kwargs=locals()) if not es_client.indices.exists(index=ac_mr_trait["index_name"]): get_confounder_ac_index() res = query_ac_name( query=query, index_name=ac_mr_trait["index_name"], es_client=es_client, size=size, ) return res
def get_pqtl_pleio(rsid: str, prflag: PrflagInput): """Returns the number or the list of associated proteins in the database - `rsid`: SNP rs_ID e.g., rs1260326 - `prflag`: - "count" - "proteins" """ log_args(api="/pqtl/pleio/", kwargs=locals()) data = calc_flag_pleio(rsid, prflag.value) return data
def get_xqtl_ac(name: XqtlAcIndex, query: str, size: int = 20) -> List[str]: log_args(api=f"/xqtl/ac/{name}", kwargs=locals()) ac_index = ac_configs[name.value] if not es_client.indices.exists(index=ac_index): get_xqtl_ac_index() res = ac_configs[name]["query_fn"]( query=query, index_name=ac_configs[name]["index_name"], es_client=es_client, size=size, ) return res
def get_prs( trait: str, pval_threshold: float = Query(1e-5, ge=0.0, le=1.0), overwrite: bool = False, ) -> bool: """This is the master processor. For actual data use sub-apis""" log_args(api="/prs", kwargs=locals()) processor = PrsQueryProcessor(params={ "trait": trait, "pval_threshold": pval_threshold }) res = processor.process_master(overwrite=overwrite) return res
def rels_list( meta_rel: EpigraphdbMetaRels, limit: int = Query(10, ge=1, le=2000), offset: int = 0, ): """List relationships under a meta relationship.""" log_args(api="/meta/rels/{meta_rel}/list", kwargs=locals()) query = MetaQueries.get_rels.format( meta_rel=meta_rel.value, skip=offset, limit=limit ) logger.info(query) response = epigraphdb.run_query(query) return response
def get_obs_cor( trait: str, cor_coef_threshold: float = Query(0.8, ge=-1.0, le=1.0), overwrite: bool = False, ) -> bool: """This is the master processor. For actual data use sub-apis""" log_args(api="/obs-cor", kwargs=locals()) processor = ObsCorQueryProcessor(params={ "trait": trait, "cor_coef_threshold": cor_coef_threshold }) res = processor.process_master(overwrite=overwrite) return res