-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from ebi-uniprot/GOA-2316-separate-descendants…
…-slim-converters separate descendants and slimming filter converters
- Loading branch information
Showing
6 changed files
with
226 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
.../ebi/quickgo/annotation/service/comm/rest/ontology/converter/SlimmingFilterConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
...uickgo/annotation/service/comm/rest/ontology/descendants/SlimmingFilterConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters