Skip to content

Commit 60bcae3

Browse files
committed
issue #121 annotation tests
1 parent 70e93bf commit 60bcae3

8 files changed

+380
-82
lines changed
+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package edu.ucsd.sbrg;
2+
3+
import de.zbit.util.prefs.SBProperties;
4+
import edu.ucsd.sbrg.bigg.IOOptions;
5+
import edu.ucsd.sbrg.bigg.ModelPolisherOptions;
6+
import edu.ucsd.sbrg.bigg.Parameters;
7+
import org.sbml.jsbml.AbstractNamedSBase;
8+
import org.sbml.jsbml.CVTerm;
9+
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.util.Collection;
12+
import java.util.HashSet;
13+
import java.util.Map;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
16+
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
18+
19+
public class TestUtils {
20+
21+
public static void initParameters() {
22+
initParameters(Map.of());
23+
}
24+
25+
public static void initParameters(Map<String, String> params) {
26+
var props = new SBProperties();
27+
props.setProperty(IOOptions.INPUT.getOptionName(),
28+
"bla");
29+
props.setProperty(IOOptions.OUTPUT.getOptionName(),
30+
"bla");
31+
props.setProperty(ModelPolisherOptions.COMPRESSION_TYPE.getOptionName(),
32+
ModelPolisherOptions.Compression.NONE.name());
33+
props.setProperty(ModelPolisherOptions.DOCUMENT_TITLE_PATTERN.getOptionName(),
34+
"");
35+
36+
for (var pair : params.entrySet()) {
37+
props.setProperty(pair.getKey(), pair.getValue());
38+
}
39+
try {
40+
var parameters = Parameters.class.getDeclaredField("parameters");
41+
parameters.setAccessible(true);
42+
parameters.set(null, null);
43+
var init = Parameters.class.getDeclaredMethod("init", SBProperties.class);
44+
init.setAccessible(true);
45+
init.invoke(null, props);
46+
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | NoSuchFieldException e) {
47+
e.printStackTrace();
48+
assertTrue(false, "Reflection on parameters failed. Sorry");
49+
}
50+
}
51+
52+
53+
public static void assertCVTermIsPresent(AbstractNamedSBase entity,
54+
CVTerm.Type t,
55+
CVTerm.Qualifier q) {
56+
assertCVTermIsPresent(entity, t, q, null, "CVTerm of type " + t.toString()
57+
+ ", " + q.toString() + " not present on entity "
58+
+ entity.getId());
59+
}
60+
61+
public static void assertCVTermIsPresent(AbstractNamedSBase entity,
62+
CVTerm.Type t,
63+
CVTerm.Qualifier q,
64+
String uri) {
65+
assertCVTermIsPresent(entity, t, q, uri, "CVTerm of type " + t.toString()
66+
+ ", " + q.toString() + " with URI " + uri + " not present on entity "
67+
+ entity.getId());
68+
}
69+
70+
71+
public static void assertCVTermIsPresent(AbstractNamedSBase entity,
72+
CVTerm.Type t,
73+
CVTerm.Qualifier q,
74+
String uri,
75+
String message) {
76+
var cvTerms = entity.getCVTerms();
77+
for (var term : cvTerms) {
78+
for (var r : term.getResources()) {
79+
if (term.getQualifierType().equals(t)
80+
&& (null == q || term.getQualifier().equals(q))
81+
&& (null == uri || r.equals(uri))) {
82+
assertTrue(true);
83+
return;
84+
}
85+
}
86+
}
87+
assertTrue(false, message);
88+
}
89+
90+
public static void assertCVTermsArePresent(AbstractNamedSBase entity,
91+
CVTerm.Type t,
92+
CVTerm.Qualifier q,
93+
Collection<String> uris,
94+
String message) {
95+
var cvTerms = entity.getCVTerms();
96+
for(var term : cvTerms) {
97+
// this is just an awkward way to implement set difference, sorry bout that
98+
Collection<String> us = null;
99+
if(null != uris) {
100+
us = new HashSet<>(uris.stream().collect(Collectors.toSet()));
101+
us.removeAll(term.getResources());
102+
}
103+
// <- until here
104+
105+
if (term.getQualifierType().equals(t)
106+
&& (null == q || term.getQualifier().equals(q))
107+
&& (null == uris || us.isEmpty())) {
108+
assertTrue(true);
109+
return;
110+
}
111+
}
112+
assertTrue(false, message);
113+
}
114+
115+
/**
116+
* This is a development-time utility, which serves to quickly get a glimpse
117+
* of which CV Terms are present on an entity to ease writing assertions.
118+
*/
119+
public static String printCVTerm(CVTerm c) {
120+
var newline = System.getProperty("line.separator");
121+
return Stream.of(c.getQualifierType().toString(),
122+
c.getQualifier().toString(),
123+
c.getResources().stream().collect(Collectors.joining(", ")))
124+
.collect(Collectors.joining(newline));
125+
}
126+
127+
/**
128+
* This is a development-time utility, which serves to quickly print assertions.
129+
*/
130+
public static String printCVTermAssertion(CVTerm c) {
131+
var newline = System.getProperty("line.separator");
132+
return "assertCVTermIsPresent(__," + newline
133+
+ "CVTerm.Type." + c.getQualifierType().toString() + "," + newline
134+
+ "CVTerm.Qualifier." + c.getQualifier().toString() + "," + newline
135+
+ "\"" + c.getResources().stream().collect(Collectors.joining("\", \""))
136+
+ "\");";
137+
}
138+
139+
public static String printAllCVTerms(AbstractNamedSBase entity) {
140+
var newline = System.getProperty("line.separator");
141+
return entity.getCVTerms().stream().map(TestUtils::printCVTermAssertion).collect(Collectors.joining(newline));
142+
}
143+
144+
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
package edu.ucsd.sbrg.bigg.annotation;
22

3-
import edu.ucsd.sbrg.bigg.Parameters;
3+
import edu.ucsd.sbrg.bigg.ModelPolisherOptions;
4+
import org.junit.jupiter.api.Test;
5+
import org.sbml.jsbml.CVTerm;
6+
import org.sbml.jsbml.Model;
47
import org.sbml.jsbml.SBMLDocument;
58

6-
public class BiGGAnnotationTest {
9+
import java.util.Map;
710

8-
private static Parameters parameters;
9-
private static SBMLDocument doc;
11+
import static edu.ucsd.sbrg.TestUtils.assertCVTermIsPresent;
12+
import static edu.ucsd.sbrg.TestUtils.initParameters;
13+
import static org.junit.jupiter.api.Assertions.assertFalse;
14+
import static org.junit.jupiter.api.Assertions.assertTrue;
15+
16+
public class BiGGAnnotationTest extends BiGGDBTest {
17+
18+
@Test
19+
public void annotatePublication() {
20+
initParameters(Map.of(
21+
ModelPolisherOptions.ANNOTATE_WITH_BIGG.getOptionName(),
22+
"true",
23+
ModelPolisherOptions.INCLUDE_ANY_URI.getOptionName(),
24+
"true"));
25+
var sbml = new SBMLDocument(3, 2);
26+
var m = new Model("iJO1366", 3,2);
27+
sbml.setModel(m);
28+
var annotater = new BiGGAnnotation();
29+
30+
assertFalse(m.isSetMetaId());
31+
assertTrue(m.getCVTerms().isEmpty());
32+
33+
annotater.annotate(sbml);
34+
35+
assertTrue(m.isSetMetaId());
36+
assertCVTermIsPresent(m,
37+
CVTerm.Type.MODEL_QUALIFIER,
38+
CVTerm.Qualifier.BQM_IS_DESCRIBED_BY,
39+
"https://identifiers.org/pubmed/21988831",
40+
"Expected pubmed publication reference 21988831 not present on the model.");
41+
}
1042

11-
// @BeforeAll
12-
// public static void setUp() throws IOException, XMLStreamException {
13-
// // get test file
14-
// String input = BiGGAnnotationTest.class.getResource("models/ecoli_core.xml").getFile();
15-
// doc = SBMLReader.read(new File(input), new UpdateListener());
16-
// // DB connection
17-
// BiGGDB.init("biggdb", "5432", "postgres", "postgres", "bigg");
18-
// }
19-
//
20-
//
2143
// @Test
2244
// public void getBiGGIdFromResourcesTest() {
2345
// for (Species species : doc.getModel().getListOfSpecies()) {
@@ -31,9 +53,5 @@ public class BiGGAnnotationTest {
3153
// }
3254
//
3355
//
34-
// @AfterAll
35-
// public static void cleanUp() {
36-
// if (BiGGDB.inUse())
37-
// BiGGDB.close();
38-
// }
56+
3957
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package edu.ucsd.sbrg.bigg.annotation;
2+
3+
import edu.ucsd.sbrg.db.BiGGDB;
4+
import org.junit.jupiter.api.AfterEach;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.testcontainers.containers.GenericContainer;
7+
import org.testcontainers.junit.jupiter.Container;
8+
import org.testcontainers.junit.jupiter.Testcontainers;
9+
import org.testcontainers.utility.DockerImageName;
10+
11+
import java.time.Duration;
12+
13+
@Testcontainers
14+
public abstract class BiGGDBTest {
15+
16+
17+
@BeforeEach
18+
public void setup() {
19+
BiGGDB.init(bigg.getHost(), bigg.getFirstMappedPort().toString(), "postgres", "postgres", "bigg");
20+
}
21+
22+
@AfterEach
23+
public void cleanUp() {
24+
if (BiGGDB.inUse())
25+
BiGGDB.close();
26+
}
27+
28+
@Container
29+
public GenericContainer bigg = new GenericContainer(DockerImageName.parse("preloaded_bigg:latest"))
30+
.withExposedPorts(5432)
31+
.withEnv("POSTGRES_PASSWORD", "postgres")
32+
.withStartupTimeout(Duration.ofMinutes(5));
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package edu.ucsd.sbrg.bigg.annotation;
2+
3+
import edu.ucsd.sbrg.db.BiGGDB;
4+
import org.junit.jupiter.api.Test;
5+
import org.sbml.jsbml.CVTerm;
6+
import org.sbml.jsbml.Compartment;
7+
import org.sbml.jsbml.SBO;
8+
9+
import static edu.ucsd.sbrg.TestUtils.assertCVTermIsPresent;
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
public class CompartmentAnnotationTest extends BiGGDBTest {
13+
14+
15+
@Test
16+
public void annotateKnownCompartments() {
17+
for (var c_id : BiGGDB.getOnce("compartment")) {
18+
var c = new Compartment(c_id,3, 2);
19+
20+
assertTrue( c.getCVTerms().isEmpty());
21+
assertFalse(c.isSetName());
22+
assertFalse(c.isSetSBOTerm());
23+
c.setSBOTerm(537);
24+
assertNotEquals(SBO.getCompartment(), c.getSBOTerm());
25+
26+
var annotator = new CompartmentAnnotation(c);
27+
annotator.annotate();
28+
29+
assertEquals(1, c.getCVTerms().size());
30+
assertCVTermIsPresent(c,
31+
CVTerm.Type.BIOLOGICAL_QUALIFIER,
32+
CVTerm.Qualifier.BQB_IS);
33+
assertTrue(c.isSetName());
34+
assertEquals(SBO.getCompartment(), c.getSBOTerm());
35+
}
36+
}
37+
38+
@Test
39+
public void nameAnnotationIsSane() {
40+
var c = new Compartment("im", "default",3, 2);
41+
42+
var annotator = new CompartmentAnnotation(c);
43+
annotator.annotate();
44+
45+
assertTrue(c.isSetName());
46+
assertEquals("intermembrane space of mitochondria", c.getName());
47+
}
48+
49+
}
50+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package edu.ucsd.sbrg.bigg.annotation;
2+
3+
import edu.ucsd.sbrg.bigg.ModelPolisherOptions;
4+
import org.junit.jupiter.api.Test;
5+
import org.sbml.jsbml.CVTerm;
6+
import org.sbml.jsbml.Model;
7+
8+
import java.util.Map;
9+
10+
import static edu.ucsd.sbrg.TestUtils.assertCVTermIsPresent;
11+
import static edu.ucsd.sbrg.TestUtils.initParameters;
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
public class ModelAnnotationTest extends BiGGDBTest {
15+
16+
@Test
17+
public void basicModelAnnotation() {
18+
initParameters(Map.of(
19+
ModelPolisherOptions.INCLUDE_ANY_URI.getOptionName(),
20+
"true"));
21+
var m = new Model("iJO1366", 3,2);
22+
var annotater = new ModelAnnotation(m);
23+
24+
assertFalse(m.isSetMetaId());
25+
assertTrue(m.getCVTerms().isEmpty());
26+
27+
annotater.annotate();
28+
29+
assertTrue(m.isSetMetaId());
30+
assertEquals(3, m.getCVTerms().size());
31+
assertCVTermIsPresent(m,
32+
CVTerm.Type.BIOLOGICAL_QUALIFIER,
33+
CVTerm.Qualifier.BQB_HAS_TAXON,
34+
"https://identifiers.org/taxonomy/511145");
35+
assertCVTermIsPresent(m,
36+
CVTerm.Type.MODEL_QUALIFIER,
37+
CVTerm.Qualifier.BQM_IS,
38+
"https://identifiers.org/bigg.model/iJO1366");
39+
assertCVTermIsPresent(m,
40+
CVTerm.Type.BIOLOGICAL_QUALIFIER,
41+
CVTerm.Qualifier.BQB_IS_VERSION_OF,
42+
"https://identifiers.org/refseq:NC_000913.3",
43+
"Expected NCBI refseq accession NC_000913.3 not present on the model.");
44+
}
45+
46+
}

0 commit comments

Comments
 (0)