Skip to content

Commit

Permalink
added a paginated term instances endpoint and made related from and e…
Browse files Browse the repository at this point in the history
…quivalent class endpoints paginated for #98
  • Loading branch information
giraygi committed Nov 22, 2024
1 parent a91be29 commit 3fce34b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriUtils;
import uk.ac.ebi.spot.ols.model.v1.V1Individual;
import uk.ac.ebi.spot.ols.model.v1.V1Term;
import uk.ac.ebi.spot.ols.repository.v1.V1GraphRepository;
import uk.ac.ebi.spot.ols.repository.v1.V1JsTreeRepository;
Expand Down Expand Up @@ -53,6 +54,9 @@ public class V1OntologyTermController {
@Autowired
V1TermAssembler termAssembler;

@Autowired
V1IndividualAssembler individualAssembler;

@Autowired
V1PreferredRootTermAssembler preferredRootTermAssembler;

Expand Down Expand Up @@ -410,39 +414,84 @@ HttpEntity<PagedModel<V1Term>> ancestors(@PathVariable("onto")

@RequestMapping(path = "/{onto}/terms/{iri}/equivalentclasses", produces = {MediaType.APPLICATION_JSON_VALUE,
MediaTypes.HAL_JSON_VALUE}, method = RequestMethod.GET)
HttpEntity<List<Map<String,Object>>> getEquivalentClasses(
HttpEntity<PagedModel<V1Term>> getEquivalentClasses(
@PathVariable("onto")
@Parameter(name = "onto",
description = "The ID of the ontology. For example for Data Use Ontology, the ID is duo.",
example = "duo") String ontologyId,
@PathVariable("iri")
@Parameter(name = "iri",
description = "The IRI of the term, this value must be single URL encoded",
example = "http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2FDUO_0000017") String termId) {
example = "http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2FDUO_0000017") String termId,
@RequestParam(value = "lang", required = false, defaultValue = "en") String lang,
@Parameter(hidden = true) Pageable pageable,
@Parameter(hidden = true) PagedResourcesAssembler assembler) {

ontologyId = ontologyId.toLowerCase();

String decoded = UriUtils.decode(termId, "UTF-8");
String entityId = ontologyId+"+class+"+decoded;
return new ResponseEntity<>( graphRepository.getEquivalentClass(entityId), HttpStatus.OK);
Page<V1Term> equivalentClasses = graphRepository.getEquivalentClassPaginated(entityId, lang, pageable);
if (equivalentClasses == null)
throw new ResourceNotFoundException("No equivalent classes could be found for " + ontologyId
+ " and " + termId);

return new ResponseEntity<>( assembler.toModel(equivalentClasses, termAssembler), HttpStatus.OK);
}


@RequestMapping(path = "/{onto}/terms/{iri}/relatedfrom", produces = {MediaType.APPLICATION_JSON_VALUE,
MediaTypes.HAL_JSON_VALUE}, method = RequestMethod.GET)
HttpEntity<Map<String,Object>> getRelatedFrom(
HttpEntity<PagedModel<V1Term>> getRelatedFrom(
@PathVariable("onto")
@Parameter(name = "onto",
description = "The ID of the ontology. For example for Data Use Ontology, the ID is duo.",
example = "duo") String ontologyId,
@PathVariable("iri")
@Parameter(name = "iri",
description = "The IRI of the term, this value must be single URL encoded",
example = "http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2FDUO_0000017") String termId,
@RequestParam(value = "lang", required = false, defaultValue = "en") String lang,
@Parameter(hidden = true) Pageable pageable,
@Parameter(hidden = true) PagedResourcesAssembler assembler) {

ontologyId = ontologyId.toLowerCase();

String decoded = UriUtils.decode(termId, "UTF-8");
String entityId = ontologyId+"+class+"+decoded;
Page<V1Term> relatedFroms = graphRepository.getRelatedFromPaginated(entityId, lang, pageable);
if (relatedFroms == null)
throw new ResourceNotFoundException("No related from terms could be found for " + ontologyId
+ " and " + termId);

return new ResponseEntity<>( assembler.toModel(relatedFroms, termAssembler), HttpStatus.OK);
}

@RequestMapping(path = "/{onto}/terms/{iri}/instances", produces = {MediaType.APPLICATION_JSON_VALUE,
MediaTypes.HAL_JSON_VALUE}, method = RequestMethod.GET)
HttpEntity<PagedModel<V1Individual>> getInstances(
@PathVariable("onto")
@Parameter(name = "onto",
description = "The ID of the ontology. For example for Data Use Ontology, the ID is duo.",
example = "duo") String ontologyId,
@PathVariable("iri")
@Parameter(name = "iri",
description = "The IRI of the term, this value must be single URL encoded",
example = "http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2FDUO_0000017") String termId) {
example = "http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2FDUO_0000017") String termId,
@RequestParam(value = "lang", required = false, defaultValue = "en") String lang,
@Parameter(hidden = true) Pageable pageable,
@Parameter(hidden = true) PagedResourcesAssembler assembler) {

ontologyId = ontologyId.toLowerCase();

String decoded = UriUtils.decode(termId, "UTF-8");
String entityId = ontologyId+"+class+"+decoded;
return new ResponseEntity<>( graphRepository.getRelatedFrom(entityId), HttpStatus.OK);
Page<V1Individual> instances = graphRepository.getTermInstancesPaginated(entityId, lang, pageable);
if (instances == null)
throw new ResourceNotFoundException("No instances could be found for " + ontologyId
+ " and " + termId);

return new ResponseEntity<>( assembler.toModel(instances, individualAssembler), HttpStatus.OK);
}

@RequestMapping(path = "/{onto}/terms/{iri}/jstree",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import uk.ac.ebi.spot.ols.model.v1.V1Individual;
import uk.ac.ebi.spot.ols.model.v1.V1Term;
import uk.ac.ebi.spot.ols.repository.transforms.LocalizationTransform;
import uk.ac.ebi.spot.ols.repository.transforms.RemoveLiteralDatatypesTransform;
import uk.ac.ebi.spot.ols.repository.v1.mappers.V1IndividualMapper;
import uk.ac.ebi.spot.ols.repository.v1.mappers.V1TermMapper;
import uk.ac.ebi.spot.ols.service.Neo4jClient;

import java.util.*;
import java.util.stream.Collectors;

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

@Component
public class V1GraphRepository {

Expand Down Expand Up @@ -134,26 +140,46 @@ Map<String,Object> getParentsAndRelatedTo(String entityId) {
return (Map<String,Object>) results.get(0).get("result");
}

public Map<String,Object> getRelatedFrom(String entityId) {
Map<String,Object> getRelatedFrom(String entityId) {

String query =
"MATCH path = (x)-[r:relatedTo]->(n:OntologyClass)\n"
+ "WHERE n.id=\"" + entityId + "\"\n"
+ "RETURN { nodes: collect({ label: x.label, iri: x.iri }),\n"
+ "edges: collect({ source: startNode(r).iri, target: endNode(r).iri, relationship: type(r) })\n"
+ "RETURN { nodes: collect(distinct x),\n"
+ "edges: collect({ source: startNode(r).iri, target: endNode(r).iri, relationship: r })\n"
+ "} AS result";

List<Map<String,Object>> results = neo4jClient.rawQuery(query);
return (Map<String,Object>) results.get(0).get("result");
}

public List<Map<String,Object>> getEquivalentClass(String entityId) {
public Page<V1Term> getRelatedFromPaginated(String entityId, String lang, Pageable pageable) {
String query = "MATCH (x:OntologyClass)-[r:relatedTo]->(n:OntologyClass) WHERE n.id= $id RETURN x";
String countQuery = "MATCH (x:OntologyClass)-[r:relatedTo]->(n:OntologyClass) WHERE n.id= $id RETURN count(x)";

return neo4jClient.queryPaginated(query, "x", countQuery, parameters("id", entityId), pageable).map(record -> V1TermMapper.mapTerm(record, lang));
}

public Page<V1Term> getEquivalentClassPaginated(String entityId, String lang, Pageable pageable) {
String query =
"MATCH (a:OntologyClass)-[r:`http://www.w3.org/2002/07/owl#equivalentClass`]-(b:OntologyClass) " +
"WHERE a.id = '"+entityId+"' RETURN {nodes: collect( DISTINCT { label: b.label, iri: b.iri })," +
"edges: collect({ source: startNode(r).iri, target: endNode(r).iri, relationship: type(r) })} AS result";
"WHERE a.id = $id RETURN DISTINCT b";
String countQuery =
"MATCH (a:OntologyClass)-[r:`http://www.w3.org/2002/07/owl#equivalentClass`]-(b:OntologyClass) " +
"WHERE a.id = $id RETURN count(DISTINCT b)";

List<Map<String,Object>> results = neo4jClient.rawQuery(query);
return results;
return neo4jClient.queryPaginated(query, "b", countQuery, parameters("id", entityId), pageable).map(record -> V1TermMapper.mapTerm(record, lang));
}

public Page<V1Individual> getTermInstancesPaginated(String entityId, String lang, Pageable pageable) {
String query =
"MATCH (a:OntologyClass)<-[r:`http://www.w3.org/1999/02/22-rdf-syntax-ns#type`]-(b:OntologyIndividual) " +
"WHERE a.id = $id RETURN b";
String countQuery =
"MATCH (a:OntologyClass)<-[r:`http://www.w3.org/1999/02/22-rdf-syntax-ns#type`]-(b:OntologyIndividual) " +
"WHERE a.id = $id RETURN count(b)";

return neo4jClient.queryPaginated(query, "b", countQuery, parameters("id", entityId), pageable).map(record -> V1IndividualMapper.mapIndividual(record, lang));
}

JsonObject getOntologyNodeJson(Node node, String lang) {
Expand Down

0 comments on commit 3fce34b

Please sign in to comment.