Skip to content

Commit 596b7d8

Browse files
committed
chore: migrate getHoseCodes to TS
1 parent c2806e6 commit 596b7d8

5 files changed

Lines changed: 108 additions & 54 deletions
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { Molecule } from 'openchemlib';
2+
3+
/**
4+
* Options for generating HOSE codes.
5+
*/
6+
export interface HoseCodesForAtomsOptions {
7+
/**
8+
* Array of the custom labels that are considered as root atoms.
9+
* By default all atoms having a customLabel
10+
*/
11+
allowedCustomLabels?: string[];
12+
13+
/**
14+
* Smallest hose code sphere
15+
* @default 0
16+
*/
17+
minSphereSize?: number;
18+
19+
/**
20+
* Largest hose code sphere
21+
* @default 4
22+
*/
23+
maxSphereSize?: number;
24+
25+
/**
26+
* Kind of hose code, default usual sphere
27+
* @default FULL_HOSE_CODE
28+
*/
29+
kind?: number;
30+
31+
/**
32+
* Array of atom from which we should start to create the HOSE.
33+
* By default we will used the taggedAtoms
34+
* @default []
35+
*/
36+
rootAtoms?: number[];
37+
38+
/**
39+
* Array of atom indices to tag as root atoms
40+
* @default []
41+
*/
42+
tagAtoms?: number[];
43+
44+
/**
45+
* Function to tag an atom as root atom. By default it is defined internal
46+
*/
47+
tagAtomFct?: (molecule: Molecule, atomIndex: number) => void;
48+
}

src/hose/getHoseCodesForAtomsAsFragments.js renamed to src/hose/getHoseCodesForAtomsAsFragments.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1+
import type { Molecule } from 'openchemlib';
2+
13
import { getXAtomicNumber } from '../util/getXAtomicNumber.js';
24
import { isCsp3 } from '../util/isCsp3.js';
35
import { makeRacemic } from '../util/makeRacemic.js';
4-
import { tagAtom } from '../util/tagAtom.ts';
6+
import { tagAtom } from '../util/tagAtom.js';
7+
8+
import type { HoseCodesForAtomsOptions } from './HoseCodesForAtomsOptions.js';
59

610
export const FULL_HOSE_CODE = 1;
711
export const HOSE_CODE_CUT_C_SP3_SP3 = 2;
812

