Skip to content

Commit

Permalink
#3949 - Parameter "sgroups" does not appear in .ket file if a S-Group…
Browse files Browse the repository at this point in the history
… is applied to structure along with molecule (#3975)

- fixed serialization/deserialization in case when one sgroup contains several fragments

---------

Co-authored-by: Roman Rodionov <roman_rodionov@epam.com>
  • Loading branch information
rrodionov91 and rrodionov91 authored Jan 29, 2024
1 parent b5ab5f2 commit 0422822
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ test.describe('Checking reaction queries attributes in SMARTS format', () => {
});

test('Checking SMARTS with S-Group with two elements', async ({ page }) => {
test.fail();
/**
* Test case: https://github.com/epam/Indigo/issues/1316
*/
Expand Down
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 @@ -250,6 +250,46 @@ export class MacromoleculesConverter {
return attachmentPointLabel;
}

// This method returns array of arrays of fragmentIds grouped by sgroup
// It needs to serialize/deserialize several molecules grouped by sgroup as a single molecule
public static getFragmentsGroupedBySgroup(struct: Struct) {
const groupedFragments: number[][] = [];
struct.frags.forEach((_fragment, fragmentId) => {
const isAlreadyGrouped = groupedFragments.find((fragmentsGroup) =>
fragmentsGroup.includes(fragmentId),
);
if (isAlreadyGrouped) {
return;
}

// Find all sgroups related to fragment
const fragmentSgroups = new Set<SGroup>();
struct.atoms.forEach((atom, atomId) => {
if (atom.fragment !== fragmentId) return;
const sgroup = struct.getGroupFromAtomId(atomId);
if (sgroup) {
fragmentSgroups.add(sgroup);
}
});

// Add new group of fragments with fragments related to one sgroup
const lastFragmentGroupIndex = groupedFragments.push([fragmentId]) - 1;
fragmentSgroups.forEach((sgroup) => {
sgroup.atoms.forEach((aid) => {
const atomFragmentId = struct.atoms.get(aid)?.fragment;
if (
atomFragmentId &&
!groupedFragments[lastFragmentGroupIndex].includes(atomFragmentId)
) {
groupedFragments[lastFragmentGroupIndex].push(atomFragmentId);
}
});
});
});

return groupedFragments;
}

public static convertStructToDrawingEntities(
struct: Struct,
drawingEntitiesManager: DrawingEntitiesManager,
Expand All @@ -268,9 +308,11 @@ export class MacromoleculesConverter {
);
}
});
const fragments = this.getFragmentsGroupedBySgroup(struct);

let fragmentNumber = 1;
struct.frags.forEach((_fragment, fragmentId) => {
const fragmentStruct = struct.getFragment(fragmentId, false);
fragments.forEach((_fragment, fragmentId) => {
const fragmentStruct = struct.getFragment(_fragment, false);
const monomerAddCommand = this.convertFragmentToChem(
fragmentNumber,
fragmentStruct,
Expand Down
8 changes: 4 additions & 4 deletions packages/ketcher-core/src/domain/entities/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,17 @@ export class Struct {
return this.clone(atomSet);
}

getFragmentIds(fid: number): Pile<number> {
getFragmentIds(_fid: number | number[]): Pile<number> {
const atomSet = new Pile<number>();

const fid = Array.isArray(_fid) ? _fid : [_fid];
this.atoms.forEach((atom, aid) => {
if (atom.fragment === fid) atomSet.add(aid);
if (fid.includes(atom.fragment)) atomSet.add(aid);
});

return atomSet;
}

getFragment(fid: number, copyNonFragmentObjects = true): Struct {
getFragment(fid: number | number[], copyNonFragmentObjects = true): Struct {
return this.clone(
this.getFragmentIds(fid),
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,12 @@ export class KetSerializer implements Serializer<Struct> {
);

let fragmentNumber = 1;
deserializedMicromolecules.frags.forEach((_fragment, fragmentId) => {
const fragments = MacromoleculesConverter.getFragmentsGroupedBySgroup(
deserializedMicromolecules,
);
fragments.forEach((_fragment) => {
const fragmentStruct = deserializedMicromolecules.getFragment(
fragmentId,
_fragment,
false,
);
const fragmentBbox = fragmentStruct.getCoordBoundingBox();
Expand Down

0 comments on commit 0422822

Please sign in to comment.