private void deleteReferencesToSample(long sampleId) throws CatalogDBException { // Remove references from files Query query = new Query(FileDBAdaptor.QueryParams.SAMPLE_UIDS.key(), sampleId); QueryResult<Long> result = dbAdaptorFactory.getCatalogFileDBAdaptor() .extractSampleFromFiles(query, Collections.singletonList(sampleId)); logger.debug("SampleId {} extracted from {} files", sampleId, result.first()); // Remove references from cohorts query = new Query(CohortDBAdaptor.QueryParams.SAMPLES.key(), sampleId); result = dbAdaptorFactory.getCatalogCohortDBAdaptor().extractSamplesFromCohorts(query, Collections.singletonList(sampleId)); logger.debug("SampleId {} extracted from {} cohorts", sampleId, result.first()); }
private void fixQueryObject(Study study, Query query, String sessionId) throws CatalogException { if (query.containsKey(CohortDBAdaptor.QueryParams.SAMPLES.key())) { // First look for the sample ids. MyResources<Sample> samples = catalogManager.getSampleManager() .getUids(query.getAsStringList(CohortDBAdaptor.QueryParams.SAMPLES.key()), study.getFqn(), sessionId); query.remove(CohortDBAdaptor.QueryParams.SAMPLES.key()); query.append(CohortDBAdaptor.QueryParams.SAMPLE_UIDS.key(), samples.getResourceList().stream().map(Sample::getUid) .collect(Collectors.toList())); } }
private void checkCohortNameExists(long studyId, String cohortName) throws CatalogDBException { QueryResult<Long> count = cohortCollection.count(Filters.and( Filters.eq(PRIVATE_STUDY_ID, studyId), Filters.eq(QueryParams.ID.key(), cohortName))); if (count.getResult().get(0) > 0) { throw CatalogDBException.alreadyExists("Cohort", "name", cohortName); } }
@Override public QueryResult<Cohort> getAllInStudy(long studyId, QueryOptions options) throws CatalogDBException { return get(new Query(QueryParams.STUDY_UID.key(), studyId), options); }
default boolean exists(long cohortId) throws CatalogDBException { return count(new Query(QueryParams.UID.key(), cohortId)).first() > 0; }
protected Cohort getDefaultCohort(String studyId) throws CatalogException { return catalogManager.getCohortManager().get(studyId, new Query(CohortDBAdaptor.QueryParams.ID.key(), DEFAULT_COHORT), new QueryOptions(), sessionId).first(); }
public void before () throws Exception { File file = opencga.createFile(studyId, "1000g_batches/1-500.filtered.10k.chr22.phase3_shapeit2_mvncall_integrated_v5.20130502.genotypes.vcf.gz", sessionId); for (int i = 0; i < coh.length; i++) { Cohort cohort = catalogManager.getCohortManager().create(studyId, "coh" + i, Study.Type.CONTROL_SET, "", file.getSamples().subList(file.getSamples() .size() / coh.length * i, file.getSamples().size() / coh.length * (i + 1)), null, null, sessionId).first(); coh[i] = cohort.getId(); } QueryOptions queryOptions = new QueryOptions(VariantStorageEngine.Options.ANNOTATE.key(), false); queryOptions.putIfNotNull(StorageOperation.CATALOG_PATH, outputId); variantManager.index(studyId, file.getId(), createTmpOutdir(file), queryOptions, sessionId); all = catalogManager.getCohortManager().get(studyId, new Query(CohortDBAdaptor.QueryParams.ID.key(), DEFAULT_COHORT), new QueryOptions(), sessionId).first().getId(); }
@Test public void testDeleteCohortWithStats() throws Exception { QueryOptions queryOptions = new QueryOptions(VariantStorageEngine.Options.ANNOTATE.key(), false) .append(VariantStorageEngine.Options.CALCULATE_STATS.key(), true); File transformedFile = transformFile(getFile(0), queryOptions); loadFile(transformedFile, queryOptions, outputId); WriteResult result = catalogManager.getCohortManager().delete(studyFqn, new Query(CohortDBAdaptor.QueryParams.ID.key(), "ALL"), null, sessionId); assertEquals(0, result.getNumModified()); assertTrue(result.getFailed().get(0).getMessage().contains("ALL cannot be deleted")); }
@Override public QueryResult<Cohort> update(long cohortId, ObjectMap parameters, List<VariableSet> variableSetList, QueryOptions queryOptions) throws CatalogDBException { long startTime = startQuery(); update(new Query(QueryParams.UID.key(), cohortId), parameters, variableSetList, queryOptions); Query query = new Query() .append(QueryParams.UID.key(), cohortId) .append(QueryParams.STUDY_UID.key(), getStudyId(cohortId)); return endQuery("Update cohort", startTime, get(query, queryOptions)); }
public void setStatus(String studyStr, String id, String status, String message, String sessionId) throws CatalogException { MyResource resource = getUid(id, studyStr, sessionId); authorizationManager.checkCohortPermission(resource.getStudy().getUid(), resource.getResource().getUid(), resource.getUser(), CohortAclEntry.CohortPermissions.UPDATE); if (status != null && !Cohort.CohortStatus.isValid(status)) { throw new CatalogException("The status " + status + " is not valid cohort status."); } ObjectMap parameters = new ObjectMap(); parameters.putIfNotNull(CohortDBAdaptor.QueryParams.STATUS_NAME.key(), status); parameters.putIfNotNull(CohortDBAdaptor.QueryParams.STATUS_MSG.key(), message); cohortDBAdaptor.update(resource.getResource().getUid(), parameters, new QueryOptions()); auditManager.recordUpdate(AuditRecord.Resource.cohort, resource.getResource().getUid(), resource.getUser(), parameters, null, null); }
@Override public DBIterator<Cohort> iterator(String studyStr, Query query, QueryOptions options, String sessionId) throws CatalogException { options = ParamUtils.defaultObject(options, QueryOptions::new); query = ParamUtils.defaultObject(query, Query::new); String userId = userManager.getUserId(sessionId); Study study = studyManager.resolveId(studyStr, userId); fixQueryObject(study, query, sessionId); Query myQuery = new Query(query).append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); return cohortDBAdaptor.iterator(myQuery, options, userId); }
private Document getStudyDocument(Query query) throws CatalogDBException { // Get the study document Query studyQuery = new Query(StudyDBAdaptor.QueryParams.UID.key(), query.getLong(QueryParams.STUDY_UID.key())); QueryResult<Document> queryResult = dbAdaptorFactory.getCatalogStudyDBAdaptor().nativeGet(studyQuery, QueryOptions.empty()); if (queryResult.getNumResults() == 0) { throw new CatalogDBException("Study " + query.getLong(QueryParams.STUDY_UID.key()) + " not found"); } return queryResult.first(); }
@Override public QueryResult<Cohort> get(long cohortId, QueryOptions options) throws CatalogDBException { Query query = new Query() .append(QueryParams.UID.key(), cohortId) .append(QueryParams.STUDY_UID.key(), getStudyId(cohortId)) .append(QueryParams.STATUS_NAME.key(), "!=" + Status.DELETED); return get(query, options); }
@Override public long getStudyId(long cohortId) throws CatalogDBException { checkId(cohortId); QueryResult queryResult = nativeGet(new Query(QueryParams.UID.key(), cohortId), new QueryOptions(MongoDBCollection.INCLUDE, PRIVATE_STUDY_ID)); if (queryResult.getResult().isEmpty()) { throw CatalogDBException.uidNotFound("Cohort", cohortId); } else { return ((Document) queryResult.first()).getLong(PRIVATE_STUDY_ID); } }
@Override public QueryResult<Long> restore(Query query, QueryOptions queryOptions) throws CatalogDBException { long startTime = startQuery(); query.put(QueryParams.STATUS_NAME.key(), Status.DELETED); return endQuery("Restore cohorts", startTime, setStatus(query, Cohort.CohortStatus.NONE)); }
private void removeStudy(Object study, QueryOptions options) throws Exception { variantManager.removeStudy(study.toString(), sessionId, new QueryOptions()); Query query = new Query(FileDBAdaptor.QueryParams.INDEX_STATUS_NAME.key(), FileIndex.IndexStatus.READY); assertEquals(0L, catalogManager.getFileManager().count(study.toString(), query, sessionId).getNumTotalResults()); Cohort all = catalogManager.getCohortManager().get(studyId, new Query(CohortDBAdaptor.QueryParams.ID.key(), StudyEntry.DEFAULT_COHORT), null, sessionId).first(); assertTrue(all.getSamples().isEmpty()); }
private String updateDefaultCohortStatus(Study study, String status, String sessionId) throws CatalogException { Query query = new Query(CohortDBAdaptor.QueryParams.ID.key(), StudyEntry.DEFAULT_COHORT); Cohort defaultCohort = catalogManager.getCohortManager().get(study.getFqn(), query, new QueryOptions(), sessionId).first(); String prevStatus = defaultCohort.getStatus().getName(); catalogManager.getCohortManager().setStatus(study.getFqn(), defaultCohort.getId(), status, null, sessionId); return prevStatus; }
@Override public Document convertToStorageType(Cohort object, List<VariableSet> variableSetList) { Document document = super.convertToStorageType(object, variableSetList); document.remove(CohortDBAdaptor.QueryParams.ANNOTATION_SETS.key()); document.put("uid", object.getUid()); document.put("studyUid", object.getStudyUid()); document.put("samples", convertSamplesToDocument(object.getSamples())); return document; }
@Override public void delete(long id) throws CatalogDBException { Query query = new Query(QueryParams.UID.key(), id); delete(query); }