Example #1
0
 public FeatureDetectionMethods(ThermoRawFile rawFile)
 {
     this.finalGroups = new List <FeatureGroup>();
     this.eiSpectra   = new List <EISpectrum>();
     this.rawFile     = rawFile;
     this.norm        = new Normalization();
 }
 public void PushFeaturesToDatabase(ThermoRawFile rawFile, List <Feature> allFeatures, SQLiteConnection conn)
 {
     AddRawFileEntry(rawFile, conn);
     using (var transaction = conn.BeginTransaction())
     {
         if (!ContainsRawFileFeatures(rawFile, conn))
         {
             int id              = GetRawFileID(rawFile, conn);
             int count           = 0;
             int onePercentCount = (int)(0.01 * allFeatures.Count);
             onePercentCount++;
             foreach (var feature in allFeatures)
             {
                 if (!ContainsSpecificFeature(feature.ID_Number, conn))
                 {
                     double apexIntensity = 0;
                     double apexRT        = 0;
                     var    smoothLine    = "";
                     var    rawLine       = "";
                     foreach (var peak in feature.smoothRTPeaks)
                     {
                         if (peak.Intensity > apexIntensity)
                         {
                             apexIntensity = peak.Intensity;
                             apexRT        = peak.RT;
                         }
                         smoothLine += peak.RT + "," + peak.Intensity + ";";
                     }
                     foreach (var peak in feature.RawRTPeaks)
                     {
                         rawLine += peak.RT + "," + peak.Intensity + ";";
                     }
                     var insertText = "INSERT INTO featureTable (ID, Name, mz, ApexRT, ApexIntensity, ID_Number, SmoothFeatureString, RawFeatureString)"
                                      +
                                      " VALUES (@ID, @Name, @mz, @ApexRT, @ApexIntensity, @ID_Number, @SmoothFeatureString, @RawFeatureString)";
                     var insertCommand = new SQLiteCommand(insertText, conn);
                     insertCommand.Parameters.AddWithValue("@ID", id);
                     insertCommand.Parameters.AddWithValue("@Name", rawFile.Name);
                     insertCommand.Parameters.AddWithValue("@mz", feature.averageMZ);
                     insertCommand.Parameters.AddWithValue("@ApexRT", apexRT);
                     insertCommand.Parameters.AddWithValue("@ApexIntensity", apexIntensity);
                     insertCommand.Parameters.AddWithValue("@SmoothFeatureString", smoothLine);
                     insertCommand.Parameters.AddWithValue("@RawFeatureString", rawLine);
                     insertCommand.Parameters.AddWithValue("@ID_Number", feature.ID_Number);
                     insertCommand.ExecuteNonQuery();
                     count++;
                     if (count % onePercentCount == 0)
                     {
                         OnProgressUpdate(.01);
                     }
                 }
             }
         }
         transaction.Commit();
         transaction.Dispose();
         OnFinish();
     }
 }
        public bool ContainsRawFileFeatures(ThermoRawFile rawFile, SQLiteConnection conn)
        {
            var queryText    = "SELECT s.Name FROM featureTable s WHERE s.Name=@Name";
            var queryCommand = new SQLiteCommand(queryText, conn);

            queryCommand.Parameters.AddWithValue("@Name", rawFile.Name);
            var reader = queryCommand.ExecuteReader();

            return(reader.HasRows);
        }
