Search in sources :

Example 1 with ApiException

use of org.rsc.chemspider.ApiException in project mzmine2 by mzmine.

the class ChemSpiderGateway method findCompounds.

@Override
public String[] findCompounds(final double mass, final MZTolerance mzTolerance, final int numOfResults, ParameterSet parameters) throws IOException {
    logger.finest("Searching by mass...");
    // Get search range
    final Range<Double> mzRange = mzTolerance.getToleranceRange(mass);
    final double queryMz = RangeUtils.rangeCenter(mzRange);
    final double queryRange = RangeUtils.rangeLength(mzRange) / 2.0;
    // Get security token.
    final String apiKey = parameters.getParameter(ChemSpiderParameters.SECURITY_TOKEN).getValue();
    try {
        FilterByMassRequest filterRequest = new FilterByMassRequest();
        filterRequest.setMass(queryMz);
        filterRequest.setRange(queryRange);
        filterRequest.setOrderBy(OrderByEnum.RECORDID);
        FilteringApi apiInstance = new FilteringApi();
        apiInstance.getApiClient().setUserAgent("MZmine " + MZmineCore.getMZmineVersion());
        FilterQueryResponse queryId = apiInstance.filterMassPost(filterRequest, apiKey);
        QueryResultResponse result = apiInstance.filterQueryIdResultsGet(queryId.getQueryId(), apiKey, 0, numOfResults);
        List<Integer> integerIDs = result.getResults();
        List<String> stringIDs = Lists.transform(integerIDs, Functions.toStringFunction());
        return stringIDs.toArray(new String[0]);
    } catch (ApiException e) {
        throw new IOException(e);
    }
}
Also used : FilterByMassRequest(org.rsc.chemspider.api.FilterByMassRequest) QueryResultResponse(org.rsc.chemspider.api.QueryResultResponse) IOException(java.io.IOException) FilterQueryResponse(org.rsc.chemspider.api.FilterQueryResponse) FilteringApi(org.rsc.chemspider.api.FilteringApi) ApiException(org.rsc.chemspider.ApiException)

Example 2 with ApiException

use of org.rsc.chemspider.ApiException 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);
    }
}
Also used : RecordsApi(org.rsc.chemspider.api.RecordsApi) RecordResponse(org.rsc.chemspider.api.RecordResponse) IOException(java.io.IOException) DBCompound(net.sf.mzmine.modules.peaklistmethods.identification.onlinedbsearch.DBCompound) URL(java.net.URL) ApiException(org.rsc.chemspider.ApiException)

Aggregations

IOException (java.io.IOException)2 ApiException (org.rsc.chemspider.ApiException)2 URL (java.net.URL)1 DBCompound (net.sf.mzmine.modules.peaklistmethods.identification.onlinedbsearch.DBCompound)1 FilterByMassRequest (org.rsc.chemspider.api.FilterByMassRequest)1 FilterQueryResponse (org.rsc.chemspider.api.FilterQueryResponse)1 FilteringApi (org.rsc.chemspider.api.FilteringApi)1 QueryResultResponse (org.rsc.chemspider.api.QueryResultResponse)1 RecordResponse (org.rsc.chemspider.api.RecordResponse)1 RecordsApi (org.rsc.chemspider.api.RecordsApi)1