Skip to content

Commit

Permalink
Merge pull request #2015 from opencb/TASK-1147
Browse files Browse the repository at this point in the history
TASK-1147
  • Loading branch information
imedina authored Jun 28, 2022
2 parents 36e011e + b3d13c7 commit a504730
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ Family insert(ClientSession clientSession, long studyUid, Family family, List<In
if (StringUtils.isEmpty(family.getUuid())) {
family.setUuid(UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.FAMILY));
}
calculateRoles(clientSession, studyUid, family);
Map<String, Map<String, Family.FamiliarRelationship>> roles = calculateRoles(clientSession, studyUid, family);
family.setRoles(roles);

Document familyDocument = familyConverter.convertToStorageType(family, variableSetList);

Expand Down Expand Up @@ -356,8 +357,6 @@ OpenCGAResult<Object> privateUpdate(ClientSession clientSession, Family family,
Bson bsonQuery = parseQuery(tmpQuery);
return versionedMongoDBAdaptor.update(clientSession, bsonQuery, () -> {
DataResult result = updateAnnotationSets(clientSession, family.getUid(), parameters, variableSetList, queryOptions, true);
UpdateDocument updateDocument = parseAndValidateUpdateParams(clientSession, parameters, tmpQuery);

List<String> familyMemberIds = family.getMembers().stream().map(Individual::getId).collect(Collectors.toList());
boolean updateRoles = queryOptions.getBoolean(ParamConstants.FAMILY_UPDATE_ROLES_PARAM);
if (CollectionUtils.isNotEmpty(parameters.getAsList(QueryParams.MEMBERS.key()))) {
Expand Down Expand Up @@ -399,10 +398,11 @@ OpenCGAResult<Object> privateUpdate(ClientSession clientSession, Family family,
OpenCGAResult<Individual> memberResult = dbAdaptorFactory.getCatalogIndividualDBAdaptor().get(clientSession,
individualQuery, relationshipOptions);
family.setMembers(memberResult.getResults());
calculateRoles(clientSession, family.getStudyUid(), family);
updateDocument.getSet().put(QueryParams.ROLES.key(), family.getRoles());
Map<String, Map<String, Family.FamiliarRelationship>> roles = calculateRoles(clientSession, family.getStudyUid(),
family);
parameters.put(QueryParams.ROLES.key(), roles);
} else {
updateDocument.getSet().put(QueryParams.ROLES.key(), Collections.emptyMap());
parameters.put(QueryParams.ROLES.key(), Collections.emptyMap());
}
}

Expand Down Expand Up @@ -1009,12 +1009,12 @@ public OpenCGAResult updateProjectRelease(long studyId, int release)
});
}

private void calculateRoles(ClientSession clientSession, long studyUid, Family family)
Map<String, Map<String, Family.FamiliarRelationship>> calculateRoles(ClientSession clientSession, long studyUid, Family family)
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException {
if (family.getMembers() == null || family.getMembers().size() <= 1) {
family.setRoles(Collections.emptyMap());
// Nothing to calculate
return;
return Collections.emptyMap();
}

Set<String> individualIds = family.getMembers().stream().map(Individual::getId).collect(Collectors.toSet());
Expand All @@ -1032,7 +1032,7 @@ private void calculateRoles(ClientSession clientSession, long studyUid, Family f
roles.put(member.getId(), memberRelation);
}

family.setRoles(roles);
return roles;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class IndividualMongoDBAdaptor extends AnnotationMongoDBAdaptor<Individua
private final IndividualConverter individualConverter;
private final VersionedMongoDBAdaptor versionedMongoDBAdaptor;

private final FamilyMongoDBAdaptor familyDBAdaptor;

public IndividualMongoDBAdaptor(MongoDBCollection individualCollection, MongoDBCollection archiveIndividualCollection,
MongoDBCollection deletedIndividualCollection, Configuration configuration,
MongoDBAdaptorFactory dbAdaptorFactory) {
Expand All @@ -94,6 +96,8 @@ public IndividualMongoDBAdaptor(MongoDBCollection individualCollection, MongoDBC
this.individualConverter = new IndividualConverter();
this.versionedMongoDBAdaptor = new VersionedMongoDBAdaptor(individualCollection, archiveIndividualCollection,
deletedIndividualCollection);

this.familyDBAdaptor = dbAdaptorFactory.getCatalogFamilyDBAdaptor();
}

@Override
Expand Down Expand Up @@ -445,10 +449,15 @@ OpenCGAResult<Object> privateUpdate(ClientSession clientSession, Individual indi
individual.getId(), parameters.getString(QueryParams.ID.key()));

// Update the family roles
dbAdaptorFactory.getCatalogFamilyDBAdaptor().updateIndividualIdFromFamilies(clientSession, individual.getStudyUid(),
familyDBAdaptor.updateIndividualIdFromFamilies(clientSession, individual.getStudyUid(),
individual.getUid(), individual.getId(), parameters.getString(QueryParams.ID.key()));
}

if (parameters.containsKey(QueryParams.FATHER_UID.key()) || parameters.containsKey(QueryParams.MOTHER_UID.key())) {
// If the parents have changed, we need to check family roles
recalculateFamilyRolesForMember(clientSession, individual.getStudyUid(), individual.getUid());
}

logger.debug("Individual {} successfully updated", individual.getId());
}

Expand All @@ -457,6 +466,24 @@ OpenCGAResult<Object> privateUpdate(ClientSession clientSession, Individual indi
iterator));
}

