public FileService(string FileName)
        {
            FileBox.RepProgress = RepProgress;
            string Ext = Path.GetExtension(FileName);

            if (Ext == ".raw")
            {
                RawFile = new RawFileBox();
            }
            else
            {
                RawFile = new AgilentFileBox();
            }
            RawFile.LoadIndex(FileName);
            //RawFile.RTCorrection = true;
            MZData.SetRawFile(RawFile);
            double EndRT = 0.0;

            //pass some last MSMS spectra?
            for (int i = RawFile.RawSpectra.Length - 1; i > 0; i--)
            {
                EndRT = RawFile.RawSpectra[i].RT;
                if (EndRT > 0.0)
                {
                    break;
                }
            }
            RawFile.LoadInterval(0.0, EndRT);
        }
示例#2
0
        static double CheckRTApex(double RT, double MZ)
        {
            int Start = 0;

            for (int i = 0; i < MSFileBox.Spectra; i++)
            {
                if (MSFileBox.RawSpectra[i].RT >= RT)
                {
                    Start = MSFileBox.IndexRev[i];
                    break;
                }
            }
            double BestIntensity = 0.0;
            int    BestIndex     = 0;

            //на минуту назад
            for (int i = Start; i > 0 && RT - MSFileBox.RawSpectra[i].RT < 1.0; i = MSFileBox.IndexRev[i])
            {
                if (MSFileBox.RawSpectra[i].Data == null)
                {
                    MSFileBox.ReadMS(i);
                }
                MZData CurPeak = MSFileBox.RawSpectra[i].FindBiggestPeak(MZ, 10.0);
                if (CurPeak.Intensity == 0.0)
                {
                    break;
                }
                if (CurPeak.Intensity > BestIntensity)
                {
                    BestIntensity = CurPeak.Intensity;
                    BestIndex     = i;
                }
            }

            //на две минуты вперед
            for (int i = MSFileBox.IndexDir[Start]; i >= 0 && MSFileBox.RawSpectra[i].RT - RT < 2.0; i = MSFileBox.IndexDir[i])
            {
                if (MSFileBox.RawSpectra[i].Data == null)
                {
                    MSFileBox.ReadMS(i);
                }
                MZData CurPeak = MSFileBox.RawSpectra[i].FindBiggestPeak(MZ, 10.0);
                if (CurPeak.Intensity == 0.0)
                {
                    break;
                }
                if (CurPeak.Intensity > BestIntensity)
                {
                    BestIntensity = CurPeak.Intensity;
                    BestIndex     = i;
                }
            }

            if (BestIntensity == 0.0)
            {
                return(0.0);
            }
            return(MSFileBox.RawSpectra[BestIndex].RT);
        }
示例#3
0
        public static Target TargetFromPoint(MZData Point, int ID)
        {
            if (Point == null)
            {
                return(null);
            }
            Target T = new Target();

            T.ID         = ID;
            T.Name       = String.Format("Target #{0}", ID);
            T.Desc       = String.Format("Target #{0}, Init abund:{1}, MZ :{2}, RT :{3}", ID, Point.Intensity, Point.Mass, Point.RT);
            T.MZ         = Point.Mass;
            T.RT         = Point.RT;
            T.RTMin      = Point.RT - Program.RTError;
            T.RTMax      = Point.RT + Program.RTError;
            T.C13toCheck = Program.C13toCheck;
            T.Charge     = 1;
            return(T);
        }
示例#4
0
 public static LCTrace CreateTrace(LCMSGroup Group, MZData Start)
 {
     if (Group == null)
     {
         return(null);
     }
     if (Start == null)
     {
         Start = Group.Points[0];
         foreach (MZData P in Group.Points)
         {
             if (P.Intensity > Start.Intensity)
             {
                 Start = P;
             }
         }
     }
     return(new LCTrace(Group, Start));
 }
示例#5
0
        public void ApplyPeak(Peak P, LCTrace Trace)
        {
            MZData thisPoint  = null;
            MZData TracePoint = null;
            double MSum       = 0.0;
            double ISum       = 0.0;
            double aSum       = 0.0;
            double bSum       = 0.0;

            for (int i = Trace.Group.Points[P.LeftIndex].Scan; i <= Trace.Group.Points[P.RightIndex].Scan; i++)
            {
                thisPoint  = PointForScan(i);
                TracePoint = Trace.PointForScan(i);
                MSum      += thisPoint == null ? 0.0 : thisPoint.Mass * thisPoint.Intensity * thisPoint.TimeCoeff;
                ISum      += thisPoint == null ? 0.0 : thisPoint.Intensity;
                aSum      += (thisPoint != null && TracePoint != null)?thisPoint.Intensity:0.0;
                bSum      += (thisPoint != null && TracePoint != null)?TracePoint.Intensity:0.0;
            }
            PeakTotal    = ISum;
            PeakMeanMass = ISum != 0.0 ? MSum / ISum : 0.0;
            PeakRatio    = bSum != 0.0 ? aSum / bSum : 0.0;
        }
        private MZData TakeFreePoint(int Scan, double Mass, LCMSGroup Group)
        {
            MZData Point = RawFile.RawSpectra[Scan].FindNearestPeak(Mass, Program.MassError);

            if (Point.Mass != 0.0)
            {
                if (Point.Group == null)
                {
                    Point.Group = Group;
                    Group.Points.Add(Point);
                    return(Point);
                }
                else
                {
                    if (Point.Group != Group)
                    {
                        //Console.WriteLine("Warning: Group conflict {0}",ConflictCounter++);
                        ConflictCounter++;
                    }
                }
            }
            return(null);
        }
