Skip to content

Commit

Permalink
contig alias name translation service
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-ebi committed Sep 5, 2024
1 parent e234728 commit fb41659
Show file tree
Hide file tree
Showing 19 changed files with 556 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* Copyright 2024 EMBL - European Bioinformatics Institute
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package uk.ac.ebi.eva.server.configuration;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import uk.ac.ebi.eva.server.ws.contigalias.ContigAliasInputParameters;
import uk.ac.ebi.eva.server.ws.contigalias.ContigAliasService;

@Configuration
public class ContigAliasConfiguration {

@Bean(name = "CONTIG_ALIAS_REST_TEMPLATE")
public RestTemplate restTemplate() {
return new RestTemplate();
}

@Bean
@ConfigurationProperties(prefix = "contig-alias")
public ContigAliasInputParameters contigAliasInputParameters() {
return new ContigAliasInputParameters();
}

@Bean
public ContigAliasService contigAliasService(@Qualifier("CONTIG_ALIAS_REST_TEMPLATE") RestTemplate restTemplate,
ContigAliasInputParameters contigAliasInputParameters) {
return new ContigAliasService(restTemplate, contigAliasInputParameters.getUrl());
}
}
13 changes: 11 additions & 2 deletions eva-server/src/main/java/uk/ac/ebi/eva/server/ws/GeneWSServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.web.bind.annotation.RestController;

