public void Load(string rawFilepath, int minimumAssumedPrecursorChargeState, int maximumAssumedPrecursorChargeState, double absoluteThreshold, double relativeThresholdPercent, int maximumNumberOfPeaks, bool assignChargeStates, int maximumThreads) { OnReportTaskWithoutProgress(new EventArgs()); IXRawfile2 raw = (IXRawfile2) new MSFileReader_XRawfile(); raw.Open(rawFilepath); raw.SetCurrentController(0, 1); Dictionary <int, double[, ]> ms1s; if (GET_PRECURSOR_MZ_AND_INTENSITY_FROM_MS1) { ms1s = new Dictionary <int, double[, ]>(); } int first_scan_number = -1; raw.GetFirstSpectrumNumber(ref first_scan_number); int last_scan_number = -1; raw.GetLastSpectrumNumber(ref last_scan_number); OnReportTaskWithProgress(new EventArgs()); object progress_lock = new object(); int spectra_processed = 0; int old_progress = 0; ParallelOptions parallel_options = new ParallelOptions(); parallel_options.MaxDegreeOfParallelism = maximumThreads; Parallel.For(first_scan_number, last_scan_number + 1, scan_number => { string scan_filter = null; raw.GetFilterForScanNum(scan_number, ref scan_filter); if (!scan_filter.Contains(" ms ")) { string spectrum_id = "controllerType=0 controllerNumber=1 scan=" + scan_number.ToString(); double retention_time_minutes = double.NaN; raw.RTFromScanNum(scan_number, ref retention_time_minutes); int polarity = DeterminePolarity(scan_filter); string fragmentation_method = GetFragmentationMethod(scan_filter); double precursor_mz; double precursor_intensity; GetPrecursor(ms1s, raw, scan_number, scan_filter, first_scan_number, out precursor_mz, out precursor_intensity); int charge = DeterminePrecursorCharge(raw, scan_number); if (polarity < 0) { charge = -charge; } double[,] label_data = GetFragmentationData(raw, scan_number, scan_filter); List <MSPeak> peaks = new List <MSPeak>(label_data.GetLength(1)); for (int peak_index = label_data.GetLowerBound(1); peak_index <= label_data.GetUpperBound(1); peak_index++) { peaks.Add(new MSPeak(label_data[(int)RawLabelDataColumn.MZ, peak_index], label_data[(int)RawLabelDataColumn.Intensity, peak_index], assignChargeStates && (int)RawLabelDataColumn.Charge < label_data.GetLength(0) ? (int)label_data[(int)RawLabelDataColumn.Charge, peak_index] : 0, polarity)); } peaks = FilterPeaks(peaks, absoluteThreshold, relativeThresholdPercent, maximumNumberOfPeaks); if (peaks.Count > 0) { for (int c = (ALWAYS_USE_PRECURSOR_CHARGE_STATE_RANGE || charge == 0 ? minimumAssumedPrecursorChargeState : charge); c <= (ALWAYS_USE_PRECURSOR_CHARGE_STATE_RANGE || charge == 0 ? maximumAssumedPrecursorChargeState : charge); c++) { double precursor_mass = MSPeak.MassFromMZ(precursor_mz, c); TandemMassSpectrum spectrum = new TandemMassSpectrum(rawFilepath, scan_number, spectrum_id, null, retention_time_minutes, fragmentation_method, precursor_mz, precursor_intensity, c, precursor_mass, peaks); lock (this) { Add(spectrum); } } } } lock (progress_lock) { spectra_processed++; int new_progress = (int)((double)spectra_processed / (last_scan_number - first_scan_number + 1) * 100); if (new_progress > old_progress) { OnUpdateProgress(new ProgressEventArgs(new_progress)); old_progress = new_progress; } } }); raw.Close(); }
public void Load(string mzmlFilepath, int minimumAssumedPrecursorChargeState, int maximumAssumedPrecursorChargeState, double absoluteThreshold, double relativeThresholdPercent, int maximumNumberOfPeaks, bool assignChargeStates, bool deisotope, MassTolerance isotopicMZTolerance, int maximumThreads) { OnReportTaskWithoutProgress(EventArgs.Empty); XmlDocument mzML_temp = new XmlDocument(); mzML_temp.Load(mzmlFilepath); XPathNavigator mzML = mzML_temp.CreateNavigator(); XmlNamespaceManager xnm = new XmlNamespaceManager(mzML.NameTable); xnm.AddNamespace("mzML", mzML_temp.DocumentElement.NamespaceURI); Dictionary<string, XPathNodeIterator> referenceable_param_groups = new Dictionary<string, XPathNodeIterator>(); foreach(XPathNavigator referenceable_param_group in mzML.Select("//mzML:mzML/mzML:referenceableParamGroupList/mzML:referenceableParamGroup", xnm)) { referenceable_param_groups.Add(referenceable_param_group.GetAttribute("id", string.Empty), referenceable_param_group.SelectChildren(XPathNodeType.All)); } ParallelOptions parallel_options = new ParallelOptions(); parallel_options.MaxDegreeOfParallelism = maximumThreads; Dictionary<string, SpectrumNavigator> ms1s = null; if(GET_PRECURSOR_MZ_AND_INTENSITY_FROM_MS1) { ms1s = new Dictionary<string, SpectrumNavigator>(); #if NON_MULTITHREADED foreach(XPathNavigator spectrum_navigator in mzML.Select("//mzML:mzML/mzML:run/mzML:spectrumList/mzML:spectrum", xnm).Cast<XPathNavigator>()) #else Parallel.ForEach(mzML.Select("//mzML:mzML/mzML:run/mzML:spectrumList/mzML:spectrum", xnm).Cast<XPathNavigator>(), parallel_options, spectrum_navigator => #endif { string scan_id = spectrum_navigator.GetAttribute("id", string.Empty); int ms_level = -1; foreach(XPathNavigator spectrum_child_navigator in spectrum_navigator.SelectChildren(XPathNodeType.All)) { if(spectrum_child_navigator.Name.Equals("cvParam", StringComparison.OrdinalIgnoreCase)) { if(spectrum_child_navigator.GetAttribute("name", string.Empty).Equals("ms level", StringComparison.OrdinalIgnoreCase)) { ms_level = int.Parse(spectrum_child_navigator.GetAttribute("value", string.Empty)); } } else if(spectrum_child_navigator.Name.Equals("referenceableParamGroupRef", StringComparison.OrdinalIgnoreCase)) { foreach(XPathNavigator navigator in referenceable_param_groups[spectrum_child_navigator.GetAttribute("ref", string.Empty)]) { if(navigator.Name.Equals("cvParam", StringComparison.OrdinalIgnoreCase)) { if(navigator.GetAttribute("name", string.Empty).Equals("ms level", StringComparison.OrdinalIgnoreCase)) { ms_level = int.Parse(navigator.GetAttribute("value", string.Empty)); break; } } } } } if(ms_level == 1) { lock(ms1s) { ms1s.Add(scan_id, new SpectrumNavigator(spectrum_navigator, xnm)); } } } #if !NON_MULTITHREADED ); #endif } int num_spectra = int.Parse(mzML.SelectSingleNode("//mzML:mzML/mzML:run/mzML:spectrumList", xnm).GetAttribute("count", string.Empty)); OnReportTaskWithProgress(EventArgs.Empty); object progress_lock = new object(); int spectra_processed = 0; int old_progress = 0; #if NON_MULTITHREADED foreach(XPathNavigator spectrum_navigator in mzML.Select("//mzML:mzML/mzML:run/mzML:spectrumList/mzML:spectrum", xnm).Cast<XPathNavigator>()) #else Parallel.ForEach(mzML.Select("//mzML:mzML/mzML:run/mzML:spectrumList/mzML:spectrum", xnm).Cast<XPathNavigator>(), parallel_options, spectrum_navigator => #endif { int spectrum_index = int.Parse(spectrum_navigator.GetAttribute("index", string.Empty)); int spectrum_number = spectrum_index + 1; string spectrum_id = spectrum_navigator.GetAttribute("id", string.Empty); string spectrum_title = null; int ms_level = -1; int polarity = 0; double retention_time_minutes = double.NaN; string precursor_scan_id = null; double precursor_mz = double.NaN; int charge = 0; double precursor_intensity = double.NaN; string fragmentation_method = "collision-induced dissociation"; double[] mz = null; double[] intensity = null; foreach(XPathNavigator spectrum_child_navigator in spectrum_navigator.SelectChildren(XPathNodeType.All)) { if(spectrum_child_navigator.Name.Equals("cvParam", StringComparison.OrdinalIgnoreCase)) { if(spectrum_child_navigator.GetAttribute("name", string.Empty).Equals("ms level", StringComparison.OrdinalIgnoreCase)) { ms_level = int.Parse(spectrum_child_navigator.GetAttribute("value", string.Empty)); } else if(spectrum_child_navigator.GetAttribute("name", string.Empty).Equals("positive scan", StringComparison.OrdinalIgnoreCase)) { polarity = 1; } else if(spectrum_child_navigator.GetAttribute("name", string.Empty).Equals("negative scan", StringComparison.OrdinalIgnoreCase)) { polarity = -1; } else if(spectrum_child_navigator.GetAttribute("name", string.Empty).Equals("spectrum title", StringComparison.OrdinalIgnoreCase)) { spectrum_title = spectrum_child_navigator.GetAttribute("value", string.Empty); } } else if(spectrum_child_navigator.Name.Equals("referenceableParamGroupRef", StringComparison.OrdinalIgnoreCase)) { foreach(XPathNavigator navigator in referenceable_param_groups[spectrum_child_navigator.GetAttribute("ref", string.Empty)]) { if(navigator.Name.Equals("cvParam", StringComparison.OrdinalIgnoreCase)) { if(navigator.GetAttribute("name", string.Empty).Equals("ms level", StringComparison.OrdinalIgnoreCase)) { ms_level = int.Parse(navigator.GetAttribute("value", string.Empty)); } else if(navigator.GetAttribute("name", string.Empty).Equals("positive scan", StringComparison.OrdinalIgnoreCase)) { polarity = 1; } else if(navigator.GetAttribute("name", string.Empty).Equals("negative scan", StringComparison.OrdinalIgnoreCase)) { polarity = -1; } } } } else if(spectrum_child_navigator.Name.Equals("scanList", StringComparison.OrdinalIgnoreCase)) { foreach(XPathNavigator navigator in spectrum_child_navigator.Select("mzML:scan/mzML:cvParam", xnm)) { if(navigator.GetAttribute("name", string.Empty).Equals("scan start time", StringComparison.OrdinalIgnoreCase)) { retention_time_minutes = double.Parse(navigator.GetAttribute("value", string.Empty), CultureInfo.InvariantCulture); if(navigator.GetAttribute("unitName", string.Empty).StartsWith("s", StringComparison.OrdinalIgnoreCase)) { retention_time_minutes = TimeSpan.FromSeconds(retention_time_minutes).TotalMinutes; } } } } else if(spectrum_child_navigator.Name.Equals("precursorList", StringComparison.OrdinalIgnoreCase)) { XPathNavigator precursor_node = spectrum_child_navigator.SelectSingleNode("mzML:precursor", xnm); precursor_scan_id = precursor_node.GetAttribute("spectrumRef", string.Empty); foreach(XPathNavigator navigator in precursor_node.Select("mzML:selectedIonList/mzML:selectedIon/mzML:cvParam", xnm)) { if(navigator.GetAttribute("name", string.Empty).Equals("selected ion m/z", StringComparison.OrdinalIgnoreCase)) { precursor_mz = double.Parse(navigator.GetAttribute("value", string.Empty), CultureInfo.InvariantCulture); } else if(navigator.GetAttribute("name", string.Empty).Equals("charge state", StringComparison.OrdinalIgnoreCase)) { charge = int.Parse(navigator.GetAttribute("value", string.Empty)); if(polarity < 0) { charge = -charge; } } else if(navigator.GetAttribute("name", string.Empty).Equals("peak intensity", StringComparison.OrdinalIgnoreCase)) { precursor_intensity = double.Parse(navigator.GetAttribute("value", string.Empty), CultureInfo.InvariantCulture); } } XPathNavigator navigator2 = spectrum_child_navigator.SelectSingleNode("mzML:precursor/mzML:activation/mzML:cvParam", xnm); if(navigator2 != null) { fragmentation_method = navigator2.GetAttribute("name", string.Empty); } } else if(spectrum_child_navigator.Name.Equals("binaryDataArrayList", StringComparison.OrdinalIgnoreCase)) { ReadDataFromSpectrumNavigator(spectrum_child_navigator.Select("mzML:binaryDataArray/*", xnm), out mz, out intensity); } if(ms_level == 1) { break; } } if(ms_level >= 2) { if(GET_PRECURSOR_MZ_AND_INTENSITY_FROM_MS1 && precursor_scan_id != null) { SpectrumNavigator ms1; if(ms1s.TryGetValue(precursor_scan_id, out ms1)) { double[] ms1_mz; double[] ms1_intensity; ms1.GetSpectrum(out ms1_mz, out ms1_intensity); int index = -1; for(int i = ms1_mz.GetLowerBound(0); i <= ms1_mz.GetUpperBound(0); i++) { if(index < 0 || Math.Abs(ms1_mz[i] - precursor_mz) < Math.Abs(ms1_mz[index] - precursor_mz)) { index = i; } } precursor_mz = ms1_mz[index]; precursor_intensity = ms1_intensity[index]; } } if(mz != null && intensity != null && mz.Length > 0 && intensity.Length > 0) { List<MSPeak> peaks = new List<MSPeak>(mz.Length); for(int i = 0; i < mz.Length; i++) { peaks.Add(new MSPeak(mz[i], intensity[i], 0, polarity)); } peaks = FilterPeaks(peaks, absoluteThreshold, relativeThresholdPercent, maximumNumberOfPeaks); if(peaks.Count > 0) { peaks.Sort(MSPeak.AscendingMZComparison); for(int c = (ALWAYS_USE_PRECURSOR_CHARGE_STATE_RANGE || charge == 0 ? minimumAssumedPrecursorChargeState : charge); c <= (ALWAYS_USE_PRECURSOR_CHARGE_STATE_RANGE || charge == 0 ? maximumAssumedPrecursorChargeState : charge); c++) { List<MSPeak> new_peaks = peaks; if(assignChargeStates) { new_peaks = AssignChargeStates(new_peaks, c, polarity, isotopicMZTolerance); if(deisotope) { new_peaks = Deisotope(new_peaks, c, polarity, isotopicMZTolerance); } } double precursor_mass = MSPeak.MassFromMZ(precursor_mz, c); TandemMassSpectrum spectrum = new TandemMassSpectrum(mzmlFilepath, spectrum_number, spectrum_id, spectrum_title, retention_time_minutes, fragmentation_method, precursor_mz, precursor_intensity, c, precursor_mass, new_peaks); lock(this) { Add(spectrum); } } } } } lock(progress_lock) { spectra_processed++; int new_progress = (int)((double)spectra_processed / num_spectra * 100); if(new_progress > old_progress) { OnUpdateProgress(new ProgressEventArgs(new_progress)); old_progress = new_progress; } } } #if !NON_MULTITHREADED ); #endif }
public void Load(string agilentDFolderPath, int minimumAssumedPrecursorChargeState, int maximumAssumedPrecursorChargeState, double absoluteThreshold, double relativeThresholdPercent, int maximumNumberOfPeaks, bool assignChargeStates, bool deisotope, int maximumThreads) { OnReportTaskWithoutProgress(new EventArgs()); IMsdrDataReader agilent_d = new MassSpecDataReader(); agilent_d.OpenDataFile(agilentDFolderPath); Dictionary <int, IBDASpecData> ms1s; if (GET_PRECURSOR_MZ_AND_INTENSITY_FROM_MS1) { ms1s = new Dictionary <int, IBDASpecData>(); } IMsdrPeakFilter ms1_peak_filter = new MsdrPeakFilter(); IMsdrPeakFilter ms2_peak_filter = new MsdrPeakFilter(); ms2_peak_filter.AbsoluteThreshold = absoluteThreshold; ms2_peak_filter.RelativeThreshold = relativeThresholdPercent; ms2_peak_filter.MaxNumPeaks = maximumNumberOfPeaks; ChargeStateAssignmentWrapper csaw = new ChargeStateAssignmentWrapper(); OnReportTaskWithProgress(new EventArgs()); object progress_lock = new object(); int spectra_processed = 0; int old_progress = 0; ParallelOptions parallel_options = new ParallelOptions(); parallel_options.MaxDegreeOfParallelism = maximumThreads; Parallel.For(0, (int)agilent_d.MSScanFileInformation.TotalScansPresent, parallel_options, row_number => { IMSScanRecord scan_record; lock (agilent_d) { scan_record = agilent_d.GetScanRecord(row_number); } if (scan_record.MSLevel == MSLevel.MSMS) { IBDASpecData agilent_spectrum; lock (agilent_d) { agilent_spectrum = agilent_d.GetSpectrum(row_number, ms1_peak_filter, ms2_peak_filter); } if (agilent_spectrum.TotalDataPoints > 0) { int polarity = 0; if (agilent_spectrum.IonPolarity == IonPolarity.Positive) { polarity = 1; } else if (agilent_spectrum.IonPolarity == IonPolarity.Negative) { polarity = -1; } int spectrum_number = row_number + 1; string scan_id = "scanId=" + agilent_spectrum.ScanId.ToString(); double precursor_mz = agilent_spectrum.MZOfInterest[0].Start; double precursor_intensity; if (GET_PRECURSOR_MZ_AND_INTENSITY_FROM_MS1) { GetAccurateMZAndIntensity(ms1s, agilent_d, agilent_spectrum.ParentScanId, ref precursor_mz, out precursor_intensity); } else { agilent_spectrum.GetPrecursorIntensity(out precursor_intensity); } int charge; agilent_spectrum.GetPrecursorCharge(out charge); if (polarity < 0) { charge = -charge; } List <MSPeak> peaks = null; if (!assignChargeStates) { peaks = new List <MSPeak>(agilent_spectrum.TotalDataPoints); for (int i = 0; i < agilent_spectrum.TotalDataPoints; i++) { peaks.Add(new MSPeak(agilent_spectrum.XArray[i], agilent_spectrum.YArray[i], 0, polarity)); } } for (int c = (ALWAYS_USE_PRECURSOR_CHARGE_STATE_RANGE || charge == 0 ? minimumAssumedPrecursorChargeState : charge); c <= (ALWAYS_USE_PRECURSOR_CHARGE_STATE_RANGE || charge == 0 ? maximumAssumedPrecursorChargeState : charge); c++) { if (assignChargeStates) { int[] peak_ids = new int[agilent_spectrum.TotalDataPoints]; double[] peak_mzs = new double[agilent_spectrum.TotalDataPoints]; double[] peak_intensities = new double[agilent_spectrum.TotalDataPoints]; for (int i = 0; i < agilent_spectrum.TotalDataPoints; i++) { peak_ids[i] = i; peak_mzs[i] = agilent_spectrum.XArray[i]; peak_intensities[i] = agilent_spectrum.YArray[i]; } int[] peak_charge_states = new int[agilent_spectrum.TotalDataPoints]; int[] peak_clusters = new int[agilent_spectrum.TotalDataPoints]; int num_peaks; lock (csaw) { csaw.SetParameters(IsotopeModel.Peptidic, 1, (short)c, false, ACCURACY_C0, ACCURACY_C1); num_peaks = csaw.AssignChargeStates(peak_ids, peak_mzs, peak_intensities, peak_charge_states, peak_clusters); } peaks = new List <MSPeak>(num_peaks); HashSet <int> observed_peak_clusters = new HashSet <int>(); for (int i = 0; i < num_peaks; i++) { bool isotopic_peak = observed_peak_clusters.Contains(peak_clusters[i]); if (!deisotope || !isotopic_peak) { peaks.Add(new MSPeak(peak_mzs[i], peak_intensities[i], peak_charge_states[i], polarity)); if (peak_clusters[i] > 0 && !isotopic_peak) { observed_peak_clusters.Add(peak_clusters[i]); } } } } double precursor_mass = MSPeak.MassFromMZ(precursor_mz, c); TandemMassSpectrum spectrum = new TandemMassSpectrum(agilentDFolderPath, spectrum_number, scan_id, null, scan_record.RetentionTime, FRAGMENTATION_METHOD, precursor_mz, precursor_intensity, c, precursor_mass, peaks); lock (this) { Add(spectrum); } } } } lock (progress_lock) { spectra_processed++; int new_progress = (int)((double)spectra_processed / agilent_d.MSScanFileInformation.TotalScansPresent * 100); if (new_progress > old_progress) { OnUpdateProgress(new ProgressEventArgs(new_progress)); old_progress = new_progress; } } }); agilent_d.CloseDataFile(); }