示例#1
0
        private static void TwoSamplesTest1(int[] colInd1, int[] colInd2, TestTruncation truncation, double threshold,
                                            TwoSampleTest test, OneSampleTest test1, TestSide side, bool log, IMatrixData data, double s0, int nrand,
                                            List <int[]> colIndsPreserve1, List <int[]> colIndsPreserve2, string suffix, string mainSuffix, bool filterValidValues,
                                            int minNumValidValues, int minNumValidValuesMode, int minPercValidValues, int minPercValidValuesMode,
                                            out double[] pvalsS0, bool randomized, bool addQval, IList <string> plotNames, IList <string> pvals1Name,
                                            IList <string> fdrs1Name, IList <string> diffs1Name, IList <string> statCol1Name, IList <string> significant1Name,
                                            IList <double[]> pvals1, IList <double[]> fdrs1, IList <double[]> diffs1, IList <double[]> statCol1,
                                            IList <string[][]> significant1, int ind, bool paired)
        {
            double[] pvals = new double[data.RowCount];
            pvalsS0 = new double[data.RowCount];
            double[]   diffs     = new double[data.RowCount];
            double[]   statCol   = new double[data.RowCount];
            List <int> validRows = new List <int>();

            for (int i = 0; i < data.RowCount; i++)
            {
                bool valid = IsValidRow(filterValidValues, minNumValidValues, minNumValidValuesMode, minPercValidValues,
                                        minPercValidValuesMode, i, colInd1, colInd2, data);
                if (valid)
                {
                    validRows.Add(i);
                }
                double[] vals1;
                double[] vals2;
                if (!paired)
                {
                    vals1 = GetValues(i, colInd1, data, true);
                    vals2 = GetValues(i, colInd2, data, true);
                }
                else
                {
                    vals1 = GetValues(i, colInd1, data, false);
                    vals2 = GetValues(i, colInd2, data, false);
                }
                if (valid)
                {
                    CalcTest(test, test1, paired, vals1, vals2, s0, side, out diffs[i], out pvals[i], out pvalsS0[i], out statCol[i]);
                }
                else
                {
                    pvals[i]   = double.NaN;
                    pvalsS0[i] = double.NaN;
                    statCol[i] = double.NaN;
                }
            }
            if (!randomized)
            {
                string[][] significant;
                double[]   fdrs = null;
                switch (truncation)
                {
                case TestTruncation.Pvalue:
                    significant = PerseusPluginUtils.CalcPvalueSignificance(pvals, threshold);
                    break;

                case TestTruncation.BenjaminiHochberg:
                    significant = PerseusPluginUtils.CalcBenjaminiHochbergFdr(pvals, threshold, out fdrs);
                    break;

                case TestTruncation.PermutationBased:
                    double[]   fdrsX;
                    string[][] significantX = CalcPermutationBasedFdr(ArrayUtils.SubArray(pvalsS0, validRows), nrand, data, test,
                                                                      test1, side, colInd1, colInd2, s0, threshold, colIndsPreserve1, colIndsPreserve2, validRows, out fdrsX, paired);
                    significant = new string[data.RowCount][];
                    fdrs        = new double[data.RowCount];
                    for (int i = 0; i < significant.Length; i++)
                    {
                        significant[i] = new string[0];
                        fdrs[i]        = 1;
                    }
                    for (int i = 0; i < validRows.Count; i++)
                    {
                        significant[validRows[i]] = significantX[i];
                        fdrs[validRows[i]]        = fdrsX[i];
                    }
                    break;

                default: throw new Exception("Never get here.");
                }
                string x = test.Name + " p-value";
                if (log)
                {
                    x = "-Log " + x;
                    for (int i = 0; i < pvals.Length; i++)
                    {
                        pvals[i] = -Math.Log10(Math.Max(pvals[i], double.Epsilon));
                    }
                }
                plotNames[ind]        = suffix + mainSuffix;
                pvals1Name[ind]       = x + " " + suffix + mainSuffix;
                fdrs1Name[ind]        = test.Name + " q-value " + suffix + mainSuffix;
                diffs1Name[ind]       = test.Name + " Difference " + suffix + mainSuffix;
                statCol1Name[ind]     = test.Name + " Test statistic " + suffix + mainSuffix;
                significant1Name[ind] = test.Name + " Significant " + suffix + mainSuffix;
                pvals1[ind]           = pvals;
                if (addQval)
                {
                    fdrs1[ind] = fdrs;
                }
                diffs1[ind]       = diffs;
                statCol1[ind]     = statCol;
                significant1[ind] = significant;
            }
        }
