Skip to content

Commit

Permalink
Two sense to antisense connection order problem
Browse files Browse the repository at this point in the history
  • Loading branch information
aproskurnov committed Jan 31, 2025
1 parent 599a441 commit f7e78c1
Showing 1 changed file with 91 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ export class ChainsCollection {
});
});
});

this.chains = [...reorderedChains.values()];

this.reorderChainsPutSenseChainOrderInAccordanceAntisenseConnection();
}

public add(chain: Chain) {
Expand Down Expand Up @@ -370,6 +373,90 @@ export class ChainsCollection {
return undefined;
}

private getComplimentaryChainIfNucleotide(node: SubChainNode) {
const monomerToChain = this.monomerToChain;

const { monomer, complimentaryMonomer } =
this.getFirstAntisenseMonomerInNode(node) || {};
const complimentaryNode =
complimentaryMonomer && this.monomerToNode.get(complimentaryMonomer);
const complimentaryChain =
complimentaryMonomer && monomerToChain.get(complimentaryMonomer);

const isRnaMonomer =
isRnaBaseOrAmbiguousRnaBase(monomer) &&
Boolean(getSugarFromRnaBase(monomer));
const isRnaComplimentaryMonomer =
isRnaBaseOrAmbiguousRnaBase(complimentaryMonomer) &&
Boolean(getSugarFromRnaBase(complimentaryMonomer));

if (
!complimentaryNode ||
!complimentaryChain ||
!isRnaMonomer ||
!isRnaComplimentaryMonomer
) {
return null;
}
return { complimentaryChain, complimentaryNode };
}

private reorderChainsPutSenseChainOrderInAccordanceAntisenseConnection() {
const handledChain = new Set<Chain>();
const reorderedSenseForSequentialAntisenseChains: Chain[] = new Array(
this.chains.length,
);
this.chains.forEach((chain) => {
if (!handledChain.has(chain)) {
reorderedSenseForSequentialAntisenseChains[handledChain.size] = chain;
handledChain.add(chain);
}

if (chain.isAntisense) {
return;
}

chain.forEachNode(({ node: sNode }) => {
const {
complimentaryChain: antisenseChain,
complimentaryNode: antisenseNode,
} = this.getComplimentaryChainIfNucleotide(sNode) ?? {};
if (!antisenseChain) {
return;
}

let isFindCur = false;
antisenseChain.forEachNode(({ node: aNode }) => {
if (aNode === antisenseNode) {
isFindCur = true;
}
if (!isFindCur) {
const { complimentaryChain: anotherSenseChain } =
this.getComplimentaryChainIfNucleotide(aNode) ?? {};
if (anotherSenseChain && !handledChain.has(anotherSenseChain)) {
const curChainIdx =
reorderedSenseForSequentialAntisenseChains.findIndex(
(v) => v === chain,
);
let last = anotherSenseChain;
for (
let i = curChainIdx;
i < reorderedSenseForSequentialAntisenseChains.length;
i++
) {
const tmp = reorderedSenseForSequentialAntisenseChains[i];
reorderedSenseForSequentialAntisenseChains[i] = last;
last = tmp;
}
handledChain.add(anotherSenseChain);
}
}
});
});
});
this.chains = [...reorderedSenseForSequentialAntisenseChains];
}

// for example
// 1 x x x
// |
Expand All @@ -385,32 +472,14 @@ export class ChainsCollection {

const res: GrouppedChain[] = [{ group: 0, chain: c }];
const handledChains = new Set<Chain>([c]);
const monomerToChain = this.monomerToChain;

while (chains.length) {
const { group, chain } = chains.pop() as GrouppedChain;
chain.forEachNode(({ node }) => {
const { monomer, complimentaryMonomer } =
this.getFirstAntisenseMonomerInNode(node) || {};
const complimentaryNode =
complimentaryMonomer && this.monomerToNode.get(complimentaryMonomer);
const complimentaryChain =
complimentaryMonomer && monomerToChain.get(complimentaryMonomer);

const isRnaMonomer =
isRnaBaseOrAmbiguousRnaBase(monomer) &&
Boolean(getSugarFromRnaBase(monomer));
const isRnaComplimentaryMonomer =
isRnaBaseOrAmbiguousRnaBase(complimentaryMonomer) &&
Boolean(getSugarFromRnaBase(complimentaryMonomer));

if (
!complimentaryNode ||
!complimentaryChain ||
!isRnaMonomer ||
!isRnaComplimentaryMonomer ||
handledChains.has(complimentaryChain)
) {
const { complimentaryChain } =
this.getComplimentaryChainIfNucleotide(node) ?? {};

if (!complimentaryChain || handledChains.has(complimentaryChain)) {
return;
}

Expand Down

0 comments on commit f7e78c1

Please sign in to comment.