Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4802 - Copy/Past operation works wrong #4910

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/ketcher-core/src/domain/entities/sgroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ export class SGroup {
cloneAttachmentPoints(
atomIdMap: Map<number, number>,
): ReadonlyArray<SGroupAttachmentPoint> {
return this.attachmentPoints.map((point) => point.clone(atomIdMap));
return this.attachmentPoints
.filter((point) => atomIdMap.has(point.atomId))
.map((point) => point.clone(atomIdMap));
}

public get isSuperatomWithoutLabel() {
Expand Down Expand Up @@ -425,14 +427,18 @@ export class SGroup {
sg.atoms = SGroup.removeNegative(SGroup.filterAtoms(sg.atoms, atomMap));
}

static clone(sgroup: SGroup, aidMap: Map<number, number>): SGroup {
static clone(
sgroup: SGroup,
aidMap: Map<number, number>,
atomsInSet?: number[],
): SGroup {
const cp = new SGroup(sgroup.type);

Object.keys(sgroup.data).forEach((field) => {
cp.data[field] = sgroup.data[field];
});

cp.atoms = sgroup.atoms.map((elem) => aidMap.get(elem));
const sgroupAtoms = atomsInSet || sgroup.atoms;
cp.atoms = sgroupAtoms.map((elem) => aidMap.get(elem));
cp.pp = sgroup.pp;
cp.bracketBox = sgroup.bracketBox;
cp.patoms = null;
Expand Down
6 changes: 4 additions & 2 deletions packages/ketcher-core/src/domain/entities/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,15 @@ export class Struct {

const sgroupIdMap = {};
this.sgroups.forEach((sg, sgroupId) => {
if (sg.atoms.some((aid) => !atomSet!.has(aid))) return;
const notInGroup = sg.atoms.some((aid) => !atomSet!.has(aid));
const atomsInSet = sg.atoms.filter((aid) => atomSet!.has(aid));
if (!atomsInSet.length && notInGroup) return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something here but why do you need notInGroup? Conditions in .some and .filter are mutually exclusive so if there are no elements satisfying the atomSet.has(aid) condition, there is definitely at least one element which satisfies the inverse !atomSet.has(aid) condition

Copy link
Collaborator Author

@Guch1g0v Guch1g0v Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes (when we copy a attachment point label) we are not in the group, but we need still filter some atoms,
but if we are not in the group and there are no atoms, we exit. Also, we must support the previous realization of mergeInto.

Screenshot from 2024-07-01 21-51-11
Screenshot from 2024-07-01 21-49-11

Perhaps there is another way to solve the problem.

const oldSgroup = sg;

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

const id = cp.sgroups.add(sg);
sg.id = id;
Expand Down
Loading