source: trunk/ontoCAT/src/uk/ac/ebi/ontocat/examples/Example15.java @ 503

Revision 503, 6.2 KB checked in by tadamusiak, 5 months ago (diff)

typo

Line 
1// Reason over ontology and build partonomy
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 */
22package uk.ac.ebi.ontocat.examples;
23
24import java.net.URI;
25import java.util.HashSet;
26import java.util.Map;
27import java.util.Map.Entry;
28import java.util.Set;
29
30import uk.ac.ebi.ontocat.OntologyServiceException;
31import uk.ac.ebi.ontocat.OntologyTerm;
32import uk.ac.ebi.ontocat.file.ReasonedFileOntologyService;
33
34/**
35 * Example 15
36 *
37 * Shows how to instantiate the reasoned file service and build partonomy
38 */
39public class Example15 {
40        private static ReasonedFileOntologyService os;
41
42        /**
43         * The main method.
44         *
45         * @param args
46         *            the arguments
47         * @throws Exception
48         *             the exception
49         */
50        public static void main(String[] args) throws Exception {
51                // Instantiate a ReasonedFileOntologyService
52                // Note behind the scenes that uses the HermiT reasoner
53                // http://hermit-reasoner.com/
54                os = new ReasonedFileOntologyService(new URI(
55                "http://www.ebi.ac.uk/efo/efo.owl"), "efo");
56
57                // Get parents of atrial myocardium (EFO_0003087)
58                // This is a defined class and it would not work
59                // otherwise
60                System.out.println("Parent of defined class atrial myocardium:");
61                System.out.println(os.getParents("efo", "EFO_0003087"));
62
63                // Build a tree of relations (including partonomy)
64                // To make it quicker we'll focus
65                // on the skeleton structure branch (EFO_0000806)
66                // to compute a larger tree try organism part (EFO_0000635)
67                OntologyTerm startNode = os.getTerm("EFO_0000806");
68
69                // But first let's see all the possible relations for the starting term
70                System.out.println("\nDirect relations for term: "
71                                + startNode.getLabel() + "...");
72                Map<String, Set<OntologyTerm>> result = os.getRelations(startNode);
73
74                for (Entry<String, Set<OntologyTerm>> entry : result.entrySet()) {
75                        System.out.println("\n\t" + entry.getKey());
76                        for (OntologyTerm ot : entry.getValue()) {
77                                System.out.println(ot.getLabel());
78                        }
79                }
80
81                // Proceed with building the partonomy tree
82                // Here focusing on a specific relation makes it quicker
83                // but see visualiseAll() for a full tree
84                Set<OntologyTerm> branch = os.getAllChildren(startNode);
85                branch.add(startNode);
86                System.out
87                .println("\nBuilding exhaustive relations tree, estimated size more than "
88                                + branch.size() + " classes\n");
89
90                System.out.println(startNode.getLabel());
91                visualise_partonomy(startNode, "    ");
92        }
93
94        /**
95         * Simple recursive visualisation of partonomy starting from the top node
96         *
97         * @param currentNode
98         *            the current node
99         * @param tab
100         *            the tab
101         * @throws OntologyServiceException
102         *             the ontology service exception
103         */
104        private static void visualise_partonomy(OntologyTerm currentNode, String tab)
105        throws OntologyServiceException {
106                String partPadding = pad("has_part", "-");
107                String isaPadding = pad("", "-");
108                String newTab = tab + pad("", " ");
109
110                Set<OntologyTerm> isa_children = new HashSet<OntologyTerm>(
111                                os.getChildren(currentNode));
112                Set<OntologyTerm> part_children = new HashSet<OntologyTerm>(
113                                os.getSpecificRelation(currentNode.getOntologyAccession(),
114                                                currentNode.getAccession(), "has_part"));
115
116                // remove has_part children from the asserted isa set
117                isa_children.removeAll(part_children);
118
119                for (OntologyTerm term : isa_children) {
120                        System.out.println(tab + isaPadding + term.getLabel());
121                        visualise_partonomy(term, newTab);
122                }
123                for (OntologyTerm term : part_children) {
124                        System.out.println(tab + partPadding + term.getLabel());
125                        visualise_partonomy(term, newTab);
126                }
127        }
128
129        /**
130         * Simple recursive visualisation of all relationships from the top node
131         * NOTE: this is rather slow
132         *
133         * @param currentNode
134         *            the current node
135         * @param tab
136         *            the tab
137         * @throws OntologyServiceException
138         *             the ontology service exception
139         */
140        @SuppressWarnings("unused")
141        private static void visualiseAll(OntologyTerm currentNode, String tab)
142        throws OntologyServiceException {
143                String isaPadding = pad("", "-");
144                String newTab = tab + pad("", " ");
145
146                Set<OntologyTerm> isaChildren = new HashSet<OntologyTerm>(
147                                os.getChildren(currentNode));
148                // Note you could use ReasonedOntologyService.getSpecificRelation
149                // to focus only on a specific axis (e.g. has_part)
150                Map<String, Set<OntologyTerm>> mOtherChildren = os.getRelations(
151                                currentNode.getOntologyAccession(), currentNode.getAccession());
152
153                // remove isa children inferred by the reasoner
154                // under a structure defined specifically to capture a relationship
155                // example skeleton structure, or skeleton disease
156                // these will be shown with the original relationship in the next step
157                for (Set<OntologyTerm> sOtherChildren : mOtherChildren.values()) {
158                        isaChildren.removeAll(sOtherChildren);
159                }
160
161                // print isa children
162                for (OntologyTerm term : isaChildren) {
163                        System.out.println(tab + isaPadding + term.getLabel());
164                        visualiseAll(term, newTab);
165                }
166                // print other children
167                for (Entry<String, Set<OntologyTerm>> e : mOtherChildren.entrySet()) {
168                        for (OntologyTerm ot : e.getValue()) {
169                                System.out.println(tab + pad(e.getKey(), "-") + ot.getLabel());
170                                visualiseAll(ot, newTab);
171                        }
172                }
173        }
174
175        public static String pad(String str, String padChar) {
176                StringBuilder padded = new StringBuilder(padChar + padChar + str);
177                while (padded.length() < 15) {
178                        padded.append(padChar);
179                }
180                return padded.toString();
181        }
182}
Note: See TracBrowser for help on using the repository browser.