|
| 1 | +__all__ = ["process_json_file", "process_json", "process_pmc"] |
| 2 | +""" |
| 3 | +This module implements an API for the textToKnowledgeGraph |
| 4 | +method which extracts BEL statements from publications via an LLM. |
| 5 | +
|
| 6 | +This module provides two integration modes: |
| 7 | +
|
| 8 | +Offline processing |
| 9 | + In this mode, a JSON file output from textToKnowledgeGraph |
| 10 | + is used as the starting point from which INDRA Statements are produced. |
| 11 | +
|
| 12 | +Live processing |
| 13 | + If the `texttoknowledgegraph` package is installed, calls |
| 14 | + the LLM extraction pipeline, processes the returned BEL relations |
| 15 | + and produces INDRA Statements. |
| 16 | +
|
| 17 | +Both modes produce an TkgProcessor instance containing INDRA |
| 18 | +Statements derived from BEL expressions. |
| 19 | +""" |
| 20 | + |
| 21 | +import os |
| 22 | +import json |
| 23 | +import logging |
| 24 | +from pathlib import Path |
| 25 | +from typing import Dict, Union |
| 26 | + |
| 27 | +from indra import get_config |
| 28 | +from .processor import TkgProcessor |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +def process_json_file(path: Union[str, Path]): |
| 35 | + """Process a single textToKnowledgeGraph JSON results file. |
| 36 | +
|
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + path : str or Path |
| 40 | + Path to a JSON file containing BEL relations. |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + TkgProcessor |
| 45 | + Processor containing the converted INDRA Statements. |
| 46 | + """ |
| 47 | + path = Path(path) |
| 48 | + logger.debug("Processing LLM-BEL results file: %s", path) |
| 49 | + |
| 50 | + with open(path, "r") as fh: |
| 51 | + data = json.load(fh) |
| 52 | + |
| 53 | + return process_json(data) |
| 54 | + |
| 55 | + |
| 56 | +def process_json(data: Dict): |
| 57 | + """Process BEL relations returned directly from the LLM engine. |
| 58 | +
|
| 59 | + Parameters |
| 60 | + ---------- |
| 61 | + data : dict |
| 62 | + Dictionary containing at least a ``"relations"`` field. |
| 63 | +
|
| 64 | + Returns |
| 65 | + ------- |
| 66 | + TkgProcessor |
| 67 | + Processor with INDRA Statements derived from BEL. |
| 68 | + """ |
| 69 | + processor = TkgProcessor(data) |
| 70 | + processor.extract_statements() |
| 71 | + return processor |
| 72 | + |
| 73 | + |
| 74 | +def process_pmc(pmc_id: str, output_base_path, **kwargs): |
| 75 | + """Run live BEL extraction using textToKnowledgeGraph, if installed. |
| 76 | +
|
| 77 | + Parameters |
| 78 | + ---------- |
| 79 | + pmc_id : str |
| 80 | + PMCID such as 'PMC3898398'. |
| 81 | + kwargs : |
| 82 | + Additional keyword arguments passed to textToKnowledgeGraph.main(). |
| 83 | +
|
| 84 | + Returns |
| 85 | + ------- |
| 86 | + TkgProcessor |
| 87 | + Processor containing INDRA Statements derived from live BEL output. |
| 88 | +
|
| 89 | + Raises |
| 90 | + ------ |
| 91 | + ImportError |
| 92 | + If textToKnowledgeGraph is not installed. |
| 93 | + ValueError |
| 94 | + If the returned data structure is unexpected. |
| 95 | + """ |
| 96 | + try: |
| 97 | + from textToKnowledgeGraph import main as tkg_main |
| 98 | + except ImportError: |
| 99 | + raise ImportError( |
| 100 | + "The 'textToKnowledgeGraph' package is not installed. " |
| 101 | + "Install it or run textToKnowledgeGraph separately to " |
| 102 | + "produce output files and then use one of the functions like " |
| 103 | + "process_json_file to process the outputs." |
| 104 | + ) |
| 105 | + |
| 106 | + api_key = get_config('OPENAI_API_KEY', failure_ok=False) |
| 107 | + |
| 108 | + logger.debug("Running live textToKnowledgeGraph extraction for %s", pmc_id) |
| 109 | + |
| 110 | + success = tkg_main( |
| 111 | + api_key=api_key, |
| 112 | + pmc_ids=[pmc_id], |
| 113 | + upload_to_ndex=False, |
| 114 | + # Note: this assumes https://github.com/ndexbio/llm-text-to-knowledge-graph/pull/27 |
| 115 | + # will be merged |
| 116 | + output_base_path=output_base_path, |
| 117 | + **kwargs, |
| 118 | + ) |
| 119 | + |
| 120 | + if success: |
| 121 | + # TKG doesn't explicitly say where the results will be put so we need to |
| 122 | + # construct this path ourselves |
| 123 | + output_path = os.path.join(output_base_path, 'results', pmc_id, |
| 124 | + 'llm_results.json') |
| 125 | + |
| 126 | + return process_json_file(output_path) |
| 127 | + return None |
0 commit comments