1 | package gscf |
---|
2 | |
---|
3 | import grails.test.* |
---|
4 | import dbnp.data.* |
---|
5 | |
---|
6 | |
---|
7 | /** |
---|
8 | * OntologyTests Test |
---|
9 | * |
---|
10 | * Test ontology/term functionality on domain model level |
---|
11 | * |
---|
12 | * @author keesvb |
---|
13 | * @since 20100510 |
---|
14 | * @package dbnp.studycapturing |
---|
15 | * |
---|
16 | * Revision information: |
---|
17 | * $Rev$ |
---|
18 | * $Author$ |
---|
19 | * $Date$ |
---|
20 | */ |
---|
21 | class OntologyTests extends GrailsUnitTestCase { |
---|
22 | |
---|
23 | final String testOntologyName = "Test ontology" |
---|
24 | final String testOntologyDescription = "Test description" |
---|
25 | final String testOntologyUrl = "http://www.test.com" |
---|
26 | final String testOntologyVersionNumber = "1.0" |
---|
27 | final int testOntologyNcboId = 0 |
---|
28 | final int testOntologyNcboVersionedId = 0 |
---|
29 | final String testTermName = "Test term" |
---|
30 | final String testAccession = 'TEST01234$' |
---|
31 | |
---|
32 | protected void setUp() { |
---|
33 | super.setUp() |
---|
34 | |
---|
35 | def ontology = new Ontology( |
---|
36 | name: testOntologyName, |
---|
37 | description: testOntologyDescription, |
---|
38 | url: testOntologyUrl, |
---|
39 | versionNumber: testOntologyVersionNumber, |
---|
40 | ncboId: testOntologyNcboId, |
---|
41 | ncboVersionedId: testOntologyNcboVersionedId |
---|
42 | ); |
---|
43 | |
---|
44 | // Validate and save ontology |
---|
45 | assert ontology.validate() |
---|
46 | assert ontology.save(flush:true) |
---|
47 | } |
---|
48 | |
---|
49 | protected void tearDown() { |
---|
50 | super.tearDown() |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Test if ontology was properly saved |
---|
55 | */ |
---|
56 | void testSave () { |
---|
57 | |
---|
58 | // Try to retrieve the ontology and make sure it's the same |
---|
59 | def ontologyDB = Ontology.findByName(testOntologyName) |
---|
60 | assert ontologyDB |
---|
61 | assert ontologyDB.name.equals(testOntologyName) |
---|
62 | assert ontologyDB.description.equals(testOntologyDescription) |
---|
63 | assert ontologyDB.url.equals(testOntologyUrl) |
---|
64 | assert ontologyDB.versionNumber.equals(testOntologyVersionNumber) |
---|
65 | assert ontologyDB.ncboId.equals(testOntologyNcboId) |
---|
66 | assert ontologyDB.ncboVersionedId.equals(testOntologyNcboVersionedId) |
---|
67 | |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * Test saving and retrieving a term within the ontology and test giveTermByName(name) |
---|
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 | |
---|
97 | |
---|
98 | /** |
---|
99 | * Test giveTerms() method |
---|
100 | */ |
---|
101 | void testGiveTerms() { |
---|
102 | |
---|
103 | // Find created ontology |
---|
104 | def testOntology = Ontology.findByName(testOntologyName) |
---|
105 | assert testOntology |
---|
106 | |
---|
107 | def terms = testOntology.giveTerms() |
---|
108 | assert terms |
---|
109 | assert terms.size() == 1 |
---|
110 | assert terms.asList().first().name.equals(testTermName) |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | /** |
---|
115 | * Ontocat test (Ontocat example 1) |
---|
116 | * |
---|
117 | * Shows how to list all the available ontologies in OLS |
---|
118 | * |
---|
119 | */ |
---|
120 | void testOntocat() { |
---|
121 | // Instantiate OLS service |
---|
122 | uk.ac.ebi.ontocat.OntologyService os = new uk.ac.ebi.ontocat.bioportal.BioportalOntologyService() |
---|
123 | // For all ontologies in OLS print their |
---|
124 | // full label and abbreviation |
---|
125 | uk.ac.ebi.ontocat.Ontology o = os.getOntology("1005") |
---|
126 | StringBuilder sb = new StringBuilder(); |
---|
127 | sb.append(o.getAbbreviation()); |
---|
128 | sb.append("\t"); |
---|
129 | sb.append(o.getLabel()); |
---|
130 | sb.append("\t"); |
---|
131 | sb.append(o.getOntologyAccession()); |
---|
132 | sb.append("\t"); |
---|
133 | o.getVersionNumber() + o.getMetaPropertyValues().each { |
---|
134 | sb.append(it.dump()) |
---|
135 | } |
---|
136 | System.out.println(sb.toString()); |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | } |
---|