QueryResult<Sample> insert(long studyId, Sample sample, List<VariableSet> variableSetList, QueryOptions options) throws CatalogDBException;
public DBIterator<Sample> indexSolr(Query query) throws CatalogException { return sampleDBAdaptor.iterator(query, null, null); } }
private long getSampleId(boolean silent, String sampleStrAux) throws CatalogException { long sampleId = Long.parseLong(sampleStrAux); try { sampleDBAdaptor.checkId(sampleId); } catch (CatalogException e) { if (silent) { return -1L; } else { throw e; } } return sampleId; }
.setCreationDate(TimeUtils.getTime(calendar.getTime())); dbAdaptorFactory.getCatalogSampleDBAdaptor().insert(studyId, sample1, null); dbAdaptorFactory.getCatalogSampleDBAdaptor().insert(studyId, sample2, null); QueryResult<Sample> sampleQueryResult = dbAdaptorFactory.getCatalogSampleDBAdaptor().get(query, options); assertEquals(1, sampleQueryResult.getNumResults()); assertEquals("sample2", sampleQueryResult.first().getId()); sampleQueryResult = dbAdaptorFactory.getCatalogSampleDBAdaptor().get(query, options); assertEquals(2, sampleQueryResult.getNumResults()); sampleQueryResult = dbAdaptorFactory.getCatalogSampleDBAdaptor().get(query, options); assertEquals(1, sampleQueryResult.getNumResults()); assertEquals("sample1", sampleQueryResult.first().getId()); sampleQueryResult = dbAdaptorFactory.getCatalogSampleDBAdaptor().get(query, options); assertEquals(2, sampleQueryResult.getNumResults()); sampleQueryResult = dbAdaptorFactory.getCatalogSampleDBAdaptor().get(query, options); assertEquals(1, sampleQueryResult.getNumResults()); assertEquals("sample1", sampleQueryResult.first().getId());
@Test public void deleteSampleTest() 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> createResult = dbAdaptorFactory.getCatalogSampleDBAdaptor().insert(studyId, hg0097, null); dbAdaptorFactory.getCatalogSampleDBAdaptor().delete(createResult.first().getUid()); thrown.expect(CatalogDBException.class); thrown.expectMessage("does not exist"); catalogSampleDBAdaptor.get(createResult.first().getUid(), QueryOptions.empty()); }
@Override Sample smartResolutor(long studyUid, String entry, String user) throws CatalogException { Query query = new Query() .append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), studyUid); if (UUIDUtils.isOpenCGAUUID(entry)) { query.put(SampleDBAdaptor.QueryParams.UUID.key(), entry); } else { query.put(SampleDBAdaptor.QueryParams.ID.key(), entry); } QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, Arrays.asList( SampleDBAdaptor.QueryParams.UUID.key(), SampleDBAdaptor.QueryParams.UID.key(), SampleDBAdaptor.QueryParams.STUDY_UID.key(), SampleDBAdaptor.QueryParams.ID.key(), SampleDBAdaptor.QueryParams.RELEASE.key(), SampleDBAdaptor.QueryParams.VERSION.key(), SampleDBAdaptor.QueryParams.STATUS.key())); QueryResult<Sample> sampleQueryResult = sampleDBAdaptor.get(query, options, user); if (sampleQueryResult.getNumResults() == 0) { sampleQueryResult = sampleDBAdaptor.get(query, options); if (sampleQueryResult.getNumResults() == 0) { throw new CatalogException("Sample " + entry + " not found"); } else { throw new CatalogAuthorizationException("Permission denied. " + user + " is not allowed to see the sample " + entry); } } else if (sampleQueryResult.getNumResults() > 1) { throw new CatalogException("More than one sample found based on " + entry); } else { return sampleQueryResult.first(); } }
@Test @Ignore // TODO: This should be tested in the sample manager, not here !!! public void deleteSampleFail3Test() 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> createResult = dbAdaptorFactory.getCatalogSampleDBAdaptor().insert(studyId, hg0097, null); dbAdaptorFactory.getCatalogCohortDBAdaptor().insert(studyId, new Cohort("Cohort", Study.Type.COLLECTION, "", "", Collections.singletonList(createResult.first()), 1, null), null); thrown.expect(CatalogDBException.class); dbAdaptorFactory.getCatalogSampleDBAdaptor().delete(createResult.first().getUid()); }
finalQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); iterator = sampleDBAdaptor.iterator(finalQuery, QueryOptions.empty(), userId); .append(SampleDBAdaptor.QueryParams.STATUS_NAME.key(), Status.DELETED) .append(SampleDBAdaptor.QueryParams.ID.key(), sample.getId() + suffixName); QueryResult<Long> update = sampleDBAdaptor.update(updateQuery, updateParams, QueryOptions.empty()); if (update.first() > 0) { numModified += 1;
default boolean exists(long sampleId) throws CatalogDBException { return count(new Query(QueryParams.UID.key(), sampleId)).first() > 0; }
default void checkId(long sampleId) throws CatalogDBException { if (sampleId < 0) { throw CatalogDBException.newInstance("Sample id '{}' is not valid: ", sampleId); } if (!exists(sampleId)) { throw CatalogDBException.newInstance("Sample id '{}' does not exist", sampleId); } }
@Override public QueryResult groupBy(@Nullable String studyStr, Query query, List<String> fields, QueryOptions options, String sessionId) throws CatalogException { query = ParamUtils.defaultObject(query, Query::new); options = ParamUtils.defaultObject(options, QueryOptions::new); if (fields == null || fields.size() == 0) { throw new CatalogException("Empty fields parameter."); } String userId = userManager.getUserId(sessionId); Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); // Add study id to the query query.put(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); QueryResult queryResult = sampleDBAdaptor.groupBy(query, fields, options, userId); return ParamUtils.defaultObject(queryResult, QueryResult::new); }
@Test public void deleteSampleFail1Test() throws Exception { thrown.expect(CatalogDBException.class); dbAdaptorFactory.getCatalogSampleDBAdaptor().delete(55555555); }
public QueryResult<VariableSetSummary> getVariableSetSummary(String studyStr, String variableSetStr, String sessionId) throws CatalogException { MyResourceId resource = getVariableSetId(variableSetStr, studyStr, sessionId); String userId = resource.getUser(); QueryResult<VariableSet> variableSet = studyDBAdaptor.getVariableSet(resource.getResourceId(), new QueryOptions(), userId); if (variableSet.getNumResults() == 0) { logger.error("getVariableSetSummary: Could not find variable set id {}. {} results returned", variableSetStr, variableSet.getNumResults()); throw new CatalogDBException("Variable set " + variableSetStr + " not found."); } int dbTime = 0; VariableSetSummary variableSetSummary = new VariableSetSummary(resource.getResourceId(), variableSet.first().getId()); QueryResult<VariableSummary> annotationSummary = sampleDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); dbTime += annotationSummary.getDbTime(); variableSetSummary.setSamples(annotationSummary.getResult()); annotationSummary = cohortDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); dbTime += annotationSummary.getDbTime(); variableSetSummary.setCohorts(annotationSummary.getResult()); annotationSummary = individualDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); dbTime += annotationSummary.getDbTime(); variableSetSummary.setIndividuals(annotationSummary.getResult()); annotationSummary = familyDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); dbTime += annotationSummary.getDbTime(); variableSetSummary.setFamilies(annotationSummary.getResult()); return new QueryResult<>("Variable set summary", dbTime, 1, 1, "", "", Arrays.asList(variableSetSummary)); }
@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()); }
@Override public QueryResult<Sample> search(String studyStr, Query query, QueryOptions options, String sessionId) throws CatalogException { query = ParamUtils.defaultObject(query, Query::new); options = ParamUtils.defaultObject(options, QueryOptions::new); String userId = userManager.getUserId(sessionId); Study study = catalogManager.getStudyManager().resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, query); AnnotationUtils.fixQueryOptionAnnotation(options); fixQueryObject(study, query, sessionId); query.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); QueryResult<Sample> queryResult = sampleDBAdaptor.get(query, options, userId); return queryResult; }
@Test @Ignore public void deleteSampleFail2Test() throws Exception { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); long fileId = dbAdaptorFactory.getCatalogFileDBAdaptor().getId(user3.getProjects().get(0).getStudies().get(0).getUid(), "data/file.vcf"); Sample hg0097 = new Sample("HG0097", "1000g", new Individual(), "A description", 1); QueryResult<Sample> createResult = dbAdaptorFactory.getCatalogSampleDBAdaptor().insert(studyId, hg0097, null); dbAdaptorFactory.getCatalogFileDBAdaptor().update(fileId, new ObjectMap(FileDBAdaptor.QueryParams.SAMPLES.key(), createResult.first().getUid()), QueryOptions.empty()); dbAdaptorFactory.getCatalogSampleDBAdaptor().delete(createResult.first().getUid()); }
return true; if (sampleDBAdaptor.count(query).first() > 0) { return true;
if (sample.getUid() <= 0 || !sampleDBAdaptor.exists(sample.getUid())) { throw new CatalogException("Sample { id: " + sample.getUid() + "} does not exist.");