use of org.rsc.chemspider.api.RecordResponse in project mzmine2 by mzmine.
the class ChemSpiderGateway method getCompound.
@Override
public DBCompound getCompound(final String ID, ParameterSet parameters) throws IOException {
logger.finest("Fetching compound info for CSID #" + ID);
// Get security token.
final String apiKey = parameters.getParameter(ChemSpiderParameters.SECURITY_TOKEN).getValue();
final List<String> fields = Arrays.asList("Formula", "CommonName", "MonoisotopicMass");
try {
RecordsApi apiInstance = new RecordsApi();
apiInstance.getApiClient().setUserAgent("MZmine " + MZmineCore.getMZmineVersion());
Integer recordId = Integer.valueOf(ID);
RecordResponse response = apiInstance.recordsRecordIdDetailsGet(recordId, fields, apiKey);
String name = response.getCommonName();
if (Strings.isNullOrEmpty(name))
name = UNKNOWN_NAME;
String formula = response.getFormula();
// Fix formula formatting
if (!Strings.isNullOrEmpty(formula))
formula = FORMULA_PATTERN.matcher(formula).replaceAll("");
// Create and return the compound record.
return new DBCompound(OnlineDatabases.CHEMSPIDER, ID, name, formula, new URL(STRUCTURE_URL_PATTERN.replaceFirst("CSID", ID)), new URL(STRUCTURE2D_URL_PATTERN.replaceFirst("CSID", ID)), new URL(STRUCTURE3D_URL_PATTERN.replaceFirst("CSID", ID)));
} catch (ApiException e) {
logger.log(Level.WARNING, "Failed to fetch compound info for CSID #" + ID, e);
throw new IOException(e);
}
}
Aggregations