Skip to content

Commit

Permalink
#4693 - Incorrect bond alignment to tBu S-group (#4741)
Browse files Browse the repository at this point in the history
- fixed position calculation for external bonds to sgroups without attachment points
  • Loading branch information
rrodionov91 authored Jun 4, 2024
1 parent 7e7f117 commit d00edaa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
21 changes: 15 additions & 6 deletions packages/ketcher-core/src/application/render/restruct/rebond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
Bond,
FunctionalGroup,
HalfBond,
SGroup,
Struct,
Vec2,
} from 'domain/entities';
Expand Down Expand Up @@ -58,19 +59,27 @@ class ReBond extends ReObject {
return true;
}

private static getAtomPositionForBond(
struct: Struct,
atomId: number,
sgroup?: SGroup,
) {
return sgroup instanceof MonomerMicromolecule
? (sgroup.getAttachmentAtomId() as number)
: sgroup?.isContracted()
? sgroup?.getContractedPosition(struct).atomId
: atomId;
}

static bondRecalc(bond: ReBond, restruct: ReStruct, options: any): void {
const render = restruct.render;
const sgroup1 = restruct.molecule.getGroupFromAtomId(bond.b.begin);
const sgroup2 = restruct.molecule.getGroupFromAtomId(bond.b.end);
const beginAtom = restruct.atoms.get(
sgroup1 instanceof MonomerMicromolecule
? (sgroup1.getAttachmentAtomId() as number)
: bond.b.begin,
ReBond.getAtomPositionForBond(restruct.molecule, bond.b.begin, sgroup1),
);
const endAtom = restruct.atoms.get(
sgroup2 instanceof MonomerMicromolecule
? (sgroup2.getAttachmentAtomId() as number)
: bond.b.end,
ReBond.getAtomPositionForBond(restruct.molecule, bond.b.end, sgroup2),
);

if (
Expand Down
21 changes: 19 additions & 2 deletions packages/ketcher-core/src/domain/entities/sgroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { FunctionalGroup, Pool, SGroupAttachmentPoint } from 'domain/entities';
import { ReSGroup } from 'application/render';
import { SgContexts } from 'application/editor/shared/constants';
import assert from 'assert';
import { isNumber } from 'lodash';

export class SGroupBracketParams {
readonly c: Vec2;
Expand Down Expand Up @@ -329,9 +330,25 @@ export class SGroup {
} {
let atomId = this.attachmentPoints[0]?.atomId;
let representAtom = struct.atoms.get(atomId);
// if there is no attachment points in sgroup - use first externally connected atom if exist or just first atom
if (!representAtom) {
atomId = this.atoms[0];
representAtom = struct.atoms.get(this.atoms[0]);
let externalConnectionAtom;
struct.bonds.forEach((bond) => {
const isBeginAtomInCurrentSgroup =
this.atoms.indexOf(bond.begin) !== -1;
const isEndAtomInCurrentSgroup = this.atoms.indexOf(bond.end) !== -1;

if (isBeginAtomInCurrentSgroup && !isEndAtomInCurrentSgroup) {
externalConnectionAtom = bond.begin;
} else if (isEndAtomInCurrentSgroup && !isBeginAtomInCurrentSgroup) {
externalConnectionAtom = bond.end;
}
});

atomId = isNumber(externalConnectionAtom)
? externalConnectionAtom
: this.atoms[0];
representAtom = struct.atoms.get(atomId);
}
assert(representAtom != null);
return { atomId, position: representAtom.pp };
Expand Down

0 comments on commit d00edaa

Please sign in to comment.