Skip to content

Commit

Permalink
Backmerge: #4879 - Ketcher part: Save to mol for micro and macro stru…
Browse files Browse the repository at this point in the history
…ctures connected through attachment points works wrong (#4970)

- added removing the leavingGroup atom from structure before serialisation if there is connection to monomer

* #4973 - Connection between molecule and monomer lost after opening and saving to ket

- fixed monomer atoms ids mapping during struct clone
- fixed removeLeavingGroupsFromConnectedAtoms
  • Loading branch information
rrodionov91 authored Jul 4, 2024
1 parent e42b47d commit c1c36a4
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 25 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,15 @@
13.044183315187814,
-7.56543102512855,
0
],
"selected": true
]
},
{
"label": "H",
"location": [
14.29031242217327,
-6.319190021917031,
0
],
"selected": true
]
}
],
"bonds": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@

-INDIGO-06142409592D
-INDIGO-07032413592D

0 0 0 0 0 0 0 0 0 0 0 V3000
M V30 BEGIN CTAB
M V30 COUNTS 8 8 1 0 0
M V30 COUNTS 7 7 1 0 0
M V30 BEGIN ATOM
M V30 1 C 10.569 -8.0 0.0 0
M V30 2 C 12.2993 -7.99959 0.0 0
M V30 3 C 11.4358 -7.49997 0.0 0
M V30 4 C 12.2993 -9.00053 0.0 0
M V30 5 C 10.569 -9.00502 0.0 0
M V30 6 C 11.438 -9.50003 0.0 0
M V30 7 H 11.4364 -6.49997 0.0 0
M V30 8 A6OH 14.9592 -7.2 0.0 0 CLASS=LINKER ATTCHORD=(2 3 Br)
M V30 7 A6OH 14.9592 -7.2 0.0 0 CLASS=LINKER ATTCHORD=(2 3 Br)
M V30 END ATOM
M V30 BEGIN BOND
M V30 1 2 3 1
Expand All @@ -22,13 +21,12 @@ M V30 4 1 6 4
M V30 5 2 4 2
M V30 6 1 2 3
M V30 7 1 3 7
M V30 8 1 3 8
M V30 END BOND
M V30 BEGIN SGROUP
M V30 1 SUP 1 ATOMS=(7 3 1 5 6 4 2 7) BRKXYZ=(9 0.000000 0.000000 0.000000 0-
M V30 .000000 0.000000 0.000000 0.000000 0.000000 0.000000) BRKXYZ=(9 0.0000-
M V30 00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.00-
M V30 0000) ESTATE=E SAP=(3 3 7 1)
M V30 1 SUP 1 ATOMS=(6 3 1 5 6 4 2) BRKXYZ=(9 0.000000 0.000000 0.000000 0.0-
M V30 00000 0.000000 0.000000 0.000000 0.000000 0.000000) BRKXYZ=(9 0.000000-
M V30 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.0000-
M V30 00) ESTATE=E SAP=(3 3 0 1)
M V30 END SGROUP
M V30 END CTAB
M V30 BEGIN TEMPLATE
Expand Down
4 changes: 2 additions & 2 deletions packages/ketcher-core/src/domain/entities/BaseMonomer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export abstract class BaseMonomer extends DrawingEntity {
},
);

if (bondsToLeavingGroupAtom.size !== 1) {
if (bondsToLeavingGroupAtom.size > 1) {
return;
}

Expand Down Expand Up @@ -456,7 +456,7 @@ export abstract class BaseMonomer extends DrawingEntity {
},
);

