Skip to content

Commit

Permalink
test coverage for oan-rest, updating models to have equals and hashco…
Browse files Browse the repository at this point in the history
…de, adding neo4j harness for tests
  • Loading branch information
iimpulse committed Nov 10, 2023
1 parent f99c9d6 commit ce06400
Show file tree
Hide file tree
Showing 20 changed files with 506 additions and 37 deletions.
2 changes: 1 addition & 1 deletion oan-etl/src/main/java/org/jax/oan/GraphCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class GraphCommand implements Runnable {
@Option(names = {"-m", "--modules"}, description = "The list of modules to load into the graph.")
List<String> modules;

public static void main(String[] args) throws Exception {
public static void main(String[] args) {
PicocliRunner.run(GraphCommand.class, args);
}

Expand Down
10 changes: 10 additions & 0 deletions oan-model/src/main/java/org/jax/oan/core/Assay.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ public class Assay extends OntologyClass {
public Assay(TermId id, String name) {
super(id, name);
}

@Override
public boolean equals(Object o) {
return super.equals(o);
}

@Override
public int hashCode() {
return super.hashCode();
}
}
10 changes: 10 additions & 0 deletions oan-model/src/main/java/org/jax/oan/core/Disease.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ public class Disease extends OntologyClass {
public Disease(TermId id, String name) {
super(id, name);
}

@Override
public boolean equals(Object o) {
return super.equals(o);
}

@Override
public int hashCode() {
return super.hashCode();
}
}
10 changes: 10 additions & 0 deletions oan-model/src/main/java/org/jax/oan/core/Gene.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ public class Gene extends OntologyClass {
public Gene(TermId id, String name) {
super(id, name);
}

@Override
public boolean equals(Object o) {
return super.equals(o);
}

@Override
public int hashCode() {
return super.hashCode();
}
}
10 changes: 10 additions & 0 deletions oan-model/src/main/java/org/jax/oan/core/MedicalAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ public class MedicalAction extends OntologyClass {
public MedicalAction(TermId id, String name) {
super(id, name);
}

@Override
public boolean equals(Object o) {
return super.equals(o);
}

@Override
public int hashCode() {
return super.hashCode();
}
}
15 changes: 15 additions & 0 deletions oan-model/src/main/java/org/jax/oan/core/OntologyClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.monarchinitiative.phenol.ontology.data.TermId;

import java.util.Objects;

public abstract class OntologyClass {

TermId id;
Expand All @@ -20,4 +22,17 @@ public String getId() {
public String getName() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OntologyClass that = (OntologyClass) o;
return Objects.equals(id, that.id) && Objects.equals(name, that.name);
}

@Override
public int hashCode() {
return Objects.hash(id, name);
}
}
16 changes: 16 additions & 0 deletions oan-model/src/main/java/org/jax/oan/core/Phenotype.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.monarchinitiative.phenol.ontology.data.TermId;

import java.util.Objects;
import java.util.Optional;

public class Phenotype extends OntologyClass {
Expand Down Expand Up @@ -33,4 +34,19 @@ public Optional<PhenotypeMetadata> getMetadata() {
public String getCategory() {
return category;
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Phenotype phenotype = (Phenotype) o;
return Objects.equals(metadata, phenotype.metadata) && Objects.equals(category, phenotype.category);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), metadata, category);
}
}
14 changes: 14 additions & 0 deletions oan-model/src/main/java/org/jax/oan/core/PhenotypeMetadata.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jax.oan.core;

import java.util.List;
import java.util.Objects;

public class PhenotypeMetadata {

Expand Down Expand Up @@ -31,4 +32,17 @@ public String getFrequency() {
public List<String> getSources() {
return sources;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PhenotypeMetadata that = (PhenotypeMetadata) o;
return Objects.equals(sex, that.sex) && Objects.equals(onset, that.onset) && Objects.equals(frequency, that.frequency) && Objects.equals(sources, that.sources);
}

@Override
public int hashCode() {
return Objects.hash(sex, onset, frequency, sources);
}
}
5 changes: 5 additions & 0 deletions oan-rest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ dependencies {
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation("io.micronaut.test:micronaut-test-junit5")
testImplementation("org.mockito:mockito-junit-jupiter")
testRuntimeOnly('javax.ws.rs:javax.ws.rs-api:2.1.1')
testRuntimeOnly("javax.validation:validation-api:2.0.1.Final")
testRuntimeOnly("javax.annotation:javax.annotation-api:1.3.2")
testRuntimeOnly("org.neo4j.test:neo4j-harness")

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import org.neo4j.driver.Value;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.neo4j.driver.Values.parameters;

Expand Down Expand Up @@ -56,7 +59,7 @@ public List<Phenotype> findPhenotypesByDisease(TermId termId){
Record r = result.next();
Value p = r.get("p");
Value pm = r.get("pm");
PhenotypeMetadata phenotypeMetadata = new PhenotypeMetadata(pm.get("sex").asString(), pm.get("onset").asString(), pm.get("frequency").asString(), List.of(pm.get("sources").asString().split(";")));
PhenotypeMetadata phenotypeMetadata = new PhenotypeMetadata(pm.get("sex").asString(), pm.get("onset").asString(), pm.get("frequency").asString(), Arrays.stream(pm.get("sources").asString().split(";")).filter(Predicate.not(String::isBlank)).collect(Collectors.toList()));
Phenotype phenotype = new Phenotype(TermId.of(p.get("id").asString()), p.get("name").asString(), p.get("category").asString(), phenotypeMetadata);
phenotypes.add(phenotype);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public List<Assay> findAssaysByTerm(TermId termId){
Result result = tx.run("MATCH (a: Assay)-[:MEASURES]-(p: Phenotype {id: $id}) RETURN a", parameters("id", termId.getValue()));

while (result.hasNext()) {
Record record = result.next();
Assay assay = new Assay(TermId.of(record.get("id").asString()), record.get("name").asString());
Value value = result.next().get("a");
Assay assay = new Assay(TermId.of(value.get("id").asString()), value.get("name").asString());
assays.add(assay);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ public SystemFile associations(TermId termId, SupportedEntity source, SupportedE
case PHENOTYPE -> {
if(target.equals(SupportedEntity.DISEASE)){
List<Disease> diseases = this.phenotypeRepository.findDiseasesByTerm(termId);
return buildFile(String.format("dieases_for_%s", termId.getValue()), diseases);
} else {
return buildFile(String.format("diseases_for_%s", termId.getValue()), diseases);
} else if(target.equals(SupportedEntity.GENE)) {
List<Gene> genes = this.phenotypeRepository.findGenesByTerm(termId);
return buildFile(String.format("genes_for_%s", termId.getValue()), genes);
} else {
List<Assay> assays = this.phenotypeRepository.findAssaysByTerm(termId);
return buildFile(String.format("assays_for_%s", termId.getValue()), assays);
}
}
default -> throw new OntologyAnnotationNetworkRuntimeException(String.format("Downloading %s association for %s failed because source type is not supported.", target.toString().toLowerCase(), termId.getValue()));
Expand Down
1 change: 1 addition & 0 deletions oan-rest/src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
neo4j.embedded.ephemeral=true
38 changes: 38 additions & 0 deletions oan-rest/src/test/java/org/jax/oan/TestData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jax.oan;

import org.jax.oan.core.Assay;
import org.jax.oan.core.Disease;
import org.jax.oan.core.Gene;
import org.jax.oan.core.Phenotype;
import org.monarchinitiative.phenol.ontology.data.TermId;

import java.util.List;

public class TestData {
public static List<Disease> diseases(){
return List.of(
new Disease(TermId.of("MONDO:099233"),"Really bad one"),
new Disease(TermId.of("DECIPHER:434444"),"Kinda bad one")
);
}

public static List<Phenotype> phenotypes(){
return List.of(
new Phenotype(TermId.of("HP:099233"),"Long legs"),
new Phenotype(TermId.of("HP:434444"),"Big bicep small arm")
);
}

public static List<Assay> assays(){
return List.of(
new Assay(TermId.of("LOINC:55555"),"Special bicep test")
);
}

public static List<Gene> genes(){
return List.of(
new Gene(TermId.of("NCBIGene:00093"),"TP4"),
new Gene(TermId.of("NCBIGene:02002"),"YZ")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

import static org.jax.oan.TestData.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -85,10 +87,12 @@ void negative_by_incorrect_term(RequestSpecification spec){

@Test
void positive_download_file(RequestSpecification spec) throws IOException {
SystemFile file = buildSimpleSpreadSheet();
when(downloadService.associations(TermId.of("OMIM:0392932"), SupportedEntity.DISEASE, SupportedEntity.GENE))
.thenReturn(buildSimpleSpreadSheet());
.thenReturn(file);
spec.when().get("/api/annotation/OMIM:0392932/download/gene").then()
.statusCode(200).contentType("text/tab-separated-values");
Files.deleteIfExists(file.getFile().toPath());
}

@Test
Expand All @@ -106,33 +110,6 @@ void negative_download_file_bad_type(RequestSpecification spec){
spec.when().get("/api/annotation/OMIM:0392932/download/disease").then().statusCode(400);
}

private static List<Gene> genes(){
return List.of(
new Gene(TermId.of("NCBIGene:00093"),"TP4"),
new Gene(TermId.of("NCBIGene:02002"),"YZ")
);
}

private static List<Disease> diseases(){
return List.of(
new Disease(TermId.of("MONDO:099233"),"Really bad one"),
new Disease(TermId.of("DECIPHER:434444"),"Kinda bad one")
);
}

private static List<Phenotype> phenotypes(){
return List.of(
new Phenotype(TermId.of("HP:099233"),"Long legs"),
new Phenotype(TermId.of("HP:434444"),"Big bicep small arm")
);
}

private static List<Assay> assays(){
return List.of(
new Assay(TermId.of("LOINC:55555"),"Special bicep test")
);
}

private static SystemFile buildSimpleSpreadSheet() throws IOException {
File file = File.createTempFile("test", ".tsv");
try (PrintWriter pw = new PrintWriter(file)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.jax.oan.repository;

import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.jax.oan.core.Gene;
import org.jax.oan.core.Phenotype;
import org.jax.oan.core.PhenotypeMetadata;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.monarchinitiative.phenol.ontology.data.TermId;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Session;
import org.neo4j.driver.Transaction;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@MicronautTest
class DiseaseRepositoryTest {

@Inject
Driver driver;

@Inject
DiseaseRepository diseaseRepository;

@BeforeAll
void initialize() {
try(Transaction tx = driver.session().beginTransaction()){
tx.run("CREATE (d:Disease {id: 'OMIM:092320', name: 'Some bad disease'})");
tx.run("CREATE (p: Phenotype {id: 'HP:000001', name: 'short stature', category: ''})");
tx.run("CREATE (g:Gene {id: 'NCBIGene:9999', name: 'TX2'})");
tx.run("CREATE (g:Gene {id: 'NCBIGene:7777', name: 'MNN'})");
tx.run("MATCH (d:Disease {id: 'OMIM:092320'}), (p:Phenotype {id: 'HP:000001'})" +
"MERGE (d)-[:MANIFESTS]->(p)<-[:WITH_METADATA {context: 'OMIM:092320'}]-(pm: PhenotypeMetadata {onset: '', frequency: '1/1', sex: 'female'," +
"sources: '' })");
tx.run("MATCH (d:Disease {id: 'OMIM:092320'}), (g:Gene {id: 'NCBIGene:7777'}) " +
"MERGE (d)-[:EXPRESSES]->(g)");
tx.run("MATCH (d:Disease {id: 'OMIM:092320'}), (g:Gene {id: 'NCBIGene:9999'}) " +
"MERGE (d)-[:EXPRESSES]->(g)");
tx.commit();
}
}

@Test
void findGenesByDisease() {
List<Gene> genes = diseaseRepository.findGenesByDisease(TermId.of("OMIM:092320"));
List<Gene> expected = List.of(
new Gene(TermId.of("NCBIGene:9999"),"TX2"),
new Gene(TermId.of("NCBIGene:7777"),"MNN")
);
assertTrue(
expected.containsAll(genes)
);
}

@Test
void findPhenotypesByDisease() {
List<Phenotype> phenotypes = diseaseRepository.findPhenotypesByDisease(TermId.of("OMIM:092320"));
List<Phenotype> expected = List.of(
new Phenotype(TermId.of("HP:000001"), "short stature", "", new PhenotypeMetadata("female", "", "1/1", List.of()))
);

assertTrue(phenotypes.containsAll(expected));
}
}
Loading

0 comments on commit ce06400

Please sign in to comment.