The SMILES string to resolve
The resolved identifiers, or undefined if nothing resolved
await resolveSmiles("CCO")
// { name: "ethanol", cas: ["64-17-5"], inchikey: "LFQSCWFLJHTTHZ-UHFFFAOYSA-N", source: "cactus-name" }
await resolveSmiles("not a structure") // undefined
export async function resolveSmiles(smiles: string): Promise<ResolvedStructure | undefined> {
const value = smiles.trim();
if (!isProbablyValidSmiles(value)) return undefined;
try {
const cactus = new Cactus(value);
const [simpleNames, rawCas, rawInchikey] = await Promise.all([
cactus.getSimpleNames(),
cactus.getCAS().catch(() => undefined),
cactus.getStdinchiKey().catch(() => undefined),
]);
const name = simpleNames?.[0];
const cas = (rawCas ?? []).map((entry) => entry.trim()).filter((entry) => isCAS(entry));
const inchikey = normalizeInchikey(rawInchikey);
if (name) {
return { name, cas: cas.length > 0 ? cas : undefined, inchikey, source: 'cactus-name' };
}
if (cas.length > 0) {
return { cas, inchikey, source: 'cactus-cas' };
}
if (inchikey) {
const pubchemName = await nameFromInchikey(inchikey);
if (pubchemName) {
return { name: pubchemName, inchikey, source: 'pubchem-inchikey' };
}
}
return undefined;
} catch (error) {
console.error('Error resolving SMILES:', error);
return undefined;
}
}
Resolves a SMILES string to searchable identifiers (name, CAS, InChIKey) by querying NCI Cactus (which canonicalizes the structure server-side), falling back to a PubChem SDQ InChIKey lookup when Cactus yields no usable name or CAS. Returns undefined when no resolver recognizes the structure or the input is not a plausible SMILES.