Esempio n. 1
0
 /// <summary>
 /// Show some information in the RAW data.
 /// </summary>
 /// <param name="rawFile">the raw file reader object</param>
 private static void showRawDataInfo(IRawDataPlus rawFile)
 {
     Log.Info("RAW file info:");
     Log.Info("  * Path             : " + rawFile.FileName);
     Log.Info("  * Raw File Version : " + rawFile.FileHeader.Revision);
     Log.Info("  * Create Time      : " + rawFile.FileHeader.CreationDate);
     Log.Info("  * Instrument       : " + rawFile.GetInstrumentData().Name);
     Log.Info("  * Serial Number    : " + rawFile.GetInstrumentData().SerialNumber);
     Log.Info("  * Samlpe Vial      : " + rawFile.SampleInformation.Vial);
     Log.Info("  * Time Range       : " + $"{rawFile.RunHeaderEx.StartTime:F2} - {rawFile.RunHeaderEx.EndTime:F2}");
     Log.Info("  * Scan Number      : " + $"{rawFile.RunHeaderEx.FirstSpectrum} - {rawFile.RunHeaderEx.LastSpectrum}");
 }
Esempio n. 2
0
        /// <summary>
        /// Write the RAW file metadata to file.
        /// <param name="rawFile">the RAW file object</param>
        /// <param name="firstScanNumber">the first scan number</param>
        /// <param name="lastScanNumber">the last scan number</param>
        /// </summary>
        public void WriteMetada(IRawDataPlus rawFile, int firstScanNumber, int lastScanNumber)
        {
            // Get the start and end time from the RAW file
            var startTime = rawFile.RunHeaderEx.StartTime;
            var endTime   = rawFile.RunHeaderEx.EndTime;

            // Collect the metadata
            var output = new List <string>
            {
                "#General information",
                "RAW file path=" + rawFile.FileName,
                "RAW file version=" + rawFile.FileHeader.Revision,
                "Creation date=" + rawFile.FileHeader.CreationDate,
                $"Created by=[MS, MS:1000529, created_by, {rawFile.FileHeader.WhoCreatedId}]",
                "Number of instruments=" + rawFile.InstrumentCount,
                "Description=" + rawFile.FileHeader.FileDescription,
                $"Instrument model=[MS, MS:1000494, Thermo Scientific instrument model, {rawFile.GetInstrumentData().Model}]",
                "Instrument name=" + rawFile.GetInstrumentData().Name,
                $"Instrument serial number=[MS, MS:1000529, instrument serial number, {rawFile.GetInstrumentData().SerialNumber}]",
                $"Software version=[NCIT, NCIT:C111093, Software Version, {rawFile.GetInstrumentData().SoftwareVersion}]",
                "Firmware version=" + rawFile.GetInstrumentData().HardwareVersion,
                "Units=" + rawFile.GetInstrumentData().Units,
                $"Mass resolution=[MS, MS:1000011, mass resolution, {rawFile.RunHeaderEx.MassResolution:F3}]",
                $"Number of scans={rawFile.RunHeaderEx.SpectraCount}",
                $"Scan range={firstScanNumber};{lastScanNumber}",
                $"Scan start time=[MS, MS:1000016, scan start time, {startTime:F2}]",
                $"Time range={startTime:F2};{endTime:F2}",
                $"Mass range={rawFile.RunHeaderEx.LowMass:F4};{rawFile.RunHeaderEx.HighMass:F4}",
                "",
                "#Sample information",
                "Sample name=" + rawFile.SampleInformation.SampleName,
                "Sample id=" + rawFile.SampleInformation.SampleId,
                "Sample type=" + rawFile.SampleInformation.SampleType,
                "Sample comment=" + rawFile.SampleInformation.Comment,
                "Sample vial=" + rawFile.SampleInformation.Vial,
                "Sample volume=" + rawFile.SampleInformation.SampleVolume,
                "Sample injection volume=" + rawFile.SampleInformation.InjectionVolume,
                "Sample row number=" + rawFile.SampleInformation.RowNumber,
                "Sample dilution factor=" + rawFile.SampleInformation.DilutionFactor
            };

            // Write the meta data to file
            File.WriteAllLines(_outputDirectory + "/" + _rawFileNameWithoutExtension + "-metadata.txt", output.ToArray());
        }
Esempio n. 3
0
        public ScanFileHeader GetHeader()
        {
            var header = new ScanFileHeader();

            header.StartTime              = (float)rawFile.RunHeaderEx.StartTime;
            header.EndTime                = (float)rawFile.RunHeaderEx.EndTime;
            header.ScanCount              = rawFile.RunHeaderEx.SpectraCount;
            header.InstrumentModel        = rawFile.GetInstrumentData().Model;
            header.InstrumentManufacturer = "ThermoFisher";
            return(header);
        }