913
/**
1014
* Returns an array of hose code fragments for the specified molecule.
11-
* @param {import('openchemlib').Molecule} molecule - The OCL molecule to process.
12-
* @param {object} options - Options for generating hose codes.
13-
* @param {string[]} [options.allowedCustomLabels] - Array of the custom labels that are considered as root atoms. By default all atoms having a customLabel
14-
* @param {number} [options.minSphereSize=0] - Smallest hose code sphere
15-
* @param {number} [options.maxSphereSize=4] - Largest hose code sphere
16-
* @param {number} [options.kind=FULL_HOSE_CODE] - Kind of hose code, default usual sphere
17-
* @param {number[]} [options.rootAtoms=[]] - Array of atom from which we should start to create the HOSE. By default we will used the taggedAtoms
18-
* @param {number[]} [options.tagAtoms=[]] - Array of atom indices to tag as root atoms
19-
* @param {Function} [options.tagAtomFct=tagAtom] - Function to tag an atom as root atom. By default it is defined internal
20-
* @returns {Array} - An array of hose code fragments.
15+
* @param molecule - The OCL molecule to process.
16+
* @param options - Options for generating hose codes.
17+
* @returns An array of hose code fragments.
2118
*/
22-
export function getHoseCodesForAtomsAsFragments(molecule, options = {}) {
19+
export function getHoseCodesForAtomsAsFragments(
20+
molecule: Molecule,
21+
options: HoseCodesForAtomsOptions = {},
22+
): Molecule[] {
2323
const OCL = molecule.getOCL();
2424
const {
2525
allowedCustomLabels,
@@ -51,14 +51,14 @@ export function getHoseCodesForAtomsAsFragments(molecule, options = {}) {
5151
}
5252
}
5353

54-
const fragments = [];
54+
const fragments: Molecule[] = [];
5555

5656
// keep track of the atoms when creating the fragment
57-
const mappings = [];
57+
const mappings: number[] = [];
5858
let min = 0;
5959
let max = 0;
60-
const atomMask = new Uint8Array(molecule.getAllAtoms());
61-
const atomList = new Uint8Array(molecule.getAllAtoms());
60+
const atomMask = new Array<boolean>(molecule.getAllAtoms()).fill(false);
61+
const atomList = new Array<number>(molecule.getAllAtoms());
6262

6363
for (let sphere = 0; sphere <= maxSphereSize; sphere++) {
6464
if (max === 0) {
@@ -70,7 +70,7 @@ export function getHoseCodesForAtomsAsFragments(molecule, options = {}) {
7070
} else {
7171
let newMax = max;
7272
for (let i = min; i < max; i++) {
73-
const atom = atomList[i];
73+
const atom = atomList[i] as number;
7474
for (let j = 0; j < molecule.getAllConnAtoms(atom); j++) {
7575
const connAtom = molecule.getConnAtom(atom, j);
7676
if (!atomMask[connAtom]) {
@@ -117,10 +117,13 @@ export function getHoseCodesForAtomsAsFragments(molecule, options = {}) {
117117
* If the atom is not an halogen, X or an hydrogen
118118
* we add query features to the atom
119119
* This includes aromaticity, ring size, number of hydrogens
120-
* @param {import('openchemlib').Molecule} fragment
121-
* @param {import('openchemlib').Molecule} molecule
120+
* @param fragment
121+
* @param molecule
122122
*/
123-
function addQueryFeaturesAndRemoveMapNo(fragment, molecule) {
123+
function addQueryFeaturesAndRemoveMapNo(
124+
fragment: Molecule,
125+
molecule: Molecule,
126+
) {
124127
const Molecule = molecule.getOCL().Molecule;
125128
for (let i = 0; i < fragment.getAllAtoms(); i++) {
126129
const mapping = fragment.getAtomMapNo(i) - 1;
@@ -193,7 +196,12 @@ function addQueryFeaturesAndRemoveMapNo(fragment, molecule) {
193196

194197
// tagging atoms may change the order of the atoms because hydrogens must be at the end of the file
195198
// in order to remember the rootAtoms we will tag before
196-
function internalTagAtoms(molecule, tagAtoms, rootAtoms, tagAtomFct) {
199+
function internalTagAtoms(
200+
molecule: Molecule,
201+
tagAtoms: number[],
202+
rootAtoms: number[],
203+
tagAtomFct: (molecule: Molecule, atomIndex: number) => void,
204+
) {
197205
const OCL = molecule.getOCL();
198206

199207
if (tagAtoms) {
@@ -216,7 +224,7 @@ function internalTagAtoms(molecule, tagAtoms, rootAtoms, tagAtomFct) {
216224
mapping[molecule.getAtomMapNo(i) - 1] = i;
217225
}
218226
for (let i = 0; i < rootAtoms.length; i++) {
219-
rootAtoms[i] = mapping[rootAtoms[i]];
227+
rootAtoms[i] = mapping[rootAtoms[i] as number] as number;
220228
}
221229
}
222230
}

src/hose/getHoseCodesForAtomsAsStrings.js

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Molecule } from 'openchemlib';
2+
3+
import type { HoseCodesForAtomsOptions } from './HoseCodesForAtomsOptions.js';
4+
import { getHoseCodesForAtomsAsFragments } from './getHoseCodesForAtomsAsFragments.js';
5+
6+
/**
7+
* Returns an array of strings (idCodes) specified molecule. Each string corresponds to a
8+
* hose code. By default it will calculate the hose codes for sphere 0 to 4 and will reuse
9+
* the existing tagged atoms.
10+
* @param molecule - The OCL molecule to process.
11+
* @param options - Options for generating hose codes.
12+
* @returns An array of hose code strings.
13+
*/
14+
export function getHoseCodesForAtomsAsStrings(
15+
molecule: Molecule,
16+
options: HoseCodesForAtomsOptions = {},
17+
): string[] {
18+
const fragments = getHoseCodesForAtomsAsFragments(molecule, options);
19+
const OCL = molecule.getOCL();
20+
const hoses = [];
21+
for (const fragment of fragments) {
22+
hoses.push(
23+
fragment.getCanonizedIDCode(
24+
OCL.Molecule.CANONIZER_ENCODE_ATOM_CUSTOM_LABELS,
25+
),
26+
);
27+
}
28+
return hoses;
29+
}

src/topic/TopicMolecule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class TopicMolecule {
179179
tagAtomFct,
180180
});
181181

182-
return fragments[0];
182+
return fragments[0] as Molecule;
183183
}
184184

185185
getAtomPathsFrom(atom: number, options: GetAtomPathFromOptions = {}) {

0 commit comments

Comments
 (0)