//public static List<BYCZIons> CalculateBYCZIon(string Sequence, MassSpectrometry.SuperSpectrum spectrum, List<ModificationList> AllValidationModifications = null, double MatchTolerance = 0.00, SortedList<char, double> aa = null, bool isauto = false, Dictionary<double, double> CurrentMonomasses = null)
        //{


        //    return CalculateBYCZIon(Sequence, spectrum)

        //}

        /// <summary>
        /// Calcaulate all the b and y ions using a given sequence.
        /// And compare it to the Monomasses already present.
        /// </summary>
        /// <param name="Sequence"></param>
        /// <param name="Monomasses"></param>
        /// <returns></returns>
        public static List <BYCZIons> CalculateBYCZIon(string Sequence, List <double> Monomasses, double ParentMass, string ActivationType, List <ModificationList> AllValidationModifications = null, double MatchTolerance = 0.00, SortedList <char, double> aa = null, bool isauto = false, Dictionary <double, double> CurrentMonomasses = null)
        {
            List <BYCZIons> AllIons = new List <BYCZIons>();

            if (string.IsNullOrWhiteSpace(Sequence))
            {
                return(AllIons);                                      // some simple validation.  If there is no valid sequence, there are no ions, so return the empty list.
            }
            AminoAcids = aa ?? AminoAcids;

            Monomasses = Monomasses.OrderBy(a => a).ToList();

            if (AllValidationModifications == null)
            {
                AllValidationModifications = App.AllValidationModifications;
            }
            if (MatchTolerance == 0.00)
            {
                MatchTolerance = Properties.Settings.Default.MatchTolerancePPM;
            }

            double ion = new double();

            double cion = new double();

            cion = Molecules.Ammonia;

            //if (!isauto)
            {
                if (ParentMass == 0.0)
                {
                    ParentMass = LocalParentMass;
                }

                Monomasses.Add(ParentMass);

                Monomasses = Monomasses.Where(a => a <= ParentMass).OrderBy(a => a).ToList();
            }


            string[] splitstring = { "[", "]" };

            List <string> sequences = Sequence.Replace("(", "").Replace(")", "").Trim().Split(splitstring, StringSplitOptions.RemoveEmptyEntries).ToList();



            int count = 1;

            Regex regex = new Regex(@"^[0-9\.\+\-]+$");

            Regex regexlwrcs = new Regex(@"[a-z]+$");

            //bool number = false;

            string tempseq = string.Empty;

            if (regex.IsMatch(sequences[0]))
            {
                ion  = Convert.ToDouble(sequences[0]);
                cion = cion + Convert.ToDouble(sequences[0]);
            }

            for (int i = 0; i < sequences.Count; i++)
            {
                string seq = sequences[i];

                if (i < sequences.Count)
                {
                    if ((regex.IsMatch(seq)) || AllValidationModifications.Any(a => (a.Abbreviation == sequences[i]) || ("+" + a.Abbreviation == sequences[i]) || ("-" + a.Abbreviation == sequences[i])))
                    {
                        continue;
                    }
                }

                {
                    {
                        bool breaksequence = false;
                        for (int j = 0; j < seq.Length; j++)     /// This is the individual AminoAcids
                        {
                            if (j == seq.Length - 1)
                            {
                                for (int ii = i + 1; ii < sequences.Count; ii++)
                                {
                                    if ((regex.IsMatch(sequences[ii])) || AllValidationModifications.Any(a => (a.Abbreviation == sequences[ii]) || ("+" + a.Abbreviation == sequences[ii]) || ("-" + a.Abbreviation == sequences[ii])))
                                    {
                                        breaksequence = true;
                                        break;
                                    }
                                    else
                                    {
                                        breaksequence = false;
                                        break;
                                    }
                                }
                            }

                            if (breaksequence)
                            {
                                for (int ii = i + 1; ii < sequences.Count; ii++)
                                {
                                    {
                                        if (AllValidationModifications.Any(a => a.Abbreviation == sequences[ii]))
                                        {
                                            tempseq = "+" + sequences[ii] + " " + tempseq;
                                            ion     = ion + Convert.ToDouble(AllValidationModifications.First(a => a.Abbreviation == sequences[ii]).Mass);
                                            cion    = cion + Convert.ToDouble(AllValidationModifications.First(a => a.Abbreviation == sequences[ii]).Mass);
                                        }
                                        else if (AllValidationModifications.Any(a => "+" + a.Abbreviation == sequences[ii]))
                                        {
                                            tempseq = sequences[ii] + " " + tempseq;
                                            ion     = ion + Convert.ToDouble(AllValidationModifications.First(a => "+" + a.Abbreviation == sequences[ii]).Mass);
                                            cion    = cion + Convert.ToDouble(AllValidationModifications.First(a => "+" + a.Abbreviation == sequences[ii]).Mass);
                                        }
                                        else if (AllValidationModifications.Any(a => "-" + a.Abbreviation == sequences[ii]))
                                        {
                                            tempseq = sequences[ii] + " " + tempseq;
                                            ion     = ion - Math.Abs(Convert.ToDouble(AllValidationModifications.First(a => "-" + a.Abbreviation == sequences[ii]).Mass));
                                            cion    = cion - Math.Abs(Convert.ToDouble(AllValidationModifications.First(a => "-" + a.Abbreviation == sequences[ii]).Mass));
                                        }
                                        else if (regex.IsMatch(sequences[ii]))
                                        {
                                            if (sequences[ii].StartsWith("+"))
                                            {
                                                ion      = ion + Convert.ToDouble(sequences[ii]);
                                                cion     = cion + Convert.ToDouble(sequences[ii]);
                                                tempseq += "+ " + sequences[ii].Remove(sequences[ii].IndexOf("+"), 1) + " ";
                                            }
                                            else if (sequences[ii].StartsWith("-"))
                                            {
                                                ion      = ion - Math.Abs(Convert.ToDouble(sequences[ii]));
                                                cion     = cion - Math.Abs(Convert.ToDouble(sequences[ii]));
                                                tempseq += "- " + sequences[ii].Remove(sequences[ii].IndexOf("-"), 1) + " ";
                                            }
                                            else
                                            {
                                                ion      = ion + Convert.ToDouble(sequences[ii]);
                                                cion     = cion + Convert.ToDouble(sequences[ii]);
                                                tempseq += "+ " + sequences[ii] + " ";
                                            }
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }


                            if (j != seq.Length - 1)
                            {
                                if (regexlwrcs.IsMatch(Convert.ToString(seq[j + 1])))
                                {
                                    ion     = ion + sequencelengthwithmods((Convert.ToString(seq[j]) + Convert.ToString(seq[j + 1])));
                                    cion    = cion + sequencelengthwithmods((Convert.ToString(seq[j]) + Convert.ToString(seq[j + 1])));
                                    tempseq = Convert.ToString(seq[j]) + Convert.ToString(seq[j + 1]) + " " + tempseq;
                                    j       = j + 1;
                                }
                                else
                                {
                                    ion     = ion + sequencelength(Convert.ToString(seq[j]));
                                    cion    = cion + sequencelength(Convert.ToString(seq[j]));
                                    tempseq = seq[j] + " " + tempseq;
                                }
                            }
                            else
                            {
                                ion     = ion + sequencelength(Convert.ToString(seq[j]));
                                cion    = cion + sequencelength(Convert.ToString(seq[j]));
                                tempseq = seq[j] + " " + tempseq;
                            }
                            //if (isauto)
                            //{

                            //    double bionerror = Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100;

                            //    AllIons.Add(new BYIons()
                            //    {
                            //        Bion = ion,
                            //        //Yion = 0,
                            //        //AminoAcid = tempseq.TrimEnd(),
                            //        //BionNumber = count,
                            //        //BorYIon = count,
                            //        bioncolor = bionerror == 100 ? false : (((PPM.CalculatePPM(ion, bionerror) <= Properties.Settings.Default.FragementIonTolerance) && (PPM.CalculatePPM(ion, bionerror) >= -(Properties.Settings.Default.FragementIonTolerance)))), /// Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)),
                            //        Intensity = CurrentMonomasses.Any(a => Math.Abs(a.Key - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? CurrentMonomasses.First(a => Math.Abs(a.Key - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Value : 0,
                            //        bnh3ioncolor = Monomasses.Any(a => Math.Abs(a - (ion - Molecules.Ammonia)) <= PPM.CurrentPPMbasedonMatchList((ion - Molecules.Ammonia), MatchTolerance)),
                            //        bh2oioncolor = Monomasses.Any(a => Math.Abs(a - (ion - Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion - Molecules.Water), MatchTolerance)),
                            //        bionerror = bionerror,/// Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100,
                            //        //bh2oion = Math.Round(ion - Molecules.Water, 5),
                            //        //bh2oioncolor = Monomasses.Any(a => Math.Abs(a - (ion - Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion - Molecules.Water), MatchTolerance)),
                            //        //bionerror = Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100,
                            //        //BionDescription = "B" + Convert.ToString(count) + " " + PPM.CalculatePPM(ion, Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100),
                            //        //BionDaltonDescription = "B" + Convert.ToString(count) + " " + (Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100)
                            //    });
                            //}
                            //else
                            {
                                double bionerror = Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100;
                                double cionerror = Monomasses.Any(a => Math.Abs(a - cion) <= PPM.CurrentPPMbasedonMatchList(cion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - cion) <= PPM.CurrentPPMbasedonMatchList(cion, MatchTolerance)).Select(a => a - cion).First() : 100;

                                AllIons.Add(new BYCZIons()
                                {
                                    Bion           = ion,
                                    Yion           = 0,
                                    Cion           = cion,
                                    Zion           = 0,
                                    AminoAcid      = tempseq.TrimEnd(),
                                    BionNumber     = count,
                                    ActivationType = ActivationType,
                                    BorYIon        = count,
                                    CionNumber     = count,
                                    CorZIon        = count,
                                    Intensity      = CurrentMonomasses != null ? (CurrentMonomasses.Any(a => Math.Abs(a.Key - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? CurrentMonomasses.First(a => Math.Abs(a.Key - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Value : 0) : 0,
                                    bioncolor      = bionerror == 100 ? false : ((Math.Abs(PPM.CalculatePPM(ParentMass, bionerror)) < Properties.Settings.Default.FragementIonTolerance)), ///Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)),
                                    //bioncolor = bionerror == 100 ? false : (((PPM.CalculatePPM(ion, bionerror) < Properties.Settings.Default.FragementIonTolerance) && (PPM.CalculatePPM(ion, bionerror) > -(Properties.Settings.Default.FragementIonTolerance)))), ///Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)),
                                    bh2oion               = Math.Round(ion - Molecules.Water, 5),
                                    bh2oioncolor          = Monomasses.Any(a => Math.Abs(a - (ion - Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion - Molecules.Water), MatchTolerance)),
                                    bionerror             = bionerror,/// Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100,
                                    BionDescription       = "B" + Convert.ToString(count) + " " + PPM.CalculatePPM(ion, Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100),
                                    BionDaltonDescription = "B" + Convert.ToString(count) + " " + (Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100),
                                    bnh3ion               = Math.Round(ion - Molecules.Ammonia, 5),
                                    bnh3ioncolor          = Monomasses.Any(a => Math.Abs(a - (ion - Molecules.Ammonia)) <= PPM.CurrentPPMbasedonMatchList((ion - Molecules.Ammonia), MatchTolerance)),
                                    cioncolor             = cionerror == 100 ? false : ((Math.Abs(PPM.CalculatePPM(ParentMass, cionerror)) < Properties.Settings.Default.FragementIonTolerance)), //Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(cion, MatchTolerance)),
                                    //bioncolor = bionerror == 100 ? false : (((PPM.CalculatePPM(ion, bionerror) < Properties.Settings.Default.FragementIonTolerance) && (PPM.CalculatePPM(ion, bionerror) > -(Properties.Settings.Default.FragementIonTolerance)))), ///Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)),
                                    ch2oion               = Math.Round(cion - Molecules.Water, 5),
                                    ch2oioncolor          = Monomasses.Any(a => Math.Abs(a - (cion - Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((cion - Molecules.Water), MatchTolerance)),
                                    cionerror             = cionerror, // Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(cion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100,
                                    CionDescription       = "C" + Convert.ToString(count) + " " + PPM.CalculatePPM(cion, Monomasses.Any(a => Math.Abs(a - cion) <= PPM.CurrentPPMbasedonMatchList(cion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - cion) <= PPM.CurrentPPMbasedonMatchList(cion, MatchTolerance)).Select(a => a - cion).First() : 100),
                                    CionDaltonDescription = "C" + Convert.ToString(count) + " " + (Monomasses.Any(a => Math.Abs(a - cion) <= PPM.CurrentPPMbasedonMatchList(cion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - cion) <= PPM.CurrentPPMbasedonMatchList(cion, MatchTolerance)).Select(a => a - cion).First() : 100),
                                    cnh3ion               = Math.Round(cion - Molecules.Ammonia, 5),
                                    cnh3ioncolor          = Monomasses.Any(a => Math.Abs(a - (cion - Molecules.Ammonia)) <= PPM.CurrentPPMbasedonMatchList((cion - Molecules.Ammonia), MatchTolerance)),
                                });
                            }
                            //{
                            //    double cionerror = Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100;

                            //    AllIons.Add(new BYIons()
                            //    {
                            //        Cion = ion,
                            //        Zion = 0,
                            //        AminoAcid = tempseq.TrimEnd(),
                            //        CionNumber = count,
                            //        CorZIon = count,
                            //        Intensity = CurrentMonomasses != null ? (CurrentMonomasses.Any(a => Math.Abs(a.Key - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? CurrentMonomasses.First(a => Math.Abs(a.Key - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Value : 0) : 0,
                            //        cioncolor = cionerror == 100 ? false : ((Math.Abs(PPM.CalculatePPM(ion, cionerror)) < Properties.Settings.Default.FragementIonTolerance)), ///Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)),
                            //        //bioncolor = bionerror == 100 ? false : (((PPM.CalculatePPM(ion, bionerror) < Properties.Settings.Default.FragementIonTolerance) && (PPM.CalculatePPM(ion, bionerror) > -(Properties.Settings.Default.FragementIonTolerance)))), ///Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)),
                            //        ch2oion = Math.Round(ion - Molecules.Water, 5),
                            //        ch2oioncolor = Monomasses.Any(a => Math.Abs(a - (ion - Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion - Molecules.Water), MatchTolerance)),
                            //        cionerror = cionerror,/// Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100,
                            //        CionDescription = "B" + Convert.ToString(count) + " " + PPM.CalculatePPM(ion, Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100),
                            //        CionDaltonDescription = "B" + Convert.ToString(count) + " " + (Monomasses.Any(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - ion) <= PPM.CurrentPPMbasedonMatchList(ion, MatchTolerance)).Select(a => a - ion).First() : 100),
                            //        cnh3ion = Math.Round(ion - Molecules.Ammonia, 5),
                            //        cnh3ioncolor = Monomasses.Any(a => Math.Abs(a - (ion - Molecules.Ammonia)) <= PPM.CurrentPPMbasedonMatchList((ion - Molecules.Ammonia), MatchTolerance)),
                            //    });
                            //}
                            count = count + 1;

                            tempseq = string.Empty;
                        }
                    }
                }
            }

            count = AllIons.Count - 1;
            ion   = 0;
            cion  = 0;
            //if (regex.IsMatch(sequences[0]))
            //{
            //    ion = Convert.ToDouble(sequences[0]);
            //}

            int c = 1;

            List <BYCZIons> intensities = new List <BYCZIons>();

            intensities = AllIons.Where(a => a.bioncolor == true).ToList();

            tempseq = string.Empty;
            for (int i = sequences.Count - 1; i >= 0; i--)
            {
                string seq = sequences[i];

                string sq = seq;

                if (regex.IsMatch(sq))
                {
                    if (sq.Contains("+"))
                    {
                        sq   = sq.Remove(sq.IndexOf("+"), 1);
                        ion  = ion + Convert.ToDouble(sq);
                        cion = cion + Convert.ToDouble(sq);
                    }
                    else if (sq.Contains("-"))
                    {
                        sq   = sq.Remove(sq.IndexOf("-"), 1);
                        ion  = ion - Math.Abs(Convert.ToDouble(sq));
                        cion = cion - Math.Abs(Convert.ToDouble(sq));
                    }
                    else
                    {
                        ion  = ion + Convert.ToDouble(sq);
                        cion = cion + Convert.ToDouble(sq);
                    }
                }
                else
                {
                    if (seq.Contains("-"))
                    {
                        if (AllValidationModifications.Any(a => ("-" + a.Abbreviation) == seq))
                        {
                            ion  = ion - Math.Abs(Convert.ToDouble(AllValidationModifications.Where(a => "-" + a.Abbreviation == seq).First().Mass));
                            cion = cion - Math.Abs(Convert.ToDouble(AllValidationModifications.Where(a => "-" + a.Abbreviation == seq).First().Mass));
                        }
                    }
                    else if (seq.Contains("+"))
                    {
                        if (AllValidationModifications.Any(a => ("+" + a.Abbreviation) == seq))
                        {
                            ion  = ion + Convert.ToDouble(AllValidationModifications.Where(a => "+" + a.Abbreviation == seq).First().Mass);
                            cion = cion + Convert.ToDouble(AllValidationModifications.Where(a => "+" + a.Abbreviation == seq).First().Mass);
                        }
                    }
                    else if (AllValidationModifications.Any(a => a.Abbreviation == seq))
                    {
                        ion  = ion + Convert.ToDouble(AllValidationModifications.Where(a => a.Abbreviation == seq).First().Mass);
                        cion = cion + Convert.ToDouble(AllValidationModifications.Where(a => a.Abbreviation == seq).First().Mass);
                    }
                    else
                    {
                        string[] alltheaminoacids = seq.Select(a => a.ToString()).ToArray();
                        double   modmass          = 0.0;
                        string   tempaminoacids   = string.Empty;
                        bool     hasaminoacidsmod = false;
                        foreach (string AminoAcid in alltheaminoacids.Reverse())
                        {
                            if (regexlwrcs.IsMatch(AminoAcid))
                            {
                                tempaminoacids   = AminoAcid;
                                hasaminoacidsmod = true;
                                continue;
                            }
                            tempaminoacids = AminoAcid + tempaminoacids;

                            ion  = ion + (hasaminoacidsmod ? sequencelengthwithmods(tempaminoacids) : sequencelength(AminoAcid));
                            cion = cion + (hasaminoacidsmod ? sequencelengthwithmods(tempaminoacids) : sequencelength(AminoAcid));

                            double Yion = Math.Round(ion + Molecules.Water, 4);

                            double Zion = Math.Round(cion, 4);

                            //AllIons[count].yioncolor = Monomasses.Any(a => Math.Abs(a - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance));
                            AllIons[count].Yion = Yion;
                            AllIons[count].Zion = Zion;
                            //AllIons[count].in
                            double yionerror = Monomasses.Any(a => Math.Abs(a - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)).Select(a => a - (ion + Molecules.Water)).First() : 100;
                            double zionerror = Monomasses.Any(a => Math.Abs(a - (cion)) <= PPM.CurrentPPMbasedonMatchList((cion), MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - (cion)) <= PPM.CurrentPPMbasedonMatchList((cion), MatchTolerance)).Select(a => a - (cion)).First() : 100;

                            //if (isauto)
                            //{
                            //    //if (AllIons[count].yioncolor)
                            //    {
                            //        AllIons[count].Intensity += CurrentMonomasses.Any(a => Math.Abs(a.Key - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)) ?
                            //                                    CurrentMonomasses.First(a => Math.Abs(a.Key - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)).Value : 0;
                            //        AllIons[count].yh2oioncolor = Monomasses.Any(a => Math.Abs(a - (ion)) <= PPM.CurrentPPMbasedonMatchList((ion), MatchTolerance));
                            //        AllIons[count].ynh3ioncolor = Monomasses.Any(a => Math.Abs(a - (ion + Molecules.Water - Molecules.Ammonia)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water - Molecules.Ammonia), MatchTolerance));
                            //        AllIons[count].yionerror = yionerror;/// Monomasses.Any(a => Math.Abs(a - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)).Select(a => a - (ion + Molecules.Water)).First() : 100;
                            //        AllIons[count].yioncolor = yionerror == 100 ? false : (((PPM.CalculatePPM(Yion, yionerror) <= Properties.Settings.Default.FragementIonTolerance) && (PPM.CalculatePPM(Yion, yionerror) >= -(Properties.Settings.Default.FragementIonTolerance)))); //Checking if the PPM ion tolerance is between the limits
                            //        //CurrentMonomasses.ContainsKey(AllIons[count].Yion) ? CurrentMonomasses[AllIons[count].Yion] : 0;
                            //        //AllIons[count].Intensity += CurrentMonomasses.ContainsKey(AllIons[count].Yion) ? CurrentMonomasses[AllIons[count].Yion] : 0;
                            //    }
                            //}
                            //if (!isauto)
                            {
                                AllIons[count].Yion       = Yion;/// Math.Round(ion + Molecules.Water, 4);
                                AllIons[count].Zion       = Zion;
                                AllIons[count].YionNumber = -c;
                                AllIons[count].ZionNumber = -c;
                                AllIons[count].Intensity += CurrentMonomasses != null ? (CurrentMonomasses.Any(a => Math.Abs(a.Key - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)) ?
                                                                                         CurrentMonomasses.First(a => Math.Abs(a.Key - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)).Value : 0) : 0;
                                AllIons[count].ActivationType = ActivationType;
                                AllIons[count].NewYIonNumber  = c;
                                AllIons[count].NewZIonNumber  = c;
                                AllIons[count].yh2oion        = Math.Round(ion, 4);
                                AllIons[count].zh2oion        = Math.Round(Zion + Molecules.Water, 4);
                                AllIons[count].ynh3ion        = Math.Round(ion + Molecules.Water - Molecules.Ammonia, 4);
                                AllIons[count].znh3ion        = Math.Round(Zion - Molecules.Ammonia, 4);
                                AllIons[count].yh2oioncolor   = Monomasses.Any(a => Math.Abs(a - (ion)) <= PPM.CurrentPPMbasedonMatchList((ion), MatchTolerance));
                                //AllIons[count].zh2oioncolor = Monomasses.Any(a => Math.Abs(a - (Zion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion), MatchTolerance));
                                AllIons[count].ynh3ioncolor = Monomasses.Any(a => Math.Abs(a - (ion + Molecules.Water - Molecules.Ammonia)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water - Molecules.Ammonia), MatchTolerance));
                                AllIons[count].znh3ioncolor = Monomasses.Any(a => Math.Abs(a - (Zion - Molecules.Ammonia)) <= PPM.CurrentPPMbasedonMatchList((Zion - Molecules.Ammonia), MatchTolerance));
                                AllIons[count].yionerror    = yionerror; /// Monomasses.Any(a => Math.Abs(a - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)) ? Monomasses.Where(a => Math.Abs(a - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)).Select(a => a - (ion + Molecules.Water)).First() : 100;

                                AllIons[count].zionerror = zionerror;

                                AllIons[count].yioncolor = yionerror == 100 ? false : ((Math.Abs(PPM.CalculatePPM(ParentMass, yionerror)) <= Properties.Settings.Default.FragementIonTolerance));

                                AllIons[count].zioncolor = zionerror == 100 ? false : ((Math.Abs(PPM.CalculatePPM(ParentMass, zionerror)) <= Properties.Settings.Default.FragementIonTolerance));

                                //AllIons[count].yioncolor = yionerror == 100 ? false : (((PPM.CalculatePPM(Yion, yionerror) <= Properties.Settings.Default.FragementIonTolerance) && (PPM.CalculatePPM(Yion, yionerror) >= -(Properties.Settings.Default.FragementIonTolerance))));

                                if (Monomasses.Any(a => Math.Abs(a - (ion + Molecules.Water)) <= PPM.CurrentPPMbasedonMatchList((ion + Molecules.Water), MatchTolerance)))
                                {
                                    AllIons[count].YionDescription       = "Y" + Convert.ToString(c) + " " + Convert.ToString(AllIons[count].yionPPM);
                                    AllIons[count].YionDaltonDescription = "Y" + Convert.ToString(c) + " " + Convert.ToString(AllIons[count].yionerror);
                                }
                                if (Monomasses.Any(a => Math.Abs(a - (Zion)) <= PPM.CurrentPPMbasedonMatchList((Zion), MatchTolerance)))
                                {
                                    AllIons[count].ZionDescription       = "Y" + Convert.ToString(c) + " " + Convert.ToString(AllIons[count].zionPPM);
                                    AllIons[count].ZionDaltonDescription = "Y" + Convert.ToString(c) + " " + Convert.ToString(AllIons[count].zionerror);
                                }
                            }
                            c                = c + 1;
                            count            = count - 1;
                            tempaminoacids   = string.Empty;
                            hasaminoacidsmod = false;
                        }
                    }
                }
            }



            return(AllIons);
        }