//Used by matching part to prevent pass by reference.
        private ResultsGroup matchPassbyValue(ResultsGroup input1, CompositionHypothesisEntry comhypo)
        {
            ResultsGroup storage = new ResultsGroup();
            //Pass by value, I only way I know we can do this is to pass them one by one. Yes, it is troublesome.
            storage.DeconRow = input1.DeconRow;
            storage.MostAbundant = input1.MostAbundant;
            storage.NumChargeStates = input1.NumChargeStates;
            storage.ScanDensity = input1.ScanDensity;
            storage.NumModiStates = input1.NumModiStates;
            storage.TotalVolume = input1.TotalVolume;
            storage.ExpectedA = input1.ExpectedA;
            storage.CentroidScan = input1.CentroidScan;
            storage.NumOfScan = input1.NumOfScan;
            storage.AvgSigNoise = input1.AvgSigNoise;
            storage.MaxScanNum = input1.MaxScanNum;
            storage.MinScanNum = input1.MinScanNum;
            storage.ScanNumList = input1.ScanNumList;
            storage.ChargeStateList = input1.ChargeStateList;
            storage.AvgSigNoiseList = input1.AvgSigNoiseList;
            storage.CentroidScanLR = input1.CentroidScanLR;
            storage.AvgAA2List = input1.AvgAA2List;

            storage.PredictedComposition = comhypo;
            storage.Match = true;
            return storage;
        }
        //this "Grouping" function performs the grouping.
        private List<ResultsGroup> Groupings(String filename, ParametersForm.ParameterSettings modelParameters, Double Mas, List<CompositionHypothesisEntry> comhyp)
        {
            GetDeconData DeconDATA1 = new GetDeconData();

            List<string> elementIDs = new List<string>();
            List<string> molename = new List<string>();
            for (int i = 0; i < comhyp.Count(); i++ )
            {
                if (comhyp[i].ElementNames.Count > 0)
                {
                    for (int j = 0; j < comhyp[i].ElementNames.Count(); j++)
                    {
                        elementIDs.Add(comhyp[i].ElementNames[j]);
                    }
                    for (int j = 0; j < comhyp[i].MoleculeNames.Count(); j++)
                    {
                        molename.Add(comhyp[i].MoleculeNames[j]);
                    }
                    break;
                }
            }
            List<DeconRow> sortedDeconData = new List<DeconRow>();;
            sortedDeconData = DeconDATA1.getdata(filename);
            //First, sort the list descendingly by its abundance.
            sortedDeconData = sortedDeconData.OrderByDescending(a => a.abundance).ToList();
            //################Second, create a new list to store results from the first grouping.###############
            List<ResultsGroup> fgResults = new List<ResultsGroup>();
            ResultsGroup GR2 = new ResultsGroup();
            Int32 currentMaxBin = new Int32();
            currentMaxBin = 1;
            GR2.DeconRow = sortedDeconData[0];
            GR2.MostAbundant = true;
            GR2.NumOfScan = 1;
            GR2.MinScanNum = sortedDeconData[0].ScanNum;
            GR2.MaxScanNum = sortedDeconData[0].ScanNum;
            GR2.ChargeStateList = new List<int>();
            GR2.ChargeStateList.Add(sortedDeconData[0].charge);
            GR2.AvgSigNoiseList = new List<Double>();
            GR2.AvgSigNoiseList.Add(sortedDeconData[0].SignalNoiseRatio);
            GR2.AvgAA2List = new List<double>();
            GR2.AvgAA2List.Add(sortedDeconData[0].MonoisotopicAbundance / (sortedDeconData[0].MonoisotopicPlus2Abundance + 1));
            GR2.ScanNumList = new List<Int32>();
            GR2.ScanNumList.Add(sortedDeconData[0].ScanNum);
            GR2.NumModiStates = 1;
            GR2.TotalVolume = sortedDeconData[0].abundance * sortedDeconData[0].fwhm;
            GR2.ListAbundance = new List<double>();
            GR2.ListAbundance.Add(sortedDeconData[0].abundance);
            GR2.ListMonoMassWeight = new List<double>();
            GR2.ListMonoMassWeight.Add(sortedDeconData[0].MonoisotopicMassWeight);
            fgResults.Add(GR2);
            for (int j = 1; j < sortedDeconData.Count; j++)
            {
                for (int i = 0; i < fgResults.Count; i++)
                {
                    //Obtain grouping error. Note: its in ppm, so it needs to be multiplied by 0.000001.
                    Double GroupingError = fgResults[i].DeconRow.MonoisotopicMassWeight * modelParameters.GroupingErrorEG * 0.000001;
                    if ((sortedDeconData[j].MonoisotopicMassWeight < (fgResults[i].DeconRow.MonoisotopicMassWeight + GroupingError) && (sortedDeconData[j].MonoisotopicMassWeight > (fgResults[i].DeconRow.MonoisotopicMassWeight - GroupingError))))
                    {
                        if (fgResults[i].MaxScanNum < sortedDeconData[j].ScanNum)
                        {
                            fgResults[i].MaxScanNum = sortedDeconData[j].ScanNum;
                        }
                        else if (fgResults[i].MinScanNum > sortedDeconData[j].ScanNum)
                        {
                            fgResults[i].MinScanNum = sortedDeconData[j].ScanNum;
                        }
                        fgResults[i].NumOfScan = fgResults[i].NumOfScan + 1;
                        fgResults[i].ScanNumList.Add(sortedDeconData[j].ScanNum);
                        fgResults[i].TotalVolume = fgResults[i].TotalVolume + sortedDeconData[j].abundance * sortedDeconData[j].fwhm;
                        fgResults[i].ChargeStateList.Add(sortedDeconData[j].charge);
                        fgResults[i].AvgSigNoiseList.Add(sortedDeconData[j].SignalNoiseRatio);
                        fgResults[i].AvgAA2List.Add(sortedDeconData[j].MonoisotopicAbundance / (sortedDeconData[j].MonoisotopicPlus2Abundance + 1));
                        fgResults[i].ListAbundance.Add(sortedDeconData[j].abundance);
                        fgResults[i].ListMonoMassWeight.Add(sortedDeconData[j].MonoisotopicMassWeight);
                        break;
                    }

                    if (i == fgResults.Count - 1)
                    {
                        ResultsGroup GR = new ResultsGroup();
                        currentMaxBin = currentMaxBin + 1;
                        GR.DeconRow = sortedDeconData[j];
                        GR.MostAbundant = true;
                        GR.NumOfScan = 1;
                        GR.MinScanNum = sortedDeconData[j].ScanNum;
                        GR.MaxScanNum = sortedDeconData[j].ScanNum;
                        GR.ChargeStateList = new List<int>();
                        GR.ChargeStateList.Add(sortedDeconData[j].charge);
                        GR.AvgSigNoiseList = new List<Double>();
                        GR.AvgSigNoiseList.Add(sortedDeconData[j].SignalNoiseRatio);
                        GR.AvgAA2List = new List<double>();
                        GR.AvgAA2List.Add(sortedDeconData[j].MonoisotopicAbundance / (sortedDeconData[j].MonoisotopicPlus2Abundance + 1));
                        GR.ScanNumList = new List<int>();
                        GR.ScanNumList.Add(sortedDeconData[j].ScanNum);
                        GR.NumModiStates = 1;
                        GR.TotalVolume = sortedDeconData[j].abundance * sortedDeconData[j].fwhm;
                        GR.ListAbundance = new List<double>();
                        GR.ListAbundance.Add(sortedDeconData[j].abundance);
                        GR.ListMonoMassWeight = new List<double>();
                        GR.ListMonoMassWeight.Add(sortedDeconData[j].MonoisotopicMassWeight);
                        fgResults.Add(GR);
                    }
                }
            }
            //Lastly calculate the Average Weighted Abundance
            for (int y = 0; y < fgResults.Count(); y++)
            {
                Double sumofTopPart = 0;
                for (int z = 0; z < fgResults[y].ListMonoMassWeight.Count(); z++)
                {
                    sumofTopPart = sumofTopPart + fgResults[y].ListMonoMassWeight[z] * fgResults[y].ListAbundance[z];
                }
                fgResults[y].DeconRow.MonoisotopicMassWeight = sumofTopPart / fgResults[y].ListAbundance.Sum();
            }

            //######################## Here is the second grouping. ################################
            fgResults = fgResults.OrderBy(o => o.DeconRow.MonoisotopicMassWeight).ToList();
            if (Mas != 0)
            {
                for (int i = 0; i < fgResults.Count - 1; i++)
                {
                    if (fgResults[i].MostAbundant == true)
                    {
                        int numModStates = 1;
                        for (int j = i + 1; j < fgResults.Count; j++)
                        {
                            Double AdductTolerance = fgResults[i].DeconRow.MonoisotopicMassWeight * modelParameters.AdductToleranceEA * 0.000001;
                            if ((fgResults[i].DeconRow.MonoisotopicMassWeight >= (fgResults[j].DeconRow.MonoisotopicMassWeight - Mas * numModStates - AdductTolerance)) && (fgResults[i].DeconRow.MonoisotopicMassWeight <= (fgResults[j].DeconRow.MonoisotopicMassWeight - Mas * numModStates + AdductTolerance)))
                            {
                                //obtain max and min scan number
                                if (fgResults[i].MaxScanNum < fgResults[j].MaxScanNum)
                                {
                                    fgResults[i].MaxScanNum = fgResults[j].MaxScanNum;
                                }
                                else
                                {
                                    fgResults[i].MaxScanNum = fgResults[i].MaxScanNum;
                                }

                                if (fgResults[i].MinScanNum > fgResults[j].MinScanNum)
                                {
                                    fgResults[i].MinScanNum = fgResults[j].MinScanNum;
                                }
                                else
                                {
                                    fgResults[i].MinScanNum = fgResults[i].MinScanNum;
                                }
                                //numOfScan
                                fgResults[i].NumOfScan = fgResults[i].NumOfScan + fgResults[j].NumOfScan;
                                fgResults[i].ScanNumList.AddRange(fgResults[j].ScanNumList);
                                //ChargeStateList
                                for (int h = 0; h < fgResults[j].ChargeStateList.Count; h++)
                                {
                                    fgResults[i].ChargeStateList.Add(fgResults[j].ChargeStateList[h]);
                                }
                                //avgSigNoiseList
                                for (int h = 0; h < fgResults[j].AvgSigNoiseList.Count; h++)
                                {
                                    fgResults[i].AvgSigNoiseList.Add(fgResults[j].AvgSigNoiseList[h]);
                                }
                                //avgAA2List
                                for (int h = 0; h < fgResults[j].AvgAA2List.Count; h++)
                                {
                                    fgResults[i].AvgAA2List.Add(fgResults[j].AvgAA2List[h]);
                                }
                                //numModiStates
                                numModStates++;
                                fgResults[i].NumModiStates = fgResults[i].NumModiStates + 1;
                                fgResults[j].MostAbundant = false;
                                //TotalVolume
                                fgResults[i].TotalVolume = fgResults[i].TotalVolume + fgResults[j].TotalVolume;
                                if (fgResults[i].DeconRow.abundance < fgResults[j].DeconRow.abundance)
                                {
                                    fgResults[i].DeconRow = fgResults[j].DeconRow;
                                    numModStates = 1;
                                }
                            }
                            else if (fgResults[i].DeconRow.MonoisotopicMassWeight < (fgResults[j].DeconRow.MonoisotopicMassWeight - (Mas + AdductTolerance * 2) * numModStates))
                            {
                                //save running time. Since the list is sorted, any other mass below won't match as an adduct.
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < fgResults.Count; i++)
                {
                    fgResults[i].NumModiStates = 0;
                }
            }
            List<ResultsGroup> sgResults = new List<ResultsGroup>();
            //Implement the scan number threshold
            fgResults = fgResults.OrderByDescending(a => a.NumOfScan).ToList();
            Int32 scanCutOff = fgResults.Count() + 1;
            for (int t = 0; t < fgResults.Count(); t++)
            {
                if (fgResults[t].NumOfScan < modelParameters.MinScanNumber)
                {
                    scanCutOff = t;
                    break;
                }
            }
            if (scanCutOff != fgResults.Count() + 1)
            {
                fgResults.RemoveRange(scanCutOff, fgResults.Count() - scanCutOff);
            }

            //############# This is the matching part. It matches the composition hypothesis with the grouped decon data.############
            String[] MolNames = new String[17];

            //These numOfMatches and lists are used to fit the linear regression model for Expect A: A+2. They are put here to decrease the already-int running time.
            Int32 numOfMatches = new Int32();
            List<Double> moleWeightforA = new List<Double>();
            List<Double> AARatio = new List<Double>();
            //Used to obtain all available bins for centroid scan error.
            //Read the other lines for compTable data.
            fgResults = fgResults.OrderByDescending(a => a.DeconRow.MonoisotopicMassWeight).ToList();
            comhyp = comhyp.OrderByDescending(b => b.MassWeight).ToList();
            bool hasMatch = false;
            int lastMatch = 0;
            for (int j = 0; j < fgResults.Count; j++)
            {
                if (fgResults[j].MostAbundant == true)
                {
                    lastMatch = lastMatch - 4;
                    if (lastMatch < 0)
                        lastMatch = 0;
                    for (int i = lastMatch; i < comhyp.Count; i++)
                    {

                        Double MatchingError = comhyp[i].MassWeight * modelParameters.MatchErrorEM * 0.000001;
                        if ((fgResults[j].DeconRow.MonoisotopicMassWeight <= (comhyp[i].MassWeight + MatchingError)) && (fgResults[j].DeconRow.MonoisotopicMassWeight >= (comhyp[i].MassWeight - MatchingError)))
                        {
                            ResultsGroup GR = new ResultsGroup();
                            GR = matchPassbyValue(fgResults[j], comhyp[i]);
                            sgResults.Add(GR);
                            //Stuffs for feature
                            numOfMatches++;
                            moleWeightforA.Add(fgResults[j].DeconRow.MonoisotopicMassWeight);
                            AARatio.Add(fgResults[j].AvgAA2List.Average());
                            lastMatch = i + 1;
                            hasMatch = true;
                            continue;
                        }
                        //Since the data is sorted, there are no more matches below that row, break it.
                        if (fgResults[j].DeconRow.MonoisotopicMassWeight > (comhyp[i].MassWeight + MatchingError))
                        {
                            if (hasMatch == false)
                            {
                                ResultsGroup GR = new ResultsGroup();
                                CompositionHypothesisEntry comhypi = new CompositionHypothesisEntry();
                                GR = fgResults[j];
                                GR.Match = false;
                                GR.PredictedComposition = comhypi;
                                sgResults.Add(GR);
                                lastMatch = i;
                                break;
                            }
                            else
                            {
                                hasMatch = false;
                                break;
                            }
                        }
                    }
                }
            }

            //##############Last part, this is to calculate the feature data needed for logistic regression###################
            //Expected A and Centroid Scan Error need linear regression. The models are built here separately.
            //In the this model. output is the Y axis and input is X.
            SimpleLinearRegression AA2regression = new SimpleLinearRegression();
            List<double> aainput = new List<double>();
            List<double> aaoutput = new List<double>();
            //Centroid Scan Error
            List<double> ccinput = new List<double>();
            List<double> ccoutput = new List<double>();
            if (numOfMatches > 3)
            {
                for (int i = 0; i < sgResults.Count; i++)
                {
                    if (sgResults[i].Match == true)
                    {
                        if (sgResults[i].AvgAA2List.Average() != 0)
                        {
                            aainput.Add(sgResults[i].DeconRow.MonoisotopicMassWeight);
                            aaoutput.Add(sgResults[i].AvgAA2List.Average());
                        }
                        if (sgResults[i].DeconRow.abundance > 250)
                        {
                            ccoutput.Add(sgResults[i].DeconRow.ScanNum);
                            ccinput.Add(sgResults[i].DeconRow.MonoisotopicMassWeight);
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < sgResults.Count; i++)
                {
                    if (sgResults[i].AvgAA2List.Average() != 0)
                    {
                        aainput.Add(sgResults[i].DeconRow.MonoisotopicMassWeight);
                        aaoutput.Add(sgResults[i].AvgAA2List.Average());
                    }
                    if (sgResults[i].DeconRow.abundance > 250)
                    {
                        ccoutput.Add(sgResults[i].ScanNumList.Average());
                        ccinput.Add(sgResults[i].DeconRow.MonoisotopicMassWeight);
                    }
                }
            }
            SimpleLinearRegression CSEregression = new SimpleLinearRegression();
            CSEregression.Regress(ccinput.ToArray(), ccoutput.ToArray());
            AA2regression.Regress(aainput.ToArray(), aaoutput.ToArray());

            //The remaining features and input them into the grouping results
            for (int i = 0; i < sgResults.Count; i++)
            {
                //ScanDensiy is: Number of scan divided by (max scan number – min scan number)
                Double ScanDensity = new Double();
                Int32 MaxScanNumber = sgResults[i].MaxScanNum;
                Int32 MinScanNumber = sgResults[i].MinScanNum;
                Double NumOfScan = sgResults[i].NumOfScan;
                List<Int32> numChargeStatesList = sgResults[i].ChargeStateList.Distinct().ToList();
                Int32 numChargeStates = numChargeStatesList.Count;
                Double numModiStates = sgResults[i].NumModiStates;
                if ((MaxScanNumber - MinScanNumber) != 0)
                    ScanDensity = NumOfScan / (MaxScanNumber - MinScanNumber + 15);
                else
                    ScanDensity = 0;
                //Use this scandensity for all molecules in this grouping.

                sgResults[i].NumChargeStates = numChargeStates;
                sgResults[i].ScanDensity = ScanDensity;
                sgResults[i].NumModiStates = numModiStates;
                sgResults[i].CentroidScanLR = CSEregression.Compute(sgResults[i].DeconRow.MonoisotopicMassWeight);
                sgResults[i].CentroidScan = Math.Abs(sgResults[i].ScanNumList.Average() - sgResults[i].CentroidScanLR);
                sgResults[i].ExpectedA = Math.Abs(sgResults[i].AvgAA2List.Average() - AA2regression.Compute(sgResults[i].DeconRow.MonoisotopicMassWeight));
                sgResults[i].AvgSigNoise = sgResults[i].AvgSigNoiseList.Average();
            }
            for (int i = 0; i < sgResults.Count(); i++ )
            {
                sgResults[i].PredictedComposition.ElementNames.Clear();
                sgResults[i].PredictedComposition.MoleculeNames.Clear();

                if (i == sgResults.Count() - 1)
                {
                    sgResults[0].PredictedComposition.ElementNames = elementIDs;
                    sgResults[0].PredictedComposition.MoleculeNames = molename;
                }
            }
            return sgResults;
        }
        //This is used to read a ResultFile
        public List<ResultsGroup> ReadResultsFromFile(String path)
        {
            //This code looks int, but its just repetitive code. Look for ext and you will understand.
            List<ResultsGroup> Ans = new List<ResultsGroup>();
            List<String> molnames = new List<String>();
            FileStream FS = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader read = new StreamReader(FS);
            String ext = Path.GetExtension(path).Replace(".", "");

            if (ext == "csv")
            {
                String header = read.ReadLine();
                String[] headers = header.Split(',');
                List<string> elementIDs = new List<string>();
                //This is another older form of data
                if (headers[5] != "Hypothesis MW")
                {
                    Boolean moreCompounds = true;
                    int i = 17;
                    while (moreCompounds)
                    {
                        if (headers[i] != "Hypothesis MW")
                        {
                            elementIDs.Add(headers[i]);
                            i++;
                        }
                        else
                        {
                            moreCompounds = false;
                            i++;
                        }
                    }
                    moreCompounds = true;
                    while (moreCompounds)
                    {
                        if (headers[i] != "Adduct/Replacement")
                        {
                            molnames.Add(headers[i]);
                            i++;
                        }
                        else
                            moreCompounds = false;
                    }
                    bool firstRow = true;
                    while (read.Peek() >= 0)
                    {
                        //Read data
                        String Line = read.ReadLine();
                        String[] Lines = Line.Split(',');
                        //initialize new gR object
                        ResultsGroup gR = new ResultsGroup();
                        DeconRow dR = new DeconRow();
                        CompositionHypothesisEntry cH = new CompositionHypothesisEntry();
                        gR.DeconRow = dR;
                        gR.PredictedComposition = cH;

                        //Input data
                        if (!String.IsNullOrEmpty(Lines[0]))
                        {
                            if (firstRow)
                            {
                                gR.PredictedComposition.ElementNames = elementIDs;
                                gR.PredictedComposition.MoleculeNames = molnames;
                                firstRow = false;
                            }

                            gR.Score = Convert.ToDouble(Lines[0]);
                            gR.DeconRow.MonoisotopicMassWeight = Convert.ToDouble(Lines[1]);
                            gR.PredictedComposition.CompoundComposition = Lines[2];
                            if (String.IsNullOrEmpty(Lines[2]) || Lines[2] == "0")
                                gR.Match = false;
                            else
                                gR.Match = true;
                            gR.PredictedComposition.PepSequence = Lines[3];
                            gR.NumModiStates = Convert.ToDouble(Lines[5]);
                            gR.NumChargeStates = Convert.ToInt32(Lines[6]);
                            gR.NumOfScan = Convert.ToDouble(Lines[7]);
                            gR.ScanDensity = Convert.ToDouble(Lines[8]);
                            gR.ExpectedA = Convert.ToDouble(Lines[9]);
                            gR.AvgAA2List = new List<double>();
                            gR.AvgAA2List.Add(Convert.ToDouble(Lines[10]));
                            gR.TotalVolume = Convert.ToDouble(Lines[11]);
                            gR.AvgSigNoise = Convert.ToDouble(Lines[12]);
                            gR.CentroidScan = Convert.ToDouble(Lines[13]);
                            gR.DeconRow.ScanNum = Convert.ToInt32(Lines[14]);
                            gR.MaxScanNum = Convert.ToInt32(Lines[15]);
                            gR.MinScanNum = Convert.ToInt32(Lines[16]);
                            gR.PredictedComposition.eqCount = new Dictionary<string, int>();
                            int sh = 17;
                            for (int ele = 0; ele < elementIDs.Count(); ele++ )
                            {
                                gR.PredictedComposition.ElementAmount.Add(Convert.ToInt32(Lines[sh]));
                                sh++;
                            }
                            gR.PredictedComposition.MassWeight = Convert.ToDouble(Lines[sh]);
                            sh++;
                            List<int> eqCoun = new List<int>();
                            for (int j = 0; j < molnames.Count(); j++)
                            {
                                eqCoun.Add(Convert.ToInt32(Lines[sh + j]));
                            }
                            gR.PredictedComposition.eqCounts = eqCoun;
                            gR.PredictedComposition.AddRep = Lines[sh + molnames.Count()];
                            gR.PredictedComposition.AdductNum = Convert.ToInt32(Lines[sh + molnames.Count() + 1]);
                            gR.PredictedComposition.PepModification = Lines[sh + molnames.Count() + 2];
                            gR.PredictedComposition.MissedCleavages = Convert.ToInt32(Lines[sh + molnames.Count() + 3]);
                            gR.PredictedComposition.NumGlycosylations = Convert.ToInt32(Lines[sh + molnames.Count() + 4]);
                            gR.PredictedComposition.StartAA = Convert.ToInt32(Lines[sh + molnames.Count() + 5]);
                            gR.PredictedComposition.EndAA = Convert.ToInt32(Lines[sh + molnames.Count() + 6]);
                            if (Lines.Count() > sh + molnames.Count() + 7)
                            {
                                gR.PredictedComposition.ProteinID = Lines[sh + molnames.Count() + 7];
                            }
                            else
                            {
                                gR.PredictedComposition.ProteinID = "?";
                            }
                            Ans.Add(gR);
                        }
                    }
                }
                //older data format.
                else if (headers[3] == "PeptideSequence")
                {
                    Boolean moreCompounds = true;
                    int i = 24;
                    while (moreCompounds)
                    {
                        if (headers[i] != "Adduct/Replacement")
                        {
                            molnames.Add(headers[i]);
                            i++;
                        }
                        else
                            moreCompounds = false;
                    }
                    bool firstRow = true;
                    while (read.Peek() >= 0)
                    {
                        //Read data
                        String Line = read.ReadLine();
                        String[] Lines = Line.Split(',');
                        //initialize new gR object
                        ResultsGroup gR = new ResultsGroup();
                        DeconRow dR = new DeconRow();
                        CompositionHypothesisEntry cH = new CompositionHypothesisEntry();
                        gR.DeconRow = dR;
                        gR.PredictedComposition = cH;
                        if (firstRow)
                        {
                            gR.PredictedComposition.ElementNames.AddRange(new List<string> { "C", "H", "N", "O", "S", "P" });
                            gR.PredictedComposition.MoleculeNames = molnames;
                            firstRow = false;
                        }

                        //Input data
                        if (!String.IsNullOrEmpty(Lines[0]))
                        {
                            gR.Score = Convert.ToDouble(Lines[0]);
                            gR.DeconRow.MonoisotopicMassWeight = Convert.ToDouble(Lines[1]);
                            gR.PredictedComposition.CompoundComposition = Lines[2];
                            if (String.IsNullOrEmpty(Lines[2]) || Lines[2] == "0")
                                gR.Match = false;
                            else
                                gR.Match = true;
                            gR.PredictedComposition.PepSequence = Lines[3];
                            gR.PredictedComposition.MassWeight = Convert.ToDouble(Lines[5]);
                            gR.NumModiStates = Convert.ToDouble(Lines[6]);
                            gR.NumChargeStates = Convert.ToInt32(Lines[7]);
                            gR.NumOfScan = Convert.ToDouble(Lines[8]);
                            gR.ScanDensity = Convert.ToDouble(Lines[9]);
                            gR.ExpectedA = Convert.ToDouble(Lines[10]);
                            gR.AvgAA2List = new List<double>();
                            gR.AvgAA2List.Add(Convert.ToDouble(Lines[11]));
                            gR.TotalVolume = Convert.ToDouble(Lines[12]);
                            gR.AvgSigNoise = Convert.ToDouble(Lines[13]);
                            gR.CentroidScan = Convert.ToDouble(Lines[14]);
                            gR.DeconRow.ScanNum = Convert.ToInt32(Lines[15]);
                            gR.MaxScanNum = Convert.ToInt32(Lines[16]);
                            gR.MinScanNum = Convert.ToInt32(Lines[17]);
                            gR.PredictedComposition.eqCount = new Dictionary<string, int>();
                            for (int k = 18; k < 24; k++)
                            {
                                gR.PredictedComposition.ElementAmount.Add(Convert.ToInt32(Lines[k]));
                            }
                            List<int> eqCoun = new List<int>();
                            for (int j = 0; j < molnames.Count(); j++)
                            {
                                eqCoun.Add(Convert.ToInt32(Lines[24 + j]));
                            }
                            gR.PredictedComposition.eqCounts = eqCoun;
                            gR.PredictedComposition.AddRep = Lines[24 + molnames.Count()];
                            gR.PredictedComposition.AdductNum = Convert.ToInt32(Lines[24 + molnames.Count() + 1]);
                            gR.PredictedComposition.PepModification = Lines[24 + molnames.Count() + 2];
                            gR.PredictedComposition.MissedCleavages = Convert.ToInt32(Lines[24 + molnames.Count() + 3]);
                            gR.PredictedComposition.NumGlycosylations = Convert.ToInt32(Lines[24 + molnames.Count() + 4]);
                            Ans.Add(gR);
                        }
                    }
                }
                //This is supporting an older format of data. Today is Sept 2013, can be deleted after 1 year.
                else
                {
                    Boolean moreCompounds = true;
                    int i = 23;
                    while (moreCompounds)
                    {
                        if (headers[i] != "Adduct/Replacement")
                        {
                            molnames.Add(headers[i]);
                            i++;
                        }
                        else
                            moreCompounds = false;
                    }
                    bool firstRow = true;
                    while (read.Peek() >= 0)
                    {
                        //Read data
                        String Line = read.ReadLine();
                        String[] Lines = Line.Split(',');
                        //initialize new gR object
                        ResultsGroup gR = new ResultsGroup();
                        if (firstRow)
                        {
                            gR.PredictedComposition.ElementNames.AddRange(new List<string> { "C", "H", "N", "O", "S", "P" });
                            gR.PredictedComposition.MoleculeNames = molnames;
                            firstRow = false;
                        }
                        DeconRow dR = new DeconRow();
                        CompositionHypothesisEntry cH = new CompositionHypothesisEntry();
                        gR.DeconRow = dR;
                        gR.PredictedComposition = cH;
                        if (!String.IsNullOrEmpty(Lines[0]))
                        {
                            //Input data

                            gR.Score = Convert.ToDouble(Lines[0]);
                            gR.DeconRow.MonoisotopicMassWeight = Convert.ToDouble(Lines[1]);
                            gR.PredictedComposition.CompoundComposition = Lines[2].Replace(",", ";");
                            if (String.IsNullOrEmpty(Lines[2]) || Lines[2] == "0")
                                gR.Match = false;
                            else
                                gR.Match = true;
                            gR.PredictedComposition.MassWeight = Convert.ToDouble(Lines[4]);
                            gR.NumModiStates = Convert.ToDouble(Lines[5]);
                            gR.NumChargeStates = Convert.ToInt32(Lines[6]);
                            gR.NumOfScan = Convert.ToDouble(Lines[7]);
                            gR.ScanDensity = Convert.ToDouble(Lines[8]);
                            gR.ExpectedA = Convert.ToDouble(Lines[9]);
                            gR.AvgAA2List = new List<double>();
                            gR.AvgAA2List.Add(Convert.ToDouble(Lines[10]));
                            gR.TotalVolume = Convert.ToDouble(Lines[11]);
                            gR.AvgSigNoise = Convert.ToDouble(Lines[12]);
                            gR.CentroidScan = Convert.ToDouble(Lines[13]);
                            gR.DeconRow.ScanNum = Convert.ToInt32(Lines[14]);
                            gR.MaxScanNum = Convert.ToInt32(Lines[15]);
                            gR.MinScanNum = Convert.ToInt32(Lines[16]);
                            gR.PredictedComposition.eqCount = new Dictionary<string, int>();
                            for (int k = 17; k < 23; k++)
                            {
                                gR.PredictedComposition.ElementAmount.Add(Convert.ToInt32(Lines[k]));
                            }
                            gR.PredictedComposition.eqCount.Add("A", Convert.ToInt32(Lines[23]));
                            gR.PredictedComposition.eqCount.Add("B", Convert.ToInt32(Lines[24]));
                            gR.PredictedComposition.eqCount.Add("C", Convert.ToInt32(Lines[25]));
                            gR.PredictedComposition.eqCount.Add("D", Convert.ToInt32(Lines[26]));
                            gR.PredictedComposition.eqCount.Add("E", Convert.ToInt32(Lines[27]));
                            gR.PredictedComposition.eqCount.Add("F", Convert.ToInt32(Lines[28]));
                            gR.PredictedComposition.eqCount.Add("G", Convert.ToInt32(Lines[29]));
                            gR.PredictedComposition.eqCount.Add("H", Convert.ToInt32(Lines[30]));
                            gR.PredictedComposition.eqCount.Add("I", Convert.ToInt32(Lines[31]));
                            gR.PredictedComposition.eqCount.Add("J", Convert.ToInt32(Lines[32]));
                            gR.PredictedComposition.eqCount.Add("K", Convert.ToInt32(Lines[33]));
                            gR.PredictedComposition.eqCount.Add("L", Convert.ToInt32(Lines[34]));
                            gR.PredictedComposition.eqCount.Add("M", Convert.ToInt32(Lines[35]));
                            gR.PredictedComposition.eqCount.Add("N", Convert.ToInt32(Lines[36]));
                            gR.PredictedComposition.eqCount.Add("O", Convert.ToInt32(Lines[37]));
                            gR.PredictedComposition.eqCount.Add("P", Convert.ToInt32(Lines[38]));
                            gR.PredictedComposition.eqCount.Add("Q", Convert.ToInt32(Lines[39]));
                            gR.PredictedComposition.AddRep = Lines[40];
                            gR.PredictedComposition.AdductNum = Convert.ToInt32(Lines[41]);
                            gR.PredictedComposition.PepSequence = Lines[42];
                            gR.PredictedComposition.PepModification = Lines[43];
                            gR.PredictedComposition.MissedCleavages = Convert.ToInt32(Lines[44]);
                            gR.PredictedComposition.NumGlycosylations = Convert.ToInt32(Lines[45]);
                            Ans.Add(gR);
                        }
                    }
                }
            }
            //This is gly1 data.
            else
            {
                String header = read.ReadLine();
                String[] headers = header.Split('\t');

                while (read.Peek() >= 0)
                {
                    //Read data
                    String Line = read.ReadLine();
                    String[] Lines = Line.Split('\t');
                    //initialize new gR object
                    ResultsGroup gR = new ResultsGroup();
                    DeconRow dR = new DeconRow();
                    CompositionHypothesisEntry cH = new CompositionHypothesisEntry();
                    gR.DeconRow = dR;
                    gR.PredictedComposition = cH;
                    if (!String.IsNullOrEmpty(Lines[0]))
                    {
                        //Input data
                        gR.PredictedComposition.MoleculeNames = molnames;
                        gR.Score = Convert.ToDouble(Lines[0]);
                        gR.DeconRow.MonoisotopicMassWeight = Convert.ToDouble(Lines[1]);
                        gR.PredictedComposition.CompoundComposition = Lines[2].Replace(",", ";");
                        if (String.IsNullOrEmpty(Lines[2]) || Lines[2] == "0")
                        {
                            gR.Match = false;
                            gR.PredictedComposition.MassWeight = 0;
                        }
                        else
                        {
                            gR.Match = true;
                            gR.PredictedComposition.MassWeight = Convert.ToDouble(Lines[4]);
                        }
                        gR.NumModiStates = Convert.ToDouble(Lines[5]);
                        gR.NumChargeStates = Convert.ToInt32(Lines[6]);
                        gR.NumOfScan = Convert.ToDouble(Lines[7]);
                        gR.ScanDensity = Convert.ToDouble(Lines[8]);
                        gR.ExpectedA = Convert.ToDouble(Lines[9]);
                        gR.AvgAA2List = new List<double>();
                        gR.AvgAA2List.Add(Convert.ToDouble(Lines[10]));
                        gR.TotalVolume = Convert.ToDouble(Lines[11]);
                        gR.AvgSigNoise = Convert.ToDouble(Lines[12]);
                        gR.CentroidScan = Convert.ToDouble(Lines[13]);
                        gR.DeconRow.ScanNum = Convert.ToInt32(Convert.ToDouble(Lines[14]));
                        Ans.Add(gR);
                    }
                }

            }
            return Ans;
        }
        private ResultsGroup AverageResultsGroup(List<ResultsGroup> combiningRows)
        {
            List<Int32> NumChargeStates = new List<int>();
            List<Double> ScanDensity= new List<Double>() ;
            List<Double> NumModiStates= new List<Double>() ;
            List<Double> TotalVolume= new List<Double>() ;
            List<Double> ExpectedA= new List<Double>() ;
            List<Double> Score= new List<Double>() ;
            List<Double> CentroidScan= new List<Double>() ;
            List<Double> NumScan= new List<Double>() ;
            List<Double> AvgSigNoise= new List<Double>() ;
            //These are for calculating the features
            List<Int32> MaxScanNum= new List<int>() ;
            List<Int32> MinScanNum= new List<int>() ;
            List<Int32> ScanNums= new List<int>() ;
            List<Int32> ChargeStateList = new List<int>();
            List<Double> AvgSigNoiseList= new List<Double>() ;
            List<Double> CentroidScanLR= new List<Double>() ;
            List<Double> AvgAA2List= new List<Double>() ;
            List<Double> RelativeTotalVolume = new List<double>();

            List<Int32> scan_num = new List<Int32>();
            List<Int32> charge = new List<Int32>();
            List<Int32> abundance = new List<Int32>();
            List<Double> mz = new List<Double>();
            List<Double> fit = new List<Double>();
            List<Double> average_mw = new List<Double>();
            List<Double> monoisotopic_mw = new List<Double>();
            List<Double> mostabundant_mw = new List<Double>();
            List<Double> fwhm = new List<Double>();
            List<Double> signal_noise = new List<Double>();
            List<Int32> mono_abundance = new List<Int32>();
            List<Int32> mono_plus2_abundance = new List<Int32>();
            List<Int32> flag = new List<Int32>();
            List<Double> interference_sore = new List<Double>();

            ResultsGroup FinalAns = new ResultsGroup();
            FinalAns.ListOfOriginalTotalVolumes = combiningRows[0].ListOfOriginalTotalVolumes;

            for (int i = 0; i < combiningRows.Count(); i++)
            {
                NumChargeStates.Add(combiningRows[i].NumChargeStates);
                ScanDensity.Add(combiningRows[i].ScanDensity);
                NumModiStates.Add(combiningRows[i].NumModiStates);
                TotalVolume.Add(combiningRows[i].TotalVolume);
                ExpectedA.Add(combiningRows[i].ExpectedA);
                Score.Add(combiningRows[i].Score);
                CentroidScan.Add(combiningRows[i].CentroidScan);
                NumScan.Add(combiningRows[i].NumOfScan);
                AvgSigNoise.Add(combiningRows[i].AvgSigNoise);
                //These are for calculating the features
                MaxScanNum.Add(combiningRows[i].MaxScanNum);
                MinScanNum.Add(combiningRows[i].MinScanNum);
                CentroidScanLR.Add(combiningRows[i].CentroidScanLR);
                AvgAA2List.AddRange(combiningRows[i].AvgAA2List);
                RelativeTotalVolume.Add(combiningRows[i].RelativeTotalVolume);

                charge.Add(combiningRows[i].DeconRow.charge);
                scan_num.Add(combiningRows[i].DeconRow.ScanNum);
                abundance.Add(combiningRows[i].DeconRow.abundance);
                mz.Add(combiningRows[i].DeconRow.mz);
                fit.Add(combiningRows[i].DeconRow.fit);
                average_mw.Add(combiningRows[i].DeconRow.average_mw);
                monoisotopic_mw.Add(combiningRows[i].DeconRow.MonoisotopicMassWeight);
                mostabundant_mw.Add(combiningRows[i].DeconRow.mostabundant_mw);
                fwhm.Add(combiningRows[i].DeconRow.fwhm);
                signal_noise.Add(combiningRows[i].DeconRow.SignalNoiseRatio);
                mono_abundance.Add(combiningRows[i].DeconRow.MonoisotopicAbundance);
                mono_plus2_abundance.Add(combiningRows[i].DeconRow.MonoisotopicPlus2Abundance);
                flag.Add(combiningRows[i].DeconRow.flag);
                interference_sore.Add(combiningRows[i].DeconRow.interference_sore);
                for (int h = 0; h < combiningRows[i].ListOfOriginalTotalVolumes.Count(); h++)
                {
                    if (combiningRows[i].ListOfOriginalTotalVolumes[h] > FinalAns.ListOfOriginalTotalVolumes[h])
                    {
                        FinalAns.ListOfOriginalTotalVolumes[h] = combiningRows[i].ListOfOriginalTotalVolumes[h];
                    }
                }

            }

            FinalAns.DeconRow = new DeconRow();
            FinalAns.PredictedComposition = combiningRows[0].PredictedComposition;
            FinalAns.NumChargeStates = Convert.ToInt32(NumChargeStates.Average());
            FinalAns.ScanDensity = ScanDensity.Average();
            FinalAns.NumModiStates = NumModiStates.Average();
            FinalAns.TotalVolume = TotalVolume.Average();
            FinalAns.ExpectedA = ExpectedA.Average();
            FinalAns.Score = Score.Average();
            FinalAns.CentroidScan = CentroidScan.Average();
            FinalAns.NumOfScan = NumScan.Average();
            FinalAns.AvgSigNoise = AvgSigNoise.Average();
            FinalAns.MaxScanNum = Convert.ToInt32(MaxScanNum.Average());
            FinalAns.MinScanNum = Convert.ToInt32(MinScanNum.Average());
            FinalAns.CentroidScanLR = CentroidScanLR.Average();
            FinalAns.TotalVolumeSD = TotalVolume.StdDev();
            FinalAns.RelativeTotalVolumeSD = RelativeTotalVolume.StdDev();
            FinalAns.AvgAA2List = new List<double>();
            FinalAns.AvgAA2List.Add(AvgAA2List.Average());
            FinalAns.RelativeTotalVolume = RelativeTotalVolume.Average();

            FinalAns.DeconRow.ScanNum = Convert.ToInt32(scan_num.Average());
            FinalAns.DeconRow.abundance = Convert.ToInt32(abundance.Average());
            FinalAns.DeconRow.mz = mz.Average();
            FinalAns.DeconRow.fit = fit.Average();
            FinalAns.DeconRow.average_mw = average_mw.Average();
            FinalAns.DeconRow.MonoisotopicMassWeight = monoisotopic_mw.Average();
            FinalAns.DeconRow.mostabundant_mw = mostabundant_mw.Average();
            FinalAns.DeconRow.fwhm = fwhm.Average();
            FinalAns.DeconRow.SignalNoiseRatio = signal_noise.Average();
            FinalAns.DeconRow.MonoisotopicAbundance = Convert.ToInt32(mono_abundance.Average());
            FinalAns.DeconRow.MonoisotopicPlus2Abundance = Convert.ToInt32(mono_plus2_abundance.Average());
            FinalAns.DeconRow.flag = Convert.ToInt32(flag.Average());
            FinalAns.DeconRow.interference_sore = interference_sore.Average();

            return FinalAns;
        }
        //this Grouping function performs the grouping.
        private List<ResultsGroup> Groupings(String filename, ParametersForm.ParameterSettings paradata)
        {
            GetDeconData DeconDATA1 = new GetDeconData();
            List<DeconRow> sortedDeconData = new List<DeconRow>();
            sortedDeconData = DeconDATA1.getdata(filename);
            //First, sort the list descendingly by its abundance.
            sortedDeconData = sortedDeconData.OrderByDescending(a => a.abundance).ToList();
            //################Second, create a new list to store results from the first grouping.###############
            List<ResultsGroup> fgResults = new List<ResultsGroup>();
            ResultsGroup GR2 = new ResultsGroup();
            GR2.PredictedComposition = new CompositionHypothesisEntry();
            Int32 currentMaxBin = new Int32();
            currentMaxBin = 1;
            GR2.DeconRow = sortedDeconData[0];
            GR2.MostAbundant = true;
            GR2.NumOfScan = 1;
            GR2.MinScanNum = sortedDeconData[0].ScanNum;
            GR2.MaxScanNum = sortedDeconData[0].ScanNum;
            GR2.ChargeStateList = new List<int>();
            GR2.ChargeStateList.Add(sortedDeconData[0].charge);
            GR2.AvgSigNoiseList = new List<Double>();
            GR2.AvgSigNoiseList.Add(sortedDeconData[0].SignalNoiseRatio);
            GR2.AvgAA2List = new List<double>();
            GR2.AvgAA2List.Add(sortedDeconData[0].MonoisotopicAbundance / (sortedDeconData[0].MonoisotopicPlus2Abundance + 1));
            GR2.ScanNumList = new List<Int32>();
            GR2.ScanNumList.Add(sortedDeconData[0].ScanNum);
            GR2.NumModiStates = 1;
            GR2.TotalVolume = sortedDeconData[0].abundance * sortedDeconData[0].fwhm;
            GR2.ListAbundance = new List<double>();
            GR2.ListAbundance.Add(sortedDeconData[0].abundance);
            GR2.ListMonoMassWeight = new List<double>();
            GR2.ListMonoMassWeight.Add(sortedDeconData[0].MonoisotopicMassWeight);
            fgResults.Add(GR2);
            for (int j = 1; j < sortedDeconData.Count; j++)
            {
                for (int i = 0; i < fgResults.Count; i++)
                {
                    //Obtain grouping error. Note: its in ppm, so it needs to be multiplied by 0.000001.
                    Double GroupingError = fgResults[i].DeconRow.MonoisotopicMassWeight * paradata.GroupingErrorEG * 0.000001;
                    if ((sortedDeconData[j].MonoisotopicMassWeight < (fgResults[i].DeconRow.MonoisotopicMassWeight + GroupingError) && (sortedDeconData[j].MonoisotopicMassWeight > (fgResults[i].DeconRow.MonoisotopicMassWeight - GroupingError))))
                    {
                        if (fgResults[i].MaxScanNum < sortedDeconData[j].ScanNum)
                        {
                            fgResults[i].MaxScanNum = sortedDeconData[j].ScanNum;
                        }
                        else if (fgResults[i].MinScanNum > sortedDeconData[j].ScanNum)
                        {
                            fgResults[i].MinScanNum = sortedDeconData[j].ScanNum;
                        }
                        fgResults[i].NumOfScan = fgResults[i].NumOfScan + 1;
                        fgResults[i].ScanNumList.Add(sortedDeconData[j].ScanNum);
                        fgResults[i].TotalVolume = fgResults[i].TotalVolume + sortedDeconData[j].abundance * sortedDeconData[j].fwhm;
                        fgResults[i].ChargeStateList.Add(sortedDeconData[j].charge);
                        fgResults[i].AvgSigNoiseList.Add(sortedDeconData[j].SignalNoiseRatio);
                        fgResults[i].AvgAA2List.Add(sortedDeconData[j].MonoisotopicAbundance / (sortedDeconData[j].MonoisotopicPlus2Abundance + 1));
                        fgResults[i].ListAbundance.Add(sortedDeconData[j].abundance);
                        fgResults[i].ListMonoMassWeight.Add(sortedDeconData[j].MonoisotopicMassWeight);
                        break;
                    }

                    if (i == fgResults.Count - 1)
                    {
                        ResultsGroup GR = new ResultsGroup();
                        GR.PredictedComposition = new CompositionHypothesisEntry();
                        currentMaxBin = currentMaxBin + 1;
                        GR.DeconRow = sortedDeconData[j];
                        GR.MostAbundant = true;
                        GR.NumOfScan = 1;
                        GR.MinScanNum = sortedDeconData[j].ScanNum;
                        GR.MaxScanNum = sortedDeconData[j].ScanNum;
                        GR.ChargeStateList = new List<int>();
                        GR.ChargeStateList.Add(sortedDeconData[j].charge);
                        GR.AvgSigNoiseList = new List<Double>();
                        GR.AvgSigNoiseList.Add(sortedDeconData[j].SignalNoiseRatio);
                        GR.AvgAA2List = new List<double>();
                        GR.AvgAA2List.Add(sortedDeconData[j].MonoisotopicAbundance / (sortedDeconData[j].MonoisotopicPlus2Abundance + 1));
                        GR.ScanNumList = new List<int>();
                        GR.ScanNumList.Add(sortedDeconData[j].ScanNum);
                        GR.NumModiStates = 1;
                        GR.TotalVolume = sortedDeconData[j].abundance * sortedDeconData[j].fwhm;
                        GR.ListAbundance = new List<double>();
                        GR.ListAbundance.Add(sortedDeconData[j].abundance);
                        GR.ListMonoMassWeight = new List<double>();
                        GR.ListMonoMassWeight.Add(sortedDeconData[j].MonoisotopicMassWeight);
                        fgResults.Add(GR);
                    }
                }
            }
            //Lastly calculate the Average Weighted Abundance
            for (int y = 0; y < fgResults.Count(); y++)
            {
                Double sumofTopPart = 0;
                for (int z = 0; z < fgResults[y].ListMonoMassWeight.Count(); z++)
                {
                    sumofTopPart = sumofTopPart + fgResults[y].ListMonoMassWeight[z] * fgResults[y].ListAbundance[z];
                }
                fgResults[y].DeconRow.MonoisotopicMassWeight = sumofTopPart / fgResults[y].ListAbundance.Sum();
            }

            //######################## Here is the second grouping for NH3. ################################
                fgResults = fgResults.OrderBy(o => o.DeconRow.MonoisotopicMassWeight).ToList();
                for (int i = 0; i < fgResults.Count - 1; i++)
                {
                    if (fgResults[i].MostAbundant == true)
                    {
                        int numModStates = 1;
                        for (int j = i + 1; j < fgResults.Count; j++)
                        {
                            Double AdductTolerance = fgResults[i].DeconRow.MonoisotopicMassWeight * paradata.AdductToleranceEA * 0.000001;
                            if ((fgResults[i].DeconRow.MonoisotopicMassWeight >= (fgResults[j].DeconRow.MonoisotopicMassWeight - 17.02654911 * numModStates - AdductTolerance)) && (fgResults[i].DeconRow.MonoisotopicMassWeight <= (fgResults[j].DeconRow.MonoisotopicMassWeight - 17.02654911 * numModStates + AdductTolerance)))
                            {
                                //obtain max and min scan number
                                if (fgResults[i].MaxScanNum < fgResults[j].MaxScanNum)
                                {
                                    fgResults[i].MaxScanNum = fgResults[j].MaxScanNum;
                                }
                                else
                                {
                                    fgResults[i].MaxScanNum = fgResults[i].MaxScanNum;
                                }

                                if (fgResults[i].MinScanNum > fgResults[j].MinScanNum)
                                {
                                    fgResults[i].MinScanNum = fgResults[j].MinScanNum;
                                }
                                else
                                {
                                    fgResults[i].MinScanNum = fgResults[i].MinScanNum;
                                }
                                //numOfScan
                                fgResults[i].NumOfScan = fgResults[i].NumOfScan + fgResults[j].NumOfScan;
                                fgResults[i].ScanNumList.AddRange(fgResults[j].ScanNumList);
                                //ChargeStateList
                                for (int h = 0; h < fgResults[j].ChargeStateList.Count; h++)
                                {
                                    fgResults[i].ChargeStateList.Add(fgResults[j].ChargeStateList[h]);
                                }
                                //avgSigNoiseList
                                for (int h = 0; h < fgResults[j].AvgSigNoiseList.Count; h++)
                                {
                                    fgResults[i].AvgSigNoiseList.Add(fgResults[j].AvgSigNoiseList[h]);
                                }
                                //avgAA2List
                                for (int h = 0; h < fgResults[j].AvgAA2List.Count; h++)
                                {
                                    fgResults[i].AvgAA2List.Add(fgResults[j].AvgAA2List[h]);
                                }
                                //numModiStates
                                numModStates++;
                                fgResults[i].NumModiStates = fgResults[i].NumModiStates + 1;
                                fgResults[j].MostAbundant = false;
                                //TotalVolume
                                fgResults[i].TotalVolume = fgResults[i].TotalVolume + fgResults[j].TotalVolume;
                                if (fgResults[i].DeconRow.abundance < fgResults[j].DeconRow.abundance)
                                {
                                    fgResults[i].DeconRow = fgResults[j].DeconRow;
                                    numModStates = 1;
                                }
                            }
                            else if (fgResults[i].DeconRow.MonoisotopicMassWeight < (fgResults[j].DeconRow.MonoisotopicMassWeight - (17.02654911 + AdductTolerance * 2) * numModStates))
                            {
                                //save running time. Since the list is sorted, any other mass below won't match as an adduct.
                                break;
                            }
                        }
                    }
                }

            //Implement the scan number threshold
            fgResults = fgResults.OrderByDescending(a => a.NumOfScan).ToList();
            Int32 scanCutOff = fgResults.Count() + 1;
            for (int t = 0; t < fgResults.Count(); t++)
            {
                if (fgResults[t].NumOfScan < paradata.MinScanNumber)
                {
                    scanCutOff = t;
                    break;
                }
            }
            if (scanCutOff != fgResults.Count() + 1)
            {
                fgResults.RemoveRange(scanCutOff, fgResults.Count() - scanCutOff);
            }

            for (int i = 0; i < fgResults.Count(); i++)
            {
                fgResults[i].Match = false;
            }

            //##############Last part, this is to calculate the feature data needed for logistic regression###################
            //Expected A and Centroid Scan Error need linear regression. The models are built here separately.
            //In the this model. output is the Y axis and input is X.
            SimpleLinearRegression AA2regression = new SimpleLinearRegression();
            List<double> aainput = new List<double>();
            List<double> aaoutput = new List<double>();
            //Centroid Scan Error
            List<double> ccinput = new List<double>();
            List<double> ccoutput = new List<double>();
            for (int i = 0; i < fgResults.Count; i++)
            {
                if (fgResults[i].AvgAA2List.Average() != 0)
                {
                    aainput.Add(fgResults[i].DeconRow.MonoisotopicMassWeight);
                    aaoutput.Add(fgResults[i].AvgAA2List.Average());
                }
                if (fgResults[i].DeconRow.abundance > 250)
                {
                    ccoutput.Add(fgResults[i].ScanNumList.Average());
                    ccinput.Add(fgResults[i].DeconRow.MonoisotopicMassWeight);
                }

            }
            SimpleLinearRegression CSEregression = new SimpleLinearRegression();
            CSEregression.Regress(ccinput.ToArray(), ccoutput.ToArray());
            AA2regression.Regress(aainput.ToArray(), aaoutput.ToArray());

            //The remaining features and input them into the grouping results
            for (int i = 0; i < fgResults.Count; i++)
            {
                //ScanDensiy is: Number of scan divided by (max scan number – min scan number)
                Double ScanDensity = new Double();
                Int32 MaxScanNumber = fgResults[i].MaxScanNum;
                Int32 MinScanNumber = fgResults[i].MinScanNum;
                Double NumOfScan = fgResults[i].NumOfScan;
                List<Int32> numChargeStatesList = fgResults[i].ChargeStateList.Distinct().ToList();
                Int32 numChargeStates = numChargeStatesList.Count;
                Double numModiStates = fgResults[i].NumModiStates;
                if ((MaxScanNumber - MinScanNumber) != 0)
                    ScanDensity = NumOfScan / (MaxScanNumber - MinScanNumber + 15);
                else
                    ScanDensity = 0;
                //Use this scandensity for all molecules in this grouping.

                fgResults[i].NumChargeStates = numChargeStates;
                fgResults[i].ScanDensity = ScanDensity;
                fgResults[i].NumModiStates = numModiStates;
                fgResults[i].CentroidScanLR = CSEregression.Compute(fgResults[i].DeconRow.MonoisotopicMassWeight);
                fgResults[i].CentroidScan = Math.Abs(fgResults[i].ScanNumList.Average() - fgResults[i].CentroidScanLR);
                fgResults[i].ExpectedA = Math.Abs(fgResults[i].AvgAA2List.Average() - AA2regression.Compute(fgResults[i].DeconRow.MonoisotopicMassWeight));
                fgResults[i].AvgSigNoise = fgResults[i].AvgSigNoiseList.Average();
            }
            return fgResults;
        }