import uk.ac.ebi.eva.commons.core.models.AnnotationMetadata;
import uk.ac.ebi.eva.commons.core.models.contigalias.ContigNamingConvention;
import uk.ac.ebi.eva.commons.core.models.ws.VariantWithSamplesAndAnnotation;
import uk.ac.ebi.eva.commons.mongodb.filter.FilterBuilder;
import uk.ac.ebi.eva.commons.mongodb.filter.VariantRepositoryFilter;
Expand All @@ -38,6 +39,7 @@
import uk.ac.ebi.eva.lib.eva_utils.DBAdaptorConnector;
import uk.ac.ebi.eva.lib.eva_utils.MultiMongoDbFactory;
import uk.ac.ebi.eva.server.Utils;
import uk.ac.ebi.eva.server.ws.contigalias.ContigAliasService;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
Expand All @@ -51,6 +53,9 @@ public class GeneWSServer extends EvaWSServer {
@Autowired
private VariantWithSamplesAndAnnotationsService service;

@Autowired
private ContigAliasService contigAliasService;

public GeneWSServer() {
}

Expand All @@ -66,6 +71,7 @@ public QueryResponse getVariantsByGene(@PathVariable("geneIds") List<String> gen
@RequestParam(name = "exclude", required = false) List<String> exclude,
@RequestParam(name = "annot-vep-version", required = false) String annotationVepVersion,
@RequestParam(name = "annot-vep-cache-version", required = false) String annotationVepCacheVersion,
@RequestParam(name = "contigNamingConvention", required = false) ContigNamingConvention contigNamingConvention,
HttpServletResponse response) {
initializeQuery();

Expand Down Expand Up @@ -113,7 +119,9 @@ public QueryResponse getVariantsByGene(@PathVariable("geneIds") List<String> gen

Long numTotalResults = service.countByGenesAndComplexFilters(geneIds, filters);

QueryResult<VariantWithSamplesAndAnnotation> queryResult = buildQueryResult(variantEntities, numTotalResults);
QueryResult<VariantWithSamplesAndAnnotation> queryResult = buildQueryResult(
contigAliasService.getVariantsWithTranslatedContig(variantEntities, contigNamingConvention),
numTotalResults);
return setQueryResponse(queryResult);
}

Expand All @@ -128,9 +136,10 @@ public QueryResponse getVariantsByGenePOST(@PathVariable("geneIds") List<String>
@RequestParam(name = "exclude", required = false) List<String> exclude,
@RequestParam(name = "annot-vep-version", required = false) String annotationVepVersion,
@RequestParam(name = "annot-vep-cache-version", required = false) String annotationVepCacheversion,
@RequestParam(name = "contigNamingConvention", required = false) ContigNamingConvention contigNamingConvention,
HttpServletResponse response) throws AnnotationMetadataNotFoundException {
return getVariantsByGene(geneIds, species, studies, consequenceType, maf, polyphenScore, siftScore, exclude,
annotationVepVersion, annotationVepCacheversion, response);
annotationVepVersion, annotationVepCacheversion, contigNamingConvention, response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedResources;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.annotations.ApiIgnore;
import uk.ac.ebi.eva.commons.core.models.FeatureCoordinates;
import uk.ac.ebi.eva.commons.core.models.contigalias.ContigNamingConvention;
import uk.ac.ebi.eva.commons.mongodb.services.FeatureService;
import uk.ac.ebi.eva.lib.eva_utils.DBAdaptorConnector;
import uk.ac.ebi.eva.lib.eva_utils.MultiMongoDbFactory;
Expand All @@ -36,6 +38,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.eva.server.ws.contigalias.ContigAliasService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -56,6 +59,9 @@ public class GeneWSServerV2 {
@Autowired
private RegionWSServerV2 regionWSServerV2;

@Autowired
private ContigAliasService contigAliasService;

public GeneWSServerV2() {
}

Expand Down Expand Up @@ -96,6 +102,8 @@ public ResponseEntity getVariantsByGene(
"e.g. 78")
@RequestParam(name = "annot-vep-cache-version", required = false) String
annotationVepCacheVersion,
@ApiParam(value = "Contig naming convention desired, default is INSDC")
@RequestParam(name = "contigNamingConvention", required = false) ContigNamingConvention contigNamingConvention,
@ApiParam(value = "The number of the page that should be displayed. Starts from 0 and is an integer.")
@RequestParam(required = false, defaultValue = "0") Integer pageNumber,
@ApiParam(value = "The number of elements that should be retrieved per page.")
Expand Down Expand Up @@ -124,7 +132,7 @@ public ResponseEntity getVariantsByGene(

ResponseEntity<PagedResources> responseEntity = regionWSServerV2.getVariantsByRegion(regions, species,
assembly, studies, consequenceType, maf, polyphenScore, siftScore, annotationVepVersion,
annotationVepCacheVersion, pageNumber, pageSize, response, request);
annotationVepCacheVersion, contigNamingConvention, pageNumber, pageSize, response, request);

if (responseEntity.getStatusCode() != HttpStatus.OK) {
return responseEntity;
Expand All @@ -133,8 +141,8 @@ public ResponseEntity getVariantsByGene(
responseEntity.getBody().removeLinks();

return new ResponseEntity(buildPage(geneIds, species, assembly, studies, consequenceType, maf, polyphenScore,
siftScore, annotationVepVersion, annotationVepCacheVersion, bufferValue, responseEntity.getBody(),
response, request), HttpStatus.OK);
siftScore, annotationVepVersion, annotationVepCacheVersion, contigNamingConvention, bufferValue,
responseEntity.getBody(), response, request), HttpStatus.OK);
}

private void checkParameters(List<String> geneIds, String species, String assembly, Integer bufferValue)
Expand Down Expand Up @@ -168,44 +176,48 @@ private String getRegionString(FeatureCoordinates coordinates) {
private PagedResources buildPage(List<String> geneIds, String species, String assembly, List<String> studies,
List<String> consequenceType, String maf, String polyphenScore, String siftScore,
String annotationVepVersion, String annotationVepCacheVersion,
Integer bufferValue, PagedResources pagedResources,
HttpServletResponse response, HttpServletRequest request) {
ContigNamingConvention contigNamingConvention, Integer bufferValue,
PagedResources pagedResources, HttpServletResponse response,
HttpServletRequest request) {

int pageNumber = (int) pagedResources.getMetadata().getNumber();
int pageSize = (int) pagedResources.getMetadata().getSize();
int totalPages = (int) pagedResources.getMetadata().getTotalPages();

if (pageNumber > 0) {
pagedResources.add(createPaginationLink(geneIds, species, assembly, studies, consequenceType,
maf, polyphenScore, siftScore, annotationVepVersion, annotationVepCacheVersion,
maf, polyphenScore, siftScore, annotationVepVersion, annotationVepCacheVersion, contigNamingConvention,
pageNumber - 1, pageSize, bufferValue, response, request, "prev"));

pagedResources.add(createPaginationLink(geneIds, species, assembly, studies, consequenceType,
maf, polyphenScore, siftScore, annotationVepVersion, annotationVepCacheVersion,
0, pageSize, bufferValue, response, request, "first"));
contigNamingConvention, 0, pageSize, bufferValue, response, request, "first"));
}

if (pageNumber < (totalPages - 1)) {
pagedResources.add(createPaginationLink(geneIds, species, assembly, studies, consequenceType,
maf, polyphenScore, siftScore, annotationVepVersion, annotationVepCacheVersion,
pageNumber + 1, pageSize, bufferValue, response, request, "next"));
contigNamingConvention, pageNumber + 1, pageSize, bufferValue, response, request,
"next"));

pagedResources.add(createPaginationLink(geneIds, species, assembly, studies, consequenceType,
maf, polyphenScore, siftScore, annotationVepVersion, annotationVepCacheVersion,
totalPages - 1, pageSize, bufferValue, response, request, "last"));
contigNamingConvention, totalPages - 1, pageSize, bufferValue, response, request,
"last"));
}
return pagedResources;
}

