示例#1
0
        private static void InterpolateDimension(string name,
                                                 ISpectralAnalysisWriter writer,
                                                 List <double> xvalues,
                                                 List <double> yvalues,
                                                 List <SpectralAnchorPointMatch> anchorPoints,
                                                 Func <double, double, double> func)
        {
            var interpolator = new LoessInterpolator();
            var pre          = new List <double>();
            var post         = new List <double>();

            var fit = interpolator.Smooth(xvalues, yvalues,
                                          FitFunctionFactory.Create(FitFunctionTypes.TriCubic));

            writer.WriteLine("");
            WriteMatches(name,
                         writer,
                         xvalues,
                         yvalues,
                         fit,
                         interpolator,
                         anchorPoints,
                         pre,
                         post,
                         func);

            WriteErrorHistogram(name, pre, post, writer);
        }
        public MassAlignmentInfo DoMassAlignment(bool recollectResultsIfAlreadyPresent = false)
        {
            var needToProcessResults = (recollectResultsIfAlreadyPresent ||
                                        (IqResultsForAlignment == null || IqResultsForAlignment.Count == 0));

            if (needToProcessResults)
            {
                Execute(Targets);
                IqResultsForAlignment = FilterAlignmentResults();
            }



            var massAlignmentDataForLoess = PrepareMassAlignmentDataForLoessSmoothing(IqResultsForAlignment);


            var iterationsForMassAlignment        = 2;
            var loessInterpolatorForMassAlignment = new LoessInterpolator(LoessBandwidthMassAlignment, iterationsForMassAlignment);


            IqLogger.Log.Info("Applying Loess smoothing to mass alignment data. LoessBandwidth= " + LoessBandwidthMassAlignment);


            var massAlignmentInfo = new MassAlignmentInfoBasic();

            massAlignmentInfo.ScanAndPpmShiftVals         = new XYData();
            massAlignmentInfo.ScanAndPpmShiftVals.Xvalues = massAlignmentDataForLoess.Xvalues;
            massAlignmentInfo.ScanAndPpmShiftVals.Yvalues = loessInterpolatorForMassAlignment.Smooth(massAlignmentDataForLoess.Xvalues, massAlignmentDataForLoess.Yvalues);


            if (massAlignmentInfo.GetNumPoints() > 0)
            {
                massAlignmentInfo.AveragePpmShift = massAlignmentInfo.ScanAndPpmShiftVals.Yvalues.Average();
            }
            else
            {
                massAlignmentInfo.AveragePpmShift = double.NaN;
            }


            if (massAlignmentInfo.GetNumPoints() > 2)
            {
                massAlignmentInfo.StdevPpmShiftData = MathUtils.GetStDev(massAlignmentInfo.ScanAndPpmShiftVals.Yvalues);
            }
            else
            {
                massAlignmentInfo.StdevPpmShiftData = double.NaN;
            }


            IqLogger.Log.Info("Mass alignment complete using " + massAlignmentInfo.GetNumPoints() + " data points");

            IqLogger.Log.Info("Average ppm shift = \t" + massAlignmentInfo.AveragePpmShift.ToString("0.00") + " +/- " + massAlignmentInfo.StdevPpmShiftData.ToString("0.00"));

            MassAlignmentInfo = massAlignmentInfo;

            return(massAlignmentInfo);
        }
        public NetAlignmentInfo DoNetAlignment(bool recollectResultsIfAlreadyPresent = false)
        {
            var needToProcessResults = (recollectResultsIfAlreadyPresent ||
                                        (IqResultsForAlignment == null || IqResultsForAlignment.Count == 0));

            if (needToProcessResults)
            {
                Execute(Targets);
                IqResultsForAlignment = FilterAlignmentResults();
            }

            IqResultsForNetAlignment = UpdateMsgfTargetsWithDatabaseNetValues(IqResultsForAlignment);

            IqLogger.Log.Info("Applying Loess smoothing to NET alignment data. LoessBandwidth= " + LoessBandwidthNetAlignment);

            var netAlignmentDataForLoess = PrepareNetAlignmentDataForLoessSmoothing();



            var iterationsForNetAlignment = 2;

            var loessInterpolatorForNetAlignment = new LoessInterpolator(LoessBandwidthNetAlignment, iterationsForNetAlignment);
            var loessSmoothedData = new XYData();

            loessSmoothedData.Xvalues = netAlignmentDataForLoess.Select(p => p.Scan).ToArray();
            loessSmoothedData.Yvalues = loessInterpolatorForNetAlignment.Smooth(netAlignmentDataForLoess.Select(p => p.Scan).ToArray(),
                                                                                netAlignmentDataForLoess.Select(p => p.NET).ToArray());

            var scanToNetVals = new List <ScanNETPair>();

            for (var i = 0; i < loessSmoothedData.Xvalues.Length; i++)
            {
                var xval = loessSmoothedData.Xvalues[i];
                var yval = loessSmoothedData.Yvalues[i];

                if (!double.IsNaN(yval))
                {
                    var scanNETPair = new ScanNETPair(xval, yval);
                    scanToNetVals.Add(scanNETPair);
                }
            }

            var netAlignmentInfo = new NetAlignmentInfoBasic(Run.MinLCScan, Run.MaxLCScan);

            netAlignmentInfo.SetScanToNETAlignmentData(scanToNetVals);


            IqLogger.Log.Info("NET alignment complete using " + scanToNetVals.Count + " data points.");

            NetAlignmentInfo = netAlignmentInfo;

            return(netAlignmentInfo);
        }