Example #4
0
 private void GroupFeatures()
 {
     if (currIndex < allRawFileNames.Count)
     {
         var currRawFileString = allRawFileNames[currIndex];
         currRawFile = new ThermoRawFile(currRawFileString);
         currRawFile.Open();
         var path = outputTextBox.Text + "\\" + currRawFile.Name + "_ExtractedFeatures.gcfeat";
         conn = new SQLiteConnection(@"Data Source=" + path);
         conn.Open();
         rawFileListBox.ClearSelected();
         rawFileListBox.SelectedIndex = currIndex;
         if (!SQLiteIOMethods.IsGroupingDone(conn))
         {
             currFDM           = new FeatureDetectionMethods(currRawFile);
             statusLabel.Text  = "Grouping Features from " + currRawFile.Name + "...";
             currFDM.Finished += grouping_Finished;
             //currentThread = new Thread(() => currFDM.GroupFeatures(conn));
             //currentThread.Start();
         }
         else
         {
             currIndex++;
             double percent = ((double)currIndex) / ((double)allRawFileNames.Count) * 100;
             statusBar.Value = (int)percent;
             GroupFeatures();
         }
     }
     else
     {
         currRawFile.ClearCachedScans();
         currRawFile.Dispose();
         if (conn != null)
         {
             conn.Close();
             conn.Dispose();
         }
         conn = null;
         if (currFDM != null)
         {
             currFDM.Dispose();
             currFDM = null;
         }
         GC.Collect();
         rawFileListBox.ClearSelected();
         foreach (Control control in this.Controls)
         {
             control.Enabled = true;
         }
         statusBar.Value  = 100;
         statusLabel.Text = "Done!";
     }
 }
        public static bool ContainsRawFileFeatures(SQLiteConnection conn, ThermoRawFile raw)
        {
            var queryText    = "SELECT s.ID FROM featureTable s WHERE s.Name=@Name";
            var queryCommand = new SQLiteCommand(queryText, conn);

            queryCommand.Parameters.AddWithValue("@Name", raw.Name);
            var reader = queryCommand.ExecuteReader();
            var hasRow = reader.HasRows;

            reader.Close();
            reader.Dispose();
            return(hasRow);
        }
        public static int GetRawFileID(ThermoRawFile raw, SQLiteConnection conn)
        {
            var queryText    = "SELECT s.ID FROM rawFileNameTable s WHERE s.Name=@Name";
            var queryCommand = new SQLiteCommand(queryText, conn);

            queryCommand.Parameters.AddWithValue("@Name", raw.Name);
            var reader = queryCommand.ExecuteReader();
            int id     = 0;

            while (reader.Read())
            {
                id = int.Parse(reader["ID"].ToString());
            }
            return(id);
        }
 public static void AddRawFileTICChroma(ThermoRawFile rawFile, SQLiteConnection conn)
 {
     if (!ContainsRawFileTIC(conn, rawFile))
     {
         var ticChroma = rawFile.GetTICChroma();
         var line      = "";
         foreach (var peak in ticChroma)
         {
             line += peak.Time + "," + peak.Intensity + ";";
         }
         int id            = GetRawFileID(rawFile, conn);
         var insertText    = "INSERT INTO ticTable (ID, Name, TICChroma) VALUES (@ID, @Name, @TICChroma)";
         var insertCommand = new SQLiteCommand(insertText, conn);
         insertCommand.Parameters.AddWithValue("@ID", id);
         insertCommand.Parameters.AddWithValue("@Name", rawFile.Name);
         insertCommand.Parameters.AddWithValue("@TICChroma", line);
         insertCommand.ExecuteNonQuery();
     }
 }
        public void PrepDatabase(string rawFilePath, string outputDirectory)
        {
            var rawFile = new ThermoRawFile(rawFilePath);

            rawFile.Open();
            var path = outputDirectory + "\\" + rawFile.Name + "_ExtractedFeatures.gcfeat";
            var conn = new SQLiteConnection(@"Data Source=" + path);

            conn.Open();
            SQLiteIOMethods.CreateTablesInDatabase(conn);
            SQLiteIOMethods.AddRawFileEntry(rawFile, conn);
            SQLiteIOMethods.AddRawFileTICChroma(rawFile, conn);
            SQLiteIOMethods.CreateIndices(conn);
            conn.Close();
            conn.Dispose();
            rawFile.ClearCachedScans();
            rawFile.Dispose();
            OnFinish();
        }
        public static void AddRawFileEntry(ThermoRawFile rawFile, SQLiteConnection conn)
        {
            List <int> ids = new List <int>();
            var        queryCommandText = "SELECT s.ID FROM rawFileNameTable s";
            var        queryCommand     = new SQLiteCommand(queryCommandText, conn);
            var        reader           = queryCommand.ExecuteReader();

            while (reader.Read())
            {
                ids.Add(int.Parse(reader["ID"].ToString()));
            }
            ids = ids.OrderBy(x => x).ToList();
            if (ids.Count == 0)
            {
                int id = 1;
                queryCommandText = "INSERT INTO rawFileNameTable (ID, Name, Extracted, Grouped) VALUES (@ID, @Name, @Extracted, @Grouped)";
                queryCommand     = new SQLiteCommand(queryCommandText, conn);
                queryCommand.Parameters.AddWithValue("@ID", id);
                queryCommand.Parameters.AddWithValue("@Name", rawFile.Name);
                queryCommand.Parameters.AddWithValue("@Extracted", false);
                queryCommand.Parameters.AddWithValue("@Grouped", false);
                queryCommand.ExecuteNonQuery();
                queryCommand.Dispose();
            }
            else
            {
                int nextID = ids.Last() + 1;
                if (!ContainsRawFile(conn, rawFile))
                {
                    var insertText    = "INSERT INTO rawFileNameTable (ID, Name, Extracted) VALUES (@ID, @Name, @Extracted, @Grouped)";
                    var insertCommand = new SQLiteCommand(insertText, conn);
                    insertCommand.Parameters.AddWithValue("@ID", nextID);
                    insertCommand.Parameters.AddWithValue("@Name", rawFile.Name);
                    insertCommand.Parameters.AddWithValue("@Extracted", false);
                    insertCommand.Parameters.AddWithValue("@Grouped", false);
                    insertCommand.ExecuteNonQuery();
                    nextID++;
                }
            }
        }
