From d2dacd772b5483d801facbcfe9ef855e85228403 Mon Sep 17 00:00:00 2001 From: Dmitrii Polovnev Date: Thu, 6 Jun 2024 03:45:50 +0300 Subject: [PATCH] #4607 - Bugfix (Chain.ts): Correct the definition of individual phosphate and linker in `add(1)` --- .../domain/entities/monomer-chains/Chain.ts | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/ketcher-core/src/domain/entities/monomer-chains/Chain.ts b/packages/ketcher-core/src/domain/entities/monomer-chains/Chain.ts index 5cde5140a1..7271a94b20 100644 --- a/packages/ketcher-core/src/domain/entities/monomer-chains/Chain.ts +++ b/packages/ketcher-core/src/domain/entities/monomer-chains/Chain.ts @@ -51,24 +51,36 @@ export class Chain { public add(monomer: BaseMonomer) { this.createSubChainIfNeed(monomer); - const nextMonomer = getNextMonomerInChain(monomer); - - if (monomer instanceof Sugar && isValidNucleoside(monomer)) { - this.lastSubChain.add(Nucleoside.fromSugar(monomer)); - } else if (monomer instanceof Sugar && isValidNucleotide(monomer)) { - this.lastSubChain.add(Nucleotide.fromSugar(monomer)); - } else if (monomer instanceof Peptide) { + if (monomer instanceof Sugar) { + if (isValidNucleoside(monomer)) { + this.lastSubChain.add(Nucleoside.fromSugar(monomer)); + return; + } + if (isValidNucleotide(monomer)) { + this.lastSubChain.add(Nucleotide.fromSugar(monomer)); + return; + } + } + if (monomer instanceof Peptide) { this.lastSubChain.add(new MonomerSequenceNode(monomer)); - } else if ( + return; + } + const nextMonomer = getNextMonomerInChain(monomer); + const isNextMonomerNucleosideOrNucleotideOrPeptide = () => { + const isNucleosideOrNucleotide = + nextMonomer instanceof Sugar && + (isValidNucleotide(nextMonomer) || isValidNucleoside(nextMonomer)); + return isNucleosideOrNucleotide || nextMonomer instanceof Peptide; + }; + if ( monomer instanceof Phosphate && - (this.lastNode instanceof Nucleoside || - (nextMonomer instanceof Sugar && - (isValidNucleotide(nextMonomer) || isValidNucleotide(nextMonomer)))) + this.lastNode instanceof Nucleoside && + (!nextMonomer || isNextMonomerNucleosideOrNucleotideOrPeptide()) ) { this.lastSubChain.add(new MonomerSequenceNode(monomer)); - } else { - this.lastSubChain.add(new LinkerSequenceNode(monomer)); + return; } + this.lastSubChain.add(new LinkerSequenceNode(monomer)); } public addNode(node: SubChainNode) {