Пример #1
0
        public void TestLoess(FitFunctionTypes type,
                              FitFunctionTypes generatingFunction,
                              double dt,
                              double attenuation)
        {
            var interpolator = new LoessInterpolator(.25, 0);

            var xValues = new List <double>();
            var yValues = new List <double>();


            double cv = 0;

            var fitFunction = FitFunctionFactory.Create(type);
            var genFunction = FitFunctionFactory.Create(generatingFunction);

            var random = new Random();

            // Take one period of the sine wave...
            while (cv < Math.PI * 3)
            {
                var value = genFunction(cv) + random.NextDouble() * attenuation;

                xValues.Add(cv);
                yValues.Add(value);
                cv += dt;
            }

            var newYValues = interpolator.Smooth(xValues, yValues, fitFunction);

            for (var i = 0; i < xValues.Count; i++)
            {
                Console.WriteLine(@"{0} {1} {2} {3}", i, xValues[i], yValues[i], newYValues[i]);
            }

            dt /= 2;
            cv  = 0;

            Console.WriteLine();

            // Take one period of the sine wave...
            while (cv < Math.PI * 3)
            {
                var predicted = interpolator.Predict(cv);
                Console.WriteLine(@"{0} {1}", cv, predicted);
                cv += dt;
            }
        }
Пример #2
0
        private static void WriteMatches(
            string name,
            ISpectralAnalysisWriter writer,
            List <double> xvalues,
            List <double> yvalues,
            IList <double> fit,
            LoessInterpolator interpolator,
            List <SpectralAnchorPointMatch> anchorPoints,
            List <double> preNet,
            List <double> postNet,
            Func <double, double, double> difference)
        {
            if (anchorPoints == null)
            {
                throw new ArgumentNullException("anchorPoints");
            }

            writer.WriteLine(string.Format(@"[{0}]", name));
            writer.WriteLine("x\ty\tfit\tpre-diff\tpost-diff\tsim score\tvalid");
            for (var index = 0; index < xvalues.Count; index++)
            {
                var x        = xvalues[index];
                var y        = yvalues[index];
                var value    = interpolator.Predict(x);
                var preDiff  = difference(x, y);
                var postDiff = difference(value, y);

                writer.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
                                               x,
                                               y,
                                               fit[index],
                                               preDiff,
                                               postDiff,
                                               anchorPoints[index].SimilarityScore,
                                               anchorPoints[index].IsValidMatch));

                preNet.Add(preDiff);
                postNet.Add(postDiff);
            }
        }
Пример #3
0
        public void TestAlignmentfunction(
            string path,
            string outputPath,
            double bandwidth,
            int robustnessIterators
            )
        {
            var data = File.ReadAllLines(path);

            var x = new List <double>();
            var y = new List <double>();

            for (var i = 1; i < data.Length; i++)
            {
                var columns = data[i].Split('\t');
                if (columns.Count() < 4)
                {
                    continue;
                }

                x.Add(Convert.ToDouble(columns[0]));
                y.Add(Convert.ToDouble(columns[2]));
            }
            using (var writer = File.CreateText(outputPath))
            {
                var loess = new LoessInterpolator(bandwidth, robustnessIterators);
                loess.MaxDistance = bandwidth;
                loess.Smooth(x, y, FitFunctionFactory.Create(FitFunctionTypes.Cubic));
                writer.WriteLine("NET\tNETY\tAligned\tNET\tERRORY\tERROR-Aligned");
                for (var i = 0; i < y.Count; i++)
                {
                    var value = loess.Predict(y[i]);
                    writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", x[i], y[i], value, x[i], y[i] - x[i], value - x[i]);
                }
            }
        }
Пример #4
0
        private static void WriteMatches(
            string name,
            ISpectralAnalysisWriter writer,
            List<double> xvalues,
            List<double> yvalues,
            IList<double> fit,
            LoessInterpolator interpolator,
            List<SpectralAnchorPointMatch> anchorPoints,
            List<double> preNet,
            List<double> postNet,
            Func<double, double, double> difference)
        {
            if (anchorPoints == null) throw new ArgumentNullException("anchorPoints");

            writer.WriteLine(string.Format(@"[{0}]", name));
            writer.WriteLine("x\ty\tfit\tpre-diff\tpost-diff\tsim score\tvalid");
            for (var index = 0; index < xvalues.Count; index++)
            {
                var x = xvalues[index];
                var y = yvalues[index];
                var value = interpolator.Predict(x);
                var preDiff = difference(x, y);
                var postDiff = difference(value, y);

                writer.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
                    x,
                    y,
                    fit[index],
                    preDiff,
                    postDiff,
                    anchorPoints[index].SimilarityScore,
                    anchorPoints[index].IsValidMatch));

                preNet.Add(preDiff);
                postNet.Add(postDiff);
            }
        }
