package com.chemspider.www.examples; import java.util.HashMap; import java.util.Map; import javax.swing.JOptionPane; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Level; import org.apache.log4j.Logger; import com.chemspider.www.*; import com.chemspider.www.InChIStub.InChIToCSIDResponse; import com.chemspider.www.SearchStub.GetAsyncSearchResultResponse; import com.chemspider.www.SearchStub.GetAsyncSearchStatusResponse; import com.chemspider.www.SearchStub.SimpleSearchResponse; import com.chemspider.www.MassSpecAPIStub.ArrayOfInt; import com.chemspider.www.MassSpecAPIStub.ArrayOfString; import com.chemspider.www.MassSpecAPIStub.ExtendedCompoundInfo; import com.chemspider.www.MassSpecAPIStub.GetDatabasesResponse; import com.chemspider.www.MassSpecAPIStub.GetExtendedCompoundInfoArrayResponse; import com.chemspider.www.MassSpecAPIStub.SearchByMassAsyncResponse; public class WebServiceExamples { /** * @param args */ private static final Logger LOG = Logger.getLogger(WebServiceExamples.class.getName()); private static String ChemSpiderToken = "YOU NEED TO INSERT YOUR OWN TOKEN IN HERE"; public static void main(String[] args) { BasicConfigurator.configure(); JOptionPane.showMessageDialog(null, "The compound with InChI InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H has CSID:"+get_InChI_InChIToCSID_Results("InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H")); int[] SimpleSearchResults = get_Search_SimpleSearch_Results("taxol", ChemSpiderToken); JOptionPane.showMessageDialog(null, "The first of "+SimpleSearchResults.length+" ChemSpider compound(s) returned by a search for Taxol has CSID:"+SimpleSearchResults[0]); int[] inputCSIDs = new int[2]; inputCSIDs[0] = 236; inputCSIDs[1] = 238; Map> GetExtendedCompoundInfoArrayResults = get_MassSpecAPI_GetExtendedCompoundInfoArray_Results(inputCSIDs, ChemSpiderToken); Map thisCompoundInfo = GetExtendedCompoundInfoArrayResults.get(238); JOptionPane.showMessageDialog(null, "The Average Mass of the compound with CSID 238 is: "+thisCompoundInfo.get("AverageMass")); String[] GetDatabaseResults = get_MassSpecAPI_GetDatabases_Results(); JOptionPane.showMessageDialog(null, "The first of "+GetDatabaseResults.length+" datasources in ChemSpider is:"+GetDatabaseResults[0]); String SearchByMassAsyncResults = get_MassSpecAPI_SearchByMassAsync_Results(1100.0, 0.1,GetDatabaseResults, ChemSpiderToken); JOptionPane.showMessageDialog(null, "Transaction ID for search on compounds with mass = 1100+/- 0.1 from any data source is" + SearchByMassAsyncResults); JOptionPane.showMessageDialog(null, "The operation status of the search with this transaction ID is" + get_Search_GetAsyncSearchStatus_Results(SearchByMassAsyncResults, ChemSpiderToken)); int[] GetAsyncSearchResultResults = get_Search_GetAsyncSearchResult_Results(SearchByMassAsyncResults, ChemSpiderToken); JOptionPane.showMessageDialog(null, "And the first of "+GetAsyncSearchResultResults.length+" ChemSpider compound(s) returned by the search has CSID:"+GetAsyncSearchResultResults[0]); } /** * Function to call the InChIToCSID operation of ChemSpider's InChI SOAP 1.2 webservice (http://www.chemspider.com/InChI.asmx?op=InChIToCSID) * Convert InChI to ChemSpider ID. * * @param inchi: string representing inchi to search ChemSpider for * @return: string representing CSID returned */ public static String get_InChI_InChIToCSID_Results(String inchi) { String Output = null; try { final InChIStub thisInChIstub = new InChIStub(); com.chemspider.www.InChIStub.InChIToCSID InChIToCSIDInput = new com.chemspider.www.InChIStub.InChIToCSID(); InChIToCSIDInput.setInchi(inchi); final InChIToCSIDResponse thisInChIToCSIDResponse = thisInChIstub.inChIToCSID(InChIToCSIDInput); Output = thisInChIToCSIDResponse.getInChIToCSIDResult(); } catch (Exception e) { LOG.log(Level.ERROR, "Problem retrieving ChemSpider webservices", e); } return Output; } /** * Function to call the SimpleSearch operation of ChemSpider's Search SOAP 1.2 webservice (http://www.chemspider.com/search.asmx?op=SimpleSearch) * Search by Name, SMILES, InChI, InChIKey, etc. Returns a list of found CSIDs (first 100 - please use AsyncSimpleSearch instead if you like to get the full list). Security token is required. * * @param query: String representing search term (can be Name, SMILES, InChI, InChIKey) * @param token: string containing your user token (listed at your http://www.chemspider.com/UserProfile.aspx page) * @return: int[] array containing the ChemSpider IDs. If more than 100 are found then only the first 100 are returned. */ public static int[] get_Search_SimpleSearch_Results(String query, String token) { int[] Output = null; try { final SearchStub thisSearchStub = new SearchStub(); com.chemspider.www.SearchStub.SimpleSearch SimpleSearchInput = new com.chemspider.www.SearchStub.SimpleSearch(); SimpleSearchInput.setQuery(query); SimpleSearchInput.setToken(token); final SimpleSearchResponse thisSimpleSearchResponse = thisSearchStub.simpleSearch(SimpleSearchInput); Output = thisSimpleSearchResponse.getSimpleSearchResult().get_int(); } catch (Exception e) { LOG.log(Level.ERROR, "Problem retrieving ChemSpider webservices", e); } return Output; } /** * Function to call the GetDatabases operation of ChemSpider's MassSpecAPI SOAP 1.2 webservice (http://www.chemspider.com/massspecapi.asmx?op=GetDatabases) * Get the list of datasources in ChemSpider. * * @return: the list of datasources in ChemSpider as a String Array */ public static String[] get_MassSpecAPI_GetDatabases_Results() { String[] Output = null; try { final MassSpecAPIStub thisMassSpecAPIStub = new MassSpecAPIStub(); com.chemspider.www.MassSpecAPIStub.GetDatabases getDatabaseInput = new com.chemspider.www.MassSpecAPIStub.GetDatabases(); final GetDatabasesResponse thisGetDatabasesResponse = thisMassSpecAPIStub.getDatabases(getDatabaseInput); Output = thisGetDatabasesResponse.getGetDatabasesResult().getString(); } catch (Exception e) { LOG.log(Level.ERROR, "Problem retrieving ChemSpider webservices", e); } return Output; } /** * Function to call the GetExtendedCompoundInfoArray operation of ChemSpider's MassSpecAPI SOAP 1.2 webservice (http://www.chemspider.com/massspecapi.asmx?op=GetExtendedCompoundInfoArray) * Get array of extended record details by an array of CSIDs. Security token is required. * * @param CSIDs: integer array containing the CSIDs of compounds for which information will be returned * @param token: string containing your user token (listed at your http://www.chemspider.com/UserProfile.aspx page) * @return: a Map> containing the results array for each CSID (with Properties CSID, MF, SMILES, InChIKey, AverageMass, MolecularWeight, MonoisotopicMass, NominalMass, ALogP, XLogP, CommonName) */ public static Map> get_MassSpecAPI_GetExtendedCompoundInfoArray_Results(int[] CSIDs, String token) { Map> Output = new HashMap>(); try { final MassSpecAPIStub thisMassSpecAPIStub = new MassSpecAPIStub(); ArrayOfInt inputCSIDsArrayofInt = new ArrayOfInt(); inputCSIDsArrayofInt.set_int(CSIDs); com.chemspider.www.MassSpecAPIStub.GetExtendedCompoundInfoArray getGetExtendedCompoundInfoArrayInput = new com.chemspider.www.MassSpecAPIStub.GetExtendedCompoundInfoArray(); getGetExtendedCompoundInfoArrayInput.setCSIDs(inputCSIDsArrayofInt); getGetExtendedCompoundInfoArrayInput.setToken(token); final GetExtendedCompoundInfoArrayResponse thisGetExtendedCompoundInfoArrayResponse = thisMassSpecAPIStub.getExtendedCompoundInfoArray(getGetExtendedCompoundInfoArrayInput); ExtendedCompoundInfo[] thisExtendedCompoundInfo = thisGetExtendedCompoundInfoArrayResponse.getGetExtendedCompoundInfoArrayResult().getExtendedCompoundInfo(); for (int i=0; i thisCompoundExtendedCompoundInfoArrayOutput = new HashMap(); thisCompoundExtendedCompoundInfoArrayOutput.put("CSID", Integer.toString(thisExtendedCompoundInfo[i].getCSID())); thisCompoundExtendedCompoundInfoArrayOutput.put("MF", thisExtendedCompoundInfo[i].getMF()); thisCompoundExtendedCompoundInfoArrayOutput.put("SMILES", thisExtendedCompoundInfo[i].getSMILES()); thisCompoundExtendedCompoundInfoArrayOutput.put("InChI", thisExtendedCompoundInfo[i].getInChI()); thisCompoundExtendedCompoundInfoArrayOutput.put("InChIKey", thisExtendedCompoundInfo[i].getInChIKey()); thisCompoundExtendedCompoundInfoArrayOutput.put("AverageMass", Double.toString(thisExtendedCompoundInfo[i].getAverageMass())); thisCompoundExtendedCompoundInfoArrayOutput.put("MolecularWeight", Double.toString(thisExtendedCompoundInfo[i].getMolecularWeight())); thisCompoundExtendedCompoundInfoArrayOutput.put("MonoisotopicMass", Double.toString(thisExtendedCompoundInfo[i].getMonoisotopicMass())); thisCompoundExtendedCompoundInfoArrayOutput.put("NominalMass", Double.toString(thisExtendedCompoundInfo[i].getNominalMass())); thisCompoundExtendedCompoundInfoArrayOutput.put("ALogP", Double.toString(thisExtendedCompoundInfo[i].getALogP())); thisCompoundExtendedCompoundInfoArrayOutput.put("XLogP", Double.toString(thisExtendedCompoundInfo[i].getXLogP())); thisCompoundExtendedCompoundInfoArrayOutput.put("CommonName", thisExtendedCompoundInfo[i].getCommonName()); Output.put(thisExtendedCompoundInfo[i].getCSID(), thisCompoundExtendedCompoundInfoArrayOutput); } } catch (Exception e) { LOG.log(Level.ERROR, "Problem retrieving ChemSpider webservices", e); } return Output; } /** * Function to call the SearchByMass2 operation of ChemSpider's MassSpecAPI SOAP 1.2 webservice (http://www.chemspider.com/massspecapi.asmx?op=SearchByMass2) * Search ChemSpider by mass +/- range. * * @param Mass: The compounds returned have a mass (Double) within the range Mass +/- Range * @param Range: The compounds returned have a mass (Double) within the range Mass +/- Range * @return: the ChemSpider IDs of compounds returned (as a String Array) */ public static String get_MassSpecAPI_SearchByMassAsync_Results(Double mass, Double range, String[] dbs, String token) { String Output = null; try { final MassSpecAPIStub thisMassSpecAPIStub = new MassSpecAPIStub(); com.chemspider.www.MassSpecAPIStub.SearchByMassAsync getSearchByMassAsyncInput = new com.chemspider.www.MassSpecAPIStub.SearchByMassAsync(); getSearchByMassAsyncInput.setMass(mass); getSearchByMassAsyncInput.setRange(range); ArrayOfString inputDBsArrayofString = new ArrayOfString(); inputDBsArrayofString.setString(dbs); getSearchByMassAsyncInput.setDbs(inputDBsArrayofString); getSearchByMassAsyncInput.setToken(token); final SearchByMassAsyncResponse thisSearchByMassAsyncResponse = thisMassSpecAPIStub.searchByMassAsync(getSearchByMassAsyncInput); Output = thisSearchByMassAsyncResponse.getSearchByMassAsyncResult(); } catch (Exception e) { LOG.log(Level.ERROR, "Problem retrieving ChemSpider webservices", e); } return Output; } /** * Function to call the GetAsyncSearchStatus operation of ChemSpider's Search SOAP 1.2 webservice (http://www.chemspider.com/search.asmx?op=GetAsyncSearchStatus) * Query asynchronous operation status. Requires transaction ID returned by AsynchSearch operation. Security token is required. * * @param rid: String representing transaction ID returned from a previous search * @param token: string containing your user token (listed at your http://www.chemspider.com/UserProfile.aspx page) * @return: String describing status of this search - can have values Unknown or Created or Scheduled or Processing or Suspended or PartialResultReady or ResultReady or Failed or TooManyRecords */ public static String get_Search_GetAsyncSearchStatus_Results(String rid, String token) { String Output = null; try { final SearchStub thisSearchStub = new SearchStub(); com.chemspider.www.SearchStub.GetAsyncSearchStatus GetAsyncSearchStatusInput = new com.chemspider.www.SearchStub.GetAsyncSearchStatus(); GetAsyncSearchStatusInput.setRid(rid); GetAsyncSearchStatusInput.setToken(token); final GetAsyncSearchStatusResponse thisGetAsyncSearchStatusResponse = thisSearchStub.getAsyncSearchStatus(GetAsyncSearchStatusInput); Output = thisGetAsyncSearchStatusResponse.getGetAsyncSearchStatusResult().toString(); } catch (Exception e) { LOG.log(Level.ERROR, "Problem retrieving ChemSpider webservices", e); } return Output; } /** * Function to call the GetAsyncSearchResult operation of ChemSpider's Search SOAP 1.2 webservice (http://www.chemspider.com/search.asmx?op=GetAsyncSearchResult) * Returns the list of CSIDs found by AsynchSearch operation. Security token is required. * * @param rid: String representing transaction ID returned from a previous search * @param token: string containing your user token (listed at your http://www.chemspider.com/UserProfile.aspx page) * @return: int[] array containing the ChemSpider IDs. */ public static int[] get_Search_GetAsyncSearchResult_Results(String rid, String token) { int[] Output = null; try { final SearchStub thisSearchStub = new SearchStub(); com.chemspider.www.SearchStub.GetAsyncSearchResult GetAsyncSearchResultInput = new com.chemspider.www.SearchStub.GetAsyncSearchResult(); GetAsyncSearchResultInput.setRid(rid); GetAsyncSearchResultInput.setToken(token); final GetAsyncSearchResultResponse thisGetAsyncSearchResultResponse = thisSearchStub.getAsyncSearchResult(GetAsyncSearchResultInput); Output = thisGetAsyncSearchResultResponse.getGetAsyncSearchResultResult().get_int(); } catch (Exception e) { LOG.log(Level.ERROR, "Problem retrieving ChemSpider webservices", e); } return Output; } }