public QMatch(QMatch Match)
 {
     ApexRT       = Match.ApexRT;
     ApexScore    = Match.ApexScore;
     ApexMZ       = Match.ApexMZ;
     ApexIndex    = Match.ApexIndex;
     Score        = Match.Score;
     RTDisp       = Match.RTDisp;
     IsotopeRatio = Match.IsotopeRatio;
     MaxConv      = Match.MaxConv;
     Shift        = Match.Shift;
     if (Match.MSMatches != null)
     {
         MSMatches = new List <MSMatch>();
         MSMatch MS;
         for (int i = 0; i < Match.MSMatches.Count; i++)
         {
             MS = new MSMatch(Match.MSMatches[i]);
             MSMatches.Add(MS);
         }
     }
     else
     {
         MSMatches = null;
     }
 }
        public MSMatch FindMatchwithCharges(int Scan, double MZ, int Charge, double LowMZ, double HighMZ)
        {
            //double LowMZ =  (MZ * Charge + (float) 1.007277) / (Charge + 1);
            //double HighMZ = (MZ * Charge - 1.007277) / (Charge - 1);

            MSMatch First = FindMatch(Scan, MZ, Charge);

            if (First.Score == 0.0 || (First.SecondIsotope == 0.0 && !Program.Processor.MetaProfile))
            {
                return(First);
            }

            MSMatch Ret;

            if (Charge > 1)
            {
                Ret = FindMatch(Scan, HighMZ, Charge - 1);
                if (Ret.Score != 0.0 && (Ret.SecondIsotope > 0.0 || Program.Processor.MetaProfile))
                {
                    First.LowerCharges = Ret;
                }
            }

            Ret = FindMatch(Scan, LowMZ, Charge + 1);
            if (Ret.Score != 0.0 && (Ret.SecondIsotope > 0.0 || Program.Processor.MetaProfile))
            {
                First.UpperCharges = Ret;
            }
            return(First);
        }
 public MSMatch(MSMatch MS)
 {
     Score         = MS.Score;
     MZ            = MS.MZ;
     RT            = MS.RT;
     FirstIsotope  = MS.FirstIsotope;
     SecondIsotope = MS.SecondIsotope;
     TimeCoeff     = MS.TimeCoeff;
     if (LowerCharges != null)
     {
         LowerCharges = new MSMatch(MS.LowerCharges);
     }
     else
     {
         LowerCharges = null;
     }
     if (UpperCharges != null)
     {
         UpperCharges = new MSMatch(MS.UpperCharges);
     }
     else
     {
         UpperCharges = null;
     }
 }
        public MSMatch FindMatch(int Scan, double MZ, int Charge)
        {
            MSMatch ret = new MSMatch();

            ret.TimeCoeff = RTCorrection?TimeCoefs[Scan]:1;

            RawData Peaks = RawSpectra[ms2index[Scan]];

            MZData FirstPeak = Peaks.FindBiggestPeak(MZ, MassError);

            if (hasPreviousIsotope(Peaks, FirstPeak, Charge))             //skip this mass if Seq turns out Seq's a part of another peak
            {
                return(ret);
            }

            ret.FirstIsotope = FirstPeak.Intensity;
            if (Program.Processor.MetaProfile)
            {
                ret.Score         = ret.FirstIsotope;
                ret.SecondIsotope = 0.0;
            }
            else
            {
                ret.SecondIsotope = CheckIsotope(Peaks, FirstPeak.Mass, Charge, 1);
                ret.Score         = ret.FirstIsotope + ret.SecondIsotope;
                if (ret.SecondIsotope != 0.0)
                {
                    ret.Score += CheckIsotope(Peaks, FirstPeak.Mass, Charge, 2);
                }
            }

            ret.MZ = FirstPeak.Mass;
            ret.RT = Peaks.RT;

            if (RTCorrection)
            {
                ret.Score *= TimeCoefs[Scan];
            }


            if (ret.SecondIsotope == 0.0 && !Program.Processor.MetaProfile)
            {
                ret.Score = 0.0;
            }

            return(ret);
        }
        public QMatch FindBestScore(double MZ, int Charge, double IsoRatio, double MinRT, double MaxRT)
        {
            MSMatch First = null, Best = null;

            QMatch Res = new QMatch();

            Res.Clean();
            Res.MSMatches = new List <MSMatch>();

            double FirstIso = 0.0, SecondIso = 0.0;
            double BestScore = 0.0, fs = 0.0;
            int    BestIndex = 0;

            int    Scan  = 0;
            double AveRT = (MinRT + MaxRT) / 2;

            RawFile.ScanNumFromRT(AveRT, ref Scan);
            Scan = ms2index[Scan];

            //отступаем до минимального времени RT в массиве спектров
            double LowMZ = (MZ * Charge + (float)1.007277) / (Charge + 1);
            double HighMZ;

            if (Charge > 1)
            {
                HighMZ = (MZ * Charge - 1.007277) / (Charge - 1);
            }
            else
            {
                HighMZ = 0.0;
            }

            while (Scan > 0 && RawSpectra[Scan].RT >= MinRT)
            {
                Scan = IndexRev[Scan];
            }

            //цикл поиска лучшего
            while (Scan != -1 && RawSpectra[Scan].RT <= MaxRT)
            {
                if (Program.Processor.Deconvolution) //прихватываем другие зарядовые состояния
                {
                    First = FindMatchwithCharges(Scan, MZ, Charge, LowMZ, HighMZ);
                }
                else
                {
                    First = FindMatch(Scan, MZ, Charge);
                }

                //если не найден сам пик или его первый изотоп - сбрасываем
                if (First.Score == 0.0 || (First.SecondIsotope == 0.0 && !Program.Processor.MetaProfile))
                {
                    Scan = IndexDir[Scan];
                    continue;
                }

                FirstIso = First.FirstIsotope +
                           (First.UpperCharges == null ? 0 : First.UpperCharges.FirstIsotope) +
                           (First.LowerCharges == null ? 0 : First.LowerCharges.FirstIsotope);
                SecondIso = First.SecondIsotope +
                            (First.UpperCharges == null ? 0 : First.UpperCharges.SecondIsotope) +
                            (First.LowerCharges == null ? 0 : First.LowerCharges.SecondIsotope);

                fs = SecondIso / FirstIso;

                double CurrentScore = First.Score +
                                      (First.UpperCharges == null ? 0 : First.UpperCharges.Score) +
                                      (First.LowerCharges == null ? 0 : First.LowerCharges.Score);

                //пик может распространяться за границы, но апекс должен быть внутри
                if (CurrentScore > BestScore && ((fs > IsoRatio * 0.5 && fs < IsoRatio * 2) || Program.Processor.MetaProfile))
                {
                    BestScore = CurrentScore;
                    BestIndex = Scan;
                    Best      = First;
                }
                //переходим к следующему full-ms
                Scan = IndexDir[Scan];
            }

            if (BestScore == 0.0)
            {
                return(null);
            }

            //если хоть что-то нашли
            //добавляем наибольшее
            LowMZ = (Best.MZ * Charge + (float)1.007277) / (Charge + 1);
            if (Charge > 1)
            {
                HighMZ = (MZ * Charge - 1.007277) / (Charge - 1);
            }
            else
            {
                HighMZ = 0.0;
            }


            Res.ApexRT = Best.RT;
            Res.MSMatches.Add(Best);

            //берем сплошную область вокруг Best - вперед
            for (Scan = IndexDir[BestIndex]; Scan > 0; Scan = IndexDir[Scan])
            {
                if (Program.Processor.Deconvolution)
                { //прихватываем другие зарядовые состояния
                    First = FindMatchwithCharges(Scan, Best.MZ, Charge, LowMZ, HighMZ);
                }
                else
                {
                    First = FindMatch(Scan, Best.MZ, Charge);
                }

                if (First.Score == 0.0 || (First.SecondIsotope == 0.0 && !Program.Processor.MetaProfile))
                {
                    Res.MSMatches.Add(First);
                    break;
                }

                //ограничение по уровню разрешения пика
                if (Program.Processor.MetaProfile && First.Score <= Best.Score * (Program.Processor.RTRes / 100.0))
                {
                    break;
                }

                //ограничение по ширине пика
                if (Program.Processor.MetaProfile && Res.MSMatches[Res.MSMatches.Count - 1].RT - Res.MSMatches[0].RT > Program.Processor.MaxRTWidth)
                {
                    return(null);
                }

                fs = First.SecondIsotope / First.FirstIsotope;
                //включаем здесь контроль по соотношению изотопов
                //if (fs < IsoRatio*0.5 || fs > IsoRatio * 2 ){
                //    break;
                //}
                //пик может распространяться за границы, но апекс должен быть внутри - по крайней мере для метаболитов
                if (Program.Processor.MetaProfile && First.RT > MaxRT && First.Score > Best.Score * 1.1)
                {
                    return(null);
                }

                Res.MSMatches.Add(First);
            }

            //берем сплошную область вокруг Best - назад
            for (Scan = IndexRev[BestIndex]; Scan > 0; Scan = IndexRev[Scan])
            {
                if (Program.Processor.Deconvolution)  //прихватываем другие зарядовые состояния
                {
                    First = FindMatchwithCharges(Scan, Best.MZ, Charge, LowMZ, HighMZ);
                }
                else
                {
                    First = FindMatch(Scan, Best.MZ, Charge);
                }

                if (First.Score == 0.0 || (First.SecondIsotope == 0.0 && !Program.Processor.MetaProfile))
                {
                    Res.MSMatches.Insert(0, First);
                    break;
                }
                //ограничение по уровню разрешения пика
                if (Program.Processor.MetaProfile && First.Score <= Best.Score * (Program.Processor.RTRes / 100.0))
                {
                    break;
                }

                //ограничение по ширине пика
                if (Program.Processor.MetaProfile && Res.MSMatches[Res.MSMatches.Count - 1].RT - Res.MSMatches[0].RT > Program.Processor.MaxRTWidth)
                {
                    return(null);
                }

                fs = First.SecondIsotope / First.FirstIsotope;
                //включаем здесь контроль по соотношению изотопов
                //if (fs < IsoRatio*0.5 || fs > IsoRatio * 2 ){
                //    break;
                //}
                Res.MSMatches.Insert(0, First);

                //пик может распространяться за границы, но апекс должен быть внутри - по крайней мере для метаболитов
                if (Program.Processor.MetaProfile && First.RT < MinRT && First.Score > Best.Score * 1.1)
                {
                    return(null);
                }
            }
            if (Program.Processor.MetaProfile && Res.MSMatches[Res.MSMatches.Count - 1].RT - Res.MSMatches[0].RT < Program.Processor.MinRTWidth)
            {
                return(null);
            }

            Res.Estimate(IsoRatio);
            Res.CleanUp();

            return(Res);
        }