/// <summary> /// Get the ResultsField associated with a PivotGridField /// </summary> /// <param name="Rf"></param> /// <param name="f0"></param> /// <returns></returns> internal ResultsField GetPivotGridFieldResultsField( PivotGridField pgf) { Query q = Rf.Query; string[] sa = pgf.UnboundFieldName.Split('.'); if (sa.Length < 2) { return(null); } QueryTable qt = q.GetTableByName(sa[0]); if (qt == null) { return(null); } QueryColumn qc = qt.GetQueryColumnByName(sa[1]); if (qc == null) { return(null); } ResultsField rfld = Rf.GetResultsField(qc); return(rfld); }
/// <summary> /// Get any existing target summary options from option column /// </summary> /// <param name="qt"></param> /// <returns></returns> public static TargetSummaryOptions GetFromMdbAssayOptionsColumn( QueryTable qt) { TargetSummaryOptions tso = null; if (qt == null) { return(null); } QueryColumn qc = qt.GetQueryColumnByName(MultiDbAssayDataNames.MultiDbViewOptions); if (qc == null) { return(null); } ParsedSingleCriteria psc = MqlUtil.ParseSingleCriteria(qc.Criteria); if (psc == null) { return(null); } string tsoString = Lex.RemoveSingleQuotes(psc.Value); if (Lex.IsNullOrEmpty(tsoString)) { return(null); } tso = TargetSummaryOptions.Deserialize(tsoString); return(tso); }
private void RunDatabaseQuery_Click(object sender, EventArgs e) { DataRow dr; UcdbDatabase ucdb; UserObject uo; int ri; if (!GetFocusedUo(out ri, out dr, out ucdb, out uo)) { return; } if (!UserData.BuildDatabaseQuery(uo)) { return; // some error } QbUtil.RenderQuery(); QueryTable qt = QbUtil.Query.Tables[0]; // root table QueryColumn qc = qt.GetQueryColumnByName("MolFormula"); if (qc == null) { throw new Exception("MolFormula not found in root table"); } MetaColumn mc = qc.MetaColumn; qc.CriteriaDisplay = "All Data Rows"; // do all data query on molformula qc.Criteria = "(" + mc.Name + " IS NOT NULL OR " + mc.Name + " IS NULL)"; QbUtil.RenderQuery(0); Response = "Command RunQuery"; DialogResult = DialogResult.OK; }
/// <summary> /// Set vo with specified QueryColumn name /// </summary> /// <param name="qt"></param> /// <param name="colName"></param> /// <param name="vo"></param> /// <param name="voi"></param> /// <param name="value"></param> /// <param name="withException"></param> public static void SetVo( QueryTable qt, string colName, object[] vo, object value) { bool withException = false; // always false for now QueryColumn qc; if (withException) { qc = qt.GetQueryColumnByNameWithException(colName); } else { qc = qt.GetQueryColumnByName(colName); if (qc == null) { return; } } int voPos = qc.VoPosition; SetVo(vo, voPos, value); return; }
/// <summary> /// Get a simple comma separated list of allowed values for the column /// </summary> /// <param name="colname"></param> /// <returns></returns> static string GetListFromColCriteria( QueryTable qt, string colName) { string list = ""; QueryColumn qc = qt.GetQueryColumnByName(colName); if (qc == null || Lex.IsNullOrEmpty(qc.Criteria)) { return(""); } ParsedSingleCriteria psc = MqlUtil.ParseSingleCriteria(qc.Criteria); if (psc.ValueList == null) { return(""); } foreach (string s in psc.ValueList) { if (list != "") { list += ", "; } list += s; } return(list); }
/// <summary> /// Attempt to get the corresponding QueryColumn in the base, untransformed query /// </summary> /// <param name="qc"></param> /// <returns></returns> internal static QueryColumn BaseQueryQc(QueryColumn qc) { Query q = BaseQuery; if (q == null) { return(qc); } QueryTable qt = q.GetQueryTableByName(qc.QueryTable.MetaTable.Name); if (qt == null) { return(qc); } QueryColumn qc0 = qt.GetQueryColumnByName(qc.MetaColumn.Name); if (qc0 == null) { return(qc); } else { return(qc0); } }
/// <summary> /// Setup the position maps between the columns and the value object array /// </summary> /// <param name="qt"></param> public void SetupPositionMaps(QueryTable qt) // (obsolete) { int maxVoi = 0; foreach (string colName in ColNameToFieldPosition.Keys) { QueryColumn qc = qt.GetQueryColumnByName(colName); if (qc == null || !ColNameToFieldPosition.ContainsKey(colName)) { throw new Exception("Column name not found in QueryTable: " + colName); } AssayAttributePosition tafp = ColNameToFieldPosition[colName]; tafp.Voi = qc.VoPosition; if (qc.VoPosition > maxVoi) { maxVoi = qc.VoPosition; } } VoiToTarColEnum = new TarColEnum[maxVoi + 1]; // setup voi to TarColEnum map foreach (AssayAttributePosition tafp in ColNameToFieldPosition.Values) { if (tafp == null || tafp.Voi < 0) { continue; } VoiToTarColEnum[tafp.Voi] = tafp.TarColEnum; } }
/// <summary> /// Get a QueryColumn for a query from a QC reference string /// </summary> /// <param name="q"></param> /// <param name="qcRef"></param> /// <returns></returns> public static QueryColumn GetQueryColumnFromRefString( Query q, string qcRef) { QueryTable qt = null; QueryColumn qc = null; string alias, mtName, mcName; if (Lex.IsUndefined(qcRef)) { return(null); } string[] sa = qcRef.Split('.'); if (sa.Length == 3) { sa = Lex.Split(qcRef, "."); alias = sa[0]; mtName = sa[1]; mcName = sa[2]; qt = q.GetQueryTableByAlias(alias); } else if (sa.Length == 2) { mtName = sa[0]; mcName = sa[1]; } else { return(null); } if (qt == null) { qt = q.GetQueryTableByName(mtName); } if (qt == null) { return(null); } qc = qt.GetQueryColumnByName(mcName); if (qc != null) { return(qc); } else { return(null); } }
/// <summary> /// Convert a simple value list to criteria for a column /// </summary> /// <param name="colName"></param> /// <param name="list"></param> public static QueryColumn SetListCriteriaForCol( QueryTable qt, string colName, string list) { string tok; QueryColumn qc = qt.GetQueryColumnByName(colName); if (qc == null) { return(null); } if (Lex.Eq(list, "All")) { list = ""; // treat all as no criteria } List <string> values = Csv.SplitCsvString(list); string c = "", cd = ""; foreach (string s in values) { tok = Lex.RemoveAllQuotes(s); if (Lex.IsNullOrEmpty(tok)) { continue; } if (cd != "") { cd += ", "; } cd += tok; if (c != "") { c += ", "; } if (!qc.MetaColumn.IsNumeric) // quote it if not numeric { tok = Lex.AddSingleQuotes(tok); } c += tok; } if (!Lex.IsNullOrEmpty(c)) { c = qc.MetaColumn.Name + " IN (" + c + ")"; } qc.Criteria = c; qc.CriteriaDisplay = cd; return(qc); }
/// <summary> /// Setup Parameters /// </summary> void SetupParameters() { QueryColumn qc; SpotfireViewProps sl = SpotfireViewProps; Dictionary <string, SpotfireLinkParameter> p = sl.SpotfireLinkParameters; if (p.Count == 0) // if no parameters then include one for CIDLIST checked { SpotfireLinkParameter p1 = new SpotfireLinkParameter("COMPOUND_ID", "True"); p[p1.Name] = p1; } QueryTable qt = CriteriaCols.QueryTable; qt.DeselectAll(); foreach (SpotfireLinkParameter p0 in sl.SpotfireLinkParameters.Values) { qc = qt.GetQueryColumnByName(p0.Name); if (qc == null) { continue; } bool selected = false; bool.TryParse(p0.Value, out selected); qc.Selected = selected; } qc = qt.GetQueryColumnByName("visualization"); // hide visualization col here if (qc != null) { qc.MetaColumn.InitialSelection = ColumnSelectionEnum.Hidden; // hide visualization col here qc.Selected = false; } CriteriaCols.TableHeaderLabel.Visible = false; CriteriaCols.CanDeselectKeyCol = true; CriteriaCols.Render(); return; }
/// <summary> /// Do Generic display of a list of structures /// </summary> /// <param name="title"></param> /// <param name="cidLabel"></param> /// <param name="structLabel"></param> /// <param name="structures"></param> public static void DisplayStructures( string title, string cidLabel, string structLabel, List <MoleculeMx> structures, MoleculeGridPanel gridPanel, bool embedDataInQuery) { QueryManager qm = null; MetaTable mt = MetaTableCollection.GetWithException("Generic_Structure_Table"); mt.MetaBrokerType = MetaBrokerType.NoSql; Query q = ToolHelper.InitEmbeddedDataToolQuery(mt); QueryTable qt = q.Tables[0]; qt.Label = title; qt.GetQueryColumnByName("CompoundId").Label = cidLabel; qt.GetQueryColumnByName("Structure").Label = structLabel; DataTableMx dt = q.ResultsDataTable as DataTableMx; for (int mi = 0; mi < structures.Count; mi++) { MoleculeMx cs = structures[mi]; DataRowMx dr = dt.NewRow(); string cid = cs.Id; if (Lex.IsUndefined(cid)) { cid = (mi + 1).ToString(); } dr[qt.Alias + ".CompoundId"] = new CompoundId(cid); dr[qt.Alias + ".Structure"] = cs; dt.Rows.Add(dr); } DisplayData(q, dt, gridPanel, embedDataInQuery); return; }
/// <summary> /// Set querycolumn selection /// </summary> /// <param name="qt"></param> /// <param name="colName"></param> /// <param name="selected"></param> public static void SetSelected( QueryTable qt, string colName, bool selected) { QueryColumn qc = qt.GetQueryColumnByName(colName); if (qc != null) { qc.Selected = selected; } return; }
/// <summary> /// Reset querycolumn selection & width for rgroups R2 - Rn to match R1 /// </summary> /// <param name="qt"></param> /// <param name="name"></param> void SetRnToMatchR1( QueryTable qt, string nameSuffix) { QueryColumn qc1 = qt.GetQueryColumnByName("R1_" + nameSuffix); for (int ri = 1; ri <= 32; ri++) { if (!RgCounts.ContainsKey(ri)) { continue; // ignore if not defined in core } if (ri > 8) { throw new UserQueryException("R" + ri.ToString() + " exceeds the limit of R8"); } QueryColumn qc = qt.GetQueryColumnByName("R" + ri.ToString() + "_" + nameSuffix); if (ri == 1) // fix up label for R1 { if (qc.Label == "") // replace generic R-group label with specific R1 { qc.Label = qc.MetaColumn.Label.Replace("R-group", "R1"); } else { qc.Label = qc.Label.Replace("R-group", "R1"); } } else { qc.Selected = qc1.Selected; qc.DisplayWidth = qc1.DisplayWidth; } } }
/// <summary> /// SetupActivityClassCondFormat /// </summary> /// <param name="tableName"></param> void SetupActivityClassCondFormat(string tableName) { QueryTable qt = BaseQuery.GetQueryTableByName(tableName); if (qt == null) { return; } QueryColumn qc = qt.GetQueryColumnByName("ACTIVITY_CLASS"); if (qc != null && qc.Selected) { qc.CondFormat = UnpivotedAssayResult.BuildActivityClassCondFormat(); } qc = qt.GetQueryColumnByName("ACTIVITY_BIN"); if (qc != null && qc.Selected) { qc.CondFormat = UnpivotedAssayResult.BuildActivityClassCondFormat(); } return; }
/// <summary> /// Return the vo position for a named QueryColumn /// </summary> /// <param name="qt"></param> /// <param name="colName"></param> /// <returns></returns> public int GetQcVoi( QueryTable qt, string colName) { QueryColumn qc = qt.GetQueryColumnByName(colName); if (qc == null) { return(NullValue.NullNumber); } else { return(qc.VoPosition); } }
/// <summary> /// If the user selected a field from a non-summarized table, give them the option to replace it with the field from the /// summarized table (if available). Performance is better. This will also avoid extra rows as well as a cartesian product. /// </summary> private void CheckForSummarizedVersionOfMetaColumn() { return; #if false // disabled for now if (SelectedColumn == null || SelectedColumn.QueryColumn == null) { return; } QueryColumn qc = SelectedColumn.QueryColumn; MetaColumn mc = qc.MetaColumn; MetaTable mt = mc.MetaTable; QueryTable qt = new QueryTable(mt); bool unsummarrizedVersionSelected = (!mt.UseSummarizedData && mt.SummarizedExists); if (!unsummarrizedVersionSelected) { return; } QueryTable summarizedQt = qt.AdjustSummarizationLevel(useSummarized: true); QueryColumn summarizedQc = summarizedQt.GetQueryColumnByName(qc.ActiveLabel); if (summarizedQc == null) { return; } string msg = "You have selected the un-summarized version of " + qc.ActiveLabel + ".\n" + "Using the summarized version will result in better performance and avoid unwanted extra rows.\n" + "It is recommended that the summarized version be used for calculated fields.\n\n" + "Would you like to use the summarized version instead?\n"; DialogResult dialogResult = MessageBoxMx.Show(msg, "Summarized Data Available!", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.Yes) { SelectedColumn = null //summarizedQc.MetaColumn; return; } #endif }
private void TextOrStructBox_MouseDown(Control sender, MouseEventArgs e, string tag) { if (tag == null) { return; } if (Query.LogicType == QueryLogicType.Complex) { MessageBoxMx.ShowError( "Since Advanced Criteria Logic has been selected\n" + "for this query you must use the advanced criteria\n" + "text box on the Criteria Summary tab to edit criteria."); return; } string[] sa = tag.Split('.'); if (sa.Length < 2) { return; } QueryTable qt = Query.GetTableByName(sa[0]); if (qt == null) { return; } QueryColumn qc = qt.GetQueryColumnByName(sa[1]); if (qc == null) { return; } CurrentCriteriaControl = sender; CurrentQc = qc; if (e.Button == MouseButtons.Left) { EditCriteriaMenuItem_Click(sender, e); } }
void SetupColumn(int ci, FieldSelectorControl fs, CheckEdit ascending, CheckEdit descending) { fs.Query = Query; if (ci >= Columns.Count) { fs.MetaColumn = null; ascending.Checked = true; return; } QueryColumn qc = Columns[ci].QueryColumn; if (qc != null) { // be sure any existing cols are still in query QueryTable qt2 = Query.GetTableByName(qc.QueryTable.MetaTable.Name); if (qt2 != null) { QueryColumn qc2 = qt2.GetQueryColumnByName(qc.MetaColumn.Name); if (qc2 == qc) { fs.MetaColumn = qc.MetaColumn; if (Columns[ci].Direction == SortOrder.Ascending) { ascending.Checked = true; } else { descending.Checked = true; } return; } } } fs.MetaColumn = null; ascending.Checked = true; }
/// <summary> /// Convert a multipivot table into a set of tables where data exists for /// one or more of the compound identifiers in the list. /// </summary> /// <param name="qt">Current form of query table</param> /// <param name="q">Query to add transformed tables to</param> /// <param name="ResultKeys">Keys data will be retrieved for</param> public override void ExpandToMultipleTables( QueryTable qt, Query q, List <string> resultKeys) { MetaTable mt2; QueryTable qt2; QueryColumn qc2; HashSet <string> geneDict = new HashSet <string>(); string sql; string geneSymbol; int t0 = TimeOfDay.Milliseconds(); // Build query & get set of gene symbols Query q2 = new Query(); q2.SingleStepExecution = true; q2.KeyCriteria = q2.KeyCriteriaDisplay = // keylist to return (may want to include all target summary option criteria) " in (" + MqlUtil.FormatValueListString(resultKeys, true) + ")"; qt2 = qt.Clone(); qt2.SelectKeyOnly(); qc2 = qt2.GetQueryColumnByNameWithException("gene_symbol"); qc2.Selected = true; q2.AddQueryTable(qt2); QueryEngine qe2 = new QueryEngine(); qe2.ExecuteQuery(q2); while (true) { object[] vo = qe2.NextRow(); if (vo == null) { qe2.Close(); break; } if (qe2.Cancelled) { qe2.Close(); return; } geneSymbol = vo[2] as string; if (Lex.IsNullOrEmpty(geneSymbol)) { continue; } geneDict.Add(geneSymbol.ToUpper()); } string[] sa = new string[geneDict.Count]; geneDict.CopyTo(sa); Array.Sort(sa); foreach (string s0 in sa) { geneSymbol = s0; string mt2Name = MultiDbAssayDataNames.BasePivotTablePrefix + geneSymbol; //if (QueryEngine.FilterAllDataQueriesByDatabaseContents && // !MetaTableCollection.IsMetaTableInContents(mtName)) continue; // metatable must be in contents mt2 = MetaTableCollection.Get(mt2Name); if (mt2 == null) { continue; // in case can't allocate for some reason } qt2 = q.GetQueryTableByName(mt2.Name); // see if already in query if (qt2 != null) { continue; // ignore if already there } qt2 = new QueryTable(q, mt2); // allocate new query table & add to query qt2.DeselectAll(); foreach (QueryColumn qc0 in qt.QueryColumns) // set selected columns to match those in the source qt { if (qc0.Selected && qt2.GetQueryColumnByName(qc0.MetaColumn.Name) != null) { qt2.GetQueryColumnByName(qc0.MetaColumn.Name).Selected = true; } } if (qt.HeaderBackgroundColor != Color.Empty) { qt2.HeaderBackgroundColor = qt.HeaderBackgroundColor; } } t0 = TimeOfDay.Milliseconds() - t0; return; }
/// <summary> /// Do setup in preparation for decompose /// </summary> /// <param name="qt"></param> /// <param name="q"></param> public void PrepareForDecompose( QueryTable qt, Query q) { throw new NotImplementedException(); #if false Stopwatch sw = Stopwatch.StartNew(); MetaTable mt = qt.MetaTable; QueryColumn qc = qt.GetQueryColumnByName("core"); if (qc == null) { throw new UserQueryException("Core column not defined in rgroup_decomposition table"); } string molfile = qc.MolString; if (molfile == null || molfile == "") { // core not defined in Rgroup decomposition table, try to get from structure search in rest of query foreach (QueryTable qt3 in q.Tables) { if (!qt3.MetaTable.IsRootTable) { continue; } MetaColumn mc3 = qt3.MetaTable.FirstStructureMetaColumn; if (mc3 != null) { QueryColumn qc3 = qt3.GetQueryColumnByName(mc3.Name); molfile = qc3.MolString; break; } } } if (molfile == null || molfile == "") { throw new UserQueryException("R-group decomposition core structure is not defined"); } bool allowHydrogenSubstituents = true; qc = qt.GetQueryColumnByName("AllowHydrogenSubstituents"); if (Lex.Contains(qc?.Criteria, "'N'")) { allowHydrogenSubstituents = false; // set to false if "no" criteria specified } TerminateOptionString = "First mapping"; bool allowMultipleCoreMappings = false; qc = qt.GetQueryColumnByName("Terminate_Option"); if (qc != null && qc.Criteria != "") { ParsedSingleCriteria psc = MqlUtil.ParseSingleCriteria(qc.Criteria); TerminateOptionString = psc.Value; allowMultipleCoreMappings = Lex.Contains(TerminateOptionString, "All"); } MoleculeMx cs = new MoleculeMx(MoleculeFormat.Molfile, molfile); cs.GetCoreRGroupInfo(out RgCounts, out RgTotalCount); // need to redefine these RGroupDecomp.Initialize(); RGroupDecomp.SetAlignStructuresToCore(true); RGroupDecomp.SetAllowMultipleCoreMappings(allowMultipleCoreMappings); RGroupDecomp.SetIncludeHydrogenFragments(allowHydrogenSubstituents); if (DebugMx.True) // debug { String coreSmiles = "[R1]c1cn2c3c(c1= O)cc(c(c3SC(C2)[R4])[R3])[R2]"; string coreChime = "CYAAFQwAUewQeV58YHemfM^EQqPRfIYlJGEkx6M7e4db95jjK5HrNFVP2e1qHphWPL98KvcvrsF7bP9bRcW4QH$si9PXkkuwre6Q5UkHZjciQqeAKVLSHNAeQTAMlkiXEBD$BePpbNQCPD3BkklFEaQqwokeI$FwUoS5cAixQXkMbLTWDaAK7t7cOkSmt3RhwQedbrba6OHKBq3OF4Dhlz$0BfLeKCUUo8ixle176M2aIzXUccTOU8Xy8ARwE3bTyMfjuI3UunZceJf4iZELvsj3PX2MHZG73baUvQGS4DaxUaBGps81PPiDljfvxwFv8OfdOyIRlOBeuEAvk2rT9SRT6oMZIV6UtLFvmCHdwKnu9WtrfV1rEctydNUVxW4qKVlV0rZtpK1oZXuKcv6WVdl6r2hrjVLxBhblTVKO7w1qGRoziquOnPQkKd9H4EQfV0rP6mzI8Au8ulti2fu3YKB94lPXftPGbwr5yA"; IMolLib coreMol = MolLibFactory.NewMolLib(MoleculeFormat.Molfile, molfile); coreChime = MoleculeMx.MolfileStringToChimeString(molfile); molfile = coreMol.MolfileString; } CoreMolecule = CdkMol.Util.MolfileStringToMolecule(molfile); RGroupDecomp.SetCoreStructure(CoreMolecule, false); CoreChemicalStructure = new MoleculeMx(MoleculeFormat.Molfile, molfile); StrMatcher = null; int msTime = (int)sw.ElapsedMilliseconds; if (RGroupDecomp.Debug) { DebugLog.Message("Time(ms): " + msTime); } return; #endif }
/// <summary> /// SetupQueryTableForRGroupDecomposition /// </summary> /// <param name="qt"></param> /// <param name="q"></param> public void SetupQueryTableForRGroupDecomposition( QueryTable qt, Query q) { Stopwatch sw = Stopwatch.StartNew(); MetaTable mt = qt.MetaTable; QueryColumn qc = qt.GetQueryColumnByName("core"); if (qc == null) { throw new UserQueryException("Core column not defined in rgroup_decomposition table"); } string molfile = qc.MolString; if (molfile == null || molfile == "") { // core not defined in Rgroup decomposition table, try to get from structure search in rest of query foreach (QueryTable qt3 in q.Tables) { if (!qt3.MetaTable.IsRootTable) { continue; } MetaColumn mc3 = qt3.MetaTable.FirstStructureMetaColumn; if (mc3 != null) { QueryColumn qc3 = qt3.GetQueryColumnByName(mc3.Name); molfile = qc3.MolString; break; } } } if (molfile == null || molfile == "") { throw new UserQueryException("R-group decomposition core structure is not defined"); } MoleculeMx cs = new MoleculeMx(MoleculeFormat.Molfile, molfile); cs.GetCoreRGroupInfo(out RgCounts, out RgTotalCount); if (RgCounts.Count == 0 || (RgCounts.ContainsKey(0) && RgCounts.Count == 1)) { throw new UserQueryException("R-group decomposition core structure must contain at least one numbered R-group"); } qc = qt.GetQueryColumnByName("R1_Structure"); // update any core structure label if (qc == null) { throw new UserQueryException("Can't find R1_Structure in " + mt.Label); } if (qc.Label.IndexOf("\tChime=") > 0) { qc.Label = "R-group, Core\tChime=" + cs.GetChimeString(); // reference core in query col header label qc.MetaColumn.Width = 25; } SetRnToMatchR1(qt, "Structure"); // reset querycolumn selection & width to match R1 SetRnToMatchR1(qt, "Smiles"); SetRnToMatchR1(qt, "Formula"); SetRnToMatchR1(qt, "Weight"); SetRnToMatchR1(qt, "SubstNo"); int msTime = (int)sw.ElapsedMilliseconds; //if (RGroupDecomp.Debug) DebugLog.Message("Time(ms): " + msTime); return; }
/// <summary> /// Add criteria to query to do a related structure search /// </summary> /// <param name="q"></param> void ModifyQueryForRelatedStructureSearch( Query q, MoleculeControl queryMolCtl) { q.KeyCriteria = q.KeyCriteriaDisplay = ""; // always remove any existing key criteria QueryTable sqt = AddStructureTableToQuery(q); // be sure we have a structure table in the query QueryColumn sqc = sqt.FirstStructureQueryColumn; sqc.Selected = true; sqc.DisplayFormatString = "Highlight=true"; if (AlignMatches.Checked) { sqc.DisplayFormatString += ";Align=true"; } QueryColumn qc = sqt.GetQueryColumnByName("molSrchType"); // include search type if (qc != null) { qc.Selected = true; } qc = sqt.GetSimilarityScoreQueryColumn(); // and match score if (qc != null) { qc.Selected = true; } ParsedStructureCriteria pssc = new ParsedStructureCriteria(); pssc.SearchType = StructureSearchType.Related; //string mfText = sqt.KeyQueryColumn.ActiveLabel + ": " + queryCid; // (queryCid no longer be accurate) //pssc.Structure = // just store a comment with the CID in the structure // new ChemicalStructureMx(StructureFormat.MolFile, ChemicalStructureMx.GetTextMessageMolFile(mfText)); MoleculeMx m = QueryMolCtl.Molecule; pssc.Molecule = new MoleculeMx(m.PrimaryFormat, m.PrimaryValue); if (AltForms.Checked) { pssc.SearchTypeUnion |= StructureSearchType.FullStructure; } if (MatchedPairs.Checked) { pssc.SearchTypeUnion |= StructureSearchType.MatchedPairs; } if (SmallWorld.Checked) { pssc.SearchTypeUnion |= StructureSearchType.SmallWorld; } if (SimilarSearch.Checked) { pssc.SearchTypeUnion |= StructureSearchType.MolSim; } if (Substructure.Checked) { pssc.SearchTypeUnion |= StructureSearchType.Substructure; } pssc.MinimumSimilarity = .75; pssc.MaxSimHits = 100; pssc.Highlight = true; pssc.Align = AlignMatches.Checked; pssc.ConvertToQueryColumnCriteria(sqc); if (ExcludeCurrentResultsCids.Checked && CurrentBaseQueryCidHitList != null) // add not in list criteria { string queryStrName = queryMolCtl.GetTemporaryStructureTag(); // see if we can get the cid q.KeysToExclude = new HashSet <string>(CurrentBaseQueryCidHitList); if (q.KeysToExclude.Contains(queryStrName)) // keep query id { q.KeysToExclude.Remove(queryStrName); } } else { q.KeyCriteria = q.KeyCriteriaDisplay = ""; // no key criteria } return; }
/// <summary> /// Search button clicked, process input /// </summary> /// <returns></returns> DialogResult ProcessInput() { CidList cidList = null; StreamReader structureFileReader = null; string qid; // query identifier, compoundId, file name or sdFile key value QueryManager qm; DataTableMx dt; DataColumn dc; DataRowMx dr; //object[] dr; // if using Qe DialogResult dlgRslt = DialogResult.OK; Query q = null; QueryTable qt = null; QueryColumn simScoreQc, structQc; // query column containing latest query settings MetaTable mt = null; MetaColumn keyMc = null, structMc = null, dbSetMc = null, simScoreMc = null, mc; MetaColumnType storageType; string txt, tok; if (DatabasesToSearch.Text == "") { MessageBoxMx.ShowError("Databases to search must be defined."); DatabasesToSearch.Focus(); return(DialogResult.Cancel); } // Get list of databases string[] dba = DatabasesToSearch.Text.Split(','); List <MetaTable> tables = new List <MetaTable>(); foreach (string dbLabel0 in dba) { string dbLabel = dbLabel0.Trim(); RootTable dbInfo = RootTable.GetFromTableLabel(dbLabel); if (dbInfo == null) { MessageBoxMx.ShowError("Can't find database " + DatabasesToSearch.Text); DatabasesToSearch.Focus(); return(DialogResult.Cancel); } mt = MetaTableCollection.Get(dbInfo.MetaTableName); if (mt == null) { MessageBoxMx.ShowError("Unable to locate parent structure table for database: " + DatabasesToSearch.Text); DatabasesToSearch.Focus(); return(DialogResult.Cancel); } if (dbSetMc == null) { dbSetMc = mt.DatabaseListMetaColumn; } tables.Add(mt); } if (dbSetMc == null) { throw new Exception("\"Databases\" metacolumn not found for any of the databases to search"); } // Validate other form values RetrieveStructures = RetrieveMatchingStructures.Checked; bool fromList = FromList.Checked; int listCidsRead = 0; int inputQueryCount = -1; if (fromList) // using list, validate list name { if (SavedListUo == null) { MessageBoxMx.ShowError("Compound list must be defined."); ListName.Focus(); return(DialogResult.Cancel); } cidList = CidListCommand.Read(SavedListUo); if (cidList == null) { MessageBoxMx.ShowError("Error reading list."); ListName.Focus(); return(DialogResult.Cancel); } inputQueryCount = cidList.Count; } else // Using SdFile, validate SdFile name { StructureFile = FileName.Text; if (StructureFile == "") { MessageBoxMx.ShowError("File must be defined."); FileName.Focus(); return(DialogResult.Cancel); } try { structureFileReader = new StreamReader(StructureFile); structureFileReader.Close(); } catch (Exception ex) { MessageBoxMx.ShowError("Can't read file: " + Lex.Dq(StructureFile)); FileName.Focus(); return(DialogResult.Cancel); } keyField = KeyField.Text; // get key, blank to use name in 1st line inputQueryCount = -1; // don't know how many queries unless we read the file (todo?) } tok = ResultsName.Text.Trim(); // name to store results under if (tok == "") { MessageBoxMx.ShowError("A name for the results must be provided."); ResultsName.Focus(); return(DialogResult.Cancel); } if (SubStruct.Checked) { Psc.SearchType = StructureSearchType.Substructure; } else if (Full.Checked) { Psc.SearchType = StructureSearchType.FullStructure; } else if (Similarity.Checked) { Psc.SearchType = StructureSearchType.MolSim; } else { throw new Exception("Unrecognized search type"); } // Write initial log entries SearchCount++; string logFileName = ClientDirs.DefaultMobiusUserDocumentsFolder + @"\Multistructure Search " + SearchCount + ".txt"; if (!UIMisc.CanWriteFileToDefaultDir(logFileName)) { return(DialogResult.Cancel); } LogStream = new StreamWriter(logFileName); if (ResultsUo == null) { ResultsUo = new UserObject(UserObjectType.Annotation); } ResultsUo.Name = tok; UserObjectTree.GetValidUserObjectTypeFolder(ResultsUo); DateTime startTime = DateTime.Now; WriteToLog("Multiple " + Psc.SearchType + " Search"); WriteToLog("Databases: " + DatabasesToSearch.Text); WriteToLog("Date: " + startTime); if (fromList) { WriteToLog("Input List: " + SavedListUo.Name); } else { WriteToLog("Input Structure File: " + StructureFile); } WriteToLog("Output List: " + ResultsUo.Name); WriteToLog("Log File: " + logFileName); WriteToLog(""); WriteToLog("Query, Match, Score"); int queryCount = 0; int matchAtLeastOneCount = 0; MoleculeMx queryStructure = null; // current structure being searched CidList matchList = new CidList(); List <MatchData> matchData = new List <MatchData>(); if (FromFile.Checked) // open SdFile as required { structureFileReader = new StreamReader(StructureFile); } // Search of structures one at a time while (true) { if (fromList) // get next structure from list { if (listCidsRead >= cidList.Count) { break; } qid = cidList[listCidsRead].Cid; listCidsRead++; if (qid.Trim() == "") { continue; } if (qid.ToLower().IndexOf(".mol") > 0 || qid.ToLower().IndexOf(".skc") > 0) { // file reference if (!File.Exists(qid)) { continue; } if (qid.ToLower().IndexOf(".mol") > 0) { queryStructure = MoleculeMx.ReadMolfile(qid); } else { queryStructure = MoleculeMx.ReadSketchFile(qid); } } else { queryStructure = MoleculeUtil.SelectMoleculeForCid(qid); } if (queryStructure == null || queryStructure.AtomCount == 0) { continue; // oops } } else // get next structure from input file { qid = null; if (StructureFile.ToLower().EndsWith(".sdf")) { List <SdFileField> fList = SdFileDao.Read(structureFileReader); if (fList == null) // end of sdFile { structureFileReader.Close(); break; } if (fList.Count == 0) { continue; } queryStructure = new MoleculeMx(MoleculeFormat.Molfile, fList[0].Data); if (queryStructure == null || queryStructure.AtomCount == 0) { continue; } if (keyField != "") // key field specified? { qid = SdFileDao.GetDataField(fList, keyField); } else // get name from 1st line of molfile { string molFile = fList[0].Data; int i1 = molFile.IndexOf("\n"); if (i1 == 0) { qid = ""; } else { qid = molFile.Substring(0, i1).Trim(); } } if (string.IsNullOrEmpty(qid)) { qid = SdFileDao.GetDataField(fList, "compound_id"); } } else // assume smiles file { string smiles = structureFileReader.ReadLine(); if (smiles == null) // end of sdFile { structureFileReader.Close(); break; } smiles = smiles.Trim(); if (smiles.Length == 0) { continue; } int i1 = smiles.IndexOf(","); // get any preceeding queryId if (i1 < 0) { i1 = smiles.IndexOf("\t"); } if (i1 >= 0) { qid = smiles.Substring(0, i1).Trim(); smiles = smiles.Substring(i1 + 1).Trim(); } queryStructure = new MoleculeMx(MoleculeFormat.Smiles, smiles); if (queryStructure == null || queryStructure.AtomCount == 0) { continue; } } if (qid == null || qid.Trim() == "") { qid = (queryCount + 1).ToString(); // be sure we have a query id } } queryCount++; // count the query if (queryStructure == null || queryStructure.AtomCount == 0) { WriteToLog("Error converting specific structure " + queryCount.ToString() + ", " + qid); continue; } queryStructure.RemoveStructureCaption(); // remove any Mobius-added caption Psc.Molecule = queryStructure; string msg = "Searching Structure: " + queryCount.ToString(); if (inputQueryCount > 0) { msg += " of " + inputQueryCount.ToString(); } msg += "\n" + "Structures with one or more matches: " + matchAtLeastOneCount.ToString() + "\n" + "Total Matches: " + matchList.Count.ToString(); Progress.Show(msg); // Do the search over the list of databases for (int ti = 0; ti < tables.Count; ti++) { mt = tables[ti]; q = new Query(); // build basic query //q.SingleStepExecution = true; // do in single step (doesn't currently return sim score) q.ShowStereoComments = false; qt = new QueryTable(mt); q.AddQueryTable(qt); qt.SelectKeyOnly(); // start selecting desired cols keyMc = mt.KeyMetaColumn; structMc = mt.FirstStructureMetaColumn; structQc = qt.GetQueryColumnByName(structMc.Name); structQc.Selected = RetrieveStructures; dbSetMc = mt.DatabaseListMetaColumn; if (dbSetMc == null) { throw new Exception("\"Databases\" metacolumn not found for table: " + mt.Label); } QueryColumn dbSetQc = qt.GetQueryColumnByName(dbSetMc.Name); dbSetQc.Selected = true; // select the database name RootTable root = RootTable.GetFromTableName(mt.Name); txt = " in (" + root.Label + ")"; dbSetQc.Criteria = dbSetMc.Name + txt; dbSetQc.CriteriaDisplay = txt; simScoreMc = mt.SimilarityScoreMetaColumn; simScoreQc = null; if (simScoreMc != null) // get sim score if it exists { simScoreQc = qt.GetQueryColumnByName(simScoreMc.Name); simScoreQc.Selected = true; // return sim score } Psc.QueryColumn = structQc; ParsedStructureCriteria psc2 = AdjustSearchForSmallWorldAsNeeded(Psc); psc2.ConvertToQueryColumnCriteria(structQc); // format the QC for the structure search DateTime t0 = DateTime.Now; //QueryEngine qe = new QueryEngine(); //qe.NextRowsMin = 1000; // minimum number of rows to prefetch //qe.NextRowsMax = -1; // maximum number of rows to prefetch //qe.NextRowsMaxTime = 10000; // max time in milliseconds for next fetch //qe.ExecuteQuery(q); qm = new QueryManager(); try { dlgRslt = qm.ExecuteQuery(ref q); } catch (Exception ex) { WriteToLog("Error searching structure: " + ex.Message + ", " + queryCount.ToString() + ", " + qid); continue; } if (dlgRslt != DialogResult.OK) { return(dlgRslt); } double executeTime = TimeOfDay.Delta(ref t0); int offset = qm.DataTableManager.KeyValueVoPos + 1; //int offset = 0; // for QE int keyPos = offset++; int strPos = RetrieveStructures ? offset++ : -1; int dbPos = offset++; int simPos = offset++; int fetchCnt = 0; while (true) { dr = qm.NextRow(); //dr = qe.NextRow(); // for Qe if (dr == null) { break; } fetchCnt++; if (fetchCnt == 1) { matchAtLeastOneCount++; // number of queries that have at least one match } MatchData md = new MatchData(); md.Qno = queryCount; md.Qid = qid; if (RetrieveStructures) { md.Qstr = "Chime=" + queryStructure.GetChimeString(); } CompoundId cid = CompoundId.ConvertTo(dr[keyPos]); md.Mid = cid.Value; if (RetrieveStructures) { MoleculeMx ms = MoleculeMx.ConvertTo(dr[strPos]); if (!NullValue.IsNull(ms)) { md.Mstr = "Chime=" + ms.GetChimeString(); } } StringMx db = StringMx.ConvertTo(dr[dbPos]); if (!NullValue.IsNull(db)) { md.Db = db.Value; } if (Psc.SearchType == StructureSearchType.MolSim) { NumberMx nex = NumberMx.ConvertTo(dr[simPos]); if (!NullValue.IsNull(nex)) { md.Score = nex.Value; } } if (matchList.Contains(cid.Value)) // already have compound id as match for other query? { if (Psc.SearchType != StructureSearchType.MolSim) { continue; // if similarity search see if more similar } CidListElement le = matchList.Get(cid.Value); // reference current score if (le.Tag > md.Score) { continue; // only replace if more similar } matchList.Remove(le.Cid); // remove from list for (int mi = 0; mi < matchData.Count; mi++) // remove from data { if (matchData[mi].Mid == md.Mid) { matchData.RemoveAt(mi); break; } } } matchList.Add(md.Mid); matchList.Get(md.Mid).Tag = md.Score; // keep score in list matchData.Add(md); // add to results txt = md.Qid + ", " + md.Mid + ", " + md.Score.ToString(); WriteToLog(txt); } // Fetch result loop double fetchTime = TimeOfDay.Delta(ref t0); } // DB loop if (Progress.CancelRequested) { Progress.Hide(); MessageBoxMx.ShowError("Search cancelled."); try { LogStream.Close(); } catch { } return(DialogResult.Cancel); } } // key loop CidListCommand.WriteCurrentList(matchList); // write the list of numbers UsageDao.LogEvent("MultipleStructSearch"); txt = "=== Multiple structure search complete ===\r\n\r\n" + "Structures Searched: " + queryCount.ToString() + "\r\n"; txt += "Structures with one or more matches: " + matchAtLeastOneCount.ToString() + "\r\n" + "Total Matches: " + matchList.Count.ToString() + "\r\n"; TimeSpan ts = DateTime.Now.Subtract(startTime); ts = new TimeSpan(ts.Hours, ts.Minutes, ts.Seconds); txt += "Total Time: " + ts + "\r\n\r\n"; WriteToLog("\r\n" + txt); try { LogStream.Close(); } catch { } if (matchList.Count == 0) { Progress.Hide(); MessageBoxMx.ShowError("No matches have been found."); return(DialogResult.Cancel); } tok = "Matching compound ids"; if (Psc.SearchType == StructureSearchType.MolSim) { tok = "Similar compound ids"; } txt += tok + " have been written to the current list: " + ResultsUo.Name + "\n" + "Log file written to: " + logFileName + "\n\n" + "Do you want to view the match results?"; DialogResult dRslt = MessageBoxMx.Show(txt, "Multiple Structure Search", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (dRslt == DialogResult.Cancel) { return(DialogResult.Cancel); } else if (dRslt == DialogResult.No) // show log { SystemUtil.StartProcess(logFileName); return(DialogResult.Cancel); } // Display results Progress.Show("Formatting results..."); mt = new MetaTable(); mt.Name = "MULTSTRUCTSEARCH_" + SearchCount; mt.Label = "Multiple Structure Search " + SearchCount; mt.TableMap = "Mobius.Tools.MultStructSearch"; // plugin id mt.MetaBrokerType = MetaBrokerType.NoSql; ColumnSelectionEnum structureColumnSelection = RetrieveStructures ? ColumnSelectionEnum.Selected : ColumnSelectionEnum.Unselected; keyMc = keyMc.Clone(); keyMc.Name = "MatchingCid"; keyMc.Label = "Matching Compound Id"; mt.AddMetaColumn(keyMc); structMc = structMc.Clone(); structMc.Name = "MatchingStructure"; structMc.Label = "Matching Structure"; structMc.InitialSelection = structureColumnSelection; mt.AddMetaColumn(structMc); dbSetMc = dbSetMc.Clone(); dbSetMc.Name = "Database"; mt.AddMetaColumn(dbSetMc); //if (DatabasesToSearch.Text.Contains(",")) dbSetMc.InitialSelection = ColumnSelectionEnum.Selected; mc = mt.AddMetaColumn("Molsimilarity", "Similarity Search Score", MetaColumnType.Number, ColumnSelectionEnum.Unselected, 10); if (Psc.SearchType == StructureSearchType.MolSim) { mc.InitialSelection = ColumnSelectionEnum.Selected; } mc = mt.AddMetaColumn("QueryNo", "Query Number", MetaColumnType.Integer); //mc = mt.AddMetaColumn("QueryMatchNo", "Query Match Number", MetaColumnType.Integer); mc = mt.AddMetaColumn("QueryId", "Query Id", MetaColumnType.String); mc = mt.AddMetaColumn("QueryStructure", "Query Structure", MetaColumnType.Structure); mc.InitialSelection = structureColumnSelection; q = ToolHelper.InitEmbeddedDataToolQuery(mt); dt = q.ResultsDataTable as DataTableMx; for (int mi = 0; mi < matchData.Count; mi++) { MatchData md = matchData[mi]; dr = dt.NewRow(); dr[qt.Alias + ".MatchingCid"] = new CompoundId(md.Mid); if (RetrieveStructures) { dr[qt.Alias + ".MatchingStructure"] = new MoleculeMx(MoleculeFormat.Chime, md.Mstr); } dr[qt.Alias + ".Database"] = new StringMx(md.Db); if (Psc.SearchType == StructureSearchType.MolSim) { dr[qt.Alias + ".Molsimilarity"] = new NumberMx(md.Score); } dr[qt.Alias + ".QueryNo"] = new NumberMx(md.Qno); dr[qt.Alias + ".QueryId"] = new StringMx(md.Qid); if (RetrieveStructures) { dr[qt.Alias + ".QueryStructure"] = new MoleculeMx(MoleculeFormat.Chime, md.Qstr); } dt.Rows.Add(dr); } ToolHelper.DisplayData(q, dt, true); Progress.Hide(); return(DialogResult.OK); }
/// <summary> /// Read the original query associated with this multitable metatable /// and substitute in the query along with any criteria, selections /// for the common fields. /// </summary> /// <param name="qt"></param> /// <param name="q"></param> /// <param name="resultKeys"></param> public override void DoPreSearchTransformation( Query originalQuery, QueryTable qt, Query newQuery) { MetaTable mt2; UserObject uo; int objectId; string name = qt.MetaTable.Name; if (Lex.Eq(name, MetaTable.AllDataQueryTable)) // special case transform for QuickSearch all data query { QueryEngine.TransformSelectAllDataQuery(originalQuery, qt, newQuery); return; } string prefix = "multitable_"; // multitable metatable names begin with "multitable_" if (name.ToLower().IndexOf(prefix) != 0) { return; } string tok = name.Substring(prefix.Length); // get the id of the associated query try { objectId = Int32.Parse(tok); } catch (Exception ex) { return; } uo = UserObjectDao.Read(objectId); // read query if (uo == null) { return; } Query q2 = Query.Deserialize(uo.Content); foreach (QueryTable qt2 in q2.Tables) { mt2 = qt2.MetaTable; if (mt2.Parent == null) { continue; // ignore root table } QueryTable qt3 = qt2.Clone(); // make copy to modify & add to query qt3.Alias = ""; // clear alias to avoid possible conflicts with existing aliases if (qt.HeaderBackgroundColor != Color.Empty) { qt2.HeaderBackgroundColor = qt.HeaderBackgroundColor; } MetaColumn keyMc = qt3.MetaTable.KeyMetaColumn; // clear any key criteria if (keyMc != null) { QueryColumn qc3 = qt3.GetQueryColumnByName(keyMc.Name); qc3.Criteria = qc3.CriteriaDisplay = ""; } foreach (QueryColumn qc in qt.QueryColumns) { // pass any criteria, selections from multitable to underlying tables int qci3 = qt3.GetQueryColumnIndexByName(qc.MetaColumn.Name); if (qci3 < 0) { continue; // ignore if doesn't match by name } QueryColumn qc3 = qt3.QueryColumns[qci3]; // cloned column if (qc.Criteria != "") { // if criteria then clone model query col for simpler copying of criteria QueryColumn qc4 = qc.Clone(); qc4.MetaColumn = qc3.MetaColumn; qc4.QueryTable = qt3; qt3.QueryColumns[qci3] = qc4; qc3 = qc4; } qc3.Selected = qc.Selected; continue; } newQuery.AddQueryTable(qt3); } return; }
/// <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> /// OK button clicked, process input /// </summary> /// <returns></returns> DialogResult ProcessInput() { int rgCount; // number of Rgroups List <Rgroup> rgList; // list of existing Rgroups bool[] rgExists; // entry = true if rgroup exists in core Rgroup rg; bool oneD, twoD; // matrix dimensionality List <string> keys = null; Dictionary <string, List <QualifiedNumber> > mElem; // matrix element dictionary List <RgroupSubstituent>[] rSubs; // substituents seen for each Rgroup Query q, q0, q2; QueryTable qt, qt2; QueryColumn qc, qc2; MetaTable mt, mt2; MetaColumn mc, mc2; DataTableMx dt; DataRowMx dr; DialogResult dlgRslt; string tok; int ri, rii, si, qti, qci, bi, bi2; // Get core structure & list of R-groups MoleculeMx core = new MoleculeMx(MoleculeFormat.Molfile, SQuery.MolfileString); if (core.AtomCount == 0) { MessageBoxMx.ShowError("A Core structure with R-groups must be defined"); return(DialogResult.None); } if (!Structure.Checked && !Smiles.Checked && !Formula.Checked && !Weight.Checked && !Index.Checked) { MessageBoxMx.ShowError("At least one substituent display format must be selected."); return(DialogResult.None); } mt = MetaTableCollection.GetWithException("Rgroup_Decomposition"); qt = new QueryTable(mt); qc = qt.GetQueryColumnByNameWithException("Core"); qc.MolString = core.GetMolfileString(); // put core structure into table criteria qc.CriteriaDisplay = "Substructure search (SSS)"; qc.Criteria = "CORE SSS SQUERY"; qc = qt.GetQueryColumnByNameWithException("R1_Structure"); if (ShowCoreStructure.Checked) { qc.Label = "R-group, Core\tChime=" + core.GetChimeString(); // reference core in query col header label qc.MetaColumn.Width = 25; } RgroupDecomposition.SetSelected(qt, "R1_Structure", Structure.Checked); // select for retrieval if checked RgroupDecomposition.SetSelected(qt, "R1_Smiles", Smiles.Checked); RgroupDecomposition.SetSelected(qt, "R1_Formula", Formula.Checked); RgroupDecomposition.SetSelected(qt, "R1_Weight", Weight.Checked); RgroupDecomposition.SetSelected(qt, "R1_SubstNo", Index.Checked); string terminateOption = "First mapping"; // terminate on first complete match qc = qt.GetQueryColumnByNameWithException("Terminate_Option"); qc.Criteria = qt.MetaTable.Name + " = " + Lex.AddSingleQuotes(terminateOption); qc.CriteriaDisplay = "= " + Lex.AddSingleQuotes(terminateOption); QueryTable rgdQt = qt; // keep a ref to it if (QbUtil.Query == null || QbUtil.Query.Tables.Count == 0) { MessageBoxMx.ShowError("No current query."); return(DialogResult.None); } q0 = QbUtil.Query; // original query this analysis is based on q = q0.Clone(); // make copy of source query we can modify q.SingleStepExecution = false; qti = 0; while (qti < q.Tables.Count) // deselect query columns that we don't want { qt = q.Tables[qti]; if (Lex.Eq(qt.MetaTable.Name, "Rgroup_Decomposition")) { // remove any rgroup decomp table qti++; continue; } mt = qt.MetaTable; if (mt.MultiPivot || // check for tables not allowed in underlying query mt.MetaBrokerType == MetaBrokerType.CalcField || // (called ShouldPresearchAndTransform previously) mt.MetaBrokerType == MetaBrokerType.MultiTable || mt.MetaBrokerType == MetaBrokerType.RgroupDecomp) { MessageBoxMx.ShowError("Multipivot/Rgroup table \"" + qt.ActiveLabel + "\" can't be included in an underlying Rgroup Matrix query"); return(DialogResult.None); } for (qci = 0; qci < qt.QueryColumns.Count; qci++) { qc = qt.QueryColumns[qci]; if (qc.MetaColumn == null) { continue; } switch (qc.MetaColumn.DataType) { case MetaColumnType.CompoundId: // keep only these case MetaColumnType.Integer: case MetaColumnType.Number: case MetaColumnType.QualifiedNo: case MetaColumnType.String: break; default: qc.Selected = false; break; } } qti++; } q.AddQueryTable(rgdQt); // Add Rgroup decom table to end of cloned source query Progress.Show("Retrieving data..."); try { dlgRslt = ToolHelper.ExecuteQuery(ref q, out keys); if (dlgRslt != DialogResult.OK) { return(dlgRslt); } } catch (Exception ex) { MessageBoxMx.ShowError("Error executing query:\r\n" + ex.Message); return(DialogResult.None); } if (keys == null || keys.Count == 0) { Progress.Hide(); MessageBoxMx.ShowError("No results were returned by the query."); return(DialogResult.None); } // Scan modified query to get list of rgroup indexes that are present rgExists = new bool[32]; rgList = new List <Rgroup>(); QueryTable rgQt = q.GetQueryTableByName("Rgroup_Decomposition"); foreach (QueryColumn qc0 in rgQt.QueryColumns) { mc = qc0.MetaColumn; if (!(mc.Name.StartsWith("R") && mc.Name.EndsWith("_STRUCTURE") && qc0.Selected)) { continue; // skip if not a selected Rgroup structure } int len = mc.Name.Length - ("R" + "_STRUCTURE").Length; tok = mc.Name.Substring(1, len); if (!int.TryParse(tok, out ri)) { continue; } rgExists[ri - 1] = true; rg = new Rgroup(); rg.RIndex = ri; rg.VoPos = qc0.VoPosition; rgList.Add(rg); } for (bi = 1; bi < rgList.Count; bi++) { // sort by increasing R index rg = rgList[bi]; for (bi2 = bi - 1; bi2 >= 0; bi2--) { if (rg.RIndex >= rgList[bi2].RIndex) { break; } rgList[bi2 + 1] = rgList[bi2]; } rgList[bi2 + 1] = rg; } rgCount = rgList.Count; twoD = TwoD.Checked; if (rgCount == 1) { twoD = false; // if only 1 rgroup can't do as 2d } oneD = !twoD; // Read data into mElem and rgroup substituents into rSubs. // Matrix mElem is keyed on [R1Smiles, R2Smiles,... RnSmiles, FieldName] for 1d and // [R1Smiles, R2Smiles,... FieldName, RnSmiles] for 2d QueryManager qm = q.QueryManager as QueryManager; DataTableManager dtm = qm.DataTableManager; dt = qm.DataTable; mElem = new Dictionary <string, List <QualifiedNumber> >(); // matrix element dictionary rSubs = new List <RgroupSubstituent> [32]; // list of substituents seen for each Rgroup for (rii = 0; rii < rgCount; rii++) // alloc substituent list for rgroup { rSubs[rii] = new List <RgroupSubstituent>(); } int rowCount = 0; while (true) { // scan data accumulating rgroup substituents and data values dr = dtm.FetchNextDataRow(); if (dr == null) { break; } rowCount++; string cid = dr[dtm.KeyValueVoPos] as string; string lastMapCid = "", rgroupKey = "", rgroupKeyLast = ""; int mapCount = 0; for (rii = 0; rii < rgCount; rii++) // for { MoleculeMx rSub = dr[rgList[rii].VoPos] as MoleculeMx; if (rSub == null || rSub.AtomCount == 0) { continue; } ri = rgList[rii].RIndex; // actual R index in query int subIdx = RgroupSubstituent.Get(rSub, rSubs[rii]); // if (ri == 1 && subIdx != 0) subIdx = subIdx; // debug if (subIdx < 0) { continue; } string rKey = "R" + ri.ToString() + "_" + (subIdx + 1).ToString(); if (oneD || rii < rgCount - 1) { if (rgroupKey != "") { rgroupKey += "\t"; } rgroupKey += rKey; } else { rgroupKeyLast = rKey; } lastMapCid = cid; mapCount++; } if (lastMapCid == cid) // add the data if compound has a mapping { AccumulateMatrixElements(mElem, q, dr, rgroupKey, rgroupKeyLast, cid); } if (Progress.IsTimeToUpdate) { Progress.Show("Retrieving data: " + StringMx.FormatIntegerWithCommas(rowCount) + " rows..."); } } if (rowCount == 0) { Progress.Hide(); MessageBoxMx.ShowError("No data rows retrieved"); return(DialogResult.None); } if (twoD && (rSubs[rgCount - 1] == null || rSubs[rgCount - 1].Count == 0)) { // if 2D be sure we have at least one substituent for the last Rgroup Progress.Hide(); MessageBoxMx.ShowError("No substituents found for R" + rgCount.ToString()); return(DialogResult.None); } // Create a MetaTable & DataTable for matrix results Progress.Show("Analyzing data..."); mt = new MetaTable(); // create output table MatrixCount++; mt.Name = "RGROUPMATRIX_" + MatrixCount; mt.Label = "R-group Matrix " + MatrixCount; mt.MetaBrokerType = MetaBrokerType.RgroupDecomp; mc = // use sequence for key mt.AddMetaColumn("RgroupMatrixId", "No.", MetaColumnType.Integer, ColumnSelectionEnum.Selected, 3); mc.ClickFunction = "None"; // avoid hyperlink on this key mc.IsKey = true; int maxLeftR = rgCount; if (twoD) { maxLeftR = rgCount - 1; } for (ri = 0; ri < maxLeftR; ri++) { string rStr = "R" + (ri + 1).ToString(); if (Structure.Checked) { mc = mt.AddMetaColumn(rStr + "Str", rStr, MetaColumnType.Structure, ColumnSelectionEnum.Selected, 12); if (ri == 0 && ShowCoreStructure.Checked) // include core structure above R1 if requested { string chimeString = MoleculeMx.MolfileStringToSmilesString(SQuery.MolfileString); mc.Label = "R1, Core\tChime=" + chimeString; mc.Width = 25; } } if (Smiles.Checked) { mc = mt.AddMetaColumn(rStr + "Smi", rStr + " Smiles", MetaColumnType.String, ColumnSelectionEnum.Selected, 12); } if (Formula.Checked) { mc = mt.AddMetaColumn(rStr + "Mf", rStr + " Formula", MetaColumnType.String, ColumnSelectionEnum.Selected, 8); } if (Weight.Checked) { mc = mt.AddMetaColumn(rStr + "MW", rStr + " Mol. Wt.", MetaColumnType.Number, ColumnSelectionEnum.Selected, 6, ColumnFormatEnum.Decimal, 2); } if (Index.Checked) { mc = mt.AddMetaColumn(rStr + "Index", rStr + " Subst. Idx.", MetaColumnType.Number, ColumnSelectionEnum.Selected, 4); mc.Format = ColumnFormatEnum.Decimal; } } mc = // add column to contain result type mt.AddMetaColumn("ResultType", "Result Type", MetaColumnType.String, ColumnSelectionEnum.Selected, 12); if (oneD) // add just 1 column to contain results { mc = mt.AddMetaColumn("Results", "Results", MetaColumnType.QualifiedNo, ColumnSelectionEnum.Selected, 12); mc.MetaBrokerType = MetaBrokerType.RgroupDecomp; // broker to do special col handling for cond formtting if (QbUtil.Query.UserObject.Id > 0) { mc.DetailsAvailable = true; } } else // add col for each substituent for last rgroup { string rStr = "R" + rgCount.ToString(); for (si = 0; si < rSubs[rgCount - 1].Count; si++) { string cName = rStr + "_" + (si + 1).ToString(); string cLabel = cName.Replace("_", "."); RgroupSubstituent rgs = rSubs[ri][si]; // get substituent info if (Structure.Checked) // include structure { cLabel += "\tChime=" + rgs.Struct.GetChimeString(); } else if (Smiles.Checked) { cLabel += " = " + rgs.Struct.GetSmilesString(); } else if (Formula.Checked) { cLabel += " = " + rgs.Struct.MolFormula; } else if (Weight.Checked) { cLabel += " = " + rgs.Struct.MolWeight; } else if (Index.Checked) { cLabel += " = " + (si + 1).ToString(); } mc = mt.AddMetaColumn(cName, cLabel, MetaColumnType.QualifiedNo, ColumnSelectionEnum.Selected, 12); mc.MetaBrokerType = MetaBrokerType.RgroupDecomp; if (QbUtil.Query.UserObject.Id > 0) { mc.DetailsAvailable = true; } } } MetaTableCollection.UpdateGlobally(mt); // add as a known metatable if (mElem.Count == 0) // be sure we have a matrix { Progress.Hide(); MessageBoxMx.ShowError("No matrix can be created because insufficient data was found."); return(DialogResult.None); } // Build the DataTable Progress.Show("Building data table..."); q2 = new Query(); // build single-table query to hold matrix qt2 = new QueryTable(q2, mt); dt = DataTableManager.BuildDataTable(q2); Dictionary <string, List <QualifiedNumber> > .KeyCollection kc = mElem.Keys; string[] rgKeys = new string[mElem.Count]; kc.CopyTo(rgKeys, 0); Array.Sort(rgKeys); string[] rgKey = null, lastRgKey = null; int rki = 0; for (rki = 0; rki < rgKeys.Length; rki++) { rgKey = rgKeys[rki].Split('\t'); int riTop = rgCount + 1; // all r substituents & field name on left if (twoD) { riTop = rgCount; } for (ri = 0; ri < riTop; ri++) // see if any changes in left side substituents or field name { if (lastRgKey == null || rgKey[ri] != lastRgKey[ri]) { break; } } if (ri < riTop || oneD) // if 2d then new row only if some change before last R { dr = dt.NewRow(); dt.Rows.Add(dr); dr[dtm.KeyValueVoPos + 1] = new NumberMx(dt.Rows.Count); // integer row key } if (!HideRepeatingSubstituents.Checked) { ri = 0; // start at first if not hiding } lastRgKey = rgKey; for (ri = ri; ri < riTop; ri++) // build row with these { string rgSub = rgKey[ri]; // get substituent id or table.column name if (rgSub == "") { continue; } if (ri < riTop - 1) { // output substituent and/or smiles string rStr = "R" + (ri + 1).ToString(); si = rgSub.IndexOf("_"); si = Int32.Parse(rgSub.Substring(si + 1)) - 1; // get substituent index RgroupSubstituent rgs = rSubs[ri][si]; // get substituent info if (Structure.Checked) { qc2 = qt2.GetQueryColumnByName(rStr + "Str"); dr[QcToDcName(qc2)] = rgs.Struct; } if (Smiles.Checked) { qc2 = qt2.GetQueryColumnByName(rStr + "Smi"); dr[QcToDcName(qc2)] = new StringMx(rgs.Struct.GetSmilesString()); } if (Formula.Checked) { qc2 = qt2.GetQueryColumnByName(rStr + "Mf"); dr[QcToDcName(qc2)] = new StringMx(rgs.Struct.MolFormula); } if (Weight.Checked) { qc2 = qt2.GetQueryColumnByName(rStr + "Mw"); dr[QcToDcName(qc2)] = new NumberMx(rgs.Struct.MolWeight); } if (Index.Checked) { qc2 = qt2.GetQueryColumnByName(rStr + "Index"); dr[QcToDcName(qc2)] = new NumberMx(si + 1); } } else // output field name { string[] sa = rgSub.Split('.'); // get field name qt = q.GetQueryTableByName(sa[0]); qc = qt.GetQueryColumnByName(sa[1]); string fieldName = qc.ActiveLabel; if (q0.Tables.Count >= 3) // qualify by table if 3 or more tables in original query { fieldName = qt.ActiveLabel + " - " + fieldName; } qc2 = qt2.GetQueryColumnByName("ResultType"); dr[QcToDcName(qc2)] = new StringMx(fieldName); } } // Output value string cName; if (oneD) { cName = "Results"; } else { cName = rgKey[rgCount]; // get key for this substituent (e.g. R2_1) } if (Lex.IsUndefined(cName)) { continue; // may be no substituent match } qc2 = qt2.GetQueryColumnByName(cName); QualifiedNumber qn = SummarizeData(mElem[rgKeys[rki]]); // get summarized value dr[QcToDcName(qc2)] = qn; } ToolHelper.DisplayData(q2, dt, true); UsageDao.LogEvent("RgroupMatrix"); Progress.Hide(); return(DialogResult.OK); }
/// <summary> /// Handle a click on a link to a 3D structure & open in Vida /// If clicked from an XtraGrid give the user a range of options for the data /// to be exported. If clicked from a HTML page give limited options. /// </summary> /// <param name="mtName"></param> /// <param name="mcName"></param> /// <param name="url"></param> public void ClickFunction( string mtName, string mcName, string url) { ResultsFormatter fmtr = null; ResultsField rfld = null; //DataRowMx dr; StringMx sx; StreamWriter sw; int markedCount = 0; string densityMapUrl = null; string target = null; _currentMetaTable = mtName; _currentPdbColumnName = mcName; IncludeElectronDensityPyMol.Checked = false; MetaTable mt = MetaTableCollection.Get(mtName); if (mt == null) { return; } MetaColumn mc = mt.GetMetaColumnByName(mcName); if (mc == null) { return; } _xRay2Request = Lex.Contains(mtName, "XRay2"); // newer XRay2 database table? _urlFileName = Path.GetFileName(url); // extract file name from url QueryManager qm = ClickFunctions.CurrentClickQueryManager; if (qm != null) { Query q = qm.Query; ResultsFormat rf = qm.ResultsFormat; fmtr = qm.ResultsFormatter; rfld = rf.GetResultsField(mc); if (rfld == null) { return; } QueryTable qt = q.GetQueryTableByNameWithException(mtName); _qcProtein = qt.GetQueryColumnByNameWithException(mcName); if (_xRay2Request) // newer XRay2 database { string mapUrl = ""; if (mcName == "ALIGNED_SPLIT_COMPLEX_URL") { mapUrl = "ALIGNED_SPLIT_MAP_URL"; } else if (mcName == "ALIGNED_FULL_COMPLEX_URL") { mapUrl = "ALIGNED_FULL_MAP_URL"; } else if (mcName == "ORIGINAL_PDB_URL") { mapUrl = "ORIGINAL_MAP_URL"; } //{ mapUrl = "ORIGINAL_MAP_URL"; } _qcDensity = qt.GetQueryColumnByName(mapUrl); //("ALIGNED_SPLIT_MAP_URL"); if (_qcDensity != null && !_qcDensity.Selected) { _qcDensity = null; } _qcTarget = qt.GetQueryColumnByName("primary_gene_name_alias"); if (_qcTarget != null && !_qcTarget.Selected) { _qcTarget = null; } } if (_qcDensity != null) { // if there is a density map url located in the density column, enable the PyMol CheckEdit DataRowMx dr = qm.DataTable.Rows[qm.MoleculeGrid.LastMouseDownCellInfo.DataRowIndex]; densityMapUrl = Lex.ToString(dr[_qcDensity.VoPosition]); } else { // user did not select the map column, try to retrieve the XRay1 or Xray2 density map url fromt he metatable densityMapUrl = _xRay2Request ? GetXray2DensityMapUrl(url, qt.MetaTable.Name, mcName) : GetXray1DensityMapUrl(url, qt.MetaTable.Name); } // default checkbox to false so user does not load electron density maps everytime, since these take // extra time to load. IncludeElectronDensityPyMol.Checked = false; IncludeElectronDensityPyMol.Enabled = !string.IsNullOrEmpty(densityMapUrl); if (_qcProtein == null && _qcDensity == null) { throw new Exception("Neither the PDB nor the MAP column is selected in the query"); } markedCount = fmtr.MarkedRowCount; int unmarkedCount = fmtr.UnmarkedRowCount; if (markedCount == 0 || unmarkedCount == 0) // if no specific selection assume user wants single structure { ExportSingle.Checked = true; FileName.Text = _urlFileName; } else { ExportMarked.Checked = true; // assume wants marked structures } ExportMarked.Enabled = true; } else // simple setup for click from HTML display { ExportSingle.Checked = true; ExportMarked.Enabled = false; FileName.Text = _urlFileName; densityMapUrl = GetXray2DensityMapUrl(url, _currentMetaTable, mcName); target = GetTarget(url, _currentMetaTable, mcName); ////IncludeProtein.Enabled = IncludeElectronDensityEnabled = false; ////if (Lex.Eq(mcName, "bsl_xray_cmplx_url")) //// IncludeProtein.Checked = true; ////else if (Lex.Eq(mcName, "bsl_xray_edensity_url")) //// IncludeElectronDensityChecked = true; } if (mcName == "ALIGNED_SPLIT_MAP_URL" || mcName == "ALIGNED_FULL_MAP_URL" || mcName == "ORIGINAL_MAP_URL") // not viewable fileds { DisablePymol(); DisableMoe(); ExportToFile.Enabled = ExportToFile.Checked = true; } else if (mcName == "ALIGNED_FULL_COMPLEX_URL" || mcName == "ORIGINAL_PDB_URL" || mcName == "ALIGNED_SPLIT_COMPLEX_URL") // viewable by PyMol { EnableMoe(); EnablePymol(); ExportToFile.Enabled = true; ExportToFile.Checked = false; } else //everything else should be viewable by MOE { EnableMoe(); DisablePymol(); ExportToFile.Enabled = true; ExportToFile.Checked = false; } DialogResult dlgRslt = ShowDialog(SessionManager.ActiveForm); if (dlgRslt == DialogResult.Cancel) { return; } bool exportSingle = ExportSingle.Checked; bool exportMarked = !exportSingle; if (!IncludeElectronDensityPyMol.Checked) { densityMapUrl = null; } if (exportMarked) // see if reasonable count if exporting marked rows { string msg; if (markedCount == 0) { msg = "No rows have been marked for export"; MessageBoxMx.Show(msg, "Mobius", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (markedCount > 10) { msg = markedCount + " structures have been selected out export.\n" + "Are you sure these are the structures you want to export?"; dlgRslt = MessageBoxMx.Show(msg, "Mobius", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (dlgRslt != DialogResult.Yes) { return; } } } // Export to PyMol (Schrodinger) - Collab with Paul Sprengeler and Ken Schwinn // Relavant Files: // 1. Plugin: CorpView.py (in Mobius config dir) // 2. Script: CorpView.pml - Runs the plugin and then passes in the .csv file (in Mobius config dir) // Expected content (note: <csv-file-name> gets replaced): // import pymol // pluginLocation = 'C:\Progra~1\PyMOL\PyMOL\modules\pmg_tk\startup\CorpView.py' // cmd.do('run ' + pluginLocation) // cmd.do('lv_create_environment') // cmd.do('lv_import_mobius <csv-file-name>') // cmd.do('lv_destroy_environment') // 3. Csv file: PyMolBatchLoad.csv - Csv file in format expected by plugin (in Mobius temp dir) if (ExportToPyMol.Checked) { ExportToPyMolMethod(url, densityMapUrl, target, qm); } else if (ExportToMOE.Checked) // Export to MOE { ExportToMOEMethod(url, qm, fmtr, rfld); } else { ExportToFilesMethod(url, qm, fmtr, rfld); } }