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

#5245 - Replacement of peptide at the end of sequence on preset w/out base works wrong #5274

Merged
merged 2 commits into from
Aug 12, 2024
Merged
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
135 changes: 66 additions & 69 deletions packages/ketcher-core/src/application/editor/modes/SequenceMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,7 @@ export class SequenceMode extends BaseMode {
selectionRange.some((nodeSelection) =>
[preset.sugar, preset.base, preset.phosphate].some(
(monomer) =>
monomer &&
!this.checkIfNewMonomerCouldEstablishConnections(
nodeSelection,
monomer,
Expand Down Expand Up @@ -1364,6 +1365,53 @@ export class SequenceMode extends BaseMode {
}
}

private createRnaPresetNode(preset: IRnaPreset, position: Vec2) {
const editor = CoreEditor.provideEditorInstance();
const { base: rnaBase, sugar, phosphate } = preset;

if (!sugar) {
return;
}

const rnaPresetAddResult = editor.drawingEntitiesManager.addRnaPreset({
sugar,
sugarPosition: position,
rnaBase,
rnaBasePosition: position,
phosphate,
phosphatePosition: position,
});

const sugarMonomer = rnaPresetAddResult.monomers.find(
(monomer) => monomer instanceof Sugar,
) as Sugar;
const rnaBaseMonomer = rnaPresetAddResult.monomers.find(
(monomer) => monomer instanceof RNABase,
) as RNABase;
const phosphateMonomer = rnaPresetAddResult.monomers.find(
(monomer) => monomer instanceof Phosphate,
) as Phosphate;

let newPresetNode: Nucleotide | Nucleoside | LinkerSequenceNode;

if (!rnaBase) {
newPresetNode = new LinkerSequenceNode(sugarMonomer);
} else if (!phosphateMonomer) {
newPresetNode = new Nucleoside(sugarMonomer, rnaBaseMonomer);
} else {
newPresetNode = new Nucleotide(
sugarMonomer,
rnaBaseMonomer,
phosphateMonomer,
);
}

return {
newPresetNode,
rnaPresetAddModelChanges: rnaPresetAddResult.command,
};
}

private replaceSelectionWithPreset(
preset: IRnaPreset,
selection: NodeSelection,
Expand All @@ -1389,44 +1437,15 @@ export class SequenceMode extends BaseMode {
});
});

const { base: rnaBase, sugar, phosphate } = preset;
const rnaAdditionResult = this.createRnaPresetNode(preset, position);

if (!rnaBase || !sugar) {
if (!rnaAdditionResult) {
return;
}

const monomerAddCommand = editor.drawingEntitiesManager.addRnaPreset({
sugar,
sugarPosition: position,
rnaBase,
rnaBasePosition: position,
phosphate,
phosphatePosition: position,
});

const sugarMonomer = monomerAddCommand.monomers.find(
(monomer) => monomer instanceof Sugar,
) as Sugar;
const rnaBaseMonomer = monomerAddCommand.monomers.find(
(monomer) => monomer instanceof RNABase,
) as RNABase;
const phosphateMonomer = monomerAddCommand.monomers.find(
(monomer) => monomer instanceof Phosphate,
) as Phosphate;

modelChanges.merge(monomerAddCommand.command);

let newPresetNode: Nucleotide | Nucleoside;
if (phosphateMonomer) {
newPresetNode = new Nucleotide(
sugarMonomer,
rnaBaseMonomer,
phosphateMonomer,
);
} else {
newPresetNode = new Nucleoside(sugarMonomer, rnaBaseMonomer);
}
const { newPresetNode, rnaPresetAddModelChanges } = rnaAdditionResult;

modelChanges.merge(rnaPresetAddModelChanges);
modelChanges.merge(
this.insertNewSequenceFragment(
newPresetNode,
Expand All @@ -1439,7 +1458,11 @@ export class SequenceMode extends BaseMode {

// TODO: This check breaks some side chains (e.g. Sugar-to-Sugar for Nucleotides), need another way of preserving connections
const monomerForSideConnections =
newPresetNode instanceof Nucleotide ? phosphateMonomer : sugarMonomer;
newPresetNode instanceof Nucleotide
? newPresetNode.phosphate
: newPresetNode instanceof Nucleoside
? newPresetNode.sugar
: newPresetNode.monomer;

sideChainConnections?.forEach((sideConnectionData) => {
const {
Expand Down Expand Up @@ -1545,45 +1568,19 @@ export class SequenceMode extends BaseMode {
} else {
const newNodePosition = this.getNewNodePosition();

const { base: rnaBase, sugar, phosphate } = preset;
const rnaAdditionResult = this.createRnaPresetNode(
preset,
newNodePosition,
);

if (!rnaBase || !sugar) {
if (!rnaAdditionResult) {
return;
}

const monomerAddCommand = editor.drawingEntitiesManager.addRnaPreset({
sugar,
sugarPosition: newNodePosition,
rnaBase,
rnaBasePosition: newNodePosition,
phosphate,
phosphatePosition: newNodePosition,
});

const sugarMonomer = monomerAddCommand.monomers.find(
(monomer) => monomer instanceof Sugar,
) as Sugar;
const rnaBaseMonomer = monomerAddCommand.monomers.find(
(monomer) => monomer instanceof RNABase,
) as RNABase;
const phosphateMonomer = monomerAddCommand.monomers.find(
(monomer) => monomer instanceof Phosphate,
) as Phosphate;

modelChanges.merge(monomerAddCommand.command);

let newPresetNode: Nucleotide | Nucleoside;
if (phosphateMonomer) {
newPresetNode = new Nucleotide(
sugarMonomer,
rnaBaseMonomer,
phosphateMonomer,
);
} else {
newPresetNode = new Nucleoside(sugarMonomer, rnaBaseMonomer);
}

modelChanges.merge(this.insertNewSequenceFragment(newPresetNode));
modelChanges.merge(rnaAdditionResult.rnaPresetAddModelChanges);
modelChanges.merge(
this.insertNewSequenceFragment(rnaAdditionResult.newPresetNode),
);

modelChanges.addOperation(new ReinitializeModeOperation());
editor.renderersContainer.update(modelChanges);
Expand Down
Loading