Skip to content

Commit

Permalink
feat: getProductsTrees in order to get products tree only when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoSilvestr committed Aug 23, 2023
1 parent aae30c0 commit 2a680bc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32143,6 +32143,8 @@ M END
}
`;

exports[`applyReactions > ethanol with limitReactions:5 and products false 1`] = `undefined`;

exports[`applyReactions > ethylene glycol 1`] = `
{
"charge": 0,
Expand Down
28 changes: 24 additions & 4 deletions src/reaction/__tests__/applyReactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('applyReactions', () => {
const { trees, products, stats } = applyReactions(
[ethanol],
reactionsDatabase,
{},
{ getProductsTrees: true },
);
expect(stats.counter).toBe(32);
removeCoordinates(trees, products);
Expand All @@ -35,7 +35,7 @@ describe('applyReactions', () => {
const { trees, products, stats } = applyReactions(
[ethanol],
reactionsDatabase,
{ limitReactions: 5 },
{ limitReactions: 5, getProductsTrees: true },
);

expect(stats.counter).toBe(5);
Expand All @@ -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);
Expand All @@ -80,7 +97,7 @@ describe('applyReactions', () => {
const { trees, products } = applyReactions(
[propanediol],
reactionsDatabase,
{},
{ getProductsTrees: true },
);
removeCoordinates(trees, products);
expect(products[0]).toMatchSnapshot();
Expand Down Expand Up @@ -116,6 +133,7 @@ describe('applyReactions', () => {
reactionsDatabase,
{
maxDepth: 1,
getProductsTrees: true,
},
);
removeCoordinates(trees, products);
Expand All @@ -135,6 +153,7 @@ describe('applyReactions', () => {
reactionsDatabase,
{
maxDepth: 5,
getProductsTrees: true,
},
);

Expand Down Expand Up @@ -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();
Expand Down
14 changes: 12 additions & 2 deletions src/reaction/applyReactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 };
}

Expand Down

0 comments on commit 2a680bc

Please # to comment.