Esempio n. 4
0
        /// <summary>
        /// Write the RAW file metadata to file.
        /// <param name="rawFile">the RAW file object</param>
        /// <param name="firstScanNumber">the first scan number</param>
        /// <param name="lastScanNumber">the last scan number</param>
        /// </summary>
        private void WriteMetada(IRawDataPlus rawFile, int firstScanNumber, int lastScanNumber)
        {
            // Get the start and end time from the RAW file
            double startTime = rawFile.RunHeaderEx.StartTime;
            double endTime   = rawFile.RunHeaderEx.EndTime;

            // Collect the metadata
            List <string> output = new List <string>
            {
                "RAW file=" + rawFile.FileName,
                "RAWfileversion=" + rawFile.FileHeader.Revision,
                "Creationdate=" + rawFile.FileHeader.CreationDate,
                "Operator=" + rawFile.FileHeader.WhoCreatedId,
                "Numberofinstruments=" + rawFile.InstrumentCount,
                "Description=" + rawFile.FileHeader.FileDescription,
                "Instrumentmode=" + rawFile.GetInstrumentData().Model,
                "Instrumentname=" + rawFile.GetInstrumentData().Name,
                "Serialnumber=" + rawFile.GetInstrumentData().SerialNumber,
                "Softwareversion=" + rawFile.GetInstrumentData().SoftwareVersion,
                "Firmwareversion=" + rawFile.GetInstrumentData().HardwareVersion,
                "Units=" + rawFile.GetInstrumentData().Units
            };

            output.Add($"Massresolution={rawFile.RunHeaderEx.MassResolution:F3}");
            output.Add($"Numberofscans={rawFile.RunHeaderEx.SpectraCount}");
            output.Add($"Scan range={firstScanNumber},{lastScanNumber}");
            output.Add($"Time range={startTime:F2},{endTime:F2}");
            output.Add($"Mass range={rawFile.RunHeaderEx.LowMass:F4},{rawFile.RunHeaderEx.HighMass:F4}");

            // Write the meta data to file
            File.WriteAllLines(outputDirectory + "/" + rawFileNameWithoutExtension + "_metadata", output.ToArray());
        }
        public Rawfile(string rawfile)
        {
            int deviceNumber = 1;

            _rawfile = rawfile;
            rawFile  = RawFileReaderAdapter.FileFactory(_rawfile);
            // https://github.com/cpanse/MsBackendRawFileReader/issues/12
            rawFile.IncludeReferenceAndExceptionData = false;

            rawFile.SelectInstrument(Device.MS, deviceNumber);

            dictInfo.Add("filename", rawFile.FileName);
            dictInfo.Add("creation date", rawFile.FileHeader.CreationDate.ToString());
            dictInfo.Add("first scan", this.GetFirstScanNumber().ToString());
            dictInfo.Add("last scan", this.GetLastScanNumber().ToString());
            dictInfo.Add("model", rawFile.GetInstrumentData().Model.ToString());
            dictInfo.Add("name", rawFile.GetInstrumentData().Name.ToString());
            dictInfo.Add("SerialNumber", rawFile.GetInstrumentData().SerialNumber.ToString());
            //dictInfo.Add("mass resolution", rawFile.RunHeaderEx.MassResolution.ToString());


            var trailerData = rawFile.GetTrailerExtraInformation(rawFile.RunHeaderEx.FirstSpectrum);

            foreach (int i in Enumerable.Range(1, trailerData.Labels.ToArray().Length))
            {
                try
                {
                    if ((trailerData.Labels[i] == "Master Scan Number:") ||
                        (trailerData.Labels[i] == "Master Scan Number") ||
                        (trailerData.Labels[i] == "Master Index:"))
                    {
                        this.masterScanIdx = i;
                    }
                }
                catch
                {
                }
            }
        }
Esempio n. 6
0
        public static MethodDataContainer MethodData(IRawDataPlus rawFile, ScanIndex index)
        {
            rawFile.SelectInstrument(Device.MS, 1);

            Log.Information("Extracting method/instrument information");

            MethodDataContainer methodData = new MethodDataContainer();

            methodData.CreationDate = rawFile.CreationDate;
            methodData.Instrument   = rawFile.GetInstrumentData().Name;

            methodData.AnalysisOrder = index.AnalysisOrder;
            int firstQuantScan = index.ScanEnumerators[index.AnalysisOrder][0];

            methodData.QuantAnalyzer = index.allScans[firstQuantScan].MassAnalyzer;

            int firstMs1Scan = index.ScanEnumerators[MSOrderType.Ms][0];

            methodData.MassAnalyzers.Add(MSOrderType.Ms, index.allScans[firstMs1Scan].MassAnalyzer);
            methodData.MSOrderEnumerator.Add(MSOrderType.Ms);

            if (methodData.AnalysisOrder == MSOrderType.Ms2)
            {
                int firstMs2Scan = index.ScanEnumerators[MSOrderType.Ms2][0];
                methodData.MassAnalyzers.Add(MSOrderType.Ms2, index.allScans[firstMs2Scan].MassAnalyzer);
                methodData.MSOrderEnumerator.Add(MSOrderType.Ms2);
            }
            else if (methodData.AnalysisOrder == MSOrderType.Ms3)
            {
                int firstMs2Scan = index.ScanEnumerators[MSOrderType.Ms2][0];
                methodData.MassAnalyzers.Add(MSOrderType.Ms2, index.allScans[firstMs2Scan].MassAnalyzer);
                methodData.MSOrderEnumerator.Add(MSOrderType.Ms2);
                int    n         = index.ScanEnumerators[MSOrderType.Ms2].Count();
                double ms2window = rawFile.GetScanEventForScanNumber(index.ScanEnumerators[MSOrderType.Ms2][n / 2]).GetIsolationWidth(0);

                int firstMs3Scan = index.ScanEnumerators[MSOrderType.Ms3][0];
                methodData.MassAnalyzers.Add(MSOrderType.Ms3, index.allScans[firstMs3Scan].MassAnalyzer);
                methodData.MSOrderEnumerator.Add(MSOrderType.Ms3);
                double ms3ms1window = rawFile.GetScanEventForScanNumber(firstMs3Scan).GetIsolationWidth(0);
                double ms3ms2window = rawFile.GetScanEventForScanNumber(firstMs3Scan).GetIsolationWidth(1);

                methodData.IsolationWindow = (MS2 : ms2window, (MS1Window : ms3ms1window, MS2Window : ms3ms2window));
            }
            else
            {
                Console.WriteLine("This raw file contains only MS1 scans.");
            }

            return(methodData);
        }
