The synonym to query the SDQ agent for.
The compound name from the SDQ agent.
const cmpd = await executeSDQSearch({
where: { cmpdsynonym: "2-Acetoxybenzenecarboxylic acid" },
select: ["cid", "cmpdname"],
limit: 1,
});
console.log(cmpd);
// Outputs: Aspirin
export async function executeSDQSearch({
where,
select = "*",
limit = 10,
}: SDQAgentQuery): Promise<SDQResultItem[] | undefined> {
try {
assertIsSDQWhere(where);
if (select !== "*") {
if (Array.isArray(select)) {
select = select.join(",");
} else {
select = "*";
}
}
const pubchemQuery = {
select,
limit,
collection: "compound",
order: ["cid,asc"],
start: 1,
where: { ands: [where] },
};
console.debug("pubchemQuery", pubchemQuery);
const queryURLString = JSON.stringify(pubchemQuery);
const response = await fetch(
`https://pubchem.ncbi.nlm.nih.gov/sdq/sdqagent.cgi?infmt=json&outfmt=json&query=${queryURLString}`,
);
const data = await response.json();
assertIsSDQResponse(data);
const outputSets = data.SDQOutputSet;
if (!outputSets || outputSets.length === 0) {
return undefined;
}
if (outputSets[0].status.code !== 0) {
console.warn(
`SDQ agent returned a non-zero status code: ${outputSets[0].status.code}`,
{ where, select, limit },
{ response: data },
);
return undefined;
}
if (outputSets[0].totalCount === 0 || outputSets[0]?.rows?.length === 0) {
console.debug(`SDQ agent returned no results`, { where, select, limit }, { response: data });
return undefined;
}
return outputSets[0].rows;
} catch (error) {
console.error("Error querying SDQ agent:", error);
}
}
Query the SDQ agent for a compound name from a synonym.