| 1 | // Xref FMAIDs in HPO to FMA ontology and retrieve all the children and parent terms |
|---|
| 2 | /** |
|---|
| 3 | * Copyright (c) 2010 - 2011 European Molecular Biology Laboratory and University of Groningen |
|---|
| 4 | * |
|---|
| 5 | * Contact: ontocat-users@lists.sourceforge.net |
|---|
| 6 | * |
|---|
| 7 | * This file is part of OntoCAT |
|---|
| 8 | * |
|---|
| 9 | * OntoCAT is free software: you can redistribute it and/or modify it under |
|---|
| 10 | * the terms of the GNU Lesser General Public License as published by the Free |
|---|
| 11 | * Software Foundation; either version 3 of the License, or (at your option) any |
|---|
| 12 | * later version. |
|---|
| 13 | * |
|---|
| 14 | * OntoCAT is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 16 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|---|
| 17 | * details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public License along |
|---|
| 20 | * with OntoCAT. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 21 | */ |
|---|
| 22 | package uk.ac.ebi.ontocat.examples; |
|---|
| 23 | |
|---|
| 24 | import java.io.File; |
|---|
| 25 | import java.net.URI; |
|---|
| 26 | import java.net.URISyntaxException; |
|---|
| 27 | import java.util.Set; |
|---|
| 28 | import java.util.regex.Matcher; |
|---|
| 29 | import java.util.regex.Pattern; |
|---|
| 30 | |
|---|
| 31 | import uk.ac.ebi.ontocat.OntologyService; |
|---|
| 32 | import uk.ac.ebi.ontocat.OntologyServiceException; |
|---|
| 33 | import uk.ac.ebi.ontocat.OntologyTerm; |
|---|
| 34 | import uk.ac.ebi.ontocat.file.FileOntologyService; |
|---|
| 35 | import uk.ac.ebi.ontocat.virtual.CompositeDecorator; |
|---|
| 36 | |
|---|
| 37 | // TODO: Auto-generated Javadoc |
|---|
| 38 | /** |
|---|
| 39 | * Example 9 |
|---|
| 40 | * |
|---|
| 41 | * Shows how to xref FMAIDs in HPO to FMA ontology and retrieve all the children |
|---|
| 42 | * and parent terms. |
|---|
| 43 | */ |
|---|
| 44 | public class Example9 { |
|---|
| 45 | |
|---|
| 46 | public static void main(String[] args) throws OntologyServiceException, URISyntaxException { |
|---|
| 47 | // These need to point to ontologies either locally or on the web |
|---|
| 48 | URI uriHPO = new File("human-phenotype-ontology_v1208.obo").toURI(); |
|---|
| 49 | URI uriFMA = new File("fma2_obo.obo").toURI(); |
|---|
| 50 | // Load FMA |
|---|
| 51 | FileOntologyService osFMA = new FileOntologyService(uriFMA, "FMA"); |
|---|
| 52 | |
|---|
| 53 | // Load HPO |
|---|
| 54 | FileOntologyService osHPO = new FileOntologyService(uriHPO, "HPO"); |
|---|
| 55 | |
|---|
| 56 | // Single entry point |
|---|
| 57 | OntologyService os = CompositeDecorator.getService(osFMA, osHPO); |
|---|
| 58 | // Load all HPO terms into a set |
|---|
| 59 | Set<OntologyTerm> termsHPO = os.getAllTerms("HPO"); |
|---|
| 60 | System.out.println("Loaded " + termsHPO.size() + " HPO terms"); |
|---|
| 61 | |
|---|
| 62 | // Regex to extract FMA xrefs from HPO definitions |
|---|
| 63 | Pattern p = Pattern.compile("(FMA:\\d+)"); |
|---|
| 64 | Matcher m = p.matcher(""); |
|---|
| 65 | |
|---|
| 66 | // Iterate through HPO terms |
|---|
| 67 | for (OntologyTerm termHPO : termsHPO) { |
|---|
| 68 | // Iterate through definitions |
|---|
| 69 | for (String def : os.getDefinitions(termHPO)) { |
|---|
| 70 | m.reset(def); |
|---|
| 71 | // For each FMA xref found |
|---|
| 72 | while (m.find()) { |
|---|
| 73 | // note: replacing : with _ in accessions |
|---|
| 74 | // as OWLAPI changes them on loading |
|---|
| 75 | String idFMA = m.group().replace(":", "_"); |
|---|
| 76 | // Fetch the full term from FMA |
|---|
| 77 | OntologyTerm termFMA = os.getTerm(idFMA); |
|---|
| 78 | // Check if null as some terms may not be |
|---|
| 79 | // resolvable due to versioning, etc. |
|---|
| 80 | if (termFMA != null) { |
|---|
| 81 | System.out.println("Found " + termFMA + " in " + termHPO); |
|---|
| 82 | System.out.println("\tFetching children..."); |
|---|
| 83 | System.out.println(osFMA.getAllChildren(termFMA)); |
|---|
| 84 | System.out.println("\tFetching parents..."); |
|---|
| 85 | System.out.println(osFMA.getAllParents(termFMA)); |
|---|
| 86 | } else { |
|---|
| 87 | System.out.println("ERROR could not resolve " + idFMA); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | } |
|---|