using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.IO; namespace com.epam.indigo { /// /// Bingo instance corresponds to a single chemical database /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public unsafe class Bingo : IDisposable { private Indigo _indigo; private BingoLib _lib; private IndigoDllLoader _dllLoader; private int _id; private Bingo(Indigo indigo, int id, BingoLib lib) { _indigo = indigo; _lib = lib; _id = id; _dllLoader = IndigoDllLoader.Instance; } /// /// Destructor /// ~Bingo() { Dispose(); } /// /// Dispose method that closes the database /// public void Dispose() { if (_id >= 0 && _dllLoader != null && _dllLoader.isValid() && _indigo != null && _lib != null) { _indigo.setSessionID(); Bingo.checkResult(_indigo, _lib.bingoCloseDatabase(_id)); _id = -1; } } /// /// Method to close a database /// public void close() { Dispose(); } internal static int checkResult(Indigo indigo, int result) { if (result < 0) { throw new BingoException(new String(indigo._indigo_lib.indigoGetLastError())); } return result; } internal static float checkResult(Indigo indigo, float result) { if (result < 0.0) { throw new BingoException(new String(indigo._indigo_lib.indigoGetLastError())); } return result; } internal static string checkResult(Indigo indigo, string result) { if (result == null) { throw new BingoException(new String(indigo._indigo_lib.indigoGetLastError())); } return result; } private static BingoLib getLib(Indigo indigo) { String dllpath = indigo.getDllPath(); string libraryName; IndigoDllLoader dll_loader = IndigoDllLoader.Instance; switch (Environment.OSVersion.Platform) { case PlatformID.Win32NT: libraryName = "bingo.dll"; dll_loader.loadLibrary(dllpath, libraryName, "com.epam.indigo.Properties.ResourcesWin", false); break; case PlatformID.Unix: if (IndigoDllLoader.isMac()) { libraryName = "libbingo.dylib"; dll_loader.loadLibrary(dllpath, libraryName, "com.epam.indigo.Properties.ResourcesMac", false); } else { libraryName = "libbingo.so"; dll_loader.loadLibrary(dllpath, libraryName, "com.epam.indigo.Properties.ResourcesLinux", false); } break; default: throw new PlatformNotSupportedException(String.Format("Unsupported platform: {0}", Environment.OSVersion.Platform)); } return dll_loader.getInterface(libraryName); } /// /// Creates a chemical storage of a specifed type in a specified location /// /// Indigo instance /// Directory with the files location /// "molecule" or "reaction" /// Additional options separated with a semicolon. See the Bingo documentation for more details. /// Bingo database instance public static Bingo createDatabaseFile(Indigo indigo, string location, string type, string options) { indigo.setSessionID(); if (options == null) { options = ""; } BingoLib lib = Bingo.getLib(indigo); int databaseID = Bingo.checkResult(indigo, lib.bingoCreateDatabaseFile(location, type, options)); return new Bingo(indigo, databaseID, lib); } /// /// Creates a chemical storage of a specifed type in a specified location /// /// Indigo instance /// Directory with the files location /// "molecule" or "reaction" /// Bingo database instance public static Bingo createDatabaseFile(Indigo indigo, string location, string type) { return createDatabaseFile(indigo, location, type, null); } /// /// Loads a chemical storage of a specifed type from a specified location /// /// Indigo instance /// Directory with the files location /// Additional options separated with a semicolon. See the Bingo documentation for more details. /// Bingo database instance public static Bingo loadDatabaseFile(Indigo indigo, string location, string options) { if (options == null) { options = ""; } BingoLib lib = Bingo.getLib(indigo); indigo.setSessionID(); int databaseID = Bingo.checkResult(indigo, lib.bingoLoadDatabaseFile(location, options)); return new Bingo(indigo, databaseID, lib); } /// /// Loads a chemical storage of a specifed type from a specified location /// /// Indigo instance /// Directory with the files location /// Bingo database instance public static Bingo loadDatabaseFile(Indigo indigo, string location) { return loadDatabaseFile(indigo, location, null); } /// /// Insert a structure into the database and returns id of this structure /// /// Indigo object with a chemical structure (molecule or reaction) /// record id public int insert(IndigoObject record) { _indigo.setSessionID(); return Bingo.checkResult(_indigo, _lib.bingoInsertRecordObj(_id, record.self)); } /// /// Inserts a structure under a specified id /// /// Indigo object with a chemical structure (molecule or reaction) /// record id /// inserted record id public int insert(IndigoObject record, int id) { _indigo.setSessionID(); return Bingo.checkResult(_indigo, _lib.bingoInsertRecordObjWithId(_id, record.self, id)); } /// /// Delete a record by id /// /// Record id public void delete(int id) { _indigo.setSessionID(); Bingo.checkResult(_indigo, _lib.bingoDeleteRecord(_id, id)); } /// /// Execute substructure search operation /// /// Indigo query object (molecule or reaction) /// Search options /// Bingo search object instance public BingoObject searchSub(IndigoObject query, string options) { if (options == null) { options = ""; } _indigo.setSessionID(); return new BingoObject(Bingo.checkResult(_indigo, _lib.bingoSearchSub(_id, query.self, options)), _indigo, _lib); } /// /// Execute substructure search operation /// /// Indigo query object (molecule or reaction) /// Bingo search object instance public BingoObject searchSub(IndigoObject query) { return searchSub(query, null); } /// /// Execute similarity search operation /// /// indigo object (molecule or reaction) /// Minimum similarity value /// Maximum similairy value /// Default value is "tanimoto" /// Bingo search object instance public BingoObject searchSim(IndigoObject query, float min, float max, string metric) { if (metric == null) { metric = "tanimoto"; } _indigo.setSessionID(); return new BingoObject(Bingo.checkResult(_indigo, _lib.bingoSearchSim(_id, query.self, min, max, metric)), _indigo, _lib); } /// /// Execute similarity search operation /// /// indigo object (molecule or reaction) /// Minimum similarity value /// Maximum similairy value /// Bingo search object instance public BingoObject searchSim(IndigoObject query, float min, float max) { return searchSim(query, min, max, null); } /// /// Perform exact search operation /// /// indigo object (molecule or reaction) /// Bingo search object instance public BingoObject searchExact(IndigoObject query) { return searchExact(query, null); } /// /// Perform exact search operation /// /// indigo object (molecule or reaction) /// exact search options /// Bingo search object instance public BingoObject searchExact(IndigoObject query, string options) { if (options == null) { options = ""; } _indigo.setSessionID(); return new BingoObject(Bingo.checkResult(_indigo, _lib.bingoSearchExact(_id, query.self, options)), _indigo, _lib); } /// /// Perform search by molecular formula /// /// string with formula to search. For example, 'C22 H23 N3 O2'. /// search options /// Bingo search object instance public BingoObject searchMolFormula(string query, string options) { if (options == null) { options = ""; } _indigo.setSessionID(); return new BingoObject(Bingo.checkResult(_indigo, _lib.bingoSearchMolFormula(_id, query, options)), _indigo, _lib); } /// /// Perform search by molecular formula /// /// string with formula to search. For example, 'C22 H23 N3 O2'. /// Bingo search object instance public BingoObject searchMolFormula(string query) { return searchMolFormula(query, null); } /// /// Post-process index optimization /// public void optimize () { _indigo.setSessionID(); Bingo.checkResult(_indigo, _lib.bingoOptimize(_id)); } /// /// Returns an IndigoObject for the record with the specified id /// /// record id /// Indigo object public IndigoObject getRecordById(int id) { _indigo.setSessionID(); return new IndigoObject(_indigo, Bingo.checkResult(_indigo, _lib.bingoGetRecordObj(_id, id))); ; } /// /// Returns Bingo version /// /// Bingo version public string version() { _indigo.setSessionID(); return Bingo.checkResult(_indigo, _lib.bingoVersion()); } } }