Skip to content

Commit

Permalink
update parseQuery to parse genequery object. use text instead of Quer…
Browse files Browse the repository at this point in the history
…yParams because they may not always be the same. #528
  • Loading branch information
julie-sullivan committed Feb 25, 2020
2 parents 46b07bb + 68e48b5 commit 3b77e97
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.opencb.cellbase.core.api.core;

import org.opencb.cellbase.core.api.queries.AbstractQuery;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
Expand Down Expand Up @@ -76,6 +77,8 @@ default List<CellBaseDataResult> nativeGet(List<Query> queries, QueryOptions opt
return queryResults;
}

CellBaseDataResult nativeGet(AbstractQuery query);

@Override
default Iterator<T> iterator() {
return iterator(new Query(), new QueryOptions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import org.apache.commons.collections.CollectionUtils;
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;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

public class AbstractQuery extends org.opencb.cellbase.core.api.queries.QueryOptions {
public abstract class AbstractQuery extends org.opencb.cellbase.core.api.queries.QueryOptions {

protected ObjectMapper objectMapper;
protected Logger logger;
Expand Down Expand Up @@ -94,37 +91,62 @@ private Map<String, Class<?>> loadPropertiesMap() {
return internalPropertiesMap;
}


protected abstract void validateQuery() throws QueryException;

/**
* Checks if values for query are legal, e.g. >= 0 and <= MAX Checks the following parameters:
*
* - SKIP
* - LIMIT
*
* NULL values are considered valid.
* @throws CellbaseException if the skip or limit values are invalid
* @throws QueryException if the skip or limit values are invalid.
*/
public void validate() throws CellbaseException {
Integer skip = getSkip();
Integer limit = getLimit();
public void validate() throws QueryException {
this.checkIncludeAndExclude();
this.checkLimitAndSkip();
this.checkSortAndOrder();

if (skip != null) {
if (skip < 0) {
throw new CellbaseException("Invalid value for skip field " + skip + ". Must be greater than zero");
}
if (skip > MAX_RECORDS) {
throw new CellbaseException("Invalid value for skip field " + skip + ". Must be less than " + MAX_RECORDS);
// Execute private checks
this.validateQuery();
}

private void checkIncludeAndExclude() throws QueryException {
if (CollectionUtils.isNotEmpty(includes) && CollectionUtils.isNotEmpty(excludes)) {
Collection intersection = CollectionUtils.intersection(includes, excludes);
if (intersection.size() > 0) {
throw new QueryException("");
}
}
}

private void checkLimitAndSkip() throws QueryException {
Integer limit = getLimit();
if (limit != null) {
if (limit < 0) {
throw new CellbaseException("Invalid value for limit field " + limit + ". Must be greater than zero");
throw new QueryException("Invalid value for limit field " + limit + ". Must be greater than zero");
}
if (limit > MAX_RECORDS) {
throw new CellbaseException("Invalid value for limit field " + limit + ". Must be less than " + MAX_RECORDS);
// if (limit > MAX_RECORDS) {
// throw new QueryException("Invalid value for limit field " + limit + ". Must be less than " + MAX_RECORDS);
// }
}

Integer skip = getSkip();
if (skip != null) {
if (skip < 0) {
throw new QueryException("Invalid value for skip field " + skip + ". Must be greater than zero");
}
// if (skip > MAX_RECORDS) {
// throw new QueryException("Invalid value for skip field " + skip + ". Must be less than " + MAX_RECORDS);
// }
}
}

private void checkSortAndOrder() throws QueryException {
if (order != null && StringUtils.isEmpty(sort)) {
throw new QueryException("");
}
return;
}

public void setDefaults() {
Expand All @@ -146,7 +168,6 @@ public QueryOptions toQueryOptions() {
queryOptions.put(QueryOptions.EXCLUDE, StringUtils.join(excludes));
queryOptions.put(QueryOptions.SORT, sort);
queryOptions.put(QueryOptions.ORDER, order);
queryOptions.put(QueryOptions.TIMEOUT, timeout);
queryOptions.put(QueryOptions.FACET, facet);
return queryOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import java.util.Map;

public class FeatureQuery extends AbstractQuery {
@Deprecated
public class FeatureQuery {

private Boolean histogram;
private static final int HISTOGRAM_INTERVAL_SIZE = 200000;
Expand All @@ -28,7 +29,7 @@ public FeatureQuery() {
}

public FeatureQuery(Map<String, String> params) {
super(params);
// super(params);
}

public Boolean getHistogram() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
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;
import java.util.Map;

public class GeneQuery extends FeatureQuery {
public class GeneQuery extends AbstractQuery {

private List<String> ids;
private List<String> id;
private List<String> names;
private List<String> biotypes;
private List<Region> regions;
Expand All @@ -50,12 +49,50 @@ public GeneQuery(Map<String, String> params) {
super(params);
}

public List<String> getIds() {
return ids;
@Override
protected void validateQuery() throws QueryException {

}

public GeneQuery setIds(List<String> ids) {
this.ids = ids;
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("GeneQuery{");
sb.append("id=").append(id);
sb.append(", names=").append(names);
sb.append(", biotypes=").append(biotypes);
sb.append(", regions=").append(regions);
sb.append(", transcriptsBiotype=").append(transcriptsBiotype);
sb.append(", transcriptsXrefs=").append(transcriptsXrefs);
sb.append(", transcriptsId=").append(transcriptsId);
sb.append(", transcriptsName=").append(transcriptsName);
sb.append(", transcriptsAnnotationFlags=").append(transcriptsAnnotationFlags);
sb.append(", transcriptsTfbsName=").append(transcriptsTfbsName);
sb.append(", annotationDiseasesId=").append(annotationDiseasesId);
sb.append(", annotationDiseasesName=").append(annotationDiseasesName);
sb.append(", annotationExpressionGene=").append(annotationExpressionGene);
sb.append(", annotationExpressionTissue=").append(annotationExpressionTissue);
sb.append(", annotationExpressionValue=").append(annotationExpressionValue);
sb.append(", annotationDrugsName=").append(annotationDrugsName);
sb.append(", annotationDrugsGene=").append(annotationDrugsGene);
sb.append(", objectMapper=").append(objectMapper);
sb.append(", limit=").append(limit);
sb.append(", skip=").append(skip);
sb.append(", count=").append(count);
sb.append(", sort='").append(sort).append('\'');
sb.append(", order=").append(order);
sb.append(", facet='").append(facet).append('\'');
sb.append(", includes=").append(includes);
sb.append(", excludes=").append(excludes);
sb.append('}');
return sb.toString();
}

public List<String> getId() {
return id;
}

public GeneQuery setId(List<String> id) {
this.id = id;
return this;
}

Expand Down Expand Up @@ -204,37 +241,8 @@ public GeneQuery setAnnotationDrugsGene(List<String> annotationDrugsGene) {
return this;
}

public void validate() throws CellbaseException {
super.validate();

// excludes and includes contain valid values
}

@Override
public String toString() {
return "GeneQuery{"
+ "ids=" + ids
+ ", names=" + names
+ ", biotypes=" + biotypes
+ ", regions=" + regions
+ ", transcriptsBiotype=" + transcriptsBiotype
+ ", transcriptsXrefs=" + transcriptsXrefs
+ ", transcriptsId=" + transcriptsId
+ ", transcriptsName=" + transcriptsName
+ ", transcriptsAnnotationFlags=" + transcriptsAnnotationFlags
+ ", transcriptsTfbsName=" + transcriptsTfbsName
+ ", annotationDiseasesId=" + annotationDiseasesId
+ ", annotationDiseasesName=" + annotationDiseasesName
+ ", annotationExpressionGene=" + annotationExpressionGene
+ ", annotationExpressionTissue=" + annotationExpressionTissue
+ ", annotationExpressionValue=" + annotationExpressionValue
+ ", annotationDrugsName=" + annotationDrugsName
+ ", annotationDrugsGene=" + annotationDrugsGene
+ '}';
}

public static class Builder {
private List<String> ids;
private List<String> id;
private List<String> names;
private List<String> biotypes;
private List<Region> regions;
Expand All @@ -256,7 +264,7 @@ public Builder() {
}

public Builder withIds(List<String> ids) {
this.ids = ids;
this.id = ids;
return this;
}

Expand Down Expand Up @@ -342,7 +350,7 @@ public Builder withAnnotationDrugsGene(List<String> annotationDrugsGene) {

public GeneQuery build() {
GeneQuery geneQuery = new GeneQuery();
geneQuery.ids = this.ids;
geneQuery.id = this.id;
geneQuery.names = this.names;
geneQuery.biotypes = this.biotypes;
geneQuery.regions = this.regions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@

public class ProjectionQueryOptions {

protected List<String> excludes;
protected List<String> includes;
protected List<String> excludes;

public ProjectionQueryOptions() {
}

public List<String> getExcludes() {
return excludes;
}

public ProjectionQueryOptions setExcludes(List<String> excludes) {
public ProjectionQueryOptions(List<String> includes, List<String> excludes) {
this.includes = includes;
this.excludes = excludes;
return this;
}

public ProjectionQueryOptions addExcludes(String excludes) {
Expand All @@ -45,6 +41,23 @@ public ProjectionQueryOptions addExcludes(String excludes) {
return this;
}

public ProjectionQueryOptions addExcludes(List<String> excludes) {
if (this.excludes == null) {
this.excludes = new ArrayList<>();
}
this.excludes.addAll(excludes);
return this;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ProjectionQueryOptions{");
sb.append("includes=").append(includes);
sb.append(", excludes=").append(excludes);
sb.append('}');
return sb.toString();
}

public List<String> getIncludes() {
return includes;
}
Expand All @@ -54,11 +67,12 @@ public ProjectionQueryOptions setIncludes(List<String> includes) {
return this;
}

@Override
public String toString() {
return "ProjectionQueryOptions{"
+ "excludes=" + excludes
+ ", includes=" + includes
+ '}';
public List<String> getExcludes() {
return excludes;
}

public ProjectionQueryOptions setExcludes(List<String> excludes) {
this.excludes = excludes;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.core.api.queries;

public class QueryException extends Exception {

public QueryException(String message) {
super(message);
}
}
Loading

0 comments on commit 3b77e97

Please sign in to comment.