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); }
public static void GetMetricsData(this MetricsData metricsData, ScanMetaDataCollection metaData, RawDataCollection rawData, IRawDataPlus rawFile, QuantDataCollection quantData = null) { List <Operations> operations = new List <Operations> { Operations.ScanIndex, Operations.RetentionTimes, Operations.MethodData, Operations.MetaData }; if (!rawData.isBoxCar) { operations.Add(Operations.PeakRetAndInt); operations.Add(Operations.PeakShape); } rawData.Check(rawFile, operations); metricsData.RawFileName = rawData.rawFileName; metricsData.Instrument = rawData.instrument; metricsData.MS1Analyzer = rawData.methodData.MassAnalyzers[MSOrderType.Ms]; metricsData.MS2Analyzer = rawData.methodData.MassAnalyzers[MSOrderType.Ms2]; metricsData.TotalAnalysisTime = rawData.retentionTimes[rawData.scanIndex.ScanEnumerators[MSOrderType.Any].Last()] - rawData.retentionTimes[rawData.scanIndex.ScanEnumerators[MSOrderType.Any].First()]; metricsData.TotalScans = rawData.scanIndex.allScans.Count(); metricsData.MS1Scans = rawData.scanIndex.ScanEnumerators[MSOrderType.Ms].Length; metricsData.MS2Scans = rawData.scanIndex.ScanEnumerators[MSOrderType.Ms2].Length; if (rawData.methodData.AnalysisOrder == MSOrderType.Ms3) { metricsData.MS3Analyzer = rawData.methodData.MassAnalyzers[MSOrderType.Ms3]; metricsData.MS3Scans = rawData.scanIndex.ScanEnumerators[MSOrderType.Ms3].Length; } else { metricsData.MS3Analyzer = MassAnalyzerType.Any; metricsData.MS3Scans = 0; } metricsData.MSOrder = rawData.methodData.AnalysisOrder; List <double> ms2intensities = new List <double>(); List <double> precursorIntensities = new List <double>(); List <double> ms1fillTimes = new List <double>(); List <double> ms2fillTimes = new List <double>(); List <double> ms3fillTimes = new List <double>(); List <double> ms2scansPerCycle = new List <double>(); List <double> dutyCycles = new List <double>(); List <double> fractionConsuming80 = new List <double>(); foreach (int scan in rawData.scanIndex.ScanEnumerators[MSOrderType.Ms]) { ms1fillTimes.Add(metaData[scan].FillTime); ms2scansPerCycle.Add(metaData[scan].MS2ScansPerCycle); dutyCycles.Add(metaData[scan].DutyCycle); } foreach (int scan in rawData.scanIndex.ScanEnumerators[MSOrderType.Ms2]) { precursorIntensities.Add(rawData.peakData[scan].ParentIntensity); ms2intensities.Add(metaData[scan].SummedIntensity); ms2fillTimes.Add(metaData[scan].FillTime); fractionConsuming80.Add(metaData[scan].FractionConsumingTop80PercentTotalIntensity); } if (rawData.methodData.AnalysisOrder == MSOrderType.Ms3) { foreach (int scan in rawData.scanIndex.ScanEnumerators[MSOrderType.Ms3]) { ms3fillTimes.Add(metaData[scan].FillTime); } } metricsData.MedianPrecursorIntensity = precursorIntensities.ToArray().Percentile(50); metricsData.MedianMs2FractionConsumingTop80PercentTotalIntensity = fractionConsuming80.ToArray().Percentile(50); metricsData.MedianSummedMS2Intensity = ms2intensities.ToArray().Percentile(50); metricsData.MedianMS1FillTime = ms1fillTimes.ToArray().Percentile(50); metricsData.MedianMS2FillTime = ms2fillTimes.ToArray().Percentile(50); if (rawData.methodData.AnalysisOrder == MSOrderType.Ms3) { metricsData.MedianMS3FillTime = ms3fillTimes.ToArray().Percentile(50); } else { metricsData.MedianMS3FillTime = -1; } metricsData.MeanTopN = ms2scansPerCycle.Average(); metricsData.MeanDutyCycle = dutyCycles.Average(); metricsData.MS1ScanRate = metricsData.MS1Scans / metricsData.TotalAnalysisTime; metricsData.MS2ScanRate = metricsData.MS2Scans / metricsData.TotalAnalysisTime; metricsData.MS3ScanRate = metricsData.MS3Scans / metricsData.TotalAnalysisTime; // only do the following if it isn't a boxcar experiment if (!rawData.isBoxCar) { metricsData.MedianBaselinePeakWidth = rawData.peakData.PeakShapeMedians.Width.P10; metricsData.MedianHalfHeightPeakWidth = rawData.peakData.PeakShapeMedians.Width.P50; // we can't access the instrument method in Linux, so we will assume the gradient length is the length of the MS acquisition metricsData.Gradient = rawData.retentionTimes[rawData.scanIndex.allScans.Keys.Max()]; metricsData.PeakCapacity = metricsData.Gradient / metricsData.MedianHalfHeightPeakWidth; metricsData.MedianAsymmetryFactor = rawData.peakData.PeakShapeMedians.Asymmetry.P10; } // add isolation interference metricsData.MedianMs1IsolationInterference = (from scan in rawData.scanIndex.ScanEnumerators[rawData.methodData.AnalysisOrder] select rawData.metaData[scan].Ms1IsolationInterference).ToArray().Percentile(50); // now add the quant meta data, if quant was performed double medianReporterIntensity = 0; QuantMetaData quantMetaData = new QuantMetaData(); SerializableDictionary <string, double> medianReporterIntensityByChannel = new SerializableDictionary <string, double>(); if (quantData != null & rawData.Performed.Contains(Operations.Quantification)) { string reagent = quantData.LabelingReagents; string[] allTags = new LabelingReagents().Reagents[reagent].Labels; List <double> allChannels = new List <double>(); Dictionary <string, List <double> > byChannel = new Dictionary <string, List <double> >(); foreach (string tag in allTags) { byChannel.Add(tag, new List <double>()); } foreach (int scan in rawData.scanIndex.ScanEnumerators[rawData.methodData.AnalysisOrder]) { foreach (string tag in allTags) { byChannel[tag].Add(quantData[scan][tag].Intensity); allChannels.Add(quantData[scan][tag].Intensity); } } medianReporterIntensity = allChannels.ToArray().Percentile(50); foreach (string tag in allTags) { medianReporterIntensityByChannel[tag] = byChannel[tag].ToArray().Percentile(50); } quantMetaData.medianReporterIntensity = medianReporterIntensity; quantMetaData.medianReporterIntensityByChannel = medianReporterIntensityByChannel; quantMetaData.quantTags = allTags; metricsData.QuantMeta = quantMetaData; metricsData.IncludesQuant = true; } }
public static void AggregateMetaData(this ScanMetaDataCollection metaData, RawDataCollection rawData, IRawDataPlus rawFile) { List <Operations> operations = new List <Operations>() { Operations.ScanIndex, Operations.MethodData, Operations.RetentionTimes, Operations.TrailerExtras, Operations.PrecursorScans }; if (rawData.methodData.MassAnalyzers[MSOrderType.Ms] == MassAnalyzerType.MassAnalyzerFTMS) { operations.Add(Operations.Ms1CentroidStreams); } else { operations.Add(Operations.Ms1SegmentedScans); } if (rawData.methodData.MassAnalyzers[MSOrderType.Ms2] == MassAnalyzerType.MassAnalyzerFTMS) { operations.Add(Operations.Ms2CentroidStreams); } else { operations.Add(Operations.Ms2SegmentedScans); } if (rawData.methodData.AnalysisOrder == MSOrderType.Ms3) { if (rawData.methodData.MassAnalyzers[MSOrderType.Ms3] == MassAnalyzerType.MassAnalyzerFTMS) { operations.Add(Operations.Ms3CentroidStreams); } else { operations.Add(Operations.Ms3SegmentedScans); } } rawData.Check(rawFile, operations); ProgressIndicator progress = new ProgressIndicator(rawData.scanIndex.ScanEnumerators[MSOrderType.Any].Count(), "Formatting scan meta data"); // add a new ScanMetaData class for each scan foreach (int scan in rawData.scanIndex.ScanEnumerators[MSOrderType.Any]) { metaData.Add(scan, new ScanMetaData()); } // get isolation window double isoWindow; if (rawData.methodData.AnalysisOrder == MSOrderType.Ms2) { isoWindow = rawData.methodData.IsolationWindow.MS2; } else { isoWindow = rawData.methodData.IsolationWindow.MS3.MS1Window; } // get topN foreach (int scan in rawData.scanIndex.ScanEnumerators[MSOrderType.Ms]) { // if the ms1 scan has no scan dependents then topN = 0 if (rawFile.GetScanDependents(scan, 0) == null) { metaData[scan].MS2ScansPerCycle = 0; } else { metaData[scan].MS2ScansPerCycle = rawFile.GetScanDependents(scan, 0).ScanDependentDetailArray.Length; } } // go through scans for each ms order sequentially foreach (MSOrderType MSOrder in new List <MSOrderType> { MSOrderType.Ms, MSOrderType.Ms2, MSOrderType.Ms3 }) { int[] scans = rawData.scanIndex.ScanEnumerators[MSOrder]; for (int i = 0; i < scans.Length; i++) { metaData[scans[i]].FillTime = rawData.trailerExtras[scans[i]].InjectionTime; // populate duty cycle if (i < scans.Length - 1) { metaData[scans[i]].DutyCycle = (rawData.retentionTimes[scans[i + 1]] - rawData.retentionTimes[scans[i]]) * 60; } else { metaData[scans[i]].DutyCycle = 0; } // populate scan rate if (MSOrder == MSOrderType.Ms2 | MSOrder == MSOrderType.Ms3) { metaData[scans[i]].MS2ScansPerCycle = metaData[rawData.precursorScans[scans[i]].MasterScan].MS2ScansPerCycle; } // populate intensity distributions if (rawData.methodData.MassAnalyzers[MSOrder] == MassAnalyzerType.MassAnalyzerFTMS) { metaData[scans[i]].IntensityDistribution = new Distribution(rawData.centroidStreams[scans[i]].Intensities); metaData[scans[i]].SummedIntensity = rawData.centroidStreams[scans[i]].Intensities.Sum(); } else { metaData[scans[i]].IntensityDistribution = new Distribution(rawData.segmentedScans[scans[i]].Intensities); metaData[scans[i]].SummedIntensity = rawData.segmentedScans[scans[i]].Intensities.Sum(); } // populate fraction of scans consuming 80% of total intensity if (rawData.methodData.MassAnalyzers[MSOrder] == MassAnalyzerType.MassAnalyzerFTMS) { metaData[scans[i]].FractionConsumingTop80PercentTotalIntensity = rawData.centroidStreams[scans[i]].Intensities.FractionOfScansConsumingTotalIntensity(percent: 80); } else { metaData[scans[i]].FractionConsumingTop80PercentTotalIntensity = rawData.segmentedScans[scans[i]].Intensities.FractionOfScansConsumingTotalIntensity(percent: 80); } // calculate ms1 isolation interference if (rawData.methodData.AnalysisOrder == MSOrder) { int preScan = rawData.precursorScans[scans[i]].MasterScan; metaData[scans[i]].Ms1IsolationInterference = Ms1Interference.CalculateForOneScan(rawData.centroidStreams[preScan], rawData.precursorMasses[scans[i]].MonoisotopicMZ, isoWindow, rawData.trailerExtras[scans[i]].ChargeState); } progress.Update(); } } progress.Done(); rawData.Performed.Add(Operations.MetaData); }
public static void WriteMatrix(RawDataCollection rawData, ScanMetaDataCollection metaData, IRawDataPlus rawFile, QuantDataCollection quantData = null, string outputDirectory = null) { string fileName = ReadWrite.GetPathToFile(outputDirectory, rawData.rawFileName, "_Matrix.txt"); CheckIfDone.Check(rawData, rawFile, new List <Operations> { Operations.ScanIndex, Operations.MethodData, Operations.PrecursorScans, Operations.RetentionTimes, Operations.PrecursorMasses, Operations.TrailerExtras, Operations.MetaData }); using (StreamWriter f = new StreamWriter(fileName)) //Open a new file { List <int> scans; if (!rawData.isBoxCar) { scans = rawData.scanIndex.ScanEnumerators[rawData.scanIndex.AnalysisOrder].ToList(); } else { scans = rawData.precursorScans.Keys.ToList(); scans.Sort(); } ProgressIndicator progress = new ProgressIndicator(scans.Count(), "Writing matrix to disk"); f.Write("MS3ScanNumber\tMS2ScanNumber\tMS1ScanNumber\tQuantScanRetTime\tParentScanRetTime\tDutyCycle" + "\tMS2ScansPerCycle\tParentIonMass\tMonoisotopicMass\tPrecursorCharge\tMS1IsolationInterference"); if (!rawData.isBoxCar) { f.Write("\tParentPeakFound"); } if (rawData.Performed.Contains(Operations.PeakArea) & !rawData.isBoxCar) { f.Write("\tParentPeakArea"); } if (!rawData.isBoxCar) { f.Write("\tPeakFirstScan\tPeakMaxScan\tPeakLastScan\tBaseLinePeakWidth(s)\tPeakParentScanIntensity\tPeakMaxIntensity"); } f.Write("\tMS1IonInjectionTime\tMS2IonInjectionTime" + "\tMS3IonInjectionTime\tHCDEnergy\tMS1MedianIntensity\tMS2MedianIntensity\t"); if (quantData != null) { string reagents = quantData.LabelingReagents; foreach (string label in new LabelingReagents().Reagents[reagents].Labels) { f.Write(label + "Intensity\t"); } foreach (string label in new LabelingReagents().Reagents[reagents].Labels) { f.Write(label + "Mass\t"); } foreach (string label in new LabelingReagents().Reagents[reagents].Labels) { f.Write(label + "Noise\t"); } foreach (string label in new LabelingReagents().Reagents[reagents].Labels) { f.Write(label + "Resolution\t"); } foreach (string label in new LabelingReagents().Reagents[reagents].Labels) { f.Write(label + "Baseline\t"); } } f.Write("\n"); foreach (int scan in scans) { int ms3scan, ms2scan, masterScan; if (rawData.scanIndex.AnalysisOrder == MSOrderType.Ms3) { ms3scan = rawData.precursorScans[scan].MS3Scan; ms2scan = rawData.precursorScans[scan].MS2Scan; masterScan = rawData.precursorScans[scan].MasterScan; } else { ms3scan = -1; ms2scan = rawData.precursorScans[scan].MS2Scan; masterScan = rawData.precursorScans[scan].MasterScan; } f.Write(ms3scan.ToString() + "\t" + ms2scan.ToString() + "\t" + masterScan.ToString() + "\t"); f.Write(rawData.retentionTimes[scan].ToString() + "\t" + rawData.retentionTimes[masterScan].ToString() + "\t"); f.Write(metaData[masterScan].DutyCycle.ToString() + "\t" + metaData[masterScan].MS2ScansPerCycle.ToString() + "\t"); f.Write(rawData.precursorMasses[ms2scan].ParentMZ.ToString() + "\t"); f.Write(rawData.precursorMasses[ms2scan].MonoisotopicMZ.ToString() + "\t"); f.Write(rawData.trailerExtras[ms2scan].ChargeState.ToString() + "\t"); f.Write(rawData.metaData[scan].Ms1IsolationInterference.ToString() + "\t"); if (!rawData.isBoxCar) { f.Write(rawData.peakData[ms2scan].PeakFound.ToString() + "\t"); } if (rawData.Performed.Contains(Operations.PeakArea) & !rawData.isBoxCar) { f.Write(rawData.peakData[ms2scan].Area.ToString() + "\t"); } if (!rawData.isBoxCar) { f.Write(rawData.peakData[ms2scan].FirstScan.ToString() + "\t"); f.Write(rawData.peakData[ms2scan].MaxScan.ToString() + "\t"); f.Write(rawData.peakData[ms2scan].LastScan.ToString() + "\t"); f.Write((rawData.peakData[ms2scan].BaselineWidth * 60).ToString() + "\t"); f.Write(rawData.peakData[ms2scan].ParentIntensity.ToString() + "\t"); f.Write(rawData.peakData[ms2scan].MaximumIntensity.ToString() + "\t"); } f.Write(rawData.trailerExtras[masterScan].InjectionTime.ToString() + "\t"); if (rawData.scanIndex.AnalysisOrder == MSOrderType.Ms3) { f.Write(rawData.trailerExtras[ms2scan].InjectionTime.ToString() + "\t"); f.Write(rawData.trailerExtras[ms3scan].InjectionTime.ToString() + "\t"); } else { f.Write(rawData.trailerExtras[ms2scan].InjectionTime.ToString() + "\t"); f.Write("-1\t"); } f.Write(rawData.trailerExtras[scan].HCDEnergy + "\t"); f.Write(metaData[masterScan].IntensityDistribution.P50 + "\t"); f.Write(metaData[ms2scan].IntensityDistribution.P50 + "\t"); if (quantData != null) { foreach (string label in quantData[scan].Keys) { f.Write(quantData[scan][label].Intensity + "\t"); } foreach (string label in quantData[scan].Keys) { f.Write(quantData[scan][label].Mass + "\t"); } foreach (string label in quantData[scan].Keys) { f.Write(quantData[scan][label].Noise + "\t"); } foreach (string label in quantData[scan].Keys) { f.Write(quantData[scan][label].Resolution + "\t"); } foreach (string label in quantData[scan].Keys) { f.Write(quantData[scan][label].Baseline + "\t"); } } f.Write("\n"); progress.Update(); } progress.Done(); } }