Esempio n. 7
0
        /*
         + "\t" + scanStatistics.TIC.ToString()
         + "\t" + scanStatistics.ScanType.ToString()
         + "\t" + scanStatistics.CycleNumber.ToString()
         + "\t" + scanStatistics.Frequency.ToString()
         + "\t" + scanStatistics.HighMass.ToString()
         + "\t" + scanFilter.IonizationMode.ToString()
         + "\t" + scanFilter.MSOrder.ToString()
         + "\t" + scanFilter.MassAnalyzer.ToString()
         + "\t" + scanFilter.Detector.ToString()
         + "\t" + scanFilter.Lock.ToString() + "\t");
         +
         +                  try
         +                  {
         +                      var reaction0 = scanEvent.GetReaction(0);
         +                      file.Write(reaction0.PrecursorMass
         + "\t" + reaction0.LastPrecursorMass
         + "\t" + reaction0.CollisionEnergy
         + "\t" + reaction0.IsolationWidth
         +                      );
         +                  }
         +                  catch
         +                  {
         +                      file.Write("NA\tNA\tNA\tNA");
         +                  }
         +
         +
         */

        /// <summary>
        /// write file header (metainfo) into R code
        /// </summary>
        public static void PrintHeaderAsRcode(this IRawDataPlus rawFile, string filename)
        {
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(filename))
            {
                file.WriteLine("#R\n\n");
                file.WriteLine("e$info <- list()\n");
                file.WriteLine("e$info$`RAW file` <- '" + Path.GetFileName(rawFile.FileName) + "'");
                file.WriteLine("e$info$`RAW file version` <- '" + rawFile.FileHeader.Revision + "'");
                file.WriteLine("e$info$`Creation date` <- '" + rawFile.FileHeader.CreationDate + "'");
                file.WriteLine("e$info$Operator <- '" + rawFile.FileHeader.WhoCreatedId + "'");
                file.WriteLine("e$info$`Number of instruments` <- {0}", rawFile.InstrumentCount);
                file.WriteLine("e$info$Description <- '" + rawFile.FileHeader.FileDescription + "'");
                file.WriteLine("e$info$`Instrument model` <- '{0}'", rawFile.GetInstrumentData().Model);
                file.WriteLine("e$info$`Instrument name` <- '{0}'", rawFile.GetInstrumentData().Name);
                file.WriteLine("e$info$`Instrument method` <- '" + rawFile.SampleInformation.InstrumentMethodFile.Replace("\\", "/") + "'");
                //file.WriteLine("e$info$`Instrument method` <- '{0}'", rawFile.GetAllInstrumentFriendlyNamesFromInstrumentMethod().Length);
                file.WriteLine("e$info$`Serial number` <- '{0}'", rawFile.GetInstrumentData().SerialNumber);
                file.WriteLine("e$info$`Software version` <- '{0}'", rawFile.GetInstrumentData().SoftwareVersion);
                file.WriteLine("e$info$`Firmware version` <- '{0}'", rawFile.GetInstrumentData().HardwareVersion);
                file.WriteLine("e$info$Units <- '{0}'", rawFile.GetInstrumentData().Units);
                file.WriteLine("e$info$`Mass resolution` <- '{0:F3}'", rawFile.RunHeaderEx.MassResolution);
                file.WriteLine("e$info$`Number of scans` <- {0}", rawFile.RunHeaderEx.SpectraCount);
                int firstScanNumber = rawFile.RunHeaderEx.FirstSpectrum;
                int lastScanNumber  = rawFile.RunHeaderEx.LastSpectrum;
                file.WriteLine("e$info$`Number of ms2 scans` <- {0}", Enumerable.Range(1, lastScanNumber - firstScanNumber).Count(x => rawFile.GetFilterForScanNumber(x).ToString().Contains("Full ms2")));
                file.WriteLine("e$info$`Scan range` <- c({0}, {1})", firstScanNumber, lastScanNumber);
                double startTime = rawFile.RunHeaderEx.StartTime;
                double endTime   = rawFile.RunHeaderEx.EndTime;
                file.WriteLine("e$info$`Time range` <- c({0:F2}, {1:F2})", startTime, endTime);
                file.WriteLine("e$info$`Mass range` <- c({0:F4}, {1:F4})", rawFile.RunHeaderEx.LowMass, rawFile.RunHeaderEx.HighMass);

                var firstFilter   = rawFile.GetFilterForScanNumber(firstScanNumber);
                var lastFilter    = rawFile.GetFilterForScanNumber(lastScanNumber);
                int numberFilters = rawFile.GetFilters().Count;
                file.WriteLine("e$info$`Scan filter (first scan)` <- '{0}'", firstFilter.ToString());
                file.WriteLine("e$info$`Scan filter (last scan)` <- '{0}'", lastFilter.ToString());
                file.WriteLine("e$info$`Total number of filters` <- '{0}'", numberFilters);

                file.WriteLine("e$info$`Sample name` <- '{0}' ", rawFile.SampleInformation.SampleName);
                file.WriteLine("e$info$`Sample id` <- '{0}' ", rawFile.SampleInformation.SampleId);
                file.WriteLine("e$info$`Sample type` <- '{0}' ", rawFile.SampleInformation.SampleType);
                file.WriteLine("e$info$`Sample comment` <- '{0}' ", rawFile.SampleInformation.Comment);
                file.WriteLine("e$info$`Sample vial` <- '{0}' ", rawFile.SampleInformation.Vial);
                file.WriteLine("e$info$`Sample volume` <- '{0}' ", rawFile.SampleInformation.SampleVolume);
                file.WriteLine("e$info$`Sample injection volume` <- '{0}' ", rawFile.SampleInformation.InjectionVolume);
                file.WriteLine("e$info$`Sample row number` <- '{0}' ", rawFile.SampleInformation.RowNumber);
                file.WriteLine("e$info$`Sample dilution factor` <- '{0}' ", rawFile.SampleInformation.DilutionFactor);
                file.WriteLine("e$info$`Sample barcode` <- '{0}' ", rawFile.SampleInformation.Barcode);

                file.WriteLine("e$info$`User text 0` <- '{0}' ", rawFile.SampleInformation.UserText[0]);
                file.WriteLine("e$info$`User text 1` <- '{0}' ", rawFile.SampleInformation.UserText[1]);
                file.WriteLine("e$info$`User text 2` <- '{0}' ", rawFile.SampleInformation.UserText[2]);
                file.WriteLine("e$info$`User text 3` <- '{0}' ", rawFile.SampleInformation.UserText[3]);
                file.WriteLine("e$info$`User text 4` <- '{0}' ", rawFile.SampleInformation.UserText[4]);
            }
        }