private void recalculateFamilyRolesForMember(ClientSession clientSession, long studyUid, long memberUid)
throws CatalogParameterException, CatalogDBException, CatalogAuthorizationException {
Query query = new Query()
.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), studyUid)
.append(FamilyDBAdaptor.QueryParams.MEMBER_UID.key(), memberUid);
QueryOptions options = new QueryOptions(QueryOptions.INCLUDE,
Arrays.asList(FamilyDBAdaptor.QueryParams.ID.key(), FamilyDBAdaptor.QueryParams.UID.key(),
FamilyDBAdaptor.QueryParams.VERSION.key(), FamilyDBAdaptor.QueryParams.STUDY_UID.key(),
FamilyDBAdaptor.QueryParams.MEMBERS.key() + "." + IndividualDBAdaptor.QueryParams.ID.key()));
try (DBIterator<Family> iterator = familyDBAdaptor.iterator(clientSession, query, options)) {
while (iterator.hasNext()) {
Family family = iterator.next();
familyDBAdaptor.privateUpdate(clientSession, family, new ObjectMap(), null,
new QueryOptions(ParamConstants.FAMILY_UPDATE_ROLES_PARAM, true));
}
}
}

private void updateReferencesAfterIndividualVersionIncrement(ClientSession clientSession, long studyUid,
MongoDBIterator<Document> iterator)
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException {
Expand All @@ -468,7 +495,7 @@ private void updateReferencesAfterIndividualVersionIncrement(ClientSession clien
}

if (!individualMap.isEmpty()) {
dbAdaptorFactory.getCatalogFamilyDBAdaptor().updateIndividualReferencesInFamily(clientSession, studyUid, individualMap);
familyDBAdaptor.updateIndividualReferencesInFamily(clientSession, studyUid, individualMap);
}
}

Expand All @@ -481,8 +508,6 @@ private void recalculateFamilyDisordersPhenotypes(ClientSession clientSession, I
.append(QueryParams.STUDY_UID.key(), individual.getStudyUid())
.append(QueryParams.UID.key(), individual.getUid()), individualOptions).first();

FamilyMongoDBAdaptor familyDBAdaptor = dbAdaptorFactory.getCatalogFamilyDBAdaptor();

Query familyQuery = new Query()
.append(FamilyDBAdaptor.QueryParams.MEMBER_UID.key(), individual.getUid())
.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), individual.getStudyUid());
Expand Down Expand Up @@ -919,8 +944,6 @@ public OpenCGAResult delete(Query query) throws CatalogDBException, CatalogParam

OpenCGAResult<Object> privateDelete(ClientSession clientSession, Document individualDocument)
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException {
FamilyMongoDBAdaptor familyDBAdaptor = dbAdaptorFactory.getCatalogFamilyDBAdaptor();

String individualId = individualDocument.getString(QueryParams.ID.key());
long individualUid = individualDocument.getLong(PRIVATE_UID);
long studyUid = individualDocument.getLong(PRIVATE_STUDY_UID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ private void connect(Configuration catalogConfiguration) throws CatalogDBExcepti
collections.put(AUDIT_COLLECTION, auditCollection);

fileDBAdaptor = new FileMongoDBAdaptor(fileCollection, deletedFileCollection, catalogConfiguration, this);
familyDBAdaptor = new FamilyMongoDBAdaptor(familyCollection, familyArchivedCollection, deletedFamilyCollection,
catalogConfiguration, this);
individualDBAdaptor = new IndividualMongoDBAdaptor(individualCollection, individualArchivedCollection, deletedIndividualCollection,
catalogConfiguration, this);
jobDBAdaptor = new JobMongoDBAdaptor(jobCollection, deletedJobCollection, catalogConfiguration, this);
Expand All @@ -422,8 +424,6 @@ private void connect(Configuration catalogConfiguration) throws CatalogDBExcepti
cohortDBAdaptor = new CohortMongoDBAdaptor(cohortCollection, deletedCohortCollection, catalogConfiguration, this);
panelDBAdaptor = new PanelMongoDBAdaptor(panelCollection, panelArchivedCollection, deletedPanelCollection, catalogConfiguration,
this);
familyDBAdaptor = new FamilyMongoDBAdaptor(familyCollection, familyArchivedCollection, deletedFamilyCollection,
catalogConfiguration, this);
clinicalDBAdaptor = new ClinicalAnalysisMongoDBAdaptor(clinicalCollection, deletedClinicalCollection, catalogConfiguration, this);
interpretationDBAdaptor = new InterpretationMongoDBAdaptor(interpretationCollection, interpretationArchivedCollection,
deletedInterpretationCollection, catalogConfiguration, this);
Expand Down
Loading

0 comments on commit a504730

Please sign in to comment.