示例#7
0
 LCTrace(LCMSGroup Group, MZData Start)
 {
     this.Group      = Group;
     this.StartPoint = Start;
 }
示例#8
0
        static int Main(string[] args)
        {
            //Data Sources
            //args[0] - database
            //args[1] - Raw File Name
            //args[2] - ID for file
            try{
                Console.ReadLine();
                Console.WriteLine("Information: Loading...", args[1]);

                //Load Parameters

                string DBName = args[0];
                int    FileID = Convert.ToInt32(args[2]);
                //against database locking at start time

                int RepeatCount = 0;
                while (RepeatCount < 10)
                {
                    try{
                        DBInt = new DBInterface();
                        DBInt.InitDB(DBName);
                        LoadParameters();
                        break;
                    }catch (System.Data.SQLite.SQLiteException sqle) {
                        if (sqle.ErrorCode == System.Data.SQLite.SQLiteErrorCode.Busy && RepeatCount < 10)
                        {
                            //здесь возможна генерация идентичных features (- с точностью до границ пика)
                            Console.WriteLine("Warning: {0}", sqle.Message);
                            System.Threading.Thread.Sleep(10000);
                            RepeatCount++;
                        }
                        else
                        {
                            throw sqle;
                        }
                    }
                }

                RawFileService = new FileService(args[1]);

                Target T    = null;
                MZData P    = null;
                double MaxI = 0;

                Console.WriteLine("Information: Building data map");
                RawFileService.BuildDataMap();
                P    = RawFileService.BiggestUnused();
                T    = Target.TargetFromPoint(P, 0);
                MaxI = P.Intensity;

                Console.WriteLine("Information: Targets processing...", args[1]);

                List <Feature> ReadyFeatures = new List <Feature>();
                int            i             = 0;
                int            ICount        = 1;
                while (T != null)
                {
                    //Progress indicator
                    if ((int)Math.Pow(100, (Math.Log10(MaxI / DataThres) - Math.Log10(P.Intensity / DataThres)) / (Math.Log10(MaxI / DataThres))) > ICount)
                    {
                        Console.WriteLine("Information: Targets - {0}; Intensity - {1} ", i, P.Intensity);
                        Console.WriteLine("{0}%...", (int)Math.Pow(100, (Math.Log10(MaxI / DataThres) - Math.Log10(P.Intensity / DataThres)) / (Math.Log10(MaxI / DataThres))));
                        ICount++;
                    }
                    Feature F = FeatureForTarget(T, P);
                    if (F != null)
                    {
                        ReadyFeatures.Add(F);
                    }
                    i++;

                    //make a features from additional peaks
                    if (F != null)
                    {
                        for (int j = 0; j < F.TPeaks.Peaks.Count; j++)
                        {
                            if (F.TPeaks.TargetPeak != F.TPeaks.Peaks[j])
                            {
                                T = Target.TargetFromPeak(F.TPeaks.Peaks[j], i);
                                Feature NF = FeatureForTarget(T, P);
                                ReadyFeatures.Add(NF); //!!!!! ReadyFeatures.Add(F);
                                i++;
                            }
                        }
                    }
                    P = RawFileService.BiggestUnused();
                    if (P == null)
                    {
                        break;
                    }
                    T = Target.TargetFromPoint(P, i);
                }

                RepeatCount = 0;
                while (RepeatCount < 10000)
                {
                    try{
                        DBInt.tr = DBInt.con.BeginTransaction();
                        DBInt.SaveFile(args[1], FileID, RawFileService.RawFile.Mode);
                        LCMSGroup.GroupBase = DBInt.GetGroupBase();
                        foreach (Feature F in ReadyFeatures)
                        {
                            if (F != null && DBInt != null)
                            {
                                F.Write(DBInt.con, FileID);
                            }
                        }
                        RawFileService.SaveRTs(DBInt.con, FileID);
                        LCMSGroup.SaveGroups(DBInt.con, FileID);
                        DBInt.tr.Commit();
                        break;
                    }catch (System.Data.SQLite.SQLiteException sqle) {
                        if (sqle.ErrorCode == System.Data.SQLite.SQLiteErrorCode.Busy && RepeatCount < 1000)
                        {
                            Console.WriteLine("Warning: {0}", sqle.Message);
                            System.Threading.Thread.Sleep(1000);
                            RepeatCount++;
                        }
                        else
                        {
                            throw sqle;
                        }
                    }
                }
                Console.WriteLine("Completed");
                Console.ReadLine();
            }catch (Exception e) {
                Console.Write("Error:");
                Console.Write(e.Message);
                Console.WriteLine("STACKINFO:" + e.StackTrace);
                Console.WriteLine("Completed");
                Console.ReadLine();
                return(1);
            }
            return(0);
        }