private Link createPaginationLink(List<String> geneIds, String species, String assembly, List<String> studies,
List<String> consequenceType, String maf, String polyphenScore, String siftScore,
String annotationVepVersion, String annotationVepCacheVersion,
ContigNamingConvention contigNamingConvention,
int pageNumber, int pageSize, Integer bufferValue, HttpServletResponse response,
HttpServletRequest request,
String linkName) {
return new Link(linkTo(methodOn(GeneWSServerV2.class).getVariantsByGene(geneIds, species, assembly, studies,
consequenceType, maf, polyphenScore, siftScore, annotationVepVersion,
annotationVepCacheVersion, pageNumber, pageSize, bufferValue, response, request))
annotationVepCacheVersion, contigNamingConvention, pageNumber, pageSize, bufferValue, response, request))
.toUriComponentsBuilder()
.toUriString(), linkName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.eva.commons.core.models.contigalias.ContigNamingConvention;
import uk.ac.ebi.eva.commons.core.models.pipeline.Variant;
import uk.ac.ebi.eva.commons.core.models.ws.VariantWithSamplesAndAnnotation;
import uk.ac.ebi.eva.commons.mongodb.services.AnnotationMetadataNotFoundException;
import uk.ac.ebi.eva.commons.mongodb.services.VariantWithSamplesAndAnnotationsService;
import uk.ac.ebi.eva.lib.eva_utils.DBAdaptorConnector;
import uk.ac.ebi.eva.lib.eva_utils.MultiMongoDbFactory;
import uk.ac.ebi.eva.server.ws.contigalias.ContigAliasService;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
Expand All @@ -55,16 +57,21 @@ public class IdentifierWSServerV2 {
@Autowired
private VariantWithSamplesAndAnnotationsService service;

@Autowired
private ContigAliasService contigAliasService;

@GetMapping(value = "/{identifier}/variants")
public ResponseEntity getVariants(
@ApiParam(value = "RS or SS identifier of a variant, e.g.: rs55880202", required = true) @PathVariable
String identifier,
String identifier,
@ApiParam(value = "First letter of the genus, followed by the full species name, e.g. hsapiens. Allowed" +
" values can be looked up in /v1/meta/species/list/ in the field named 'taxonomyCode'.",
required = true) @RequestParam String species,
@ApiParam(value = "Encoded assembly name, e.g. grch37. Allowed values can be looked up in" +
" /v1/meta/species/list/ in the field named 'assemblyCode'.", required = true)
@RequestParam String assembly,
@ApiParam(value = "Contig naming convention desired, default is INSDC")
@RequestParam(name = "contigNamingConvention", required = false) ContigNamingConvention contigNamingConvention,
HttpServletResponse response)
throws AnnotationMetadataNotFoundException, IllegalArgumentException {
checkParameters(species, assembly);
Expand All @@ -76,7 +83,12 @@ public ResponseEntity getVariants(
List<Resource> resourcesList = new ArrayList<>();

variantEntities.forEach(variantEntity -> {
Variant variant = new Variant(variantEntity.getChromosome(), variantEntity.getStart(), variantEntity
String variantContig = variantEntity.getChromosome();
String translatedContig = contigAliasService.translateContigFromInsdc(variantEntity.getChromosome(), contigNamingConvention);
if (!translatedContig.isEmpty()) {
variantContig = translatedContig;
}
Variant variant = new Variant(variantContig, variantEntity.getStart(), variantEntity
.getEnd(), variantEntity.getReference(), variantEntity.getAlternate());
variant.setIds(variantEntity.getIds());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import uk.ac.ebi.eva.commons.core.models.AnnotationMetadata;
import uk.ac.ebi.eva.commons.core.models.Region;
import uk.ac.ebi.eva.commons.core.models.contigalias.ContigNamingConvention;
import uk.ac.ebi.eva.commons.core.models.ws.VariantWithSamplesAndAnnotation;
import uk.ac.ebi.eva.commons.mongodb.filter.FilterBuilder;
import uk.ac.ebi.eva.commons.mongodb.filter.VariantRepositoryFilter;
Expand All @@ -47,12 +48,14 @@
import uk.ac.ebi.eva.lib.utils.QueryResult;
import uk.ac.ebi.eva.server.RateLimit;
import uk.ac.ebi.eva.server.Utils;
import uk.ac.ebi.eva.server.ws.contigalias.ContigAliasService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/v1/segments", produces = "application/json")
Expand All @@ -62,6 +65,9 @@ public class RegionWSServer extends EvaWSServer {
@Autowired
private VariantWithSamplesAndAnnotationsService service;

@Autowired
private ContigAliasService contigAliasService;

protected static Logger logger = LoggerFactory.getLogger(FeatureWSServer.class);

private static final int REGION_REQUEST_RATE_LIMIT = 5;
Expand All @@ -82,6 +88,7 @@ public QueryResponse getVariantsByRegion(@PathVariable("regionId") String region
@RequestParam(name = "exclude", required = false) List<String> exclude,
@RequestParam(name = "annot-vep-version", required = false) String annotationVepVersion,
@RequestParam(name = "annot-vep-cache-version", required = false) String annotationVepCacheVersion,
@RequestParam(name = "contigNamingConvention", required = false) ContigNamingConvention contigNamingConvention,
HttpServletResponse response,
@ApiIgnore HttpServletRequest request)
throws IOException {
Expand Down Expand Up @@ -136,7 +143,9 @@ public QueryResponse getVariantsByRegion(@PathVariable("regionId") String region

Long numTotalResults = service.countByRegionsAndComplexFilters(regions, filters);

QueryResult<VariantWithSamplesAndAnnotation> queryResult = buildQueryResult(variantEntities, numTotalResults);
QueryResult<VariantWithSamplesAndAnnotation> queryResult = buildQueryResult(
contigAliasService.getVariantsWithTranslatedContig(variantEntities, contigNamingConvention),
numTotalResults);
return setQueryResponse(queryResult);
}

Expand All @@ -148,6 +157,7 @@ public QueryResponse getVariantsByRegion() {
@RequestMapping(value = "", method = RequestMethod.GET)
@ResponseBody
public QueryResponse getChromosomes(@RequestParam(name = "species") String species,
@RequestParam(name = "contigNamingConvention", required = false) ContigNamingConvention contigNamingConvention,
HttpServletResponse response)
throws IOException {
if (species.isEmpty()) {
Expand All @@ -157,6 +167,18 @@ public QueryResponse getChromosomes(@RequestParam(name = "species") String speci

MultiMongoDbFactory.setDatabaseNameForCurrentThread(DBAdaptorConnector.getDBName(species));
List<String> chromosomeList = new ArrayList<>(service.findDistinctChromosomes());

if (contigNamingConvention != null && !contigNamingConvention.equals(ContigNamingConvention.NO_REPLACEMENT)
&& !contigNamingConvention.equals(ContigNamingConvention.INSDC)) {
chromosomeList = chromosomeList.stream().map(chromosome -> {
String translatedChromosome = contigAliasService.translateContigFromInsdc(chromosome, contigNamingConvention);
if (translatedChromosome.equals("")) {
return chromosome;
} else {
return translatedChromosome;
}
}).collect(Collectors.toList());
}
QueryResult<String> queryResult = buildQueryResult(chromosomeList);
return setQueryResponse(queryResult);
}
Expand Down
Loading

0 comments on commit fb41659

Please sign in to comment.