Skip to content

Commit

Permalink
#4973 - Connection between molecule and monomer lost after opening an…
Browse files Browse the repository at this point in the history
…d saving to ket (#4980)

- fixed monomer atoms ids mapping during struct clone
- fixed removeLeavingGroupsFromConnectedAtoms
  • Loading branch information
rrodionov91 authored Jul 4, 2024
1 parent 98d6c08 commit 46d3f1f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ export class MonomerMicromolecule extends SGroup {
return { position: this.pp, atomId: sgroupContractedPosition.atomId };
}

public static clone(monomerMicromolecule: MonomerMicromolecule) {
public static clone(
monomerMicromolecule: MonomerMicromolecule,
atomIdMap?: Map<number, number>,
) {
const monomerMicromoleculeClone = new MonomerMicromolecule(
monomerMicromolecule.type,
monomerMicromolecule.monomer,
);
monomerMicromoleculeClone.pp = monomerMicromolecule.pp;
monomerMicromoleculeClone.atoms = monomerMicromolecule.atoms;
monomerMicromoleculeClone.atoms = atomIdMap
? monomerMicromolecule.atoms.map((elem) => atomIdMap.get(elem))
: monomerMicromolecule.atoms;

return monomerMicromoleculeClone;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ketcher-core/src/domain/entities/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class Struct {

sg =
oldSgroup instanceof MonomerMicromolecule
? MonomerMicromolecule.clone(oldSgroup)
? MonomerMicromolecule.clone(oldSgroup, aidMap!)
: SGroup.clone(sg, aidMap!);

const id = cp.sgroups.add(sg);
Expand Down
13 changes: 11 additions & 2 deletions packages/ketcher-core/src/domain/serializers/ket/ketSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,10 @@ export class KetSerializer implements Serializer<Struct> {

struct.sgroups.forEach((sgroup) => {
const attachmentPoints = sgroup.getAttachmentPoints();
const attachmentPointsToReplace: Map<
SGroupAttachmentPoint,
SGroupAttachmentPoint
> = new Map();
attachmentPoints.forEach((attachmentPoint) => {
if (
isNumber(attachmentPoint.leaveAtomId) &&
Expand All @@ -655,14 +659,19 @@ export class KetSerializer implements Serializer<Struct> {
attachmentPoint.attachmentId,
attachmentPoint.attachmentPointNumber,
);
sgroup.removeAttachmentPoint(attachmentPoint);
sgroup.addAttachmentPoint(attachmentPointClone);
attachmentPointsToReplace.set(attachmentPoint, attachmentPointClone);
sgroup.atoms.splice(
sgroup.atoms.indexOf(attachmentPoint.leaveAtomId),
1,
);
}
});
attachmentPointsToReplace.forEach(
(attachmentPointToAdd, attachmentPointToDelete) => {
sgroup.removeAttachmentPoint(attachmentPointToDelete);
sgroup.addAttachmentPoint(attachmentPointToAdd);
},
);
});

return struct;
Expand Down

0 comments on commit 46d3f1f

Please sign in to comment.