Beispiel #1
0
        [Timeout(36000000)]  // These can take a long time in code coverage mode
        public void TestRunQuantification()
        {
            var cache                    = new QrFactorizationCache();
            var csvReader                = new DsvFileReader(GetTextReader("quant.csv"), ',');
            var dataRowsByProtein        = ToDataRows(ReadCsvFile(csvReader));
            var expectedResultsByProtein = ReadCsvFile(new DsvFileReader(GetTextReader("runquantdata.csv"), ',')).ToLookup(row => row["Protein"]);

            foreach (var entry in dataRowsByProtein)
            {
                var expectedResultsByRun  = expectedResultsByProtein[entry.Key].ToLookup(row => row["RUN"]);
                FoldChangeDataSet dataSet = FoldChangeCalculator.MakeDataSet(entry.Value);
                var designMatrix          = DesignMatrix.GetRunQuantificationDesignMatrix(dataSet);
                var runNames = FoldChangeCalculator.GetUniqueList(entry.Value.Select(row => row.Run));
                var results  = designMatrix.PerformLinearFit(cache);
                for (int i = 0; i < dataSet.RunCount; i++)
                {
                    string message     = string.Format("Protein:{0} Run:{1}", entry.Key, runNames[i]);
                    var    expectedRow = expectedResultsByRun[runNames[i]].FirstOrDefault();
                    Assert.IsNotNull(expectedRow);
                    Assert.AreEqual(double.Parse(expectedRow["LogIntensities"], CultureInfo.InvariantCulture), results[i].EstimatedValue, .000001, message);
                    Assert.AreEqual(int.Parse(expectedRow["NumFeature"], CultureInfo.InvariantCulture), dataSet.FeatureCount, message);
                    Assert.AreEqual(int.Parse(expectedRow["NumPeaks"], CultureInfo.InvariantCulture), dataSet.GetFeatureCountForRun(i), message);
                }
            }
        }
Beispiel #2
0
        private GroupComparisonResult CalculateFoldChangeUsingRegression(
            GroupComparisonSelector selector, List <RunAbundance> runAbundances)
        {
            var detailRows = new List <DataRowDetails>();

            GetDataRows(selector, detailRows);
            if (detailRows.Count == 0)
            {
                return(null);
            }
            runAbundances = runAbundances ?? new List <RunAbundance>();
            var foldChangeDataRows = detailRows
                                     .Where(row => !double.IsNaN(row.GetLog2Abundance()) && !double.IsInfinity(row.GetLog2Abundance()))
                                     .Select(row => new FoldChangeCalculator.DataRow
            {
                Abundance = row.GetLog2Abundance(),
                Control   = row.Control,
                Feature   = row.IdentityPath,
                Run       = row.ReplicateIndex,
                Subject   = row.BioReplicate,
            }).ToArray();
            FoldChangeDataSet runQuantificationDataSet = FoldChangeCalculator.MakeDataSet(foldChangeDataRows);
            var runNumberToReplicateIndex     = FoldChangeCalculator.GetUniqueList(foldChangeDataRows.Select(row => row.Run));
            var runQuantificationDesignMatrix = DesignMatrix.GetRunQuantificationDesignMatrix(runQuantificationDataSet);
            var quantifiedRuns = runQuantificationDesignMatrix.PerformLinearFit(_qrFactorizationCache);
            var subjects       = new List <int>();

            for (int run = 0; run < quantifiedRuns.Count; run++)
            {
                int iRow = runQuantificationDataSet.Runs.IndexOf(run);
                subjects.Add(runQuantificationDataSet.Subjects[iRow]);
                var replicateIndex   = runNumberToReplicateIndex[run];
                var replicateDetails = _replicateIndexes.First(kvp => kvp.Key == replicateIndex).Value;

                runAbundances.Add(new RunAbundance
                {
                    ReplicateIndex = replicateIndex,
                    Control        = replicateDetails.IsControl,
                    BioReplicate   = replicateDetails.BioReplicate,
                    Log2Abundance  = quantifiedRuns[run].EstimatedValue
                });
            }
            var abundances        = quantifiedRuns.Select(result => result.EstimatedValue).ToArray();
            var quantifiedDataSet = new FoldChangeDataSet(
                abundances,
                Enumerable.Repeat(0, quantifiedRuns.Count).ToArray(),
                Enumerable.Range(0, quantifiedRuns.Count).ToArray(),
                subjects,
                runQuantificationDataSet.SubjectControls);

            if (quantifiedDataSet.SubjectControls.Distinct().Count() < 2)
            {
                return(null);
            }

            var foldChangeResult = DesignMatrix.GetDesignMatrix(quantifiedDataSet, false).PerformLinearFit(_qrFactorizationCache).First();

            return(new GroupComparisonResult(selector, quantifiedRuns.Count, foldChangeResult, runAbundances));
        }
Beispiel #3
0
        [Timeout(36000000)]  // These can take a long time in code coverage mode
        public void TestGroupComparisonWithRunQuantification()
        {
            var csvReader                = new DsvFileReader(GetTextReader("quant.csv"), ',');
            var dataRowsByProtein        = ToDataRows(ReadCsvFile(csvReader));
            var expectedResultsByProtein = ReadCsvFile(new DsvFileReader(GetTextReader("result_newtesting_v2.csv"), ','))
                                           .ToDictionary(row => row["Protein"]);
            var cache = new QrFactorizationCache();

            foreach (var entry in dataRowsByProtein)
            {
                FoldChangeDataSet dataSet = FoldChangeCalculator.MakeDataSet(entry.Value);
                var quantifiedRuns        = DesignMatrix.GetRunQuantificationDesignMatrix(dataSet).PerformLinearFit(cache);
                var subjects = new List <int>();

                for (int run = 0; run < quantifiedRuns.Count; run++)
                {
                    int iRow = dataSet.Runs.IndexOf(run);
                    subjects.Add(dataSet.Subjects[iRow]);
                }
                var abundances        = quantifiedRuns.Select(result => result.EstimatedValue).ToArray();
                var quantifiedDataSet = new FoldChangeDataSet(
                    abundances,
                    Enumerable.Repeat(0, quantifiedRuns.Count).ToArray(),
                    Enumerable.Range(0, quantifiedRuns.Count).ToArray(),
                    subjects,
                    dataSet.SubjectControls);
                var    foldChangeResult = DesignMatrix.GetDesignMatrix(quantifiedDataSet, false).PerformLinearFit(cache).First();
                var    expectedResult   = expectedResultsByProtein[entry.Key];
                string message          = entry.Key;
                Assert.AreEqual(double.Parse(expectedResult["logFC"], CultureInfo.InvariantCulture), foldChangeResult.EstimatedValue, 1E-6, message);
                Assert.AreEqual(double.Parse(expectedResult["SE"], CultureInfo.InvariantCulture), foldChangeResult.StandardError, 1E-6, message);
                Assert.AreEqual(int.Parse(expectedResult["DF"], CultureInfo.InvariantCulture), foldChangeResult.DegreesOfFreedom, message);
                if (Math.Abs(foldChangeResult.EstimatedValue) > 1E-8)
                {
                    Assert.AreEqual(double.Parse(expectedResult["pvalue"], CultureInfo.InvariantCulture), foldChangeResult.PValue, 1E-6, message);
                    Assert.AreEqual(double.Parse(expectedResult["Tvalue"], CultureInfo.InvariantCulture), foldChangeResult.TValue, 1E-6, message);
                }
            }
        }