示例#4
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;
            }
        }
示例#5
0
        public override void Train(double[] xArr, double[] yArr, CustomCancellationToken token)
        {
            //Calculate lowess
            Array.Sort(xArr, yArr);
            double[] lowessArr;
            if (xArr.Length > 2)
            {
                LoessInterpolator interpolator = new LoessInterpolator(Math.Max(_bandwidth, 2.0 / xArr.Length), _robustIters);
                lowessArr = interpolator.Smooth(xArr, yArr, token);
            }
            else
            {
                lowessArr = yArr;
            }

            _minX = xArr[0];
            _maxX = xArr[xArr.Length - 1];

            _minY = lowessArr.Min();
            _maxY = lowessArr.Max();

            _xArr         = xArr;
            _yArrOrig     = yArr;
            _yArrSmoothed = lowessArr;

            var sum = 0.0;

            for (int i = 0; i < _yArrOrig.Length; i++)
            {
                var e = _yArrOrig[i] - lowessArr[i];
                sum += (e * e) / _yArrOrig.Length;
            }
            _rmsd = Math.Sqrt(sum);

            if (CanCalculateReverseRegression)
            {
                //We must copy arrays and sort twice since
                //X and Y arrays are not necessarily monotonically increasing
                _reverseXArr = new double[_xArr.Length];
                _reverseYArr = new double[_yArrSmoothed.Length];

                Array.Copy(_xArr, _reverseYArr, _xArr.Length);
                Array.Copy(_yArrSmoothed, _reverseXArr, _yArrSmoothed.Length);

                Array.Sort(_reverseXArr, _reverseYArr);
            }
        }
示例#6
0
        public void LoessOnMassAlignmentData1()
        {
            var testFile = @"\\protoapps\UserData\Slysz\DeconTools_TestFiles\MassAlignmentTesting\scanPpmErrorValues1.txt";

            var xydata = TestUtilities.LoadXYDataFromFile(testFile);



            var loessInterpolator = new LoessInterpolator();

            var smoothedData = loessInterpolator.Smooth(xydata.Xvalues, xydata.Yvalues);

            for (var i = 0; i < xydata.Xvalues.Length; i++)
            {
                Console.WriteLine(xydata.Xvalues[i] + "\t" + xydata.Yvalues[i] + "\t" + smoothedData[i]);
            }
        }