Esempio n. 8
0
        public RawDataCollection(IRawDataPlus rawFile)
        {
            rawFileName     = rawFile.FileName;
            instrument      = rawFile.GetInstrumentData().Name;
            isExactive      = instrument.ToLower().Contains("exactive");
            isBoxCar        = rawFile.GetScanEventForScanNumber(1).MassRangeCount > 1;
            centroidStreams = new Dictionary <int, CentroidStreamData>();
            segmentedScans  = new Dictionary <int, SegmentedScanData>();
            trailerExtras   = new Dictionary <int, TrailerExtraData>();
            precursorScans  = new Dictionary <int, PrecursorScanData>();
            precursorMasses = new Dictionary <int, PrecursorMassData>();
            retentionTimes  = new Dictionary <int, double>();
            methodData      = new Containers.MethodData();
            quantData       = new QuantDataCollection();
            metaData        = new ScanMetaDataCollection();
            peakData        = new PrecursorPeakDataCollection();

            this.ExtractScanIndex(rawFile);
            this.ExtractMethodData(rawFile);
        }
Esempio n. 9
0
        public static MethodDataContainer Ms1MethodData(IRawDataPlus rawFile, ScanIndex index)
        {
            rawFile.SelectInstrument(Device.MS, 1);

            Log.Information("Extracting method/instrument information");

            MethodDataContainer methodData = new MethodDataContainer();

            methodData.CreationDate = rawFile.CreationDate;
            methodData.Instrument   = rawFile.GetInstrumentData().Name;

            methodData.AnalysisOrder = index.AnalysisOrder;
            int firstQuantScan = index.ScanEnumerators[index.AnalysisOrder][0];

            methodData.QuantAnalyzer = index.allScans[firstQuantScan].MassAnalyzer;

            int firstMs1Scan = index.ScanEnumerators[MSOrderType.Ms][0];

            methodData.MassAnalyzers.Add(MSOrderType.Ms, index.allScans[firstMs1Scan].MassAnalyzer);

            methodData.MSOrderEnumerator.Add(MSOrderType.Ms);

            return(methodData);
        }