示例#9
0
        static Feature FeatureForTarget(Target T, MZData P)
        {
            Feature F = new Feature();

            T.Feature = F;
            F.Target  = T;

            //Monoisotopic trace
            MZData Apex = null;

            Apex = P;

            if (Apex == null)
            {
                return(null);
            }

            F.MainTrace = LCTrace.CreateTrace(RawFileService.GroupFromPoint(Apex), Apex);//main for untargeted analysis

            //? gapped for main trace
            if (F.MainTrace == null)
            {
                return(null);
            }
            F.MainTrace.Attribution = "C0N0";

            double RTStart = F.MainTrace.Group.Points[0].RT;
            double RTEnd   = F.MainTrace.Group.Points[F.MainTrace.Group.Points.Count - 1].RT;

            //Check if Apex outside of RT Window
            if (F.MainTrace.Apex.RT < T.RTMin || F.MainTrace.Apex.RT > T.RTMax)
            {
                F.MainApexOutsideRtWindow = true;
            }

            F.TPeaks = new TracePeaks(F.MainTrace);
            F.TPeaks.waveletPeakDetection(PeakMinWidth, PeakMaxWidth, PeakMinIntensity, PeakbaselineRatio);
            F.TPeaks.SelectClosestAsTarget(F.Target);
            //End of monotrace

            F.HasPrevIsotope = false;
            //End of preisotopes

            //Checks for isotopic peaks
            F.Isotopes = new LCTrace[T.C13toCheck + 1];

            F.Isotopes[0] = F.MainTrace;

            //Pure isotopes
            for (int C13 = 1; C13 <= T.C13toCheck; C13++)
            {
                double TargetMass = Apex.Mass + (C13Shift * (double)(C13)) / T.Charge;
                MZData D          = RawFileService.RawFile.RawSpectra[Apex.Scan].FindNearestPeak(TargetMass, MassError);
                if (D.Mass > 0.0)
                {
                    F.Isotopes[C13] = LCTrace.CreateTrace(RawFileService.GroupFromPoint(D), D);
                }
                else
                {
                    F.Isotopes[C13] = null;
                }
                //Gapped trace (it was only actual if low signals is turned on - may provide some non-obvious errors)
                if (F.Isotopes[C13] == null ||
                    (F.Isotopes[C13].Group.Points[0].RT > T.RTMin &&
                     F.Isotopes[C13].Group.Points[F.Isotopes[C13].Group.Points.Count - 1].RT < T.RTMax))
                {
                    F.Isotopes[C13] = LCTrace.CreateTrace(RawFileService.GroupFromArea(T.RTMin, T.RTMax, TargetMass), D.Mass == 0.0?null:D);
                }
                if (F.Isotopes[C13] != null)
                {
                    F.Isotopes[C13].Attribution = String.Format("C{0}N{1}", C13, 0);
                }
            }

            //Apply peaks
            if (F.TPeaks.TargetPeak != null)
            {
                F.ApplyPeak(F.TPeaks.TargetPeak);
            }
            return(F);
        }
        public LCMSGroup GroupFromPoint(MZData Point)
        {
            if (Point.Group != null)
            {
                return(Point.Group as LCMSGroup);
            }

            LCMSGroup Group = new LCMSGroup();

            Point.Group = Group;
            Group.Points.Add(Point);
            Queue <MZData> QPoints = new Queue <MZData>();

            QPoints.Enqueue(Point);
            do
            {
                MZData CurrentPoint = QPoints.Dequeue();
                int    ForwardScan  = RawFile.IndexDir[CurrentPoint.Scan];
                int    BackwardScan = RawFile.IndexRev[CurrentPoint.Scan];
                for (int i = 0; i < Program.ZeroScans + 1; i++)
                {
                    if (ForwardScan > 0)
                    {
                        MZData NextPoint = TakeFreePoint(ForwardScan, CurrentPoint.Mass, Group);
                        if (NextPoint != null)
                        {
                            QPoints.Enqueue(NextPoint);
                        }
                        ForwardScan = RawFile.IndexDir[ForwardScan];
                    }
                    if (BackwardScan > 0)
                    {
                        MZData NextPoint = TakeFreePoint(BackwardScan, CurrentPoint.Mass, Group);
                        if (NextPoint != null)
                        {
                            QPoints.Enqueue(NextPoint);
                        }
                        BackwardScan = RawFile.IndexRev[BackwardScan];
                    }
                }
            } while(QPoints.Count > 0);

            Group.Points.Sort((p1, p2) => p1.Scan.CompareTo(p2.Scan));
            //Add zero points
            Group.Points.Add(MZData.CreateZero(RawFile.IndexDir[Group.Points[Group.Points.Count - 1].Scan]));
            Group.Points.Add(MZData.CreateZero(RawFile.IndexRev[Group.Points[0].Scan]));
            for (int i = Group.Points.Count - 3; i > 0; i--)
            {
                int CurrentScan = RawFile.IndexRev[Group.Points[i].Scan];
                while (CurrentScan > Group.Points[i - 1].Scan)
                {
                    Group.Points.Add(MZData.CreateZero(CurrentScan));
                    CurrentScan = RawFile.IndexRev[CurrentScan];
                }
            }
            Group.Points.Sort((p1, p2) => p1.Scan.CompareTo(p2.Scan));

            if (Group.Points[Group.Points.Count - 1].RT - Group.Points[0].RT < Program.PeakMinWidth)
            {
                return(null);
            }

            LCMSGroup.Global.Add(Group);
            return(Group);
        }
 public static int CompMZDatabyIntensity(MZData x, MZData y)
 {
     return((x.Intensity == y.Intensity)?0:(x.Intensity > y.Intensity?-1:1));
 }
        public LCMSGroup GroupFromArea(double StartRT, double EndRT, double MZ, double MassError = 0.0)
        {
            LCMSGroup Group = new LCMSGroup();

            if (MassError == 0.0)
            {
                MassError = Program.MassError;
            }
            int StartScan = RawFile.ScanNumFromRT(StartRT);
            int EndScan   = RawFile.ScanNumFromRT(EndRT);

            for (int Scan = RawFile.IndexDir[StartScan]; Scan <= EndScan; Scan = RawFile.IndexDir[Scan])
            {
                if (Scan == -1)
                {
                    break;
                }
                MZData Next = RawFile.RawSpectra[Scan].FindBiggestPeak(MZ, MassError);
                Group.Points.Add(Next);
            }

            //Заполнение дыр
            //из начала в конец
            int LastIndex = -Program.ZeroScans - 1;

            for (int i = 0; i < Group.Points.Count; i++)
            {
                if (Group.Points[i].Intensity == 0.0)
                {
                    if (i - LastIndex < Program.ZeroScans)
                    {
                        double LastMZ = Group.Points[LastIndex].Mass;
                        MZData Next   = RawFile.RawSpectra[Group.Points[i].Scan].FindBiggestPeak(LastMZ, MassError);
                        Group.Points[i] = Next;
                        if (Next.Intensity != 0.0)
                        {
                            LastIndex = i;
                        }
                    }
                }
                else
                {
                    LastIndex = i;
                }
            }
            //из конца в начало
            LastIndex = Group.Points.Count + Program.ZeroScans + 1;
            for (int i = Group.Points.Count - 1; i >= 0; i--)
            {
                if (Group.Points[i].Intensity == 0.0)
                {
                    if (LastIndex - i < Program.ZeroScans)
                    {
                        double LastMZ = Group.Points[LastIndex].Mass;
                        MZData Next   = RawFile.RawSpectra[Group.Points[i].Scan].FindBiggestPeak(LastMZ, MassError);
                        Group.Points[i] = Next;
                        if (Next.Intensity != 0.0)
                        {
                            LastIndex = i;
                        }
                    }
                }
                else
                {
                    LastIndex = i;
                }
            }

            //leading and tailing zeros (except one)
            while (Group.Points.Count > 1 &&
                   Group.Points[0].Intensity == 0.0 &&
                   Group.Points[1].Intensity == 0.0)
            {
                Group.Points.RemoveAt(0);
            }
            while (Group.Points.Count > 1 &&
                   Group.Points[Group.Points.Count - 1].Intensity == 0.0 &&
                   Group.Points[Group.Points.Count - 2].Intensity == 0.0)
            {
                Group.Points.RemoveAt(Group.Points.Count - 1);
            }

            if (Group.Points.Count <= 1)
            {
                return(null);
            }

            LCMSGroup.Global.Add(Group);
            return(Group);
        }