public QueryResult<Family> create(String studyStr, Family family, QueryOptions options, String sessionId) throws CatalogException { String userId = catalogManager.getUserManager().getUserId(sessionId); Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); authorizationManager.checkStudyPermission(study.getUid(), userId, StudyAclEntry.StudyPermissions.WRITE_FAMILIES); ParamUtils.checkObj(family, "family"); ParamUtils.checkAlias(family.getId(), "id"); family.setName(ParamUtils.defaultObject(family.getName(), family.getId())); family.setMembers(ParamUtils.defaultObject(family.getMembers(), Collections.emptyList())); family.setPhenotypes(ParamUtils.defaultObject(family.getPhenotypes(), Collections.emptyList())); family.setDisorders(ParamUtils.defaultObject(family.getDisorders(), Collections.emptyList())); family.setCreationDate(TimeUtils.getTime()); family.setDescription(ParamUtils.defaultString(family.getDescription(), "")); family.setStatus(new Family.FamilyStatus()); family.setAnnotationSets(ParamUtils.defaultObject(family.getAnnotationSets(), Collections.emptyList())); family.setRelease(catalogManager.getStudyManager().getCurrentRelease(study, userId)); family.setVersion(1); family.setAttributes(ParamUtils.defaultObject(family.getAttributes(), Collections.emptyMap())); List<VariableSet> variableSetList = validateNewAnnotationSetsAndExtractVariableSets(study.getUid(), family.getAnnotationSets()); autoCompleteFamilyMembers(family, study, sessionId); validateFamily(family); validateMultiples(family); validatePhenotypes(family); validateDisorders(family); createMissingMembers(family, study, sessionId); options = ParamUtils.defaultObject(options, QueryOptions::new); family.setUuid(UUIDUtils.generateOpenCGAUUID(UUIDUtils.Entity.FAMILY)); QueryResult<Family> queryResult = familyDBAdaptor.insert(study.getUid(), family, variableSetList, options); auditManager.recordCreation(AuditRecord.Resource.family, queryResult.first().getId(), userId, queryResult.first(), null, null); return queryResult; }
@Test public void createFamilyDuo() throws CatalogException { Family family = new Family() .setId("test") .setPhenotypes(Arrays.asList(new Phenotype("E00", "blabla", "blabla"))) .setMembers(Arrays.asList(new Individual().setId("proband").setSex(IndividualProperty.Sex.MALE), new Individual().setFather(new Individual().setId("proband")).setId("child") .setSex(IndividualProperty.Sex.FEMALE))); QueryResult<Family> familyQueryResult = familyManager.create(STUDY, family, QueryOptions.empty(), sessionIdUser); assertEquals(2, familyQueryResult.first().getMembers().size()); }
familySolrModel.setUid(family.getUid()); familySolrModel.setStudyId(study.getFqn().replace(":", "__")); Date date = TimeUtils.toDate(family.getCreationDate()); LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); familySolrModel.setCreationDay(localDate.getDayOfMonth()); familySolrModel.setCreationDayOfWeek(localDate.getDayOfWeek().toString()); familySolrModel.setStatus(family.getStatus().getName()); if (family.getStatus() != null) { familySolrModel.setStatus(family.getStatus().getName()); familySolrModel.setPhenotypes(SolrConverterUtil.populatePhenotypes(family.getPhenotypes())); familySolrModel.setNumMembers(family.getMembers() != null ? family.getMembers().size() : 0); familySolrModel.setExpectedSize(family.getExpectedSize()); familySolrModel.setRelease(family.getRelease()); familySolrModel.setVersion(family.getVersion()); familySolrModel.setAnnotations(SolrConverterUtil.populateAnnotations(variableMap, family.getAnnotationSets())); if (family.getAnnotationSets() != null) { familySolrModel.setAnnotationSets(family.getAnnotationSets().stream().map(AnnotationSet::getId).collect(Collectors.toList())); } else { familySolrModel.setAnnotationSets(Collections.emptyList()); SolrConverterUtil.parseInternalOpenCGAAcls((List<Map<String, Object>>) family.getAttributes().get("OPENCGA_ACL")); List<String> effectivePermissions =
private void validatePhenotypes(Family family) throws CatalogException { if (family.getPhenotypes() == null || family.getPhenotypes().isEmpty()) { if (ListUtils.isNotEmpty(family.getMembers())) { Map<String, Phenotype> phenotypeMap = new HashMap<>(); for (Individual member : family.getMembers()) { if (ListUtils.isNotEmpty(member.getPhenotypes())) { for (Phenotype phenotype : member.getPhenotypes()) { family.setPhenotypes(phenotypeList); if (family.getMembers() == null || family.getMembers().isEmpty()) { throw new CatalogException("Missing family members"); for (Individual individual : family.getMembers()) { if (individual.getPhenotypes() != null && !individual.getPhenotypes().isEmpty()) { memberPhenotypes.addAll(individual.getPhenotypes().stream().map(Phenotype::getId).collect(Collectors.toSet())); Set<String> familyPhenotypes = family.getPhenotypes().stream().map(Phenotype::getId).collect(Collectors.toSet()); if (!familyPhenotypes.containsAll(memberPhenotypes)) { throw new CatalogException("Some of the phenotypes are not present in any member of the family");
if (StringUtils.isEmpty(family.getId())) { throw new CatalogException("Missing family id"); List<Individual> selectedMembers = family.getMembers(); QueryResult<Family> familyQueryResult = catalogManager.getFamilyManager().get(study.getFqn(), family.getId(), new QueryOptions(), sessionId); if (familyQueryResult.getNumResults() == 0) { throw new CatalogException("Family " + family.getId() + " not found"); if (ListUtils.isEmpty(finalFamily.getMembers())) { throw new CatalogException("Family " + family.getId() + " does not have any members associated"); for (Individual member : finalFamily.getMembers()) { memberMap.put(member.getId(), member); Individual fullMember = memberMap.get(selectedMember.getId()); if (fullMember == null) { throw new CatalogException("Member " + selectedMember.getId() + " does not belong to family " + family.getId()); finalFamily.setMembers(finalMembers); } else { finalFamily.setMembers(Collections.emptyList());
@Test public void FamilyToSolrTest() { Study study = new Study().setFqn("user@project:study").setAttributes(new HashMap<>()); Family family = new Family("id", "family", null, null, Arrays.asList(new Individual().setId("I1"), new Individual().setId("I2")), "test", 1000, AnnotationHelper.createAnnotation(), null); family.setUid(100).setStatus(new Family.FamilyStatus("READY")).setRelease(1).setVersion(2); FamilySolrModel familySolrModel = new CatalogFamilyToSolrFamilyConverter(study).convertToStorageType(family); assertEquals(familySolrModel.getUid(), family.getUid()); assertEquals(familySolrModel.getStatus(), family.getStatus().getName()); assertEquals(familySolrModel.getNumMembers(), family.getMembers().size()); assertEquals(familySolrModel.getRelease(), family.getRelease()); assertEquals(familySolrModel.getVersion(), family.getVersion()); assertEquals(familySolrModel.getPhenotypes().size(), 0); assertEquals(familySolrModel.getAnnotations().get("annotations__o__annotName.vsId.a.ab2.ab2c1.ab2c1d1"), Arrays.asList(1, 2, 3, 4, 11, 12, 13, 14, 21)); assertEquals(familySolrModel.getAnnotations().get("annotations__o__annotName.vsId.a.ab1.ab1c1"), Arrays.asList(true, false, false)); assertEquals(familySolrModel.getAnnotations().get("annotations__s__annotName.vsId.a.ab1.ab1c2"), "hello world"); assertEquals(familySolrModel.getAnnotations().get("annotations__o__annotName.vsId.a.ab2.ab2c1.ab2c1d2"), Arrays.asList("hello ab2c1d2 1", "hello ab2c1d2 2")); assertEquals(familySolrModel.getAnnotations().get("annotations__o__annotName.vsId.a.ab3.ab3c1.ab3c1d1"), Arrays.asList(Arrays.asList("hello"), Arrays.asList("hello2", "bye2"), Arrays.asList("byeee2", "hellooo2"))); assertEquals(familySolrModel.getAnnotations().get("annotations__o__annotName.vsId.a.ab3.ab3c1.ab3c1d2"), Arrays.asList(2.0, 4.0, 24.0)); assertNull(familySolrModel.getAnnotations().get("nothing")); assertEquals(familySolrModel.getAnnotations().keySet().size(), 6); } }
filterList.add(Filters.eq(QueryParams.ID.key(), family.getName())); filterList.add(Filters.eq(PRIVATE_STUDY_ID, studyId)); filterList.add(Filters.eq(QueryParams.STATUS_NAME.key(), Status.READY)); QueryResult<Long> count = familyCollection.count(bson); if (count.getResult().get(0) > 0) { throw new CatalogDBException("Cannot create family. A family with { name: '" + family.getName() + "'} already exists."); family.setUid(familyId); family.setStudyUid(studyId); family.setVersion(1); if (StringUtils.isEmpty(family.getUuid())) { family.setUuid(UUIDUtils.generateOpenCGAUUID(UUIDUtils.Entity.FAMILY)); familyObject.put(RELEASE_FROM_VERSION, Arrays.asList(family.getRelease())); familyObject.put(LAST_OF_VERSION, true); familyObject.put(LAST_OF_RELEASE, true); if (StringUtils.isNotEmpty(family.getCreationDate())) { familyObject.put(PRIVATE_CREATION_DATE, TimeUtils.toDate(family.getCreationDate())); } else { familyObject.put(PRIVATE_CREATION_DATE, TimeUtils.getDate());
if (family.getMembers() == null || family.getMembers().isEmpty()) { family.setMembers(familyQueryResult.first().getMembers()); } else { if (family.getPhenotypes() == null || family.getMembers().isEmpty()) { family.setPhenotypes(familyQueryResult.first().getPhenotypes()); if (ListUtils.isEmpty(family.getDisorders())) { family.setDisorders(familyQueryResult.first().getDisorders());
public static Pedigree getPedigreeFromFamily(Family family) { List<Individual> members = family.getMembers(); Map<String, Member> individualMap = new HashMap<>(); // Parse all the individuals for (Individual member : members) { Member individual = new Member( member.getId(), member.getName(), null, null, member.getMultiples(), Member.Sex.getEnum(member.getSex().toString()), member.getLifeStatus(), Member.AffectionStatus.getEnum(member.getAffectationStatus().toString()), member.getPhenotypes(), member.getAttributes()); individualMap.put(individual.getId(), individual); } // Fill parent information for (Individual member : members) { if (member.getFather() != null && StringUtils.isNotEmpty(member.getFather().getId())) { individualMap.get(member.getId()).setFather(individualMap.get(member.getFather().getId())); } if (member.getMother() != null && StringUtils.isNotEmpty(member.getMother().getId())) { individualMap.get(member.getId()).setMother(individualMap.get(member.getMother().getId())); } } List<Member> individuals = new ArrayList<>(individualMap.values()); return new Pedigree(family.getId(), individuals, family.getPhenotypes(), family.getAttributes()); }
@Test public void updateFamilyPhenotype() throws JsonProcessingException, CatalogException { QueryResult<Family> originalFamily = createDummyFamily("Martinez-Martinez"); Phenotype phenotype1 = new Phenotype("dis1", "New name", "New source"); Phenotype phenotype2 = new Phenotype("dis2", "New name", "New source"); Phenotype phenotype3 = new Phenotype("dis3", "New name", "New source"); Family family = new Family(); family.setPhenotypes(Arrays.asList(phenotype1, phenotype2, phenotype3)); ObjectMapper jsonObjectMapper = catalogManagerResource.generateNewObjectMapper(); ObjectMap params = new ObjectMap(jsonObjectMapper.writeValueAsString(family)); params = new ObjectMap(FamilyDBAdaptor.QueryParams.PHENOTYPES.key(), params.get(FamilyDBAdaptor.QueryParams.PHENOTYPES.key())); QueryResult<Family> updatedFamily = familyManager.update(STUDY, originalFamily.first().getName(), params, QueryOptions.empty(), sessionIdUser); assertEquals(3, updatedFamily.first().getPhenotypes().size()); // Only one id should be the same as in originalFamilyIds (father id) for (Phenotype phenotype : updatedFamily.first().getPhenotypes()) { assertEquals("New name", phenotype.getName()); assertEquals("New source", phenotype.getSource()); } }
for (Individual member : family.getMembers()) { if (member.getUid() != individual.getUid()) { members.add(member); if (members.size() + 1 == family.getMembers().size()) { .append(FamilyDBAdaptor.QueryParams.UID.key(), family.getUid()) .append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()) .append(Constants.ALL_VERSIONS, true); throw new CatalogException("Individual could not be extracted from family " + family.getId() + ". " + "Individual not deleted"); + "following members: {}", individual.getId(), family.getId(), family.getMembers().stream().map(Individual::getId).collect(Collectors.toList())); throw new CatalogException("Internal error: Could not delete individual");
if (family.getMembers().isEmpty()) { throw VariantQueryException.malformedParam(FAMILY, familyId, "Empty family"); for (Individual member : family.getMembers()) { int numSamples = 0; for (Sample sample : member.getSamples()) { if (family.getPhenotypes().isEmpty()) { throw VariantQueryException.malformedParam(FAMILY, familyId, "Family doesn't have phenotypes"); if (isValidParam(query, FAMILY_PHENOTYPE)) { String phenotypeId = query.getString(FAMILY_PHENOTYPE.key()); phenotype = family.getPhenotypes() .stream() .filter(familyPhenotype -> familyPhenotype.getId().equals(phenotypeId)) if (phenotype == null) { throw VariantQueryException.malformedParam(FAMILY_PHENOTYPE, phenotypeId, "Available phenotypes: " + family.getPhenotypes() .stream() .map(Phenotype::getId) if (family.getPhenotypes().size() > 1) { throw VariantQueryException.missingParam(FAMILY_PHENOTYPE, "More than one phenotype found for the family \"" + familyId + "\". " + "Available phenotypes: " + family.getPhenotypes() .stream() .map(Phenotype::getId) .collect(Collectors.toList()));
authorizationManager.checkFamilyPermission(study.getUid(), family.getUid(), userId, FamilyAclEntry.FamilyPermissions.DELETE); .append(FamilyDBAdaptor.QueryParams.UID.key(), family.getUid()) .append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()) .append(Constants.ALL_VERSIONS, true); ObjectMap updateParams = new ObjectMap() .append(FamilyDBAdaptor.QueryParams.STATUS_NAME.key(), Status.DELETED) .append(FamilyDBAdaptor.QueryParams.ID.key(), family.getName() + suffixName); QueryResult<Long> update = familyDBAdaptor.update(updateQuery, updateParams, QueryOptions.empty()); if (update.first() > 0) { numModified += 1; auditManager.recordDeletion(AuditRecord.Resource.family, family.getUid(), userId, null, updateParams, null, null); } else { failedList.add(new WriteResult.Fail(family.getId(), "Unknown reason")); failedList.add(new WriteResult.Fail(family.getId(), e.getMessage())); logger.debug("Cannot delete family {}: {}", family.getId(), e.getMessage(), e);
private void validateMultiples(Family family) throws CatalogException { if (family.getMembers() == null || family.getMembers().isEmpty()) { return; } Map<String, List<String>> multiples = new HashMap<>(); // Look for all the multiples for (Individual individual : family.getMembers()) { if (individual.getMultiples() != null && individual.getMultiples().getSiblings() != null && !individual.getMultiples().getSiblings().isEmpty()) { multiples.put(individual.getId(), individual.getMultiples().getSiblings()); } } if (multiples.size() > 0) { // Check if they are all cross-referenced for (Map.Entry<String, List<String>> entry : multiples.entrySet()) { for (String sibling : entry.getValue()) { if (!multiples.containsKey(sibling)) { throw new CatalogException("Missing sibling " + sibling + " of member " + entry.getKey()); } if (!multiples.get(sibling).contains(entry.getKey())) { throw new CatalogException("Incomplete sibling information. Sibling " + sibling + " does not contain " + entry.getKey() + " as its sibling"); } } } } }
@Test public void updateFamilyMissingPhenotype() throws JsonProcessingException, CatalogException { QueryResult<Family> originalFamily = createDummyFamily("Martinez-Martinez"); Phenotype phenotype1 = new Phenotype("dis1", "New name", "New source"); Family family = new Family(); family.setPhenotypes(Arrays.asList(phenotype1)); ObjectMapper jsonObjectMapper = catalogManagerResource.generateNewObjectMapper(); ObjectMap params = new ObjectMap(jsonObjectMapper.writeValueAsString(family)); params = new ObjectMap(FamilyDBAdaptor.QueryParams.PHENOTYPES.key(), params.get(FamilyDBAdaptor.QueryParams.PHENOTYPES.key())); thrown.expect(CatalogException.class); thrown.expectMessage("not present in any member of the family"); familyManager.update(STUDY, originalFamily.first().getName(), params, QueryOptions.empty(), sessionIdUser); }
private void validateDisorders(Family family) throws CatalogException { if (ListUtils.isEmpty(family.getDisorders())) { if (ListUtils.isNotEmpty(family.getMembers())) { for (Individual member : family.getMembers()) { if (ListUtils.isNotEmpty(member.getDisorders())) { for (Disorder disorder : member.getDisorders()) { family.setDisorders(disorderList); if (family.getMembers() == null || family.getMembers().isEmpty()) { throw new CatalogException("Missing family members"); for (Individual individual : family.getMembers()) { if (ListUtils.isNotEmpty(individual.getDisorders())) { memberDisorders.addAll(individual.getDisorders().stream().map(Disorder::getId).collect(Collectors.toSet())); Set<String> familyDisorders = family.getDisorders().stream().map(Disorder::getId).collect(Collectors.toSet()); if (!familyDisorders.containsAll(memberDisorders)) { throw new CatalogException("Some of the disorders are not present in any member of the family");
@Test public void updateFamilyMissingMember() throws CatalogException, JsonProcessingException { QueryResult<Family> originalFamily = createDummyFamily("Martinez-Martinez"); Individual father = new Individual().setId("father"); Individual mother = new Individual().setId("mother2"); // We create a new father and mother with the same information to mimic the behaviour of the webservices. Otherwise, we would be // ingesting references to exactly the same object and this test would not work exactly the same way. Individual relFather = new Individual().setId("father").setPhenotypes(Arrays.asList(new Phenotype("dis1", "dis1", "OT"))); Individual relChild1 = new Individual().setId("child3") .setPhenotypes(Arrays.asList(new Phenotype("dis1", "dis1", "OT"), new Phenotype("dis2", "dis2", "OT"))) .setFather(father) .setMother(mother) .setParentalConsanguinity(true); Family family = new Family(); family.setMembers(Arrays.asList(relChild1, relFather)); ObjectMapper jsonObjectMapper = catalogManagerResource.generateNewObjectMapper(); ObjectMap params = new ObjectMap(jsonObjectMapper.writeValueAsString(family)); params = new ObjectMap(FamilyDBAdaptor.QueryParams.MEMBERS.key(), params.get(FamilyDBAdaptor.QueryParams.MEMBERS.key())); thrown.expect(CatalogException.class); thrown.expectMessage("not present in the members list"); familyManager.update(STUDY, originalFamily.first().getName(), params, QueryOptions.empty(), sessionIdUser); }
@Test public void createClinicalAnalysisTest() throws CatalogException { QueryResult<ClinicalAnalysis> dummyEnvironment = createDummyEnvironment(true); assertEquals(1, dummyEnvironment.getNumResults()); assertEquals(0, dummyEnvironment.first().getInterpretations().size()); assertEquals("family", dummyEnvironment.first().getFamily().getId()); assertEquals(5, dummyEnvironment.first().getFamily().getMembers().size()); assertNotNull(dummyEnvironment.first().getProband()); assertEquals("child1", dummyEnvironment.first().getProband().getId()); assertEquals(1, dummyEnvironment.first().getProband().getSamples().size()); assertEquals("sample2", dummyEnvironment.first().getProband().getSamples().get(0).getId()); assertEquals(catalogManager.getSampleManager().getUid("sample2", STUDY, sessionIdUser).getResource().getUid(), dummyEnvironment.first().getProband().getSamples().get(0).getUid()); }
if (family.getMembers() == null || family.getMembers().isEmpty()) { return; for (Individual individual : family.getMembers()) { memberMap.put(individual.getId(), individual); individualIds.add(individual.getId()); family.setMembers(memberMap.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList()));
private void validateFamilyAndProband(ClinicalAnalysis clinicalAnalysis, Study study, String sessionId) throws CatalogException { if (clinicalAnalysis.getFamily() != null && StringUtils.isNotEmpty(clinicalAnalysis.getFamily().getId())) { MyResource<Family> familyResource = catalogManager.getFamilyManager().getUid(clinicalAnalysis.getFamily().getId(), study.getFqn(), sessionId); clinicalAnalysis.setFamily(familyResource.getResource()); // Check the proband is an actual member of the family Query query = new Query() .append(FamilyDBAdaptor.QueryParams.UID.key(), familyResource.getResource().getUid()) .append(FamilyDBAdaptor.QueryParams.MEMBER_UID.key(), clinicalAnalysis.getProband().getUid()); QueryResult<Family> count = catalogManager.getFamilyManager().count(study.getFqn(), query, sessionId); if (count.getNumTotalResults() == 0) { throw new CatalogException("The member " + clinicalAnalysis.getProband().getId() + " does not belong to the family " + clinicalAnalysis.getFamily().getId()); } } }