Skip to content

Commit

Permalink
more replacements #522
Browse files Browse the repository at this point in the history
  • Loading branch information
julie-sullivan committed Feb 3, 2020
1 parent 4a2a1b5 commit 44de29b
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,11 @@ public SpeciesWSServer(@PathParam("version")
value = "Retrieves info about current species chromosomes.", response = Chromosome.class,
responseContainer = "QueryResponse")
public Response getSpeciesInfo(@QueryParam("exclude")
@ApiParam(value = "Set which fields are excluded in the response, "
+ "e.g.: transcripts.exons.") String exclude,
@ApiParam(value = ParamConstants.EXCLUDE_DESCRIPTION) String exclude,
@QueryParam("include")
@ApiParam(value = "Set which fields are include in the response, "
+ "e.g.: transcripts.exons.") String include,
@ApiParam(value = ParamConstants.INCLUDE_DESCRIPTION) String include,
@QueryParam("sort")
@ApiParam(value = "Sort returned results by a certain data model attribute.")
String sort) {
@ApiParam(value = ParamConstants.SORT_DESCRIPTION) String sort) {
try {
parseIncludesAndExcludes(exclude, include, sort);
parseQueryParams();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* Copyright 2015-2020 OpenCB
*
* 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 org.opencb.cellbase.server.rest.feature;

import com.google.common.base.Splitter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.bson.Document;
import org.opencb.biodata.models.core.Xref;
import org.opencb.cellbase.core.ParamConstants;
import org.opencb.cellbase.core.api.GeneDBAdaptor;
import org.opencb.cellbase.core.api.XRefDBAdaptor;
import org.opencb.cellbase.core.exception.CellbaseException;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.cellbase.server.exception.SpeciesException;
import org.opencb.cellbase.server.exception.VersionException;
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;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author imedina
*/
@Path("/{version}/{species}/feature/id")
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "Xref", description = "External References RESTful Web Services API")
public class IdWSServer extends GenericRestWSServer {

public IdWSServer(@PathParam("version")
@ApiParam(name = "version", value = ParamConstants.VERSION_DESCRIPTION,
defaultValue = ParamConstants.DEFAULT_VERSION) String version,
@PathParam("species")
@ApiParam(name = "species", value = ParamConstants.SPECIES_DESCRIPTION) String species,
@Context UriInfo uriInfo, @Context HttpServletRequest hsr) throws VersionException,
SpeciesException, IOException, CellbaseException {
super(version, species, uriInfo, hsr);
}

@GET
@Path("/model")
@ApiOperation(httpMethod = "GET", value = ParamConstants.DATA_MODEL_DESCRIPTION, response = Map.class,
responseContainer = "QueryResponse")
public Response getModel() {
return createModelResponse(Xref.class);
}

@GET
@Path("/{id}/info")
@ApiOperation(httpMethod = "GET", value = "Retrieves the external reference(s) info for the ID(s)",
notes = "An independent database query will be issued for each id, meaning that results for each id will be"
+ " returned in independent CellBaseDataResult objects within the QueryResponse object.", response = Xref.class,
responseContainer = "QueryResponse")
public Response getByFeatureIdInfo(@PathParam("id")
@ApiParam(name = "id", value = "Comma separated list of ids, e.g.: BRCA2. Exact "
+ "text matches will be returned.", required = true) String id,
@QueryParam("exclude")
@ApiParam(value = ParamConstants.EXCLUDE_DESCRIPTION) String exclude,
@QueryParam("include")
@ApiParam(value = ParamConstants.INCLUDE_DESCRIPTION) String include,
@QueryParam("sort")
@ApiParam(value = ParamConstants.SORT_DESCRIPTION) String sort) {
try {
parseIncludesAndExcludes(exclude, include, sort);
parseQueryParams();
XRefDBAdaptor xRefDBAdaptor = dbAdaptorFactory.getXRefDBAdaptor(this.species, this.assembly);

List<String> list = Splitter.on(",").splitToList(id);
List<Query> queries = createQueries(id, XRefDBAdaptor.QueryParams.ID.key());

List<CellBaseDataResult<Document>> dbNameList = xRefDBAdaptor.nativeGet(queries, queryOptions);
for (int i = 0; i < dbNameList.size(); i++) {
dbNameList.get(i).setId(list.get(i));
for (Document document : dbNameList.get(i).getResults()) {
if (document.get("id").equals(list.get(i))) {
List<Document> objectList = new ArrayList<>(1);
objectList.add(document);
dbNameList.get(i).setResults(objectList);
break;
}
}
}
return createOkResponse(dbNameList);
} catch (Exception e) {
return createErrorResponse(e);
}
}

@GET
@Path("/{id}/xref")
@ApiOperation(httpMethod = "GET", value = "Retrieves all the external references related with given ID(s)",
response = Xref.class, responseContainer = "QueryResponse")
public Response getAllXrefsByFeatureId(@PathParam("id")
@ApiParam(name = "id", value = "Comma separated list of ids, e.g.: BRCA2."
+ "Exact text matches will be searched.", required = true) String ids,
@DefaultValue("")
@QueryParam("dbname")
@ApiParam(name = "dbname", value = "Comma separated list of source DB names"
+ " to include in the search, e.g.: ensembl_gene,vega_gene,havana_gene."
+ " Available db names are shown by this web service: "
+ " https://bioinfo.hpc.cam.ac.uk/cellbase/webservices/#!/Xref/"
+ "getDBNames", required = false) String dbname) {
try {
parseQueryParams();
XRefDBAdaptor xRefDBAdaptor = dbAdaptorFactory.getXRefDBAdaptor(this.species, this.assembly);

Query query = new Query();
query.put(XRefDBAdaptor.QueryParams.ID.key(), ids);
if (dbname != null && !dbname.isEmpty()) {
query.put(XRefDBAdaptor.QueryParams.DBNAME.key(), dbname);
}
CellBaseDataResult queryResult = xRefDBAdaptor.nativeGet(query, queryOptions);
queryResult.setId(ids);
return createOkResponse(queryResult);
} catch (Exception e) {
return createErrorResponse(e);
}
}

@GET
@Path("/{id}/startsWith")
@ApiOperation(httpMethod = "GET", value = "Get the gene HGNC symbols of genes for which there is an Xref id that "
+ "matches the beginning of the given string", response = Map.class, responseContainer = "QueryResponse")
public Response getByLikeQuery(@PathParam("id")
@ApiParam(name = "id", value = "One single string to be matched at the beginning of"
+ " the Xref id", required = true) String id) {
try {
parseQueryParams();
XRefDBAdaptor x = dbAdaptorFactory.getXRefDBAdaptor(this.species, this.assembly);
CellBaseDataResult queryResult = x.startsWith(id, queryOptions);
queryResult.setId(id);
return createOkResponse(queryResult);
} catch (Exception e) {
return createErrorResponse(e);
}
}

@GET
@Path("/{id}/contains")
@ApiOperation(httpMethod = "GET", value = "Get gene HGNC symbols for which there is an Xref id containing the given "
+ "string", response = Map.class, responseContainer = "QueryResponse")
public Response getByContainsQuery(@PathParam("id")
@ApiParam(name = "id", value = "Comma separated list of strings to "
+ "be contained within the xref id, e.g.: BRCA2", required = true) String id) {
try {
parseQueryParams();
XRefDBAdaptor xRefDBAdaptor = dbAdaptorFactory.getXRefDBAdaptor(this.species, this.assembly);
CellBaseDataResult xrefs = xRefDBAdaptor.contains(id, queryOptions);
xrefs.setId(id);
return createOkResponse(xrefs);
} catch (Exception e) {
return createErrorResponse(e);
}
}

@GET
@Path("/{id}/gene")
@ApiOperation(httpMethod = "GET", value = "Get the gene(s) for the given ID(s)", notes = "An independent"
+ " database query will be issued for each id, meaning that results for each id will be"
+ " returned in independent CellBaseDataResult objects within the QueryResponse object.",
responseContainer = "QueryResponse")
public Response getGeneByEnsemblId(@PathParam("id")
@ApiParam(name = "id", value = "Comma separated list of ids to look"
+ " for within gene xrefs, e.g.: BRCA2", required = true) String id) {
try {
parseQueryParams();
GeneDBAdaptor geneDBAdaptor = dbAdaptorFactory.getGeneDBAdaptor(this.species, this.assembly);

String[] ids = id.split(",");
List<Query> queries = new ArrayList<>(ids.length);
for (String s : ids) {
queries.add(new Query(GeneDBAdaptor.QueryParams.XREFS.key(), s));
}
List<CellBaseDataResult> queryResults = geneDBAdaptor.nativeGet(queries, queryOptions);
for (int i = 0; i < ids.length; i++) {
queryResults.get(i).setId(ids[i]);
}
return createOkResponse(queryResults);
} catch (Exception e) {
return createErrorResponse(e);
}
}

@GET
@Path("/dbnames")
@ApiOperation(httpMethod = "GET", value = "Get list of distinct source DB names from which xref ids were collected ",
response = String.class, responseContainer = "QueryResponse")
public Response getDBNames() {
try {
parseQueryParams();
XRefDBAdaptor xRefDBAdaptor = dbAdaptorFactory.getXRefDBAdaptor(this.species, this.assembly);
CellBaseDataResult xrefs = xRefDBAdaptor.distinct(query, "transcripts.xrefs.dbName");
return createOkResponse(xrefs);
} catch (Exception e) {
return createErrorResponse(e);
}
}

@GET
@Path("/help")
public Response help() {
return createOkResponse("Usage:");
}

}
Loading

0 comments on commit 44de29b

Please sign in to comment.