Skip to content

Commit

Permalink
analysis: fix several analysis, #TASK-4389
Browse files Browse the repository at this point in the history
  • Loading branch information
pfurio committed Nov 20, 2023
1 parent 1b4d882 commit 09054d1
Show file tree
Hide file tree
Showing 38 changed files with 421 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.opencb.opencga.catalog.db.api.ProjectDBAdaptor;
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.catalog.managers.CatalogManager;
import org.opencb.opencga.catalog.utils.CatalogFqn;
import org.opencb.opencga.core.models.JwtPayload;
import org.opencb.opencga.core.models.project.Project;
import org.opencb.opencga.core.response.OpenCGAResult;

Expand Down Expand Up @@ -116,6 +118,11 @@ public static DownloadedRefGenome downloadRefGenome(String assembly, Path outDir
public static String getAssembly(CatalogManager catalogManager, String studyId, String sessionId) throws CatalogException {
String assembly = "";
OpenCGAResult<Project> projectQueryResult;

JwtPayload jwtPayload = catalogManager.getUserManager().validateToken(sessionId);
CatalogFqn studyFqn = CatalogFqn.extractFqnFromStudy(studyId, jwtPayload);
String organizationId = studyFqn.getOrganizationId();

projectQueryResult = catalogManager.getProjectManager().search(organizationId, new Query(ProjectDBAdaptor.QueryParams.STUDY.key(), studyId),
new QueryOptions(QueryOptions.INCLUDE, ProjectDBAdaptor.QueryParams.ORGANISM.key()), sessionId);
if (CollectionUtils.isNotEmpty(projectQueryResult.getResults())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected void run() throws ToolException {
}
qc.getCoverage().setGeneCoverageStats(geneCoverageStats);

catalogManager.getFileManager().update(organizationId, getStudy(), catalogBamFile.getId(), new FileUpdateParams().setQualityControl(qc),
catalogManager.getFileManager().update(getStudy(), catalogBamFile.getId(), new FileUpdateParams().setQualityControl(qc),
QueryOptions.empty(), getToken());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ protected void run() throws ToolException {
}

if (saveQc) {
catalogManager.getFileManager().update(organizationId, getStudy(), catalogBamFile.getId(), new FileUpdateParams().setQualityControl(qc),
catalogManager.getFileManager().update(getStudy(), catalogBamFile.getId(), new FileUpdateParams().setQualityControl(qc),
QueryOptions.empty(), getToken());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.catalog.managers.FileManager;
import org.opencb.opencga.catalog.utils.AnnotationUtils;
import org.opencb.opencga.catalog.utils.CatalogFqn;
import org.opencb.opencga.catalog.utils.Constants;
import org.opencb.opencga.core.api.ParamConstants;
import org.opencb.opencga.core.exceptions.ToolException;
import org.opencb.opencga.core.models.JwtPayload;
import org.opencb.opencga.core.models.common.AnnotationSet;
import org.opencb.opencga.core.models.file.File;
import org.opencb.opencga.core.models.sample.SamplePermissions;
Expand Down Expand Up @@ -71,7 +73,9 @@ public void setStudy(String study) {

@Override
protected void check() throws Exception {
String userId = catalogManager.getUserManager().getUserId(organizationId, token);
JwtPayload jwtPayload = catalogManager.getUserManager().validateToken(token);
CatalogFqn studyFqn = CatalogFqn.extractFqnFromStudy(study, jwtPayload);
String userId = jwtPayload.getUserId(studyFqn.getOrganizationId());

OpenCGAResult<File> fileResult = catalogManager.getFileManager().get(study, path, FileManager.INCLUDE_FILE_URI_PATH, token);
if (fileResult.getNumResults() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@
import org.opencb.opencga.catalog.managers.ClinicalAnalysisManager;
import org.opencb.opencga.catalog.managers.FamilyManager;
import org.opencb.opencga.catalog.managers.StudyManager;
import org.opencb.opencga.catalog.utils.CatalogFqn;
import org.opencb.opencga.core.api.ParamConstants;
import org.opencb.opencga.core.common.JacksonUtils;
import org.opencb.opencga.core.exceptions.ToolException;
import org.opencb.opencga.core.models.JwtPayload;
import org.opencb.opencga.core.models.clinical.ClinicalAnalysis;
import org.opencb.opencga.core.models.clinical.Interpretation;
import org.opencb.opencga.core.models.individual.Individual;
Expand Down Expand Up @@ -818,9 +820,13 @@ public List<ClinicalVariant> getPrimaryFindings(String clinicalAnalysisId, Query
return clinicalVariants;
}

public ClinicalAnalyst getAnalyst(String token) throws ToolException {
public ClinicalAnalyst getAnalyst(String studyId, String token) throws ToolException {
try {
String userId = catalogManager.getUserManager().getUserId(organizationId, token);
JwtPayload jwtPayload = catalogManager.getUserManager().validateToken(token);
CatalogFqn studyFqn = CatalogFqn.extractFqnFromStudy(studyId, jwtPayload);
String organizationId = studyFqn.getOrganizationId();
String userId = jwtPayload.getUserId(studyFqn.getOrganizationId());

OpenCGAResult<User> userQueryResult = catalogManager.getUserManager().get(organizationId, userId, new QueryOptions(QueryOptions.INCLUDE,
Arrays.asList(UserDBAdaptor.QueryParams.EMAIL.key(), UserDBAdaptor.QueryParams.ORGANIZATION.key())), token);
User user = userQueryResult.first();
Expand All @@ -831,6 +837,10 @@ public ClinicalAnalyst getAnalyst(String token) throws ToolException {
}

public String getAssembly(String studyId, String sessionId) throws CatalogException {
JwtPayload jwtPayload = catalogManager.getUserManager().validateToken(sessionId);
CatalogFqn studyFqn = CatalogFqn.extractFqnFromStudy(studyId, jwtPayload);
String organizationId = studyFqn.getOrganizationId();

String assembly = "";
OpenCGAResult<Project> projectQueryResult;
projectQueryResult = catalogManager.getProjectManager().search(organizationId, new Query(ProjectDBAdaptor.QueryParams.STUDY.key(), studyId),
Expand Down Expand Up @@ -877,7 +887,9 @@ private Query checkQueryPermissions(Query query, String token) throws ClinicalVa
}

// Get userId from token and Study numeric IDs from the query
String userId = catalogManager.getUserManager().getUserId(organizationId, token);
JwtPayload jwtPayload = catalogManager.getUserManager().validateToken(token);
String organizationId = jwtPayload.getOrganization();
String userId = jwtPayload.getUserId();
List<String> studyIds = getStudyIds(userId, query);

// If one specific clinical analysis, sample or individual is provided we expect a single valid study as well
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected void saveInterpretation(String studyId, ClinicalAnalysis clinicalAnaly
.setCommit(GitRepositoryState.getInstance().getCommitId())));

// Analyst
ClinicalAnalyst analyst = clinicalInterpretationManager.getAnalyst(token);
ClinicalAnalyst analyst = clinicalInterpretationManager.getAnalyst(studyId, token);

List<ClinicalVariant> primaryFindings = readClinicalVariants(Paths.get(getOutDir().toString() + "/"
+ PRIMARY_FINDINGS_FILENAME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ protected void saveInterpretation(String studyId, ClinicalAnalysis clinicalAnaly
.setVersion(ExomiserWrapperAnalysisExecutor.DOCKER_IMAGE_VERSION)));

// Analyst
ClinicalAnalyst analyst = clinicalInterpretationManager.getAnalyst(token);
ClinicalAnalyst analyst = clinicalInterpretationManager.getAnalyst(studyId, token);

List<ClinicalVariant> primaryFindings = getPrimaryFindings();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.opencb.opencga.catalog.stats.solr.converters.CatalogCohortToSolrCohortConverter;
import org.opencb.opencga.catalog.stats.solr.converters.SolrConverterUtil;
import org.opencb.opencga.core.api.ParamConstants;
import org.opencb.opencga.core.models.JwtPayload;
import org.opencb.opencga.core.models.common.Enums;
import org.opencb.opencga.core.models.study.Study;
import org.opencb.opencga.core.response.OpenCGAResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected void run() throws ToolException {
for (Family family: familiesToUpdate) {
try {
logger.info("Updating pedigree graph for family '{}'", family.getId());
catalogManager.getFamilyManager().update(organizationId, study, family.getId(), null,
catalogManager.getFamilyManager().update(study, family.getId(), null,
new QueryOptions(ParamConstants.FAMILY_UPDATE_PEDIGREEE_GRAPH_PARAM, true), token);
String msg = "Updated pedigree graph for family '" + family.getId() + "'";
logger.info(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.opencb.opencga.analysis.tools.OpenCgaTool;
import org.opencb.opencga.analysis.variant.relatedness.RelatednessAnalysis;
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.catalog.utils.CatalogFqn;
import org.opencb.opencga.core.exceptions.ToolException;
import org.opencb.opencga.core.models.JwtPayload;
import org.opencb.opencga.core.models.common.Enums;
import org.opencb.opencga.core.models.family.Family;
import org.opencb.opencga.core.models.family.FamilyQualityControl;
Expand Down Expand Up @@ -67,8 +69,12 @@ protected void check() throws Exception {

// Check permissions
try {
JwtPayload jwtPayload = catalogManager.getUserManager().validateToken(token);
CatalogFqn studyFqn = CatalogFqn.extractFqnFromStudy(studyId, jwtPayload);
String organizationId = studyFqn.getOrganizationId();
String userId = jwtPayload.getUserId(organizationId);

Study study = catalogManager.getStudyManager().get(studyId, QueryOptions.empty(), token).first();
String userId = catalogManager.getUserManager().getUserId(organizationId, token);
catalogManager.getAuthorizationManager().checkStudyPermission(organizationId, study.getUid(), userId, WRITE_FAMILIES);
} catch (CatalogException e) {
throw new ToolException(e);
Expand Down Expand Up @@ -128,7 +134,7 @@ protected void run() throws ToolException {
try {
qualityControl = executor.getQualityControl();
if (qualityControl != null) {
catalogManager.getFamilyManager().update(organizationId, getStudyId(), familyId, new FamilyUpdateParams().setQualityControl(qualityControl),
catalogManager.getFamilyManager().update(getStudyId(), familyId, new FamilyUpdateParams().setQualityControl(qualityControl),
QueryOptions.empty(), token);
}
} catch (CatalogException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.catalog.exceptions.CatalogIOException;
import org.opencb.opencga.catalog.managers.StudyManager;
import org.opencb.opencga.catalog.utils.CatalogFqn;
import org.opencb.opencga.core.common.UriUtils;
import org.opencb.opencga.core.exceptions.ToolException;
import org.opencb.opencga.core.models.JwtPayload;
import org.opencb.opencga.core.models.common.Enums;
import org.opencb.opencga.core.models.file.File;
import org.opencb.opencga.core.models.file.FileFetch;
Expand Down Expand Up @@ -73,9 +75,11 @@ protected void check() throws Exception {
}

try {
JwtPayload jwtPayload = catalogManager.getUserManager().validateToken(token);
CatalogFqn catalogFqn= CatalogFqn.extractFqnFromStudy(studyFqn, jwtPayload);
Study study = catalogManager.getStudyManager().get(studyFqn, StudyManager.INCLUDE_STUDY_IDS, token).first();

OpenCGAResult<File> parents = catalogManager.getFileManager().getParents(studyFqn, toolParams.getPath(), false,
OpenCGAResult<File> parents = catalogManager.getFileManager().getParents(study.getFqn(), toolParams.getPath(), false,
QueryOptions.empty(), token);
if (parents.getNumResults() == 0) {
throw new ToolException("No parent folders found for " + toolParams.getPath());
Expand All @@ -85,10 +89,10 @@ protected void check() throws Exception {
throw new CatalogException("Parent path " + parents.first().getPath() + " is external. Cannot download to mounted folders");
}

String userId = catalogManager.getUserManager().getUserId(organizationId, token);
String userId = jwtPayload.getUserId(catalogFqn.getOrganizationId());
// Check write permissions over the path
catalogManager.getAuthorizationManager()
.checkFilePermission(organizationId, study.getUid(), parents.first().getUid(), userId, FilePermissions.WRITE);
catalogManager.getAuthorizationManager().checkFilePermission(catalogFqn.getOrganizationId(), study.getUid(),
parents.first().getUid(), userId, FilePermissions.WRITE);
} catch (CatalogException e) {
throw new ToolException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ protected void run() throws Exception {
try {
// Update file status to PENDING_DELETE and add tags mark
if (catalogFile.getType() == File.Type.FILE) {
fileManager.update(organizationId, studyFqn, file, updateParams, options, token);
fileManager.update(studyFqn, file, updateParams, options, token);
} else {
// We mark for deletion all the
Query query = new Query()
.append(FileDBAdaptor.QueryParams.INTERNAL_STATUS_ID.key(), FileStatus.READY)
.append(FileDBAdaptor.QueryParams.PATH.key(), "~^" + catalogFile.getPath() + "*");
fileManager.update(organizationId, studyFqn, query, updateParams, options, token);
fileManager.update(studyFqn, query, updateParams, options, token);
}
} catch (Exception e) {
logger.error("Error updating status of file '{}' to PENDING_DELETE: {}", file, e.getMessage(), e);
Expand Down Expand Up @@ -236,7 +236,7 @@ private void setToMissingFile(File file) {

private void restore(Query query, FileUpdateParams updateParams, QueryOptions options) {
try {
catalogManager.getFileManager().update(organizationId, studyFqn, query, updateParams, options, token);
catalogManager.getFileManager().update(studyFqn, query, updateParams, options, token);
} catch (CatalogException e) {
addCriticalError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public int count(Query query) throws CatalogException {
public void addAnnotationSet(String entryId, AnnotationSet annotationSet, QueryOptions options) throws CatalogException {
FileUpdateParams fileUpdateParams = new FileUpdateParams().setAnnotationSets(Collections.singletonList(annotationSet));
options.put(Constants.ACTIONS, new ObjectMap(AnnotationSetManager.ANNOTATION_SETS, ParamUtils.BasicUpdateAction.ADD));
catalogManager.getFileManager().update(organizationId, study, entryId, fileUpdateParams, options, token);
catalogManager.getFileManager().update(study, entryId, fileUpdateParams, options, token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ protected void run() throws Exception {
try {
// Update file status to PENDING_DELETE and add tags mark
if (catalogFile.getType() == File.Type.FILE) {
fileManager.update(organizationId, studyFqn, file, updateParams, options, token);
fileManager.update(studyFqn, file, updateParams, options, token);
} else {
// We mark for deletion all the
Query query = new Query()
.append(FileDBAdaptor.QueryParams.INTERNAL_STATUS_ID.key(), FileStatus.READY)
.append(FileDBAdaptor.QueryParams.PATH.key(), "~^" + catalogFile.getPath() + "*");
fileManager.update(organizationId, studyFqn, query, updateParams, options, token);
fileManager.update(studyFqn, query, updateParams, options, token);
}
} catch (Exception e) {
logger.error("Error updating status of file '{}' to PENDING_DELETE: {}", file, e.getMessage(), e);
Expand Down Expand Up @@ -176,15 +176,14 @@ private void restoreFiles(Query query) {

private void restore(Query query, FileUpdateParams updateParams, QueryOptions options) {
try {
catalogManager.getFileManager().update(organizationId, studyFqn, query, updateParams, options, token);
catalogManager.getFileManager().update(studyFqn, query, updateParams, options, token);
} catch (CatalogException e) {
addCriticalError(e);
}
}

private void addCriticalError(CatalogException e) {
CatalogException exception = new CatalogException("Critical: Could not restore status of pending files to "
+ FileStatus.READY, e);
CatalogException exception = new CatalogException("Critical: Could not restore status of pending files to " + FileStatus.READY, e);
logger.error("{}", e.getMessage(), e);
try {
addError(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ protected void run() throws Exception {

QueryOptions queryOptions = new QueryOptions(Constants.ACTIONS, actionMap);

OpenCGAResult<File> fileUpdateResult = catalogManager.getFileManager().update(organizationId, study, file.getUuid(),
OpenCGAResult<File> fileUpdateResult = catalogManager.getFileManager().update(study, file.getUuid(),
fileUpdateParams, queryOptions, token);
if (fileUpdateResult.getNumUpdated() != 1) {
throw new CatalogException("Could not update sample list of file '" + file.getPath() + "'.");
Expand All @@ -206,7 +206,7 @@ protected void run() throws Exception {
FileUpdateParams fileUpdateParams = new FileUpdateParams()
.setInternal(new SmallFileInternal(new FileStatus(FileStatus.READY), MissingSamples.initialize()));

OpenCGAResult<File> fileUpdateResult = catalogManager.getFileManager().update(organizationId, study, file.getUuid(), fileUpdateParams,
OpenCGAResult<File> fileUpdateResult = catalogManager.getFileManager().update(study, file.getUuid(), fileUpdateParams,
QueryOptions.empty(), token);
if (fileUpdateResult.getNumUpdated() != 1) {
throw new CatalogException("Could not update internal status of file '" + file.getPath() + "'.");
Expand Down
Loading

0 comments on commit 09054d1

Please sign in to comment.