Skip to content

Commit

Permalink
Merge pull request #41 from TheJacksonLaboratory/feature/search
Browse files Browse the repository at this point in the history
Feature-search-mp
  • Loading branch information
iimpulse authored Jan 24, 2024
2 parents 6d69bcd + a7e9654 commit 03d1705
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id("io.micronaut.test-resources") version "4.1.0"
}

version = "0.5.6"
version = "0.5.7"
group = "org.jacksonlaboratory"

repositories {
Expand Down
12 changes: 12 additions & 0 deletions config/ontologies.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,17 @@
"api": "https://ontology.jax.org/api/mondo/docs",
"international": false,
"description": "The Mondo Disease Ontology (Mondo) aims to harmonize disease definitions across the world. The name Mondo comes from the latin word 'mundus' and means 'for the world.'"
},
{
"name": "Mammalian Phenotype Ontology",
"prefix": "mp",
"github": {
"api": "https://api.github.com/repos/mgijax/mammalian-phenotype-ontology",
"home": "https://github.com/mgijax/mammalian-phenotype-ontology"
},
"home": "https://www.informatics.jax.org/vocab/mp_ontology",
"api": "https://ontology.jax.org/api/mp/docs",
"international": false,
"description": "The Mammalian Phenotype Ontology (MP) provides a standardized vocabulary of phenotypic abnormalities encountered in Mus musculus."
}
]
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package org.jacksonlaboratory.controller;

import com.fasterxml.jackson.annotation.JsonView;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.serde.annotation.SerdeImport;
import org.jacksonlaboratory.model.dto.SearchDto;
import org.jacksonlaboratory.model.entity.OntologyTerm;
import org.jacksonlaboratory.service.TermService;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;
import java.util.stream.Collectors;

