/// <summary> /// Build the query that is executed to retrieve the summarized target data and optional structure data /// </summary> /// <returns></returns> void BuildQuery(string title) { int qid = SS.I.ServicesIniFile.ReadInt("SpillTheBeansToolModelQuery", -1); if (qid < 0) { throw new Exception("SpillTheBeansToolModelQuery not defined"); } Query q = QbUtil.ReadQuery(qid); if (q == null) { throw new Exception("Failed to read SpillTheBeansToolModelQuery: " + qid); } AssertMx.IsDefined(KeyQc?.Criteria, "KeyQc.Criteria"); ParsedSingleCriteria psc = MqlUtil.ParseSingleCriteria(KeyQc?.Criteria); // parse key criteria KeyQc.CopyCriteriaToQueryKeyCritera(q); Query = q; // save for later use return; }
/// <summary> /// Initialize with associated command line control /// </summary> /// <param name="commandLineControl"></param> public static QuickSearchPopup Initialize( MRUEdit commandLineControl, Form parentForm, ContentsTreeControl contentstree) { QuickSearchPopup qsp = null; AssertMx.IsNotNull(commandLineControl, "CommandLineControl"); AssertMx.IsNotNull(parentForm, "ParentForm"); foreach (Control c in parentForm.Controls) // see if already exists { if (c is QuickSearchPopup) { qsp = c as QuickSearchPopup; break; } } bool newPopup = false; if (qsp == null) // need to create new popup in parent form { qsp = new QuickSearchPopup(); parentForm.Controls.Add(qsp); newPopup = true; } qsp.Initialize2(commandLineControl, parentForm, contentstree, newPopup); return(qsp); }
/// <summary> /// Ignore criteria because they are used as parameters for formatting Rgroup table /// not as search criteria. /// </summary> /// <param name="mt"></param> /// <returns></returns> public override bool IgnoreCriteria( MetaTable mt) { AssertMx.IsNotNull(SpecificBroker, "SpecificBroker"); return(SpecificBroker.IgnoreCriteria(mt)); }
internal void SetupColumnsTab() { QueryColumn qc = null; ColumnMapMsx map = null; AssertMx.IsNotNull(SVP, "ViewManager"); //AssertMx.IsNotNull(SVP.BaseQuery, "ViewQuery"); //ColumnsTabSourceQueryControl.Setup(SVM.SVP.DataMap); // Id column //IdColumnQc = CurrentMap.ColumnMapCollection.GetQueryColumnForSpotfireColumnName(V.IdColumnName); //if (IdColumnQc == null) //{ // IdColumnQc = SVP.BaseQuery.GetFirstMatchingQueryColumn(MetaColumnType.CompoundId, mustBeSelected: true); //} //IdColumnSelector.Setup(SVP, IdColumnQc, EditValueChanged); // Focus Column //FocusColumnQc = CurrentMap.ColumnMapCollection.GetQueryColumnForSpotfireColumnName(V.FocusColumnName); //if (FocusColumnQc == null) //{ // FocusColumnQc = SVP.BaseQuery.GetFirstMatchingQueryColumn(MetaColumnType.Structure, mustBeSelected: true); //} //FocusColumnSelector.Setup(SVP, FocusColumnQc, EditValueChanged); // Horizontal & vertical chart setups SetupCondFormatRulesFromColorCodingItems(HorizontalGradientBarChartControl, V.SelectedColumns, CondFormatStyle.ColorScale); SetupCondFormatRulesFromColorCodingItems(VerticalBarChartControl, V.FeatureColumns, CondFormatStyle.ColorSet); return; }
/// <summary> /// Execute a chunk of JavaScript, wait for completion & return response /// </summary> /// <param name="script"></param> public static object ExecuteScript( KekuleJsWinFormsBrowserMsft KekuleJsBrowser, string script) { //if (DebugMx.True) return new JavascriptResponse(); // disable - debug Stopwatch sw = Stopwatch.StartNew(); if (KekuleJsBrowser?.Browser?.Document == null) { AssertMx.IsNotNull(KekuleJsBrowser?.Browser?.Document, "KekuleJsBrowser?.Browser?.Document"); } WebBrowser browser = KekuleJsBrowser.Browser; if (Debug) { DebugLog.Message("Script: " + script + KekuleJsBrowser.IdText); } object result = browser.Document.InvokeScript("execScript", new Object[] { script, "JavaScript" }); if (Debug) { DebugLog.StopwatchMessage("Script complete, Time: ", sw); } return(result); }
/// <summary> /// Try to get the row at the given index from the buffer or cache /// </summary> /// <param name="cursor"></param> /// <returns></returns> public bool TryToGetNextRow( VoArrayListCursor cursor) { object[] vo = null; int ri = cursor.Position + 1; AssertMx.IsTrue(ri >= 0, "Invalid row index:" + ri); if (ri >= TotalRowCount) { if (DebugCaching) { DebugLog.Message(string.Format("Row {0} not in cache or buffer (cacheSize: {1}, bufferSize: {2})", ri, RowsWrittenToCache, RowBuffer.Count)); } return(false); // not in collection } vo = GetRow(ri); cursor.CurrentRow = vo; cursor.Position = ri; return(true); }
/// <summary> /// NextRow - Return the next matching row value object /// </summary> /// <returns></returns> public override Object[] NextRow() { AssertMx.IsNotNull(SpecificBroker, "SpecificBroker"); Object[] row = SpecificBroker.NextRow(); return(row); }
/// <summary> /// Do a quick validation on a node to be sure the key document links are defined /// </summary> public void ValidateNode() { AssertMx.IsNotNull(App, "App"); AssertMx.IsNotNull(App?.Document, "App.Document"); AssertMx.IsNotNull(App?.Document?.DataManager, "App.Document.DataManager"); AssertMx.IsNotNull(App?.Document?.DataManager?.TableCollection, "App.Document.TableCollection"); AssertMx.IsNotNull(App?.Document?.Pages, "App.Document.Pages"); }
/// <summary> /// GetFormValues /// </summary> /// <param name="tso"></param> /// <returns></returns> public bool GetFormValues( QueryColumn tso) { QueryColumn qc = QueryColumn; AssertMx.IsNotNull(qc, "QueryColumn"); return(true); }
/// <summary> /// Build query to get summarization detail for RgroupMatrix data element /// </summary> /// <param name="mt"></param> /// <param name="mc"></param> /// <param name="resultId">act_code.compound_id</param> /// <returns></returns> public override Query GetDrilldownDetailQuery( MetaTable mt, MetaColumn mc, int level, string resultId) { AssertMx.IsNotNull(SpecificBroker, "SpecificBroker"); Query q = SpecificBroker.GetDrilldownDetailQuery(mt, mc, level, resultId); return(q); }
/// <summary> /// Setup the control for specified view /// </summary> /// <param name="view"></param> public void Setup( DataTableMapMsx dataTableMap, EventHandler callerEditValueChangedEventHandler = null) { InSetup = true; DataTableMap = dataTableMap; CallerEditValueChangedHandler = callerEditValueChangedEventHandler; QueryTablesComboBox.Text = ""; QueryTablesComboBox.SelectedItem = null; ComboBoxItemCollection items = QueryTablesComboBox.Properties.Items; items.Clear(); QueryTablesComboBox.Text = ""; AssertMx.IsNotNull(Query, "Query"); foreach (QueryTable qtEnum in Query.Tables) { items.Add(new ComboBoxItem(new LabeledUiItem(qtEnum.ActiveLabel, qtEnum))); } items.Add(AllTablesItem); items.Add(NoTablesItem); QueryTable qt = DataTableMap?.QueryTable; if (dataTableMap.IsMappedToSingleQueryTable) { QueryTablesComboBox.Text = qt.ActiveLabel; QueryTablesComboBox.SelectedItem = new LabeledUiItem(qt.ActiveLabel, qt); } else if (dataTableMap.IsMappedToAllQueryTables) // if cols are mapped assume all tables { QueryTablesComboBox.Text = AllTablesItem.Label; QueryTablesComboBox.SelectedItem = AllTablesItem; } else // not mapped to any tables { QueryTablesComboBox.Text = NoTablesItem.Label; QueryTablesComboBox.SelectedItem = NoTablesItem; } InSetup = false; return; }
/// <summary> /// Indexer into pages /// </summary> /// <param name="cpi"></param> /// <returns></returns> public ResultsPage this[int cpi] { get { AssertMx.IsTrue(cpi >= 0 && cpi < Pages.Count); return(Pages[cpi]); } set { AssertMx.IsTrue(cpi >= 0 && cpi < Pages.Count); Pages[cpi] = value; } }
/// <summary> /// Get/set a single element in the ItemArray object array by col name /// </summary> /// <param name="columnName"></param> /// <returns></returns> public object this[string columnName] { get { int index = Table.Columns.IndexOf(columnName); AssertMx.IsTrue(index >= 0, "Column not found: " + columnName); return(this[index]); } set { int index = Table.Columns.IndexOf(columnName); AssertMx.IsTrue(index >= 0, "Column not found: " + columnName); this[index] = value; } }
/// CopyCriteriaToKeyQueryCritera (Query.KeyCriteria has precedence over QC.Critera) public void CopyCriteriaToQueryKeyCritera( Query query) { AssertMx.IsNotNull(query, "Query"); string criteria = Criteria; string colName = MetaColumnName + " "; // prepend metacolumn name if not done yet if (Lex.StartsWith(criteria, colName)) // remove col name { criteria = criteria.Substring(colName.Length); } query.KeyCriteria = criteria; query.KeyCriteriaDisplay = CriteriaDisplay; return; }
/// <summary> /// Adding a new SasMap view to an existing query /// </summary> /// <param name="args"></param> /// <returns></returns> public DialogResult ShowInitialViewSetupDialog( QueryColumn sasQc, Query baseQuery) { MetaTable mt; QueryColumn qc = null; DialogResult dr; AssertMx.IsNotNull(sasQc, "SasQc"); AssertMx.IsNotNull(SasQuery, "SasMapQuery"); SasQc = sasQc; BaseQuery = baseQuery; Parms = GetParmsFromQueryColumnCriteria(SasQc); qc = BaseQuery?.GetFirstKeyColumn(); if (qc == null) { qc = QueryTable.GetDefaultRootQueryTable()?.KeyQueryColumn; } qc = qc.Clone(); Parms.KeyCriteriaQc = qc; // assign QC with undefined criteria QueryManager refQm = (baseQuery.QueryManager as QueryManager); List <string> resultsKeys = refQm?.DataTableManager?.ResultsKeys; if (resultsKeys != null && resultsKeys.Count > 0) { string listText = Csv.JoinCsvString(resultsKeys); qc.CriteriaDisplay = CidList.FormatAbbreviatedCidListForDisplay(qc, listText); qc.Criteria = qc.MetaColumn.Name + " IN (" + listText + ")"; } Parms.EndpointMc = BaseQuery?.GetFirstResultColumnByPriority(true)?.MetaColumn; // pick the highest priority result column as the default endpoint dr = Edit(); if (dr == DialogResult.OK) { StoreParmsInQueryColumnCriteria(Parms, SasQc); } return(dr); }
/// <summary> /// Read in all of the rows that have been cached /// </summary> /// <returns></returns> public List <object[]> ReadCachedRows() { AssertMx.IsTrue(File.Exists(CacheFilePath), "CacheFilePath doesn't exist"); FileStream fs = File.Open(CacheFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(fs); List <object[]> cachedRows = new List <object[]>(); // in-memory buffer list of rows for (int ri = 0; ri < RowsWrittenToCache; ri++) { object[] vo = VoArray.ReadBinaryVoArray(br, VoaLength); cachedRows.Add(vo); } br.Close(); // close reader and the underlying stream return(cachedRows); }
/// <summary> /// CopyCriteriaFromKeyQueryCritera - Query.KeyCriteria has precedence over QC.Critera /// </summary> public void CopyCriteriaFromQueryKeyCriteria( Query query) { AssertMx.IsNotNull(query, "Query"); Criteria = query.KeyCriteria; CriteriaDisplay = query.KeyCriteriaDisplay; if (Lex.IsUndefined(Criteria)) { return; // undefined } string colName = MetaColumnName + " "; // prepend metacolumn name if not done yet if (!Lex.StartsWith(Criteria, colName)) { Criteria = colName + Criteria; } return; }
/// <summary> /// Invoke the editor /// </summary> /// <param name="qc">QueryColumn to edit</param> /// <returns></returns> public static bool Edit( QueryColumn qc) { AssertMx.IsNotNull(qc, "qc"); if (Instance == null) { Instance = new CriteriaCompoundId(); } new JupyterGuiConverter().ConvertFormOrUserControl(Instance); if (qc.IsKey) // if key col be sure qc is in sync with Query.KeyCriteria { qc.CopyCriteriaFromQueryKeyCriteria(); } if (qc.Criteria.Trim().StartsWith("=") && qc.MetaColumn != null) { qc.Criteria = qc.MetaColumnName + " " + qc.Criteria; } Instance.Qc = qc; Instance.Setup(qc); DialogResult dr = Instance.ShowDialog(SessionManager.ActiveForm); if (dr == DialogResult.OK) { if (qc.IsKey) // if key col then update Query.KeyCriteria { qc.CopyCriteriaToQueryKeyCritera(); } return(true); } else { return(false); } }
/// <summary> /// Edit the Cids criteris /// </summary> /// <returns></returns> bool EditCids() { QueryColumn qc = QueryColumn; AssertMx.IsNotNull(qc, "QueryColumn"); if (!CriteriaEditor.GetCompoundIdCriteria(qc)) { return(false); } if (Lex.IsDefined(qc.CriteriaDisplay)) { Cids.Text = qc.CriteriaDisplay; } else { Cids.Text = qc.Criteria; } return(true); }
/// <summary> /// Search a single file /// </summary> void SearchSingleFile(int fi) { StructSearchMatch sm = null; AssertMx.IsNotNull(FpDao, "FpDao"); List <StructSearchMatch> matchList = FileMatchLists[fi]; AssertMx.IsNotNull(matchList, "matchList"); OpenBitSet queryObs = new OpenBitSet(QueryFpLongArray, QueryFpLongArray.Length); AssertMx.IsNotNull(queryObs, "queryObs"); OpenBitSet dbObs = new OpenBitSet(QueryFpLongArray, QueryFpLongArray.Length); // gets set to DB fp for intersect AssertMx.IsNotNull(dbObs, "dbObs"); FileStream fs = FileStreamReaders[fi]; AssertMx.IsNotNull(fs, "fs"); ReadFingerprintRecArgs a = new ReadFingerprintRecArgs(); a.Initialize(fs, QueryFpLongArray.Length); try { while (true) { bool readOk = FpDao.ReadRawFingerprintRec(a); if (!readOk) { break; } //if (IsSrcCidMatch("03435269", a)) a = a; // debug dbObs.Bits = a.fingerprint; dbObs.Intersect(queryObs); int commonCnt = (int)dbObs.Cardinality(); float simScore = commonCnt / (float)(a.cardinality + QueryFpCardinality - commonCnt); if (simScore >= MinimumSimilarity) { sm = ReadFingerprintRec_To_StructSearchMatch(a); sm.SearchType = StructureSearchType.MolSim; sm.MatchScore = simScore; matchList.Add(sm); } } } catch (Exception ex) { string msg = ex.Message; msg += string.Format("\r\nfi: {0}, fs.Name: {1}, sm: {2}", fi, fs.Name, sm != null ? sm.Serialize() : ""); DebugLog.Message(DebugLog.FormatExceptionMessage(ex, msg)); throw new Exception(msg, ex); } return; }
/// <summary> /// ExecuteSearch /// </summary> /// <param name="queryMol"></param> public List <StructSearchMatch> ExecuteSearch( IAtomContainer queryMol) { AssertMx.IsTrue(FingerprintType == FingerprintType.MACCS || FingerprintType == FingerprintType.Circular, "Invalid FingerprintType: " + FingerprintType); QueryMol = queryMol; BitSetFingerprint fp = // generate a fingerprint CdkMol.BuildBitSetFingerprintForLargestFragment(queryMol, FingerprintType); QueryFpCardinality = fp.cardinality(); QueryFpLongArray = fp.asBitSet().toLongArray(); MatchList = new List <StructSearchMatch>(); ThreadException = null; foreach (string databaseName in FingerprintDbMx.Databases) // loop on all databases { int srcId = -1; if (Lex.Contains(databaseName, "corp")) { if (!GetCorpSim) { continue; } srcId = StructSearchMatch.CorpDbId; } else if (Lex.Contains(databaseName, "chembl")) { if (!GetChemblSim) { continue; } srcId = StructSearchMatch.ChemblDbId; } if (Debug) { DebugLog.Message("Starting sim search on " + databaseName + " database"); } FpDao = new FingerprintDao(databaseName, FingerprintType); if (!FpDao.DataFilesExist()) { continue; // no files for this database } FileStreamReaders = FpDao.OpenReaders(); FileMatchLists = new List <StructSearchMatch> [FileStreamReaders.Length]; for (int i1 = 0; i1 < FileMatchLists.Length; i1++) { FileMatchLists[i1] = new List <StructSearchMatch>(); } DateTime t0 = DateTime.Now; if (UseMultipleThreads) { ExecuteMultiThreadSearch(); } else { ExecuteSingleThreadSearch(); } double et = TimeOfDay.Delta(ref t0); FpDao.CloseReaders(); List <StructSearchMatch> matchList = MergeIndividualFileMatchLists(); if (KeysToExclude != null || SearchKeySubset != null) // filter by any allowed/disallowed keys { List <StructSearchMatch> matchList2 = new List <StructSearchMatch>(); foreach (StructSearchMatch m0 in matchList) { if (KeysToExclude != null && KeysToExclude.Contains(m0.SrcCid)) { continue; } if (SearchKeySubset != null && !SearchKeySubset.Contains(m0.SrcCid)) { continue; } matchList2.Add(m0); } matchList = matchList2; } matchList.Sort(StructSearchMatch.CompareByMatchQuality); //int removeCount = matchList.Count - MaxHits; // limit to maxhits per database //if (removeCount > 0) // matchList.RemoveRange(MaxHits, removeCount); //foreach (StructSearchMatch ssm0 in matchList) // if (ssm0.SrcId != srcId) ssm0.SrcId = srcId; // debug MatchList.AddRange(matchList); double et2 = TimeOfDay.Delta(ref t0); string msg = string.Format("Search complete (" + databaseName + ").Time : {0:0.00} ", et) + string.Format("{0} Hits: ", FileMatchLists[0].Count); if (Debug) { DebugLog.Message(msg); } for (int hi = 0; hi < 5 && hi < FileMatchLists[0].Count; hi++) { StructSearchMatch sm = FileMatchLists[0][hi]; msg += sm.SrcCid + string.Format(" = {0:0.00}, ", sm.MatchScore); } } // database loop if (ThreadException != null) { throw new Exception(ThreadException.Message, ThreadException); } MatchList.Sort( // sort by decreasing sim value delegate(StructSearchMatch p1, StructSearchMatch p2) { return(p2.MatchScore.CompareTo(p1.MatchScore)); }); if (MaxHits > 0 && MatchList.Count > MaxHits) // remove hits beyond maximum if defined { MatchList.RemoveRange(MaxHits, MatchList.Count - MaxHits); } //ShowProgress(msg); //Thread.Sleep(10000000); return(MatchList); }
/// <summary> /// Setup the form for display /// </summary> /// <param name="cid"></param> /// <param name="qm"></param> /// <returns></returns> bool SetupForm( string cid, QueryManager qm) { AssertMx.IsDefined(cid, "Compound Id (CorpId)"); SelectedCid = cid; PreviousCid = cid; RootTable = CompoundId.GetRootMetaTableFromCid(cid); Qm = null; // no query context SourceQuery = null; CurrentBaseQueryCidHitList = null; if (qm != null) // if querymanager defined then base the related data query on the "current" query { Qm = qm; // setup query context SourceQuery = qm.Query; RootTable = qm.Query.RootMetaTable; if (qm.Query != null && qm.Query.Mode == QueryMode.Browse) // if browsing current base query results then get that cid list { CurrentBaseQueryCidHitList = Qm.DataTableManager.GetMostCompleteResultsKeyList(); } } //else throw new Exception("Parameters not defined"); //if (Lex.IsUndefined(SelectedCid) && // (Qm == null || Qm.MoleculeGrid == null || Qm.MoleculeGrid.Helpers == null)) // return false; SetupCheckmarks(); // Current Cid //if (Qm != null) // SelectedCid = Qm.MoleculeGrid.Helpers.GetCidForSelectedCell(); SelectedCid = CompoundId.Format(SelectedCid); CidCtl.Text = SelectedCid; //CidCtl.Focus(); // Marked cid count MarkedCidsList = null; if (Qm?.MoleculeGrid != null) { CidList cl = Qm.MoleculeGrid.GetMarkedList(); if (cl != null) { MarkedCidsList = cl.ToStringList(); } } int selCnt = (MarkedCidsList != null ? MarkedCidsList.Count : 0); MarkedCidsCheckEdit.Text = "Selected compound Ids (" + FormatCidListForDisplay(MarkedCidsList) + ")"; MarkedCidsCheckEdit.Enabled = (selCnt > 0); if (selCnt == 0 && MarkedCidsCheckEdit.Checked) { CurrentCidCheckEdit.Checked = true; } // All Cid count int allCnt = (CurrentBaseQueryCidHitList != null ? CurrentBaseQueryCidHitList.Count : 0); AllCidsCheckEdit.Text = "All Ids in the current result set (" + FormatCidListForDisplay(CurrentBaseQueryCidHitList) + ")"; AllCidsCheckEdit.Enabled = (allCnt > 0); if (selCnt == 0 && AllCidsCheckEdit.Checked) { CurrentCidCheckEdit.Checked = true; } // Structure MoleculeMx cs = new MoleculeMx(); if (Lex.IsDefined(SelectedCid)) { cs = MoleculeUtil.SelectMoleculeForCid(SelectedCid); } QueryMolCtl.SetupAndRenderMolecule(cs); // MRU list RenderMruList(); return(true); }
/// <summary> /// GetFpFileName /// </summary> /// <param name="fi"></param> /// <returns></returns> public string GetFpFileName(int fi) { AssertMx.IsTrue(FingerprintType == FingerprintType.MACCS || FingerprintType == FingerprintType.Circular); return(ServicesDirs.BinaryDataDir + @"\Fingerprints\" + DatabaseName + FingerprintFileNameToken + string.Format(@"Fingerprints{0:00}", fi)); }
/// <summary> /// Run the query /// </summary> /// <param name="browseExistingResults">If true browse existing results</param> /// <returns></returns> public string RunQuery3( ResultsFormat rf, bool saveHitlist, bool browseExistingResults) { Query modifiedQuery; QueryTable qt; QueryColumn qc; ResultsTable rt; ResultsField rfld; MetaTable mt; MetaColumn mc; string txt, msg; DialogResult dr; bool success; CellGraphic cg; Lex lex = new Lex(); string tempfile, tok, command, unrecognizedCommand, response; int ti, gi, rc, i1, i2; // Begin execution if (rf == null) { throw new Exception("QueryExec.Run - Null ResultsFormat"); } if (ResultsFormatter == null) { throw new Exception("QueryExec.Run - Null ResultsFormatter"); } if (rf.Segments == null) { throw new Exception("QueryExec.Run - Null ResultsFormat.Segments"); } if (Query == null) { throw new Exception("QueryExec.Run - Null Rf.Query"); } if (Query.Tables == null || Query.Tables.Count <= 0) { throw new QueryException("QueryExec.Run - No Query Tables"); } QueryManager qm = QueryManager; ReturnMsg = ""; //bool useExistingQueryEngine = Rf.ParentQe != null; //bool useExistingDataTable = Query.BrowseExistingResultsWhenOpened && Query.SerializeResults && // qm.DataTable != null && qm.DataTable.Rows.Count > 0; try { //if (Math.Sqrt(4) == 2) throw new Exception("test"); // debug if (!browseExistingResults) // normal open of search { Progress.Show("Analyzing query..."); // put up a status message to the user as soon as possible to let them know something is happening... dr = ValidateQuery(Query); if (dr == DialogResult.Cancel) { return(""); } WriteCurrentListToServerIfNeeded(Query); if (rf.OutputDestination == OutputDest.WinForms) // update access stats if grid { UpdateTableUsageStatistics(Query); } Query.ResultsDataTable = null; // be sure to get new results qm = BuildQueryManager(Query, rf); Query.ResetViewStates(); // reset state of views for proper operation if (Rf.ParentQe == null) // open search unless using existing query engine { if (!ExecuteSearch(saveHitlist)) // returns false if cancelled by user { Progress.Hide(); return(""); } } if ((ResultsKeys == null || ResultsKeys.Count == 0) && // nothing for search !Query.Preview && !MqlUtil.SingleStepExecution(Query) && qm.DataTable.Rows.Count == 0 && Query.RetrievesDataFromQueryEngine) { // if (!Rf.PopupDisplay) Progress.Hide(); if (qm.StatusBarManager != null) { qm.StatusBarManager.DisplayStatusMessage(""); } // if (QueryEngine.Cancelled) return ""; // cancelled by user msg = "No data have been found that matches your query."; if (ResultsFormat.PopupOutputFormContext && !ResultsFormat.SuppressNoDataMessage) { MessageBoxMx.Show(msg, "Search Result", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return("Command EditQuery"); // return to edit query menu } else { return(msg); } } //if (ResultsFormat.PopupDisplay) // SessionManager.DisplayStatusMessage("Retrieving data..."); //else Progress.Show("Retrieving data...", UmlautMobius.Value, true, "Cancelling Retrieval..."); //Progress.Show("Retrieving data...", UmlautMobius.String, true, "Cancelling Retrieval..."); Progress.Hide(); // hide progress - "Retrieving data..." message now appears as bottom line of grid if (ResultsFormat.Grid) { if (ResultsFormat.SessionOutputFormContext) // if normal main session form grid display, set browse mode & view state { Query.ResetViewStates(); // reset view state for all views QbUtil.SetMode(QueryMode.Browse, Query); if (ResultsFormat.Query.LogicType == QueryLogicType.And) // log grid query by logic type { UsageDao.LogEvent("QueryGridAnd", ""); } else if (ResultsFormat.Query.LogicType == QueryLogicType.Or) { UsageDao.LogEvent("QueryGridOr", ""); } else if (ResultsFormat.Query.LogicType == QueryLogicType.Complex) { UsageDao.LogEvent("QueryGridComplex", ""); } } else if (ResultsFormat.PopupOutputFormContext) // create popup window & configure { PopupResults.Show(qm); //MoleculeGridPanel.ConfigureAndShow(qm, null); } else if (ResultsFormat.ToolOutputFormContext) { ContainerControl cc; QueryResultsControl qrc = ResultsFormat.OutputContainerControl as QueryResultsControl; AssertMx.IsTrue(qrc != null, "ResultsFormat.OutputContainerControl must be a QueryResultsControl"); if (!WindowsHelper.FindContainerControl(qrc, typeof(ToolResultsContainer), out cc)) { throw new Exception("ToolResultsContainer not found"); } ToolResultsContainer trc = cc as ToolResultsContainer; trc.SetupQueryResultsControlForResultsDisplay(qm); } else { throw new Exception("Invalid OutputformContext: " + ResultsFormat.OutputFormContext); } } } else // reentering display switch to browse tab { QbUtil.SetMode(QueryMode.Browse, Query); } response = ResultsFormatter.BeginFormatting(browseExistingResults); // format the data if (ResultsFormat.SessionOutputFormContext) // normal display { if (MqlUtil.SingleStepExecution(Query)) { // be sure hit count display is up to date if (ResultsKeys != null) { if (qm.StatusBarManager != null) { qm.StatusBarManager.DisplayCurrentCount(); } } } if (saveHitlist) { CidList hitList = new CidList(ResultsKeys); rc = CidListCommand.WriteCurrentList(hitList); SessionManager.DisplayCurrentCount(); } } return(response); } // end of surrounding try catch (Exception ex) { Progress.Hide(); if (ex is UserQueryException) // exception that can occur from user error { throw new UserQueryException(ex.Message, ex); } else { msg = DebugLog.FormatExceptionMessage(ex); if (!Lex.Contains(msg, "QueryLogged:")) // exception & query { QueryEngine.LogExceptionAndSerializedQuery(msg, Query); } else { ServicesLog.Message(msg); // just log exception } throw new Exception(ex.Message, ex); // pass it up } } }
public static bool Edit( QueryColumn qc) { AssertMx.IsNotNull(qc, "qc"); MetaColumn mc = qc.MetaColumn; if (Instance == null) { Instance = new CriteriaYesNo(); } new JupyterGuiConverter().ConvertFormOrUserControl(Instance); Instance.Text = "Search criteria for " + qc.ActiveLabel; Instance.Prompt.Text = "Select a search option for " + qc.ActiveLabel + " from the list below."; ParsedSingleCriteria psc = MqlUtil.ParseSingleCriteria(qc.Criteria); if (psc == null) { psc = new ParsedSingleCriteria(); // no criteria } if (Lex.Eq(psc.Value, "Y")) { Instance.YesCheckEdit.Checked = true; } else if (Lex.Eq(psc.Value, "N")) { Instance.NoCheckEdit.Checked = true; } else { Instance.None.Checked = true; } DialogResult dr = Instance.ShowDialog(SessionManager.ActiveForm); if (dr == DialogResult.OK) { if (Instance.YesCheckEdit.Checked) { qc.CriteriaDisplay = "= Y"; qc.Criteria = mc.Name + " = 'Y'"; } else if (Instance.NoCheckEdit.Checked) { qc.CriteriaDisplay = "= N"; qc.Criteria = mc.Name + " = 'N'"; } else if (Instance.None.Checked) { qc.CriteriaDisplay = ""; qc.Criteria = ""; } return(true); } else { return(false); } }
/// <summary> /// Get the next value for the sequence (MySQL) /// </summary> /// <param name="seqName"></param> /// <returns></returns> public static long NextValLongMySQL( string seqName) { string sql; long nextVal; SequenceDao seqDao = Lookup(seqName); Queue <long> seqQueue = seqDao.Queue; if (seqQueue.Count > 0) { nextVal = seqQueue.Dequeue(); return(nextVal); } int count = (seqDao.CacheSize > 0 ? seqDao.CacheSize : 1); int t0 = TimeOfDay.Milliseconds(); DbCommandMx seqCmd = new DbCommandMx(); seqCmd.MxConn = DbConnectionMx.GetConnection("MySql_Mobius"); // "MySql_Mobius_Sequences" sql = String.Format( @"update mbs_owner.mbs_sequences set value = last_insert_id(value) + {0} where name = '{1}'" , count, seqName.ToUpper()); seqCmd.PrepareUsingDefinedConnection(sql); int updCount = seqCmd.ExecuteNonReader(); if (updCount <= 0) { throw new Exception("Error updating sequence (may not exist): " + seqName); } sql = "select last_insert_id()"; // gets value before update above seqCmd.PrepareUsingDefinedConnection(sql, null); DbDataReader rdr = seqCmd.ExecuteReader(); bool readOk = rdr.Read(); AssertMx.IsTrue(readOk, "readOk"); long value = rdr.GetInt64(0); rdr.Close(); seqCmd.CloseReader(); nextVal = value + 1; // return this one now long v2 = value + 2; // next value long vn = value + count; // last value for (long vi = v2; vi <= vn; vi++) { seqQueue.Enqueue(vi); } t0 = TimeOfDay.Milliseconds() - t0; // DebugLog.Message("Read sequence, set size = " + seqQueue.Count.ToString() + ", Time(ms) = " + t0.ToString()); return(nextVal); }
/// <summary> /// Close broker & release resources /// </summary> public override void Close() { AssertMx.IsNotNull(SpecificBroker, "SpecificBroker"); SpecificBroker.Close(); }
/// <summary> /// Read input data from database /// </summary> /// <param name="smp"> /// <returns></returns> List<CompoundStructureActivityData> ReadData( SasMapParms smp) { MetaColumn activityMc = smp.EndpointMc; QueryColumn keyCriteriaQc = smp.KeyCriteriaQc; AssertMx.IsNotNull(activityMc, "mc"); AssertMx.IsNotNull(keyCriteriaQc, "keyCriteriaQc"); MetaTable mt, mt2; MetaColumn mc2 = null; Query q = new Query(); mt = activityMc.MetaTable; QueryTable qt = new QueryTable(mt); if (mt.SummarizedExists && !mt.UseSummarizedData) { // retrieve summarized data if exists mt2 = MetaTableCollection.Get(mt.Name + MetaTable.SummarySuffix); if (mt2 != null) { mc2 = mt2.GetMetaColumnByName(activityMc.Name); if (mc2 == null) mc2 = mt2.GetMetaColumnByLabel(activityMc.Label); } if (mc2 != null) // same column available in summarized? { mt = mt2; activityMc = mc2; } } SMP.KeyCriteriaQc.CopyCriteriaToQueryKeyCritera(q); q.KeyCriteriaDisplay = SMP.KeyCriteriaQc.CriteriaDisplay; qt.SelectKeyOnly(); QueryColumn qc = qt.GetQueryColumnByName(activityMc.Name); qc.Selected = true; q.AddQueryTable(qt); QueryEngine qe = new QueryEngine(); List<string> keyList = qe.ExecuteQuery(q); // note that keylist may be empty if single-step query HashSet<string> keySet = new HashSet<string>(StringComparer.OrdinalIgnoreCase); List<CompoundStructureActivityData> data = new List<CompoundStructureActivityData>(); int rowCount = 0; while (true) { object[] vo = qe.NextRow(); if (vo == null) break; CompoundStructureActivityData cd = new CompoundStructureActivityData(); string cid = (string)vo[0]; cd.Cid = cid; keySet.Add(cid); // accumulate keys object val = vo[2]; if (NullValue.IsNull(val)) continue; if (val is double) cd.Activity = (double)val; else if (val is Int32) cd.Activity = (Int32)val; else if (val is NumberMx) { NumberMx nex = val as NumberMx; cd.Activity = nex.Value; } else if (val is QualifiedNumber) { QualifiedNumber qn = val as QualifiedNumber; cd.Activity = qn.NumberValue; //if (qn.Qualifier != null && qn.Qualifier != "" && qn.Qualifier != "=") // continue; // (don't want to do this since may filter out good data (e.g. IC50 <0.0001)) } else continue; if (cd.Activity == NullValue.NullNumber) continue; data.Add(cd); rowCount++; } // Retrieve structures keyList = new List<string>(keySet); Dictionary<string, MoleculeMx> csDict = MoleculeUtil.SelectMoleculesForCidList(keyList, qt.MetaTable); // get the structures in a single step // Add structures and build/store fingerprints to data DebugLog.Message("========== Fingerprints ============"); foreach (CompoundStructureActivityData cd in data) { if (!csDict.ContainsKey(cd.Cid) || csDict[cd.Cid] == null) continue; if (cd.Cid == "111" || cd.Cid == "222") csDict = csDict; // debug MoleculeMx cs = csDict[cd.Cid]; cd.Structure = cs; FingerprintType fpType = FingerprintType.Circular; int fpSubtype = -1; if (SMP.SimilarityType == SimilaritySearchType.ECFP4) // some issue with ECFP4? { fpType = FingerprintType.Circular; fpSubtype = CircularFingerprintType.ECFP4; } else if (SMP.SimilarityType == SimilaritySearchType.Normal) { fpType = FingerprintType.MACCS; } cd.BitsetFingerprint = cs.BuildBitSetFingerprint(fpType, fpSubtype); if (cd.BitsetFingerprint == null) continue; // couldn't build fingerprint (e.g. no structure) if (Debug) DebugLog.Message(cd.Cid + ": " + Lex.Join(CdkMolUtil.GetBitSet(cd.BitsetFingerprint), ", ")); } return data; }
/// <summary> /// Try to get the row at the given index from the buffer or cache /// </summary> /// <param name="ri"></param> /// <returns></returns> /// public object[] GetRow( int ri) { object[] vo = null; if (ri < 0 || ri >= TotalRowCount) { string msg = string.Format("Row {0} not in cache or buffer (cacheSize: {1}, bufferSize: {2})", ri, RowsWrittenToCache, RowBuffer.Count); throw new InvalidDataException(msg); } // See if in buffer if (ri >= RowsWrittenToCache) { vo = (object[])RowBuffer[ri - RowsWrittenToCache]; if (DebugCaching) { DebugLog.Message(string.Format("Row: {0} in Buffer (cacheSize: {1}, bufferSize: {2})", ri, RowsWrittenToCache, RowBuffer.Count)); } return(vo); } // Get from cache if (ri < CacheReaderPosition + 1) // if already read past this then close current cursor { CloseCacheReader(); } if (CacheReader == null) // need to open reader? { AssertMx.IsNotNull(CacheWriter, "CacheWriter"); AssertMx.IsTrue(File.Exists(CacheFilePath), "CacheFilePath doesn't exist"); FileStream fs = File.Open(CacheFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); CacheReader = new BinaryReader(fs); CacheReaderPosition = -1; if (DebugCaching) { DebugLog.Message("Opening cache reader"); } } while (CacheReaderPosition + 1 <= ri) // read rows until we get the one we want { vo = VoArray.ReadBinaryVoArray(CacheReader, VoaLength); CacheReaderPosition++; } CacheReaderPosition = ri; if (DebugCaching) { DebugLog.Message(string.Format("Row: {0} in Cache (cacheSize: {1}, bufferSize: {2})", ri, RowsWrittenToCache, RowBuffer.Count)); } return(vo); }
/// <summary> /// Execute query /// </summary> /// <param name="eqp"></param> public override void ExecuteQuery( ExecuteQueryParms eqp) { MetaTable mt; MetaColumn mc; Query q; QueryTable qt; QueryColumn qc; ResultsPage rp; ResultsViewProps view; CompoundStructureActivityData cd1, cd2; bool smallerIsbetter; double r1, r2, r3, r4; int di, di2, pdi, pdi2, i3; string tok; qt = eqp.QueryTable; qc = qt.GetQueryColumnByNameWithException(SasMapParms.ParametersMetaColumnName); AssertMx.IsDefined(qc.Criteria, qc.Label + " criteria not defined"); if (Lex.Eq(qc.Criteria, LastCriteriaString)) // if same criteria as last time then use existing data { VoListPos = -1; // init list position return; } VoList = new List<object[]>(); VoListPos = -1; // init list position LastCriteriaString = qc.Criteria; ParsedSingleCriteria psc = ParsedSingleCriteria.Parse(qc); SMP = SasMapParms.Deserialize(psc.Value); mc = SMP.EndpointMc; smallerIsbetter = mc.MultiPoint; List<CompoundStructureActivityData> ds1 = ReadData(SMP); // read in the data to analyze if (ds1 == null || ds1.Count == 0) return; // throw new QueryException("No data retrieved"); List<CompoundStructureActivityData> ds2 = ds1; // just one set for now // Calculate difference or ratio coefficents for each pair List<PairData> pd = new List<PairData>(); int minCoef = -1; // index of minimum coefficent selected so far double molFactor = AssayAttributes.GetMolarConcFactor(SMP.EndpointMc); for (di = 0; di < ds1.Count; di++) { // process all compounds in 1st set // if (ds1[di].Nearest == 0) continue; // any data? if (ds2 == ds1) di2 = di + 1; // only do lower rt diagonal if one dataset else di2 = 0; // must do all compares, check for dups later for ( /* start at di2 */; di2 < ds2.Count; di2++) { // if (ds2[di2].Nearest == 0) continue; // any data? if (ds1[di].Cid == ds2[di2].Cid) continue; // avoid self compare double sim = // similarity CalculateSimilarity(ds1[di], ds2[di2]); //if (sim==1.0 && !stereo) // eliminate stereo pairs if requested // continue; // a more careful check may be needed if (sim < SMP.MinimumSimilarity) continue; // below cutoff value? double denom = 1 - sim; // denominator is 1 - sim if (denom == 0) denom = .00000000001f; // avoid divide by zero double actChange = 0; if (smallerIsbetter && ds1[di].Activity < ds2[di2].Activity) { cd1 = ds1[di]; cd2 = ds2[di2]; } else { cd1 = ds2[di2]; cd2 = ds1[di]; } double a1 = cd1.Activity; double a2 = cd2.Activity; if (a1 == NullValue.NullNumber || a2 == NullValue.NullNumber) actChange = 0; else switch (SMP.ActDiffCalcType) { case ActDiffCalcType.SimpleDifference: // activity difference { actChange = a1 - a2; break; } case ActDiffCalcType.NegativeLog: { actChange = -Math.Log10(a1) - -Math.Log10(a2); break; } case ActDiffCalcType.MolarNegativeLog: { actChange = (-Math.Log10(a1 * molFactor)) - (-Math.Log10(a2 * molFactor)); break; } case ActDiffCalcType.Ratio: // activity ratio { r1 = a1; if (r1 == 0) r1 = .00000000001f; r2 = a2; if (r2 == 0) r2 = .00000000001f; r3 = r1 / r2; r4 = r2 / r1; actChange = r3; if (SMP.UseAbsoluteValue && r4 > r3) // take the max value actChange = r4; break; } case ActDiffCalcType.Advanced: { throw new InvalidOperationException("SarMapCalcType.Advanced"); } default: throw new InvalidOperationException("SarMapCalcType: " + (int)SMP.ActDiffCalcType); } if (SMP.UseAbsoluteValue && SMP.ActDiffCalcType != ActDiffCalcType.Ratio) actChange = Math.Abs(actChange); double coef = actChange / denom; if (pd.Count < SMP.MaxPairCount) // just add this pair to end { pdi = pd.Count; pd.Add(new PairData()); } else { // see if this value is greater than anything in list if (minCoef < 0) { // find element with minimum coef minCoef = 0; for (i3 = 1; i3 < pd.Count; i3++) { if (pd[i3].Coef < pd[minCoef].Coef) minCoef = i3; } } if (coef <= pd[minCoef].Coef) continue; // if this one better? //if (ds1 != ds2) //{ // be sure not a duplicate of what we have in list // for (i3 = 0; i3 < pd.Count; i3++) // { // check for pair in list already // if ((di == pd[i3].CD1 && di2 == pd[i3].CD2) || // (di == pd[i3].CD2 && di2 == pd[i3].CD1)) break; // } // if (i3 < pd.Count) continue; // continue to next pair if found //} pdi = minCoef; // replace this item minCoef = -1; // reset to get new minimum next time } // Save data for the pair pd[pdi].CD1 = cd1; pd[pdi].CD2 = cd2; pd[pdi].Sim = sim; pd[pdi].ActChange = actChange; pd[pdi].Coef = coef; } } // Build the list of pair Vos int voLen = qt.SetSimpleVoPositions(); PairData pdItem; for (pdi = 1; pdi < pd.Count; pdi++) // sort from max to min coef value { pdItem = pd[pdi]; for (pdi2 = pdi - 1; pdi2 >= 0; pdi2--) { if (pdItem.Coef < pd[pdi2].Coef) break; pd[pdi2 + 1] = pd[pdi2]; } pd[pdi2 + 1] = pdItem; } for (pdi = 0; pdi < pd.Count; pdi++) { pdItem = pd[pdi]; cd1 = pdItem.CD1; cd2 = pdItem.CD2; object[] vo = new object[voLen]; VoArray.SetVo(qt, "PAIR_ID", vo, new NumberMx(pdi + 1)); VoArray.SetVo(qt, "COMPOUND1", vo, new StringMx(cd1.Cid)); VoArray.SetVo(qt, "STRUCTURE1", vo, cd1.Structure); VoArray.SetVo(qt, "ACTIVITY1", vo, new NumberMx(cd1.Activity)); VoArray.SetVo(qt, "COMPOUND2", vo, new StringMx(cd2.Cid)); VoArray.SetVo(qt, "STRUCTURE2", vo, cd2.Structure); VoArray.SetVo(qt, "ACTIVITY2", vo, new NumberMx(cd2.Activity)); VoArray.SetVo(qt, "SIMILARITY", vo, new NumberMx(pdItem.Sim)); VoArray.SetVo(qt, "ACTIVITY_DIFF", vo, new NumberMx(pdItem.ActChange)); VoArray.SetVo(qt, "ACT_SIM_COEF", vo, new NumberMx(pdItem.Coef)); VoList.Add(vo); } VoListPos = -1; // init list position return; }