示例#7
0
        public void LoessOnNETAlignmentData2()
        {
            var testFile =
                @"C:\Users\d3x720\Documents\PNNL\My_DataAnalysis\2013\IQ\2013_06_04_Using MSGF for IQ alignment - part2\QCShew_ScanVsTheorNetValues.txt";

            var xydata = TestUtilities.LoadXYDataFromFile(testFile);

            var scanNetVals = new Dictionary <decimal, double>();

            for (var i = 0; i < xydata.Xvalues.Length; i++)
            {
                var xval = (decimal)Math.Round(xydata.Xvalues[i], 1);
                var yval = xydata.Yvalues[i];

                if (!scanNetVals.ContainsKey(xval))
                {
                    scanNetVals.Add(xval, yval);
                }
            }

            var xyDataForInterpolator = new XYData();

            xyDataForInterpolator.Xvalues = scanNetVals.Keys.Select(p => (double)p).ToArray();
            xyDataForInterpolator.Yvalues = scanNetVals.Values.ToArray();

            var bandwidth         = 0.1;
            var robustIter        = 2;
            var loessInterpolator = new LoessInterpolator(bandwidth, robustIter);


            var smoothedData = loessInterpolator.Smooth(xyDataForInterpolator.Xvalues, xyDataForInterpolator.Yvalues);

            var sb = new StringBuilder();

            for (var i = 0; i < xyDataForInterpolator.Xvalues.Length; i++)
            {
                sb.Append(xyDataForInterpolator.Xvalues[i] + "\t" + xyDataForInterpolator.Yvalues[i] + "\t" + smoothedData[i]);
                sb.Append(Environment.NewLine);
            }

            Console.WriteLine(sb.ToString());
        }
示例#8
0
        public void LoessOnNETAlignmentData1()
        {
            var testFile = @"\\protoapps\UserData\Slysz\DeconTools_TestFiles\MassAlignmentTesting\scanToNetTheorData1.txt";

            var xydata = TestUtilities.LoadXYDataFromFile(testFile);

            var scanNetVals = new Dictionary <decimal, double>();

            for (var i = 0; i < xydata.Xvalues.Length; i++)
            {
                var xval = (decimal)Math.Round(xydata.Xvalues[i], 1);
                var yval = xydata.Yvalues[i];

                if (!scanNetVals.ContainsKey(xval))
                {
                    scanNetVals.Add(xval, yval);
                }
            }

            var xyDataForInterpolator = new XYData();

            xyDataForInterpolator.Xvalues = scanNetVals.Keys.Select(p => (double)p).ToArray();
            xyDataForInterpolator.Yvalues = scanNetVals.Values.ToArray();

            var bandwidth         = 0.005;
            var robustIter        = 4;
            var loessInterpolator = new LoessInterpolator(bandwidth, robustIter);


            var smoothedData = loessInterpolator.Smooth(xyDataForInterpolator.Xvalues, xyDataForInterpolator.Yvalues);

            var sb = new StringBuilder();

            for (var i = 0; i < xyDataForInterpolator.Xvalues.Length; i++)
            {
                sb.Append(xyDataForInterpolator.Xvalues[i] + "\t" + xyDataForInterpolator.Yvalues[i] + "\t" + smoothedData[i]);
                sb.Append(Environment.NewLine);
            }

            Console.WriteLine(sb.ToString());
        }