Esempio n. 10
0
        /// <summary>
        /// Populate the instrument configuration list
        /// </summary>
        /// <param name="mzMl"></param>
        private void ConstructInstrumentConfigurationList(mzMLType mzMl)
        {
            var instrumentData = _rawFile.GetInstrumentData();

            // Referenceable param group for common instrument properties
            mzMl.referenceableParamGroupList = new ReferenceableParamGroupListType
            {
                count = "1",
                referenceableParamGroup = new ReferenceableParamGroupType[1]
            };
            mzMl.referenceableParamGroupList.referenceableParamGroup[0] = new ReferenceableParamGroupType
            {
                id      = "commonInstrumentParams",
                cvParam = new CVParamType[2]
            };

            // Instrument model
            if (!OntologyMapping.InstrumentModels.TryGetValue(instrumentData.Name, out var instrumentModel))
            {
                instrumentModel = new CVParamType
                {
                    accession = "MS:1000483",
                    name      = "Thermo Fisher Scientific instrument model",
                    cvRef     = "MS",
                    value     = ""
                };
            }

            mzMl.referenceableParamGroupList.referenceableParamGroup[0].cvParam[0] = instrumentModel;

            // Instrument serial number
            mzMl.referenceableParamGroupList.referenceableParamGroup[0].cvParam[1] = new CVParamType
            {
                cvRef     = "MS",
                accession = "MS:1000529",
                name      = "instrument serial number",
                value     = instrumentData.SerialNumber
            };

            // Add a default analyzer if none were found
            if (_massAnalyzers.Count == 0)
            {
                _massAnalyzers.Add(MassAnalyzerType.Any, "IC1");
            }

            // Set the run default instrument configuration ref
            mzMl.run.defaultInstrumentConfigurationRef = "IC1";

            var instrumentConfigurationList = new InstrumentConfigurationListType
            {
                count = _massAnalyzers.Count.ToString(),
                instrumentConfiguration = new InstrumentConfigurationType[_massAnalyzers.Count]
            };

            // Make a new instrument configuration for each analyzer
            var massAnalyzerIndex = 0;

            foreach (var massAnalyzer in _massAnalyzers)
            {
                instrumentConfigurationList.instrumentConfiguration[massAnalyzerIndex] = new InstrumentConfigurationType
                {
                    id = massAnalyzer.Value,
                    referenceableParamGroupRef = new ReferenceableParamGroupRefType[1],
                    componentList = new ComponentListType(),
                    cvParam       = new CVParamType[3]
                };
                instrumentConfigurationList.instrumentConfiguration[massAnalyzerIndex].referenceableParamGroupRef[0] =
                    new ReferenceableParamGroupRefType
                {
                    @ref = "commonInstrumentParams"
                };
                instrumentConfigurationList.instrumentConfiguration[massAnalyzerIndex].componentList =
                    new ComponentListType
                {
                    count    = "3",
                    source   = new SourceComponentType[1],
                    analyzer = new AnalyzerComponentType[1],
                    detector = new DetectorComponentType[1]
                };

                // Instrument source
                if (_ionizationTypes.IsNullOrEmpty())
                {
                    _ionizationTypes.Add(IonizationModeType.Any,
                                         OntologyMapping.IonizationTypes[IonizationModeType.Any]);
                }

                instrumentConfigurationList.instrumentConfiguration[massAnalyzerIndex].componentList.source[0] =
                    new SourceComponentType
                {
                    order   = 1,
                    cvParam = new CVParamType[_ionizationTypes.Count]
                };

                var index = 0;
                // Ionization type
                foreach (var ionizationType in _ionizationTypes)
                {
                    instrumentConfigurationList.instrumentConfiguration[massAnalyzerIndex].componentList.source[0]
                    .cvParam[index] =
                        ionizationType.Value;
                    index++;
                }

                // Instrument analyzer
                // Mass analyzer type
                instrumentConfigurationList.instrumentConfiguration[massAnalyzerIndex].componentList.analyzer[0] =
                    new AnalyzerComponentType
                {
                    order   = index + 1,
                    cvParam = new CVParamType[1]
                };
                instrumentConfigurationList.instrumentConfiguration[massAnalyzerIndex].componentList.analyzer[0]
                .cvParam[0] =
                    OntologyMapping.MassAnalyzerTypes[massAnalyzer.Key];
                index++;

                // Instrument detector
                instrumentConfigurationList.instrumentConfiguration[massAnalyzerIndex].componentList.detector[0] =
                    new DetectorComponentType
                {
                    order   = index + 1,
                    cvParam = new CVParamType[1]
                };

                // Try to map the instrument to the detector
                var         detectorCvParams = OntologyMapping.InstrumentToDetectors[instrumentModel.accession];
                CVParamType detectorCvParam;
                if (massAnalyzerIndex < detectorCvParams.Count)
                {
                    detectorCvParam = detectorCvParams[massAnalyzerIndex];
                }
                else
                {
                    detectorCvParam = OntologyMapping.InstrumentToDetectors["MS:1000483"][0];
                }

                instrumentConfigurationList.instrumentConfiguration[massAnalyzerIndex].componentList.detector[0]
                .cvParam[0] = detectorCvParam;
                massAnalyzerIndex++;
            }

            mzMl.instrumentConfigurationList = instrumentConfigurationList;
        }
        /// <summary>
        /// Write the RAW file metadata to file.
        /// <param name="rawFile">the RAW file object</param>
        /// <param name="firstScanNumber">the first scan number</param>
        /// <param name="lastScanNumber">the last scan number</param>
        /// </summary>
        public void WriteJsonMetada(IRawDataPlus rawFile, int firstScanNumber, int lastScanNumber)
        {
            // Get the start and end time from the RAW file
            var startTime = rawFile.RunHeaderEx.StartTime;
            var endTime   = rawFile.RunHeaderEx.EndTime;

            var metadata = new Metadata();

            // File Properties
            metadata.addFileProperty(new CVTerm("NCIT:C47922", "NCIT", "Pathname", rawFile.FileName));
            metadata.addFileProperty(new CVTerm("NCIT:C25714", "NCIT", "Version",
                                                rawFile.FileHeader.Revision.ToString()));
            metadata.addFileProperty(new CVTerm("NCIT:C69199", "NCIT", "Content Creation Date",
                                                rawFile.FileHeader.CreationDate.ToString()));
            metadata.addFileProperty(new CVTerm("NCIT:C25365", "NCIT", "Description",
                                                rawFile.FileHeader.FileDescription));


            metadata.addScanSetting(new CVTerm("MS:1000016", "MS", "scan start time", startTime.ToString()));
            metadata.addScanSetting(new CVTerm("MS:1000011", "MS", "mass resolution",
                                               rawFile.RunHeaderEx.MassResolution.ToString()));
            metadata.addScanSetting(new CVTerm("UO:0000002", "MS", "mass unit",
                                               rawFile.GetInstrumentData().Units.ToString()));
            metadata.addScanSetting(new CVTerm("PRIDE:0000478", "PRIDE", "Number of scans",
                                               rawFile.RunHeaderEx.SpectraCount.ToString()));
            metadata.addScanSetting(new CVTerm("PRIDE:0000479", "PRIDE", "MS scan range",
                                               firstScanNumber + ":" + lastScanNumber));
            metadata.addScanSetting(new CVTerm("PRIDE:0000484", "PRIDE", "Retention time range",
                                               startTime + ":" + endTime));
            metadata.addScanSetting(new CVTerm("PRIDE:0000485", "PRIDE", "Mz range",
                                               rawFile.RunHeaderEx.LowMass + ":" + rawFile.RunHeaderEx.HighMass));

            metadata.addInstrumentProperty(new CVTerm("MS:1000494", "MS", "Thermo Scientific instrument model",
                                                      rawFile.GetInstrumentData().Model));
            metadata.addInstrumentProperty(new CVTerm("MS:1000496", "MS", "instrument attribute",
                                                      rawFile.GetInstrumentData().Name));
            metadata.addInstrumentProperty(new CVTerm("MS:1000529", "MS", "instrument serial number",
                                                      rawFile.GetInstrumentData().SerialNumber));

            var    msTypes   = new Dictionary <string, int>();
            double minTime   = 1000000000000000;
            double maxTime   = 0;
            double minMz     = 1000000000000000000;
            double maxMz     = 0;
            double minCharge = 100000000000000;
            double maxCharge = 0;

            ICollection <CVTerm> fragmentationType = new HashSet <CVTerm>(CVTerm.CvTermComparer);

            for (var scanNumber = firstScanNumber; scanNumber <= lastScanNumber; scanNumber++)
            {
                var time = rawFile.RetentionTimeFromScanNumber(scanNumber);

                // Get the scan filter for this scan number
                var scanFilter = rawFile.GetFilterForScanNumber(scanNumber);

                // Get the scan event for this scan number
                var scanEvent = rawFile.GetScanEventForScanNumber(scanNumber);

                // Only consider MS2 spectra
                if (msTypes.ContainsKey(scanFilter.MSOrder.ToString()))
                {
                    var value = msTypes[scanFilter.MSOrder.ToString()];
                    value = value + 1;
                    msTypes[scanFilter.MSOrder.ToString()] = value;
                }
                else
                {
                    msTypes.Add(scanFilter.MSOrder.ToString(), 1);
                }

                if (time > maxTime)
                {
                    maxTime = time;
                }
                if (time < minTime)
                {
                    minTime = time;
                }


                if (scanFilter.MSOrder == MSOrderType.Ms2)
                {
                    fragmentationType.Add(ParseActivationType(scanFilter.GetActivation(0)));

                    if (scanEvent.ScanData == ScanDataType.Centroid || (scanEvent.ScanData == ScanDataType.Profile))
                    {
                        try
                        {
                            var reaction      = scanEvent.GetReaction(0);
                            var precursorMass = reaction.PrecursorMass;
                            if (precursorMass > maxMz)
                            {
                                maxMz = precursorMass;
                            }
                            if (precursorMass < minMz)
                            {
                                minMz = precursorMass;
                            }
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            Log.Warn("No reaction found for scan " + scanNumber);
                        }

                        // trailer extra data list
                        var trailerData = rawFile.GetTrailerExtraInformation(scanNumber);
                        for (var i = 0; i < trailerData.Length; i++)
                        {
                            if (trailerData.Labels[i] == "Charge State:")
                            {
                                if (int.Parse(trailerData.Values[i]) > maxCharge)
                                {
                                    maxCharge = int.Parse(trailerData.Values[i]);
                                }

                                if (int.Parse(trailerData.Values[i]) < minCharge)
                                {
                                    minCharge = int.Parse(trailerData.Values[i]);
                                }
                            }
                        }
                    }
                }
            }

            if (minCharge == 100000000000000)
            {
                minCharge = 0;
            }

            foreach (KeyValuePair <string, int> entry in msTypes)
            {
                if (entry.Key.Equals(MSOrderType.Ms.ToString()))
                {
                    metadata.addMSData(new CVTerm("PRIDE:0000481", "PRIDE", "Number of MS1 spectra",
                                                  entry.Value.ToString()));
                }
                if (entry.Key.Equals(MSOrderType.Ms2.ToString()))
                {
                    metadata.addMSData(new CVTerm("PRIDE:0000482", "PRIDE", "Number of MS2 spectra",
                                                  entry.Value.ToString()));
                }
                if (entry.Key.Equals(MSOrderType.Ms3.ToString()))
                {
                    metadata.addMSData(new CVTerm("PRIDE:0000483", "PRIDE", "Number of MS3 spectra",
                                                  entry.Value.ToString()));
                }
            }

            metadata.addScanSetting(fragmentationType);

            metadata.addMSData(new CVTerm("PRIDE:0000472", "PRIDE", "MS min charge", minCharge.ToString()));
            metadata.addMSData(new CVTerm("PRIDE:0000473", "PRIDE", "MS max charge", maxCharge.ToString()));

            metadata.addMSData(new CVTerm("PRIDE:0000474", "PRIDE", "MS min RT", minTime.ToString()));
            metadata.addMSData(new CVTerm("PRIDE:0000475", "PRIDE", "MS max RT", maxTime.ToString()));

            metadata.addMSData(new CVTerm("PRIDE:0000476", "PRIDE", "MS min MZ", minMz.ToString()));
            metadata.addMSData(new CVTerm("PRIDE:0000477", "PRIDE", "MS max MZ", maxMz.ToString()));


            // Write the meta data to file
            var json = JsonConvert.SerializeObject(metadata);

            json.Replace("\r\n", "\n");

            string metadataOutputPath;

            if (_outputDirectory == null)
            {
                metadataOutputPath = _metadataFileName;
            }
            else
            {
                metadataOutputPath = _outputDirectory + "/" + _metadataFileName + "-metadata.json";
            }

            File.WriteAllText(metadataOutputPath, json);
        }
