private List<String> createCohortsIfNeeded(String studyId, Set<String> cohortNames, String sessionId) throws CatalogException { List<String> cohorts = new ArrayList<>(); // Silent query, so it does not fail for missing cohorts Set<String> catalogCohorts = catalogManager.getCohortManager().get(studyId, new ArrayList<>(cohortNames), new Query(), new QueryOptions(QueryOptions.INCLUDE, "name,id"), true, sessionId) .stream() .map(QueryResult::first) .filter(Objects::nonNull) .map(Cohort::getId) .collect(Collectors.toSet()); for (String cohortName : cohortNames) { if (!catalogCohorts.contains(cohortName)) { QueryResult<Cohort> cohort = catalogManager.getCohortManager().create(studyId, cohortName, Study.Type.COLLECTION, "", Collections.emptyList(), null, null, sessionId); logger.info("Creating cohort {}", cohortName); cohorts.add(cohort.first().getId()); } else { logger.debug("cohort {} was already created", cohortName); cohorts.add(cohortName); } } return cohorts; }
static CatalogException unableToCalculateCohortReady(Cohort cohort) { return new CatalogException("Unable to calculate stats for cohort " + "{ uid: " + cohort.getUid() + " id: \"" + cohort.getId() + "\" }" + " with status \"" + cohort.getStatus().getName() + "\". " + "Resume or update stats for continue calculation"); }
static CatalogException unableToCalculateCohortCalculating(Cohort cohort) { return new CatalogException("Unable to calculate stats for cohort " + "{ uid: " + cohort.getUid() + " id: \"" + cohort.getId() + "\" }" + " with status \"" + cohort.getStatus().getName() + "\". " + "Resume for continue calculation."); }
public void checkCohortCanBeDeleted(Cohort cohort) throws CatalogException { // Check if the cohort is different from DEFAULT_COHORT if (StudyEntry.DEFAULT_COHORT.equals(cohort.getId())) { throw new CatalogException("Cohort " + StudyEntry.DEFAULT_COHORT + " cannot be deleted."); } // Check if the cohort can be deleted if (cohort.getStatus() != null && cohort.getStatus().getName() != null && !cohort.getStatus().getName().equals(Cohort.CohortStatus.NONE)) { throw new CatalogException("The cohort is used in storage."); } }
@Deprecated public void checkInUse(long sampleId) throws CatalogDBException { long studyId = getStudyId(sampleId); Query query = new Query(FileDBAdaptor.QueryParams.SAMPLE_UIDS.key(), sampleId); QueryOptions queryOptions = new QueryOptions(MongoDBCollection.INCLUDE, Arrays.asList(FILTER_ROUTE_FILES + FileDBAdaptor .QueryParams.UID.key(), FILTER_ROUTE_FILES + FileDBAdaptor.QueryParams.PATH.key())); QueryResult<File> fileQueryResult = dbAdaptorFactory.getCatalogFileDBAdaptor().get(query, queryOptions); if (fileQueryResult.getNumResults() != 0) { String msg = "Can't delete Sample " + sampleId + ", still in use in \"sampleId\" array of files : " + fileQueryResult.getResult().stream() .map(file -> "{ id: " + file.getUid() + ", path: \"" + file.getPath() + "\" }") .collect(Collectors.joining(", ", "[", "]")); throw new CatalogDBException(msg); } queryOptions = new QueryOptions(CohortDBAdaptor.QueryParams.SAMPLES.key(), sampleId) .append(MongoDBCollection.INCLUDE, Arrays.asList(FILTER_ROUTE_COHORTS + CohortDBAdaptor.QueryParams.UID.key(), FILTER_ROUTE_COHORTS + CohortDBAdaptor.QueryParams.ID.key())); QueryResult<Cohort> cohortQueryResult = dbAdaptorFactory.getCatalogCohortDBAdaptor().getAllInStudy(studyId, queryOptions); if (cohortQueryResult.getNumResults() != 0) { String msg = "Can't delete Sample " + sampleId + ", still in use in cohorts : " + cohortQueryResult.getResult().stream() .map(cohort -> "{ id: " + cohort.getUid() + ", name: \"" + cohort.getId() + "\" }") .collect(Collectors.joining(", ", "[", "]")); throw new CatalogDBException(msg); } }
for (String cohort : cohorts) { String cohortId = catalogManager.getCohortManager().get(study.getFqn(), cohort, new QueryOptions(QueryOptions.INCLUDE, CohortDBAdaptor.QueryParams.ID.key()), sessionId).first().getId(); cohortIds.add(cohortId);
throw new IllegalStateException("Unknown status " + cohort.getStatus().getName()); cohortMap.put(cohort.getId(), cohort.getSamples().stream().map(Sample::getId).collect(Collectors.toList()));
public void calculateAggregatedStats(QueryOptions options) throws Exception { // coh0 = catalogManager.createCohort(studyId, "ALL", Cohort.Type.COLLECTION, "", file.getSampleIds(), null, sessionId).first().getId(); String cohId = catalogManager.getCohortManager().get(studyId, (Query) null, null, sessionId).first().getId(); calculateStats(cohId, options); checkCalculatedAggregatedStats(Collections.singleton(DEFAULT_COHORT), dbName); }
public static void checkCalculatedStats(String studyId, Map<String, Cohort> cohorts, CatalogManager catalogManager, String dbName, String sessionId) throws Exception { VariantDBAdaptor dbAdaptor = StorageEngineFactory.get().getVariantStorageEngine(null, dbName).getDBAdaptor(); for (Variant variant : dbAdaptor) { for (StudyEntry sourceEntry : variant.getStudies()) { assertEquals("In variant " + variant.toString(), cohorts.size(), sourceEntry.getStats().size()); for (Map.Entry<String, VariantStats> entry : sourceEntry.getStats().entrySet()) { assertTrue("In variant " + variant.toString(), cohorts.containsKey(entry.getKey())); if (cohorts.get(entry.getKey()) != null) { assertEquals("Variant: " + variant.toString() + " does not have the correct number of samples in cohort '" + entry.getKey() + "'. jsonVariant: " + variant.toJson() , cohorts.get(entry.getKey()).getSamples().size(), entry.getValue().getGenotypeCount().values().stream().reduce(Integer::sum).orElse(0).intValue()); } } } } for (Cohort cohort : cohorts.values()) { cohort = catalogManager.getCohortManager().get(studyId, cohort.getId(), null, sessionId).first(); assertEquals(Cohort.CohortStatus.READY, cohort.getStatus().getName()); } }
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 QueryResult<Cohort> insert(long studyId, Cohort cohort, List<VariableSet> variableSetList, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); dbAdaptorFactory.getCatalogStudyDBAdaptor().checkId(studyId); checkCohortNameExists(studyId, cohort.getId()); long newId = dbAdaptorFactory.getCatalogMetaDBAdaptor().getNewAutoIncrementId(); cohort.setUid(newId); cohort.setStudyUid(studyId); if (StringUtils.isEmpty(cohort.getUuid())) { cohort.setUuid(UUIDUtils.generateOpenCGAUUID(UUIDUtils.Entity.COHORT)); } Document cohortObject = cohortConverter.convertToStorageType(cohort, variableSetList); if (StringUtils.isNotEmpty(cohort.getCreationDate())) { cohortObject.put(PRIVATE_CREATION_DATE, TimeUtils.toDate(cohort.getCreationDate())); } else { cohortObject.put(PRIVATE_CREATION_DATE, TimeUtils.getDate()); } cohortObject.put(PERMISSION_RULES_APPLIED, Collections.emptyList()); try { cohortCollection.insert(cohortObject, null); } catch (MongoWriteException e) { throw ifDuplicateKeyException(() -> CatalogDBException.alreadyExists("Cohort", studyId, "name", cohort.getId(), e), e); } Query query = new Query() .append(QueryParams.UID.key(), newId) .append(QueryParams.STUDY_UID.key(), studyId); return endQuery("createCohort", startTime, get(query, options)); }
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(); }
/** * Sample methods * *************************** */ /* * Cohort methods * */ @Test public void testCreateCohort() throws CatalogException { Sample sampleId1 = catalogManager.getSampleManager().create(studyFqn, new Sample().setId("SAMPLE_1"), new QueryOptions(), sessionIdUser).first(); Sample sampleId2 = catalogManager.getSampleManager().create(studyFqn, new Sample().setId("SAMPLE_2"), new QueryOptions(), sessionIdUser).first(); Sample sampleId3 = catalogManager.getSampleManager().create(studyFqn, new Sample().setId("SAMPLE_3"), new QueryOptions(), sessionIdUser).first(); Cohort myCohort = catalogManager.getCohortManager().create(studyFqn, new Cohort().setId("MyCohort") .setSamples(Arrays.asList(sampleId1, sampleId2, sampleId3)), null, sessionIdUser).first(); assertEquals("MyCohort", myCohort.getId()); assertEquals(3, myCohort.getSamples().size()); assertTrue(myCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId1.getUid())); assertTrue(myCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId2.getUid())); assertTrue(myCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId3.getUid())); }
@Test public void testUpdateCohort() throws CatalogException { Sample sampleId1 = catalogManager.getSampleManager().create(studyFqn, new Sample().setId("SAMPLE_1"), new QueryOptions(), sessionIdUser).first(); Sample sampleId2 = catalogManager.getSampleManager().create(studyFqn, new Sample().setId("SAMPLE_2"), new QueryOptions(), sessionIdUser).first(); Sample sampleId3 = catalogManager.getSampleManager().create(studyFqn, new Sample().setId("SAMPLE_3"), new QueryOptions(), sessionIdUser).first(); Sample sampleId4 = catalogManager.getSampleManager().create(studyFqn, new Sample().setId("SAMPLE_4"), new QueryOptions(), sessionIdUser).first(); Sample sampleId5 = catalogManager.getSampleManager().create(studyFqn, new Sample().setId("SAMPLE_5"), new QueryOptions(), sessionIdUser).first(); Cohort myCohort = catalogManager.getCohortManager().create(studyFqn, new Cohort().setId("MyCohort").setType(Study.Type.FAMILY) .setSamples(Arrays.asList(sampleId1, sampleId2, sampleId3)), null, sessionIdUser).first(); assertEquals("MyCohort", myCohort.getId()); assertEquals(3, myCohort.getSamples().size()); assertTrue(myCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId1.getUid())); assertTrue(myCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId2.getUid())); assertTrue(myCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId3.getUid())); Cohort myModifiedCohort = catalogManager.getCohortManager().update(studyFqn, myCohort.getId(), new ObjectMap("samples", Arrays.asList(sampleId1.getId(), sampleId3.getId(), sampleId4.getId(), sampleId5.getId())) .append(CohortDBAdaptor.QueryParams.ID.key(), "myModifiedCohort"), new QueryOptions(), sessionIdUser).first(); assertEquals("myModifiedCohort", myModifiedCohort.getId()); assertEquals(4, myModifiedCohort.getSamples().size()); assertTrue(myModifiedCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId1.getUid())); assertTrue(myModifiedCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId3.getUid())); assertTrue(myModifiedCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId4.getUid())); assertTrue(myModifiedCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId5.getUid())); }
if (StudyEntry.DEFAULT_COHORT.equals(cohort.getId())) { throw new CatalogException("Cannot modify cohort " + StudyEntry.DEFAULT_COHORT);
String fileErrorMsg = dbAdaptorFactory.getCatalogCohortDBAdaptor().insert(study.getUid(), cohort, options).getErrorMsg(); if (fileErrorMsg != null && !fileErrorMsg.isEmpty()) { errorMsg += cohort.getId() + ":" + fileErrorMsg + ", ";
@Test public void testDeleteCohort() throws CatalogException, IOException { String studyId = "user@1000G:phase1"; Sample sampleId1 = catalogManager.getSampleManager().create(studyId, new Sample().setId("SAMPLE_1"), new QueryOptions(), sessionIdUser).first(); Sample sampleId2 = catalogManager.getSampleManager().create(studyId, new Sample().setId("SAMPLE_2"), new QueryOptions(), sessionIdUser).first(); Sample sampleId3 = catalogManager.getSampleManager().create(studyId, new Sample().setId("SAMPLE_3"), new QueryOptions(), sessionIdUser).first(); Cohort myCohort = catalogManager.getCohortManager().create(studyId, new Cohort().setId("MyCohort").setType(Study.Type.FAMILY) .setSamples(Arrays.asList(sampleId1, sampleId2, sampleId3)), null, sessionIdUser).first(); assertEquals("MyCohort", myCohort.getId()); assertEquals(3, myCohort.getSamples().size()); assertTrue(myCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId1.getUid())); assertTrue(myCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId2.getUid())); assertTrue(myCohort.getSamples().stream().map(Sample::getUid).collect(Collectors.toList()).contains(sampleId3.getUid())); WriteResult deleteResult = catalogManager.getCohortManager().delete(studyId, new Query(CohortDBAdaptor.QueryParams.UID.key(), myCohort.getUid()), null, sessionIdUser); assertEquals(1, deleteResult.getNumModified()); Query query = new Query() .append(CohortDBAdaptor.QueryParams.UID.key(), myCohort.getUid()) .append(CohortDBAdaptor.QueryParams.STATUS_NAME.key(), "!=" + Cohort.CohortStatus.READY); Cohort cohort = catalogManager.getCohortManager().get(studyId, query, null, sessionIdUser).first(); assertEquals(Status.DELETED, cohort.getStatus().getName()); }
ParamUtils.checkParameter(cohort.getId(), "name"); ParamUtils.checkObj(cohort.getSamples(), "Sample list"); cohort.setType(ParamUtils.defaultObject(cohort.getType(), Study.Type.COLLECTION));
cohortId = cohort.getId(); cohortUid = cohort.getUid(); files.add(create("platinum/1K.end.platinum-genomes-vcf-NA12877_S1.genome.vcf.gz"));
String msg = "Can't delete VariableSetId, still in use as \"variableSetId\" of cohorts : ["; for (Cohort cohort : cohorts.getResult()) { msg += " { id: " + cohort.getUid() + ", name: \"" + cohort.getId() + "\" },";