1 | package gscf |
---|
2 | |
---|
3 | import grails.test.* |
---|
4 | import dbnp.data.* |
---|
5 | |
---|
6 | import uk.ac.ebi.ontocat.* |
---|
7 | |
---|
8 | /** |
---|
9 | * OntologyTests Test |
---|
10 | * |
---|
11 | * Test ontology/term functionality on domain model level |
---|
12 | * |
---|
13 | * @author keesvb |
---|
14 | * @since 20100510 |
---|
15 | * @package dbnp.studycapturing |
---|
16 | * |
---|
17 | * Revision information: |
---|
18 | * $Rev$ |
---|
19 | * $Author$ |
---|
20 | * $Date$ |
---|
21 | */ |
---|
22 | class OntologyTests extends GrailsUnitTestCase { |
---|
23 | |
---|
24 | final String testOntologyName = "Test ontology" |
---|
25 | final String testOntologyDescription = "Test description" |
---|
26 | final String testOntologyUrl = "http://www.test.com" |
---|
27 | final String testOntologyVersionNumber = "1.0" |
---|
28 | final int testOntologyNcboId = 0 |
---|
29 | final int testOntologyNcboVersionedId = 0 |
---|
30 | final String testTermName = "Test term" |
---|
31 | final String testAccession = 'TEST01234$' |
---|
32 | |
---|
33 | protected void setUp() { |
---|
34 | super.setUp() |
---|
35 | |
---|
36 | def ontology = new Ontology( |
---|
37 | name: testOntologyName, |
---|
38 | description: testOntologyDescription, |
---|
39 | url: testOntologyUrl, |
---|
40 | versionNumber: testOntologyVersionNumber, |
---|
41 | ncboId: testOntologyNcboId, |
---|
42 | ncboVersionedId: testOntologyNcboVersionedId |
---|
43 | ); |
---|
44 | |
---|
45 | // Validate and save ontology |
---|
46 | assert ontology.validate() |
---|
47 | assert ontology.save(flush: true) |
---|
48 | } |
---|
49 | |
---|
50 | protected void tearDown() { |
---|
51 | super.tearDown() |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Test if ontology was properly saved |
---|
56 | */ |
---|
57 | void testSave() { |
---|
58 | |
---|
59 | // Try to retrieve the ontology and make sure it's the same |
---|
60 | def ontologyDB = Ontology.findByName(testOntologyName) |
---|
61 | assert ontologyDB |
---|
62 | assert ontologyDB.name.equals(testOntologyName) |
---|
63 | assert ontologyDB.description.equals(testOntologyDescription) |
---|
64 | assert ontologyDB.url.equals(testOntologyUrl) |
---|
65 | assert ontologyDB.versionNumber.equals(testOntologyVersionNumber) |
---|
66 | assert ontologyDB.ncboId.equals(testOntologyNcboId) |
---|
67 | assert ontologyDB.ncboVersionedId.equals(testOntologyNcboVersionedId) |
---|
68 | |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Test saving and retrieving a term within the ontology and test giveTermByName(name) and giveTerms() |
---|
73 | */ |
---|
74 | void testTermSave() { |
---|
75 | |
---|
76 | // Find created ontology |
---|
77 | def testOntology = Ontology.findByName(testOntologyName) |
---|
78 | assert testOntology |
---|
79 | |
---|
80 | // Create a new term |
---|
81 | def term = new Term( |
---|
82 | name: testTermName, |
---|
83 | accession: testAccession, |
---|
84 | ontology: testOntology |
---|
85 | ) |
---|
86 | |
---|
87 | assert term.validate() |
---|
88 | assert term.save(flush: true) |
---|
89 | |
---|
90 | // Try to retrieve the term from the ontology and make sure it's the same |
---|
91 | def termDB = testOntology.giveTermByName(testTermName) |
---|
92 | assert termDB.name.equals(testTermName) |
---|
93 | assert termDB.accession.equals(testAccession) |
---|
94 | assert termDB.ontology == testOntology |
---|
95 | |
---|
96 | // Test giveTerms() and make sure the term is in there |
---|
97 | def terms = testOntology.giveTerms() |
---|
98 | assert terms |
---|
99 | assert terms.size() == 1 |
---|
100 | assert terms.asList().first().name.equals(testTermName) |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Ontocat test for debug purposes: show all properties of a certain ontology |
---|
105 | */ |
---|
106 | private void testOntocatBioPortalDebug() { |
---|
107 | // Instantiate OLS service |
---|
108 | uk.ac.ebi.ontocat.OntologyService os = new uk.ac.ebi.ontocat.bioportal.BioportalOntologyService() |
---|
109 | |
---|
110 | // Find ontology by ncboId |
---|
111 | uk.ac.ebi.ontocat.Ontology o = os.getOntology("1005") |
---|
112 | StringBuilder sb = new StringBuilder(); |
---|
113 | def bean = os.getOntologyBean() |
---|
114 | String codingScheme = bean.codingScheme |
---|
115 | sb.append("OntologyBean:\n") |
---|
116 | sb.append("property codingScheme=" + codingScheme + "\n") |
---|
117 | sb.append("Bean.properties:\n") |
---|
118 | bean.properties.each { |
---|
119 | sb.append(it.key + "=" + it.value + "\n") |
---|
120 | } |
---|
121 | sb.append "Bean:\t" + bean.dump() |
---|
122 | sb.append(bean.properties['codingScheme']) |
---|
123 | sb.append("\t"); |
---|
124 | sb.append(o.getAbbreviation()); |
---|
125 | sb.append("\t"); |
---|
126 | sb.append(o.getLabel()); |
---|
127 | sb.append("\t"); |
---|
128 | sb.append(o.getOntologyAccession()); |
---|
129 | sb.append("\t"); |
---|
130 | sb.append("Ontology meta properties:\n") |
---|
131 | o.getVersionNumber() + o.getMetaPropertyValues().each { |
---|
132 | sb.append(it.name + "=" + it.value + "\n") |
---|
133 | } |
---|
134 | sb.append("Ontology properties:\n"); |
---|
135 | o.getProperties().each { |
---|
136 | sb.append(it.key + "=" + it.value + "\n") |
---|
137 | } |
---|
138 | sb.append("Ontology root terms:\n"); |
---|
139 | os.getRootTerms(o).each { |
---|
140 | sb.append("Term ${os.makeLookupHyperlink(it.properties.get('accession'))} properties:\n") |
---|
141 | it.properties.each { |
---|
142 | sb.append it.key + "=" + it.value + "\n" |
---|
143 | } |
---|
144 | } |
---|
145 | System.out.println(sb.toString()); |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Add all OLS ontologies to the database via the Ontocat framework |
---|
150 | */ |
---|
151 | void testOntocatOLSOntologies() { |
---|
152 | // Instantiate EBI OLS service |
---|
153 | uk.ac.ebi.ontocat.OntologyService os = new uk.ac.ebi.ontocat.ols.OlsOntologyService() |
---|
154 | addOntologies(os) |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Add all BioPortal ontologies to the database via the Ontocat framework |
---|
159 | */ |
---|
160 | void testOntocatBioPortalOntologies() { |
---|
161 | // Instantiate BioPortal service |
---|
162 | uk.ac.ebi.ontocat.OntologyService os = new uk.ac.ebi.ontocat.bioportal.BioportalOntologyService() |
---|
163 | addOntologies(os) |
---|
164 | } |
---|
165 | |
---|
166 | private void addOntologies(uk.ac.ebi.ontocat.OntologyService os) { |
---|
167 | |
---|
168 | // Iterate over all ontologies in OLS |
---|
169 | os.getOntologies().each { |
---|
170 | |
---|
171 | // Get bean and extract versionedId |
---|
172 | def bean = os.ontologyBean |
---|
173 | def matcher = bean.codingScheme =~ "/(\\d{5})/" |
---|
174 | assert matcher |
---|
175 | assert matcher.size() == 1 |
---|
176 | assert matcher[0].size() == 2 |
---|
177 | def versionedId = matcher[0][1] |
---|
178 | |
---|
179 | // Instantiate ontology |
---|
180 | def ontology = new Ontology( |
---|
181 | name: o.label, |
---|
182 | description: o.description, |
---|
183 | url: bean.properties['homepage'], |
---|
184 | //url: 'http://bioportal.bioontology.org/ontologies/' + versionedId, |
---|
185 | versionNumber: o.versionNumber, |
---|
186 | ncboId: o.ontologyAccession, |
---|
187 | ncboVersionedId: versionedId |
---|
188 | ); |
---|
189 | |
---|
190 | // Validate and save ontology |
---|
191 | assert ontology.validate() |
---|
192 | assert ontology.save(flush: true) |
---|
193 | |
---|
194 | //println ontology.dump() |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|