Creates a new Pubchem instance for querying compound information
The chemical compound name or identifier to search for
Retrieves the first matching compound name from PubChem's autocomplete API
Promise resolving to the first matching compound name
const compound = await pubchem.getCompound();
console.log(compound); // "aspirin"
async getCompound(): Promise<string | undefined> {
try {
const response = await fetch(
`${this.baseURL}/rest/autocomplete/compound/${encodeURIComponent(this.query)}`,
);
const data = await response.json();
assertIsCompoundResponseResponse(data);
return data.dictionary_terms.compound[0];
} catch (error) {
console.error("Error fetching compound:", error);
}
}
Retrieves the CID (Compound ID) for the compound by first getting the compound name and then looking up its CID
Promise resolving to the Compound ID (CID)
const cid = await pubchem.getCID();
console.log(cid); // 2244
async getCID(): Promise<number | undefined> {
try {
const compound = await this.getCompound();
if (!compound) return undefined;
const response = await fetch(
`${this.baseURL}/rest/pug/concepts/name/JSON?name=${encodeURIComponent(compound)}`,
);
const data = await response.json();
assertIsCIDResponse(data);
return data.ConceptsAndCIDs.CID[0];
} catch (error) {
console.error("Error fetching CID:", error);
}
}
Queries PubChem's SDQ (Structure Data Query) agent to retrieve detailed molecular properties
Promise resolving to the SDQ response containing detailed compound data
const sdqData = await pubchem.querySdqAgent();
console.log(sdqData.SDQOutputSet[0].rows[0].mw); // Molecular weight
async querySdqAgent(sdqQuery: SDQQuery): Promise<SDQResponse | undefined> {
try {
const sdqAgentQuery = {
select: "*",
collection: "compound",
order: ["cid,asc"],
start: 1,
limit: 10,
where: { ands: [sdqQuery] },
width: 1000000,
listids: 0,
};
console.debug({ sdqAgentQuery });
const queryURL = JSON.stringify(sdqAgentQuery).replace(/"/g, "%22").replace(/ /g, "%20");
console.debug(
`Querying URL: ${this.baseURL}/sdq/sdqagent.cgi?infmt=json&outfmt=json&query=${queryURL}`,
);
const response = await fetch(
`${this.baseURL}/sdq/sdqagent.cgi?infmt=json&outfmt=json&query=${queryURL}`,
);
const data = await response.json();
assertIsSdqAgentResponse(data);
return data;
} catch (error) {
console.error("Error querying SDQ agent:", error);
}
}
Retrieves the simple compound name from the SDQ agent query results
Promise resolving to the compound name
const name = await pubchem.getSimpleName();
console.log(name); // "2-acetyloxybenzoic acid"
async getSimpleName(): Promise<string | undefined> {
try {
const cid = await this.getCID();
if (!cid) return undefined;
const data = await this.querySdqAgent({ cid });
if (!data) return undefined;
return data.SDQOutputSet[0].rows[0].cmpdname;
} catch (error) {
console.error("Error fetching simple name:", error);
}
}
Retrieves the compound name from the SDQ agent query results
Promise resolving to the compound name
const name = await pubchem.getCompoundNameFromAlias("aspirin");
async getCompoundNameFromAlias(cmpdsynonym: string): Promise<string | undefined> {
try {
const data = await this.querySdqAgent({ cmpdsynonym });
if (!data) return undefined;
return data.SDQOutputSet[0].rows[0].cmpdname;
} catch (error) {
console.error("Error fetching compound name from alias:", error);
}
}
A utility class for interacting with the PubChem API to retrieve chemical compound information.
This class provides methods to search for compounds, retrieve their CID (Compound ID), and query detailed molecular properties using PubChem's SDQ (Structure Data Query) agent.
Example
Source