Spaces:
Sleeping
Sleeping
File size: 9,465 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Install owlready2 if not installed\n",
"# !pip install owlready2\n",
"\n",
"# Import libraries\n",
"from owlready2 import *"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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",
"onto = get_ontology(\"DrugInteraction.owl\").load()\n",
"print(\"Ontology loaded successfully.\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Classes in the ontology:\n",
"DrugInteraction.TestProgram\n",
"DrugInteraction.Drug\n",
"DrugInteraction.Interaction\n",
"DrugInteraction.Conflict\n",
"DrugInteraction.Similarity\n",
"\n",
"Object Properties in the ontology:\n",
"DrugInteraction.causedBy\n",
"DrugInteraction.between\n",
"DrugInteraction.relatedTo\n",
"\n",
"Instances of classes:\n",
"DrugInteraction.Warfarin is an instance of DrugInteraction.Drug\n",
"DrugInteraction.Aspirin is an instance of DrugInteraction.Drug\n",
"DrugInteraction.Nitroglycerin is an instance of DrugInteraction.Drug\n",
"DrugInteraction.Sildenafil is an instance of DrugInteraction.Drug\n",
"DrugInteraction.Interaction1 is an instance of DrugInteraction.Interaction\n",
"DrugInteraction.Conflict1 is an instance of DrugInteraction.Conflict\n",
"DrugInteraction.Similarity1 is an instance of DrugInteraction.Similarity\n"
]
}
],
"source": [
"# Step 2: Explore Ontology Structure\n",
"print(\"Classes in the ontology:\")\n",
"for cls in onto.classes():\n",
" print(cls)\n",
"\n",
"print(\"\\nObject Properties in the ontology:\")\n",
"for prop in onto.object_properties():\n",
" print(prop)\n",
"\n",
"print(\"\\nInstances of classes:\")\n",
"for ind in onto.individuals():\n",
" print(f\"{ind} is an instance of {ind.is_a[0]}\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Querying Drug Interactions:\n",
"Interaction: Interaction1, Caused By: Warfarin, Aspirin\n"
]
}
],
"source": [
"# Step 3: Query Relationships\n",
"print(\"\\nQuerying Drug Interactions:\")\n",
"interaction_class = onto.search_one(iri=\"*Interaction\")\n",
"for interaction in interaction_class.instances():\n",
" caused_by_drugs = [drug.name for drug in interaction.causedBy]\n",
" print(f\"Interaction: {interaction.name}, Caused By: {', '.join(caused_by_drugs)}\")\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Querying Conflicts:\n",
"Conflict: Conflict1, Between: [DrugInteraction.Sildenafil, DrugInteraction.Nitroglycerin]\n",
"Drug: DrugInteraction.Sildenafil\n",
"Drug: DrugInteraction.Nitroglycerin\n",
"Conflict: Conflict1, Involves Drugs: Sildenafil, Nitroglycerin\n"
]
}
],
"source": [
"print(\"\\nQuerying Conflicts:\") \n",
"conflict_class = onto.search_one(iri=\"*Conflict\") \n",
"for conflict in conflict_class.instances(): \n",
"\tprint(f\"Conflict: {conflict.name}, Between: {conflict.between}\") \n",
"\tinvolved_drugs = [] \n",
"\tfor drug in conflict.between: \n",
"\t\tprint(f\"Drug: {drug}\") # Print the drug to see its structure \n",
"\t\ttry: \n",
"\t\t\tinvolved_drugs.append(drug.name) \n",
"\t\texcept AttributeError: \n",
"\t\t\tprint(f\"Error: 'str' object has no attribute 'name'. Drug: {drug}\") \t\t\n",
"print(f\"Conflict: {conflict.name}, Involves Drugs: {', '.join(involved_drugs)}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Querying Similarities:\n",
"Similarity: Similarity1, Related Drugs: Warfarin, Aspirin\n"
]
}
],
"source": [
"# Querying Similarities with error handling\n",
"print(\"\\nQuerying Similarities:\")\n",
"similarity_class = onto.search_one(iri=\"*Similarity\")\n",
"\n",
"if similarity_class: # Ensure the class exists\n",
" for similarity in similarity_class.instances():\n",
" # Safely handle 'relatedTo' to avoid AttributeError\n",
" related_drugs = [\n",
" drug.name if isinstance(drug, Thing) else str(drug) \n",
" for drug in getattr(similarity, \"relatedTo\", [])\n",
" ]\n",
" print(f\"Similarity: {similarity.name}, Related Drugs: {', '.join(related_drugs)}\")\n",
"else:\n",
" print(\"Similarity class not found in the ontology.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"* Owlready2 * Running HermiT...\n",
" java -Xmx2000M -cp /home/manith/.local/lib/python3.12/site-packages/owlready2/hermit:/home/manith/.local/lib/python3.12/site-packages/owlready2/hermit/HermiT.jar org.semanticweb.HermiT.cli.CommandLine -c -O -D -I file:////tmp/tmps4u6q73_\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Reasoning complete. Checking inferred facts...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"* Owlready2 * HermiT took 0.49095606803894043 seconds\n",
"* Owlready * (NB: only changes on entities loaded in Python are shown, other changes are done but not listed)\n"
]
}
],
"source": [
"# Step 4: Test Reasoning\n",
"# Add reasoning to infer new facts based on ontology\n",
"with onto:\n",
" sync_reasoner() # Runs the HermiT reasoner\n",
"print(\"\\nReasoning complete. Checking inferred facts...\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Inferred Relationships for Interactions:\n",
"Interaction: Interaction1, Caused By: Warfarin, Aspirin\n"
]
}
],
"source": [
"# Re-query after reasoning\n",
"print(\"\\nInferred Relationships for Interactions:\")\n",
"for interaction in interaction_class.instances():\n",
" caused_by_drugs = [drug.name for drug in interaction.causedBy]\n",
" print(f\"Interaction: {interaction.name}, Caused By: {', '.join(caused_by_drugs)}\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Custom Query: Find interactions involving 'Warfarin'\n",
"Interaction Found: Interaction1 caused by Warfarin\n"
]
}
],
"source": [
"# Step 5: Custom Queries and Testing\n",
"print(\"\\nCustom Query: Find interactions involving 'Warfarin'\")\n",
"warfarin = onto.search_one(iri=\"*Warfarin\")\n",
"for interaction in interaction_class.instances():\n",
" if warfarin in interaction.causedBy:\n",
" print(f\"Interaction Found: {interaction.name} caused by Warfarin\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Custom Query: Find conflicts involving 'Nitroglycerin'\n",
"Conflict Found: Conflict1 involving Nitroglycerin\n"
]
}
],
"source": [
"print(\"\\nCustom Query: Find conflicts involving 'Nitroglycerin'\")\n",
"\n",
"# Dynamically find the Nitroglycerin individual\n",
"nitroglycerin = onto.search_one(iri=\"*Nitroglycerin\")\n",
"\n",
"if nitroglycerin:\n",
" # Query Conflicts and check 'between' property\n",
" for conflict in conflict_class.instances():\n",
" involved_drugs = getattr(conflict, \"between\", [])\n",
" if nitroglycerin in involved_drugs:\n",
" print(f\"Conflict Found: {conflict.name} involving Nitroglycerin\")\n",
"else:\n",
" print(\"Nitroglycerin not found in the ontology.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
|