예제 #1
0
/// <summary>
/// Build the query that is executed to retrieve the summarized target data and optional structure data
/// </summary>
/// <returns></returns>

        void BuildQuery(string title)
        {
            int qid = SS.I.ServicesIniFile.ReadInt("SpillTheBeansToolModelQuery", -1);

            if (qid < 0)
            {
                throw new Exception("SpillTheBeansToolModelQuery not defined");
            }

            Query q = QbUtil.ReadQuery(qid);

            if (q == null)
            {
                throw new Exception("Failed to read SpillTheBeansToolModelQuery: " + qid);
            }

            AssertMx.IsDefined(KeyQc?.Criteria, "KeyQc.Criteria");

            ParsedSingleCriteria psc = MqlUtil.ParseSingleCriteria(KeyQc?.Criteria);             // parse key criteria

            KeyQc.CopyCriteriaToQueryKeyCritera(q);
            Query = q;             // save for later use

            return;
        }
예제 #2
0
		/// <summary>
		/// Execute query
		/// </summary>
		/// <param name="eqp"></param>

		public override void ExecuteQuery(
				ExecuteQueryParms eqp)
		{
			MetaTable mt;
			MetaColumn mc;
			Query q;
			QueryTable qt;
			QueryColumn qc;
			ResultsPage rp;
			ResultsViewProps view;
			CompoundStructureActivityData cd1, cd2;

			bool smallerIsbetter;
			double r1, r2, r3, r4;
			int di, di2, pdi, pdi2, i3;
			string tok;

			qt = eqp.QueryTable;
			qc = qt.GetQueryColumnByNameWithException(SasMapParms.ParametersMetaColumnName);
			AssertMx.IsDefined(qc.Criteria, qc.Label + " criteria not defined");

			if (Lex.Eq(qc.Criteria, LastCriteriaString)) // if same criteria as last time then use existing data
			{
				VoListPos = -1; // init list position
				return;
			}

			VoList = new List<object[]>();
			VoListPos = -1; // init list position

			LastCriteriaString = qc.Criteria;
			ParsedSingleCriteria psc = ParsedSingleCriteria.Parse(qc);
			SMP = SasMapParms.Deserialize(psc.Value);

			mc = SMP.EndpointMc;
			smallerIsbetter = mc.MultiPoint;

			List<CompoundStructureActivityData> ds1 = ReadData(SMP); // read in the data to analyze
			if (ds1 == null || ds1.Count == 0) return; //  throw new QueryException("No data retrieved");

			List<CompoundStructureActivityData> ds2 = ds1; // just one set for now

			// Calculate difference or ratio coefficents for each pair

			List<PairData> pd = new List<PairData>();
			int minCoef = -1; // index of minimum coefficent selected so far
			double molFactor = AssayAttributes.GetMolarConcFactor(SMP.EndpointMc);

			for (di = 0; di < ds1.Count; di++)
			{ // process all compounds in 1st set
				//		if (ds1[di].Nearest == 0) continue; // any data?
				if (ds2 == ds1) di2 = di + 1; // only do lower rt diagonal if one dataset
				else di2 = 0; // must do all compares, check for dups later

				for ( /* start at di2 */; di2 < ds2.Count; di2++)
				{
					//			if (ds2[di2].Nearest == 0) continue; // any data?
					if (ds1[di].Cid == ds2[di2].Cid) continue; // avoid self compare

					double sim = // similarity
					 CalculateSimilarity(ds1[di], ds2[di2]);
					//if (sim==1.0 && !stereo) // eliminate stereo pairs if requested
					// continue; // a more careful check may be needed

					if (sim < SMP.MinimumSimilarity) continue; // below cutoff value?

					double denom = 1 - sim; // denominator is 1 - sim
					if (denom == 0) denom = .00000000001f; // avoid divide by zero

					double actChange = 0;

					if (smallerIsbetter && ds1[di].Activity < ds2[di2].Activity)
					{
						cd1 = ds1[di];
						cd2 = ds2[di2];
					}

					else
					{
						cd1 = ds2[di2];
						cd2 = ds1[di];
					}

					double a1 = cd1.Activity;
					double a2 = cd2.Activity;

					if (a1 == NullValue.NullNumber || a2 == NullValue.NullNumber)
						actChange = 0;

					else switch (SMP.ActDiffCalcType)
						{
							case ActDiffCalcType.SimpleDifference: // activity difference
								{
									actChange = a1 - a2;
									break;
								}

							case ActDiffCalcType.NegativeLog:
								{
									actChange = -Math.Log10(a1) - -Math.Log10(a2);
									break;
								}

							case ActDiffCalcType.MolarNegativeLog:
								{
									actChange = (-Math.Log10(a1 * molFactor)) - (-Math.Log10(a2 * molFactor));
									break;
								}

							case ActDiffCalcType.Ratio: // activity ratio
								{
									r1 = a1;
									if (r1 == 0) r1 = .00000000001f;
									r2 = a2;
									if (r2 == 0) r2 = .00000000001f;
									r3 = r1 / r2;
									r4 = r2 / r1;

									actChange = r3;
									if (SMP.UseAbsoluteValue && r4 > r3) // take the max value
										actChange = r4;

									break;
								}

							case ActDiffCalcType.Advanced:
								{
									throw new InvalidOperationException("SarMapCalcType.Advanced");
								}

							default:
								throw new InvalidOperationException("SarMapCalcType: " + (int)SMP.ActDiffCalcType);
						}

					if (SMP.UseAbsoluteValue && SMP.ActDiffCalcType != ActDiffCalcType.Ratio)
						actChange = Math.Abs(actChange);

					double coef = actChange / denom;

					if (pd.Count < SMP.MaxPairCount)  // just add this pair to end
					{
						pdi = pd.Count;
						pd.Add(new PairData());
					}

					else
					{ // see if this value is greater than anything in list
						if (minCoef < 0)
						{ // find element with minimum coef
							minCoef = 0;
							for (i3 = 1; i3 < pd.Count; i3++)
							{
								if (pd[i3].Coef < pd[minCoef].Coef)
									minCoef = i3;
							}
						}
						if (coef <= pd[minCoef].Coef) continue; // if this one better?

						//if (ds1 != ds2)
						//{ // be sure not a duplicate of what we have in list
						//	for (i3 = 0; i3 < pd.Count; i3++)
						//	{ // check for pair in list already
						//		if ((di == pd[i3].CD1 && di2 == pd[i3].CD2) ||
						//				(di == pd[i3].CD2 && di2 == pd[i3].CD1)) break;
						//	}
						//	if (i3 < pd.Count) continue; // continue to next pair if found
						//}

						pdi = minCoef; // replace this item
						minCoef = -1; // reset to get new minimum next time
					}

					// Save data for the pair

					pd[pdi].CD1 = cd1;
					pd[pdi].CD2 = cd2;
					pd[pdi].Sim = sim;
					pd[pdi].ActChange = actChange;
					pd[pdi].Coef = coef;
				}
			}

			// Build the list of pair Vos

			int voLen = qt.SetSimpleVoPositions();

			PairData pdItem;
			for (pdi = 1; pdi < pd.Count; pdi++) // sort from max to min coef value
			{
				pdItem = pd[pdi];
				for (pdi2 = pdi - 1; pdi2 >= 0; pdi2--)
				{
					if (pdItem.Coef < pd[pdi2].Coef) break;
					pd[pdi2 + 1] = pd[pdi2];
				}
				pd[pdi2 + 1] = pdItem;
			}

			for (pdi = 0; pdi < pd.Count; pdi++)
			{
				pdItem = pd[pdi];
				cd1 = pdItem.CD1;
				cd2 = pdItem.CD2;

				object[] vo = new object[voLen];

				VoArray.SetVo(qt, "PAIR_ID", vo, new NumberMx(pdi + 1));
				VoArray.SetVo(qt, "COMPOUND1", vo, new StringMx(cd1.Cid));
				VoArray.SetVo(qt, "STRUCTURE1", vo, cd1.Structure);
				VoArray.SetVo(qt, "ACTIVITY1", vo, new NumberMx(cd1.Activity));

				VoArray.SetVo(qt, "COMPOUND2", vo, new StringMx(cd2.Cid));
				VoArray.SetVo(qt, "STRUCTURE2", vo, cd2.Structure);
				VoArray.SetVo(qt, "ACTIVITY2", vo, new NumberMx(cd2.Activity));

				VoArray.SetVo(qt, "SIMILARITY", vo, new NumberMx(pdItem.Sim));
				VoArray.SetVo(qt, "ACTIVITY_DIFF", vo, new NumberMx(pdItem.ActChange));
				VoArray.SetVo(qt, "ACT_SIM_COEF", vo, new NumberMx(pdItem.Coef));

				VoList.Add(vo);
			}

			VoListPos = -1; // init list position
			return;
		}
