/// <summary> /// Let the user select an existing annotation and edit it /// </summary> /// <returns></returns> public static MetaTable OpenExistingAnnotationTable( UserObject uo) { if (uo == null) // prompt if not supplied { uo = UserObjectOpenDialog.ShowDialog(UserObjectType.Annotation, "Open Annotation"); } if (uo == null) { return(null); } if (!Permissions.UserHasWriteAccess(SS.I.UserName, uo)) { MessageBoxMx.ShowError("You are not authorized to edit this annotation table."); return(null); } UserDataEditor editor = new UserDataEditor(); UserObject uo2 = editor.Edit(uo); if (uo2 == null) { return(null); } string tName = "ANNOTATION_" + uo.Id.ToString(); MetaTable mt = MetaTableCollection.Get(tName); // return new version of metatable return(mt); }
/// <summary> /// Create MetaTable from template /// </summary> public MetaTable GetMetaTable( string mtName) { if (!Lex.StartsWith(mtName, UnpivotedAssayView.UnsummarizedMetaTableName)) { return(null); } MetaTable mt0 = MetaTableCollection.Get(UnpivotedAssayView.UnsummarizedMetaTableName + "_TEMPLATE"); // get template metatable if (mt0 == null) { return(null); } MetaTable mt = mt0.Clone(); string labelSuffix = ""; // default to integrated view of these mt.Label += labelSuffix; List <MetaColumn> mcList = new List <MetaColumn>(); // Build summarized version of table if (Lex.EndsWith(mtName, MetaTable.SummarySuffix)) // summarized version { mt.Name = mtName; mt.Label += " Summary"; mt.UseSummarizedData = true; foreach (MetaColumn mc in mt.MetaColumns) { if (mc.SummarizedExists) { mcList.Add(mc); } } mt.MetaColumns = mcList; return(mt); } // Build unsummarized version of table else { mt.Name = mtName; foreach (MetaColumn mc in mt.MetaColumns) { if (mc.UnsummarizedExists) { mcList.Add(mc); } } mt.MetaColumns = mcList; return(mt); } }
/// <summary> /// Build a basic database query for the user database /// </summary> /// <param name="uo"></param> /// <returns></returns> internal static bool BuildDatabaseQuery( UserObject uo) { List <UserObject> luo = UserDataEditor.GetUcdbUserObjects(uo.Id); if (luo == null || luo.Count == 0) { MessageBoxMx.ShowError("No tables found for database"); return(false); } Query q = new Query(); foreach (UserObject uo2 in luo) { if (!UserObject.IsMetaTableType(uo2.Type)) { continue; } string mtName = uo2.Type.ToString() + "_" + uo2.Id; MetaTable mt = MetaTableCollection.Get(mtName); if (mt == null) { continue; } QueryTable qt = new QueryTable(mt); q.AddQueryTable(qt); } QbUtil.NewQuery(uo.Name); QbUtil.SetCurrentQueryInstance(q); return(true); }
/// <summary> /// SetupForm /// </summary> void SetupForm() { string DefaultLibraryPath = "/Mobius/Visualizations/"; string DefaultCorpIdParm = "CorpId_LIST"; InSetup = true; SpotfireViewProps svp = SpotfireViewProps; string libPath = svp.AnalysisPath; if (Lex.IsUndefined(libPath)) { libPath = DefaultLibraryPath; } LibraryPath.Text = libPath; MetaTable mt = MetaTableCollection.Get(SpotfireLinkModel); if (mt == null) { throw new Exception(SpotfireLinkModel + " table not found"); } mt = mt.Clone(); CriteriaCols.QueryTable = new QueryTable(mt); SetupParameters(); Description.Text = svp.Description; InSetup = false; return; }
/// <summary> /// Read the list of ids for a compound library /// </summary> /// <param name="libId"></param> /// <returns></returns> public static CidList ReadLibrary( int libId) { if (ServiceFacade.UseRemoteServices) { Mobius.Services.Native.INativeSession nativeClient = ServiceFacade.CreateNativeSessionProxy(); Services.Native.NativeMethodTransportObject resultObject = ServiceFacade.InvokeNativeMethod(nativeClient, (int)Services.Native.ServiceCodes.MobiusCidListService, (int)Services.Native.ServiceOpCodes.MobiusCidListService.ReadLibrary, new Services.Native.NativeMethodTransportObject(new object[] { libId })); ((System.ServiceModel.IClientChannel)nativeClient).Close(); if (resultObject == null || resultObject.Value == null) { return(null); } UserObject uo = UserObject.DeserializeBinary((byte[])resultObject.Value); MetaTable mt = MetaTableCollection.Get(MetaTable.PrimaryRootTable); CidList cidList = CidList.Deserialize(uo, mt); return(cidList); } else { return(UAL.CidListDao.ReadLibrary(libId)); } }
/// <summary> /// Parse external format advanced expression into internal form /// </summary> /// <param name="advExprExt"></param> /// <param name="mcList"></param> /// <returns></returns> string ParseAdvancedExpr( string advExprExt, out List <MetaColumn> mcList) { MetaTable mt; MetaColumn mc; string advExpr = advExprExt; // copy external to internal mcList = new List <MetaColumn>(); Lex lex = new Lex(); lex.OpenString(advExprExt); while (true) { string tok = lex.Get(); if (tok == "") { break; } if (Lex.IsQuoted(tok, '"')) { tok = tok.Substring(1, tok.Length - 2).Trim(); // remove quotes int i1 = tok.IndexOf('.'); if (i1 < 0) { throw new Exception("Invalid table.field name: " + tok); } string table = tok.Substring(0, i1); string column = tok.Substring(i1 + 1); mt = MetaTableCollection.Get(table); if (mt == null) { throw new Exception("Unknown table name: " + table); } mc = mt.GetMetaColumnByLabel(column); if (mc == null) { mc = mt.GetMetaColumnByName(column); } if (mc == null) { throw new Exception("Unknown column name: " + column); } string tok2 = mt.Name + "." + mc.Name; // store as internal name advExpr = advExpr.Replace(tok, tok2); if (!mcList.Contains(mc)) { mcList.Add(mc); } } } return(advExpr); // return internal form }
/// <summary> /// Build the Query, QueryManager and DataTable for the matching structure display /// </summary> public void BuildStructureDisplayQuery() { MetaTable mt = MetaTableCollection.Get("QuickSearchRelatedStructures"); if (mt == null) { DebugLog.Message("QuickSearchRelatedStructures MetaTable not found"); RSC.MoleculeGridPageControl.Visible = false; return; } Query q = ToolHelper.InitEmbeddedDataToolQuery(mt); QueryTable qt = q.Tables[0]; q.UserObject.Name = qt.MetaTable.Label; StructureDisplayQuery = q; QueryColumn qc = qt.FirstStructureQueryColumn; if (qc != null) // setup dummy criteria { ParsedStructureCriteria pssc = new ParsedStructureCriteria(); pssc.SearchType = StructureSearchType.FullStructure; // just full structure search for now pssc.Molecule = new MoleculeMx(MoleculeFormat.Smiles, "C"); // placeholder structure pssc.ConvertToQueryColumnCriteria(qc); qc.DisplayFormatString = "Highlight=true;Align=true"; } string title = "Related Structures"; qt.Label = title; DataTableMx dt = q.ResultsDataTable as DataTableMx; Qm = ToolHelper.SetupQueryManager(q, dt); MoleculeGridPageControl mgpc = RSC.MoleculeGridPageControl; MoleculeGridPanel gp = mgpc.MoleculeGridPanel; mgpc.Width = RSC.Width - 3; // be sure we have page control correctly sized mgpc.Height = RSC.Height - mgpc.Top - 3; ToolHelper.SetupGridPanelForDisplay(Qm, mgpc.MoleculeGridPanel, true, true); MoleculeGridControl grid = mgpc.MoleculeGrid; if (grid != null && grid.GV != null) { grid.GV.IndicatorWidth = 40; // narrow indicator a bit } //ToolHelper.DisplayData // build and display empty grid for query with columns scaled to fit the grid // (q, dt, RSC.MoleculeGridPageControl.MoleculeGridPanel, true, true); return; }
/// <summary> /// Lookup a MetaTable by name throwing any exceptions from underlying factories /// </summary> /// <param name="name"></param> /// <returns>MetaTable or null if not found</returns> /// public MetaTable GetMetaTable( String name) { MetaTable mt; if (RestrictedMetaTables.MetatableIsRestricted(name)) { return(null); } if (RestrictedMetatable.MetatableIsGenerallyRestricted(name)) { return(null); } name = name.Trim().ToUpper(); if (MetaTableCollection.TableMap.ContainsKey(name)) { // see if in collection already mt = MetaTableCollection.TableMap[name]; return(mt); } for (int i1 = 0; i1 < MetaFactories.Count; i1++) { MetaTableFactoryRef mtfr = MetaFactories[i1]; mt = mtfr.MetaTableFactory.GetMetaTable(name); if (mt != null) { NormalizeMetaTable(mt); return(mt); } } // Check to see if this is a summary table that can be created from an unsummarized version of itself if (!Lex.EndsWith(name, MetaTable.SummarySuffix)) { return(null); // see if named as summary table } string name2 = name.Substring(0, name.Length - MetaTable.SummarySuffix.Length); MetaTable mt2 = MetaTableCollection.Get(name2); if (mt2 == null || !mt2.SummarizedExists) { return(null); } mt = mt2.Clone(); AdjustForSummarization(mt, true); return(mt); }
private void SummarizedViewMenuItem_Click(object sender, EventArgs e) { string mtName = FocusedMetaTable.Name + MetaTable.SummarySuffix; // indicate summary table MetaTable mt = MetaTableCollection.Get(mtName); if (mt == null) { MessageBoxMx.ShowError("MetaTable not found: " + mtName); return; } RenderTable(mt, null); }
/// <summary> /// Add a MetaTableItem to the DataTable /// </summary> /// <param name="mtName"></param> void AddMetaTableItemToDataTable( string mtName) { MetaTreeNode mtn = MetaTreeNodeCollection.GetNode(mtName); if (mtn != null && mtn.IsFolderType) { return; // ignore folders } MetaTable mt = MetaTableCollection.Get(mtName); if (mt == null) { MessageBoxMx.ShowError("The selected item is not a recognized data table: " + mtName); return; } string allowedTypes = Qc.MetaColumn.TableMap; if (Lex.IsDefined(allowedTypes)) // see if supplied table is allowed { int ai; string[] sa = allowedTypes.Split(','); for (ai = 0; ai < sa.Length; ai++) { string allowedType = sa[ai]; if (Lex.IsUndefined(allowedType)) { continue; } if (Lex.Contains(mt.Name, allowedType.Trim())) { break; } } if (ai >= sa.Length) { MessageBoxMx.ShowError("The selected data table is not is not of a type that can be added here (" + allowedTypes + ")"); return; } } MetaTableItem i = new MetaTableItem(); i.ExternalName = mt.Label; i.InternalName = mt.Name; AddMetaTableItemToDataTable(i); return; }
/// <summary> /// Select an existing calculated field definition /// </summary> /// <returns></returns> public static MetaTable SelectExisting() { UserObject uo = UserObjectOpenDialog.ShowDialog(UserObjectType.CalcField, "Select Calculated Field"); if (uo == null) { return(null); } string tName = "CALCFIELD_" + uo.Id.ToString(); MetaTable mt = MetaTableCollection.Get(tName); return(mt); }
/// <summary> /// Create a new calculated field /// </summary> /// <returns></returns> public static MetaTable CreateNew() { CalcField cf = new CalcField(); UserObject uo = Edit(cf, null); if (uo == null) { return(null); } string tName = "CALCFIELD_" + uo.Id.ToString(); MetaTable mt = MetaTableCollection.Get(tName); return(mt); }
/// <summary> /// Create a new annotation /// </summary> /// <returns></returns> public static MetaTable CreateNewAnnotationTable() { UserDataEditor editor = new UserDataEditor(); UserObject uo = new UserObject(UserObjectType.Annotation); uo = editor.Edit(uo); if (uo == null) { return(null); } string tName = "ANNOTATION_" + uo.Id.ToString(); MetaTable mt = MetaTableCollection.Get(tName); return(mt); }
/// <summary> /// Read the list of compounds for a library /// </summary> /// <param name="libId"></param> /// <returns></returns> public static CidList ReadLibrary( int libId) { CidList l = new CidList(); DbCommandMx dao = new DbCommandMx(); string sql = @" SELECT l.library_name, l.library_desc_text, s.corp_nbr FROM corp_owner.corp_substance s, corp_owner.corp_library_substance ls, corp_owner.corp_library l WHERE l.lib_id = <libId> and s.cpd_id = ls.cpd_id and l.lib_id = ls.lib_id" ; sql = Lex.Replace(sql, "<libId>", libId.ToString()); dao.Prepare(sql); dao.ExecuteReader(); MetaTable rootMt = MetaTableCollection.Get(MetaTable.PrimaryRootTable); while (dao.Read()) { if (Lex.IsNullOrEmpty(l.UserObject.Name)) { string name = dao.GetString(0); if (Lex.IsNullOrEmpty(name) || Lex.IsInteger(name)) { name = dao.GetString(1); // use desc if no name or just a number } if (Lex.IsNullOrEmpty(name)) { name = "Library " + libId; } l.UserObject.Name = name; } int intCorpId = dao.GetInt(2); string corpId = CompoundId.Normalize(intCorpId, rootMt); l.Add(corpId); } dao.CloseReader(); dao.Dispose(); return(l); }
public MetaTable GetMetaTable( string mtName) { MetaTable mt = null; MetaColumn mc, mc2; int methodId; string txt, tok; mtName = mtName.Trim().ToUpper(); string name2 = mtName; string prefix = "ORACLE"; if (mtName.StartsWith(prefix + "_") || mtName.StartsWith(prefix + ".")) { name2 = mtName.Substring(prefix.Length + 1); } string[] sa = name2.Split('.'); if (sa.Length < 2) { return(null); } name2 = sa[0] + "." + sa[1]; mt = MetaTableFactory.GetMetaTableFromDatabaseDictionary(name2); if (mt == null || mt.MetaColumns.Count == 0) { return(null); } mt.Name = mtName; // assign fully qualified name if (sa.Length >= 3) // key col name included { mc = mt.GetMetaColumnByName(sa[2]); if (mc != null) { mc.DataType = MetaColumnType.CompoundId; } } if (sa.Length >= 4) // assign parent if parent table name included { mt.Parent = MetaTableCollection.Get(sa[3]); } return(mt); }
/// <summary> /// Get query to select all data. /// It consists of a single QueryTable with the CID and key table name /// This keeps the initial query sent to the client small which helps /// response time for users not on the same local network as the server. /// </summary> /// <param name="keyMtName"></param> /// <param name="cn"></param> /// <returns></returns> public static Query GetSelectAllDataQuery( string keyMtName, string cn) { MetaTable mt = MetaTableCollection.GetWithException(MetaTable.AllDataQueryTable); Query q = new Query(); QueryTable qt = new QueryTable(mt); q.AddQueryTable(qt); MetaTable keyMt = null; if (Lex.IsDefined(keyMtName)) { keyMt = MetaTableCollection.Get(keyMtName); } if (keyMt != null && keyMt.Root.IsUserDatabaseStructureTable) // if root metatable is user database then normalize based on key { keyMt = keyMt.Root; // be sure we have root cn = CompoundId.Normalize(cn, keyMt); } else { cn = CompoundId.Normalize(cn); keyMt = CompoundId.GetRootMetaTableFromCid(cn, keyMt); keyMt = keyMt.Root; // be sure we have root (may not be structure table) } if (keyMt == null) { throw new Exception("Failed to identify key MetaTable"); } q.KeyCriteria = q.KeyCriteriaDisplay = " = " + cn; QueryColumn qc = qt.GetQueryColumnByNameWithException("root_table"); qc.Criteria = qc.CriteriaDisplay = qc.MetaColumn.Name + " = " + keyMt.Name; return(q); }
/// <summary> /// ContentsTreeItemSelected /// </summary> /// <param name="nodeTarget"></param> private void ContentsTreeItemSelected(string nodeTarget) { MetaTreeNode node = MetaTreeNodeCollection.GetNode(nodeTarget); if (node == null || (node.Type & TypeFilter) == 0) { SelectionName.Text = SelectionTarget.Text = ""; return; } else if (node.IsDataTableType) { MetaTable mt = MetaTableCollection.Get(node.Target); if (mt == null) { return; // shouldn't happen } if (mt.SummarizedExists) // Prompt user for summarization-level choice { FocusedMetaTable = mt; Point p = Form.MousePosition; p = ContentsTreeWithSearch.ContentsTreeCtl.PointToClient(p); SummarizationLevelContextMenu.Show(ContentsTreeWithSearch.ContentsTreeCtl, p); return; } } SelectionName.Text = node.Label; SelectionTarget.Text = node.Target; // If item is double clicked then we're done MouseEventArgs ma = ContentsTreeWithSearch.CurrentContentsTreeMouseDownEvent; if (ma != null && ma.Clicks >= 2) { DialogResult = DialogResult.OK; } return; }
/// <summary> /// Get list of Tables that underlie the CalcTable in the form of a prototype Query /// </summary> /// <param name="qt"></param> /// <returns></returns> Query GetUnderlyingTables(QueryTable qt) { QueryTable qt2; MetaTable mt2; string mtName, alias, tok; Query q2 = new Query(); string[] l1 = qt.MetaTable.TableMap.Split(','); foreach (string s in l1) { tok = s.Trim(); if (tok.Contains(" ")) { string[] sa = tok.Split(' '); mtName = sa[0].Trim(); alias = sa[1].Trim(); } else { mtName = tok; alias = tok; } mt2 = MetaTableCollection.Get(mtName); if (mt2 == null) { throw new Exception("Metatable not found: " + mtName); } qt2 = new QueryTable(mt2); qt2.SelectKeyOnly(); qt2.Alias = alias; q2.AddQueryTable(qt2); } return(q2); }
/// <summary> /// Get list list of ResultsViewModels /// </summary> /// <returns></returns> public static List <ResultsViewModel> GetResultsViewModels() { string viewTypeName, viewSubtype, imageName, viewTitle; int imageIdx; if (ResultsViewModels != null) { return(ResultsViewModels); } ResultsViewModels = new List <ResultsViewModel>(); MetaTable mt = MetaTableCollection.Get("AddResultsViewMenu"); if (mt == null) { DebugLog.Message("AddResultsViewMenu MetaTable not found"); return(null); } foreach (MetaColumn mc in mt.MetaColumns) { ResultsViewModel m = new ResultsViewModel(); m.Name = mc.Name; m.Title = mc.Label; m.Description = mc.Description; m.ShowInViewsMenu = (mc.InitialSelection == ColumnSelectionEnum.Selected); Lex.Split(mc.ColumnMap, ",", out viewTypeName, out viewSubtype, out imageName); Enum.TryParse <ResultsViewType>(viewTypeName, true, out m.ViewType); m.ViewSubtype = viewSubtype; m.CustomViewTypeImageName = imageName; ResultsViewModels.Add(m); } return(ResultsViewModels); }
/// <summary> /// ContentsTreeItemSelected /// </summary> /// <param name="nodeTarget"></param> private void ContentsTreeItemSelected(string nodeTarget) { //MetaTreeNode node = ContentsTreeWithSearch.ContentsTree.GetMetaTreeNodeAt(ContentsTreeWithSearch.ContentsTree.PointToClient(Cursor.Position)); //if (node == null || !node.IsDataTableType) return; string mtName = nodeTarget; MetaTable mt = MetaTableCollection.Get(mtName); if (mt == null) { return; } if (mt.UseSummarizedData || !mt.SummarizedExists) { RenderTable(mt, null); // use table as selected } else // select table based on desired summarization level { if (mt.SummarizedExists && SelectSummarizedDataByDefault) { string mtName2 = mtName += MetaTable.SummarySuffix; MetaTable mt2 = MetaTableCollection.Get(mtName2); if (mt2 != null) { RenderTable(mt2, null); // use table as selected return; } } // Prompt user for summarization-level choice FocusedMetaTable = mt; Point p = Form.MousePosition; p = ContentsTreeWithSearch.ContentsTreeCtl.PointToClient(p); SummarizationLevelContextMenu.Show(ContentsTreeWithSearch.ContentsTreeCtl, p); } }
public Query BuildStructureSearchQuery() { MetaTable mt = MetaTableCollection.Get(MetaTable.SmallWorldMetaTableName); if (mt == null) { DebugLog.Message("SmallWorld MetaTable not found"); MoleculeGridPageControl.Visible = false; return(null); } Query q = new Query(); // build single-table query q.SingleStepExecution = true; QueryTable qt = new QueryTable(q, mt); string title = "SmallWorld"; qt.Label = title; q.UserObject.Name = qt.MetaTable.Label; // need? return(q); }
/// <summary> /// Parse metatable & metacolumn /// </summary> /// <param name="mtName"></param> /// <param name="mt"></param> /// <param name="mcName"></param> /// <param name="mc"></param> static void ParseMetaTableMetaColumn( string mtName, out MetaTable mt, string mcName, out MetaColumn mc) { mt = null; mc = null; mt = MetaTableCollection.Get(mtName); if (mt == null) { throw new Exception("Can't find metatable: " + mtName); } mc = mt.GetMetaColumnByName(mcName); if (mc == null) { throw new Exception("Can't find metacolumn: " + mcName); } return; }
/// <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; string sql; int methodId, i1; int t0 = TimeOfDay.Milliseconds(); List <string> normalizedResultKeys = new List <string>(); for (i1 = 0; i1 < resultKeys.Count; i1++) // copy keys to parameter array properly normalized { string key = CompoundId.NormalizeForDatabase(resultKeys[i1], qt.MetaTable); if (key == null) { key = NullValue.NullNumber.ToString(); // if fails supply a "null" numeric value } normalizedResultKeys.Add(key); } sql = // todo: Make to work in general case (PubChem only now) "select mthd_vrsn_id " + "from " + "mbs_owner.mbs_pbchm_rslt" + " " + "where ext_cmpnd_id_nbr in (<list>) " + " and sts_id = 1 " + // active records only "group by mthd_vrsn_id"; DbCommandMx drd = new DbCommandMx(); drd.PrepareListReader(sql, DbType.Int32); drd.ExecuteListReader(normalizedResultKeys); if (drd.Cancelled) { // todo qe.Cancelled = true; drd.Dispose(); return; } Hashtable mtHash = new Hashtable(); int methodIdCount = 0; while (true) // convert list of methods to set of metatable names { if (!drd.ListRead()) { break; } methodId = drd.GetInt(0); string mtName = "pubchem_aid_" + methodId.ToString(); // todo: Make to work in general case (PubChem only now) if (QueryEngine.FilterAllDataQueriesByDatabaseContents && !MetaTableCollection.IsMetaTableInContents(mtName)) { continue; // metatable must be in contents } if (qt.MetaTable.UseSummarizedData) { mtName += MetaTable.SummarySuffix; } mtHash[mtName] = null; methodIdCount++; } drd.Dispose(); if (drd.Cancelled) { // todo qe.Cancelled = true; return; } ArrayList mtList = new ArrayList(); foreach (string mtName2 in mtHash.Keys) { // put metatable labels & names into a list for sorting mt2 = MetaTableCollection.Get(mtName2); if (mt2 == null) { continue; } if (mt2.Parent == null) { continue; // skip if no parent } mtList.Add(mt2.Label.ToLower().PadRight(64) + "\t" + mt2.Name); } mtList.Sort(); foreach (string mts in mtList) { // add new querytables/metatables to query string[] sa = mts.Split('\t'); mt2 = MetaTableCollection.Get(sa[1]); if (mt2 == null) { continue; } qt2 = new QueryTable(q, mt2); if (qt.HeaderBackgroundColor != Color.Empty) { qt2.HeaderBackgroundColor = qt.HeaderBackgroundColor; } } t0 = TimeOfDay.Milliseconds() - t0; return; }
/// <summary> /// Update Common Assay Attributes table (mbs_owner.cmn_assy_atrbts) /// /// Command: Update AssayAttributesTable /// /// This command builds an entry (or two) in the cmn_assy_atrbts table for each assay /// referenced in the Mobius contents tree that reports an SP or CRC value as determined /// by the associated metafactory and available in each metatable. /// If the gene target associated with an assay can be identified then information on that /// gene is included as well. /// /// Additional gene information may come from the metadata for a source such as the results /// warehouse. /// /// Note that this function must be run under an account that has access to all restricted data so that it /// can properly see what's available and build the table. /// </summary> /// <param name="lex"></param> /// <returns></returns> public static string UpdateAssayAttributesTable(string args) { MetaTable mt; MetaColumn mc; AssayAttributes aa, aa2; Dictionary <int, int> geneIdCounts = new Dictionary <int, int>(); // genes and counts keyed by entrez gene id Dictionary <int, double> targetMapXDict; // dendogram X coord keyed by gene id Dictionary <int, double> targetMapYDict; // dendogram Y coord keyed by gene id Dictionary <string, int> targetTypeCounts = new Dictionary <string, int>(); // target types and counts Dictionary <int, string> assayTypeDict; Dictionary <int, string> assayModeDict; //UnpivotedAssayResult rr, rr2; List <string> toks; string mtName, tableNamePrefix, key, fileName, msg; bool crcExists, spExists, isSummary; int tableId, step, ri, resultTypeId, assayRowCount; Log = new LogFile(ServicesDirs.LogDir + @"\UpdateCommonAssayAttributes.log"); Log.ResetFile(); // Get list of all of the assays in the tree LogMessage("Accumulating assays..."); HashSet <string> mtNameHash = new HashSet <string>(); foreach (MetaTreeNode mtn0 in MetaTree.Nodes.Values) { if (mtn0.Type != MetaTreeNodeType.MetaTable) { continue; } mtName = mtn0.Target; if (AssayMetaData.IsAssayMetaTableName(mtName)) { mtNameHash.Add(mtName); } } bool debug = false; // set to true to debug with limited list of assays from below if (debug) { mtNameHash.Clear(); //assayHash.Add("ASSAY_1"); //assayHash.Add("ASSAY_2"); //assayHash.Add("ASSAY_3"); } // Get other informatin needed from AssayMetadata LogMessage("Reading AssayMetadata ASSAY metadata..."); Dictionary <int, AssayDbMetadata> assayMetadataAssayDict = // get assays and associated target/gene information AssayMetadataDao.GetAssayTargetGeneData(); LogMessage("Getting AssayMetadata result types..."); Dictionary <int, AssayDbResultType> resultTypeDict = AssayMetadataDao.GetResultTypeDict(); LogMessage("Getting assay types and modes..."); AssayMetadataDao.GetAssayTypesAndModes(out assayTypeDict, out assayModeDict); LogMessage("Getting gene dendogram coordinates..."); try { AssayMetadataDao.GetImageCoords(out targetMapXDict, out targetMapYDict); } catch (Exception ex) // may fail if problem with data source { LogMessage(DebugLog.FormatExceptionMessage(ex, true)); targetMapXDict = new Dictionary <int, double>(); targetMapYDict = new Dictionary <int, double>(); } // Process each assay int metatablesFound = 0, metatablesNotFound = 0; int assaysProcessed = 0; int assaysWithGenes = 0; int assaysWithGeneCoords = 0; int assaysWithTargets = 0; int assaysWithSpOnly = 0; int assaysWithCrcOnly = 0; int assaysWithNoCrcSP = 0; int assaysWithOtherTypes = 0; int assaysWithCrcAndSp = 0; int assaysWithNoKeyTypes = 0; int assaysWithProcessingErrors = 0; Dictionary <string, int> CrcAssayCnt = new Dictionary <string, int>() { { "ASSAY_DB1", 0 }, { "ASSAY_DB2", 0 }, { "ASSAY_DB3", 0 } }; Dictionary <string, int> SpAssayCnt = new Dictionary <string, int>() { { "ASSAY_DB1", 0 }, { "ASSAY_DB2", 0 }, { "ASSAY_DB3", 0 } }; Dictionary <string, int> OtherAssayCnt = new Dictionary <string, int>() { { "ASSAY_DB1", 0 }, { "ASSAY_DB2", 0 }, { "ASSAY_DB3", 0 } }; List <AssayAttributes> resultTypeRows = new List <AssayAttributes>(); List <AssayAttributes> geneRows = new List <AssayAttributes>(); List <AssayAttributes> dbRows = new List <AssayAttributes>(); string copyUpdateMsg = ""; foreach (string mtName0 in mtNameHash) { AssayMetaData assayMetaData = null; // metadata for assay bool isAssay = false; int assayIdNbr = NullValue.NullNumber; int assayId = NullValue.NullNumber; //if (assaysProcessed >= 100) break; // debug mtName = mtName0; MetaTable.ParseMetaTableName(mtName, out tableNamePrefix, out tableId, out isSummary); string resultType = ""; string rtId = ""; string assayName, assayDb; mt = MetaTableCollection.Get(mtName); // get metatable if (mt == null) { metatablesNotFound++; LogMessage("MetaTable not found: " + mtName); continue; } metatablesFound++; if (mt.Code == "") { continue; // must be single pivoted assay } assayDb = "ASSAY_DB"; // customize assayIdNbr = -1; if (UalUtil.IClient != null && UalUtil.IClient.Attended) { UAL.Progress.Show((assaysProcessed + 1).ToString() + " / " + mtNameHash.Count + " - " + mt.Name + "\r\n" + mt.Label); } aa = new AssayAttributes(); aa.AssayDatabase = assayDb; aa.AssayIdNbr = assayIdNbr; // data-source-specific assay Id aa.AssayIdTxt = mt.Name; // store ASSAY_1234 type table name aa.AssayId2 = assayId; // any associated assay id if (isAssay) { aa.AssayName = assayMetaData.Name; // name from AssayMetadata } else { aa.AssayName = MetaTable.RemoveSuffixesFromName(mt.Label); // name from metatable } if (mt.SummarizedExists) { aa.SummarizedAvailable = true; } else { aa.SummarizedAvailable = false; } if (isAssay) { if (assayTypeDict.ContainsKey(tableId)) { aa.AssayType = assayTypeDict[tableId]; } if (assayModeDict.ContainsKey(tableId)) { aa.AssayMode = assayModeDict[tableId]; } aa.AssaySource = AssayMetaData.GetAssaySource(tableId); aa.AssociationSource = "TODO"; // customize } aa.AssayStatus = "Active"; // say all active for now MetaTableStats mts = MetaTableFactory.GetStats(mtName); if (mts != null) { aa.ResultCount = (int)mts.RowCount; aa.AssayUpdateDate = mts.UpdateDateTime; } else { aa.ResultCount = 0; // assume no results if no stats } if (mt.DescriptionIsAvailable()) // use description from Mobius { aa.AssayDesc = "Y"; } if (String.IsNullOrEmpty(aa.GeneFamily)) { aa.GeneFamily = "Unknown"; // set these to "Unknown" rather than null } if (String.IsNullOrEmpty(aa.AssayType)) { aa.AssayType = "UNKNOWN"; // upper case UNKNOWN } if (String.IsNullOrEmpty(aa.AssayMode)) { aa.AssayMode = "UNKNOWN"; // upper case UNKNOWN } // Step1: Add a row for primary & any secondary results resultTypeRows.Clear(); MetaColumn firstResultCol = null, firstKeyResultCol = null, firstOtherKeyResultCol = null; string resultTypeConcType; HashSet <string> keyResultTypeCodes = new HashSet <string>(); int spCnt = 0, crcCnt = 0, otherCnt = 0; for (int mci = 0; mci < mt.MetaColumns.Count; mci++) // pick first col with result code (could also check summarization method) { mc = mt.MetaColumns[mci]; if (Lex.IsUndefined(mc.ResultCode)) { continue; // must have code defined } if (keyResultTypeCodes.Contains(mc.ResultCode)) { continue; // and not included so far } if (mc.InitialSelection != ColumnSelectionEnum.Selected) { continue; // selected only } if (firstResultCol == null) { firstResultCol = mc; } if (!IsKeyResultType(mc, out resultTypeConcType)) { continue; } if (firstKeyResultCol == null) { firstKeyResultCol = mc; } keyResultTypeCodes.Add(mc.ResultCode); aa2 = aa.Clone(); if (resultTypeRows.Count == 0) { aa2.TopLevelResult = "Y"; } else { aa2.TopLevelResult = "N"; } aa2.ResultTypeId2 = GetAssayResultTypeId(mc); // AssayMetadata result type id aa2.ResultTypeIdNbr = GetInternalResultTypeId(mc); // Internal database result type id aa2.ResultTypeIdTxt = mc.Name; // Mobius column name if (isAssay && resultTypeDict.ContainsKey(aa2.ResultTypeId2)) { aa2.ResultName = resultTypeDict[aa2.ResultTypeId2].Name; // use name from AssayMetadata result type dict } else { aa2.ResultName = mc.Label; // use label from Mobius } aa2.ResultTypeUnits = mc.Units; // result units if (Lex.Eq(resultTypeConcType, "SP")) { aa2.ResultTypeConcType = "SP"; spCnt++; } else if (Lex.Eq(resultTypeConcType, "CRC")) { aa2.ResultTypeConcType = "CRC"; crcCnt++; } else { aa2.ResultTypeConcType = ""; otherCnt++; if (firstOtherKeyResultCol == null) { firstOtherKeyResultCol = mc; } } aa2.ResultTypeConcUnits = ""; // todo resultTypeRows.Add(aa2); } if (resultTypeRows.Count >= 1) { if (crcCnt > 0) { CrcAssayCnt[assayDb]++; // count primary type by db } else if (spCnt > 0) { SpAssayCnt[assayDb]++; } else { OtherAssayCnt[assayDb]++; } if (crcCnt > 0 && spCnt == 0) { assaysWithCrcOnly++; // count overall primary/secondary types } else if (crcCnt == 0 && spCnt > 0) { assaysWithSpOnly++; } else if (crcCnt > 0 && spCnt > 0) { assaysWithCrcAndSp++; } if (crcCnt == 0 && spCnt == 0) // no SP or CRC result types { assaysWithNoCrcSP++; mc = firstKeyResultCol; LogMessage("Assay with No SP/CRC key results: " + mt.Name + "." + mc.Name + " (" + mc.ResultCode + "), " + mt.Label + "." + mc.Label); } else if (otherCnt > 0) // no SP or CRC result types { assaysWithOtherTypes++; mc = firstOtherKeyResultCol; LogMessage("Non SP/CRC key result: " + mt.Name + "." + mc.Name + " (" + mc.ResultCode + "), " + mt.Label + "." + mc.Label); } } else // no key result type { aa2 = aa.Clone(); resultTypeRows.Add(aa2); // include row for step1 OtherAssayCnt[assayDb]++; assaysWithNoKeyTypes++; LogMessage("No key result type for metatable: " + mt.Name + ", " + mt.Label); } // Build a step2 row for each target/gene geneRows.Clear(); List <AssayTarget> targets = new List <AssayTarget>(); int geneCount = 0; if (isAssay) { targets = assayMetaData.Targets; } if (targets.Count > 0) { assaysWithTargets++; } foreach (AssayTarget target in targets) { aa = new AssayAttributes(); aa.GeneFamily = target.TargetTypeShortName; // count target type occurance if (Lex.IsUndefined(aa.GeneFamily)) { aa.GeneFamily = "Unknown"; } if (!targetTypeCounts.ContainsKey(aa.GeneFamily)) { targetTypeCounts[aa.GeneFamily] = 0; } targetTypeCounts[aa.GeneFamily]++; if (target.Genes == null || target.Genes.Count == 0) // if no genes add a single target row { geneRows.Add(aa); continue; } foreach (AssayGene rg in target.Genes) { if (!Lex.IsDefined(rg.GeneSymbol)) { continue; } aa2 = aa.Clone(); geneRows.Add(aa2); aa2.GeneSymbol = rg.GeneSymbol; int.TryParse(rg.GeneId, out aa2.GeneId); if (aa2.GeneId > 0 && targetMapXDict.ContainsKey(aa2.GeneId)) { aa2.TargetMapX = targetMapXDict[aa2.GeneId]; aa2.TargetMapY = targetMapYDict[aa2.GeneId]; if (geneCount == 0) { assaysWithGeneCoords++; } } if (!geneIdCounts.ContainsKey(aa2.GeneId)) // count gene occurance { geneIdCounts[aa2.GeneId] = 0; } geneIdCounts[aa2.GeneId]++; if (geneCount == 0) { assaysWithGenes++; } geneCount++; } } if (geneRows.Count == 0) // if no step 2 rows (i.e. no targets), create a single step2 row { aa = new AssayAttributes(); geneRows.Add(aa); } // Combine key result types with target/genes for (int i1 = 0; i1 < resultTypeRows.Count; i1++) { AssayAttributes s1aa = resultTypeRows[i1]; for (int i2 = 0; i2 < geneRows.Count; i2++) { AssayAttributes s2aa = geneRows[i2]; aa = s1aa.Clone(); aa.GeneId = s2aa.GeneId; aa.GeneSymbol = s2aa.GeneSymbol; aa.GeneFamily = s2aa.GeneFamily; aa.TargetMapX = s2aa.TargetMapX; aa.TargetMapY = s2aa.TargetMapY; aa.GeneCount = geneCount; if (i2 > 0) { aa.GeneCount = -geneCount; // negative for other than 1st gene } dbRows.Add(aa); } } assaysProcessed++; } // Update table bool updateTable = true; // set to false for debug if (dbRows.Count <= 0) { LogMessage("No rows in new dataset, table not updated"); } else if (updateTable) { LogMessage("Deleting existing data..."); DbCommandMx dao = new DbCommandMx(); string sql = "delete from mbs_owner.cmn_assy_atrbts"; sql = AssayAttributesDao.AdjustAssayAttrsTableName(sql); dao.Prepare(sql); dao.BeginTransaction(); int delCnt = dao.ExecuteNonReader(); LogMessage("Inserting new data..."); int t0 = TimeOfDay.Milliseconds(); for (ri = 0; ri < dbRows.Count; ri++) { aa = dbRows[ri]; aa.Id = ri + 1; //aa.Id += 10000; // debug if (aa.GeneSymbol != null) { aa.GeneSymbol = aa.GeneSymbol.ToUpper(); // be sure key match cols are upper case } if (aa.GeneFamily != null) { aa.GeneFamily = aa.GeneFamily.ToUpper(); } if (aa.GeneFamilyTargetSymbol != null) { aa.GeneFamilyTargetSymbol = aa.GeneFamilyTargetSymbol.ToUpper(); } if (aa.ResultTypeConcType != null) { aa.ResultTypeConcType = aa.ResultTypeConcType.ToUpper(); } if (aa.AssayType != null) { aa.AssayType = aa.AssayType.ToUpper(); } if (aa.AssayMode != null) { aa.AssayMode = aa.AssayMode.ToUpper(); } AssayAttributesDao.InsertCommonAssayAttributes(aa, dao); if (TimeOfDay.Milliseconds() - t0 > 1000) { //Progress.Show("Inserting new data " + (ri + 1) + "/" + rows.Count + "..."); t0 = TimeOfDay.Milliseconds(); } } dao.Commit(); dao.Dispose(); copyUpdateMsg = UpdateCmnAssyAtrbtsCopies(); } string response = "----------------------------------\r\n" + "Assays processed: " + assaysProcessed + "\r\n" + "Assays with processing errors: " + assaysWithProcessingErrors + "\r\n" + "Rows inserted: " + dbRows.Count + "\r\n" + copyUpdateMsg + "----------------------------------\r\n" + "Assays with CRC only: " + assaysWithCrcOnly + "\r\n" + "Assays with SP only: " + assaysWithSpOnly + "\r\n" + "Assays with CRC and SP: " + assaysWithCrcAndSp + "\r\n" + "Assays with no CRC or SP: " + assaysWithNoCrcSP + "\r\n" + "Assays with non CRC/SP key types: " + assaysWithOtherTypes + "\r\n" + "Assays with no key types: " + assaysWithNoKeyTypes + "\r\n" + "----------------------------------\r\n" + "Assays with targets defined: " + assaysWithTargets + "\r\n" + "Assays with genes defined: " + assaysWithGenes + "\r\n" + "Assays with gene map coordinates: " + assaysWithGeneCoords + "\r\n" + "----------------------------------\r\n" + //"CRC Assays: " + CrcAssayCnt["ASSAY"] + "\r\n" + //"SP Assays: " + SpAssayCnt["ASSAY"] + "\r\n" + //"??? Assays: " + OtherAssayCnt["ASSAY"] + "\r\n" + "----------------------------------"; LogMessage("\r\n" + response); UAL.Progress.Hide(); return(response); }
/// <summary> /// Create an annotation table from a DataTable /// </summary> /// <param name="fullyQualifiedName">Fully qualified name to assign to table</param> /// <param name="dataTable">DataTable containing table definition & data</param> /// <param name="showProgress">Display dialog box showing progress of creation</param> /// <returns>Internal name assigned to annotation table (ANNOTATION_12345)</returns> public static MetaTable CreateAnnotationTable( string fullyQualifiedName, DataTable dataTable, bool showProgress) { List <AnnotationVo> voList = new List <AnnotationVo>(); AnnotationVo avo; if (dataTable == null) { DebugMx.ArgException("DataTable is null"); } if (dataTable.Columns.Count == 0) { DebugMx.ArgException("No DataColumns are defined"); } if (dataTable.Columns[0].DataType != typeof(CompoundId)) { DebugMx.ArgException("The first column must be of type CompoundId"); } if (showProgress) { Progress.Show("Creating annotation table..."); } AnnotationDao aDao = new AnnotationDao(); UserObject auo = UserObjectUtil.ParseInternalUserObjectName(fullyQualifiedName); auo.Type = UserObjectType.Annotation; UserObjectTree.GetValidUserObjectTypeFolder(auo); UserObject auo2 = UserObjectDao.Read(auo); // see if there already MetaTable oldMt = null; if (auo2 == null) // get new identifier { auo.Id = UserObjectDao.GetNextId(); // id to store table under } else // reuse identifier { auo.Id = auo2.Id; // copy it over aDao.DeleteTable(auo.Id); // delete any existing data string oldMtName = "ANNOTATION_" + auo2.Id; oldMt = MetaTableCollection.Get(oldMtName); } MetaTable mt = new MetaTable(); mt.MetaBrokerType = MetaBrokerType.Annotation; mt.Name = "ANNOTATION_" + auo.Id; // name table by uo mt.Label = auo.Name; mt.Code = auo.Id.ToString(); // code for the metatable int mtCode = auo.Id; if (dataTable.ExtendedProperties.ContainsKey("ParentTableName")) { mt.Parent = MetaTableCollection.Get((string)dataTable.ExtendedProperties["ParentTableName"]); } foreach (DataColumn dc in dataTable.Columns) { MetaColumn mc = new MetaColumn(); mc.MetaTable = mt; mc.Name = dc.ColumnName; MetaColumn oldMc = null; if (oldMt != null) { oldMc = oldMt.GetMetaColumnByName(mc.Name); // see if column name exists } if (oldMc != null && oldMc.ResultCode != "") // use any existing code { mc.ResultCode = oldMc.ResultCode; } else { mc.ResultCode = aDao.GetNextIdLong().ToString(); } if (dc.Caption != null) { mc.Label = dc.Caption; } else { mc.Label = mc.Name; } if (dc.DataType == typeof(CompoundId)) { mc.DataType = MetaColumnType.CompoundId; if (dc.ExtendedProperties.ContainsKey("StorageType") && dc.ExtendedProperties["StorageType"] is MetaColumnType && ((MetaColumnType)dc.ExtendedProperties["StorageType"]) == MetaColumnType.String) { mc.ColumnMap = "EXT_CMPND_ID_TXT"; // text column } else { mc.ColumnMap = "EXT_CMPND_ID_NBR"; // numeric column otherwise } } else if (dc.DataType == typeof(int) || dc.DataType == typeof(Int16) || dc.DataType == typeof(Int32)) { mc.DataType = MetaColumnType.Integer; } else if (dc.DataType == typeof(float) || dc.DataType == typeof(double)) { mc.DataType = MetaColumnType.Number; } else if (dc.DataType == typeof(QualifiedNumber)) { mc.DataType = MetaColumnType.QualifiedNo; } else if (dc.DataType == typeof(string)) { mc.DataType = MetaColumnType.String; } else if (dc.DataType == typeof(DateTime)) { mc.DataType = MetaColumnType.Date; } else if (dc.DataType == typeof(MoleculeMx)) { mc.DataType = MetaColumnType.Structure; } else { throw new Exception("Invalid data type " + dc.DataType.ToString()); } if (dc.ExtendedProperties.ContainsKey("DisplayLevel")) { mc.InitialSelection = (ColumnSelectionEnum)dc.ExtendedProperties["DisplayLevel"]; } if (dc.ExtendedProperties.ContainsKey("DisplayWidth")) { mc.Width = (float)dc.ExtendedProperties["DisplayWidth"]; } if (dc.ExtendedProperties.ContainsKey("DisplayFormat")) { mc.Format = (ColumnFormatEnum)dc.ExtendedProperties["DisplayFormat"]; } if (dc.ExtendedProperties.ContainsKey("Decimals")) { mc.Decimals = (int)dc.ExtendedProperties["Decimals"]; } mt.MetaColumns.Add(mc); } ToolHelper.CreateAnnotationTable(mt, auo); aDao.BeginTransaction(); // insert all data in single transaction if (showProgress) { Progress.Show("Writing data to annotation table..."); } int t1 = TimeOfDay.Milliseconds(); int writeCount = 0; foreach (DataRow dr in dataTable.Rows) { if (dr.IsNull(0)) { continue; // shouldn't happen } string key = dr[0].ToString(); key = CompoundId.NormalizeForDatabase(key, mt.Root); long rslt_grp_id = aDao.GetNextIdLong(); for (int ci = 1; ci < dataTable.Columns.Count; ci++) // do columns after key { if (dr.IsNull(ci)) { continue; } DataColumn dc = dataTable.Columns[ci]; MetaColumn mc = mt.MetaColumns[ci]; int mcCode = Int32.Parse(mc.ResultCode); avo = new AnnotationVo(); avo.rslt_grp_id = rslt_grp_id; // keep row together if (dc.DataType == typeof(CompoundId)) // shouldn't happen since key processed already { avo.rslt_val_txt = dr[ci].ToString(); } else if (dc.DataType == typeof(int) || dc.DataType == typeof(Int16) || dc.DataType == typeof(Int32) || dc.DataType == typeof(float) || dc.DataType == typeof(double)) { avo.rslt_val_nbr = (double)dr[ci]; } else if (dc.DataType == typeof(QualifiedNumber)) { QualifiedNumber qn = (QualifiedNumber)dr[ci]; avo.rslt_val_nbr = qn.NumberValue; avo.rslt_val_prfx_txt = qn.Qualifier; avo.rslt_val_txt = qn.TextValue; avo.dc_lnk = qn.Hyperlink; } else if (dc.DataType == typeof(string)) { avo.rslt_val_txt = dr[ci].ToString(); } else if (dc.DataType == typeof(DateTime)) { avo.rslt_val_dt = (DateTime)dr[ci]; } else if (dc.DataType == typeof(MoleculeMx)) { avo.rslt_val_txt = dr[ci].ToString(); } AddAnnotationVoToList(avo, key, mtCode, mcCode, voList); } writeCount++; if (Progress.CancelRequested) // check for cancel { aDao.Commit(); aDao.Insert(voList); voList.Clear(); aDao.Commit(); aDao.Dispose(); if (showProgress) { Progress.Hide(); } MessageBoxMx.ShowError("Writing of annotation table cancelled."); return(null); } int t2 = TimeOfDay.Milliseconds(); if (showProgress && t2 - t1 >= 1000) { t1 = t2; Progress.Show("Writing data to annotation table " + writeCount.ToString() + " of " + dataTable.Rows.Count.ToString() + " ..."); aDao.Insert(voList); voList.Clear(); aDao.Commit(); } } aDao.Insert(voList); voList.Clear(); aDao.Commit(); aDao.Dispose(); if (showProgress) { Progress.Hide(); } return(mt); // return metatable name }
/// <summary> /// (OLD VERSION) /// Get list of compounds whose fragments match those of the compounds in the list. /// </summary> /// <param name="cnList"></param> /// <returns></returns> public static Dictionary <string, List <string> > GetAllSaltFormsNew( List <string> cnList) { int t0, t1; t0 = TimeOfDay.Milliseconds(); Dictionary <string, List <string> > cidDict = new Dictionary <string, List <string> >(); List <string> cnList2 = new List <string>(); foreach (string s in cnList) { // get just the list entries that are integers (e.g. remove MFCD numbers) if (Lex.IsInteger(s)) { cnList2.Add(s); } } t1 = TimeOfDay.Milliseconds() - t0; if (cnList2.Count == 0) { return(cidDict); } //MetaTable mt = MetaTableCollection.Get("frag_occurrence"); MetaTable mt = MetaTableCollection.Get("CorpId_salt_isomer_info"); if (mt == null) { return(cidDict); } string sql = mt.TableMap; // get sql to use from metatable if (sql.StartsWith("(")) { sql = sql.Substring(1, sql.Length - 2); // remove surround parens if necessary } sql = Lex.Replace(sql, "where", "where CorpId in (<list>) and "); // add criteria needed to do list search DbCommandMx drd = new DbCommandMx(); try { drd.PrepareListReader(sql, DbType.Int32); drd.ExecuteListReader(cnList2); if (drd.Cancelled) { drd.Dispose(); return(null); } while (true) { if (!drd.ListRead()) { break; } string cn = CompoundId.Normalize(drd.GetInt(0).ToString()); string cn2 = CompoundId.Normalize(drd.GetInt(1).ToString()); if (!cidDict.ContainsKey(cn)) { cidDict[cn] = new List <string>(); } List <string> al = cidDict[cn]; if (al.Count == 0 || al[al.Count - 1] != cn2) // add if not dup { al.Add(cn2); } } drd.Dispose(); } catch (Exception ex) { // catch case non-numeric item in list, single-row subquery returns more than one row, etc. drd.Dispose(); return(new Dictionary <string, List <string> >()); } t1 = TimeOfDay.Milliseconds() - t0; return(cidDict); }
/// <summary> /// Select a Molecule object for a compound id /// </summary> /// <param name="cid"></param> /// <param name="mt"></param> /// <returns></returns> public static MoleculeMx SelectMoleculeForCid( string cid, MetaTable mt = null) { MetaColumn strMc = null; string mtName = null, keyColName, strColExpr, chimeString; MoleculeMx cs; if (cid == null || cid == "") { return(null); } if (RestrictedDatabaseView.KeyIsRetricted(cid)) { return(null); } //if (mt != null) mtName = mt.Name; // debug bool isUcdb = (mt != null && mt.Root.IsUserDatabaseStructureTable); // user compound database if (isUcdb) // if root metatable is user database then normalize based on key { mt = mt.Root; // be sure we have root cid = CompoundId.Normalize(cid, mt); } else { cid = CompoundId.Normalize(cid, mt); cs = MoleculeCache.Get(cid); // see if in cache if (cs != null) { return(cs); } mt = CompoundId.GetRootMetaTableFromCid(cid, mt); if (mt != null && Lex.Eq(mt.Name, "corp_structure") && MetaTableCollection.Get("corp_structure2") != null) { mt = MetaTableCollection.Get("corp_structure2"); // hack to search both small & large mols for Corp database } } if (mt == null) { return(null); } strMc = mt.FirstStructureMetaColumn; //.getmt.GetMetaColumnByName("molstructure"); if (strMc == null) { return(null); } cid = CompoundId.NormalizeForDatabase(cid, mt); if (String.IsNullOrEmpty(cid)) { return(null); } // Call external method to select structure if (strMc.ColumnMap.StartsWith("InternalMethod", StringComparison.OrdinalIgnoreCase) || strMc.ColumnMap.StartsWith("PluginMethod", StringComparison.OrdinalIgnoreCase)) { // call external method to get structure List <MetaColumn> selectList = new List <MetaColumn>(); selectList.Add(mt.KeyMetaColumn); selectList.Add(strMc); object[] vo = new object[2]; vo[0] = cid; try { GenericMetaBroker.CallLateBindMethodToFillVo(vo, 1, mt, selectList); } catch (Exception ex) { return(null); } if (vo[1] is MoleculeMx) { cs = (MoleculeMx)vo[1]; } else if (vo[1] is string) { cs = MoleculeMx.MolStringToMoleculeMx((string)vo[1]); } else { cs = null; } if (!isUcdb) { MoleculeCache.AddMolecule(cid, cs); } return(cs); } // Normal case //if (HelmEnabled) // temp till server back //{ // cs = new MoleculeMx(); // MoleculeMx.SetMoleculeToTestHelmString(cid, cs); // return cs; //} string tableMap = mt.GetTableMapWithAliasAppendedIfNeeded(); // some SQL (e.g. Postgres) requires an alias for subqueries) strColExpr = strMc.ColumnMap; if (strColExpr == "") { strColExpr = strMc.Name; } if (MqlUtil.IsCartridgeMetaTable(mt)) // selecting from Direct cartridge { if (!Lex.Contains(tableMap, "chime(")) // if no chime expression { strColExpr = "chime(ctab)"; // then create one (gets clob) } strColExpr += ", chime(ctab)"; // add 2nd column that gets clob in case first just gets first 4000 characters } keyColName = mt.KeyMetaColumn.ColumnMap; if (keyColName == "") { keyColName = mt.KeyMetaColumn.Name; } DbType parmType = DbType.String; object cidObj = cid; if (mt.KeyMetaColumn.IsNumeric) { if (!Lex.IsInteger(cid)) { return(null); // if numeric col be sure cid is numeric also } parmType = DbType.Int64; cidObj = Int64.Parse(cid); } string sql = "select " + strColExpr + " " + "from " + tableMap + " " + "where " + keyColName + " = :0"; DbCommandMx drd = new DbCommandMx(); try { drd.PrepareParameterized(sql, parmType); drd.ExecuteReader(cidObj); if (!drd.Read() || drd.Rdr.IsDBNull(0)) { drd.Dispose(); return(null); } string molString = drd.GetString(0); drd.Dispose(); MoleculeMx.TrySetStructureFormatPrefix(ref molString, strMc.DataTransform); // add molString type if indicated by data transform cs = MoleculeMx.MolStringToMoleculeMx(molString); cs.StoreKeyValueInMolComments(strMc, cid); if (!isUcdb) { MoleculeCache.AddMolecule(cid, cs); } //if (MoleculeMx.HelmEnabled == true && Lex.IsInteger(cid)) // MoleculeMx.SetMoleculeToTestHelmString(cid, cs); return(cs); } catch (Exception ex) { // just log message & return; DebugLog.Message("SelectMoleculeForCid Exception, Cid: " + cid + ", table: " + mt.Name + "\n" + "sql: " + OracleMx.FormatSql(sql) + "\n" + DebugLog.FormatExceptionMessage(ex)); if (drd != null) { drd.Dispose(); } return(null); } }
/// <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 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> /// See if a ClickFunction command & process if so /// </summary> /// <param name="command"></param> /// <param name="qm"></param> /// <param name="cInf"></param> public static void Process( string command, QueryManager qm, CellInfo cInf = null) { QueryTable rootQt, qt; QueryColumn qc; MetaTable mt; MetaColumn mc; Query q2; string dbName = "", mtName = "", mcName = ""; List <string> args0, args; string funcName, arg1, arg2, arg3, arg4, arg5; string value = "", keyValue = ""; int ai; try { // Parse click function arguments stripping all single quotes. // Arguments may be defined in the clickfunction definition including col values // indicated by field.Value in the metacolumn clickfunction definition. // If no args are defined in the clickfunction definition then a field value // argument will be added by default or the keyValue if [keyvalue] appears in the // ClickFunction definition CurrentClickQueryManager = qm; args0 = Lex.ParseAllExcludingDelimiters(command, "( , )", false); args = new List <string>(); for (ai = 0; ai < args0.Count; ai++) // strip all single quotes { string arg = args0[ai]; if (arg.StartsWith("'")) { arg = Lex.RemoveSingleQuotes(arg); } //if (Lex.Eq(arg, "[rowcol]") && cInf!= null) //{ // pass grid row & col // args.Add(cInf.GridRowHandle.ToString()); // args.Add(cInf.GridColAbsoluteIndex.ToString()); //} //else args.Add(arg); } funcName = args[0]; arg1 = (args.Count >= 2 ? args[1] : ""); // get other args arg2 = (args.Count >= 3 ? args[2] : ""); arg3 = (args.Count >= 4 ? args[3] : ""); arg4 = (args.Count >= 5 ? args[4] : ""); arg5 = (args.Count >= 6 ? args[5] : ""); if (Lex.Eq(funcName, "DisplayAllData")) { // do all data display for supplied root table and key, i.e. DisplayAllData(TableName, KeyColName, KeyValue) ParseMetaTableMetaColumn(arg1, out mt, arg2, out mc); string extKey = arg3; string intKey = CompoundId.Normalize(extKey, mt); Progress.Show("Building Query..."); _query = QueryEngine.GetSelectAllDataQuery(mt.Name, intKey); Progress.Show("Retrieving data..."); // put up progress dialog since this may take a while QbUtil.RunPopupQuery(_query, mt.KeyMetaColumn.Name + " " + extKey); Progress.Hide(); return; } else if (Lex.Eq(funcName, "DisplayAllDataUsingDbName")) { // display all data for supplied database synonym & key value, i.e. DisplayAllData2(DataBaseSynonym, KeyValue) mtName = null; dbName = arg1; RootTable rti = RootTable.GetFromTableLabel(dbName); if (rti != null) { mtName = rti.MetaTableName; } else // try synonyms { DictionaryMx dict = DictionaryMx.Get("Database_Synonyms"); if (dict != null) { mtName = dict.LookupDefinition(dbName); } } if (String.IsNullOrEmpty(mtName)) { MessageBoxMx.ShowError("Unrecognized database: " + dbName); return; } mt = MetaTableCollection.Get(mtName); if (mt == null) { MessageBoxMx.ShowError("Can't find key metatable " + mtName + " for database " + dbName); return; } string extKey = arg2; string intKey = CompoundId.Normalize(extKey, mt); Progress.Show("Building Query..."); _query = QueryEngine.GetSelectAllDataQuery(mt.Name, intKey); Progress.Show("Retrieving data..."); // put up progress dialog since this may take a while QbUtil.RunPopupQuery(_query, mt.KeyMetaColumn.Name + " " + extKey); return; } // Run a query displaying results to a grid or web page and substituting a parameter value else if (Lex.Eq(funcName, "RunHtmlQuery") || Lex.Eq(funcName, "RunGridQuery")) { // command to display to grid or html if (arg1.StartsWith("MetaTreeNode=", StringComparison.OrdinalIgnoreCase)) { // query based on metatables under a tree node string nodeName = arg1.Substring("MetaTreeNode=".Length).Trim(); _cid = arg2; MetaTreeNode mtn = MetaTree.GetNode(nodeName); if (mtn == null) { MessageBoxMx.ShowError("Can't find tree node referenced in ClickFunction: " + nodeName); return; } _query = new Query(); MetaTable rootMt = null; foreach (MetaTreeNode mtn_ in mtn.Nodes) { if (!mtn_.IsDataTableType) { continue; } mt = MetaTableCollection.Get(mtn_.Target); if (mt == null) { continue; } if (rootMt == null) { rootMt = mt.Root; rootQt = new QueryTable(_query, rootMt); } if (mt == rootMt) { continue; } qt = new QueryTable(_query, mt); } if (_query.Tables.Count == 0) { MessageBoxMx.ShowError("No valid data tables found: " + nodeName); return; } _query.KeyCriteria = "= " + _cid; _title = mtn.Label + " for " + _query.Tables[0].MetaTable.MetaColumns[0].Label + " " + CompoundId.Format(_cid); } else if (arg1.StartsWith("Query=", StringComparison.OrdinalIgnoreCase)) { // query based on saved query string qIdString = arg1.Substring("Query=".Length).Trim(); if (qIdString.StartsWith("Query_", StringComparison.OrdinalIgnoreCase)) { qIdString = qIdString.Substring("Query_".Length).Trim(); } int qId = int.Parse(qIdString); _query = QbUtil.ReadQuery(qId); _cid = arg2; _query.KeyCriteria = "= " + _cid; _title = _query.UserObject.Name + " for " + _query.Tables[0].MetaTable.MetaColumns[0].Label + " " + CompoundId.Format(_cid); } else // explicit mql string to execute { _mql = arg1; // mql to execute if (Lex.IsUndefined(_mql)) { throw new Exception("Expected MQL query not found: " + command); } mt = null; mc = null; if (Lex.IsDefined(arg2) && Lex.IsDefined(arg3)) { mtName = arg2; mcName = arg3; value = arg4; // value to plug in to mql keyValue = value; ParseMetaTableMetaColumn(arg2, out mt, arg3, out mc); } else if (cInf != null) { mt = cInf.Mt; mc = cInf.Mc; value = cInf?.DataValue?.ToString(); keyValue = qm?.DataTableManager?.GetRowKey(cInf.DataRowIndex); } if (mt == null || mc == null) { throw new Exception("Invalid MetaTable or MetaColumn name(s): " + command); } if (!mc.IsNumeric) { value = Lex.AddSingleQuotes(value); // quote if not numeric } int i1 = _mql.ToLower().IndexOf("[value]"); // see if a value parameter if (i1 >= 0) { string value2 = value; _mql = _mql.Replace(_mql.Substring(i1, 7), value); _title = mc.Label + " " + value; } i1 = _mql.ToLower().IndexOf("[keyvalue]"); // see if a key value parameter if (i1 >= 0) { _mql = _mql.Replace(_mql.Substring(i1, 10), keyValue); _title = mt.KeyMetaColumn.Label + " " + keyValue; } try { _query = MqlUtil.ConvertMqlToQuery(_mql); } catch (Exception ex) { MessageBoxMx.ShowError("Error converting Mql to query: " + ex.Message); return; } } if (Lex.Eq(funcName, "RunHtmlQuery")) { QbUtil.RunPopupQuery(_query, _title, OutputDest.Html); } else // output to grid { QbUtil.RunPopupQuery(_query, _title, OutputDest.WinForms); } //else // create new grid query & run (will lose results for current query) //{ // QbUtil.NewQuery(_title); // show in query builder // QbUtil.SetCurrentQueryInstance(_query); // QbUtil.RenderQuery(); // string nextCommand = QueryExec.RunQuery(_query, OutputDest.Grid); //} return; } // Open a URL, normally substituting parameter value else if (Lex.Eq(funcName, "OpenUrl")) { string url = arg1; // url to execute value = arg2; // value to plug in to url int i1 = Lex.IndexOf(url, "[value]"); // fill in one of the value place holders if (i1 >= 0) { string value2 = value; url = url.Replace(url.Substring(i1, 7), value); } else // check to see if we are matching on key { i1 = Lex.IndexOf(url, "[keyvalue]"); if (i1 >= 0) { url = url.Replace(url.Substring(i1, 10), value); } } SystemUtil.StartProcess(url); return; } else if (Lex.Eq(funcName, "OpenUrlFromSmallWorldCid")) { SmallWorldDepictions.OpenUrlFromSmallWorldCid(arg1); return; } else if (Lex.Eq(funcName, "ShowProjectDescription")) { string projName = arg1; QbUtil.ShowProjectDescription(projName); return; } else if (Lex.Eq(funcName, "ShowTableDescription")) { mtName = arg1; QbUtil.ShowTableDescription(mtName); return; } else if (Lex.Eq(funcName, "DisplayDrilldownDetail")) { // drill down into a result value mtName = arg1; // table mcName = arg2; // column int level = Int32.Parse(arg3); string resultId = arg4; // quoted resultId to get q2 = QueryEngine.GetSummarizationDetailQuery(mtName, mcName, level, resultId); if (q2 == null) { throw new Exception("Unable to build drill-down query for: " + mtName + "." + mcName); } bool success = QbUtil.RunPopupQuery(q2, "Result Detail", OutputDest.WinForms); return; } //else if (Lex.Eq(funcName, "PopupSmilesStructure")) // display structure for a Smiles string (still needs some work...) //{ // string molString = arg1.ToString(); // ChemicalStructure cs = new ChemicalStructure(StructureFormat.Smiles, molString); // ToolHelper.DisplayStructureInPopupGrid("Title...", "Smiles", "Structure", cs); //} //else if (Lex.Eq(funcName, "PopupChimeStructure")) // display structure for a Chime string //{ // string molString = arg1.ToString(); // ChemicalStructure cs = new ChemicalStructure(StructureFormat.Smiles, molString); // ToolHelper.DisplayStructureInPopupGrid("Title...", "Smiles", "Structure", cs); //} else if (Lex.Eq(funcName, "DisplayWebPage")) { // substitute a field value into a url & display associated web page string url = arg1; ParseMetaTableMetaColumn(arg2, out mt, arg3, out mc); value = arg4; // value to plug in to mql // value = "{6E9C28EF-407E-44A0-9007-5FFB735A5C6C}"; // debug // value = "{0AC17903-E551-445E-BFAA-860023D2884F}"; // debug // value = "{63EE71F9-15BA-42FB-AFDC-C399103707B1}"; // debug // value = "{80591183-B7BA-4669-8C5F-7E7F53D981CE}"; //lex.OpenString(mc.ClickFunction); // reparse url to get proper case //funcName = lex.GetNonDelimiter(); //url = Lex.RemoveAllQuotes(lex.GetNonDelimiter()); _title = mc.Label + " " + value; int i1 = url.ToLower().IndexOf("[value]"); // fill in one of the value place holders if (i1 >= 0) { url = url.Replace(url.Substring(i1, 7), value); } else // check to see if we are matching on key { i1 = url.ToLower().IndexOf("[keyvalue]"); if (i1 >= 0) { url = url.Replace(url.Substring(i1, 10), value); _title = mt.KeyMetaColumn.Label + " " + value; } } UIMisc.ShowHtmlPopupFormDocument(url, _title); return; } else if (Lex.Eq(funcName, "DisplayOracleBlobDocument")) // display a document contained in an Oracle blob column { // Syntax: DisplayOracleBlobDocument(<table-to-lookup>, <value_match_column>, <file-name-or-type-col>, <content-column>) string table = arg1; string matchCol = arg2; string typeCol = arg3; string contentCol = arg4; string matchVal = arg5; // value to match try { string typeName; byte[] ba; UalUtil.SelectOracleBlob(table, matchCol, typeCol, contentCol, matchVal, out typeName, out ba); if (ba == null || ba.Length == 0) { return; } UIMisc.SaveAndOpenBinaryDocument(typeName, ba); } catch (Exception ex) { MessageBoxMx.ShowError("Error retrieving document: " + ex.Message); return; } return; } else if (Lex.Eq(funcName, "DisplayOracleClobDocument")) // display a document contained in an Oracle clob column { // Syntax: DisplayOracleBlobDocument(<table-to-lookup>, <value_match_column>, <file-name-or-type-col>, <content-column>) string table = arg1; string matchCol = arg2; string typeCol = arg3; string contentCol = arg4; string matchVal = arg5; // value to match try { string typeName, clobString; UalUtil.SelectOracleClob(table, matchCol, typeCol, contentCol, matchVal, out typeName, out clobString); if (Lex.IsUndefined(clobString)) { return; } UIMisc.SaveAndOpenBase64BinaryStringDocument(typeName, clobString); // assume clob string is a Base64Binary string } catch (Exception ex) { MessageBoxMx.ShowError("Error retrieving document: " + ex.Message); return; } return; } else if (Plugins.IsMethodExtensionPoint(funcName)) { List <object> objArgs = new List <object>(); for (ai = 1; ai < args.Count; ai++) // build list of object arguments { objArgs.Add(args[ai]); } Plugins.CallStringExtensionPointMethod(funcName, objArgs); } else if (Lex.Eq(funcName, "None")) // dummy click function { return; } else { MessageBoxMx.ShowError("Unrecogized click function: " + funcName); return; } } catch (Exception ex) { Exception ex2 = ex; if (ex.InnerException != null) { ex2 = ex.InnerException; } string msg = "Error executing ClickFunction: " + command + "\r\n" + DebugLog.FormatExceptionMessage(ex); MessageBoxMx.ShowError(msg); ServicesLog.Message(msg); } }