rdkit / rdkit-js

A powerful cheminformatics and molecule rendering toolbelt for JavaScript, powered by RDKit .
https://rdkitjs.com
BSD 3-Clause "New" or "Revised" License
149 stars 37 forks source link

Cannot use RDKitModule.get_mol() with InChI #353

Closed ellensmalley closed 1 year ago

ellensmalley commented 1 year ago

Describe the bug Hello! I've been working to integrate rdkit-js in a new React project and found that passing an InChI string to get_mol will not return an RDKit Molecule.

To Reproduce Steps to reproduce the behavior:

  1. I have the RDKitModule loaded in a custom RDKitProvider component, which loads the RDKit module asynchronously and makes it available to its child components through the context. The child components grab the RDKitModule via a custom useRDKit hook.
    Provider and hook code snippets here if helpful:
    
    const RDKit = createContext<Context>({ isLoading: true, rdkit: undefined });

export const RDKitProvider: React.FC<{ children: React.ReactNode }> = ({ children, }) => { const [rdkit, setRDKit] = useState<RDKitModule | undefined>(undefined);

const handleLoad = async () => { const rdkit = await window.initRDKitModule(); setRDKit(rdkit); };

const value: Context = !rdkit ? { isLoading: true, rdkit: undefined } : { isLoading: false, rdkit };

return ( <> <Script src="https://unpkg.com/@rdkit/rdkit/dist/RDKit_minimal.js" onLoad={handleLoad} />

{children} ); }; export const useRDKit = () => { const context = useContext(RDKit); if (!context) throw new Error("useRDKit must be called from within the RDKitProvider"); return context; }; ``` 2. I use the RDKitModule like this: ``` const { rdkit, isLoading } = useRDKit(); const inchi = "InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2-5H,1H3,(H,11,12)"; const smiles = "CC(=O)Oc1ccccc1C(=O)O"; const molecule = rdkit?.get_mol(smiles); // this is the path I was using before, loading from smiles, works! console.log("smile mol is valid?", molecule?.is_valid()); // this evaluates to true const inchiForMolecule = molecule?.get_inchi(); // grab inchi from the created molecule, just to check the var above is valid console.log("is it the same as provided?", inchiForMolecule === inchi); // evaluates to true const moleculeFromInchi = rdkit?.get_mol(inchi); // ok lets try inchi console.log("inchi mol is valid?", moleculeFromInchi?.is_valid()); // evaluates to false const svgFromInchi = moleculeFromInchi?.get_svg_with_highlights( JSON.stringify({ height, width, }) ); // no svg ``` **Expected behavior** I'm expecting to get the same molecule when passing in a smiles string or an InChI string. I'm basing this off the docs: https://docs.rdkitjs.com/interfaces/RDKitModule.html#get_mol I poked around in the source code and found: https://github.com/rdkit/rdkit/blob/88c5d80b3309d2b2ae15d766434650029923fa05/Code/MinimalLib/common.h#L113 (Caveat, I work mainly in Python and JavaScript, could be misreading!) it seems like the input string will never be parsed as an InChI? **Additional Info:** I also tried this in the https://www.rdkitjs.com/ UI (in the first input box): ``` var smiles = "CC(=O)Oc1ccccc1C(=O)O"; var inchi = "InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2-5H,1H3,(H,11,12)"; var mol = RDKitModule.get_mol(inchi); console.log("valid?", mol.is_valid()); // evaluates to TRUE when get_mol(smiles), FALSE when get_mol(inchi) var dest = document.getElementById("example-1-output"); var svg = mol.get_svg(); dest.innerHTML = "
" + svg + "
"; ``` **Desktop (please complete the following information):** - React `^18.2.0` - Next.js `^13.4.4` - Browser `chrome` - Version `^2023.3.1-1.0.1` Thank you!
ptosco commented 1 year ago

The documentation is incorrect. As you noticed, mol_from_input will only accept SMILES, CTAB or rdkitjson as input formats. InChI is a unique chemical substance identifier rather than a molecule input format - one should generate InChIs from molecules, rather than the opposite (though possible).

ellensmalley commented 1 year ago

Thank you! Closing this issue then

ptosco commented 1 year ago

Reopening as there is actually a documentation bug :)