//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;
        }
        public List<DeconRow> getdata(String DeconData)
        {
            ParametersForm parad = new ParametersForm();
            ParametersForm.ParameterSettings paradata = parad.GetParameters();

            //try
               // {
                FileStream fileinput = new FileStream(DeconData, FileMode.Open, FileAccess.Read);
                StreamReader readdata = new StreamReader(fileinput);

                //The first line in the file contains the column names, we don't need it.
                readdata.ReadLine();
                while (readdata.Peek() >= 0)
                {
                    DeconRow Row = new DeconRow();
                    String Line = readdata.ReadLine();
                    String[] column = Line.Split(',');
                    Row.ScanNum = Convert.ToInt32(column[0]);
                    Row.charge = Convert.ToInt32(column[1]);
                    Row.abundance = Convert.ToInt32(column[2]);
                    Row.mz = Convert.ToDouble(column[3]);
                    Row.fit = Convert.ToDouble(column[4]);
                    Row.average_mw = Convert.ToDouble(column[5]);
                    Row.MonoisotopicMassWeight = Convert.ToDouble(column[6]);
                    Row.mostabundant_mw = Convert.ToDouble(column[7]);
                    Row.fwhm = Convert.ToDouble(column[8]);
                    Row.SignalNoiseRatio = Convert.ToDouble(column[9]);
                    Row.MonoisotopicAbundance = Convert.ToInt32(column[10]);
                    Double mp2a = Convert.ToDouble(column[11]);
                    Row.MonoisotopicPlus2Abundance = Convert.ToInt32(mp2a);
                    //Flag maybe empty, so, special treatment.
                    if (column[12] == "")
                    { Row.flag = 0; }
                    else
                    { Row.flag = Convert.ToInt32(column[12]); }
                    if (Convert.ToInt32(column.Count()) == 14)
                        Row.interference_sore = Convert.ToDouble(column[13]);
                    else
                        Row.interference_sore = 0;
                    //Check if the data are within the boundaries of the Parameters
                    if (Row.abundance >= paradata.DataNoiseTheshold)
                    {
                        if (Row.MonoisotopicMassWeight <= paradata.MolecularWeightUpperBound)
                        {
                            if (Row.MonoisotopicMassWeight >= paradata.MolecularWeightLowerBound)
                            {
                                DeconDATA.Add(Row);
                            }
                        }
                    }
                }
                fileinput.Close();
               // }
              //  catch(Exception ex)
              //  {
              //      MessageBox.Show("Error: Could not read DeconTools Data file from disk. Original error: " + ex.Message);
             //   }
            return DeconDATA;
        }