diff --git a/cellbase-core/src/main/java/org/opencb/cellbase/core/api/queries/AbstractQuery.java b/cellbase-core/src/main/java/org/opencb/cellbase/core/api/queries/AbstractQuery.java index e1d4d2fcd8..d7b2b8a76f 100644 --- a/cellbase-core/src/main/java/org/opencb/cellbase/core/api/queries/AbstractQuery.java +++ b/cellbase-core/src/main/java/org/opencb/cellbase/core/api/queries/AbstractQuery.java @@ -24,6 +24,7 @@ import org.apache.commons.lang3.StringUtils; import org.opencb.cellbase.core.exception.CellbaseException; import org.opencb.commons.datastore.core.ObjectMap; +import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -149,4 +150,9 @@ public QueryOptions toQueryOptions() { queryOptions.put(QueryOptions.FACET, facet); return queryOptions; } + + // temporary method because java commons still uses query + public Query toQuery() { + return new Query(); + } } diff --git a/cellbase-core/src/main/java/org/opencb/cellbase/core/api/queries/GeneQuery.java b/cellbase-core/src/main/java/org/opencb/cellbase/core/api/queries/GeneQuery.java index 0c070dd603..9626e5ab9f 100644 --- a/cellbase-core/src/main/java/org/opencb/cellbase/core/api/queries/GeneQuery.java +++ b/cellbase-core/src/main/java/org/opencb/cellbase/core/api/queries/GeneQuery.java @@ -17,6 +17,7 @@ package org.opencb.cellbase.core.api.queries; import org.opencb.biodata.models.core.Region; +import org.opencb.cellbase.core.exception.CellbaseException; import java.util.ArrayList; import java.util.List; @@ -203,6 +204,12 @@ public GeneQuery setAnnotationDrugsGene(List annotationDrugsGene) { return this; } + public void validate() throws CellbaseException { + super.validate(); + + // excludes and includes contain valid values + } + @Override public String toString() { return "GeneQuery{" diff --git a/cellbase-core/src/test/java/org/opencb/cellbase/core/GeneQueryTest.java b/cellbase-core/src/test/java/org/opencb/cellbase/core/GeneQueryTest.java index 5552000e95..daff19b02e 100644 --- a/cellbase-core/src/test/java/org/opencb/cellbase/core/GeneQueryTest.java +++ b/cellbase-core/src/test/java/org/opencb/cellbase/core/GeneQueryTest.java @@ -114,7 +114,6 @@ public void testCount() { assertFalse(geneQuery.getCount()); } - @Test public void testBuild() { geneQuery = new GeneQuery.Builder().withIds(Arrays.asList("1")).withBiotypes(Arrays.asList("a", "b", "c")) diff --git a/cellbase-lib/src/main/java/org/opencb/cellbase/lib/impl/core/GeneMongoDBAdaptor.java b/cellbase-lib/src/main/java/org/opencb/cellbase/lib/impl/core/GeneMongoDBAdaptor.java index ef25136e3e..86539c8eb8 100644 --- a/cellbase-lib/src/main/java/org/opencb/cellbase/lib/impl/core/GeneMongoDBAdaptor.java +++ b/cellbase-lib/src/main/java/org/opencb/cellbase/lib/impl/core/GeneMongoDBAdaptor.java @@ -31,6 +31,7 @@ import org.opencb.biodata.models.core.Gene; import org.opencb.biodata.models.core.Region; import org.opencb.cellbase.core.api.core.GeneDBAdaptor; +import org.opencb.cellbase.core.api.queries.GeneQuery; import org.opencb.cellbase.core.result.CellBaseDataResult; import org.opencb.cellbase.lib.MongoDBCollectionConfiguration; import org.opencb.commons.datastore.core.Query; @@ -144,6 +145,13 @@ public CellBaseDataResult nativeGet(Query geneQuery, QueryOptions queryOption) { return postDBFiltering(geneQuery, new CellBaseDataResult<>(mongoDBCollection.find(bson, null))); } + public CellBaseDataResult nativeGet(GeneQuery geneQuery) { + Bson bson = parseQuery(geneQuery); + logger.info("geneQuery: {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()) .toJson()); +// logger.info("options: {}", options.toJson()); + return postDBFiltering(geneQuery, new CellBaseDataResult<>(mongoDBCollection.find(bson, geneQuery.toQueryOptions()))); + } + @Override public Iterator iterator(Query geneQuery, QueryOptions options) { return null; @@ -252,8 +260,37 @@ public CellBaseDataResult getTfbs(Query geneQuery, QueryOptions queryOptions) { private Bson parseQuery(Query geneQuery) { List andBsonList = new ArrayList<>(); + createRegionQuery(geneQuery, QueryParams.REGION.key(), MongoDBCollectionConfiguration.GENE_CHUNK_SIZE, andBsonList); + + createOrQuery(geneQuery, QueryParams.ID.key(), "id", andBsonList); + createOrQuery(geneQuery, QueryParams.NAME.key(), "name", andBsonList); + createOrQuery(geneQuery, QueryParams.BIOTYPE.key(), "biotype", andBsonList); + createOrQuery(geneQuery, QueryParams.XREFS.key(), "transcripts.xrefs.id", andBsonList); + + createOrQuery(geneQuery, QueryParams.TRANSCRIPT_ID.key(), "transcripts.id", andBsonList); + createOrQuery(geneQuery, QueryParams.TRANSCRIPT_NAME.key(), "transcripts.name", andBsonList); + createOrQuery(geneQuery, QueryParams.TRANSCRIPT_BIOTYPE.key(), "transcripts.biotype", andBsonList); + createOrQuery(geneQuery, QueryParams.TRANSCRIPT_ANNOTATION_FLAGS.key(), "transcripts.annotationFlags", andBsonList); + createOrQuery(geneQuery, QueryParams.TFBS_NAME.key(), "transcripts.tfbs.name", andBsonList); + createOrQuery(geneQuery, QueryParams.ANNOTATION_DISEASE_ID.key(), "annotation.diseases.id", andBsonList); + createOrQuery(geneQuery, QueryParams.ANNOTATION_DISEASE_NAME.key(), "annotation.diseases.name", andBsonList); + createOrQuery(geneQuery, QueryParams.ANNOTATION_EXPRESSION_GENE.key(), "annotation.expression.geneName", andBsonList); + createOrQuery(geneQuery, QueryParams.ANNOTATION_DRUGS_NAME.key(), "annotation.drugs.drugName", andBsonList); + createOrQuery(geneQuery, QueryParams.ANNOTATION_DRUGS_GENE.key(), "annotation.drugs.geneName", andBsonList); + + createExpressionQuery(geneQuery, andBsonList); + + if (andBsonList.size() > 0) { + return Filters.and(andBsonList); + } else { + return new Document(); + } + } + + private Bson parseQuery(GeneQuery geneQuery) { + List andBsonList = new ArrayList<>(); createRegionQuery(geneQuery, QueryParams.REGION.key(), MongoDBCollectionConfiguration.GENE_CHUNK_SIZE, andBsonList); createOrQuery(geneQuery, QueryParams.ID.key(), "id", andBsonList); @@ -300,6 +337,7 @@ private Boolean postDBFilteringParametersEnabled(Query geneQuery) { return StringUtils.isNotEmpty(geneQuery.getString(QueryParams.TRANSCRIPT_ANNOTATION_FLAGS.key())); } + @Deprecated private CellBaseDataResult postDBFiltering(Query geneQuery, CellBaseDataResult documentCellBaseDataResult) { String annotationFlagsString = geneQuery.getString(QueryParams.TRANSCRIPT_ANNOTATION_FLAGS.key()); if (StringUtils.isNotEmpty(annotationFlagsString)) { @@ -323,4 +361,25 @@ private CellBaseDataResult postDBFiltering(Query geneQuery, CellBaseDa return documentCellBaseDataResult; } + private CellBaseDataResult postDBFiltering(GeneQuery geneQuery, CellBaseDataResult documentCellBaseDataResult) { + List flags = geneQuery.getTranscriptsAnnotationFlags(); + if (flags != null && !flags.isEmpty()) { + List documents = documentCellBaseDataResult.getResults(); + for (Document document : documents) { + ArrayList transcripts = document.get(TRANSCRIPTS, ArrayList.class); + ArrayList matchedTranscripts = new ArrayList<>(); + for (Document transcript : transcripts) { + ArrayList annotationFlags = transcript.get(ANNOTATION_FLAGS, ArrayList.class); + if (annotationFlags != null && annotationFlags.size() > 0) { + if (CollectionUtils.containsAny(annotationFlags, flags)) { + matchedTranscripts.add(transcript); + } + } + } + document.put(TRANSCRIPTS, matchedTranscripts); + } + documentCellBaseDataResult.setResults(documents); + } + return documentCellBaseDataResult; + } } diff --git a/cellbase-lib/src/main/java/org/opencb/cellbase/lib/managers/GeneManager.java b/cellbase-lib/src/main/java/org/opencb/cellbase/lib/managers/GeneManager.java index 87f75c3e8d..ec977f0c68 100644 --- a/cellbase-lib/src/main/java/org/opencb/cellbase/lib/managers/GeneManager.java +++ b/cellbase-lib/src/main/java/org/opencb/cellbase/lib/managers/GeneManager.java @@ -44,8 +44,8 @@ private void init() { public CellBaseDataResult search(GeneQuery geneQuery) throws CellbaseException { geneQuery.setDefaults(); geneQuery.validate(); -// return geneDBAdaptor.nativeGet(geneQuery); - return null; + // TODO throw execption if facets populated + return geneDBAdaptor.nativeGet(geneQuery); } public CellBaseDataResult groupBy(Query geneQuery, String fields) { diff --git a/cellbase-server/src/main/java/org/opencb/cellbase/server/rest/feature/GeneWSServer.java b/cellbase-server/src/main/java/org/opencb/cellbase/server/rest/feature/GeneWSServer.java index 17b48c8859..d53c07e350 100755 --- a/cellbase-server/src/main/java/org/opencb/cellbase/server/rest/feature/GeneWSServer.java +++ b/cellbase-server/src/main/java/org/opencb/cellbase/server/rest/feature/GeneWSServer.java @@ -359,30 +359,30 @@ public Response getAll() throws CellbaseException { @ApiOperation(httpMethod = "GET", value = "Get information about the specified gene(s)", response = Gene.class, responseContainer = "QueryResponse") @ApiImplicitParams({ - @ApiImplicitParam(name = "biotype", value = ParamConstants.GENE_BIOTYPES, - required = false, dataType = "java.util.List", paramType = "query"), - @ApiImplicitParam(name = "transcripts.biotype", value = ParamConstants.TRANSCRIPT_BIOTYPES, - required = false, dataType = "java.util.List", paramType = "query"), - @ApiImplicitParam(name = "transcripts.id", value = ParamConstants.TRANSCRIPT_ENSEMBL_IDS, - required = false, dataType = "java.util.List", paramType = "query"), - @ApiImplicitParam(name = "transcripts.name", value = ParamConstants.TRANSCRIPT_NAMES, - required = false, dataType = "java.util.List", paramType = "query"), - @ApiImplicitParam(name = "transcripts.annotationFlags", value = ParamConstants.TRANSCRIPT_ANNOTATION_FLAGS, - required = false, dataType = "string", paramType = "query"), - @ApiImplicitParam(name = "transcripts.tfbs.name", value = ParamConstants.TRANSCRIPT_TFBS_NAMES, - required = false, dataType = "java.util.List", paramType = "query"), - @ApiImplicitParam(name = "annotation.diseases.id", value = ParamConstants.ANNOTATION_DISEASES_IDS, - required = false, dataType = "java.util.List", paramType = "query"), - @ApiImplicitParam(name = "annotation.diseases.name", value = ParamConstants.ANNOTATION_DISEASES_NAMES, - required = false, dataType = "java.util.List", paramType = "query"), - @ApiImplicitParam(name = "annotation.expression.gene", value = ParamConstants.ANNOTATION_EXPRESSION_GENE, - required = false, dataType = "java.util.List", paramType = "query"), - @ApiImplicitParam(name = "annotation.expression.tissue", value = ParamConstants.ANNOTATION_EXPRESSION_TISSUE, - required = false, dataType = "java.util.List", paramType = "query"), - @ApiImplicitParam(name = "annotation.drugs.name", value = ParamConstants.ANNOTATION_DRUGS_NAME, - required = false, dataType = "java.util.List", paramType = "query"), - @ApiImplicitParam(name = "annotation.drugs.gene", value = ParamConstants.ANNOTATION_DRUGS_GENE, - required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "biotype", value = ParamConstants.GENE_BIOTYPES, +// required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "transcripts.biotype", value = ParamConstants.TRANSCRIPT_BIOTYPES, +// required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "transcripts.id", value = ParamConstants.TRANSCRIPT_ENSEMBL_IDS, +// required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "transcripts.name", value = ParamConstants.TRANSCRIPT_NAMES, +// required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "transcripts.annotationFlags", value = ParamConstants.TRANSCRIPT_ANNOTATION_FLAGS, +// required = false, dataType = "string", paramType = "query"), +// @ApiImplicitParam(name = "transcripts.tfbs.name", value = ParamConstants.TRANSCRIPT_TFBS_NAMES, +// required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "annotation.diseases.id", value = ParamConstants.ANNOTATION_DISEASES_IDS, +// required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "annotation.diseases.name", value = ParamConstants.ANNOTATION_DISEASES_NAMES, +// required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "annotation.expression.gene", value = ParamConstants.ANNOTATION_EXPRESSION_GENE, +// required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "annotation.expression.tissue", value = ParamConstants.ANNOTATION_EXPRESSION_TISSUE, +// required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "annotation.drugs.name", value = ParamConstants.ANNOTATION_DRUGS_NAME, +// required = false, dataType = "java.util.List", paramType = "query"), +// @ApiImplicitParam(name = "annotation.drugs.gene", value = ParamConstants.ANNOTATION_DRUGS_GENE, +// required = false, dataType = "java.util.List", paramType = "query"), @ApiImplicitParam(name = "exclude", value = ParamConstants.EXCLUDE_DESCRIPTION, required = false, dataType = "java.util.List", paramType = "query"), @ApiImplicitParam(name = "include", value = ParamConstants.INCLUDE_DESCRIPTION,