示例#9
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]);
                }
            }
        }
        public void TestCalibration()
        {
            string uimfFileLocation = Cheetah;

            LcImsPeptideSearchParameters parameters = new LcImsPeptideSearchParameters
            {
                ChargeStateMax = 5,
                NetTolerance = 0.5,
                IsotopicFitScoreThreshold = 0.15,
                MassToleranceInPpm = 30,
                NumPointForSmoothing = 9
            };

            List<PeptideTarget> targetList = MassTagImporter.ImportMassTags("elmer", "MT_Human_Sarcopenia_P789", 1e-10, true);
            Console.WriteLine("Using " + targetList.Count + " targets for calibration.");

            List<Tuple<double, double>> netAlignmentInput = new List<Tuple<double, double>>();
            List<Tuple<double, double>> massAlignmentInput = new List<Tuple<double, double>>();

            LcImsPeptideSearchWorkfow lcImsPeptideSearchWorkfow = new LcImsPeptideSearchWorkfow(uimfFileLocation, parameters);

            foreach (var imsTarget in targetList.OrderBy(x => x.NormalizedElutionTime))
            {
                ChargeStateCorrelationResult correlationResult = lcImsPeptideSearchWorkfow.RunInformedWorkflow(imsTarget);

                if (correlationResult == null || !correlationResult.CorrelatedResults.Any()) continue;

                LcImsTargetResult result = correlationResult.CorrelatedResults.OrderByDescending(x => x.Intensity).First();
                //LcImsTargetResult result = correlationResult.CorrelatedResults.OrderByDescending(X => X.Intensity * (1 - Math.Abs(X.NormalizedElutionTime - imsTarget.NormalizedElutionTime))).First();
                //LcImsTargetResult result = correlationResult.CorrelatedResults.OrderBy(X => X.NormalizedElutionTime).First();

                //if (netAlignmentInput.Count == 0 || Math.Abs(netAlignmentInput.Last().Item1 - imsTarget.NormalizedElutionTime) > 0.0001)
                //{
                //    netAlignmentInput.Add(new Tuple<double, double>(imsTarget.NormalizedElutionTime, result.NormalizedElutionTime));
                //    massAlignmentInput.Add(new Tuple<double, double>(imsTarget.NormalizedElutionTime, result.PpmError));
                //}

                netAlignmentInput.Add(new Tuple<double, double>(result.NormalizedElutionTime, imsTarget.NormalizedElutionTime));
                massAlignmentInput.Add(new Tuple<double, double>(result.IsotopicProfile.MonoPeakMZ, result.PpmError));
            }

            var netAlignmentInputGroup = netAlignmentInput.GroupBy(x => x.Item1).OrderBy(x => x.Key);
            var massAlignmentInputGroup = massAlignmentInput.GroupBy(x => x.Item1).OrderBy(x => x.Key);

            netAlignmentInput = netAlignmentInput.OrderBy(x => x.Item1).ToList();
            massAlignmentInput = massAlignmentInput.OrderBy(x => x.Item1).ToList();

            var groupedNetTuple = netAlignmentInputGroup.Select(x => x.OrderBy(y => Math.Abs(y.Item1 - y.Item2)).First()).ToArray();
            //var groupedNetTuple = netAlignmentInputGroup.Select(X => X.Average(Y => Y.Item2)).ToArray();
            var groupedMassTuple = massAlignmentInputGroup.Select(x => x.First()).ToArray();

            var loessInterpolatorForNetAlignment = new LoessInterpolator(0.1, 4);
            var loessInterpolatorForMassAlignment = new LoessInterpolator(0.2, 1);

            //double[] newNetValues = loessInterpolatorForNetAlignment.Smooth(netAlignmentInputGroup.Select(X => X.Key).ToArray(), netAlignmentInputGroup.Select(X => X.Average(Y => Y.Item2)).ToArray());
            double[] newNetValues = loessInterpolatorForNetAlignment.Smooth(groupedNetTuple.Select(x => x.Item1).ToArray(), groupedNetTuple.Select(x => x.Item2).ToArray());
            double[] newMassValues = loessInterpolatorForMassAlignment.Smooth(groupedMassTuple.Select(x => x.Item1).ToArray(), groupedMassTuple.Select(x => x.Item2).ToArray());

            using(StreamWriter writer = new StreamWriter("oldNetValues.csv"))
            {
                foreach (var netTuple in groupedNetTuple)
                {
                    writer.WriteLine(netTuple.Item1 + "," + netTuple.Item2);
                }
            }

            using (StreamWriter writer = new StreamWriter("oldMassValues.csv"))
            {
                foreach (var massTuple in groupedMassTuple)
                {
                    writer.WriteLine(massTuple.Item1 + "," + massTuple.Item2);
                }
            }

            using (StreamWriter writer = new StreamWriter("smoothedNetValues.csv"))
            {
                for (int i = 0; i < groupedNetTuple.Length; i++)
                {
                    writer.WriteLine(groupedNetTuple[i].Item1 + "," + newNetValues[i]);
                }
            }

            using (StreamWriter writer = new StreamWriter("smoothedMassValues.csv"))
            {
                for (int i = 0; i < groupedMassTuple.Length; i++)
                {
                    writer.WriteLine(groupedMassTuple[i].Item1 + "," + newMassValues[i]);
                }
            }
        }
        public void TestCreateNewCalibrationFromOldCalibration()
        {
            FileInfo fileInfo = new FileInfo("oldNetValues.csv");

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

            xValues.Add(0);
            yValues.Add(0);

            using (TextReader reader = new StreamReader(fileInfo.FullName))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] splitLine = line.Split(',');

                    double xValue = double.Parse(splitLine[0]);
                    double yValue = double.Parse(splitLine[1]);

                    xValues.Add(xValue);
                    yValues.Add(yValue);
                }
            }

            xValues.Add(1);
            yValues.Add(1);

            var loessInterpolatorForNetAlignment = new LoessInterpolator(0.1, 4);

            double[] newNetValues = loessInterpolatorForNetAlignment.Smooth(xValues.ToArray(), yValues.ToArray());

            using (StreamWriter writer = new StreamWriter("smoothedNetValues.csv"))
            {
                for (int i = 0; i < xValues.Count; i++)
                {
                    writer.WriteLine(xValues[i] + "," + newNetValues[i]);
                }
            }
        }