Example #10
0
        public List <MZPeak> GetPeaksFromNearestSpectrum(FeatureGroup group, ThermoRawFile rawFile)
        {
            int              scanNum     = rawFile.GetSpectrumNumber(group.ApexTime);
            List <MZPeak>    returnPeaks = new List <MZPeak>();
            ThermoSpectrum   spectrum    = rawFile.GetLabeledSpectrum(scanNum);
            HashSet <double> peakMZs     = new HashSet <double>();

            foreach (Feature feature in group.allFeatures)
            {
                MZPeak outPeak = spectrum.GetClosestPeak(MassRange.FromPPM(feature.AverageMZ, 10));
                if (outPeak != null)
                {
                    if (!peakMZs.Contains(outPeak.Intensity))
                    {
                        returnPeaks.Add(new MZPeak(feature.AverageMZ, outPeak.Intensity));
                        peakMZs.Add(outPeak.Intensity);
                    }
                }
            }
            returnPeaks = returnPeaks.OrderBy(x => x.MZ).ToList();
            return(returnPeaks);
        }
Example #11
0
        private void extractFeatures()
        {
            if (currIndex < allRawFileNames.Count)
            {
                var currRawFileString = allRawFileNames[currIndex];
                currRawFile = new ThermoRawFile(currRawFileString);
                currRawFile.Open();
                var path = outputTextBox.Text + "\\" + currRawFile.Name + "_ExtractedFeatures.gcfeat";
                conn = new SQLiteConnection(@"Data Source=" + path);
                conn.Open();
                rawFileListBox.ClearSelected();
                rawFileListBox.SelectedIndex = currIndex;
                this.Text = currIndex + "\\" + allRawFileNames.Count + " Deconvolved...";
                if (!SQLiteIOMethods.IsExtractionDone(conn))
                {
                    currFDM           = new FeatureDetectionMethods(currRawFile);
                    statusLabel.Text  = "Adding Features from " + currRawFile.Name + " to Database...";
                    currFDM.Progress += deconvolution_Progress;
                    currFDM.Finished += deconvolution_Finished;
                    currentThread     = new Thread(() => currFDM.StepwiseSetupFinalGroups(currRawFile, conn));
                    currentThread.Start();
                }
                else
                {
                    conn.Close();
                    conn.Dispose();
                    currRawFile.Dispose();
                    currIndex++;
                    extractFeatures();
                }
            }
            else
            {
                currIndex = 0;
                //statusBar.Value = 0;
                //statusLabel.Text = "Starting Feature Grouping...";
                //GroupFeatures();

                currRawFile.ClearCachedScans();
                currRawFile.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }
                conn = null;
                if (currFDM != null)
                {
                    currFDM.Dispose();
                    currFDM = null;
                }
                GC.Collect();
                rawFileListBox.ClearSelected();
                foreach (Control control in this.Controls)
                {
                    control.Enabled = true;
                }
                statusBar.Value  = 100;
                statusLabel.Text = "Done!";
                this.Text        = "Deconvolution Engine";
            }
        }
