@Override public QueryResult<Sample> create(String studyStr, Sample sample, QueryOptions options, String sessionId) throws CatalogException { ParamUtils.checkAlias(sample.getId(), "name"); sample.setSource(ParamUtils.defaultString(sample.getSource(), "")); sample.setDescription(ParamUtils.defaultString(sample.getDescription(), "")); sample.setType(ParamUtils.defaultString(sample.getType(), "")); sample.setPhenotypes(ParamUtils.defaultObject(sample.getPhenotypes(), Collections.emptyList())); sample.setAnnotationSets(ParamUtils.defaultObject(sample.getAnnotationSets(), Collections.emptyList())); sample.setStats(ParamUtils.defaultObject(sample.getStats(), Collections.emptyMap())); sample.setAttributes(ParamUtils.defaultObject(sample.getAttributes(), Collections.emptyMap())); sample.setStatus(new Status()); sample.setCreationDate(TimeUtils.getTime()); sample.setVersion(1); List<VariableSet> variableSetList = validateNewAnnotationSetsAndExtractVariableSets(study.getUid(), sample.getAnnotationSets()); if (sample.getIndividual() != null && StringUtils.isNotEmpty(sample.getIndividual().getId())) { try { QueryResult<Individual> individualQueryResult = catalogManager.getIndividualManager().get(studyStr, sample.getIndividual().getId(), new QueryOptions(QueryOptions.INCLUDE, Arrays.asList( IndividualDBAdaptor.QueryParams.UID.key(), sample.setRelease(catalogManager.getStudyManager().getCurrentRelease(study, userId)); sample.setUuid(UUIDUtils.generateOpenCGAUUID(UUIDUtils.Entity.SAMPLE)); QueryResult<Sample> queryResult = sampleDBAdaptor.insert(study.getUid(), sample, variableSetList, options); auditManager.recordCreation(AuditRecord.Resource.sample, queryResult.first().getUid(), userId, queryResult.first(), null, null); sample.getIndividual().setSamples(Collections.singletonList(queryResult.first())); try {
@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())); }
@Test public void testAvoidDuplicatedSamples() throws CatalogDBException { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); Sample sample1 = catalogDBAdaptor.getCatalogSampleDBAdaptor().insert(studyId, new Sample().setId("sample1").setStatus(new Status()), QueryOptions.empty()).first(); Sample sample2 = catalogDBAdaptor.getCatalogSampleDBAdaptor().insert(studyId, new Sample().setId("sample2").setStatus(new Status()), QueryOptions.empty()).first(); Individual individual = new Individual() .setName("in2") .setStatus(new Status()) .setSamples(Arrays.asList(sample1, sample1, sample2, new Sample().setUid(-1).setStatus(new Status()))); Individual individualStored = catalogIndividualDBAdaptor.insert(studyId, individual, null).first(); assertEquals(2, individualStored.getSamples().size()); assertTrue(individualStored.getSamples().stream().map(Sample::getUid).collect(Collectors.toSet()).containsAll(Arrays.asList( sample1.getUid(), sample2.getUid()))); // Update samples ObjectMap params = new ObjectMap(IndividualDBAdaptor.QueryParams.SAMPLES.key(), individual.getSamples()); Individual update = catalogIndividualDBAdaptor.update(individualStored.getUid(), params, QueryOptions.empty()).first(); assertEquals(2, update.getSamples().size()); assertTrue(update.getSamples().stream().map(Sample::getUid).collect(Collectors.toSet()).containsAll(Arrays.asList(sample1.getUid(), sample2.getUid()))); }
@Deprecated public QueryResult<Sample> create(String studyStr, String name, String source, String description, String type, boolean somatic, Individual individual, Map<String, Object> stats, Map<String, Object> attributes, QueryOptions options, String sessionId) throws CatalogException { Sample sample = new Sample(name, source, individual, null, null, -1, 1, description, type, somatic, Collections.emptyList(), Collections.emptyList(), attributes).setStats(stats); return create(studyStr, sample, options, sessionId); }
public void validateSamplesToUpdate(Document document) { List<Document> samples = (List) document.get("samples"); if (samples != null) { // We make sure we don't store duplicates Map<Long, Sample> sampleMap = new HashMap<>(); for (Document sample : samples) { long id = sample.getInteger("uid").longValue(); int version = sample.getInteger("version"); if (id > 0) { Sample tmpSample = new Sample() .setVersion(version); tmpSample.setUid(id); sampleMap.put(id, tmpSample); } } document.put("samples", sampleMap.entrySet().stream() .map(entry -> new Document() .append("uid", entry.getValue().getUid()) .append("version", entry.getValue().getVersion())) .collect(Collectors.toList())); } }
@Override public Document convertToStorageType(Sample object, List<VariableSet> variableSetList) { Document document = super.convertToStorageType(object, variableSetList); document.remove(SampleDBAdaptor.QueryParams.ANNOTATION_SETS.key()); document.put("uid", object.getUid()); document.put("studyUid", object.getStudyUid()); document.put("individual", new Document()); return document; }
setPopulation(new Individual.Population("valencian", "", "")); Sample sample = new Sample(); sample.setUid(500).setSource("Lab").setRelease(3).setVersion(2).setStatus(new Status("READY")) .setType("Sample").setSomatic(true).setCreationDate(TimeUtils.getTime()) .setAnnotationSets(AnnotationHelper.createAnnotation()); sample.setAttributes(attributes); assertEquals(sampleSolrModel.getUid(), sample.getUid()); assertEquals(sampleSolrModel.getSource(), sample.getSource()); assertEquals(sampleSolrModel.getRelease(), sample.getRelease()); assertEquals(sampleSolrModel.getVersion(), sample.getVersion()); assertEquals(sampleSolrModel.getStatus(), sample.getStatus().getName()); assertEquals(sampleSolrModel.getType(), sample.getType()); assertEquals(sampleSolrModel.isSomatic(), sample.isSomatic()); assertEquals(sampleSolrModel.getPhenotypes().size(), 0);
sampleSolrModel.setUid(sample.getUid()); sampleSolrModel.setSource(sample.getSource()); sampleSolrModel.setStudyId(study.getFqn().replace(":", "__")); sampleSolrModel.setRelease(sample.getRelease()); sampleSolrModel.setVersion(sample.getVersion()); Date date = TimeUtils.toDate(sample.getCreationDate()); LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); sampleSolrModel.setCreationDay(localDate.getDayOfMonth()); sampleSolrModel.setCreationDayOfWeek(localDate.getDayOfWeek().toString()); sampleSolrModel.setStatus(sample.getStatus().getName()); sampleSolrModel.setStatus(sample.getStatus().getName()); sampleSolrModel.setType(sample.getType()); sampleSolrModel.setSomatic(sample.isSomatic()); if (sample.getPhenotypes() != null) { sampleSolrModel.setPhenotypes(SolrConverterUtil.populatePhenotypes(sample.getPhenotypes())); sampleSolrModel.setAnnotations(SolrConverterUtil.populateAnnotations(variableMap, sample.getAnnotationSets())); if (sample.getAnnotationSets() != null) { sampleSolrModel.setAnnotationSets(sample.getAnnotationSets().stream().map(AnnotationSet::getId).collect(Collectors.toList())); } else { sampleSolrModel.setAnnotationSets(Collections.emptyList()); if (sample.getCollection() != null) { sampleSolrModel.setTissue(StringUtils.defaultIfEmpty(sample.getCollection().getTissue(), ""));
@Test public void testAnnotateUnique() throws CatalogException { String sampleId = catalogManager.getSampleManager().create(studyFqn, new Sample().setId("SAMPLE_1"), new QueryOptions(), sessionIdUser).first().getId(); QueryResult<Sample> sampleQueryResult = catalogManager.getSampleManager().get(studyFqn, sampleId, new QueryOptions(QueryOptions.INCLUDE, SampleDBAdaptor.QueryParams.ANNOTATION_SETS.key()), sessionIdUser); assertEquals(1, sampleQueryResult.first().getAnnotationSets().size());
@Test public void getSampleWithIndividual() throws CatalogDBException { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); QueryOptions queryOptions = new QueryOptions(); // We create a new sample with the individual Sample sample = new Sample().setId("sample1").setStatus(new Status()); QueryResult<Sample> sampleQR = catalogSampleDBAdaptor.insert(studyId, sample, queryOptions); long sampleId = sampleQR.first().getUid(); // We create an individual String individualName = "individualName"; Individual individual = new Individual() .setId(individualName) .setStatus(new Status()) .setSamples(Arrays.asList(new Sample().setUid(sampleId).setVersion(1).setStatus(new Status()))); dbAdaptorFactory.getCatalogIndividualDBAdaptor().insert(studyId, individual, queryOptions); // Get the sample Query query = new Query() .append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), studyId) .append(SampleDBAdaptor.QueryParams.INDIVIDUAL.key(), "individualName"); QueryResult<Sample> individualQuery = catalogSampleDBAdaptor.get(query, queryOptions); assertEquals("sample1", individualQuery.first().getId()); query = new Query() .append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), studyId) .append(SampleDBAdaptor.QueryParams.INDIVIDUAL.key(), "non-existing"); QueryResult<Sample> inexistentIndividualQuery = catalogSampleDBAdaptor.get(query, queryOptions); assertEquals(0, inexistentIndividualQuery.getNumResults()); }
filterList.add(Filters.eq(QueryParams.ID.key(), sample.getId())); filterList.add(Filters.eq(PRIVATE_STUDY_ID, studyId)); filterList.add(Filters.eq(QueryParams.STATUS_NAME.key(), Status.READY)); throw new CatalogDBException("Sample { id: '" + sample.getId() + "'} already exists."); sample.setUid(sampleId); sample.setStudyUid(studyId); sample.setVersion(1); if (StringUtils.isEmpty(sample.getUuid())) { sample.setUuid(UUIDUtils.generateOpenCGAUUID(UUIDUtils.Entity.SAMPLE)); sampleObject.put(RELEASE_FROM_VERSION, Arrays.asList(sample.getRelease())); sampleObject.put(LAST_OF_VERSION, true); sampleObject.put(LAST_OF_RELEASE, true); if (StringUtils.isNotEmpty(sample.getCreationDate())) { sampleObject.put(PRIVATE_CREATION_DATE, TimeUtils.toDate(sample.getCreationDate())); } else { sampleObject.put(PRIVATE_CREATION_DATE, TimeUtils.getDate());
new Phenotype("go:123", "My go term", "go", Phenotype.Status.UNKNOWN) ); Sample sample1 = new Sample().setId("sample1").setPhenotypes(ontologyList).setStatus(new Status()); new Phenotype("go:yyy", "My go term", "go", Phenotype.Status.UNKNOWN) ); Sample sample2 = new Sample().setId("sample2").setPhenotypes(ontologyList).setStatus(new Status()); catalogSampleDBAdaptor.insert(studyId, sample1, new QueryOptions()).first().getUid(); catalogSampleDBAdaptor.insert(studyId, sample2, new QueryOptions()).first().getUid(); assertEquals(sample2.getId(), sampleQueryResult.first().getId()); sampleQueryResult = catalogSampleDBAdaptor.get(query, new QueryOptions()); assertEquals(1, sampleQueryResult.getNumResults()); assertEquals(sample1.getId(), sampleQueryResult.first().getId());
@Test public void readSampleForbiddenForExternalUser() throws CatalogException { catalogManager.getSampleManager().updateAcl(String.valueOf(studyFqn), Arrays.asList(smp2.getId()), externalUser, new Sample.SampleAclParams("", AclParams.Action.SET, null, null, null), ownerSessionId); thrown.expect(CatalogAuthorizationException.class); catalogManager.getSampleManager().get(studyFqn, smp2.getId(), null, externalSessionId); }
@Test public void readSampleExplicitUnshared() throws CatalogException { QueryResult<Sample> sample = catalogManager.getSampleManager().get(studyFqn, smp1.getId(), null, externalSessionId); assertEquals(1, sample.getNumResults()); catalogManager.getAuthorizationManager().removeAcls(Collections.singletonList(smp1.getUid()), Collections.singletonList(externalUser), null, Entity.SAMPLE); thrown.expect(CatalogAuthorizationException.class); catalogManager.getSampleManager().get(studyFqn, smp1.getId(), null, externalSessionId); }
/** * 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 testCreateSampleWithDotInName() throws CatalogException { String name = "HG007.sample"; QueryResult<Sample> sampleQueryResult = catalogManager.getSampleManager().create(studyFqn, new Sample().setId(name), null, sessionIdUser); assertEquals(name, sampleQueryResult.first().getId()); }
@Test public void testModifySample() throws CatalogException { String sampleId1 = catalogManager.getSampleManager() .create(studyFqn, new Sample().setId("SAMPLE_1"), new QueryOptions(), sessionIdUser).first().getId(); String individualId = catalogManager.getIndividualManager().create(studyFqn, new Individual().setId("Individual1"), new QueryOptions(), sessionIdUser).first().getId(); Sample sample = catalogManager.getSampleManager() .update(studyFqn, sampleId1, new ObjectMap(SampleDBAdaptor.QueryParams.INDIVIDUAL.key(), individualId), new QueryOptions("lazy", false), sessionIdUser).first(); assertEquals(individualId, ((Individual) sample.getAttributes().get("individual")).getId()); }
Sample sample = catalogManager.getSampleManager().create(studyStr, entry.getKey(), source, description, null, false, null, new HashMap<>(), Collections.emptyMap(), QueryOptions.empty(), sessionId).first(); samplesMap.put(sample.getId(), (int) sample.getUid()); samplesIdMap.put(entry.getValue(), (int) sample.getUid()); for (Integer sampleId : sampleIds) { if (samplesIdMap.containsKey(sampleId)) { newSampleList.add(new Sample().setUid(samplesIdMap.get(sampleId))); .get(oldFileId) .stream() .map(integer -> new Sample().setUid(((long) integer))) .collect(Collectors.toList());
sessionId).getResult(); for (Sample catalogSample : catalogSamples) { org.opencb.biodata.models.metadata.Sample sample = samplesMap.get(catalogSample.getId()); List<AnnotationSet> annotationSets = catalogSample.getAnnotationSets(); if (annotationSets != null) { sample.setAnnotations(new LinkedHashMap<>(sample.getAnnotations()));
@Test public void createSampleTest() throws Exception { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); Sample hg0097 = new Sample("HG0097", "1000g", new Individual(), "A description", 1); QueryResult<Sample> result = dbAdaptorFactory.getCatalogSampleDBAdaptor().insert(studyId, hg0097, null); assertEquals(hg0097.getId(), result.first().getId()); assertEquals(hg0097.getDescription(), result.first().getDescription()); assertTrue(result.first().getUid() > 0); }