@Test public void testModifyIndividual() throws Exception { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); long individualId = catalogIndividualDBAdaptor.insert(studyId, new Individual("in1", "in1", IndividualProperty.Sex .UNKNOWN, "", null, 1, Collections.emptyList(), null), null).first().getUid(); ObjectMap params = new ObjectMap("sex", "MALE"); catalogIndividualDBAdaptor.update(individualId, params, QueryOptions.empty()); Individual individual = catalogIndividualDBAdaptor.get(individualId, null).first(); assertEquals(IndividualProperty.Sex.MALE, individual.getSex()); }
@Override public DBIterator<Individual> iterator(String studyStr, Query query, QueryOptions options, String sessionId) throws CatalogException { ParamUtils.checkObj(sessionId, "sessionId"); query = ParamUtils.defaultObject(query, Query::new); options = ParamUtils.defaultObject(options, QueryOptions::new); String userId = userManager.getUserId(sessionId); Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); query.append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); return individualDBAdaptor.iterator(query, options, userId); }
@Test public void testGetIndividualNoExists() throws Exception { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); Individual individual = new Individual("in1", "An Individual", IndividualProperty.Sex.MALE, "", new Individual .Population(), 1, Collections.emptyList(), null); catalogIndividualDBAdaptor.insert(studyId, individual, null).first(); catalogIndividualDBAdaptor.get(individual.getUid(), null).first(); thrown.expect(CatalogDBException.class); //Id not found catalogIndividualDBAdaptor.get(9999, null); }
@Test public void testModifyIndividualBadGender() throws Exception { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); long individualId = catalogIndividualDBAdaptor.insert(studyId, new Individual("in1", "in1", IndividualProperty.Sex .UNKNOWN, "", null, 1, Collections.emptyList(), null), null).first().getUid(); thrown.expect(CatalogDBException.class); catalogIndividualDBAdaptor.update(individualId, new ObjectMap("sex", "bad sex"), QueryOptions.empty()); }
QueryResult<Individual> insert(long studyId, Individual individual, List<VariableSet> variableSetList, QueryOptions options) throws CatalogDBException;
private List<String> getSamplesFromIndividuals(MyResources<Individual> resource) throws CatalogDBException { // Look for all the samples belonging to the individual Query query = new Query() .append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), resource.getStudy().getUid()) .append(IndividualDBAdaptor.QueryParams.UID.key(), resource.getResourceList().stream().map(Individual::getUid) .collect(Collectors.toList())); QueryResult<Individual> individualQueryResult = individualDBAdaptor.get(query, new QueryOptions(QueryOptions.INCLUDE, IndividualDBAdaptor.QueryParams.SAMPLES.key())); List<String> sampleIds = new ArrayList<>(); for (Individual individual : individualQueryResult.getResult()) { sampleIds.addAll(individual.getSamples().stream().map(Sample::getId).collect(Collectors.toList())); } return sampleIds; }
@Test public void testNativeGet() throws Exception { long studyId = user4.getProjects().get(0).getStudies().get(0).getUid(); Individual individual = new Individual("in1", "An Individual", IndividualProperty.Sex.MALE, "", new Individual .Population(), 1, Collections.emptyList(), null); individual = catalogIndividualDBAdaptor.insert(studyId, individual, null).first(); Individual individual2 = new Individual("in2", "Another Individual", IndividualProperty.Sex.FEMALE, "", new Individual .Population(), 1, Collections.emptyList(), null); individual2 = catalogIndividualDBAdaptor.insert(studyId, individual2, null).first(); List<QueryResult> queryResults = catalogIndividualDBAdaptor.nativeGet(Arrays.asList( new Query(IndividualDBAdaptor.QueryParams.UID.key(), individual.getUid()), new Query(IndividualDBAdaptor.QueryParams.UID.key(), individual2.getUid())), new QueryOptions()); assertEquals(2, queryResults.size()); // Individual List<Document> results = queryResults.get(0).getResult(); assertEquals(1, results.size()); assertEquals("MALE", results.get(0).get("sex")); // Individual2 results = queryResults.get(1).getResult(); assertEquals(1, results.size()); assertEquals("FEMALE", results.get(0).get("sex")); } }
iterator = individualDBAdaptor.iterator(finalQuery, QueryOptions.empty(), userId); .append(IndividualDBAdaptor.QueryParams.STATUS_NAME.key(), Status.DELETED) .append(IndividualDBAdaptor.QueryParams.ID.key(), individual.getName() + suffixName); QueryResult<Long> update = individualDBAdaptor.update(updateQuery, updateParams, QueryOptions.empty()); if (update.first() > 0) { numModified += 1;
QueryOptions indOptions = new QueryOptions(QueryOptions.INCLUDE, Arrays.asList( IndividualDBAdaptor.QueryParams.UID.key(), IndividualDBAdaptor.QueryParams.SAMPLES.key())); QueryResult<Individual> individualQueryResult = individualDBAdaptor.get(query, indOptions); individualDBAdaptor.update(individual.getUid(), params, QueryOptions.empty()); } catch (CatalogDBException e) { logger.error("Could not update sample information: {}", e.getMessage(), e);
@Test public void testGetStudyIdByIndividualId() throws Exception { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); long individualId = catalogIndividualDBAdaptor.insert(studyId, new Individual().setStatus(new Status()), null).first().getUid(); long studyIdByIndividualId = catalogIndividualDBAdaptor.getStudyId(individualId); assertEquals(studyId, studyIdByIndividualId); }
.append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), studyId) .append(IndividualDBAdaptor.QueryParams.ID.key(), myName); if (individualDBAdaptor.count(query).first() > 0) { throw new CatalogException("Individual name " + myName + " already in use"); .append(IndividualDBAdaptor.QueryParams.ID.key(), StringUtils.join(siblingList, ",")); QueryOptions queryOptions = new QueryOptions(QueryOptions.INCLUDE, IndividualDBAdaptor.QueryParams.UID.key()); QueryResult<Individual> individualQueryResult = individualDBAdaptor.get(query, queryOptions); if (individualQueryResult.getNumResults() < siblingList.size()) { int missing = siblingList.size() - individualQueryResult.getNumResults();
QueryResult<Individual> unsafeUpdate(Study study, Individual individual, ObjectMap parameters, QueryOptions options, String userId) throws CatalogException { try { ParamUtils.checkAllParametersExist(parameters.keySet().iterator(), (a) -> IndividualDBAdaptor.UpdateParams.getParam(a) != null); } catch (CatalogParameterException e) { throw new CatalogException("Could not update: " + e.getMessage(), e); } MyResource<Individual> resource = new MyResource<>(userId, study, individual); List<VariableSet> variableSetList = checkUpdateAnnotationsAndExtractVariableSets(resource, parameters, options, individualDBAdaptor); if (options.getBoolean(Constants.INCREMENT_VERSION)) { // We do need to get the current release to properly create a new version options.put(Constants.CURRENT_RELEASE, studyManager.getCurrentRelease(study, userId)); } QueryResult<Individual> queryResult = individualDBAdaptor.update(individual.getUid(), parameters, variableSetList, options); auditManager.recordUpdate(AuditRecord.Resource.individual, individual.getUid(), userId, parameters, null, null); return queryResult; }
default boolean exists(long sampleId) throws CatalogDBException { return count(new Query(QueryParams.UID.key(), sampleId)).first() > 0; }
default void checkId(long individualId) throws CatalogDBException { if (individualId < 0) { throw CatalogDBException.newInstance("Individual id '{}' is not valid: ", individualId); } if (!exists(individualId)) { throw CatalogDBException.newInstance("Indivivual id '{}' does not exist", individualId); } }
@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); ParamUtils.checkObj(fields, "fields"); String userId = userManager.getUserId(sessionId); Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); Query finalQuery = new Query(query); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); AnnotationUtils.fixQueryOptionAnnotation(options); try { fixQuery(study, finalQuery, sessionId); } catch (CatalogException e) { // Any of mother, father or sample ids or names do not exist or were not found return new QueryResult<>(null); } // Add study id to the query finalQuery.put(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); QueryResult queryResult = individualDBAdaptor.groupBy(finalQuery, fields, options, userId); return ParamUtils.defaultObject(queryResult, QueryResult::new); }
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 testModifyIndividualBadFatherId() throws Exception { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); long individualId = catalogIndividualDBAdaptor.insert(studyId, new Individual("in1", "in1", IndividualProperty.Sex .UNKNOWN, "", null, 1, Collections.emptyList(), null), null).first().getUid(); thrown.expect(CatalogDBException.class); catalogIndividualDBAdaptor.update(individualId, new ObjectMap("fatherId", 4000), QueryOptions.empty()); }
@Test public void testGetIndividual() throws Exception { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); Individual individual = new Individual("an_individual", "An Individual", IndividualProperty.Sex.MALE, "", new Individual.Population(), 1, Collections.emptyList(), null); individual = catalogIndividualDBAdaptor.insert(studyId, individual, null).first(); Individual individual2 = catalogIndividualDBAdaptor.get(individual.getUid(), null).first(); assertEquals(individual.toString(), individual2.toString()); }
@Test public void testCreateIndividualAlreadyExists() throws Exception { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); catalogIndividualDBAdaptor.insert(studyId, new Individual("in1", "in1", null, "", null, 1, Collections .emptyList(), null), null); thrown.expect(CatalogDBException.class); //Name already exists catalogIndividualDBAdaptor.insert(studyId, new Individual("in1", "in1", null, "", null, 1, Collections .emptyList(), null), null); }