1 | package dbnp.data |
---|
2 | |
---|
3 | /** |
---|
4 | * This class describes an existing ontology, of which terms can be stored (actually 'cached' would be a better description) |
---|
5 | * in the (global) Term store. |
---|
6 | * This information is mapped from the BioPortal NCBO REST service, e.g.: http://rest.bioontology.org/bioportal/ontologies/38802 |
---|
7 | * @see http://www.bioontology.org/wiki/index.php/NCBO_REST_services |
---|
8 | * |
---|
9 | * Revision information: |
---|
10 | * $Rev: 625 $ |
---|
11 | * $Author: keesvb $ |
---|
12 | * $Date: 2010-06-25 14:01:49 +0000 (vr, 25 jun 2010) $ |
---|
13 | */ |
---|
14 | class Ontology implements Serializable { |
---|
15 | String name // BioPortal: displayLabel |
---|
16 | String description // BioPortal: description |
---|
17 | String url // BioPortal: homepage |
---|
18 | String versionNumber // BioPortal: versionNumber |
---|
19 | int ncboId // BioPortal: ontologyId |
---|
20 | int ncboVersionedId // BioPortal: id |
---|
21 | |
---|
22 | /** |
---|
23 | * Find child terms |
---|
24 | * @return A set containing all terms that reside under this ontology |
---|
25 | */ |
---|
26 | Set<Term> giveTerms() { |
---|
27 | Term.findAllByOntology(this) |
---|
28 | } |
---|
29 | |
---|
30 | Object giveTermByName(String name) { |
---|
31 | giveTerms().find { |
---|
32 | it.name == name |
---|
33 | } |
---|
34 | |
---|
35 | /* TODO: find out why the following doesn't work (probably more efficient): |
---|
36 | Term.find { |
---|
37 | it.name == name |
---|
38 | it.ontology == this |
---|
39 | } |
---|
40 | }*/ |
---|
41 | } |
---|
42 | |
---|
43 | static Ontology getBioPortalOntology(int ncboId) { |
---|
44 | getBioPortalOntology(ncboId as String) |
---|
45 | } |
---|
46 | static Ontology getBioPortalOntology(String ncboId) { |
---|
47 | // Get ontology from BioPortal via Ontocat |
---|
48 | // TODO: maybe make a static OntologyService instance to be more efficient, and decorate it with caching? |
---|
49 | uk.ac.ebi.ontocat.OntologyService os = new uk.ac.ebi.ontocat.bioportal.BioportalOntologyService() |
---|
50 | uk.ac.ebi.ontocat.Ontology o = os.getOntology(ncboId) |
---|
51 | |
---|
52 | // Instantiate and return Ontology object |
---|
53 | new dbnp.data.Ontology( |
---|
54 | name: o.label, |
---|
55 | description: o.description, |
---|
56 | url: o.properties['homepage'] ?: "http://bioportal.bioontology.org/ontologies/${o.id}", |
---|
57 | versionNumber: o.versionNumber, |
---|
58 | ncboId: o.ontologyAccession, |
---|
59 | ncboVersionedId: o.id |
---|
60 | ); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | * Return the Ontology by ncboId, or create it if nonexistent. |
---|
66 | * @param ncboId |
---|
67 | * @return Ontology |
---|
68 | */ |
---|
69 | static Ontology getOrCreateOntologyByNcboId( String ncboId ) { |
---|
70 | return getOrCreateOntologyByNcboId( ncboId as int ) |
---|
71 | } |
---|
72 | static Ontology getOrCreateOntologyByNcboId( int ncboId ) { |
---|
73 | println "find ${ncboId} in ${list()*.ncboId}" |
---|
74 | def ontology = findByNcboId( ncboId as String ) |
---|
75 | |
---|
76 | // got an ontology? |
---|
77 | if (!ontology) { |
---|
78 | // no, fetch it from the webservice |
---|
79 | ontology = getBioPortalOntology( ncboId ) |
---|
80 | |
---|
81 | if (ontology && ontology.validate() && ontology.save(flush:true)) { |
---|
82 | ontology.refresh() |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | return ontology |
---|
87 | } |
---|
88 | |
---|
89 | static Ontology getBioPortalOntologyByTerm(String termId) { |
---|
90 | // Get ontology from BioPortal via Ontocat |
---|
91 | // TODO: maybe make a static OntologyService instance to be more efficient, and decorate it with caching? |
---|
92 | uk.ac.ebi.ontocat.OntologyService os = new uk.ac.ebi.ontocat.bioportal.BioportalOntologyService() |
---|
93 | uk.ac.ebi.ontocat.OntologyTerm term = os.getTerm( termId ); |
---|
94 | println( term ); |
---|
95 | uk.ac.ebi.ontocat.Ontology o = os.getOntology( term.getOntologyAccession() ); |
---|
96 | println( o ); |
---|
97 | |
---|
98 | // Instantiate and return Ontology object |
---|
99 | new dbnp.data.Ontology( |
---|
100 | name: o.label, |
---|
101 | description: o.description, |
---|
102 | url: o.properties['homepage'] ?: "http://bioportal.bioontology.org/ontologies/${o.id}", |
---|
103 | versionNumber: o.versionNumber, |
---|
104 | ncboId: o.ontologyAccession, |
---|
105 | ncboVersionedId: o.id |
---|
106 | ); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Instantiate Ontotology class by searching the web service for (versioned)id. |
---|
111 | * @param ontologyId (bioportal versionedId) |
---|
112 | * @return ontology instance |
---|
113 | */ |
---|
114 | static Ontology getBioPortalOntologyByVersionedId(String ncboVersionedId) { |
---|
115 | try { |
---|
116 | // use the NCBO REST service to fetch ontology information |
---|
117 | def url = "http://rest.bioontology.org/bioportal/ontologies/" + ncboVersionedId |
---|
118 | def xml = new URL(url).getText() |
---|
119 | def data = new XmlParser().parseText(xml) |
---|
120 | def bean = data.data.ontologyBean |
---|
121 | |
---|
122 | // instantiate Ontology with the proper values |
---|
123 | def ontology = new dbnp.data.Ontology( |
---|
124 | name: bean.displayLabel.text(), |
---|
125 | description: bean.description.text(), |
---|
126 | url: bean.homepage.text(), |
---|
127 | versionNumber: bean.versionNumber.text(), |
---|
128 | ncboId: bean.ontologyId.text() as int, |
---|
129 | ncboVersionedId: bean.id.text() as int |
---|
130 | ) |
---|
131 | |
---|
132 | // validate ontology |
---|
133 | if (ontology.validate()) { |
---|
134 | // proper instance |
---|
135 | return ontology |
---|
136 | } else { |
---|
137 | // it does not validate |
---|
138 | println ".encountered errors instantiating Ontology by versionedId [" + ncboVersionedId + "] :" |
---|
139 | ontology.errors.each() { |
---|
140 | println " -" + it |
---|
141 | } |
---|
142 | throw new Exception("instantiating Ontology by (versioned) id [" + ncboVersionedId + "] failed") |
---|
143 | } |
---|
144 | } catch (Exception e) { |
---|
145 | // whoops?! |
---|
146 | return false |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|