File size: 892 Bytes
4704777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from .base_agent import BaseAgent
from rdflib import Graph, Namespace
from typing import List, Dict
import logging

class AlternativeAgent(BaseAgent):
    def __init__(self, rdf_graph: Graph, namespace: Namespace):
        super().__init__(rdf_graph, namespace)
        self.query_template = """
            PREFIX ns: <http://www.example.org/DrugInteraction.owl#>
            SELECT DISTINCT ?drug1 ?drug2
            WHERE {{
                ?drug1 ns:hasAlternative ?drug2 .
                FILTER(STRENDS(str(?drug1), "#{drug}") || STRENDS(str(?drug2), "#{drug}"))
            }}
        """
        logging.debug("AlternativeAgent initialized.")
    
    def get_alternatives(self, drug_name: str) -> List[Dict[str, str]]:
        query = self.query_template.format(drug=drug_name)
        logging.info(f"Fetching alternatives for {drug_name}")
        return self.query_ontology(query)