示例#2
0
        private static void PerformSingleTest(string firstGroup, string secondGroup, SecondGroupMode secondGroupMode,
                                              IMatrixData mdata, int groupInd, ref string err, bool log, double threshold, TestTruncation truncation, int nrand,
                                              int preserveGroupInd, TwoSampleTest test, OneSampleTest test1, TestSide side, double s0, bool filterValidValues,
                                              int minNumValidValues, int minNumValidValuesMode, int minPercValidValues, int minPercValidValuesMode,
                                              out double[] pvalsS0, IDictionary <int, int> indMap, bool qval, string[] plotNames, string[] pvals1Name,
                                              string[] fdrs1Name, string[] diffs1Name, string[] statCol1Name, string[] significant1Name, double[][] pvals1,
                                              double[][] fdrs1, double[][] diffs1, double[][] statCol1, string[][][] significant1, int ind, List <string>[] sigCol,
                                              out string suffix, string mainSuffix, bool paired)
        {
            bool randomized = indMap != null;
            bool addQval    = qval && truncation != TestTruncation.Pvalue;

            string[][] groupCol = mdata.GetCategoryRowAt(groupInd);
            int[]      colInds1;
            int[]      colInds2;
            if (secondGroupMode == SecondGroupMode.Complement)
            {
                int[][] colInds = PerseusPluginUtils.GetMainColIndices(groupCol, new[] { firstGroup });
                colInds1 = colInds[0];
                colInds2 = ArrayUtils.Complement(colInds1, mdata.ColumnCount);
            }
            else
            {
                int[][] colInds = PerseusPluginUtils.GetMainColIndices(groupCol, new[] { firstGroup, secondGroup });
                colInds1 = colInds[0];
                colInds2 = colInds[1];
            }
            if (indMap != null)
            {
                Transform(colInds1, indMap);
                Transform(colInds2, indMap);
            }
            Array.Sort(colInds1);
            Array.Sort(colInds2);
            suffix = firstGroup;
            if (secondGroupMode != SecondGroupMode.Complement)
            {
                suffix += "_" + secondGroup;
            }
            if (paired && colInds1.Length != colInds2.Length)
            {
                err     = "Group sizes have to be equal for paired test.";
                pvalsS0 = null;
                return;
            }
            List <int[]> colIndsPreserve1 = null;
            List <int[]> colIndsPreserve2 = null;

            if (truncation == TestTruncation.PermutationBased)
            {
                if (preserveGroupInd >= 0)
                {
                    if (paired)
                    {
                        err     = "Preserved subgroups are not supported for paired tests.";
                        pvalsS0 = null;
                        return;
                    }
                    string[][] preserveGroupCol  = mdata.GetCategoryRowAt(preserveGroupInd);
                    string[]   allGroupsPreserve = ArrayUtils.UniqueValuesPreserveOrder(ArrayUtils.Concat(preserveGroupCol));
                    int[][]    colIndsPreserve   = PerseusPluginUtils.GetMainColIndices(preserveGroupCol, allGroupsPreserve);
                    int[]      allInds           = ArrayUtils.Concat(colIndsPreserve);
                    int[]      allIndsUnique     = ArrayUtils.UniqueValues(allInds);
                    if (allInds.Length != allIndsUnique.Length)
                    {
                        err     = "The grouping for randomizations is not unique";
                        pvalsS0 = null;
                        return;
                    }
                    if (allInds.Length != colInds1.Length + colInds2.Length)
                    {
                        err     = "The grouping for randomizations is not valid because it does not cover all samples.";
                        pvalsS0 = null;
                        return;
                    }
                    colIndsPreserve1 = new List <int[]>();
                    colIndsPreserve2 = new List <int[]>();
                    foreach (int[] inds in colIndsPreserve)
                    {
                        int index = DetermineGroup(colInds1, colInds2, inds);
                        if (index == 0)
                        {
                            err     = "The grouping for randomizations is not hierarchical with respect to the main grouping.";
                            pvalsS0 = null;
                            return;
                        }
                        switch (index)
                        {
                        case 1:
                            colIndsPreserve1.Add(inds);
                            break;

                        case 2:
                            colIndsPreserve2.Add(inds);
                            break;
                        }
                    }
                }
            }
            TwoSamplesTest1(colInds1, colInds2, truncation, threshold, test, test1, side, log, mdata, s0, nrand,
                            colIndsPreserve1, colIndsPreserve2, suffix, mainSuffix, filterValidValues, minNumValidValues, minNumValidValuesMode,
                            minPercValidValues, minPercValidValuesMode, out pvalsS0, randomized, addQval, plotNames, pvals1Name, fdrs1Name,
                            diffs1Name, statCol1Name, significant1Name, pvals1, fdrs1, diffs1, statCol1, significant1, ind, paired);
            if (significant1 != null)
            {
                for (int i = 0; i < significant1[ind].Length; i++)
                {
                    if (significant1[ind][i].Length > 0)
                    {
                        sigCol[i].Add(suffix);
                    }
                }
            }
        }