Example #1
0
 public virtual void LoadFromAnnotation(IAnnotation ann, LinearRegressionRatioResult lrrr)
 {
     if (ann.IsEnabled(false))
     {
         if (ann.Annotations[this.RatioKey] is LinearRegressionRatioResult)
         {
             var oldLrrr = ann.Annotations[this.RatioKey] as LinearRegressionRatioResult;
             lrrr.CopyFrom(oldLrrr);
         }
         else
         {
             lrrr.Ratio = double.Parse(ann.Annotations[this.RatioKey].ToString());
         }
         lrrr.ReferenceIntensity = double.Parse(ann.Annotations[this.ReferenceKey].ToString());
         lrrr.SampleIntensity    = double.Parse(ann.Annotations[this.SampleKey].ToString());
         lrrr.PValue             = double.Parse(ann.Annotations[this.PValueKey].ToString());
     }
     else
     {
         lrrr.Ratio = 1.0;
         lrrr.ReferenceIntensity = 0.0;
         lrrr.SampleIntensity    = 0.0;
         lrrr.PValue             = 1.0;
     }
 }
Example #2
0
 public virtual void SaveToAnnotation(IAnnotation ann, LinearRegressionRatioResult lrrr)
 {
     ann.SetEnabled(true);
     ann.Annotations[this.RatioKey]     = lrrr;
     ann.Annotations[this.ReferenceKey] = string.Format("{0:0.0}", lrrr.ReferenceIntensity);
     ann.Annotations[this.SampleKey]    = string.Format("{0:0.0}", lrrr.SampleIntensity);
     ann.Annotations[this.PValueKey]    = string.Format("{0:0.##E+0}", lrrr.PValue);
 }
        private static LinearRegressionRatioResult ParseLinearRegressionRatioResult(string[] parts, int startIndex)
        {
            LinearRegressionRatioResult result;

            result = new LinearRegressionRatioResult();
            result.ReferenceIntensity = double.Parse(parts[startIndex]);
            result.SampleIntensity    = double.Parse(parts[startIndex + 1]);
            result.Ratio = double.Parse(parts[startIndex + 2]);
            result.Stdev = parts[startIndex + 3].Equals("NA") ? 0 : double.Parse(parts[startIndex + 3]);
            //lrrr.RSquare = double.Parse(parts[2]);
            result.TValue     = parts[startIndex + 4].Equals("NA") ? 0 : ParseDouble(parts[startIndex + 4], 0);
            result.PValue     = parts[startIndex + 5].Equals("NA") ? 1 : ParseDouble(parts[startIndex + 5], 0);
            result.PointCount = int.Parse(parts[startIndex + 6]);
            if (result.ReferenceIntensity > result.SampleIntensity)
            {
                result.SampleIntensity = result.ReferenceIntensity * result.Ratio;
            }
            else
            {
                result.ReferenceIntensity = result.SampleIntensity / result.Ratio;
            }

            return(result);
        }
        public static LinearRegressionRatioResult CalculateRatio(double[][] intensities)
        {
            double maxA = intensities[0].Max();
            double maxB = intensities[1].Max();

            if (maxA == 0.0)
            {
                return(new LinearRegressionRatioResult(20, 0.0));
            }

            if (maxB == 0.0)
            {
                return(new LinearRegressionRatioResult(0.05, 0.0));
            }

            if (intensities[0].Length == 1) // only one point
            {
                return(new LinearRegressionRatioResult(intensities[1][0] / intensities[0][0], 0.0));
            }

            var aList = new List <double>(intensities[0]);

            aList.Add(0);
            var a = new double[1][];

            a[0] = aList.ToArray();

            var bList = new List <double>(intensities[1]);

            bList.Add(0);
            double[] b = bList.ToArray();

            var x = new double[1];

            double rnorm = 0.0;

            NonNegativeLeastSquaresCalc.NNLS(a, b.Length, 1, b, x, out rnorm, null, null, null);

            var bPredicted = new double[aList.Count];

            for (int i = 0; i < aList.Count; i++)
            {
                bPredicted[i] = x[0] * aList[i];
            }

            var result = new LinearRegressionRatioResult();

            result.Ratio      = x[0];
            result.RSquare    = StatisticsUtils.RSquare(bList.ToArray(), bPredicted);
            result.PointCount = bList.Count;
            result.TValue     = StatisticsUtils.FCalculatedValueForLinearRegression(bList.ToArray(), bPredicted);

            if (double.IsInfinity(result.TValue) || double.IsNaN(result.TValue) || result.TValue < 0)
            {
                result.TValue = 0;
            }

            result.PValue = StatisticsUtils.FProbabilityForLinearRegression(bList.Count, result.TValue);

            return(result);
        }
        public static LinearRegressionRatioResult CalculateRatioDistance(double[][] intensities)
        {
            int len = intensities[0].Length;

            double maxA = intensities[0].Max();
            double maxB = intensities[1].Max();

            if (maxA == 0.0)
            {
                return(new LinearRegressionRatioResult(20, 0.0));
            }

            if (maxB == 0.0)
            {
                return(new LinearRegressionRatioResult(0.05, 0.0));
            }

            if (len == 1) // only one point
            {
                return(new LinearRegressionRatioResult(intensities[1][0] / intensities[0][0], 0.0));
            }

            var a = new double[2][];

            a[0] = new double[len];
            a[1] = new double[len];
            for (int i = 0; i < len; i++)
            {
                a[0][i] = intensities[0][i];
                a[1][i] = 1;
            }

            double[] b = new List <double>(intensities[1]).ToArray();

            var x = new double[2];

            double rnorm = 0.0;

            NonNegativeLeastSquaresCalc.NNLS(a, b.Length, 2, b, x, out rnorm, null, null, null);

            var bPredicted = new double[len];

            for (int i = 0; i < len; i++)
            {
                bPredicted[i] = x[0] * intensities[0][i] + x[1];
            }

            var result = new LinearRegressionRatioResult();

            result.Ratio      = x[0];
            result.Distance   = x[1];
            result.RSquare    = StatisticsUtils.RSquare(intensities[1], bPredicted);
            result.PointCount = b.Length;
            result.TValue     = StatisticsUtils.FCalculatedValueForLinearRegression(intensities[1], bPredicted);

            if (double.IsInfinity(result.TValue) || double.IsNaN(result.TValue) || result.TValue < 0)
            {
                result.TValue = 0;
            }

            result.PValue = StatisticsUtils.FProbabilityForLinearRegression(len, result.TValue);

            return(result);
        }
        private WaitingEntry DoCalculate(IIdentifiedProteinGroup proteinGroup, Func <IIdentifiedSpectrum, bool> validFunc, bool runRImmediately)
        {
            List <IIdentifiedSpectrum> spectra = (from s in proteinGroup[0].GetSpectra()
                                                  where validFunc(s) && s.IsEnabled(true) && HasPeptideRatio(s)
                                                  select s).ToList();

            if (spectra.Count == 1)
            {
                var lrrr = new LinearRegressionRatioResult(CalculatePeptideRatio(spectra[0]), 0.0)
                {
                    PointCount         = 1,
                    TValue             = 0,
                    PValue             = 1,
                    ReferenceIntensity = this.intensityFunc.GetReferenceIntensity(spectra[0]),
                    SampleIntensity    = this.intensityFunc.GetSampleIntensity(spectra[0])
                };

                var r = CalculatePeptideRatio(spectra[0]);
                foreach (var protein in proteinGroup)
                {
                    this.intensityFunc.SaveToAnnotation(protein, lrrr);
                }
                return(null);
            }
            else if (spectra.Count > 1)
            {
                var intensities = this.intensityFunc.ConvertToArray(spectra);

                double sumSam = intensities[0].Max();
                double sumRef = intensities[1].Max();

                LinearRegressionRatioResult lrrr;

                if (sumSam == 0.0)
                {
                    lrrr = new LinearRegressionRatioResult(20, 0.0)
                    {
                        PointCount         = intensities.Count(),
                        TValue             = 0,
                        PValue             = 0,
                        ReferenceIntensity = sumRef,
                    };
                    lrrr.SampleIntensity = sumRef / lrrr.Ratio;
                }
                else
                {
                    if (sumRef == 0.0)
                    {
                        lrrr = new LinearRegressionRatioResult(0.05, 0.0)
                        {
                            PointCount      = intensities.Count(),
                            TValue          = 0,
                            PValue          = 0,
                            SampleIntensity = sumSam
                        };
                        lrrr.ReferenceIntensity = sumSam * lrrr.Ratio;
                    }
                    else
                    {
                        var filename = (this.DetailDirectory + "/" + proteinGroup[0].Name.Replace("|", "_") + ".csv").Replace("\\", "/");

                        PrepareIntensityFile(spectra, filename);

                        if (!runRImmediately)
                        {
                            return(new WaitingEntry()
                            {
                                Group = proteinGroup,
                                IntensityFile = filename
                            });
                        }

                        var linearfile = filename + ".linear";

                        var roptions = new RTemplateProcessorOptions();

                        roptions.InputFile  = filename;
                        roptions.OutputFile = linearfile;
                        roptions.RTemplate  = FileUtils.GetTemplateDir() + "/PairQuantification.r";

                        new RTemplateProcessor(roptions).Process();

                        var parts = File.ReadAllLines(linearfile).Skip(1).First().Split('\t');

                        lrrr = ParseLinearRegressionRatioResult(parts, 0);
                    }
                }

                foreach (IIdentifiedProtein protein in proteinGroup)
                {
                    this.intensityFunc.SaveToAnnotation(protein, lrrr);
                }
            }
            else
            {
                foreach (IIdentifiedProtein protein in proteinGroup)
                {
                    this.intensityFunc.RemoveFromAnnotation(protein);
                }
            }
            return(null);
        }