Skip to content

Commit

Permalink
fix: TopicMolecule path was not correctly dealing with hydrogens
Browse files Browse the repository at this point in the history
We also renamed distance to pathLength
  • Loading branch information
lpatiny committed May 14, 2024
1 parent 1ce912b commit 11f1e61
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 373 deletions.
18 changes: 9 additions & 9 deletions src/path/__tests__/__snapshots__/getAllAtomsPaths.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ exports[`ethanol 1`] = `
[
[
{
"distance": 0,
"path": [
0,
],
"pathLength": 0,
},
],
[
{
"distance": 1,
"path": [
0,
1,
],
"pathLength": 1,
},
],
[
{
"distance": 2,
"path": [
0,
1,
2,
],
"pathLength": 2,
},
],
[],
Expand All @@ -37,26 +37,26 @@ exports[`ethanol 1`] = `
[
[
{
"distance": 0,
"path": [
1,
],
"pathLength": 0,
},
],
[
{
"distance": 1,
"path": [
1,
0,
],
"pathLength": 1,
},
{
"distance": 1,
"path": [
1,
2,
],
"pathLength": 1,
},
],
[],
Expand All @@ -67,29 +67,29 @@ exports[`ethanol 1`] = `
[
[
{
"distance": 0,
"path": [
2,
],
"pathLength": 0,
},
],
[
{
"distance": 1,
"path": [
2,
1,
],
"pathLength": 1,
},
],
[
{
"distance": 2,
"path": [
2,
1,
0,
],
"pathLength": 2,
},
],
[],
Expand Down
6 changes: 3 additions & 3 deletions src/path/__tests__/getAllAtomsPaths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test('isotopropyl alcohol with hydrogens', () => {
const allAtomsPaths = getAllAtomsPaths(molecule);
expect(allAtomsPaths).toHaveLength(12);
const flatten = allAtomsPaths.flat().flat();
expect(flatten).toHaveLength(52);
expect(flatten).toHaveLength(122);
});

test('isotopropyl alcohol with hydrogens, maxPathLength: 2', () => {
Expand All @@ -30,7 +30,7 @@ test('isotopropyl alcohol with hydrogens, maxPathLength: 2', () => {
const allAtomsPaths = getAllAtomsPaths(molecule, { maxPathLength: 2 });
expect(allAtomsPaths).toHaveLength(12);
const flatten = allAtomsPaths.flat().flat();
expect(flatten).toHaveLength(40);
expect(flatten).toHaveLength(68);
});

test('cyclosporin with hydrogens', () => {
Expand All @@ -41,6 +41,6 @@ test('cyclosporin with hydrogens', () => {
const allAtomsPaths = getAllAtomsPaths(molecule);
expect(allAtomsPaths).toHaveLength(196);
const flatten = allAtomsPaths.flat().flat();
expect(flatten).toHaveLength(2907);
expect(flatten).toHaveLength(5098);
expect(Date.now() - start).toBeLessThan(50);
});
8 changes: 4 additions & 4 deletions src/path/getAllAtomsPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface GetAllAtomsPathsOptions {

export interface AtomPath {
path: number[];
distance: number;
pathLength: number;
}

/**
Expand All @@ -32,7 +32,7 @@ export function getAllAtomsPaths(
allAtomsPaths.push(oneAtomPaths);

let atomPaths: AtomPath[] = [];
atomPaths.push({ path: [i], distance: 0 });
atomPaths.push({ path: [i], pathLength: 0 });
oneAtomPaths.push(atomPaths);

let nextIndexes = [0];
Expand All @@ -51,14 +51,14 @@ export function getAllAtomsPaths(
const index = currentIndexes[i];

const previousPath = oneAtomPaths[sphere - 1][index].path;
for (let conn = 0; conn < molecule.getConnAtoms(atom); conn++) {
for (let conn = 0; conn < molecule.getAllConnAtoms(atom); conn++) {
const connectedAtom = molecule.getConnAtom(atom, conn);
if (previousPath.includes(connectedAtom)) continue;
nextIndexes.push(atomPaths.length);
nextAtoms.push(connectedAtom);
atomPaths.push({
path: [...previousPath, connectedAtom],
distance: sphere,
pathLength: sphere,
});
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/topic/TopicMolecule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface GetAtomPathOptions {
/*
* The distance between the two atoms. If not specified, all the distances will be considered
*/
distance?: number;
pathLength?: number;
}

interface GetHoseFragmentOptions {
Expand Down Expand Up @@ -173,15 +173,15 @@ export class TopicMolecule {
}

getAtomPaths(atom1: number, atom2: number, options: GetAtomPathOptions = {}) {
const { distance } = options;
if (distance !== undefined && distance > this.options.maxPathLength) {
const { pathLength } = options;
if (pathLength !== undefined && pathLength > this.options.maxPathLength) {
throw new Error(
'The distance is too long, you should increase the maxPathLength when instanciating the TopicMolecule',
);
}
const atomPaths = this.atomsPaths[atom1];
const minDistance = distance || 0;
const maxDistance = distance || this.options.maxPathLength;
const minDistance = pathLength || 0;
const maxDistance = pathLength || this.options.maxPathLength;
const paths = [];
for (let i = minDistance; i <= maxDistance; i++) {
for (const atomPath of atomPaths[i]) {
Expand Down
11 changes: 9 additions & 2 deletions src/topic/__tests__/TopicMolecule.pathHose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ test('TopicMolecule.path', () => {
const molecule = Molecule.fromSmiles('CCCCCO');
const topicMolecule = new TopicMolecule(molecule);

expect(topicMolecule.getAtomPaths(0, 2, { distance: 2 })).toStrictEqual([
expect(topicMolecule.getAtomPaths(0, 2, { pathLength: 2 })).toStrictEqual([
[0, 1, 2],
]);
expect(topicMolecule.getAtomPaths(0, 2, { distance: 3 })).toStrictEqual([]);
expect(topicMolecule.getAtomPaths(0, 2, { pathLength: 3 })).toStrictEqual([]);
});

test('TopicMolecule.getHoseFragment', async () => {
const molecule = Molecule.fromSmiles('ClC=C')
const topicMolecule = new TopicMolecule(molecule);

expect(topicMolecule.atomsPaths).toMatchSnapshot()
})

test('TopicMolecule.getHoseFragment', async () => {
const molecule = Molecule.fromSmiles('Cl/C=C/CCCCl');
//const molecule = Molecule.fromSmiles('C1=CC=CC=C1');
Expand Down
Loading

0 comments on commit 11f1e61

Please sign in to comment.