The raw search query
The resolved structure, or undefined if the query names nothing renderable
await resolveMolecule('aspirin');
// { cid: 2244, molecule: { atoms: […], bonds: […], isPlanar: false }, recordType: '3d' }
await resolveMolecule('sodium chloride');
// { cid: 5234, molecule: {…, isPlanar: true }, recordType: '2d' }
await resolveMolecule('acetone AND NOT sigma'); // resolves the acetone term
await resolveMolecule('qqqq'); // undefined
export async function resolveMolecule(query: string): Promise<ResolvedMolecule | undefined> {
for (const term of candidateTerms(query)) {
const cid = await resolveCid(term);
if (cid === undefined) continue;
const record = await getStructureSdf(cid);
if (record === undefined) continue;
const molecule = parseSdf(record.sdf);
if (molecule === undefined) continue;
return { cid, molecule, recordType: record.recordType };
}
return undefined;
}
Resolves a search query — a CAS number, chemical name, SMILES string or molecular formula — to a parsed molecular structure fetched from PubChem.
Every failure mode (no CID, no structure record, unparseable SDF) resolves to undefined rather than throwing, so a caller can fall back to a generic loading animation.