Skip to content

Commit

Permalink
include count, skip and limit only in /search endpoints. #522
Browse files Browse the repository at this point in the history
  • Loading branch information
julie-sullivan committed Jan 27, 2020
1 parent 9570d06 commit 8ba42fc
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,9 @@ public class GenericRestWSServer implements IWSServer {
@ApiParam(name = "include", value = "Set which fields are included in the response, e.g.: transcripts.id. ")
protected String include;

@DefaultValue("10")
@QueryParam("limit")
@ApiParam(name = "limit", value = "Max number of results to be returned. Cannot exceed 5,000.")
protected int limit;

@DefaultValue("0")
@QueryParam("skip")
@ApiParam(name = "skip", value = "Number of results to be skipped. No skip applied when 0.")
protected int skip;

@DefaultValue("false")
@QueryParam("count")
@ApiParam(name = "count", value = "Get a count of the number of results obtained. Deactivated by default.",
defaultValue = "false", allowableValues = "false,true")
protected String count;

@DefaultValue("")
@QueryParam("sort")
@ApiParam(name = "sort", value = "Sort returned results by a certain data model attribute.", defaultValue = "ASC",
allowableValues = "ASC,DESC")
@ApiParam(name = "sort", value = "Sort returned results by a certain data model attribute.")
protected String sort;

@DefaultValue("json")
Expand Down Expand Up @@ -131,8 +114,9 @@ public class GenericRestWSServer implements IWSServer {
protected DBAdaptorFactory dbAdaptorFactory;
protected Monitor monitor;

private static final int SKIP_DEFAULT = 0;
private static final int LIMIT_DEFAULT = 10;
private static final int LIMIT_MAX = 5000;
private static final int MAX_RECORDS = 5000;
private static final String ERROR = "error";
private static final String OK = "ok";
// this webservice has no species, do not validate
Expand Down Expand Up @@ -264,20 +248,6 @@ public void parseQueryParams() throws CellbaseException {
queryOptions.put(QueryOptions.SORT, sort);
}

if (limit < 0) {
throw new CellbaseException("Limit is not valid. Expected a number greater than zero but was " + limit);
} else if (limit > LIMIT_MAX) {
throw new CellbaseException("Limit is not valid. Expected a number less than " + LIMIT_MAX + " but was " + limit);
}

if (skip < 0) {
throw new CellbaseException("Skip is not valid. Expected a number greater than zero but was " + skip);
}

queryOptions.put(QueryOptions.LIMIT, limit);
queryOptions.put(QueryOptions.SKIP, skip);
queryOptions.put(QueryOptions.COUNT, StringUtils.isNotBlank(count) && Boolean.parseBoolean(count));

// Add all the others QueryParams from the URL
for (Map.Entry<String, List<String>> entry : multivaluedMap.entrySet()) {
if (!queryOptions.containsKey(entry.getKey())) {
Expand All @@ -286,6 +256,31 @@ public void parseQueryParams() throws CellbaseException {
}
}

public void parseExtraQueryParams(Integer limit, Integer skip) throws CellbaseException {
if (limit != null) {
if (limit > MAX_RECORDS) {
throw new CellbaseException("Limit cannot exceed " + MAX_RECORDS + " but was " + limit);
}
if (limit < 0) {
throw new CellbaseException("Limit must be >= 0, but was " + limit);
}
queryOptions.put(QueryOptions.LIMIT, limit);
} else {
queryOptions.put(QueryOptions.LIMIT, LIMIT_DEFAULT);
}
if (skip != null) {
if (skip > MAX_RECORDS) {
throw new CellbaseException("Skip cannot exceed " + MAX_RECORDS + " but was " + skip);
}
if (skip < 0) {
throw new CellbaseException("Skip must be >= 0, but was " + skip);
}
queryOptions.put(QueryOptions.SKIP, skip);
} else {
queryOptions.put(QueryOptions.SKIP, SKIP_DEFAULT);
}
}

protected void logQuery(String status) {
try {
logger.info("{}\t{}\t{}\t{}\t{}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public SpeciesWSServer(@PathParam("version")
+ "of potentially available species ids, please refer to: "
+ "https://bioinfo.hpc.cam.ac.uk/cellbase/webservices/rest/v4/meta/species") String species,
@Context UriInfo uriInfo,
@Context HttpServletRequest hsr) throws VersionException, SpeciesException, IOException, CellbaseException {
@Context HttpServletRequest hsr) throws VersionException, SpeciesException, IOException,
CellbaseException {
super(version, species, uriInfo, hsr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
Expand All @@ -35,12 +36,10 @@
* Created by fjlopez on 06/12/16.
*/
@Path("/{version}/{species}/clinical")
@Produces("application/json")
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "Clinical", description = "Clinical RESTful Web Services API")
public class ClinicalWSServer extends GenericRestWSServer {



public ClinicalWSServer(@PathParam("version")
@ApiParam(name = "version", value = "Possible values: v4, v5",
defaultValue = "v5") String version,
Expand All @@ -58,6 +57,10 @@ public ClinicalWSServer(@PathParam("version")
@ApiOperation(httpMethod = "GET", notes = "No more than 1000 objects are allowed to be returned at a time. ",
value = "Retrieves all clinical variants", response = Variant.class, responseContainer = "QueryResponse")
@ApiImplicitParams({
@ApiImplicitParam(name = "count",
value = "Get a count of the number of results obtained.",
required = false, dataType = "boolean", paramType = "query", defaultValue = "false",
allowableValues = "false,true"),
@ApiImplicitParam(name = "source",
value = "Comma separated list of database sources of the documents to be returned. Possible values "
+ " are clinvar,cosmic or iarctp53. E.g.: clinvar,cosmic",
Expand Down Expand Up @@ -114,8 +117,12 @@ public ClinicalWSServer(@PathParam("version")
+ "https://bioinfo.hpc.cam.ac.uk/cellbase/webservices/rest/v4/hsapiens/clinical/variant/allele_origin_labels",
required = false, dataType = "java.util.List", paramType = "query")
})
public Response getAll() {
public Response getAll(@QueryParam("limit") @DefaultValue("10")
@ApiParam(value = "Max number of results to be returned. Cannot exceed 5,000.") Integer limit,
@QueryParam("skip") @DefaultValue("0")
@ApiParam(value = "Number of results to be skipped.") Integer skip) {
try {
parseExtraQueryParams(limit, skip);
parseQueryParams();
ClinicalDBAdaptor clinicalDBAdaptor = dbAdaptorFactory.getClinicalDBAdaptor(this.species, this.assembly);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.opencb.cellbase.server.rest.GenericRestWSServer;
import org.opencb.commons.datastore.core.Query;


import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
Expand Down Expand Up @@ -259,6 +258,10 @@ public Response groupBy(@DefaultValue("")
value = "Retrieves all gene objects", response = Gene.class,
responseContainer = "QueryResponse")
@ApiImplicitParams({
@ApiImplicitParam(name = "count",
value = "Get a count of the number of results obtained.",
required = false, dataType = "java.lang.Boolean", paramType = "query", defaultValue = "false",
allowableValues = "false,true"),
@ApiImplicitParam(name = "region",
value = "Comma separated list of genomic regions to be queried, e.g.: 1:6635137-6635325",
required = false, dataType = "java.util.List", paramType = "query"),
Expand Down Expand Up @@ -329,8 +332,12 @@ public Response groupBy(@DefaultValue("")
+ " Exact text matches will be returned",
required = false, dataType = "java.util.List", paramType = "query")
})
public Response getAll() {
public Response getAll(@QueryParam("limit") @DefaultValue("10")
@ApiParam(value = "Max number of results to be returned. Cannot exceed 5,000.") Integer limit,
@QueryParam("skip") @DefaultValue("0")
@ApiParam(value = "Number of results to be skipped.") Integer skip) {
try {
parseExtraQueryParams(limit, skip);
parseQueryParams();
GeneDBAdaptor geneDBAdaptor = dbAdaptorFactory.getGeneDBAdaptor(this.species, this.assembly);
return createOkResponse(geneDBAdaptor.nativeGet(query, queryOptions));
Expand Down Expand Up @@ -553,7 +560,8 @@ public Response getTranscriptsByGeneId(@PathParam("geneId")

@GET
@Path("/distinct/{fieldName}")
@ApiOperation(httpMethod = "GET", value = "Get a unique list of values for a given field, e.g. biotype")
@ApiOperation(httpMethod = "GET", notes = "Gets a unique list of values for specified gene, e.g. biotype or chromosome",
value = "Get a unique list of values for a given field.")
@ApiImplicitParams({
@ApiImplicitParam(name = "region",
value = "Comma separated list of genomic regions to be queried, e.g.: 1:6635137-6635325",
Expand All @@ -563,11 +571,11 @@ public Response getTranscriptsByGeneId(@PathParam("geneId")
+ "Exact text matches will be returned",
required = false, dataType = "java.util.List", paramType = "query"),
@ApiImplicitParam(name = "name",
value = "Comma separated list of gene HGNC names, e.g.: BRCA2,TTN,MUC4"
value = "Comma separated list of gene HGNC names, e.g.: BRCA2,TTN,MUC4. "
+ "Exact text matches will be returned",
required = false, dataType = "java.util.List", paramType = "query"),
@ApiImplicitParam(name = "transcripts.xrefs",
value = "Comma separated list transcript xrefs ids, e.g.: ENSG00000145113,35912_at,GO:0002020."
value = "Comma separated list transcript xrefs ids, e.g.: ENSG00000145113,35912_at,GO:0002020. "
+ "Exact text matches will be returned",
required = false, dataType = "java.util.List", paramType = "query"),
@ApiImplicitParam(name = "transcripts.id",
Expand Down Expand Up @@ -829,7 +837,8 @@ public Response getProteinById(@PathParam("geneId")

@GET
@Path("/{geneId}/ppi")
@ApiOperation(httpMethod = "GET", value = "Get the protein-protein interactions in which this gene is involved")
@ApiOperation(httpMethod = "GET", value = "Get the protein-protein interactions in which this gene is involved",
hidden = true)
public Response getPPIByEnsemblId(@PathParam("geneId") String gene) {
try {
parseQueryParams();
Expand All @@ -852,7 +861,7 @@ public Response getPPIByEnsemblId(@PathParam("geneId") String gene) {
+ "a look at "
+ "https://github.com/opencb/cellbase/wiki/MongoDB-implementation#clinical for further details.",
value = "[DEPRECATED] Use {version}/{species}/clinical/variant/search instead.", response = Document.class,
responseContainer = "QueryResponse")
responseContainer = "QueryResponse", hidden = true)
@ApiImplicitParams({
@ApiImplicitParam(name = "source",
value = "Comma separated list of database sources of the documents to be returned. Possible values "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;


import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
Expand Down Expand Up @@ -103,6 +99,10 @@ public Response getInfoByEnsemblId(@PathParam("proteinId")
@ApiOperation(httpMethod = "GET", notes = "No more than 1000 objects are allowed to be returned at a time.",
value = "Get all proteins", response = Entry.class, responseContainer = "QueryResponse")
@ApiImplicitParams({
@ApiImplicitParam(name = "count",
value = "Get a count of the number of results obtained. ",
required = false, dataType = "boolean", paramType = "query", defaultValue = "false",
allowableValues = "false,true"),
@ApiImplicitParam(name = "accession",
value = "Comma separated list of UniProt accession ids, e.g.: Q9UL59,B2R8Q1,Q9UKT9."
+ "Exact text matches will be returned",
Expand All @@ -124,8 +124,12 @@ public Response getInfoByEnsemblId(@PathParam("proteinId")
+ "Transcription,Zinc. Exact text matches will be returned",
required = false, dataType = "java.util.List", paramType = "query")
})
public Response getAll() {
public Response getAll(@QueryParam("limit") @DefaultValue("10")
@ApiParam(value = "Max number of results to be returned. Cannot exceed 5,000.") Integer limit,
@QueryParam("skip") @DefaultValue("0")
@ApiParam(value = "Number of results to be skipped.") Integer skip) {
try {
parseExtraQueryParams(limit, skip);
parseQueryParams();
ProteinDBAdaptor proteinDBAdaptor = dbAdaptorFactory.getProteinDBAdaptor(this.species, this.assembly);
return createOkResponse(proteinDBAdaptor.nativeGet(query, queryOptions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ public Response getGeneById(@PathParam("transcriptId")
value = "Retrieves all transcript objects", response = Transcript.class,
responseContainer = "QueryResponse")
@ApiImplicitParams({
@ApiImplicitParam(name = "count",
value = "Get a count of the number of results obtained.",
required = false, dataType = "boolean", paramType = "query", defaultValue = "false",
allowableValues = "false,true"),
@ApiImplicitParam(name = "region",
value = "Comma separated list of genomic regions to be queried, e.g.: 1:6635137-6635325",
dataType = "java.util.List", paramType = "query"),
Expand All @@ -229,14 +233,19 @@ public Response getGeneById(@PathParam("transcriptId")
+ "within the gene model, e.g.: basic,CCDS. Exact text matches will be returned",
dataType = "string", paramType = "query"),
})
public Response getAll() {
public Response getAll(@QueryParam("limit") @DefaultValue("10")
@ApiParam(value = "Max number of results to be returned. Cannot exceed 5,000.") Integer limit,
@QueryParam("skip") @DefaultValue("0")
@ApiParam(value = "Number of results to be skipped.") Integer skip) {
try {
parseExtraQueryParams(limit, skip);
parseQueryParams();
TranscriptDBAdaptor transcriptDBAdaptor = dbAdaptorFactory.getTranscriptDBAdaptor(this.species, this.assembly);
CellBaseDataResult queryResult = transcriptDBAdaptor.nativeGet(query, queryOptions);
// Total number of results is always same as the number of results. As this is misleading, we set it as -1 until
// properly fixed
queryResult.setNumTotalResults(-1);
queryResult.setNumResults(-1);
return createOkResponse(queryResult);
} catch (Exception e) {
return createErrorResponse(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package org.opencb.cellbase.server.rest.genomic;

import com.google.common.base.Splitter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.*;
import org.opencb.biodata.models.core.Chromosome;
import org.opencb.cellbase.core.api.GenomeDBAdaptor;
import org.opencb.cellbase.core.exception.CellbaseException;
Expand All @@ -30,10 +28,7 @@
import org.opencb.commons.datastore.core.QueryOptions;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
Expand Down Expand Up @@ -75,8 +70,18 @@ public Response getModel() {
@Path("/search")
@ApiOperation(httpMethod = "GET", value = "Retrieves all the chromosome objects", response = Chromosome.class,
responseContainer = "QueryResponse")
public Response getChromosomesAll() {
@ApiImplicitParams({
@ApiImplicitParam(name = "count",
value = "Get a count of the number of results obtained. Deactivated by default.",
required = false, dataType = "boolean", paramType = "query", defaultValue = "false",
allowableValues = "false,true")
})
public Response getChromosomesAll(@QueryParam("limit") @DefaultValue("10")
@ApiParam(value = "Max number of results to be returned. Cannot exceed 5,000.") Integer limit,
@QueryParam("skip") @DefaultValue("0")
@ApiParam(value = "Number of results to be skipped.") Integer skip) {
try {
parseExtraQueryParams(limit, skip);
parseQueryParams();
GenomeDBAdaptor dbAdaptor = dbAdaptorFactory.getGenomeDBAdaptor(this.species, this.assembly);
return createOkResponse(dbAdaptor.getGenomeInfo(queryOptions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ public Response getByEnsemblId(@PathParam("id")
@ApiOperation(httpMethod = "GET", notes = "No more than 1000 objects are allowed to be returned at a time.",
value = "Retrieves all variation objects", response = Variant.class, responseContainer = "QueryResponse")
@ApiImplicitParams({
@ApiImplicitParam(name = "count",
value = "Get a count of the number of results obtained.",
required = false, dataType = "boolean", paramType = "query", defaultValue = "false",
allowableValues = "false,true"),
@ApiImplicitParam(name = "region",
value = "Comma separated list of genomic regions to be queried, e.g.: 1:6635137-6635325",
dataType = "java.util.List", paramType = "query"),
Expand All @@ -433,8 +437,12 @@ public Response getByEnsemblId(@PathParam("id")
value = "Comma separated list of possible alternate to be queried, e.g.: A,T",
dataType = "java.util.List", paramType = "query")
})
public Response search() {
public Response search(@QueryParam("limit") @DefaultValue("10")
@ApiParam(value = "Max number of results to be returned. Cannot exceed 5,000.") Integer limit,
@QueryParam("skip") @DefaultValue("0")
@ApiParam(value = "Number of results to be skipped.") Integer skip) {
try {
parseExtraQueryParams(limit, skip);
parseQueryParams();
VariantDBAdaptor variationDBAdaptor = dbAdaptorFactory.getVariationDBAdaptor(this.species, this.assembly);
return createOkResponse(variationDBAdaptor.nativeGet(query, queryOptions));
Expand Down
Loading

0 comments on commit 8ba42fc

Please sign in to comment.