Skip to content

Commit

Permalink
#5242 - Replace phosphate at the end with petide causes cycled polymer (
Browse files Browse the repository at this point in the history
#5271)

- changed usage of previous/next node to previous/next node in same chain for replacing sequence items through the library
- fixed replacement of single selection of two chains
  • Loading branch information
rrodionov91 authored Aug 9, 2024
1 parent d8ee4f6 commit f87ac65
Showing 1 changed file with 51 additions and 24 deletions.
75 changes: 51 additions & 24 deletions packages/ketcher-core/src/application/editor/modes/SequenceMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,10 +1086,13 @@ export class SequenceMode extends BaseMode {
previousSelectionNode?: SubChainNode,
) {
const editor = CoreEditor.provideEditorInstance();
const nextNode = SequenceRenderer.getNextNode(selection.node);
const nextNode = SequenceRenderer.getNextNodeInSameChain(selection.node);
const position = selection.node.monomer.position;

const sideChainConnections = this.preserveSideChainConnections(selection);
const hasPreviousNodeInChain =
selection.node.firstMonomerInNode.attachmentPointsToBonds.R1;
const hasNextNodeInChain =
selection.node.lastMonomerInNode.attachmentPointsToBonds.R2;

selection.node.monomers.forEach((monomer) => {
modelChanges.merge(editor.drawingEntitiesManager.deleteMonomer(monomer));
Expand All @@ -1111,8 +1114,10 @@ export class SequenceMode extends BaseMode {
modelChanges.merge(
this.insertNewSequenceFragment(
newMonomerSequenceNode,
nextNode,
nextNode || null,
previousSelectionNode,
Boolean(hasPreviousNodeInChain),
Boolean(hasNextNodeInChain),
),
);

Expand Down Expand Up @@ -1156,11 +1161,15 @@ export class SequenceMode extends BaseMode {
const modelChanges = new Command();

selections.forEach((selectionRange) => {
let previousReplacedNode = SequenceRenderer.getNodeByPointer(
selectionRange[0].nodeIndexOverall - 1,
let previousReplacedNode = SequenceRenderer.getPreviousNodeInSameChain(
selectionRange[0].node,
);

selectionRange.forEach((nodeSelection) => {
if (nodeSelection.node instanceof EmptySequenceNode) {
return;
}

previousReplacedNode = this.replaceSelectionWithMonomer(
monomerItem,
nodeSelection,
Expand Down Expand Up @@ -1362,8 +1371,12 @@ export class SequenceMode extends BaseMode {
previousSelectionNode?: SubChainNode,
) {
const editor = CoreEditor.provideEditorInstance();
const nextNode = SequenceRenderer.getNextNode(selection.node);
const nextNode = SequenceRenderer.getNextNodeInSameChain(selection.node);
const position = selection.node.monomer.position;
const hasPreviousNodeInChain =
selection.node.firstMonomerInNode.attachmentPointsToBonds.R1;
const hasNextNodeInChain =
selection.node.lastMonomerInNode.attachmentPointsToBonds.R2;

const sideChainConnections = this.preserveSideChainConnections(selection);

Expand Down Expand Up @@ -1417,8 +1430,10 @@ export class SequenceMode extends BaseMode {
modelChanges.merge(
this.insertNewSequenceFragment(
newPresetNode,
nextNode,
nextNode || null,
previousSelectionNode,
Boolean(hasPreviousNodeInChain),
Boolean(hasNextNodeInChain),
),
);

Expand Down Expand Up @@ -1465,11 +1480,15 @@ export class SequenceMode extends BaseMode {
const modelChanges = new Command();

selections.forEach((selectionRange) => {
let previousReplacedNode = SequenceRenderer.getNodeByPointer(
selectionRange[0].nodeIndexOverall - 1,
let previousReplacedNode = SequenceRenderer.getPreviousNodeInSameChain(
selectionRange[0].node,
);

selectionRange.forEach((nodeSelection) => {
if (nodeSelection.node instanceof EmptySequenceNode) {
return;
}

previousReplacedNode = this.replaceSelectionWithPreset(
preset,
nodeSelection,
Expand Down Expand Up @@ -1629,8 +1648,10 @@ export class SequenceMode extends BaseMode {

private insertNewSequenceFragment(
chainsCollectionOrNode: ChainsCollection | SubChainNode,
nextNodeToConnect?: SubChainNode,
nextNodeToConnect?: SubChainNode | null,
previousNodeToConnect?: SubChainNode,
needConnectWithPreviousNodeInChain = true,
needConnectWithNextNodeInChain = true,
) {
const chainsCollection =
chainsCollectionOrNode instanceof ChainsCollection
Expand All @@ -1639,7 +1660,9 @@ export class SequenceMode extends BaseMode {
new Chain().addNode(chainsCollectionOrNode),
);
const currentNode =
nextNodeToConnect || SequenceRenderer.currentEdittingNode;
nextNodeToConnect === null
? undefined
: nextNodeToConnect || SequenceRenderer.currentEdittingNode;
const previousNodeInSameChain =
previousNodeToConnect || SequenceRenderer.previousNodeInSameChain;
const modelChanges = new Command();
Expand All @@ -1649,20 +1672,24 @@ export class SequenceMode extends BaseMode {

this.deleteBondToNextNodeInChain(previousNodeInSameChain, modelChanges);

this.connectNodes(
previousNodeInSameChain,
firstNodeOfNewFragment,
modelChanges,
newNodePosition,
currentNode,
);
if (needConnectWithPreviousNodeInChain) {
this.connectNodes(
previousNodeInSameChain,
firstNodeOfNewFragment,
modelChanges,
newNodePosition,
currentNode,
);
}

this.connectNodes(
lastNodeOfNewFragment,
currentNode,
modelChanges,
newNodePosition,
);
if (needConnectWithNextNodeInChain) {
this.connectNodes(
lastNodeOfNewFragment,
currentNode,
modelChanges,
newNodePosition,
);
}

return modelChanges;
}
Expand Down

0 comments on commit f87ac65

Please sign in to comment.