@Controller("${api-url.prefix}/${ontology}/search")
@SerdeImport(SearchDto.class)
public class SearchController {

private TermService termService;
Expand All @@ -24,8 +27,18 @@ public SearchController(TermService termService) {
* @param query The search value
* @return List of matching ontology terms
*/
@Get(uri="/", produces="application/json")
public List<OntologyTerm> search(@QueryValue("q") @Schema(minLength = 3, maxLength = 250, type = "string", pattern = ".*") String query) {
return this.termService.searchOntologyTerm(query);
@Get(produces="application/json")
public SearchDto search(
@QueryValue("q") @Schema(minLength = 3, maxLength = 250, type = "string", pattern = ".*") String query,
@QueryValue(value = "page", defaultValue = "0") @Schema(maxLength = 1000, type = "number") int page,
@QueryValue(value = "limit", defaultValue = "10") @Schema(maxLength = 1, type = "number") int limit
) {
page = page * limit;
List<OntologyTerm> terms = this.termService.searchOntologyTerm(query);
if (limit == -1) {
return new SearchDto(terms, terms.size());
} else {
return new SearchDto(terms.stream().skip(page).limit(limit).collect(Collectors.toList()), terms.size());
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/jacksonlaboratory/model/dto/SearchDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.jacksonlaboratory.model.dto;

import org.jacksonlaboratory.model.entity.OntologyTerm;

import java.util.List;

public class SearchDto {
private final List<OntologyTerm> terms;
private final int totalCount;

public SearchDto(List<OntologyTerm> terms, int totalCount) {
this.terms = terms;
this.totalCount = totalCount;
}

public List<OntologyTerm> getTerms() {
return terms;
}

public int getTotalCount() {
return totalCount;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jacksonlaboratory.model.dto;

import com.fasterxml.jackson.annotation.JsonGetter;
import io.micronaut.serde.annotation.Serdeable;
import org.jacksonlaboratory.model.entity.OntologyTerm;
import org.jacksonlaboratory.model.entity.Translation;
Expand All @@ -13,12 +12,15 @@ public class SimpleOntologyTerm {
private final String id;
private final String name;

private final int nDescendant;

private final List<Translation> translations;

public SimpleOntologyTerm(OntologyTerm ontologyTerm) {
this.id = ontologyTerm.getId();
this.name = ontologyTerm.getName();
this.translations = ontologyTerm.getTranslations();
this.nDescendant = ontologyTerm.getDescendantCount();
}

public String getId() {
Expand All @@ -29,11 +31,14 @@ public String getName() {
return name;
}

@JsonGetter(value = "translations")
public List<Translation> getTranslations() {
return translations;
}

public int getDescendantCount() {
return nDescendant;
}

@Override
public String toString() {
return "SimpleOntologyTerm{" +
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/org/jacksonlaboratory/model/entity/OntologyTerm.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jacksonlaboratory.model.entity;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.micronaut.core.annotation.Creator;
import io.micronaut.serde.annotation.Serdeable;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -12,7 +11,6 @@
import org.monarchinitiative.phenol.ontology.data.Term;
import org.monarchinitiative.phenol.ontology.data.TermId;
import org.monarchinitiative.phenol.ontology.data.TermSynonym;
import org.jacksonlaboratory.view.Views;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -49,6 +47,9 @@ public class OntologyTerm {
@Column(columnDefinition = "text")
private String xrefs;

@Column(columnDefinition = "int")
private int nDescendants;

@Transient
private List<Translation> translations;

Expand Down Expand Up @@ -84,6 +85,11 @@ public String getId() {
return id.toString();
}

@JsonIgnore
public TermId getTermId() {
return id;
}

@Schema(maxLength = 255, type = "string", pattern = ".*")
public String getName() {
return name;
Expand All @@ -99,6 +105,10 @@ public String getComment() {
return comment;
}

public int getDescendantCount() {
return nDescendants;
}

@ArraySchema(maxItems = 25)
public List<String> getSynonyms() {
if(this.synonyms == null || this.synonyms.isBlank()){
Expand Down Expand Up @@ -127,6 +137,10 @@ public void setTranslation(List<Translation> translations) {
this.translations = translations;
}

public void setDescendantCount(int count) {
this.nDescendants = count;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public void init() {
log.info("Loading ontology terms..");
try {
this.termRepository.configure();
List<OntologyTerm> terms = graphService.getOntology().getTerms().stream().distinct().map(OntologyTerm::new).collect(Collectors.toList());
List<OntologyTerm> terms = graphService.getOntology().getTerms().stream().distinct().map(OntologyTerm::new).peek(
t -> t.setDescendantCount(graphService.getDescendantCount(t.getTermId()))
).collect(Collectors.toList());
this.termRepository.saveAll(terms);
log.info("Finished loading ontology terms..");
if (international){
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/jacksonlaboratory/service/GraphService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jacksonlaboratory.service;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.annotation.Value;
Expand Down Expand Up @@ -72,6 +73,11 @@ public List<SimpleOntologyTerm> getChildren(TermId termId) {
}
}

@JsonIgnore
public int getDescendantCount(TermId termId){
return (int) this.ontology.graph().getDescendantsStream(termId, false).count();
}

public MinimalOntology getOntology() {
return ontology;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ class SearchControllerSpec extends Specification {

void "should search #q and return the fake object"() {
when:
def response = client.toBlocking().retrieve(HttpRequest.GET('/api/hp/search?q='+ q), Argument.listOf(Map.class))
def response = client.toBlocking().retrieve(HttpRequest.GET('/api/hp/search?q='+ q), Map.class)
then:
termService.searchOntologyTerm(q) >> res
response.size() == res.size()
response.get(0).get("id").equals(res.get(0).getId())
response.get(0).get("name").equals(res.get(0).getName())
response.get(0).get("definition").equals(res.get(0).getDefinition())

response.get("terms").size() == res.size();
response.get("terms").get(0).get("id").equals(res.get(0).getId())
response.get("terms").get(0).get("name").equals(res.get(0).getName())
response.get("terms").get(0).get("definition").equals(res.get(0).getDefinition())
response.get("terms").get(0).get("comment").equals(res.get(0).getComment())
where:
q | res
"arach" | [new OntologyTerm(TermId.of("HP:000003"), "fake name", "fake def", "comment")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ class OntologyTermSpec extends Specification {

result.contains("my name")
result.contains("def")
result == "{\"id\":\"HP:000001\",\"name\":\"my name\",\"definition\":\"def\",\"comment\":null,\"synonyms\":[],\"xrefs\":[],\"translations\":null}"
result == "{\"id\":\"HP:000001\",\"name\":\"my name\",\"definition\":\"def\",\"comment\":null,\"synonyms\":[],\"xrefs\":[],\"translations\":null,\"descendantCount\":0}"
}
}

0 comments on commit 03d1705

Please sign in to comment.