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

#3949 - Parameter "sgroups" does not appear in .ket file if a S-Group is applied to structure along with molecule #3975

Merged
merged 3 commits into from
Jan 29, 2024
Merged
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
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
Loading