Skip to content

Commit

Permalink
catalog: add aggregation stats (facets) for cohorts, clinical analyse…
Browse files Browse the repository at this point in the history
…s, families, individuals, interpretations, jobs and panels, #TASK-7152, #TASK-7134
  • Loading branch information
jtarraga committed Oct 28, 2024
1 parent d43005e commit 7bbfea6
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public class AggregationStatsCommandOptions {
@Parameter(names = {"--annotation"}, description = "Annotation filters. Example: age>30;gender=FEMALE. For more information, please visit http://docs.opencb.org/display/opencga/AnnotationSets+1.4.0", required = false, arity = 1)
public String annotation;

@Parameter(names = {"--field"}, description = "List of fields to aggregate separated by semicolons, e.g.: max(version);release", required = false, arity = 1)
@Parameter(names = {"--field"}, description = "List of fields separated by semicolons, e.g.: studies;type;numSamples[0..10]:1", required = false, arity = 1)
public String field;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.catalog.exceptions.CatalogParameterException;
import org.opencb.opencga.catalog.models.InternalGetDataResult;
import org.opencb.opencga.catalog.utils.CatalogFqn;
import org.opencb.opencga.core.api.ParamConstants;
import org.opencb.opencga.core.config.Configuration;
import org.opencb.opencga.core.models.IPrivateStudyUid;
import org.opencb.opencga.core.models.JwtPayload;
import org.opencb.opencga.core.models.study.Group;
import org.opencb.opencga.core.models.study.Study;
import org.opencb.opencga.core.response.OpenCGAResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -62,6 +65,11 @@ public abstract class AbstractManager {

protected static final String INTERNAL_DELIMITER = "__";

protected JwtPayload tokenPayload;
protected String organizationId;
protected String userId;
protected Study study;

AbstractManager(AuthorizationManager authorizationManager, AuditManager auditManager, CatalogManager catalogManager,
DBAdaptorFactory catalogDBAdaptorFactory, Configuration configuration) {
this.authorizationManager = authorizationManager;
Expand All @@ -73,6 +81,15 @@ public abstract class AbstractManager {
logger = LoggerFactory.getLogger(this.getClass());
}

protected void setInternalVariables(String studyStr, String token) throws CatalogException {
tokenPayload = catalogManager.getUserManager().validateToken(token);
CatalogFqn studyFqn = CatalogFqn.extractFqnFromStudy(studyStr, tokenPayload);
organizationId = studyFqn.getOrganizationId();
userId = tokenPayload.getUserId(organizationId);

study = catalogManager.getStudyManager().resolveId(studyFqn, StudyManager.INCLUDE_VARIABLE_SET, tokenPayload);
}

protected DBAdaptorFactory getCatalogDBAdaptorFactory() {
return catalogDBAdaptorFactory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,15 @@ public DBIterator<ClinicalAnalysis> iterator(String studyStr, Query query, Query

@Override
public OpenCGAResult<FacetField> facet(String studyStr, Query query, String facet, String token) throws CatalogException {
return null;
query = ParamUtils.defaultObject(query, Query::new);

// Set internal variables: tokenPayload, study, organizationId, userId
setInternalVariables(studyStr, token);

fixQueryObject(organizationId, study, query, userId, token);
query.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid());

return getClinicalAnalysisDBAdaptor(organizationId).facet(study.getUid(), query, facet, userId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,16 @@ public DBIterator<Cohort> iterator(String studyStr, Query query, QueryOptions op

@Override
public OpenCGAResult<FacetField> facet(String studyStr, Query query, String facet, String token) throws CatalogException {
return null;
query = ParamUtils.defaultObject(query, Query::new);

// Set internal variables: tokenPayload, study, organizationId, userId
setInternalVariables(studyStr, token);

Query finalQuery = new Query(query);
fixQueryObject(organizationId, study, finalQuery, userId);
finalQuery.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid());

return getCohortDBAdaptor(organizationId).facet(study.getUid(), finalQuery, facet, userId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,16 @@ public DBIterator<Family> iterator(String studyStr, Query query, QueryOptions op

@Override
public OpenCGAResult<FacetField> facet(String studyStr, Query query, String facet, String token) throws CatalogException {
return null;
query = ParamUtils.defaultObject(query, Query::new);

// Set internal variables: tokenPayload, study, organizationId, userId
setInternalVariables(studyStr, token);

Query finalQuery = new Query(query);
fixQueryObject(organizationId, study, query, tokenPayload);
query.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid());

return getFamilyDBAdaptor(organizationId).facet(study.getUid(), query, facet, userId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1422,15 +1422,10 @@ public DBIterator<File> iterator(String studyStr, Query query, QueryOptions opti
public OpenCGAResult facet(String studyStr, Query query, String facet, String token) throws CatalogException {
query = ParamUtils.defaultObject(query, Query::new);

JwtPayload tokenPayload = catalogManager.getUserManager().validateToken(token);
CatalogFqn studyFqn = CatalogFqn.extractFqnFromStudy(studyStr, tokenPayload);
String organizationId = studyFqn.getOrganizationId();
String userId = tokenPayload.getUserId(organizationId);

Study study = catalogManager.getStudyManager().resolveId(studyFqn, StudyManager.INCLUDE_VARIABLE_SET, tokenPayload);
// Set internal variables: tokenPayload, study, organizationId, userId
setInternalVariables(studyStr, token);

fixQueryObject(study, query, userId);

query.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid());

return getFileDBAdaptor(organizationId).facet(study.getUid(), query, facet, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,16 @@ public DBIterator<Individual> iterator(String studyStr, Query query, QueryOption

@Override
public OpenCGAResult<FacetField> facet(String studyStr, Query query, String facet, String token) throws CatalogException {
return null;
query = ParamUtils.defaultObject(query, Query::new);

// Set internal variables: tokenPayload, study, organizationId, userId
setInternalVariables(studyStr, token);

Query finalQuery = new Query(query);
fixQuery(organizationId, study, finalQuery, userId);
finalQuery.append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid());

return getIndividualDBAdaptor(organizationId).facet(study.getUid(), finalQuery, facet, userId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,15 @@ public DBIterator<Interpretation> iterator(String studyStr, Query query, QueryOp

@Override
public OpenCGAResult<FacetField> facet(String studyStr, Query query, String facet, String token) throws CatalogException {
return null;
query = ParamUtils.defaultObject(query, Query::new);

// Set internal variables: tokenPayload, study, organizationId, userId
setInternalVariables(studyStr, token);

fixQueryObject(organizationId, study, query, userId);
query.append(InterpretationDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid());

return getInterpretationDBAdaptor(organizationId).facet(study.getUid(), query, facet, userId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,15 @@ public DBIterator<Job> iterator(String studyId, Query query, QueryOptions option

@Override
public OpenCGAResult<FacetField> facet(String studyStr, Query query, String facet, String token) throws CatalogException {
return null;
query = ParamUtils.defaultObject(query, Query::new);

// Set internal variables: tokenPayload, study, organizationId, userId
setInternalVariables(studyStr, token);

fixQueryObject(organizationId, study, query, userId);
query.append(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid());

return getJobDBAdaptor(organizationId).facet(study.getUid(), query, facet, userId);
}

public OpenCGAResult countInOrganization(String organizationId, Query query, String token) throws CatalogException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import org.opencb.commons.utils.ListUtils;
import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager;
import org.opencb.opencga.catalog.db.DBAdaptorFactory;
import org.opencb.opencga.catalog.db.api.DBIterator;
import org.opencb.opencga.catalog.db.api.FamilyDBAdaptor;
import org.opencb.opencga.catalog.db.api.PanelDBAdaptor;
import org.opencb.opencga.catalog.db.api.StudyDBAdaptor;
import org.opencb.opencga.catalog.db.api.*;
import org.opencb.opencga.catalog.exceptions.CatalogAuthorizationException;
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.catalog.exceptions.CatalogParameterException;
Expand Down Expand Up @@ -596,7 +593,15 @@ public DBIterator<Panel> iterator(String studyStr, Query query, QueryOptions opt

@Override
public OpenCGAResult<FacetField> facet(String studyStr, Query query, String facet, String token) throws CatalogException {
return null;
query = ParamUtils.defaultObject(query, Query::new);

// Set internal variables: tokenPayload, study, organizationId, userId
setInternalVariables(studyStr, token);

fixQueryObject(query);
query.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid());

return getPanelDBAdaptor(organizationId).facet(study.getUid(), query, facet, userId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public class SampleManager extends AnnotationSetManager<Sample> {
studyManager = catalogManager.getStudyManager();
}

protected void test(Query query) throws CatalogException {
fixQueryObject(organizationId, study, query, userId);

query.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid());
}

@Override
Enums.Resource getEntity() {
return Enums.Resource.SAMPLE;
Expand Down Expand Up @@ -288,15 +294,10 @@ public DBIterator<Sample> iterator(String studyStr, Query query, QueryOptions op
public OpenCGAResult facet(String studyStr, Query query, String facet, String token) throws CatalogException {
query = ParamUtils.defaultObject(query, Query::new);

JwtPayload tokenPayload = catalogManager.getUserManager().validateToken(token);
CatalogFqn studyFqn = CatalogFqn.extractFqnFromStudy(studyStr, tokenPayload);
String organizationId = studyFqn.getOrganizationId();
String userId = tokenPayload.getUserId(organizationId);

Study study = catalogManager.getStudyManager().resolveId(studyFqn, StudyManager.INCLUDE_VARIABLE_SET, tokenPayload);
// Set internal variables: tokenPayload, study, organizationId, userId
setInternalVariables(studyStr, token);

fixQueryObject(organizationId, study, query, userId);

query.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid());

return getSampleDBAdaptor(organizationId).facet(study.getUid(), query, facet, userId);
Expand Down
2 changes: 1 addition & 1 deletion opencga-client/src/main/R/R/Sample-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ setMethod("sampleClient", "OpencgaR", function(OpencgaR, annotationSet, members,
#' @param version Version.
#' @param somatic Somatic.
#' @param annotation Annotation filters. Example: age>30;gender=FEMALE. For more information, please visit http://docs.opencb.org/display/opencga/AnnotationSets+1.4.0.
#' @param field List of fields to aggregate separated by semicolons, e.g.: max(version);release.
#' @param field List of fields separated by semicolons, e.g.: studies;type;numSamples[0..10]:1.
aggregationStats=fetchOpenCGA(object=OpencgaR, category="samples", categoryId=NULL, subcategory=NULL,
subcategoryId=NULL, action="aggregationStats", params=params, httpMethod="GET", as.queryParam=NULL,
...),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public RestResponse<SampleAclEntryList> updateAcl(String members, String action,
* somatic: Somatic.
* annotation: Annotation filters. Example: age>30;gender=FEMALE. For more information, please visit
* http://docs.opencb.org/display/opencga/AnnotationSets+1.4.0.
* field: List of fields to aggregate separated by semicolons, e.g.: max(version);release.
* field: List of fields separated by semicolons, e.g.: studies;type;numSamples[0..10]:1.
* @return a RestResponse object.
* @throws ClientException ClientException if there is any server error.
*/
Expand Down
2 changes: 1 addition & 1 deletion opencga-client/src/main/javascript/Sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class Sample extends OpenCGAParentClass {
* @param {Boolean} [params.somatic] - Somatic.
* @param {String} [params.annotation] - Annotation filters. Example: age>30;gender=FEMALE. For more information, please visit
* http://docs.opencb.org/display/opencga/AnnotationSets+1.4.0.
* @param {String} [params.field] - List of fields to aggregate separated by semicolons, e.g.: max(version);release.
* @param {String} [params.field] - List of fields separated by semicolons, e.g.: studies;type;numSamples[0..10]:1.
* @returns {Promise} Promise object in the form of RestResponse instance.
*/
aggregationStats(params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def aggregation_stats(self, **options):
:param str annotation: Annotation filters. Example:
age>30;gender=FEMALE. For more information, please visit
http://docs.opencb.org/display/opencga/AnnotationSets+1.4.0.
:param str field: List of fields to aggregate separated by semicolons,
e.g.: max(version);release.
:param str field: List of fields separated by semicolons, e.g.:
studies;type;numSamples[0..10]:1.
"""

return self._get(category='samples', resource='aggregationStats', **options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ public Response getAggregationStats(
return run(() -> {
query.remove(ParamConstants.STUDY_PARAM);
query.remove("field");
return catalogManager.getFileManager().facet(studyStr, query, facet, token);
return catalogManager.getSampleManager().facet(studyStr, query, facet, token);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,11 @@ public Response getAggregationStats(
@ApiParam(value = "Version") @QueryParam("version") String version,
@ApiParam(value = "Somatic") @QueryParam("somatic") Boolean somatic,
@ApiParam(value = ParamConstants.ANNOTATION_DESCRIPTION) @QueryParam("annotation") String annotation,
@ApiParam(value = "List of fields to aggregate separated by semicolons, e.g.: max(version);release") @QueryParam("field") String field) {
@ApiParam(value = "List of fields separated by semicolons, e.g.: studies;type;numSamples[0..10]:1") @QueryParam("field") String facet) {
return run(() -> {
query.remove(ParamConstants.STUDY_PARAM);
query.remove("field");
return catalogManager.getSampleManager().facet(studyStr, query, field, token);
return catalogManager.getSampleManager().facet(studyStr, query, facet, token);
});
}
}

0 comments on commit 7bbfea6

Please sign in to comment.