if (bondsToLeavingGroupAtom.size !== 1) {
if (bondsToLeavingGroupAtom.size > 1) {
return;
}

Expand Down
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/sgroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class SGroup {
mul: 1, // multiplication count for MUL group
connectivity: 'ht', // head-to-head, head-to-tail or either-unknown
name: '',
subscript: 'n',
subscript: '',
expanded: undefined,
// data s-group fields
attached: false,
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
61 changes: 59 additions & 2 deletions packages/ketcher-core/src/domain/serializers/ket/ketSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
* limitations under the License.
***************************************************************************/

import { Struct, Vec2 } from 'domain/entities';
import {
Atom,
Bond,
SGroupAttachmentPoint,
Struct,
Vec2,
} from 'domain/entities';
import { arrowToKet, plusToKet } from './toKet/rxnToKet';
import { Serializer } from '../serializers.types';
import { headerToKet } from './toKet/headerToKet';
Expand Down Expand Up @@ -625,11 +631,62 @@ export class KetSerializer implements Serializer<Struct> {
};
}

public static removeLeavingGroupsFromConnectedAtoms(_struct: Struct) {
const struct = _struct.clone();

struct.atoms.forEach((_atom, atomId) => {
if (Atom.isHiddenLeavingGroupAtom(struct, atomId)) {
struct.atoms.delete(atomId);
}
});

struct.bonds.forEach((bond, bondId) => {
if (Bond.isBondToHiddenLeavingGroup(struct, bond)) {
struct.bonds.delete(bondId);
}
});

struct.sgroups.forEach((sgroup) => {
const attachmentPoints = sgroup.getAttachmentPoints();
const attachmentPointsToReplace: Map<
SGroupAttachmentPoint,
SGroupAttachmentPoint
> = new Map();
attachmentPoints.forEach((attachmentPoint) => {
if (
isNumber(attachmentPoint.leaveAtomId) &&
Atom.isHiddenLeavingGroupAtom(struct, attachmentPoint.leaveAtomId)
) {
const attachmentPointClone = new SGroupAttachmentPoint(
attachmentPoint.atomId,
undefined,
attachmentPoint.attachmentId,
attachmentPoint.attachmentPointNumber,
);
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;
}

serialize(
struct: Struct,
_struct: Struct,
drawingEntitiesManager = new DrawingEntitiesManager(),
selection?: EditorSelection,
) {
const struct = KetSerializer.removeLeavingGroupsFromConnectedAtoms(_struct);
struct.enableInitiallySelected();
const populatedStruct = populateStructWithSelection(struct, selection);
MacromoleculesConverter.convertStructToDrawingEntities(
Expand Down
16 changes: 13 additions & 3 deletions packages/ketcher-core/src/domain/serializers/mol/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
***************************************************************************/

import { Pile, SGroup } from 'domain/entities';
import { MonomerMicromolecule, Pile, SGroup } from 'domain/entities';

import utils from './utils';
import v2000 from './v2000';
Expand Down Expand Up @@ -202,9 +202,19 @@ function saveSupToMolfile(sgroup, mol, sgMap, atomMap, bondMap) {
let lines = [];
lines = lines.concat(makeAtomBondLines('SAL', idstr, sgroup.atoms, atomMap));
lines = lines.concat(makeAtomBondLines('SBL', idstr, sgroup.bonds, bondMap));
if (sgroup.data.name && sgroup.data.name !== '') {
lines.push('M SMT ' + idstr + ' ' + sgroup.data.name);

let sgroupName;

if (sgroup instanceof MonomerMicromolecule) {
sgroupName = sgroup.monomer.label;
} else if (sgroup.data.name && sgroup.data.name !== '') {
sgroupName = sgroup.data.name;
}

if (sgroupName) {
lines.push('M SMT ' + idstr + ' ' + sgroupName);
}

return lines.join('\n');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Molfile } from './molfile';
import { Serializer } from '../serializers.types';
import { Struct } from 'domain/entities';
import { KetcherLogger } from 'utilities';
import { KetSerializer } from 'domain/serializers';

export class MolSerializer implements Serializer<Struct> {
static DefaultOptions: MolSerializerOptions = {
Expand Down Expand Up @@ -75,7 +76,9 @@ export class MolSerializer implements Serializer<Struct> {
}
}

serialize(struct: Struct): string {
serialize(_struct: Struct): string {
const struct = KetSerializer.removeLeavingGroupsFromConnectedAtoms(_struct);

return new Molfile().saveMolecule(
struct,
this.options.ignoreErrors,
Expand Down

0 comments on commit c1c36a4

Please sign in to comment.