예제 #3
0
        /// <summary>
        /// Setup the form for display
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="qm"></param>
        /// <returns></returns>

        bool SetupForm(
            string cid,
            QueryManager qm)
        {
            AssertMx.IsDefined(cid, "Compound Id (CorpId)");

            SelectedCid = cid;
            PreviousCid = cid;

            RootTable = CompoundId.GetRootMetaTableFromCid(cid);

            Qm          = null;    // no query context
            SourceQuery = null;
            CurrentBaseQueryCidHitList = null;

            if (qm != null)             // if querymanager defined then base the related data query on the "current" query
            {
                Qm          = qm;       // setup query context
                SourceQuery = qm.Query;
                RootTable   = qm.Query.RootMetaTable;

                if (qm.Query != null && qm.Query.Mode == QueryMode.Browse)                 // if browsing current base query results then get that cid list
                {
                    CurrentBaseQueryCidHitList = Qm.DataTableManager.GetMostCompleteResultsKeyList();
                }
            }

            //else throw new Exception("Parameters not defined");

            //if (Lex.IsUndefined(SelectedCid) &&
            // (Qm == null || Qm.MoleculeGrid == null || Qm.MoleculeGrid.Helpers == null))
            //	return false;

            SetupCheckmarks();

            // Current Cid

            //if (Qm != null)
            //	SelectedCid = Qm.MoleculeGrid.Helpers.GetCidForSelectedCell();

            SelectedCid = CompoundId.Format(SelectedCid);
            CidCtl.Text = SelectedCid;
            //CidCtl.Focus();

            // Marked cid count

            MarkedCidsList = null;
            if (Qm?.MoleculeGrid != null)
            {
                CidList cl = Qm.MoleculeGrid.GetMarkedList();
                if (cl != null)
                {
                    MarkedCidsList = cl.ToStringList();
                }
            }

            int selCnt = (MarkedCidsList != null ? MarkedCidsList.Count : 0);

            MarkedCidsCheckEdit.Text    = "Selected compound Ids (" + FormatCidListForDisplay(MarkedCidsList) + ")";
            MarkedCidsCheckEdit.Enabled = (selCnt > 0);
            if (selCnt == 0 && MarkedCidsCheckEdit.Checked)
            {
                CurrentCidCheckEdit.Checked = true;
            }

            // All Cid count


            int allCnt = (CurrentBaseQueryCidHitList != null ? CurrentBaseQueryCidHitList.Count : 0);

            AllCidsCheckEdit.Text    = "All Ids in the current result set (" + FormatCidListForDisplay(CurrentBaseQueryCidHitList) + ")";
            AllCidsCheckEdit.Enabled = (allCnt > 0);
            if (selCnt == 0 && AllCidsCheckEdit.Checked)
            {
                CurrentCidCheckEdit.Checked = true;
            }

            // Structure

            MoleculeMx cs = new MoleculeMx();

            if (Lex.IsDefined(SelectedCid))
            {
                cs = MoleculeUtil.SelectMoleculeForCid(SelectedCid);
            }

            QueryMolCtl.SetupAndRenderMolecule(cs);

            // MRU list

            RenderMruList();

            return(true);
        }