File size: 5,891 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install rdflib if not already installed\n",
    "# !pip install rdflib\n",
    "\n",
    "from rdflib import Graph, Namespace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading ontology...\n",
      "Ontology loaded successfully.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Step 1: Load the Ontology\n",
    "print(\"Loading ontology...\")\n",
    "g = Graph()\n",
    "g.parse(\"DrugInteraction.owl\", format=\"xml\")  # Ensure your OWL file is named correctly and in the same directory\n",
    "print(\"Ontology loaded successfully.\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Query Templates\n",
    "query_interactions = \"\"\"\n",
    "PREFIX ns: <http://www.example.org/DrugInteraction.owl#>\n",
    "SELECT ?drug1 ?drug2\n",
    "WHERE {{\n",
    "    ?drug1 ns:hasInteraction ?drug2 .\n",
    "    FILTER (STR(?drug1) = \"http://www.example.org/DrugInteraction.owl#{drug}\" || \n",
    "            STR(?drug2) = \"http://www.example.org/DrugInteraction.owl#{drug}\")\n",
    "}}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_conflicts = \"\"\"\n",
    "PREFIX ns: <http://www.example.org/DrugInteraction.owl#>\n",
    "SELECT ?drug1 ?drug2\n",
    "WHERE {{\n",
    "    ?drug1 ns:hasConflict ?drug2 .\n",
    "    FILTER (STR(?drug1) = \"http://www.example.org/DrugInteraction.owl#{drug}\" || \n",
    "            STR(?drug2) = \"http://www.example.org/DrugInteraction.owl#{drug}\")\n",
    "}}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_similarities = \"\"\"\n",
    "PREFIX ns: <http://www.example.org/DrugInteraction.owl#>\n",
    "SELECT ?drug1 ?drug2\n",
    "WHERE {{\n",
    "    ?drug1 ns:hasSimilarity ?drug2 .\n",
    "    FILTER (STR(?drug1) = \"http://www.example.org/DrugInteraction.owl#{drug}\" || \n",
    "            STR(?drug2) = \"http://www.example.org/DrugInteraction.owl#{drug}\")\n",
    "}}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_alternatives = \"\"\"\n",
    "PREFIX ns: <http://www.example.org/DrugInteraction.owl#>\n",
    "SELECT ?drug1 ?drug2\n",
    "WHERE {{\n",
    "    ?drug1 ns:hasAlternative ?drug2 .\n",
    "    FILTER (STR(?drug1) = \"http://www.example.org/DrugInteraction.owl#{drug}\" || \n",
    "            STR(?drug2) = \"http://www.example.org/DrugInteraction.owl#{drug}\")\n",
    "}}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function to execute a query and print results\n",
    "def run_query(query, description, drug_name):\n",
    "    print(f\"Results for {description} involving '{drug_name}':\")\n",
    "    results = g.query(query.format(drug=drug_name))\n",
    "    if len(results) == 0:\n",
    "        print(\"  No results found.\\n\")\n",
    "    else:\n",
    "        for row in results:\n",
    "            drug1_label = row[0].split('#')[-1]\n",
    "            drug2_label = row[1].split('#')[-1]\n",
    "            print(f\"  {drug1_label} → {drug2_label}\")\n",
    "        print(\"\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Step 2: Accept User Input for Drug Names\n",
    "def main():\n",
    "    print(\"Enter drug names separated by commas (e.g., Aspirin, Warfarin):\")\n",
    "    user_input = input(\"Drugs: \")\n",
    "    drug_names = [drug.strip() for drug in user_input.split(\",\") if drug.strip()]\n",
    "\n",
    "    if not drug_names:\n",
    "        print(\"No drug names provided. Exiting.\")\n",
    "        return\n",
    "\n",
    "    # Step 3: Run Queries for Each Drug\n",
    "    for drug in drug_names:\n",
    "        print(f\"\\n--- Checking for {drug} ---\")\n",
    "        run_query(query_interactions, \"Drug Interactions\", drug)\n",
    "        run_query(query_conflicts, \"Conflicts\", drug)\n",
    "        run_query(query_similarities, \"Similarities\", drug)\n",
    "        run_query(query_alternatives, \"Alternative Drugs\", drug)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter drug names separated by commas (e.g., Aspirin, Warfarin):\n",
      "\n",
      "--- Checking for panadol ---\n",
      "Results for Drug Interactions involving 'panadol':\n",
      "  No results found.\n",
      "\n",
      "Results for Conflicts involving 'panadol':\n",
      "  No results found.\n",
      "\n",
      "Results for Similarities involving 'panadol':\n",
      "  No results found.\n",
      "\n",
      "Results for Alternative Drugs involving 'panadol':\n",
      "  No results found.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Run the main function\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}