Skip to content

Commit

Permalink
core: check verstion to use token or apiKey, and use param constants:…
Browse files Browse the repository at this point in the history
… apiKey, token, dataRelease, #TASK-4791, #TASK-4641
  • Loading branch information
jtarraga committed Aug 29, 2023
1 parent be9c607 commit a553982
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
import org.opencb.biodata.models.variant.avro.VariantAnnotation;
import org.opencb.cellbase.client.config.ClientConfiguration;
import org.opencb.cellbase.client.rest.models.mixin.DrugResponseClassificationMixIn;
import org.opencb.cellbase.core.api.query.AbstractQuery;
import org.opencb.cellbase.core.result.CellBaseDataResponse;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.commons.datastore.core.*;
import org.opencb.commons.datastore.core.Event;
import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.commons.utils.VersionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -53,6 +56,8 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static org.opencb.cellbase.core.ParamConstants.*;

/**
* Created by imedina on 12/05/16.
*/
Expand Down Expand Up @@ -440,28 +445,36 @@ private <U> CellBaseDataResponse<U> restCall(List<String> hosts, String version,
// Add the last URL part, the 'action' or 'resource'
callUrl = callUrl.path(resource);

// Attention: parameter 'token' was renamed to 'apiKey' in CelBase v5.7
String apiKeyParam = null;
if (VersionUtils.isMinVersion("v5.7", configuration.getVersion())) {
apiKeyParam = API_KEY_PARAM;
} else if (VersionUtils.isMinVersion("v5.3", configuration.getVersion())) {
apiKeyParam = TOKEN_PARAM;
}

if (queryOptions != null) {
for (String s : queryOptions.keySet()) {
callUrl = callUrl.queryParam(s, queryOptions.get(s));
}
if (assembly != null && StringUtils.isEmpty(queryOptions.getString("assembly"))) {
callUrl = callUrl.queryParam("assembly", assembly);
}
if (dataRelease != null && StringUtils.isEmpty(queryOptions.getString(AbstractQuery.DATA_RELEASE_PARAM))) {
callUrl = callUrl.queryParam(AbstractQuery.DATA_RELEASE_PARAM, dataRelease);
if (dataRelease != null && StringUtils.isEmpty(queryOptions.getString(DATA_RELEASE_PARAM))) {
callUrl = callUrl.queryParam(DATA_RELEASE_PARAM, dataRelease);
}
if (apiKey != null && StringUtils.isEmpty(queryOptions.getString(AbstractQuery.API_KEY_PARAM))) {
callUrl = callUrl.queryParam(AbstractQuery.API_KEY_PARAM, apiKey);
if (apiKeyParam != null && apiKey != null && StringUtils.isEmpty(queryOptions.getString(API_KEY_PARAM))) {
callUrl = callUrl.queryParam(apiKeyParam, apiKey);
}
} else {
if (assembly != null) {
callUrl = callUrl.queryParam("assembly", assembly);
}
if (dataRelease != null) {
callUrl = callUrl.queryParam(AbstractQuery.DATA_RELEASE_PARAM, dataRelease);
callUrl = callUrl.queryParam(DATA_RELEASE_PARAM, dataRelease);
}
if (apiKey != null) {
callUrl = callUrl.queryParam(AbstractQuery.API_KEY_PARAM, apiKey);
if (apiKeyParam != null && apiKey != null) {
callUrl = callUrl.queryParam(apiKeyParam, apiKey);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,18 @@ public Type type() {
+ "or dot notation (e.g. transcripts.biotype).";
public static final String DEFAULT_VERSION = "v5.5";
public static final String VERSION_DESCRIPTION = "API version, e.g.: " + DEFAULT_VERSION;

public static final String DATA_RELEASE_PARAM = "dataRelease";
public static final String DATA_RELEASE_DESCRIPTION = "Data release. To use the default data release, set this to 0. To get the list"
+ " of available data release, please call the endpoint 'meta/dataReleases'";
public static final String API_KEY_DESCRIPTION = "Data token to allow access to licensed/restricted data sources such as"

public static final String API_KEY_PARAM = "apiKey";
public static final String API_KEY_DESCRIPTION = "API key to allow access to licensed/restricted data sources such as"
+ " Cosmic or HGMD";
// For backward-compatibility, but it should be deprecated
public static final String TOKEN_PARAM = "token";


public static final String DEFAULT_ASSEMBLY = "grch38";
public static final String ASSEMBLY_DESCRIPTION = "Set the reference genome assembly, e.g. grch38. For a full list of "
+ "potentially available assemblies, please call the endpoint 'meta/species'";
Expand Down Expand Up @@ -294,8 +302,6 @@ public Type type() {
+ " Exact text matches will be returned";

// ---------------------------------------------
public static final String DATA_TOKEN_DESCRIPTION = "Token to allow access to licensed/restricted data sources";
public static final String DATA_TOKEN_PARAM = "dataToken";

public static final String SOURCE_DESCRIPTION = "Comma separated list of database sources of the documents to be returned."
+ " Possible values are clinvar or cosmic";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import java.lang.reflect.Field;
import java.util.*;

import static org.opencb.cellbase.core.ParamConstants.API_KEY_PARAM;
import static org.opencb.cellbase.core.ParamConstants.DATA_RELEASE_PARAM;

/**
* Helper object used to construct queries consumed by the dbadapters.
*/
Expand All @@ -45,11 +48,6 @@ public abstract class AbstractQuery extends CellBaseQueryOptions {
// public static final int DEFAULT_LIMIT = 50;
public static final int DEFAULT_SKIP = 0;

public static final String DATA_RELEASE_PARAM = "dataRelease";
public static final String API_KEY_PARAM = "apiKey";
// @Deprecated
// public static final String TOKEN_PARAM = "token";

// list of fields in this class
private Map<String, Field> classFields;
// key = transcripts.biotype, value = transcriptsBiotype
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.opencb.biodata.models.variant.avro.VariantType;
import org.opencb.cellbase.core.ParamConstants;
import org.opencb.cellbase.core.api.ClinicalVariantQuery;
import org.opencb.cellbase.core.api.query.AbstractQuery;
import org.opencb.cellbase.core.api.query.ProjectionQueryOptions;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.core.result.CellBaseDataResult;
Expand All @@ -46,6 +45,8 @@
import java.util.*;
import java.util.function.Consumer;

import static org.opencb.cellbase.core.ParamConstants.DATA_RELEASE_PARAM;

/**
* Created by fjlopez on 06/12/16.
*/
Expand Down Expand Up @@ -103,15 +104,15 @@ public CellBaseDataResult<Long> count(Query query) throws CellBaseException {
Bson bson = parseQuery(query);

MongoDBCollection mongoDBCollection = getCollectionByRelease(mongoDBCollectionByRelease,
(Integer) query.getOrDefault(AbstractQuery.DATA_RELEASE_PARAM, 0));
(Integer) query.getOrDefault(DATA_RELEASE_PARAM, 0));
return new CellBaseDataResult<>(mongoDBCollection.count(bson));
}

public CellBaseDataResult distinct(Query query, String field) throws CellBaseException {
Bson bson = parseQuery(query);

MongoDBCollection mongoDBCollection = getCollectionByRelease(mongoDBCollectionByRelease,
(Integer) query.getOrDefault(AbstractQuery.DATA_RELEASE_PARAM, 0));
(Integer) query.getOrDefault(DATA_RELEASE_PARAM, 0));
return new CellBaseDataResult<>(mongoDBCollection.distinct(field, bson));
}

Expand All @@ -128,7 +129,7 @@ public CellBaseDataResult<Variant> get(Query query, QueryOptions options) throws
logger.debug("queryOptions: {}", options.toJson());

MongoDBCollection mongoDBCollection = getCollectionByRelease(mongoDBCollectionByRelease,
(Integer) query.getOrDefault(AbstractQuery.DATA_RELEASE_PARAM, 0));
(Integer) query.getOrDefault(DATA_RELEASE_PARAM, 0));
return new CellBaseDataResult<>(mongoDBCollection.find(bson, null, Variant.class, parsedOptions));
}

Expand All @@ -140,7 +141,7 @@ public CellBaseDataResult nativeGet(Query query, QueryOptions options) throws Ce
logger.debug("queryOptions: {}", options.toJson());

MongoDBCollection mongoDBCollection = getCollectionByRelease(mongoDBCollectionByRelease,
(Integer) query.getOrDefault(AbstractQuery.DATA_RELEASE_PARAM, 0));
(Integer) query.getOrDefault(DATA_RELEASE_PARAM, 0));
return new CellBaseDataResult<>(mongoDBCollection.find(bson, parsedOptions));
}

Expand All @@ -152,7 +153,7 @@ public Iterator nativeIterator(Query query, QueryOptions options) throws CellBas
Bson bson = parseQuery(query);

MongoDBCollection mongoDBCollection = getCollectionByRelease(mongoDBCollectionByRelease,
(Integer) query.getOrDefault(AbstractQuery.DATA_RELEASE_PARAM, 0));
(Integer) query.getOrDefault(DATA_RELEASE_PARAM, 0));
return mongoDBCollection.nativeQuery().find(bson, options);
}

Expand Down Expand Up @@ -417,8 +418,8 @@ private CellBaseDataResult<Variant> getClinicalVariant(Variant variant, GenomeMa
}

// Add data release to query
if (!query.containsKey(AbstractQuery.DATA_RELEASE_PARAM)) {
query.put(AbstractQuery.DATA_RELEASE_PARAM, dataRelease);
if (!query.containsKey(DATA_RELEASE_PARAM)) {
query.put(DATA_RELEASE_PARAM, dataRelease);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.*;
import java.util.function.Consumer;

import static org.opencb.cellbase.core.ParamConstants.DATA_RELEASE_PARAM;
import static org.opencb.cellbase.lib.MongoDBCollectionConfiguration.VARIATION_FUNCTIONAL_SCORE_CHUNK_SIZE;

/**
Expand Down Expand Up @@ -252,7 +253,7 @@ public Bson parseQuery(VariantQuery query) {
case "region":
createRegionQuery(query, query.getRegions(), MongoDBCollectionConfiguration.VARIATION_CHUNK_SIZE, andBsonList);
break;
case VariantQuery.DATA_RELEASE_PARAM:
case DATA_RELEASE_PARAM:
case "svType":
// don't do anything, this is parsed later
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.ArrayList;
import java.util.List;

import static org.opencb.cellbase.core.api.query.AbstractQuery.API_KEY_PARAM;
import static org.opencb.cellbase.core.ParamConstants.API_KEY_PARAM;

public class AbstractManager implements AutoCloseable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.opencb.biodata.models.variant.avro.Expression;
import org.opencb.biodata.models.variant.avro.ExpressionCall;
import org.opencb.cellbase.core.api.GeneQuery;
import org.opencb.cellbase.core.api.query.AbstractQuery;
import org.opencb.cellbase.core.api.query.LogicalList;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.cellbase.lib.GenericMongoDBAdaptorTest;
Expand All @@ -36,6 +35,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opencb.cellbase.core.ParamConstants.DATA_RELEASE_PARAM;

/**
* Created by fjlopez on 08/10/15.
Expand All @@ -53,7 +53,7 @@ public void testQueryId() throws Exception {
Map<String, String> paramMap = new HashMap<>();
paramMap.put("id", "ENSG00000248746");
paramMap.put("include", "id,name,start,end");
paramMap.put(AbstractQuery.DATA_RELEASE_PARAM, String.valueOf(dataRelease));
paramMap.put(DATA_RELEASE_PARAM, String.valueOf(dataRelease));

GeneQuery geneQuery = new GeneQuery(paramMap);
geneQuery.setCount(Boolean.TRUE);
Expand All @@ -73,7 +73,7 @@ public void testQuery() throws Exception {
paramMap.put("annotation.expression.tissue", "brain");
paramMap.put("annotation.expression.value", "UP");
paramMap.put("include", "id,name");
paramMap.put(AbstractQuery.DATA_RELEASE_PARAM, String.valueOf(dataRelease));
paramMap.put(DATA_RELEASE_PARAM, String.valueOf(dataRelease));

GeneQuery geneQuery = new GeneQuery(paramMap);
geneQuery.setCount(Boolean.TRUE);
Expand Down Expand Up @@ -125,7 +125,7 @@ public void testConstraints() throws Exception {

Map<String, String> paramMap = new HashMap<>();
paramMap.put("constraints", "oe_lof<=0.85585");
paramMap.put(AbstractQuery.DATA_RELEASE_PARAM, String.valueOf(dataRelease));
paramMap.put(DATA_RELEASE_PARAM, String.valueOf(dataRelease));
GeneQuery geneQuery = new GeneQuery(paramMap);
CellBaseDataResult<Gene> cellBaseDataResult = geneManager.search(geneQuery);
assertEquals(12, cellBaseDataResult.getNumResults());
Expand All @@ -134,28 +134,28 @@ public void testConstraints() throws Exception {

paramMap = new HashMap<>();
paramMap.put("constraints", "oe_mis>0.8");
paramMap.put(AbstractQuery.DATA_RELEASE_PARAM, String.valueOf(dataRelease));
paramMap.put(DATA_RELEASE_PARAM, String.valueOf(dataRelease));
geneQuery = new GeneQuery(paramMap);
cellBaseDataResult = geneManager.search(geneQuery);
assertEquals(9, cellBaseDataResult.getNumResults());

paramMap = new HashMap<>();
paramMap.put("constraints", "oe_syn=0.91766");
paramMap.put(AbstractQuery.DATA_RELEASE_PARAM, String.valueOf(dataRelease));
paramMap.put(DATA_RELEASE_PARAM, String.valueOf(dataRelease));
geneQuery = new GeneQuery(paramMap);
cellBaseDataResult = geneManager.search(geneQuery);
assertEquals(0, cellBaseDataResult.getNumResults());

paramMap = new HashMap<>();
paramMap.put("constraints", " exac_pLI<0.17633");
paramMap.put(AbstractQuery.DATA_RELEASE_PARAM, String.valueOf(dataRelease));
paramMap.put(DATA_RELEASE_PARAM, String.valueOf(dataRelease));
geneQuery = new GeneQuery(paramMap);
cellBaseDataResult = geneManager.search(geneQuery);
assertEquals(0, cellBaseDataResult.getNumResults());

paramMap = new HashMap<>();
paramMap.put("constraints", "exac_oe_lof>=0.45091");
paramMap.put(AbstractQuery.DATA_RELEASE_PARAM, String.valueOf(dataRelease));
paramMap.put(DATA_RELEASE_PARAM, String.valueOf(dataRelease));
geneQuery = new GeneQuery(paramMap);
cellBaseDataResult = geneManager.search(geneQuery);
assertEquals(7, cellBaseDataResult.getNumResults());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.opencb.cellbase.core.api.query.AbstractQuery.API_KEY_PARAM;
import static org.opencb.cellbase.core.ParamConstants.*;

@Path("/{version}/{species}")
@Produces("text/plain")
Expand Down Expand Up @@ -179,13 +179,13 @@ private void initQuery() throws CellBaseException {

// Set default API key, if necessary
// For back-compatibility, we get 'token' parameter after checking 'apiKey' parameter
String apiKey = uriParams.containsKey(API_KEY_PARAM) ? uriParams.get(API_KEY_PARAM) : uriParams.get("token");
logger.info("before checking, API key {}", apiKey);
String apiKey = uriParams.containsKey(API_KEY_PARAM) ? uriParams.get(API_KEY_PARAM) : uriParams.get(TOKEN_PARAM);
logger.info("Before checking, API key {}", apiKey);
if (StringUtils.isEmpty(apiKey)) {
apiKey = defaultApiKey;
}
uriParams.put(API_KEY_PARAM, apiKey);
logger.info("after checking, API key {}", uriParams.get(API_KEY_PARAM));
logger.info("After checking, API key {}", uriParams.get(API_KEY_PARAM));

checkLimit();

Expand All @@ -209,9 +209,9 @@ private void initQuery() throws CellBaseException {
}

protected int getDataRelease() throws CellBaseException {
if (uriParams.containsKey("dataRelease") && StringUtils.isNotEmpty(uriParams.get("dataRelease"))) {
if (uriParams.containsKey(DATA_RELEASE_PARAM) && StringUtils.isNotEmpty(uriParams.get(DATA_RELEASE_PARAM))) {
try {
int dataRelease = Integer.parseInt(uriParams.get("dataRelease"));
int dataRelease = Integer.parseInt(uriParams.get(DATA_RELEASE_PARAM));
// If data release is 0, then use the default data release
if (dataRelease == 0) {
logger.info("Using data release 0 in query: using the default data release '" + defaultDataRelease + "' for CellBase"
Expand All @@ -221,7 +221,7 @@ protected int getDataRelease() throws CellBaseException {
return dataRelease;
}
} catch (NumberFormatException e) {
throw new CellBaseException("Invalid data release number '" + uriParams.get("dataRelease") + "'");
throw new CellBaseException("Invalid data release number '" + uriParams.get(DATA_RELEASE_PARAM) + "'");
}
}
// If no data release is present in the query, then use the default data release
Expand Down

0 comments on commit a553982

Please sign in to comment.