using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; using PubChem.PUG_SOAP; namespace PubChem { class PUG_SOAP_Program { private static PUG pug_ws = new PUG(); private static string[] FILES = { "cubane.sdf", "CID_123.asnt", "CID_123.sdf", "CID_123.xml" }; private static string[] STRUCTURES = { "COBRA", "c1cccc2", "C", "CCCCCC", "c1ccc1", "c1cccccccccc1", "InChI=1/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2-5H,1H3,(H,11,12)", "InChI=1/2C9H8O4.Ca/c2*1-6(10)13-8-5-3-2-4-7(8)9(11)12;/h2*2-5H,1H3,(H,11,12);/q;;+2/p-2/f2C9H7O4.Ca/q2*-1;m" }; private static string[] MFS = { "C2H6", "CCCCCC", "c1ccc1", "c1c1", "COBRA" }; static void Main(string[] args) { Console.Out.WriteLine("Testing ScoreMatrix..."); ScoreMatrix(); Console.Out.WriteLine("Done."); /////////////////////////////////////////////////////////////////////////////////////////// Console.Out.WriteLine("Testing IdentitySearch..."); foreach ( string str in STRUCTURES ) IdentitySearch(str); Console.Out.WriteLine("Done."); /////////////////////////////////////////////////////////////////////////////////////////// Console.Out.WriteLine("Testing SubstructureSearch..."); foreach ( string file in FILES ) SubstructureSearch(file); Console.Out.WriteLine("Done."); /////////////////////////////////////////////////////////////////////////////////////////// Console.Out.WriteLine("Testing SuperstructureSearch..."); foreach ( string file in FILES ) SuperstructureSearch(file); Console.Out.WriteLine("Done."); /////////////////////////////////////////////////////////////////////////////////////////// Console.Out.WriteLine("Testing SimilaritySearch..."); foreach ( string file in FILES ) SimilaritySearch(file); Console.Out.WriteLine("Done."); /////////////////////////////////////////////////////////////////////////////////////////// Console.Out.WriteLine("Testing MFSearch..."); foreach ( string mf in MFS ) MSSearch(mf); Console.Out.WriteLine("Done."); /////////////////////////////////////////////////////////////////////////////////////////// Console.Out.WriteLine("Testing Standardize..."); foreach ( string str in STRUCTURES ) Standardize(str); Console.Out.WriteLine("Done."); /////////////////////////////////////////////////////////////////////////////////////////// Console.Out.WriteLine("Testing Download..."); int[] cids = { 1, 2, 3, 4, 5 }; Download(cids); Console.Out.WriteLine("Done."); } private static FormatType decodeFormatByFileExt(string file) { if ( file.EndsWith(".asnt", StringComparison.InvariantCultureIgnoreCase) ) return FormatType.eFormat_ASNT; else if ( file.EndsWith(".asn", StringComparison.InvariantCultureIgnoreCase) || file.EndsWith(".asnb", StringComparison.InvariantCultureIgnoreCase) ) return FormatType.eFormat_ASNB; else if ( file.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase) ) return FormatType.eFormat_XML; else if ( file.EndsWith(".sdf", StringComparison.InvariantCultureIgnoreCase) ) return FormatType.eFormat_SDF; else throw new FormatException("Cannot detect file format: " + file); } private static string ImportStructureFromFile(string file) { // Input structure string strkey = pug_ws.InputStructureBase64(File.ReadAllBytes(file), decodeFormatByFileExt(file)); if ( pug_ws.GetOperationStatus(strkey) == StatusType.eStatus_Success ) { Console.Out.WriteLine("InputStructureBase64(\"{0}\"): {1}", file, strkey); return strkey; } else { Console.Error.WriteLine("InputStructureBase64(\"{0}\") failed: {1}", file, pug_ws.GetStatusMessage(strkey)); return String.Empty; } } private static FormatType decodeFormatByValue(string value) { if ( value.StartsWith("InChI=", StringComparison.InvariantCultureIgnoreCase) ) return FormatType.eFormat_InChI; else return FormatType.eFormat_SMILES; } private static void IdentitySearch(string str) { try { // Input structure string strkey = pug_ws.InputStructure(str, decodeFormatByValue(str)); if ( pug_ws.GetOperationStatus(strkey) == StatusType.eStatus_Success ) Console.Out.WriteLine("InputStructure(\"{0}\"): {1}", str, strkey); else { Console.Error.WriteLine("InputStructure(\"{0}\") failed", str); return; } // IdentitySearch IdentitySearchOptions idOptions = new IdentitySearchOptions(); idOptions.eIdentity = IdentityType.eIdentity_SameConnectivity; string listkey = pug_ws.IdentitySearch(strkey, idOptions, null); Console.Out.WriteLine("IdentitySearch({0}, eIdentity_SameConnectivity): {1}", strkey, listkey); processSearchResult(listkey); } catch ( Exception ex ) { Console.Error.WriteLine(ex.Message); } } private static void processSearchResult(string listkey) { OutcomeType ot = waitForCompletion(listkey, TimeSpan.FromSeconds(60)); if ( ot == OutcomeType.Timeout ) Console.Error.WriteLine("Could not get results within reasonable timespan"); else if ( ot == OutcomeType.Error ) Console.Error.WriteLine("Search failed: {0}", pug_ws.GetStatusMessage(listkey)); else { // Retrieve CIDs int[] cids = pug_ws.GetIDList(listkey); string[] s_cids = Array.ConvertAll(cids, delegate(int i) { return i.ToString(); }); Console.Out.WriteLine("GetIDList({0}): {1}", listkey, String.Join(", ", s_cids)); } } private static void SubstructureSearch(string file) { try { // Input structure string strkey = ImportStructureFromFile(file); if ( strkey != String.Empty ) { // SubstructureSearch StructureSearchOptions ssOptions = new StructureSearchOptions(); ssOptions.eStereo = StereoType.eStereo_Ignore; string listkey = pug_ws.SubstructureSearch(strkey, ssOptions, null); Console.Out.WriteLine("SubstructureSearch({0}, eStereo_Ignore): {1}", strkey, listkey); processSearchResult(listkey); } } catch ( Exception ex ) { Console.Error.WriteLine(ex.Message); } } private static void SuperstructureSearch(string file) { try { // Input structure string strkey = ImportStructureFromFile(file); if ( strkey != String.Empty ) { // SuperstructureSearch StructureSearchOptions ssOptions = new StructureSearchOptions(); ssOptions.eStereo = StereoType.eStereo_Ignore; string listkey = pug_ws.SuperstructureSearch(strkey, ssOptions, null); Console.Out.WriteLine("SuperstructureSearch({0}, eStereo_Ignore): {1}", strkey, listkey); processSearchResult(listkey); } } catch ( Exception ex ) { Console.Error.WriteLine(ex.Message); } } private static void SimilaritySearch(string file) { try { // Input structure string strkey = ImportStructureFromFile(file); if ( strkey != String.Empty ) { // SuperstructureSearch SimilaritySearchOptions simOptions = new SimilaritySearchOptions(); simOptions.threshold = 99; string listkey = pug_ws.SimilaritySearch2D(strkey, simOptions, null); Console.Out.WriteLine("SimilaritySearch({0}, 99%): {1}", strkey, listkey); processSearchResult(listkey); } } catch ( Exception ex ) { Console.Error.WriteLine(ex.Message); } } private static void MSSearch(string mf) { try { // MFSearch MFSearchOptions mf_options = new MFSearchOptions(); mf_options.AllowOtherElements = false; string listkey = pug_ws.MFSearch(mf, mf_options, null); Console.Out.WriteLine("MFSearch({0}, AllowOtherElements): {1}", mf, listkey); processSearchResult(listkey); } catch ( Exception ex ) { Console.Error.WriteLine(ex.Message); } } private static void Standardize(string str) { try { // Input structure string strkey = pug_ws.InputStructure(str, decodeFormatByValue(str)); if ( pug_ws.GetOperationStatus(strkey) == StatusType.eStatus_Success ) Console.Out.WriteLine("InputStructure(\"{0}\"): {1}", str, strkey); else { Console.Error.WriteLine("InputStructure(\"{0}\") failed", str); return; } pug_ws.Standardize(ref strkey); Console.Out.WriteLine("Standardize({0}): {1}", str, strkey); OutcomeType ot = waitForCompletion(strkey, TimeSpan.FromSeconds(60)); if ( ot == OutcomeType.Timeout ) Console.Error.WriteLine("Could not get \"{0}\" standardized within reasonable timespan", str); else if ( ot == OutcomeType.Error ) Console.Error.WriteLine("Standardization of \"{0}\" failed: {1}", str, pug_ws.GetStatusMessage(strkey)); else { // Retrieve CID int CID; bool bCIDExists; pug_ws.GetStandardizedCID(strkey, out CID, out bCIDExists); Console.Out.WriteLine("CID({0}): {1}", str, bCIDExists ? CID.ToString() : "unavailable"); // Retrieve structure string inchi = pug_ws.GetStandardizedStructure(strkey, FormatType.eFormat_InChI); Console.Out.WriteLine("InChI({0}): {1}", str, inchi); } } catch ( Exception ex ) { Console.Error.WriteLine(ex.Message); } } private static void Download(int[] cids) { try { string listkey = pug_ws.InputList(cids, PCIDType.eID_CID); string dwldkey = pug_ws.Download(listkey, FormatType.eFormat_SDF, CompressType.eCompress_GZip, true); OutcomeType ot = waitForCompletion(dwldkey, TimeSpan.FromSeconds(60)); if ( ot == OutcomeType.Timeout ) Console.Error.WriteLine("Could not get results within reasonable timespan"); else if ( ot == OutcomeType.Error ) Console.Error.WriteLine("Download failed: {0}", pug_ws.GetStatusMessage(dwldkey)); else Console.Out.WriteLine("Download URL: {0}", pug_ws.GetDownloadUrl(dwldkey)); } catch ( Exception ex ) { Console.Error.WriteLine(ex.Message); } } private static void ScoreMatrix() { try { string listkey = searchMF("C6H6"); string dwldkey = pug_ws.ScoreMatrix(listkey, null, ScoreTypeType.eScoreType_Sim2DSubs, MatrixFormatType.eMatrixFormat_IdIdScore, CompressType.eCompress_None, true); if ( waitForCompletion(dwldkey, TimeSpan.FromSeconds(60)) != OutcomeType.Success ) throw new Exception(String.Format("Download failed: {0}", pug_ws.GetStatusMessage(dwldkey))); Console.Out.WriteLine("Download URL: {0}", pug_ws.GetDownloadUrl(dwldkey)); string listkey2 = searchMF("C8H8"); dwldkey = pug_ws.ScoreMatrix(listkey, listkey2, ScoreTypeType.eScoreType_Sim2DSubs, MatrixFormatType.eMatrixFormat_IdIdScore, CompressType.eCompress_None, true); if ( waitForCompletion(dwldkey, TimeSpan.FromSeconds(60)) != OutcomeType.Success ) throw new Exception(String.Format("Download failed: {0}", pug_ws.GetStatusMessage(dwldkey))); Console.Out.WriteLine("Download URL: {0}", pug_ws.GetDownloadUrl(dwldkey)); } catch ( Exception ex ) { Console.Error.WriteLine(ex.Message); } } private static string searchMF(string mf) { MFSearchOptions mf_options = new MFSearchOptions(); mf_options.AllowOtherElements = false; string listkey = pug_ws.MFSearch(mf, mf_options, null); Console.Out.WriteLine("MFSearch({0}, !AllowOtherElements): {1}", mf, listkey); OutcomeType ot = waitForCompletion(listkey, TimeSpan.FromSeconds(60)); if ( ot != OutcomeType.Success ) throw new Exception(String.Format("Search failed: {0}", pug_ws.GetStatusMessage(listkey))); return listkey; } private enum OutcomeType { Success, Timeout, Error }; private static OutcomeType waitForCompletion(string rid, TimeSpan timeout) { DateTime start = DateTime.Now; StatusType status = pug_ws.GetOperationStatus(rid); while ( status == StatusType.eStatus_Queued || status == StatusType.eStatus_Running ) { if ( timeout != null && DateTime.Now - start >= timeout ) return OutcomeType.Timeout; Thread.Sleep(TimeSpan.FromSeconds(1)); status = pug_ws.GetOperationStatus(rid); } if ( status != StatusType.eStatus_Success && status != StatusType.eStatus_HitLimit && status != StatusType.eStatus_TimeLimit ) { return OutcomeType.Error; } return OutcomeType.Success; } } }