Example #12
0
        public void StepwiseSetupFinalGroups(ThermoRawFile rawfile, SQLiteConnection conn)
        {
            List <int> list = new List <int>();

            foreach (var scan in rawFile.GetMsScans())
            {
                if (scan.MsnOrder == 1)
                {
                    list.Add(scan.SpectrumNumber);
                }
            }
            var extractedFeatures = ExtractFeatures(list, 10).ToList();

            //Features have been extracted
            //Check which features are valid and which are not based on how their max intensity changes

            //nwk temp code
            var tmpFeatures = extractedFeatures.Where(x => x.ApexTime > 13.55 && x.apexTime <13.70 && x.AverageMZ> 267.082 && x.averageMZ < 267.095).ToList();
            var q           = "";



            CheckPeakRise(extractedFeatures);
            //Smooth features now
            foreach (Feature feature in extractedFeatures)
            {
                feature.SmoothRTPeaks = GetRollingAveragePeaks(feature.RawRTPeaks, 9);
            }
            CheckPeakRiseSmooth(extractedFeatures);
            List <Feature> finalFeatures = GetFinalFeatures(extractedFeatures);

            extractedFeatures.Clear();
            CheckPeakRiseSmooth(finalFeatures);
            int count = 1;

            foreach (var feat in finalFeatures)
            {
                feat.ID_Number = count;
                count++;
            }
            //push features to database
            PushFeaturesToDatabase(rawfile, finalFeatures, conn);


            List <FeatureGroup> groups = GroupFeatures(finalFeatures, .04, 2).ToList();

            DoubleBack(groups, finalFeatures);
            finalFeatures.Clear();
            List <FeatureGroup> allSubGroups = new List <FeatureGroup>();

            foreach (FeatureGroup group in groups)
            {
                GetMainSubgroup(group);
                GetFeatureSubGroups(group);
                if (group.mainSubGroup.allFeatures.Count > 1)
                {
                    allSubGroups.Add(group.mainSubGroup);
                }
                foreach (FeatureGroup subGroup in group.SubGroups)
                {
                    allSubGroups.Add(subGroup);
                }
            }
            this.finalGroups = GetFinalFeatures(allSubGroups);
            foreach (FeatureGroup group in this.finalGroups)
            {
                group.finalPeaks = GetPeaksFromNearestSpectrum(group, rawFile);
            }
            foreach (FeatureGroup group in this.finalGroups)
            {
                EISpectrum spec = new EISpectrum();
                spec.ApexTimeEI   = group.ApexTime;
                spec.FeatureGroup = group;
                spec.FinalEIPeaks = group.finalPeaks;
                spec.FinalEIPeaks = spec.FinalEIPeaks.OrderByDescending(x => x.Intensity).ToList();
                if (group.finalPeaks.Count >= 2) //here as well (changed to remove num feature restriction)
                {
                    spec.BasePeak = spec.FinalEIPeaks[0];
                    this.eiSpectra.Add(spec);
                }
                spec.FinalNormalizedEIPeaks.AddRange(spec.FinalEIPeaks);
                spec.FinalNormalizedEIPeaks = spec.FinalNormalizedEIPeaks.OrderBy(x => x.MZ).ToList();
                norm.CombineLikeMZPeaks(spec.FinalNormalizedEIPeaks);
                spec.AdjustedPeaks.AddRange(spec.FinalNormalizedEIPeaks);
                norm.GetAdjustedPeaks(spec.AdjustedPeaks);
                spec.DenominatorTerm = norm.GetDemoninatorTerm(spec.AdjustedPeaks);
            }
            //this.eiSpectra = CleanUpSpectra(eiSpectra);
            this.eiSpectra = eiSpectra.Where(x => x.FinalEIPeaks.Count >= 2).ToList();
            AddFeatureGroupsToDatabase(this.eiSpectra, conn);
            SQLiteIOMethods.ExtractionComplete(conn);
            SQLiteIOMethods.GroupingComplete(conn);
            OnFinish();
        }
