Skip to content

Commit

Permalink
Several improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
imedina committed Mar 12, 2024
1 parent 56b3eb6 commit 82e57f3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public DownloadFile downloadDbSnp() throws IOException, InterruptedException {
return null;
}
if (speciesConfiguration.getScientificName().equals("Homo sapiens")) {
logger.info("Downloading dbSNP scores information ...");
logger.info("Downloading dbSNP information ...");

Path variation = downloadFolder.resolve(VARIATION_DATA);
Files.createDirectories(variation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ public List<CellBaseDataResult<OntologyTerm>> info(List<String> ids, ProjectionQ
return results;
}

@Override
public CellBaseDataResult<Long> count(OntologyQuery query) {
return null;
}

@Override
public CellBaseDataResult<String> distinct(OntologyQuery query) throws CellBaseException {
Bson bsonDocument = parseQuery(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ public List<CellBaseDataResult<PubmedArticle>> info(List<String> ids, Projection
return results;
}

@Override
public CellBaseDataResult<Long> count(PublicationQuery query) {
return null;
}

@Override
public CellBaseDataResult<String> distinct(PublicationQuery query) throws CellBaseException {
Bson bsonDocument = parseQuery(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.opencb.cellbase.lib.impl.core;

import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.opencb.biodata.models.core.Snp;
Expand All @@ -36,6 +37,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static org.opencb.cellbase.core.ParamConstants.API_KEY_PARAM;
import static org.opencb.cellbase.core.ParamConstants.DATA_RELEASE_PARAM;
Expand Down Expand Up @@ -72,8 +74,8 @@ public CellBaseIterator<Snp> iterator(SnpQuery query) throws CellBaseException {
}

@Override
public List<CellBaseDataResult<Snp>> info(List<String> ids, ProjectionQueryOptions queryOptions, int dataRelease,
String apiKey) throws CellBaseException {
public List<CellBaseDataResult<Snp>> info(List<String> ids, ProjectionQueryOptions queryOptions, int dataRelease, String apiKey)
throws CellBaseException {
List<CellBaseDataResult<Snp>> results = new ArrayList<>();
Bson projection = getProjection(queryOptions);
MongoDBCollection mongoDBCollection = getCollectionByRelease(mongoDBCollectionByRelease, dataRelease);
Expand All @@ -86,14 +88,12 @@ public List<CellBaseDataResult<Snp>> info(List<String> ids, ProjectionQueryOptio
return results;
}

@Override
public CellBaseDataResult<Long> count(SnpQuery query) {
return null;
}

@Override
public CellBaseDataResult<String> distinct(SnpQuery query) throws CellBaseException {
return null;
Bson bsonQuery = parseQuery(query);
logger.info("snpQuery distinct: {}", bsonQuery.toBsonDocument().toJson());
MongoDBCollection mongoDBCollection = getCollectionByRelease(mongoDBCollectionByRelease, query.getDataRelease());
return new CellBaseDataResult<>(mongoDBCollection.distinct(query.getFacet(), bsonQuery, String.class));
}

@Override
Expand All @@ -103,7 +103,27 @@ public CellBaseDataResult<Snp> aggregationStats(SnpQuery query) {

@Override
public CellBaseDataResult groupBy(SnpQuery query) throws CellBaseException {
return null;
Bson bsonQuery = parseQuery(query);
logger.info("snpQuery groupBy: {}", bsonQuery.toBsonDocument().toJson());
MongoDBCollection mongoDBCollection = getCollectionByRelease(mongoDBCollectionByRelease, query.getDataRelease());
return groupBy(bsonQuery, query, "name", mongoDBCollection);
}

public CellBaseDataResult<Snp> startsWith(String id, QueryOptions options, int dataRelease) throws CellBaseException {
Bson regex = Filters.regex("id", Pattern.compile("^" + id));
Bson projection;
if (options.containsKey(QueryOptions.INCLUDE)) {
projection = Projections.include(options.getAsStringList(QueryOptions.INCLUDE));
} else {
if (options.containsKey(QueryOptions.EXCLUDE)) {
projection = Projections.exclude(options.getAsStringList(QueryOptions.EXCLUDE));
} else {
projection = Projections.exclude("annotation");
}
}

MongoDBCollection mongoDBCollection = getCollectionByRelease(mongoDBCollectionByRelease, dataRelease);
return new CellBaseDataResult<>(mongoDBCollection.find(regex, projection, CONVERTER, options));
}

public Bson parseQuery(SnpQuery query) {
Expand Down Expand Up @@ -132,9 +152,8 @@ public Bson parseQuery(SnpQuery query) {
e.printStackTrace();
}

logger.info("SNP parsed query: " + andBsonList);
if (andBsonList.size() > 0) {
System.out.println("SnpMongoDBAdaptor, parse query = " + andBsonList);
logger.info("SnpMongoDBAdaptor parsed query: {}", andBsonList);
if (!andBsonList.isEmpty()) {
return Filters.and(andBsonList);
} else {
return new Document();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
Expand Down Expand Up @@ -247,7 +248,7 @@ private Bson parseQuery(Query query) throws CellBaseException {
"annotation.consequenceTypes.sequenceOntologyTerms.name", andBsonList);
createGeneOrQuery(query, ParamConstants.QueryParams.GENE.key(), andBsonList);

if (andBsonList.size() > 0) {
if (!andBsonList.isEmpty()) {
return Filters.and(andBsonList);
} else {
return new Document();
Expand All @@ -262,6 +263,7 @@ public Bson parseQuery(VariantQuery query) throws CellBaseException {
Object value = entry.getValue();
switch (dotNotationName) {
case "id":
// Both variant IDs and dbSNP IDs are allowed
List<String> variantIds = getVariantIds(Arrays.asList(query.getId().split(",")), query.getDataRelease());
createAndOrQuery(variantIds, dotNotationName, QueryParam.Type.STRING, andBsonList);
break;
Expand Down Expand Up @@ -302,8 +304,8 @@ public Bson parseQuery(VariantQuery query) throws CellBaseException {
throw new CellBaseException(e.getMessage());
}

logger.debug("variant parsed query: " + andBsonList.toString());
if (andBsonList.size() > 0) {
logger.debug("variant parsed query: {}", andBsonList);
if (!andBsonList.isEmpty()) {
return Filters.and(andBsonList);
} else {
return new Document();
Expand Down Expand Up @@ -585,21 +587,11 @@ public CellBaseDataResult<Score> getFunctionalScoreVariant(Variant variant, Quer
if (position >= chunkStart && position <= chunkEnd) {
int offset = (position - chunkStart);
ArrayList basicDBList = dbObject.get("values", ArrayList.class);

// long l1 = 0L; // TODO: delete
// try { // TODO: delete
long l1 = Long.parseLong(basicDBList.get(offset).toString());
// l1 = (Long) basicDBList.get(offset);
// } catch (Exception e) { // TODO: delete
// logger.error("problematic variant: {}", variant.toString());
// throw e;
// }

if (dbObject.getString("source").equalsIgnoreCase("cadd_raw")) {
float value = 0f;
switch (alternate.toLowerCase()) {
case "a":
// value = ((short) (l1 >> 48) - 10000) / DECIMAL_RESOLUTION;
value = (((short) (l1 >> 48)) / DECIMAL_RESOLUTION) - 10;
break;
case "c":
Expand All @@ -618,7 +610,6 @@ public CellBaseDataResult<Score> getFunctionalScoreVariant(Variant variant, Quer
.setScore(value)
.setSource(dbObject.getString("source"))
.setDescription(null)
// .setDescription("")
.build());
}

Expand Down Expand Up @@ -795,28 +786,32 @@ public CellBaseDataResult<GenomicScoreRegion> getFunctionalScoreRegion(List<Stri
}

private List<String> getVariantIds(List<String> ids, int dataRelease) throws CellBaseException {
List<String> variantIds = new ArrayList<>();
List<String> variantIds = new ArrayList<>(ids.size());
List<String> snpIds = new ArrayList<>();
// Split dbSNP IDs and variant IDs
for (String id : ids) {
if (id.startsWith("rs")) {
snpIds.add(id);
} else {
variantIds.add(id);
}
}

// Get the variant ID for the dbSNP ID
if (CollectionUtils.isNotEmpty(snpIds)) {
// 1. Prepare the query
List<Bson> orBsonList = new ArrayList<>();
for (String snpId : snpIds) {
orBsonList.add(Filters.eq("id", snpId));
}
Bson query = Filters.or(orBsonList);

// 2. We must exclude as much information as possible to improve performance
MongoDBCollection mongoDBCollection = getCollectionByRelease(snpDBCollectionByRelease, dataRelease);
DataResult<Snp> snpDataResult = mongoDBCollection.find(query, null, Snp.class, new QueryOptions());
DataResult<Snp> snpDataResult = mongoDBCollection.find(query, Projections.exclude("annotation"), Snp.class, new QueryOptions());

// 3. Build the variant IDs
Set<String> results = new HashSet<>();

// Build the variant IDs
if (snpDataResult.getNumResults() > 0) {
for (Snp snp : snpDataResult.getResults()) {
for (String allele : snp.getAlleles()) {
Expand All @@ -825,7 +820,7 @@ private List<String> getVariantIds(List<String> ids, int dataRelease) throws Cel
}
}

// Add new variant IDs, if necessary
// 4. Add new variant IDs, if necessary
if (CollectionUtils.isNotEmpty(results)) {
variantIds.addAll(results);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public VariantWSServer(@PathParam("apiVersion") @ApiParam(name = "apiVersion", v
defaultValue = DEFAULT_VERSION) String apiVersion,
@PathParam("species") @ApiParam(name = "species", value = SPECIES_DESCRIPTION) String species,
@ApiParam(name = "assembly", value = ASSEMBLY_DESCRIPTION) @DefaultValue("") @QueryParam("assembly")
String assembly,
String assembly,
@ApiParam(name = "dataRelease", value = DATA_RELEASE_DESCRIPTION) @DefaultValue("0") @QueryParam("dataRelease")
int dataRelease,
int dataRelease,
@ApiParam(name = "apiKey", value = API_KEY_DESCRIPTION) @DefaultValue("") @QueryParam("apiKey") String apiKey,
@Context UriInfo uriInfo, @Context HttpServletRequest hsr)
throws CellBaseServerException {
Expand Down Expand Up @@ -119,20 +119,6 @@ public Response getNormalization(@PathParam("variants") @ApiParam(name = "varian

}

// @GET
// @Path("/{phenotype}/phenotype")
// @ApiOperation(httpMethod = "GET",
// value = "Not implemented yet",
// response = CellBaseDataResponse.class, hidden = true)
// public Response getVariantsByPhenotype(@PathParam("phenotype") String phenotype) {
// try {
// parseQueryParams();
// return Response.ok("Not implemented").build();
// } catch (Exception e) {
// return createErrorResponse(e);
// }
// }

@POST
@Consumes("text/plain")
@Path("/annotation")
Expand Down Expand Up @@ -291,7 +277,7 @@ public Response getAnnotationByVariantsGET(@PathParam("variants")
@QueryParam("checkAminoAcidChange")
@ApiParam(name = "checkAminoAcidChange", value = "<DESCRIPTION GOES HERE>",
allowableValues = "false,true", defaultValue = "false", required = false)
Boolean checkAminoAcidChange,
Boolean checkAminoAcidChange,
@QueryParam("consequenceTypeSource")
@ApiParam(name = "consequenceTypeSource", value = "Gene set, either ensembl (default) "
+ "or refseq", allowableValues = "ensembl,refseq", allowMultiple = true,
Expand Down Expand Up @@ -360,29 +346,6 @@ private Response getAnnotationByVariant(String variants,
}
}

// @GET
// @Deprecated
// @Path("/{variants}/cadd")
// @ApiOperation(httpMethod = "GET", value = "Get CADD scores for a (list of) variant(s)", response = Score.class,
// responseContainer = "QueryResponse", hidden = true)
// public Response getCaddScoreByVariant(@PathParam("variants")
// @ApiParam(name = "variants", value = "Comma separated list of variants for"
// + "which CADD socores will be returned, e.g. "
// + "19:45411941:T:C,14:38679764:-:GATCTG,1:6635210:G:-,"
// + "2:114340663:GCTGGGCATCCT:ACTGGGCATCCT",
// required = true) String variants) {
// try {
// parseQueryParams();
// VariantDBAdaptor variantDBAdaptor = dbAdaptorFactory.getVariationDBAdaptor(this.species, this.assembly);
//
// List<CellBaseDataResult<Score>> functionalScoreVariant =
// variantDBAdaptor.getFunctionalScoreVariant(Variant.parseVariants(variants), queryOptions);
// return createOkResponse(functionalScoreVariant);
// } catch (Exception e) {
// return createErrorResponse(e);
// }
// }

// @GET
// @Path("/stats")
// @Override
Expand Down Expand Up @@ -497,9 +460,8 @@ public Response getAllConsequenceTypes() {

@GET
@Path("/snp")
@ApiOperation(httpMethod = "GET", value = "Get SNPs",
response = Snp.class, responseContainer = "QueryResponse")
@ApiImplicitParams({
@ApiOperation(httpMethod = "GET", value = "Get SNPs", response = Snp.class, responseContainer = "QueryResponse")
@ApiImplicitParams({
@ApiImplicitParam(name = "exclude", value = EXCLUDE_DESCRIPTION,
required = false, dataType = "java.util.List", paramType = "query"),
@ApiImplicitParam(name = "include", value = INCLUDE_DESCRIPTION,
Expand All @@ -510,15 +472,14 @@ public Response getAllConsequenceTypes() {
required = false, dataType = "java.util.List", paramType = "query",
defaultValue = "", allowableValues="ASCENDING,DESCENDING"),
@ApiImplicitParam(name = "limit", value = LIMIT_DESCRIPTION,
required = false, defaultValue = DEFAULT_LIMIT, dataType = "java.util.List",
paramType = "query"),
required = false, defaultValue = DEFAULT_LIMIT, dataType = "java.util.List", paramType = "query"),
@ApiImplicitParam(name = "skip", value = SKIP_DESCRIPTION,
required = false, defaultValue = DEFAULT_SKIP, dataType = "java.util.List",
paramType = "query")
required = false, defaultValue = DEFAULT_SKIP, dataType = "java.util.List", paramType = "query")
})
public Response getSnps(@QueryParam("id") @ApiParam(name = "id", value = "ID") String id,
@QueryParam("chromosome") @ApiParam(name = "chromosome", value = "Chromsome") String chromosome,
@QueryParam("position") @ApiParam(name = "position", value = "Position") Integer position) {
@QueryParam("chromosome") @ApiParam(name = "chromosome", value = "Chromosome") String chromosome,
@QueryParam("position") @ApiParam(name = "position", value = "Position") Integer position,
@QueryParam("reference") @ApiParam(name = "reference", value = "Reference") String reference) {
try {
SnpQuery query = new SnpQuery(uriParams);
CellBaseDataResult<Snp> queryResult = variantManager.getSnps(query);
Expand All @@ -528,58 +489,4 @@ public Response getSnps(@QueryParam("id") @ApiParam(name = "id", value = "ID") S
}
}

// FIXME: 29/04/16 GET and POST web services to be fixed
// @GET
// @Path("/{variants}/consequenceType")
// @ApiOperation(httpMethod = "GET", value = "Get the biological impact of the variant(s)", response = String.class,
// responseContainer = "QueryResponse")
// public Response getConsequenceTypeByGetMethod(@PathParam("variants") String variants) {
// return getConsequenceType(variants);
// }
//
// private Response getConsequenceType(String variants) {
// try {
// parseQueryParams();
// VariantDBAdaptor variationDBAdaptor = dbAdaptorFactory.getVariationDBAdaptor(this.species, this.assembly);
// query.put(VariantDBAdaptor.QueryParams.ID.key(), variants);
// queryOptions.put(QueryOptions.INCLUDE, "annotation.displayConsequenceType");
// CellBaseDataResult<Variant> queryResult = variationDBAdaptor.get(query, queryOptions);
// CellBaseDataResult queryResult1 = new CellBaseDataResult<>(
// queryResult.getId(), queryResult.getTime(), queryResult.getEvents(), queryResult.getNumResults(),
// Collections.singletonList(queryResult.getResults().get(0).getAnnotation().getDisplayConsequenceType()), 1);
// return createOkResponse(queryResult1);
// } catch (Exception e) {
// return createErrorResponse("getConsequenceTypeByPostMethod", e.toString());
// }
// }

// FIXME: 29/04/16 GET and POST methods to be fixed
// @GET
// @Path("/{variants}/regulatory")
// @ApiOperation(httpMethod = "GET", value = "Get the regulatory impact of the variant(s)", hidden = true)
// public Response getRegulatoryByGetMethod(@PathParam("variants") String variants) {
// return getRegulatoryType(variants);
// }
//
// private Response getRegulatoryType(String variants) {
// try {
// parseQueryParams();
// VariantDBAdaptor variationDBAdaptor = dbAdaptorFactory.getVariationDBAdaptor(this.species, this.assembly);
// return null;
// } catch (Exception e) {
// return createErrorResponse(e);
// }
// }

// @GET
// @Path("/{variants}/sequence")
// @ApiOperation(httpMethod = "GET", value = "Get the adjacent sequence to the SNP(s) - Not yet implemented",
// hidden = true)
// public Response getSequence(@PathParam("variants") String query) {
// try {
// return null;
// } catch (Exception e) {
// return createErrorResponse(e);
// }
// }
}

0 comments on commit 82e57f3

Please sign in to comment.