Skip to content

Commit

Permalink
#1810 – fixed the layout for s-group name, when pasting from Molfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitvex committed Nov 18, 2022
1 parent f831891 commit 4e0d533
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ class ReAtom extends ReObject {
false
)
) {
if (FunctionalGroup.isFirstAtomInFunctionalGroup(sgroups, aid)) {
if (FunctionalGroup.isAttachmentPointAtom(aid, restruct.molecule)) {
let sgroupName
for (const sg of sgroups.values()) {
if (aid === sg.atoms[0]) sgroupName = sg.data.name
if (sg.atoms.includes(aid)) sgroupName = sg.data.name
}
const path = render.paper.text(ps.x, ps.y, sgroupName).attr({
'font-weight': 700,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ class ReSGroup extends ReObject {
if (
FunctionalGroup.isContractedFunctionalGroup(sgroup.id, functionalGroups)
) {
sgroup.firstSgroupAtom = remol.molecule.atoms.get(sgroup.atoms[0])
sgroup.functionalGroup = true
sgroup.atoms.forEach((aid) => {
if (FunctionalGroup.isAttachmentPointAtom(aid, remol.molecule)) {
sgroup.firstSgroupAtom = remol.molecule.atoms.get(aid)
sgroup.functionalGroup = true
}
})
} else {
switch (sgroup.type) {
case 'MUL':
Expand Down
39 changes: 39 additions & 0 deletions packages/ketcher-core/src/domain/entities/functionalGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
***************************************************************************/
import { FunctionalGroupsProvider } from '../helpers'
import { SGroup } from './sgroup'
import { Struct } from './struct'
import assert from 'assert'

export class FunctionalGroup {
Expand Down Expand Up @@ -99,6 +100,44 @@ export class FunctionalGroup {
return new FunctionalGroup(functionalGroup.#sgroup)
}

/**
* This function determines, if an atom is used for attachment to other structure.
* For example, having sgroup CF3, which looks like
* F
* |
* F-C-F
* |
* other struct
* C – is an attachment point
*/
static isAttachmentPointAtom(atomId: number, molecule: Struct): boolean {
const { sgroups, bonds } = molecule
const isAtomInSameFunctionalGroup = (otherAtomId, sgroup) =>
sgroup.atoms.includes(otherAtomId)
for (const sgroup of sgroups.values()) {
const isFunctionalGroup = FunctionalGroup.isFunctionalGroup(sgroup)
const isSGroupFound = sgroup.atoms.includes(atomId)
if (!isFunctionalGroup || !isSGroupFound) {
continue
}
for (const bond of bonds.values()) {
const isBondBeginInSGroupAndBondEndOutside =
bond.begin === atomId &&
!isAtomInSameFunctionalGroup(bond.end, sgroup)
const isBondEndInSGroupAndBondBeginOutside =
bond.end === atomId &&
!isAtomInSameFunctionalGroup(bond.begin, sgroup)
const isAttachmentBond =
isBondBeginInSGroupAndBondEndOutside ||
isBondEndInSGroupAndBondBeginOutside
if (isAttachmentBond) {
return true
}
}
}
return false
}

static isFirstAtomInFunctionalGroup(sgroups, aid): boolean {
for (const sg of sgroups.values()) {
if (FunctionalGroup.isFunctionalGroup(sg) && aid === sg.atoms[0]) {
Expand Down

0 comments on commit 4e0d533

Please sign in to comment.