Esempio n. 12
0
        /// <summary>
        /// Write the RAW file metadata to file.
        /// <param name="rawFile">the RAW file object</param>
        /// <param name="firstScanNumber">the first scan number</param>
        /// <param name="lastScanNumber">the last scan number</param>
        /// </summary>
        public void WriteJsonMetada(IRawDataPlus rawFile, int firstScanNumber, int lastScanNumber)
        {
            // Get the start and end time from the RAW file
            var startTime = rawFile.RunHeaderEx.StartTime;
            var endTime   = rawFile.RunHeaderEx.EndTime;

            var metadata = new Metadata();

            /** File Properties **/
            metadata.addFileProperty("path", rawFile.FileName);
            metadata.addFileProperty("version", rawFile.FileHeader.Revision.ToString());
            metadata.addFileProperty("creation-date", rawFile.FileHeader.CreationDate.ToString());
            metadata.addFileProperty("number-instruments", rawFile.InstrumentCount.ToString());
            metadata.addFileProperty("description", rawFile.FileHeader.FileDescription);

            /** Sample Properties **/
            metadata.addSampleProperty("name", rawFile.SampleInformation.SampleName);
            metadata.addSampleProperty("id", rawFile.SampleInformation.SampleId);
            metadata.addSampleProperty("type", rawFile.SampleInformation.SampleType.ToString());
            metadata.addSampleProperty("comment", rawFile.SampleInformation.Comment);
            metadata.addSampleProperty("vial", rawFile.SampleInformation.Vial);
            metadata.addSampleProperty("volume", rawFile.SampleInformation.SampleVolume.ToString());
            metadata.addSampleProperty("injection-volume", rawFile.SampleInformation.InjectionVolume.ToString());
            metadata.addSampleProperty("row-number", rawFile.SampleInformation.RowNumber.ToString());
            metadata.addSampleProperty("dilution-factor", rawFile.SampleInformation.DilutionFactor.ToString());

            metadata.addScanSetting("start-time", new CVTerm("MS:1000016", "MS", "scan start time", startTime.ToString()));
            metadata.addScanSetting("resolution", new CVTerm("MS:1000011", "MS", "mass resolution", rawFile.RunHeaderEx.MassResolution.ToString()));
            metadata.addScanSetting("tolerance-unit", new CVTerm("UO:0000002", "MS", "mass unit", rawFile.GetInstrumentData().Units.ToString()));
            metadata.addScanSetting("number-scans", rawFile.RunHeaderEx.SpectraCount.ToString());
            metadata.addScanSetting("scan-range", firstScanNumber + ":" + lastScanNumber);
            metadata.addScanSetting("time-range", startTime + ":" + endTime);
            metadata.addScanSetting("mass-range", rawFile.RunHeaderEx.LowMass + ":" + rawFile.RunHeaderEx.HighMass);

            metadata.addInstrumentProperty("model", new CVTerm("MS:1000494", "MS", "Thermo Scientific instrument model", rawFile.GetInstrumentData().Model));
            metadata.addInstrumentProperty("name", new CVTerm("MS:1000496", "MS", "instrument attribute", rawFile.GetInstrumentData().Name));
            metadata.addInstrumentProperty("serial", new CVTerm("MS:1000529", "MS", "instrument serial number", rawFile.GetInstrumentData().SerialNumber));

            var    msTypes   = new Dictionary <string, int>();
            double minTime   = 1000000000000000;
            double maxTime   = 0;
            double minMz     = 1000000000000000000;
            double maxMz     = 0;
            double minCharge = 100000000000000;
            double maxCharge = 0;

            HashSet <CVTerm> fragmentationType = new HashSet <CVTerm>(CVTerm.CvTermComparer);

            for (var scanNumber = firstScanNumber; scanNumber <= lastScanNumber; scanNumber++)
            {
                var time = rawFile.RetentionTimeFromScanNumber(scanNumber);

                // Get the scan filter for this scan number
                var scanFilter = rawFile.GetFilterForScanNumber(scanNumber);

                // Get the scan event for this scan number
                var scanEvent = rawFile.GetScanEventForScanNumber(scanNumber);

                // Only consider MS2 spectra
                if (msTypes.ContainsKey(scanFilter.MSOrder.ToString()))
                {
                    var value = msTypes[scanFilter.MSOrder.ToString()];
                    value = value + 1;
                    msTypes[scanFilter.MSOrder.ToString()] = value;
                }
                else
                {
                    msTypes.Add(scanFilter.MSOrder.ToString(), 1);
                }

                if (time > maxTime)
                {
                    maxTime = time;
                }
                if (time < minTime)
                {
                    minTime = time;
                }


                if (scanFilter.MSOrder == MSOrderType.Ms2)
                {
                    fragmentationType.Add(parseActivationType(scanFilter.GetActivation(0)));

                    if (scanEvent.ScanData == ScanDataType.Centroid || (scanEvent.ScanData == ScanDataType.Profile))
                    {
                        try
                        {
                            var reaction      = scanEvent.GetReaction(0);
                            var precursorMass = reaction.PrecursorMass;
                            if (precursorMass > maxMz)
                            {
                                maxMz = precursorMass;
                            }
                            if (precursorMass < minMz)
                            {
                                minMz = precursorMass;
                            }
                        }
                        catch (ArgumentOutOfRangeException exception)
                        {
                            Log.Warn("No reaction found for scan " + scanNumber);
                        }

                        // trailer extra data list
                        var trailerData = rawFile.GetTrailerExtraInformation(scanNumber);
                        for (var i = 0; i < trailerData.Length; i++)
                        {
                            if (trailerData.Labels[i] == "Charge State:")
                            {
                                if (Int32.Parse(trailerData.Values[i]) > maxCharge)
                                {
                                    maxCharge = Int32.Parse(trailerData.Values[i]);
                                }

                                if (Int32.Parse(trailerData.Values[i]) < minCharge)
                                {
                                    maxCharge = Int32.Parse(trailerData.Values[i]);
                                }
                            }
                        }
                    }
                }
            }

            if (minCharge == 100000000000000)
            {
                minCharge = 0;
            }

            metadata.addMSData("ms-number", msTypes);
            metadata.addMSData("activation-ypes", fragmentationType);

            metadata.addMSData("min-charge", minCharge);
            metadata.addMSData("max-charge", maxCharge);

            metadata.addMSData("min-Time", minTime);
            metadata.addMSData("max-Time", maxTime);

            metadata.addMSData("min-Mz", minMz);
            metadata.addMSData("max-Mz", maxMz);


            // Write the meta data to file
            var json = JsonConvert.SerializeObject(metadata);

            json.Replace("\r\n", "\n");
            File.WriteAllText(_outputDirectory + "/" + _rawFileNameWithoutExtension + "-metadata.json", json);
        }
