/// <summary> /// Reads a Spectrum from the DPBF file with isotope peaks populated /// </summary> /// <param name="fullData">The file used to create the DPBF file (throws <see cref="System.ArgumentException"/> if it is not) - PBF file preferred.</param> /// <param name="scanNum">The scan to read</param> /// <returns></returns> /// <exception cref="System.ArgumentException">If the checksum of the source file does not match the checksum stored in the DPBF file</exception> public DeconvolutedSpectrum GetSpectrumWithIsotopePeaks(IMassSpecDataReader fullData, int scanNum) { if (SrcFileChecksum != fullData.SrcFileChecksum || (fullData is PbfLcMsRun && SrcFileChecksum != ((PbfLcMsRun)fullData).PbfFileChecksum)) { throw new ArgumentException("Supplied file was not used to create this DPBF file!", nameof(fullData)); } if (!(GetSpectrum(scanNum, true) is DeconvolutedSpectrum spec)) { return(null); } var pbfSpec = fullData.ReadMassSpectrum(scanNum, true); foreach (var peakBase in spec.Peaks) { var peak = peakBase as DeconvolutedPeak; if (peak == null) { continue; } peak.SetObservedPeaksFromSpectrum(pbfSpec); } return(spec); }
public static string ConvertToPbf(string specFilePath, IMassSpecDataReader specReader, double precursorSignalToNoiseRatioThreshold, double productSignalToNoiseRatioThreshold, string pbfFilePath = null, IProgress <ProgressData> progress = null) { return(PbfLcMsRun.ConvertToPbf(specFilePath, MassSpecDataReaderFactory.GetMassSpecDataReader(specFilePath), precursorSignalToNoiseRatioThreshold, productSignalToNoiseRatioThreshold, pbfFilePath, progress)); }
public DiaLcMsRun(IMassSpecDataReader massSpecDataReader, double precursorSignalToNoiseRatioThreshold, double productSignalToNoiseRatioThreshold) : base(massSpecDataReader, precursorSignalToNoiseRatioThreshold, productSignalToNoiseRatioThreshold) { _isolationWindows = new SortedSet <IsolationWindow>(); _isoWindowToChromPeaks = new Dictionary <IsolationWindow, List <LcMsPeak> >(); var variableWindows = false; var upper = -1.0; var lower = -1.0; for (var scanNum = MinLcScan; scanNum <= MaxLcScan; scanNum++) { var productSpec = GetSpectrum(scanNum) as ProductSpectrum; if (productSpec == null) { continue; } var isoWindow = productSpec.IsolationWindow; if (isoWindow == null) { continue; } if (_isolationWindows.Add(isoWindow)) { // new isolation window if (!variableWindows) { if (upper > 0 && Math.Abs(upper - isoWindow.IsolationWindowUpperOffset) > 0.001) { variableWindows = true; } if (lower > 0 && Math.Abs(lower - isoWindow.IsolationWindowLowerOffset) > 0.001) { variableWindows = true; } if (upper < 0) { upper = isoWindow.IsolationWindowUpperOffset; } if (lower < 0) { lower = isoWindow.IsolationWindowLowerOffset; } } _isoWindowToChromPeaks.Add(isoWindow, new List <LcMsPeak>()); } var peakList = _isoWindowToChromPeaks[isoWindow]; peakList.AddRange(productSpec.Peaks.Select(peak => new LcMsPeak(peak.Mz, peak.Intensity, scanNum))); } foreach (var peakList in _isoWindowToChromPeaks.Values) { peakList.Sort(); } _variableWindows = variableWindows; _isolationWindowLowerOffset = lower; _isolationWindowUpperOffset = upper; }
/// <summary> /// Constructor for creating and/or opening a DPBF file /// </summary> /// <param name="specFileName"></param> /// <param name="msdr"></param> /// <param name="pbfFileName"></param> /// <param name="precursorSignalToNoiseRatioThreshold"></param> /// <param name="productSignalToNoiseRatioThreshold"></param> /// <param name="progress"></param> /// <param name="keepDataReaderOpen"></param> public DPbfLcMsRun(string specFileName, IMassSpecDataReader msdr, string pbfFileName = null, double precursorSignalToNoiseRatioThreshold = 0.0, double productSignalToNoiseRatioThreshold = 0.0, IProgress <ProgressData> progress = null, bool keepDataReaderOpen = false) : base(precursorSignalToNoiseRatioThreshold, productSignalToNoiseRatioThreshold) { GetPbfFile(specFileName, msdr, pbfFileName, progress, keepDataReaderOpen); }
/// <summary> /// Gets the appropriate IMassSpecDataReader for the supplied path. /// It is recommended that "NormalizeDatasetPath" be called prior to calling this function, and that the returned string be used instead of the original path /// </summary> /// <param name="filePath"></param> /// <returns></returns> /// <remarks>It is recommended that "NormalizeDatasetPath" be called prior to calling this function, and that the returned string be used instead of the original path</remarks> public static IMassSpecDataReader GetMassSpecDataReader(string filePath) { filePath = NormalizeDatasetPath(filePath); var type = GetMassSpecDataType(filePath); IMassSpecDataReader reader = null; switch (type) { case MassSpecDataType.XCaliburRun: reader = new XCaliburReader(filePath); break; case MassSpecDataType.MzMLFile: reader = new MzMLReader(filePath); break; case MassSpecDataType.PbfFile: reader = new PbfLcMsRun(filePath); break; case MassSpecDataType.Unknown: if (_pwizAvailable) { reader = new ProteoWizardReader(filePath); } break; } return(reader); }
/// <summary> /// Initializes a new instance of the <see cref="LcmsRunDeconvoluter" /> class. /// This constructor creates an instance with a single MSLevel /// </summary> /// <param name="dataReader">MassSpec data reader to read raw spectra from.</param> /// <param name="deconvoluter">Spectrum deconvoluter.</param> /// <param name="msLevel">The MS level (ex MS1, MS2, etc) to extract spectra for.</param> /// <param name="maxDegreeOfParallelism">The maximum number of threads the deconvoluter can use.</param> public LcmsRunDeconvoluter( IMassSpecDataReader dataReader, Deconvoluter deconvoluter, int msLevel, int maxDegreeOfParallelism = 1) : this(dataReader, deconvoluter, new HashSet <int> { msLevel }, maxDegreeOfParallelism) { }
/// <summary> /// Initializes a new instance of the <see cref="LcmsRunDeconvoluter" /> class. /// This constructor creates an instance with multiple MSLevels for default (MS1 and MS2). /// </summary> /// <param name="dataReader">MassSpec data reader to read raw spectra from.</param> /// <param name="deconvoluter">Spectrum deconvoluter.</param> /// <param name="msLevels">The MS levels (ex MS1, MS2, etc) to extract spectra for.</param> /// <param name="maxDegreeOfParallelism">The maximum number of threads the deconvoluter can use.</param> public LcmsRunDeconvoluter( IMassSpecDataReader dataReader, Deconvoluter deconvoluter, IEnumerable <int> msLevels = null, int maxDegreeOfParallelism = 1) { this.deconvoluter = deconvoluter; this.dataReader = dataReader; this.maxDegreeOfParallelism = maxDegreeOfParallelism; this.msLevelSet = msLevels == null ? new HashSet <int> { 1, 2 } : new HashSet <int>(msLevels); }
/// <summary> /// /// </summary> /// <param name="specFilePath"></param> /// <param name="specReader"></param> /// <param name="precursorSignalToNoiseRatioThreshold"></param> /// <param name="productSignalToNoiseRatioThreshold"></param> /// <param name="progress"></param> /// <param name="scanStart"></param> /// <param name="scanEnd"></param> /// <returns></returns> /// <remarks>It is recommended that "MassSpecDataReaderFactory.NormalizeDatasetPath" be called prior to calling this function, and that the returned string be used instead of the original path</remarks> public static LcMsRun GetLcMsRun( string specFilePath, IMassSpecDataReader specReader, double precursorSignalToNoiseRatioThreshold, double productSignalToNoiseRatioThreshold, IProgress <ProgressData> progress = null, int scanStart = 0, int scanEnd = 0) { return(new InMemoryLcMsRun(new PbfLcMsRun(specFilePath, specReader, "", precursorSignalToNoiseRatioThreshold, productSignalToNoiseRatioThreshold, progress), precursorSignalToNoiseRatioThreshold, productSignalToNoiseRatioThreshold, progress, scanStart, scanEnd)); }
public DiaLcMsRun(IMassSpecDataReader massSpecDataReader, double precursorSignalToNoiseRatioThreshold, double productSignalToNoiseRatioThreshold) : base(massSpecDataReader, precursorSignalToNoiseRatioThreshold, productSignalToNoiseRatioThreshold) { _isolationWindows = new SortedSet<IsolationWindow>(); _isoWindowToChromPeaks = new Dictionary<IsolationWindow, List<LcMsPeak>>(); var variableWindows = false; var upper = -1.0; var lower = -1.0; for (var scanNum = MinLcScan; scanNum <= MaxLcScan; scanNum++) { var productSpec = GetSpectrum(scanNum) as ProductSpectrum; if (productSpec == null) continue; var isoWindow = productSpec.IsolationWindow; if (isoWindow == null) continue; if (_isolationWindows.Add(isoWindow)) { // new isolation window if (!variableWindows) { if (upper > 0 && Math.Abs(upper - isoWindow.IsolationWindowUpperOffset) > 0.001) { variableWindows = true; } if (lower > 0 && Math.Abs(lower - isoWindow.IsolationWindowLowerOffset) > 0.001) { variableWindows = true; } if (upper < 0) upper = isoWindow.IsolationWindowUpperOffset; if (lower < 0) lower = isoWindow.IsolationWindowLowerOffset; } _isoWindowToChromPeaks.Add(isoWindow, new List<LcMsPeak>()); } var peakList = _isoWindowToChromPeaks[isoWindow]; peakList.AddRange(productSpec.Peaks.Select(peak => new LcMsPeak(peak.Mz, peak.Intensity, scanNum))); } foreach (var peakList in _isoWindowToChromPeaks.Values) { peakList.Sort(); } _variableWindows = variableWindows; _isolationWindowLowerOffset = lower; _isolationWindowUpperOffset = upper; }
/// <summary> /// Gets the appropriate IMassSpecDataReader for the supplied path. /// It is recommended that "NormalizeDatasetPath" be called prior to calling this function, and that the returned string be used instead of the original path /// </summary> /// <param name="filePath"></param> /// <returns></returns> /// <remarks>It is recommended that "NormalizeDatasetPath" be called prior to calling this function, and that the returned string be used instead of the original path</remarks> public static IMassSpecDataReader GetMassSpecDataReader(string filePath) { filePath = NormalizeDatasetPath(filePath); var type = GetMassSpecDataType(filePath); IMassSpecDataReader reader = null; switch (type) { case MassSpecDataType.XCaliburRun: reader = new XCaliburReader(filePath); break; case MassSpecDataType.MzMLFile: reader = new MzMLReader(filePath); break; case MassSpecDataType.PbfFile: reader = new PbfLcMsRun(filePath); break; case MassSpecDataType.DeconvolutedPbfFile: reader = new DPbfLcMsRun(filePath); break; case MassSpecDataType.Unknown: if (_pwizAvailable) { reader = new ProteoWizardReader(filePath); } else { ConsoleMsgUtils.ShowWarning(string.Format("WARNING: Could not find a reader for file \"{0}\"." + " Is MSFileReader and/or ProteoWizard installed?", filePath)); } break; } return(reader); }
public InMemoryLcMsRun( IMassSpecDataReader massSpecDataReader, double precursorSignalToNoiseRatioThreshold, double productSignalToNoiseRatioThreshold, IProgress <ProgressData> progress = null, int scanStart = 0, int scanEnd = 0) { ScanNumElutionTimeMap = new Dictionary <int, double>(); ScanNumToMsLevel = new Dictionary <int, int>(); IsolationMzBinToScanNums = new Dictionary <int, int[]>(); _ms1PeakList = new List <LcMsPeak>(); _scanNumSpecMap = new Dictionary <int, Spectrum>(); var isolationMzBinToScanNums = new Dictionary <int, List <int> >(); // Read all spectra var progressData = new ProgressData(progress) { Status = "Reading spectra from file" }; var trackingInfo = new SpectrumTrackingInfo { NumSpectra = massSpecDataReader.NumSpectra, PrecursorSignalToNoiseRatioThreshold = precursorSignalToNoiseRatioThreshold, ProductSignalToNoiseRatioThreshold = productSignalToNoiseRatioThreshold, SpecRead = 0, MinScanNum = int.MaxValue, MaxScanNum = int.MinValue, MinMsLevel = int.MaxValue, MaxMsLevel = int.MinValue }; NumSpectra = massSpecDataReader.NumSpectra; progressData.StepRange(95.0); if (scanStart > 0 && scanEnd >= scanStart) { for (var scanNum = scanStart; scanNum <= scanEnd; scanNum++) { var spec = massSpecDataReader.ReadMassSpectrum(scanNum); progressData.Report(trackingInfo.SpecRead, trackingInfo.NumSpectra); HandleSpectrum(ref trackingInfo, isolationMzBinToScanNums, spec); } } else { foreach (var spec in massSpecDataReader.ReadAllSpectra()) { progressData.Report(trackingInfo.SpecRead, trackingInfo.NumSpectra); HandleSpectrum(ref trackingInfo, isolationMzBinToScanNums, spec); } } progressData.Status = "Processing Isolation Bins"; progressData.IsPartialRange = false; progressData.Report(95.1); foreach (var entry in isolationMzBinToScanNums) { var binNum = entry.Key; entry.Value.Sort(); var scanNumList = entry.Value.ToArray(); IsolationMzBinToScanNums[binNum] = scanNumList; } _ms1PeakList.Sort(); //_ms2PeakList.Sort(); progressData.Report(99.5); // Read MS levels and precursor information MinLcScan = trackingInfo.MinScanNum; MaxLcScan = trackingInfo.MaxScanNum; MinMsLevel = trackingInfo.MinMsLevel; MaxMsLevel = trackingInfo.MaxMsLevel; //var precursorMap = new Dictionary<int, int>(); //var nextScanMap = new Dictionary<int, int>(); // //for (var msLevel = MinMsLevel; msLevel <= maxMsLevel; msLevel++) //{ // precursorMap[msLevel] = 0; // nextScanMap[msLevel] = MaxLcScan + 1; //} //progressData.Report(99.8); progressData.Report(100.0); CreatePrecursorNextScanMap(); }
public static int Main(string[] args) { string specFilePath; string outputDir; try { var handle = Process.GetCurrentProcess().MainWindowHandle; SetConsoleMode(handle, EnableExtendedFlags); if (args.Length == 0) { PrintUsageInfo(); return(-1); } var paramDic = new Dictionary <string, string> { { "-s", null }, { "-o", null } }; for (var i = 0; i < args.Length / 2; i++) { var key = args[2 * i]; var value = args[2 * i + 1]; if (!paramDic.ContainsKey(key)) { PrintUsageInfo("Invalid parameter: " + key); return(-1); } paramDic[key] = value; } // Parse command line parameters specFilePath = paramDic["-s"]; if (specFilePath == null) { PrintUsageInfo("Missing required parameter -s!"); return(-1); } // Check for folder-type datasets, and replace specFilePath with the directory name if it is. specFilePath = MassSpecDataReaderFactory.GetDatasetName(specFilePath); var isDirectoryDataset = MassSpecDataReaderFactory.IsADirectoryDataset(specFilePath); // True if specFilePath is a directory that is NOT a supported folder-type dataset. var specPathIsDirectory = Directory.Exists(specFilePath) && !isDirectoryDataset; if (!File.Exists(specFilePath) && !specPathIsDirectory && !isDirectoryDataset) { PrintUsageInfo("File not found: " + specFilePath); return(-1); } var types = MassSpecDataReaderFactory.MassSpecDataTypeFilterList; types.Remove(".pbf"); if (!specPathIsDirectory && !(types.Select(ext => specFilePath.ToLower().EndsWith(ext)).Any())) { PrintUsageInfo("Invalid file extension: (" + Path.GetExtension(specFilePath) + ") " + specFilePath); return(-1); } // Must use "Path.GetFullPath" to return the absolute path when the source file is in the working directory // But, it could cause problems with too-long paths. outputDir = paramDic["-o"] ?? (specPathIsDirectory ? specFilePath : Path.GetDirectoryName(Path.GetFullPath(specFilePath))); if (outputDir == null) { PrintUsageInfo("Invalid output file directory: " + specFilePath); return(-1); } if (!Directory.Exists(outputDir)) { if (File.Exists(outputDir) && !File.GetAttributes(outputDir).HasFlag(FileAttributes.Directory)) { PrintUsageInfo("OutputDir " + outputDir + " is not a directory!"); return(-1); } Directory.CreateDirectory(outputDir); } } catch (Exception ex) { Console.WriteLine("Exception while parsing the command line parameters: " + ex.Message); return(-5); } #if (!DEBUG) try { #endif string[] specFilePaths = new[] { specFilePath }; if (Directory.Exists(specFilePath) && !MassSpecDataReaderFactory.IsADirectoryDataset(specFilePath)) { specFilePaths = Directory.GetFiles(specFilePath, "*.raw"); } foreach (var rawFilePath in specFilePaths) { var pbfFilePath = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(rawFilePath) + PbfLcMsRun.FileExtension); bool isCurrent; if (File.Exists(pbfFilePath) && PbfLcMsRun.CheckFileFormatVersion(pbfFilePath, out isCurrent) && isCurrent) { Console.WriteLine("{0} already exists.", pbfFilePath); continue; } Console.WriteLine("Creating {0} from {1}", pbfFilePath, rawFilePath); IMassSpecDataReader reader = MassSpecDataReaderFactory.GetMassSpecDataReader(rawFilePath); var progress = new Progress <ProgressData>(p => { p.UpdateFrequencySeconds = 2; if ((p.Percent % 25).Equals(0) || p.ShouldUpdate()) { Console.Write("\r{0}, {1:00.0}% complete ", p.Status, p.Percent); } }); var run = new PbfLcMsRun(rawFilePath, reader, pbfFilePath, 0, 0, progress); Console.WriteLine(); } Console.WriteLine("PbfFormatVersion: {0}", PbfLcMsRun.FileFormatId); return(0); #if (!DEBUG) } catch (Exception ex) { // NOTE: The DMS Analysis Manager looks for this text; do not change it Console.WriteLine("Exception while processing: " + ex.Message); Console.WriteLine(ex.StackTrace); var errorCode = -Math.Abs(ex.Message.GetHashCode()); if (errorCode == 0) { return(-1); } else { return(errorCode); } } #endif }
/// <summary> /// /// </summary> /// <param name="specFilePath"></param> /// <param name="specReader"></param> /// <param name="precursorSignalToNoiseRatioThreshold"></param> /// <param name="productSignalToNoiseRatioThreshold"></param> /// <param name="progress"></param> /// <param name="scanStart"></param> /// <param name="scanEnd"></param> /// <returns></returns> /// <remarks>It is recommended that "MassSpecDataReaderFactory.NormalizeDatasetPath" be called prior to calling this function, and that the returned string be used instead of the original path</remarks> public static LcMsRun GetLcMsRun( string specFilePath, IMassSpecDataReader specReader, double precursorSignalToNoiseRatioThreshold, double productSignalToNoiseRatioThreshold, IProgress<ProgressData> progress = null, int scanStart = 0, int scanEnd = 0) { return new InMemoryLcMsRun(new PbfLcMsRun(specFilePath, specReader, "", precursorSignalToNoiseRatioThreshold, productSignalToNoiseRatioThreshold, progress), precursorSignalToNoiseRatioThreshold, productSignalToNoiseRatioThreshold, progress, scanStart, scanEnd); }
public InMemoryLcMsRun( IMassSpecDataReader massSpecDataReader, double precursorSignalToNoiseRatioThreshold, double productSignalToNoiseRatioThreshold, IProgress<ProgressData> progress = null, int scanStart = 0, int scanEnd = 0) { ScanNumElutionTimeMap = new Dictionary<int, double>(); ScanNumToMsLevel = new Dictionary<int, int>(); IsolationMzBinToScanNums = new Dictionary<int, int[]>(); _ms1PeakList = new List<LcMsPeak>(); _scanNumSpecMap = new Dictionary<int, Spectrum>(); var isolationMzBinToScanNums = new Dictionary<int, List<int>>(); // Read all spectra var progressData = new ProgressData(progress) { Status = "Reading spectra from file" }; var trackingInfo = new SpectrumTrackingInfo { NumSpectra = massSpecDataReader.NumSpectra, PrecursorSignalToNoiseRatioThreshold = precursorSignalToNoiseRatioThreshold, ProductSignalToNoiseRatioThreshold = productSignalToNoiseRatioThreshold, SpecRead = 0, MinScanNum = int.MaxValue, MaxScanNum = int.MinValue, MinMsLevel = int.MaxValue, MaxMsLevel = int.MinValue }; NumSpectra = massSpecDataReader.NumSpectra; progressData.StepRange(95.0); if (scanStart > 0 && scanEnd >= scanStart) { for (var scanNum = scanStart; scanNum <= scanEnd; scanNum++) { var spec = massSpecDataReader.ReadMassSpectrum(scanNum); progressData.Report(trackingInfo.SpecRead, trackingInfo.NumSpectra); HandleSpectrum(ref trackingInfo, isolationMzBinToScanNums, spec); } } else { foreach (var spec in massSpecDataReader.ReadAllSpectra()) { progressData.Report(trackingInfo.SpecRead, trackingInfo.NumSpectra); HandleSpectrum(ref trackingInfo, isolationMzBinToScanNums, spec); } } progressData.Status = "Processing Isolation Bins"; progressData.IsPartialRange = false; progressData.Report(95.1); foreach (var entry in isolationMzBinToScanNums) { var binNum = entry.Key; entry.Value.Sort(); var scanNumList = entry.Value.ToArray(); IsolationMzBinToScanNums[binNum] = scanNumList; } _ms1PeakList.Sort(); //_ms2PeakList.Sort(); progressData.Report(99.5); // Read MS levels and precursor information MinLcScan = trackingInfo.MinScanNum; MaxLcScan = trackingInfo.MaxScanNum; MinMsLevel = trackingInfo.MinMsLevel; MaxMsLevel = trackingInfo.MaxMsLevel; //var precursorMap = new Dictionary<int, int>(); //var nextScanMap = new Dictionary<int, int>(); // //for (var msLevel = MinMsLevel; msLevel <= maxMsLevel; msLevel++) //{ // precursorMap[msLevel] = 0; // nextScanMap[msLevel] = MaxLcScan + 1; //} //progressData.Report(99.8); progressData.Report(100.0); CreatePrecursorNextScanMap(); }
public static string ConvertToPbf(string specFilePath, IMassSpecDataReader specReader, double precursorSignalToNoiseRatioThreshold, double productSignalToNoiseRatioThreshold, string pbfFilePath = null, IProgress<ProgressData> progress = null) { return PbfLcMsRun.ConvertToPbf(specFilePath, MassSpecDataReaderFactory.GetMassSpecDataReader(specFilePath), precursorSignalToNoiseRatioThreshold, productSignalToNoiseRatioThreshold, pbfFilePath, progress); }