Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate descendants and slimming filter converters #65

Merged
merged 1 commit into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationFields;
import uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.model.ConvertedOntologyFilter;
import uk.ac.ebi.quickgo.rest.comm.FilterContext;
import uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery;
import uk.ac.ebi.quickgo.rest.search.request.converter.ConvertedFilter;
import uk.ac.ebi.quickgo.rest.search.request.converter.FilterConverter;
Expand All @@ -16,9 +15,7 @@
/**
* This class is responsible for converting an ontology model containing GO id descendant information, to a
* {@link ConvertedFilter}. This result encapsulates a {@link QuickGOQuery} applicable to filtering the annotation
* core data, by these descendants. Additionally, this process records slimming information consisting of original
* GO ids and their mappings to slimmed up GO ids; this is stored within an instance of {@link SlimmingConversionInfo}
* available from the {@link ConvertedFilter}'s {@link FilterContext}.
* core data, by these descendants.
*
* Created 09/08/16
* @author Edd
Expand All @@ -28,24 +25,16 @@ public class DescendantsFilterConverter implements FilterConverter<ConvertedOnto
@Override public ConvertedFilter<QuickGOQuery> transform(ConvertedOntologyFilter response) {
ConvertedFilter<QuickGOQuery> convertedFilter;

SlimmingConversionInfo conversionInfo = new SlimmingConversionInfo();

if (response.getResults() != null && atLeastOneDescendantExists(response)) {
Set<QuickGOQuery> queries = new HashSet<>();
FilterContext context = new FilterContext();

for (ConvertedOntologyFilter.Result result : response.getResults()) {
for (String desc : result.getDescendants()) {
queries.add(QuickGOQuery.createQuery(AnnotationFields.GO_ID, desc));
conversionInfo.addOriginal2SlimmedGOIdMapping(desc, result.getId());
}
}

context.save(SlimmingConversionInfo.class, conversionInfo);

convertedFilter = new ConvertedFilter<>(
or(queries.toArray(new QuickGOQuery[queries.size()])),
context);
convertedFilter = new ConvertedFilter<>(or(queries.toArray(new QuickGOQuery[queries.size()])));
} else {
convertedFilter = new ConvertedFilter<>(not(QuickGOQuery.createAllQuery()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.converter;

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationFields;
import uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.model.ConvertedOntologyFilter;
import uk.ac.ebi.quickgo.rest.comm.FilterContext;
import uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery;
import uk.ac.ebi.quickgo.rest.search.request.converter.ConvertedFilter;
import uk.ac.ebi.quickgo.rest.search.request.converter.FilterConverter;

import java.util.HashSet;
import java.util.Set;

import static uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery.not;
import static uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery.or;

/**
* This class is responsible for converting an ontology model containing GO id descendant information, to a
* {@link ConvertedFilter}. This result encapsulates a {@link QuickGOQuery} applicable to filtering the annotation
* core data, by these descendants. Additionally, this process records slimming information consisting of original
* GO ids and their mappings to slimmed up GO ids; this is stored within an instance of {@link SlimmingConversionInfo}
* available from the {@link ConvertedFilter}'s {@link FilterContext}.
*
* Created 09/08/16
* @author Edd
*/
public class SlimmingFilterConverter implements FilterConverter<ConvertedOntologyFilter, QuickGOQuery> {

@Override public ConvertedFilter<QuickGOQuery> transform(ConvertedOntologyFilter response) {
ConvertedFilter<QuickGOQuery> convertedFilter;

SlimmingConversionInfo conversionInfo = new SlimmingConversionInfo();

if (response.getResults() != null && atLeastOneDescendantExists(response)) {
Set<QuickGOQuery> queries = new HashSet<>();
FilterContext context = new FilterContext();

for (ConvertedOntologyFilter.Result result : response.getResults()) {
for (String desc : result.getDescendants()) {
queries.add(QuickGOQuery.createQuery(AnnotationFields.GO_ID, desc));
conversionInfo.addOriginal2SlimmedGOIdMapping(desc, result.getId());
}
}

context.save(SlimmingConversionInfo.class, conversionInfo);

convertedFilter = new ConvertedFilter<>(
or(queries.toArray(new QuickGOQuery[queries.size()])),
context);
} else {
convertedFilter = new ConvertedFilter<>(not(QuickGOQuery.createAllQuery()));
}

return convertedFilter;
}

private boolean atLeastOneDescendantExists(ConvertedOntologyFilter response) {
return response.getResults().stream()
.filter(r -> !r.getDescendants().isEmpty())
.findFirst()
.isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationFields;
import uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.converter.DescendantsFilterConverter;
import uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.converter.SlimmingConversionInfo;
import uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.model.ConvertedOntologyFilter;
import uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery;
import uk.ac.ebi.quickgo.rest.search.request.converter.ConvertedFilter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.core.Is.is;
import static uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery.not;
import static uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery.or;
Expand Down Expand Up @@ -45,6 +41,7 @@ public void descendantsFromSingleResourceAreConvertedToQuickGOQuery() {
ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(convertedFilter.getConvertedValue(), is(QuickGOQuery.createQuery(AnnotationFields.GO_ID, desc1)));
assertThat(convertedFilter.getFilterContext(), is(Optional.empty()));
}

@Test
Expand All @@ -62,6 +59,7 @@ public void differentDescendantsFromMultipleResourcesAreConvertedToQuickGOQuery(
assertThat(convertedFilter.getConvertedValue(), is(
or(QuickGOQuery.createQuery(AnnotationFields.GO_ID, desc1),
QuickGOQuery.createQuery(AnnotationFields.GO_ID, desc2))));
assertThat(convertedFilter.getFilterContext(), is(Optional.empty()));
}

@Test
Expand All @@ -77,6 +75,7 @@ public void sameDescendantsFromMultipleResourcesAreConvertedToQuickGOQuery() {

assertThat(convertedFilter.getConvertedValue(), is(
QuickGOQuery.createQuery(AnnotationFields.GO_ID, desc1)));
assertThat(convertedFilter.getFilterContext(), is(Optional.empty()));
}

@Test
Expand All @@ -85,52 +84,15 @@ public void nullResultsMeansFilterEverything() {
ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(convertedFilter.getConvertedValue(), is(not(QuickGOQuery.createAllQuery())));
assertThat(convertedFilter.getFilterContext(), is(Optional.empty()));
}

@Test
public void emptyResultsMeansFilterEverything() {
ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(convertedFilter.getConvertedValue(), is(not(QuickGOQuery.createAllQuery())));
}

@Test
public void conversionContextContainsOneMapping() {
String id1 = "id1";
String desc1 = "desc1";

addResponseDescendant(id1, desc1);
ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(extractContextProperties(convertedFilter), hasEntry(desc1, singletonList(id1)));
}

@Test
public void conversionContextContainsTwoMappings() {
String id1 = "id1";
String id2 = "id2";
String desc1 = "desc1";
String desc2 = "desc2";
String desc3 = "desc3";

addResponseDescendant(id1, desc1);
addResponseDescendant(id1, desc2);
addResponseDescendant(id1, desc3);
addResponseDescendant(id2, desc3);
ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(extractContextProperties(convertedFilter), hasEntry(desc1, singletonList(id1)));
assertThat(extractContextProperties(convertedFilter), hasEntry(desc2, singletonList(id1)));
assertThat(extractContextProperties(convertedFilter), hasEntry(desc3, asList(id1, id2)));
}

private Map<String, List<String>> extractContextProperties(ConvertedFilter<QuickGOQuery> convertedFilter) {
SlimmingConversionInfo conversionInfo = convertedFilter.getFilterContext()
.map(t -> t.get(SlimmingConversionInfo.class)
.orElse(new SlimmingConversionInfo()))
.orElseThrow(IllegalStateException::new);

return conversionInfo.getInfo();
assertThat(convertedFilter.getFilterContext(), is(Optional.empty()));
}

private void addResponseDescendant(String termId, String descendantId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.descendants;

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationFields;
import uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.converter.SlimmingConversionInfo;
import uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.converter.SlimmingFilterConverter;
import uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.model.ConvertedOntologyFilter;
import uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery;
import uk.ac.ebi.quickgo.rest.search.request.converter.ConvertedFilter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.core.Is.is;
import static uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery.not;
import static uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery.or;

/**
* Created 10/08/16
* @author Edd
*/
public class SlimmingFilterConverterTest {
private ConvertedOntologyFilter response;
private SlimmingFilterConverter converter;

@Before
public void setUp() {
response = new ConvertedOntologyFilter();
response.setResults(new ArrayList<>());
converter = new SlimmingFilterConverter();
}

@Test
public void descendantsFromSingleResourceAreConvertedToQuickGOQuery() {
String id1 = "id1";
String desc1 = "desc1";

addResponseDescendant(id1, desc1);
ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(convertedFilter.getConvertedValue(), is(QuickGOQuery.createQuery(AnnotationFields.GO_ID, desc1)));
assertThat(convertedFilter.getFilterContext().isPresent(), is(true));
}

@Test
public void differentDescendantsFromMultipleResourcesAreConvertedToQuickGOQuery() {
String id1 = "id1";
String id2 = "id2";
String desc1 = "desc1";
String desc2 = "desc2";

addResponseDescendant(id1, desc1);
addResponseDescendant(id2, desc2);

ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(convertedFilter.getConvertedValue(), is(
or(QuickGOQuery.createQuery(AnnotationFields.GO_ID, desc1),
QuickGOQuery.createQuery(AnnotationFields.GO_ID, desc2))));
assertThat(convertedFilter.getFilterContext().isPresent(), is(true));
}

@Test
public void sameDescendantsFromMultipleResourcesAreConvertedToQuickGOQuery() {
String id1 = "id1";
String id2 = "id2";
String desc1 = "desc1";

addResponseDescendant(id1, desc1);
addResponseDescendant(id2, desc1);

ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(convertedFilter.getConvertedValue(), is(
QuickGOQuery.createQuery(AnnotationFields.GO_ID, desc1)));
assertThat(convertedFilter.getFilterContext().isPresent(), is(true));
}

@Test
public void nullResultsMeansFilterEverything() {
response.setResults(null);
ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(convertedFilter.getConvertedValue(), is(not(QuickGOQuery.createAllQuery())));
}

@Test
public void emptyResultsMeansFilterEverything() {
ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(convertedFilter.getConvertedValue(), is(not(QuickGOQuery.createAllQuery())));
}

@Test
public void conversionContextContainsOneMapping() {
String id1 = "id1";
String desc1 = "desc1";

addResponseDescendant(id1, desc1);
ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(extractContextProperties(convertedFilter), hasEntry(desc1, singletonList(id1)));
}

@Test
public void conversionContextContainsTwoMappings() {
String id1 = "id1";
String id2 = "id2";
String desc1 = "desc1";
String desc2 = "desc2";
String desc3 = "desc3";

addResponseDescendant(id1, desc1);
addResponseDescendant(id1, desc2);
addResponseDescendant(id1, desc3);
addResponseDescendant(id2, desc3);
ConvertedFilter<QuickGOQuery> convertedFilter = converter.transform(response);

assertThat(extractContextProperties(convertedFilter), hasEntry(desc1, singletonList(id1)));
assertThat(extractContextProperties(convertedFilter), hasEntry(desc2, singletonList(id1)));
assertThat(extractContextProperties(convertedFilter), hasEntry(desc3, asList(id1, id2)));
}

private Map<String, List<String>> extractContextProperties(ConvertedFilter<QuickGOQuery> convertedFilter) {
SlimmingConversionInfo conversionInfo = convertedFilter.getFilterContext()
.map(t -> t.get(SlimmingConversionInfo.class)
.orElse(new SlimmingConversionInfo()))
.orElseThrow(IllegalStateException::new);

return conversionInfo.getInfo();
}

private void addResponseDescendant(String termId, String descendantId) {
for (ConvertedOntologyFilter.Result result : response.getResults()) {
if (result.getId().equals(termId)) {
result.getDescendants().add(descendantId);
return;
}
}

ConvertedOntologyFilter.Result newResult = new ConvertedOntologyFilter.Result();
newResult.setId(termId);
List<String> descList = new ArrayList<>();
descList.add(descendantId);
newResult.setDescendants(descList);
response.getResults().add(newResult);
}
}
2 changes: 1 addition & 1 deletion annotation-rest/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ search:
ip: localhost,
resourceFormat: "/QuickGO/services/go/terms/{usageIds}/descendants?relations={usageRelationships}",
responseClass: "uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.model.ConvertedOntologyFilter",
responseConverter: "uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.converter.DescendantsFilterConverter"
responseConverter: "uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.converter.SlimmingFilterConverter"
}
model:
repo2domainFieldNameTransformations:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ search:
ip: "http://ves-hx-c2:8082",
resourceFormat: "/QuickGO/services/go/terms/{usageIds}/descendants?relations={usageRelationships}",
responseClass: "uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.model.ConvertedOntologyFilter",
responseConverter: "uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.converter.DescendantsFilterConverter"
responseConverter: "uk.ac.ebi.quickgo.annotation.service.comm.rest.ontology.converter.SlimmingFilterConverter"
}
model:
repo2domainFieldNameTransformations:
Expand Down