Skip to content

Commit

Permalink
#3019 - Fix templates not being updated after changing ignoreChiralFl…
Browse files Browse the repository at this point in the history
…ag setting
  • Loading branch information
nanoblit committed Aug 24, 2023
1 parent 1968d77 commit a3b70a9
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 36 deletions.
40 changes: 28 additions & 12 deletions packages/ketcher-core/src/application/render/restruct/reatom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,26 @@ class ReAtom extends ReObject {
// TODO: fragment should not be null
const fragment = restruct.molecule.frags.get(fragmentId);

const text =
(shouldDisplayStereoLabel(
stereoLabel,
options.stereoLabelStyle,
options.ignoreChiralFlag,
fragment?.enhancedStereoFlag,
)
? `${stereoLabel}\n`
: '') +
(queryAttrsText.length > 0 ? `${queryAttrsText}\n` : '') +
(aamText.length > 0 ? `.${aamText}.` : '');
const displayStereoLabel = shouldDisplayStereoLabel(
stereoLabel,
options.stereoLabelStyle,
options.ignoreChiralFlag,
fragment?.enhancedStereoFlag,
);

let text = '';

if (displayStereoLabel) {
text = `${stereoLabel}\n`;
}

if (queryAttrsText.length > 0) {
text += `${queryAttrsText}\n`;
}

if (aamText.length > 0) {
text += `.${aamText}.`;
}

if (text.length > 0) {
const elem = Elements.get(this.a.label);
Expand Down Expand Up @@ -574,12 +583,19 @@ function shouldDisplayStereoLabel(
ignoreChiralFlag,
flag: StereoFlag | undefined,
): boolean {
if (!stereoLabel || ignoreChiralFlag) {
if (!stereoLabel) {
return false;
}

const stereoLabelType = stereoLabel.match(/\D+/g)[0];

if (ignoreChiralFlag && stereoLabelType === StereoLabel.Abs) {
return false;
}
if (ignoreChiralFlag && stereoLabelType !== StereoLabel.Abs) {
return true;
}

switch (labelStyle) {
case StereLabelStyleType.Off:
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,8 @@ class ReEnhancedFlag extends ReObject {
show(restruct: ReStruct, fragmentId: number, options: any): void {
const render = restruct.render;
const fragment = restruct.molecule.frags.get(fragmentId);
const stereoFlag = fragment?.enhancedStereoFlag;

if (!stereoFlag) {
return;
}

// We don't want to show the ovarall label (flag) in case there are different types
// of stereo labels inside the structure ("mixed" and "or" stereo flags indicate that)
if (stereoFlag === StereoFlag.Mixed || stereoFlag === StereoFlag.Or) {
if (!fragment?.enhancedStereoFlag) {
return;
}

Expand All @@ -85,7 +78,13 @@ class ReEnhancedFlag extends ReObject {

if (options.showStereoFlags && !options.ignoreChiralFlag) {
this.#path = paper
.text(ps.x, ps.y, stereoFlag ? stereoFlagMap[stereoFlag] : '')
.text(
ps.x,
ps.y,
fragment.enhancedStereoFlag
? stereoFlagMap[fragment.enhancedStereoFlag]
: '',
)
.attr({
font: options.font,
'font-size': options.fontsz,
Expand Down
10 changes: 5 additions & 5 deletions packages/ketcher-core/src/domain/serializers/mol/mol.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
***************************************************************************/

export interface MolSerializerOptions {
reactionRelayout: boolean;
badHeaderRecover: boolean;
ignoreErrors: boolean;
noRgroups: boolean;
preserveIndigoDesc: boolean;
reactionRelayout?: boolean;
badHeaderRecover?: boolean;
ignoreErrors?: boolean;
noRgroups?: boolean;
preserveIndigoDesc?: boolean;
ignoreChiralFlag?: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ import { SdfItem, StructAssociatedData } from './sdf.types';

import { MolSerializer } from '../mol/molSerializer';
import { Serializer } from '../serializers.types';
import { MolSerializerOptions } from '../mol';

const DelimeterRegex = /^[^]+?\$\$\$\$$/gm;
export class SdfSerializer implements Serializer<Array<SdfItem>> {
private readonly molSerializerOptions?: MolSerializerOptions;

constructor(options?: MolSerializerOptions) {
this.molSerializerOptions = options;
}

deserialize(content: string): Array<SdfItem> {
let m: any;
const result: Array<SdfItem> = [];
const molSerializer = new MolSerializer();
const molSerializer = new MolSerializer(this.molSerializerOptions);
while ((m = DelimeterRegex.exec(content)) !== null) {
const chunk = m[0].replace(/\r/g, '').trim(); // TODO: normalize newline?
const end = chunk.indexOf('M END');
Expand Down Expand Up @@ -55,7 +62,7 @@ export class SdfSerializer implements Serializer<Array<SdfItem>> {
}

serialize(sdfItems: Array<SdfItem>): string {
const molSerializer = new MolSerializer();
const molSerializer = new MolSerializer(this.molSerializerOptions);
return sdfItems.reduce((res, item) => {
res += molSerializer.serialize(item.struct);

Expand Down
2 changes: 2 additions & 0 deletions packages/ketcher-react/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const KETCHER_INIT_EVENT_NAME = 'ketcher-init';

export const KETCHER_SAVED_SETTINGS_KEY = 'ketcher_editor_saved_settings';

export const KETCHER_SAVED_OPTIONS_KEY = 'ketcher-opts';

export const MODES = {
FG: 'fg',
};
Expand Down
8 changes: 6 additions & 2 deletions packages/ketcher-react/src/script/ui/state/options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {

import { pick } from 'lodash/fp';
import { storage } from '../../storage-ext';
import { reinitializeTemplateLibrary } from '../templates/init-lib';
import { KETCHER_SAVED_OPTIONS_KEY } from 'src/constants';

export const initOptionsState = {
app: {
Expand Down Expand Up @@ -59,7 +61,7 @@ export const initOptionsState = {
},
settings: Object.assign(
getDefaultOptions(),
validation(storage.getItem('ketcher-opts')),
validation(storage.getItem(KETCHER_SAVED_OPTIONS_KEY)),
),
getServerSettings() {
return pick(SERVER_OPTIONS, this.settings);
Expand All @@ -75,7 +77,9 @@ export function appUpdate(data) {

/* SETTINGS */
export function saveSettings(newSettings) {
storage.setItem('ketcher-opts', newSettings);
storage.setItem(KETCHER_SAVED_OPTIONS_KEY, newSettings);
reinitializeTemplateLibrary();

return {
type: 'SAVE_SETTINGS',
data: newSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { KetSerializer, SdfSerializer } from 'ketcher-core';
import { appUpdate } from '../options';
import { storage } from '../../storage-ext';
import templatesRawData from '../../../../templates/library.sdf';
import { OptionsManager } from '../../utils/optionsManager';

let cachedInitData: [unknown, unknown, unknown];

export function initLib(lib) {
return {
Expand All @@ -28,7 +31,11 @@ export function initLib(lib) {
}

const deserializeSdfTemplates = (baseUrl, cacheEl, _fileName) => {
const sdfSerializer = new SdfSerializer();
const options = {
ignoreChiralFlag: OptionsManager.ignoreChiralFlag,
};

const sdfSerializer = new SdfSerializer(options);
const tmpls = sdfSerializer.deserialize(templatesRawData);
const prefetch = prefetchRender(tmpls, baseUrl + '/templates/', cacheEl);

Expand All @@ -45,14 +52,27 @@ const deserializeSdfTemplates = (baseUrl, cacheEl, _fileName) => {
};

export default function initTmplLib(dispatch, baseUrl, cacheEl) {
cachedInitData = [dispatch, baseUrl, cacheEl];

const fileName = 'library.sdf';

return deserializeSdfTemplates(baseUrl, cacheEl, fileName).then((res) => {
const lib = res.concat(userTmpls());
const lib = res.concat(userTmpls() as []);
dispatch(initLib(lib));
dispatch(appUpdate({ templates: true }));
});
}

export function reinitializeTemplateLibrary() {
if (!cachedInitData) {
throw new Error(
'The template library must be initialized before it can be reinitialized',
);
}

initTmplLib(...cachedInitData);
}

function userTmpls() {
const userLib = storage.getItem('ketcher-tmpls');
if (!Array.isArray(userLib) || userLib.length === 0) return [];
Expand Down
38 changes: 38 additions & 0 deletions packages/ketcher-react/src/script/ui/utils/optionsManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { KETCHER_SAVED_OPTIONS_KEY } from 'src/constants';
import { storage } from '../storage-ext';

interface SavedOptions {
ignoreChiralFlag?: boolean;
}

export class OptionsManager {
static getOptions(): SavedOptions {
try {
return JSON.parse(storage.getItem(KETCHER_SAVED_OPTIONS_KEY) || '{}');
} catch (e) {
return {} as SavedOptions;
}
}

static saveSettings(settings: SavedOptions) {
if (!settings) {
return;
}
storage.setItem(KETCHER_SAVED_OPTIONS_KEY, JSON.stringify(settings));
}

static get ignoreChiralFlag() {
const { ignoreChiralFlag } = this.getOptions();

return ignoreChiralFlag;
}

static set ignoreChiralFlag(ignoreChiralFlag: boolean | undefined) {
const settings = this.getOptions();

this.saveSettings({
...settings,
ignoreChiralFlag,
});
}
}
7 changes: 3 additions & 4 deletions packages/ketcher-react/src/script/ui/utils/settingsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
***************************************************************************/

import { KETCHER_SAVED_SETTINGS_KEY } from 'src/constants';
import { storage } from '../storage-ext';

interface SavedSettings {
selectionTool?: any;
Expand All @@ -23,9 +24,7 @@ interface SavedSettings {
export class SettingsManager {
static getSettings(): SavedSettings {
try {
return JSON.parse(
localStorage.getItem(KETCHER_SAVED_SETTINGS_KEY) || '{}',
);
return JSON.parse(storage.getItem(KETCHER_SAVED_SETTINGS_KEY) || '{}');
} catch (e) {
return {} as SavedSettings;
}
Expand All @@ -35,7 +34,7 @@ export class SettingsManager {
if (!settings) {
return;
}
localStorage.setItem(KETCHER_SAVED_SETTINGS_KEY, JSON.stringify(settings));
storage.setItem(KETCHER_SAVED_SETTINGS_KEY, JSON.stringify(settings));
}

static get selectionTool() {
Expand Down

0 comments on commit a3b70a9

Please sign in to comment.