示例#12
0
        private static void InterpolateDimension(string name,
            ISpectralAnalysisWriter writer,
            List<double> xvalues,
            List<double> yvalues,
            List<SpectralAnchorPointMatch> anchorPoints,
            Func<double, double, double> func)
        {
            var interpolator = new LoessInterpolator();
            var pre = new List<double>();
            var post = new List<double>();

            var fit = interpolator.Smooth(xvalues, yvalues,
                FitFunctionFactory.Create(FitFunctionTypes.TriCubic));

            writer.WriteLine("");
            WriteMatches(name,
                writer,
                xvalues,
                yvalues,
                fit,
                interpolator,
                anchorPoints,
                pre,
                post,
                func);

            WriteErrorHistogram(name, pre, post, writer);
        }
示例#13
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;
            }
        }
示例#14
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);
            }
        }
示例#15
-1
文件: Loess.cs 项目: msdna/MultiAlign
        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]);
                }
            }
        }
        public void TestRunAllTargetsSqlOutput()
        {
            // Setup sqlite output file by deleting any current file and copying a blank schema over
            string sqliteSchemaLocation = @"..\..\..\testFiles\informedSchema.db3";
            string sqliteOutputLocation = "Sarc_Many_Datasets.db3";
            if(File.Exists(sqliteOutputLocation)) File.Delete(sqliteOutputLocation);
            File.Copy(sqliteSchemaLocation, sqliteOutputLocation);

            //string uimfFileLocation = @"..\..\..\testFiles\Sarc_MS2_90_6Apr11_Cheetah_11-02-19.uimf";
            //string uimfFileLocation = @"..\..\..\testFiles\Sarc_P23_C07_2143_23Feb12_Cheetah_11-05-40.uimf";

            //IInterpolation interpolation = AlignmentImporter.ReadFile(netAlignmentFileLocation);

            // Setup calibration workflow and targets
            LcImsPeptideSearchParameters calibrationParameters = new LcImsPeptideSearchParameters
            {
                ChargeStateMax = 5,
                NetTolerance = 0.5,
                IsotopicFitScoreThreshold = 0.15,
                MassToleranceInPpm = 30,
                NumPointForSmoothing = 9
            };

            List<PeptideTarget> calibrationTargetList = MassTagImporter.ImportMassTags("elmer", "MT_Human_Sarcopenia_P789", 1e-10, true);
            Console.WriteLine("Using " + calibrationTargetList.Count + " targets for calibration.");

            LcImsPeptideSearchParameters parameters = new LcImsPeptideSearchParameters
            {
                ChargeStateMax = 5,
                NetTolerance = 0.03,
                IsotopicFitScoreThreshold = 0.2,
                MassToleranceInPpm = 30,
                NumPointForSmoothing = 9
            };

            List<PeptideTarget> targetList = MassTagImporter.ImportMassTags("elmer", "MT_Human_Sarcopenia_P789");
            Console.WriteLine(DateTime.Now + ": Reading ViperCompatibleMass Tags from MTDB");

            Console.WriteLine(DateTime.Now + ": Using " + targetList.Count + " targets.");

            List<string> uimfFileList = new List<string>();
            uimfFileList.Add(@"..\..\..\testFiles\Sarc_MS2_13_1Apr11_Cheetah_11-02-24.uimf");
            //uimfFileList.Add(@"..\..\..\testFiles\Sarc_MS2_27_2Apr11_Cheetah_11-01-11.uimf");
            //uimfFileList.Add(@"..\..\..\testFiles\Sarc_MS2_54_6Apr11_Cheetah_11-02-18.uimf");
            //uimfFileList.Add(@"..\..\..\testFiles\Sarc_MS2_55_3Apr11_Cheetah_11-02-15.uimf");
            //uimfFileList.Add(@"..\..\..\testFiles\Sarc_MS2_73_5Apr11_Cheetah_11-02-24.uimf");
            //uimfFileList.Add(@"..\..\..\testFiles\Sarc_MS2_90_6Apr11_Cheetah_11-02-19.uimf");
            //uimfFileList.Add(@"..\..\..\testFiles\Sarc_MS2_102_7Apr11_Cheetah_11-02-19.uimf");
            //uimfFileList.Add(@"..\..\..\testFiles\Sarc_MS2_119_8Apr11_Cheetah_11-02-18.uimf");
            //uimfFileList.Add(@"..\..\..\testFiles\Sarc_MS2_125_8Apr11_Cheetah_11-02-15.uimf");
            //uimfFileList.Add(@"..\..\..\testFiles\Sarc_MS2_146_9Apr11_Cheetah_11-02-24.uimf");

            using (var connection = new SQLiteConnection(@"Data Source=" + sqliteOutputLocation + ";New=False;"))
            {
                connection.Open();

                using (var command = new SQLiteCommand(connection))
                {
                    using (var transaction = connection.BeginTransaction())
                    {
                        Console.WriteLine(DateTime.Now + ": Writing ViperCompatibleMass Tags to database");

                        //foreach (var Target in targetList)
                        //{
                        //    string insertMassTagsQuery = Target.CreateSqlMassTagQueries();
                        //    command.CommandText = insertMassTagsQuery;
                        //    command.ExecuteNonQuery();
                        //}

                        //transaction.Commit();
                    }

                    for (int i = 0; i < uimfFileList.Count; i++)
                    {
                        string uimfFileLocation = uimfFileList[i];
                        FileInfo uimfFileInfo = new FileInfo(uimfFileLocation);
                        Console.WriteLine(DateTime.Now + ": Processing " + uimfFileInfo.Name);

                        // NET Alignment
                        string netAlignmentFileName = uimfFileInfo.Name.Replace(".uimf", "_NetAlign.csv");
                        string netAlignmentLocation = Path.Combine(uimfFileInfo.DirectoryName, netAlignmentFileName);
                        FileInfo netAlignmentFileInfo = new FileInfo(netAlignmentLocation);
                        if (!File.Exists(netAlignmentFileInfo.FullName))
                        {
                            Console.WriteLine(DateTime.Now + ": Creating alignment file using " + calibrationTargetList.Count + " possible targets.");
                            LcImsPeptideSearchWorkfow calibrationWorkflow = new LcImsPeptideSearchWorkfow(uimfFileLocation, calibrationParameters);
                            List<Tuple<double, double>> netAlignmentInput = new List<Tuple<double, double>>();

                            int index = 0;
                            // Run calibration workflow on each of the calibration targets
                            foreach (var imsTarget in calibrationTargetList.OrderBy(x => x.NormalizedElutionTime))
                            {
                                //Console.WriteLine(DateTime.Now + ": Processing Target " + index);
                                ChargeStateCorrelationResult correlationResult = calibrationWorkflow.RunInformedWorkflow(imsTarget);

                                if (correlationResult != null && correlationResult.CorrelatedResults.Any())
                                {
                                    var elutionTimeFilteredResults = correlationResult.CorrelatedResults.Where(x => x.NormalizedElutionTime >= 0.1);
                                    if (elutionTimeFilteredResults.Any())
                                    {
                                        LcImsTargetResult result = correlationResult.CorrelatedResults.Where(x => x.NormalizedElutionTime >= 0.1).OrderByDescending(x => x.Intensity).First();
                                        netAlignmentInput.Add(new Tuple<double, double>(result.NormalizedElutionTime, imsTarget.NormalizedElutionTime));
                                    }
                                }

                                //Console.WriteLine(DateTime.Now + ": Done Processing Target " + index);
                                imsTarget.RemoveResults();
                                //Console.WriteLine(DateTime.Now + ": Removed results from Target " + index);

                                index++;
                            }

                            // Place data points at beginning and end to finish off the alignment
                            netAlignmentInput.Add(new Tuple<double, double>(0, 0));
                            netAlignmentInput.Add(new Tuple<double, double>(1, 1));

                            // Do LOESS to get NET alignment
                            Console.WriteLine(DateTime.Now + ": Found " + netAlignmentInput.Count + " targets to use for alignment.");
                            var netAlignmentInputGroup = netAlignmentInput.GroupBy(x => x.Item1).OrderBy(x => x.Key);
                            var groupedNetTuple = netAlignmentInputGroup.Select(x => x.OrderBy(y => Math.Abs(y.Item1 - y.Item2)).First()).ToArray();
                            var loessInterpolatorForNetAlignment = new LoessInterpolator(0.1, 4);
                            double[] xArray = groupedNetTuple.Select(x => x.Item1).ToArray();
                            double[] yArray = groupedNetTuple.Select(x => x.Item2).ToArray();
                            double[] newNetValues = loessInterpolatorForNetAlignment.Smooth(xArray, yArray);

                            // Creates a file for the NET Alignment to be stored
                            using (StreamWriter writer = new StreamWriter(netAlignmentFileInfo.FullName))
                            {
                                for (int j = 0; j < groupedNetTuple.Length; j++)
                                {
                                    writer.WriteLine(groupedNetTuple[j].Item1 + "," + newNetValues[j]);
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now + ": Using existing alignment file");
                        }

                        // Grab the net alignment
                        IInterpolation interpolation = AlignmentImporter.ReadFile(netAlignmentFileInfo.FullName);

                        LcImsPeptideSearchWorkfow lcImsPeptideSearchWorkfow = new LcImsPeptideSearchWorkfow(uimfFileInfo.FullName, parameters, interpolation);

                        //using (var transaction = connection.BeginTransaction())
                        //{
                        //    string insertDatasetQuery = "INSERT INTO T_Dataset (Dataset_Id, File_Name) VALUES(" + i + ",'" + uimfFileInfo.Name + "');";
                        //    command.CommandText = insertDatasetQuery;
                        //    command.ExecuteNonQuery();
                        //    transaction.Commit();
                        //}

                        List<ChargeStateCorrelationResult> resultList = new List<ChargeStateCorrelationResult>();

                        foreach (var imsTarget in targetList)
                        {
                            using (var transaction = connection.BeginTransaction())
                            {
                                ChargeStateCorrelationResult correlationResult = lcImsPeptideSearchWorkfow.RunInformedWorkflow(imsTarget);

                                if (correlationResult != null)
                                {
                                    resultList.Add(correlationResult);
                                }

                                //string queries = imsTarget.CreateSqlResultQueries(i);
                                //command.CommandText = queries;
                                //command.ExecuteNonQuery();

                                //if (correlationResult != null && correlationResult.CorrelationMap.Count > 1)
                                //{
                                //    string correlationQueries = correlationResult.CreateSqlUpdateQueries();
                                //    if (correlationQueries != "")
                                //    {
                                //        command.CommandText = correlationQueries;
                                //        command.ExecuteNonQuery();
                                //    }
                                //}

                                //transaction.Commit();
                            }

                            // Reset the Target so it can be used again by another dataset
                            imsTarget.RemoveResults();
                        }

                        foreach (var chargeStateCorrelationResult in resultList)
                        {

                        }
                    }
                }

                connection.Close();
            }
        }