Example #13
0
        private void StepwiseSetupFinalGroups(ThermoRawFile rawFile)
        {
            List <int> list = new List <int>();

            foreach (var scan in rawFile.GetMsScans())
            {
                if (scan.RetentionTime < 7)
                {
                    list.Add(scan.SpectrumNumber);
                }
            }
            var extractedFeatures = ExtractFeatures(list, 10).ToList();

            //Features have been extracted
            //Check which features are valid and which are not based on how their max intensity changes
            CheckPeakRise(extractedFeatures);
            //Smooth features now
            foreach (Feature feature in extractedFeatures)
            {
                feature.SmoothRTPeaks = GetRollingAveragePeaks(feature.RawRTPeaks, 9);
            }
            CheckPeakRiseSmooth(extractedFeatures);
            List <Feature> finalFeatures = GetFinalFeatures(extractedFeatures);

            extractedFeatures.Clear();
            CheckPeakRiseSmooth(finalFeatures);
            List <FeatureGroup> groups = GroupFeatures(finalFeatures, .04, 2).ToList();

            DoubleBack(groups, finalFeatures);
            finalFeatures.Clear();
            List <FeatureGroup> allSubGroups = new List <FeatureGroup>();

            foreach (FeatureGroup group in groups)
            {
                GetMainSubgroup(group);
                GetFeatureSubGroups(group);
                if (group.mainSubGroup.allFeatures.Count > 1)
                {
                    allSubGroups.Add(group.mainSubGroup);
                }
                foreach (FeatureGroup subGroup in group.SubGroups)
                {
                    allSubGroups.Add(subGroup);
                }
            }
            this.finalGroups = GetFinalFeatures(allSubGroups);
            foreach (FeatureGroup group in this.finalGroups)
            {
                group.finalPeaks = GetPeaksFromNearestSpectrum(group, rawFile);
            }
            foreach (FeatureGroup group in this.finalGroups)
            {
                EISpectrum spec = new EISpectrum();
                spec.ApexTimeEI   = group.ApexTime;
                spec.FeatureGroup = group;
                spec.FinalEIPeaks = group.finalPeaks;
                spec.FinalEIPeaks = spec.FinalEIPeaks.OrderByDescending(x => x.Intensity).ToList();
                if (group.finalPeaks.Count > 6) //here as well (changed to remove num feature restriction)
                {
                    spec.BasePeak = spec.FinalEIPeaks[0];
                    this.eiSpectra.Add(spec);
                }
                spec.FinalNormalizedEIPeaks.AddRange(spec.FinalEIPeaks);
                spec.FinalNormalizedEIPeaks = spec.FinalNormalizedEIPeaks.OrderBy(x => x.MZ).ToList();
                norm.CombineLikeMZPeaks(spec.FinalNormalizedEIPeaks);
                spec.AdjustedPeaks.AddRange(spec.FinalNormalizedEIPeaks);
                norm.GetAdjustedPeaks(spec.AdjustedPeaks);
                spec.DenominatorTerm = norm.GetDemoninatorTerm(spec.AdjustedPeaks);
            }

            OnFinish();
        }