From 2a680bce4b8333f46e779baca4dd41db3bd639a2 Mon Sep 17 00:00:00 2001 From: RicardoSilvestr Date: Wed, 23 Aug 2023 14:38:56 +0200 Subject: [PATCH] feat: getProductsTrees in order to get products tree only when necessary --- .../__snapshots__/applyReactions.test.js.snap | 2 ++ src/reaction/__tests__/applyReactions.test.js | 28 ++++++++++++++++--- src/reaction/applyReactions.js | 14 ++++++++-- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/reaction/__tests__/__snapshots__/applyReactions.test.js.snap b/src/reaction/__tests__/__snapshots__/applyReactions.test.js.snap index 598932e..dac0b6c 100644 --- a/src/reaction/__tests__/__snapshots__/applyReactions.test.js.snap +++ b/src/reaction/__tests__/__snapshots__/applyReactions.test.js.snap @@ -32143,6 +32143,8 @@ M END } `; +exports[`applyReactions > ethanol with limitReactions:5 and products false 1`] = `undefined`; + exports[`applyReactions > ethylene glycol 1`] = ` { "charge": 0, diff --git a/src/reaction/__tests__/applyReactions.test.js b/src/reaction/__tests__/applyReactions.test.js index 9082a73..1839e4a 100644 --- a/src/reaction/__tests__/applyReactions.test.js +++ b/src/reaction/__tests__/applyReactions.test.js @@ -11,7 +11,7 @@ describe('applyReactions', () => { const { trees, products, stats } = applyReactions( [ethanol], reactionsDatabase, - {}, + { getProductsTrees: true }, ); expect(stats.counter).toBe(32); removeCoordinates(trees, products); @@ -35,7 +35,7 @@ describe('applyReactions', () => { const { trees, products, stats } = applyReactions( [ethanol], reactionsDatabase, - { limitReactions: 5 }, + { limitReactions: 5, getProductsTrees: true }, ); expect(stats.counter).toBe(5); @@ -50,10 +50,27 @@ describe('applyReactions', () => { expect(firstProduct.mf).toBe('C2H6S'); expect(firstProduct.children).toHaveLength(0); }); + it('ethanol with limitReactions:5 and products false', () => { + const ethanol = Molecule.fromSmiles('CCO'); + const { trees, products, stats } = applyReactions( + [ethanol], + reactionsDatabase, + { limitReactions: 5, getProductsTrees: false }, + ); + expect(stats.counter).toBe(5); + removeCoordinates(trees, products); + + expect(products[0]).toMatchSnapshot(); + + expect(trees).toHaveLength(2); + expect(products).toHaveLength(0); + }); it('ethylene glycol', () => { const diol = Molecule.fromSmiles('OCCO'); - const { trees, products } = applyReactions([diol], reactionsDatabase, {}); + const { trees, products } = applyReactions([diol], reactionsDatabase, { + getProductsTrees: true, + }); removeCoordinates(trees, products); expect(products[0]).toMatchSnapshot(); expect(trees).toHaveLength(2); @@ -80,7 +97,7 @@ describe('applyReactions', () => { const { trees, products } = applyReactions( [propanediol], reactionsDatabase, - {}, + { getProductsTrees: true }, ); removeCoordinates(trees, products); expect(products[0]).toMatchSnapshot(); @@ -116,6 +133,7 @@ describe('applyReactions', () => { reactionsDatabase, { maxDepth: 1, + getProductsTrees: true, }, ); removeCoordinates(trees, products); @@ -135,6 +153,7 @@ describe('applyReactions', () => { reactionsDatabase, { maxDepth: 5, + getProductsTrees: true, }, ); @@ -163,6 +182,7 @@ describe('applyReactions', () => { const molecule = Molecule.fromSmiles('OCOCO'); const { products } = applyReactions([molecule], reactionsDatabase, { maxDepth: 5, + getProductsTrees: true, }); removeCoordinates(undefined, products); expect(products).toMatchSnapshot(); diff --git a/src/reaction/applyReactions.js b/src/reaction/applyReactions.js index 4f53e9c..611efdb 100644 --- a/src/reaction/applyReactions.js +++ b/src/reaction/applyReactions.js @@ -7,13 +7,18 @@ import { groupTreesByProducts } from './utils/groupTreesByProducts.js'; * @param {object} options options to apply the reaction * @param {number} [options.maxDepth=5] max depth of the recursion * @param {number} [options.limitReactions=5000] limit the number of reactions to apply + * @param {boolean} [options.getProductsTrees=false] if true, the returned object will have a products property with the products trees grouped by idCode else it will be an empty array * @returns {Object} The returned object has two properties: * - trees: the tree of reactions * - products: reactions trees grouped by product idCode */ export function applyReactions(reactants, reactions, options = {}) { // Reaction are applied recursively until maximal tree depth is reached (default 10) - const { maxDepth = 5, limitReactions = 5000 } = options; + const { + maxDepth = 5, + limitReactions = 5000, + getProductsTrees = false, + } = options; const moleculesInfo = new Map(); const processedMolecules = new Set(); if (!reactants.length) { @@ -44,7 +49,12 @@ export function applyReactions(reactants, reactions, options = {}) { } todoCurrentLevel = nexts.flat(); } while (todoCurrentLevel.length > 0); - const products = groupTreesByProducts(trees); + let products; + if (getProductsTrees) { + products = groupTreesByProducts(trees); + } else { + products = []; + } return { trees, products, stats }; }