Пример #5
0
        public void TestLoess(FitFunctionTypes type,
            FitFunctionTypes generatingFunction,
            double dt,
            double attenuation)
        {
            var interpolator = new LoessInterpolator(.25, 0);

            var xValues = new List<double>();
            var yValues = new List<double>();

            double cv = 0;

            var fitFunction = FitFunctionFactory.Create(type);
            var genFunction = FitFunctionFactory.Create(generatingFunction);

            var random = new Random();

            // Take one period of the sine wave...
            while (cv < Math.PI*3)
            {
                var value = genFunction(cv) + random.NextDouble()*attenuation;

                xValues.Add(cv);
                yValues.Add(value);
                cv += dt;
            }

            var newYValues = interpolator.Smooth(xValues, yValues, fitFunction);
            for (var i = 0; i < xValues.Count; i++)
            {
                Console.WriteLine(@"{0}	{1}	{2}	{3}", i, xValues[i], yValues[i], newYValues[i]);
            }

            dt /= 2;
            cv = 0;

            Console.WriteLine();

            // Take one period of the sine wave...
            while (cv < Math.PI*3)
            {
                var predicted = interpolator.Predict(cv);
                Console.WriteLine(@"{0}	{1}", cv, predicted);
                cv += dt;
            }
        }
Пример #6
0
        public void CreateAlignmentFunctions(IEnumerable <SpectralAnchorPointMatch> matches)
        {
            var netXvalues  = new List <double>();
            var netYvalues  = new List <double>();
            var massXvalues = new List <double>();
            var massYvalues = new List <double>();

            matches = matches.ToList().OrderBy(x => x.AnchorPointX.Net);

            // 1. Find the best matches
            // 2. Find only matches that have been made once.
            var bestMatches = new Dictionary <int, SpectralAnchorPointMatch>();

            foreach (var match in matches)
            {
                var scan = match.AnchorPointX.Scan;
                if (bestMatches.ContainsKey(scan))
                {
                    if (bestMatches[scan].SimilarityScore < match.SimilarityScore)
                    {
                        bestMatches[scan] = match;
                    }
                }
                else
                {
                    bestMatches.Add(scan, match);
                }
            }

            // 2. Find only those matched once
            var all = new Dictionary <int, SpectralAnchorPointMatch>();

            foreach (var match in bestMatches.Values)
            {
                var scan = match.AnchorPointY.Scan;
                if (all.ContainsKey(scan))
                {
                    if (all[scan].SimilarityScore < match.SimilarityScore)
                    {
                        all[scan] = match;
                    }
                }
                else
                {
                    all.Add(scan, match);
                }
            }

            // Then generate the NET Alignment using R1
            var anchorPoints = all.Values.OrderBy(x => x.AnchorPointY.Net).ToList();

            matches =
                anchorPoints.Where(
                    x => FeatureLight.ComputeMassPPMDifference(x.AnchorPointX.Mz, x.AnchorPointY.Mz) < 20 &&
                    x.AnchorPointX.Spectrum.ParentFeature.ChargeState == x.AnchorPointY.Spectrum.ParentFeature.ChargeState
                    ).ToList();

            foreach (var match in matches)
            {
                netXvalues.Add(match.AnchorPointX.Net);
                netYvalues.Add(match.AnchorPointY.Net);
            }

            var netInterpolator = new LoessInterpolator(Bandwidth, 5);

            netInterpolator.Smooth(netYvalues, netXvalues, FitFunctionFactory.Create(FitFunctionTypes.TriCubic));

            // Then generate the Mass Alignment using R1
            // We also have to resort the matches based on mass now too
            anchorPoints = all.Values.OrderBy(x => x.AnchorPointY.Mz).ToList();
            foreach (var match in anchorPoints)
            {
                massXvalues.Add(match.AnchorPointX.Mz);
                massYvalues.Add(match.AnchorPointY.Mz);
            }

            var massInterpolator = new LoessInterpolator();

            massInterpolator.Smooth(massYvalues, massXvalues, FitFunctionFactory.Create(FitFunctionTypes.TriCubic));

            m_netInterpolator  = netInterpolator;
            m_massInterpolator = massInterpolator;

            foreach (var match in anchorPoints)
            {
                match.AnchorPointY.NetAligned = netInterpolator.Predict(match.AnchorPointY.Net);
                match.AnchorPointY.MzAligned  = massInterpolator.Predict(match.AnchorPointY.Mz);
            }
        }
Пример #7
0
 public double AlignNet(double net)
 {
     return(m_netInterpolator.Predict(net));
 }
Пример #8
0
 public double AlignMass(double mass)
 {
     return(m_massInterpolator.Predict(mass));
 }
Пример #9
-1
        public void TestAlignmentfunction(
            string path,
            string outputPath,
            double bandwidth,
            int robustnessIterators
            )
        {
            var data = File.ReadAllLines(path);

            var x = new List<double>();
            var y = new List<double>();

            for (var i = 1; i < data.Length; i++)
            {
                var columns = data[i].Split('\t');
                if (columns.Count() < 4)
                    continue;

                x.Add(Convert.ToDouble(columns[0]));
                y.Add(Convert.ToDouble(columns[2]));
            }
            using (var writer = File.CreateText(outputPath))
            {
                var loess = new LoessInterpolator(bandwidth, robustnessIterators);
                loess.MaxDistance = bandwidth;
                loess.Smooth(x, y, FitFunctionFactory.Create(FitFunctionTypes.Cubic));
                writer.WriteLine("NET\tNETY\tAligned\tNET\tERRORY\tERROR-Aligned");
                for (var i = 0; i < y.Count; i++)
                {
                    var value = loess.Predict(y[i]);
                    writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", x[i], y[i], value, x[i], y[i] - x[i], value - x[i]);
                }
            }
        }