Esempio n. 13
0
        static int DoStuff(ArgumentParser.ParseOptions opts)
        {
            List <string> files = new List <string>();

            if (opts.InputFiles.Count() > 0) // did the user give us a list of files?
            {
                List <string> problems = new List <string>();
                files = opts.InputFiles.ToList();

                // check if the list provided contains only .raw files
                foreach (string file in files)
                {
                    if (!file.EndsWith(".raw", StringComparison.OrdinalIgnoreCase))
                    {
                        problems.Add(file);
                    }
                }

                if (problems.Count() == 1)
                {
                    Console.WriteLine("\nERROR: {0} does not appear to be a .raw file. Invoke '>RawTools --help' if you need help.", problems.ElementAt(0));
                    Log.Error("Invalid file provided: {0}", problems.ElementAt(0));

                    return(1);
                }

                if (problems.Count() > 1)
                {
                    Console.WriteLine("\nERROR: The following {0} files do not appear to be .raw files. Invoke '>RawTools --help' if you need help." +
                                      "\n\n{1}", problems.Count(), String.Join("\n", problems));
                    Log.Error("Invalid files provided: {0}", String.Join(" ", problems));
                    return(1);
                }

                Log.Information("Files to be processed, provided as list: {0}", String.Join(" ", files));
            }

            else // did the user give us a directory?
            {
                if (Directory.Exists(opts.InputDirectory))
                {
                    files = Directory.GetFiles(opts.InputDirectory, "*.*", SearchOption.TopDirectoryOnly)
                            .Where(s => s.EndsWith(".raw", StringComparison.OrdinalIgnoreCase)).ToList();
                }
                else
                {
                    Console.WriteLine("ERROR: The provided directory does not appear to be valid.");
                    Log.Error("Invalid directory provided: {0}", opts.InputDirectory);
                    return(1);
                }

                Log.Information("Files to be processed, provided as directory: {0}", String.Join(" ", files));
            }

            if (opts.Quant)
            {
                List <string> possible = new List <string>()
                {
                    "TMT0", "TMT2", "TMT6", "TMT10", "TMT11", "iTRAQ4", "iTRAQ8"
                };
                if (!possible.Contains(opts.LabelingReagents))
                {
                    Console.WriteLine("ERROR: For quantification, the labeling reagent must be one of {TMT0, TMT2, TMT6, TMT10, TMT11, iTRAQ4, iTRAQ8}");
                    Log.Error("Invalid labeling reagent provided: {0}", opts.LabelingReagents);
                    return(1);
                }
            }

            if (opts.Chromatogram != null)
            {
                List <string> possible = new List <string>()
                {
                    "1T", "2T", "3T", "1B", "2B", "3B", "1TB", "2TB", "3TB", "1TB", "2TB", "3TB"
                };
                if (!possible.Contains(opts.Chromatogram))
                {
                    Console.WriteLine("ERROR: Incorrect format for --chro. See help.");
                    Log.Error("Invalid chromatogram argument provided: {Chro}", opts.Chromatogram);
                    return(1);
                }
            }

            System.Diagnostics.Stopwatch singleFileTime = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch totalTime      = new System.Diagnostics.Stopwatch();
            totalTime.Start();

            foreach (string file in files)
            {
                singleFileTime.Start();

                Console.WriteLine("\nProcessing: {0}\n", file);

                using (IRawDataPlus rawFile = RawFileReaderFactory.ReadFile(fileName: file))
                {
                    rawFile.SelectInstrument(Device.MS, 1);

                    Log.Information("Now processing: {File} --- Instrument: {Instrument}", Path.GetFileName(file), rawFile.GetInstrumentData().Name);

                    RawDataCollection   rawData   = new RawDataCollection(rawFile: rawFile);
                    QuantDataCollection quantData = new QuantDataCollection();

                    bool isBoxCar = rawData.isBoxCar;

                    if (rawData.isBoxCar)
                    {
                        Console.WriteLine("\nRaw file appears to be a boxcar-type experiment. Precursor peak analysis won't be performed!\n");
                    }

                    if (opts.ParseData | opts.Metrics | opts.Quant)
                    {
                        rawData.ExtractAll(rawFile);

                        if (!isBoxCar)
                        {
                            rawData.CalcPeakRetTimesAndInts(rawFile: rawFile);
                        }
                    }

                    if (opts.Quant)
                    {
                        rawData.quantData.Quantify(rawData: rawData, rawFile: rawFile, labelingReagent: opts.LabelingReagents);
                    }

                    if (opts.UnlabeledQuant & !isBoxCar)
                    {
                        rawData.QuantifyPrecursorPeaks(rawFile);
                    }

                    if (opts.Metrics)
                    {
                        rawData.metaData.AggregateMetaData(rawData, rawFile);
                    }

                    if (opts.ParseData | opts.Quant)
                    {
                        if (opts.Quant)
                        {
                            Parse.WriteMatrix(rawData: rawData, rawFile: rawFile, metaData: rawData.metaData, quantData: rawData.quantData, outputDirectory: opts.OutputDirectory);
                        }
                        else
                        {
                            Parse.WriteMatrix(rawData: rawData, rawFile: rawFile, metaData: rawData.metaData, outputDirectory: opts.OutputDirectory);
                        }
                    }

                    if (opts.WriteMGF)
                    {
                        MGF.WriteMGF(rawData: rawData, rawFile: rawFile, outputDirectory: opts.OutputDirectory, cutoff: opts.MassCutOff,
                                     intensityCutoff: opts.IntensityCutoff);
                    }

                    if (opts.Metrics)
                    {
                        MetricsData metricsData = new MetricsData();

                        if (opts.Quant)
                        {
                            metricsData.GetMetricsData(metaData: rawData.metaData, rawData: rawData, rawFile: rawFile, quantData: rawData.quantData);
                        }
                        else
                        {
                            metricsData.GetMetricsData(metaData: rawData.metaData, rawData: rawData, rawFile: rawFile);
                        }

                        metricsData.GetMetricsData(metaData: rawData.metaData, rawData: rawData, rawFile: rawFile);
                        Metrics.WriteMatrix(rawData, metricsData, opts.OutputDirectory);
                    }

                    if (opts.Chromatogram != null)
                    {
                        int order = Convert.ToInt32((opts.Chromatogram.ElementAt(0).ToString()));

                        if (order > (int)rawData.methodData.AnalysisOrder)
                        {
                            Log.Error("Specified MS order ({Order}) for chromatogram is higher than experiment order ({ExpOrder})",
                                      (MSOrderType)order, rawData.methodData.AnalysisOrder);
                            Console.WriteLine("Specified MS order ({0}) for chromatogram is higher than experiment order ({1}). Chromatogram(s) won't be written.",
                                              (MSOrderType)order, rawData.methodData.AnalysisOrder);
                        }
                        else
                        {
                            rawData.WriteChromatogram(rawFile, (MSOrderType)order, opts.Chromatogram.Contains("T"), opts.Chromatogram.Contains("B"), opts.OutputDirectory);
                        }
                    }
                }

                singleFileTime.Stop();
                Console.WriteLine("\nElapsed time: {0} s", Math.Round(Convert.ToDouble(singleFileTime.ElapsedMilliseconds) / 1000.0, 2));
                singleFileTime.Reset();
            }
            totalTime.Stop();
            Console.WriteLine("\nTime to process all {0} files: {1}", files.Count(), totalTime.Elapsed);

            return(0);
        }