private static bool IsValidEnvelope(O18QuanEnvelope envelope, int charge) { if (envelope[0].Intensity == 0 || envelope[4].Intensity == 0) { return(false); } if (envelope[0].Charge == 0 && envelope[4].Charge == 0) { return(false); } if (envelope[0].Charge != 0 && envelope[0].Charge != charge) { return(false); } if (envelope[4].Charge != 0 && envelope[4].Charge != charge) { return(false); } return(true); }
public O18QuantificationSummaryItem ReadFromFile(string filename) { var result = new O18QuantificationSummaryItem(); var root = XElement.Load(filename); result.RawFilename = root.Element("RawFilename").Value; var ele = root.Element("SoftwareVersion"); if (ele != null) { result.SoftwareVersion = ele.Value; } result.PeptideSequence = root.Element("PeptideSequence").Value; result.AdditionalFormula = root.Element("AdditionalFormula").Value; result.PeptideAtomComposition = root.Element("PeptideAtomComposition").Value; result.PurityOfO18Water = MyConvert.ToDouble(root.Element("PurityOfO18Water").Value); result.IsPostDigestionLabelling = bool.Parse(root.Element("IsPostDigestionLabelling").Value); if (root.Element("TheoreticalO16Mz") != null) { result.TheoreticalO16Mz = MyConvert.ToDouble(root.Element("TheoreticalO16Mz").Value); result.Charge = Convert.ToInt32(root.Element("Charge").Value); } result.PeptideProfile = (from x in root.Element("PeptideProfile").Elements() let mzEle = x.Attribute("Mz") let mz = mzEle == null ? 0.0 : MyConvert.ToDouble(mzEle.Value) let intensity = MyConvert.ToDouble(x.Value) select new Peak(mz, intensity)).ToList(); if (result.PeptideProfile[0].Mz == 0.0) { var pkls = IsotopicBuilderFactory.GetBuilder().GetProfile(new AtomComposition(result.PeptideAtomComposition), result.Charge, 10); for (int i = 0; i < Math.Min(result.PeptideProfile.Count, pkls.Count); i++) { result.PeptideProfile[i].Mz = pkls[i].Mz; result.PeptideProfile[i].Intensity = pkls[i].Intensity; } } var observed = root.Element("ObservedEnvelopes"); result.ObservedEnvelopes = new List <O18QuanEnvelope>(); foreach (var pkl in observed.Elements()) { var peakList = new O18QuanEnvelope(); int scan = int.Parse(pkl.Attribute("Scan").Value); double retentiontime = MyConvert.ToDouble(pkl.Attribute("Retentiontime").Value); peakList.Enabled = bool.Parse(pkl.Attribute("Enabled").Value); peakList.IsIdentified = bool.Parse(pkl.Attribute("Identified").Value); peakList.ScanTimes.Add(new ScanTime(scan, retentiontime)); foreach (var peak in pkl.Elements()) { double mz = MyConvert.ToDouble(peak.Attribute("Mz").Value); double intensity = MyConvert.ToDouble(peak.Attribute("Intensity").Value); var p = new Peak(mz, intensity); peakList.Add(p); } result.ObservedEnvelopes.Add(peakList); } var species = root.Element("SpeciesAbundance"); result.SpeciesAbundance.O16 = MyConvert.ToDouble(species.Element("O16").Value); result.SpeciesAbundance.O181 = MyConvert.ToDouble(species.Element("O181").Value); result.SpeciesAbundance.O182 = MyConvert.ToDouble(species.Element("O182").Value); var regression = species.Element("Regression"); result.SpeciesAbundance.RegressionCorrelation = MyConvert.ToDouble(regression.Attribute("Correlation").Value); result.SpeciesAbundance.RegressionItems.Clear(); foreach (var item in regression.Elements()) { var ritem = new SpeciesRegressionItem(); ritem.Mz = MyConvert.ToDouble(item.Attribute("Mz").Value); ritem.ObservedIntensity = MyConvert.ToDouble(item.Attribute("Observed").Value); ritem.RegressionIntensity = MyConvert.ToDouble(item.Attribute("Regression").Value); result.SpeciesAbundance.RegressionItems.Add(ritem); } var samples = root.Element("SampleAbundance"); result.SampleAbundance.LabellingEfficiency = MyConvert.ToDouble(samples.Attribute("LabellingEfficiency").Value); result.SampleAbundance.Ratio = MyConvert.ToDouble(samples.Attribute("Ratio").Value); result.SampleAbundance.O16 = MyConvert.ToDouble(samples.Element("O16").Value); result.SampleAbundance.O18 = MyConvert.ToDouble(samples.Element("O18").Value); return(result); }
public override IEnumerable <string> Process(string optionFile) { this.options = O18QuantificationFileProcessorOptions.Load(optionFile); var calc = options.GetProteinRatioCalculator(); var detailDirectory = options.GetDetailDirectory(); if (!Directory.Exists(detailDirectory)) { Directory.CreateDirectory(detailDirectory); } var format = new MascotResultTextFormat(); IIdentifiedResult mr = format.ReadFromFile(options.ProteinFile); CheckRawFilename(mr, optionFile); Dictionary <string, List <IIdentifiedSpectrum> > filePepMap = GetFilePeptideMap(mr); Dictionary <IIdentifiedPeptide, O18QuantificationSummaryItem> pepResultMap = new Dictionary <IIdentifiedPeptide, O18QuantificationSummaryItem>(); foreach (string filename in filePepMap.Keys) { Progress.SetMessage("Processing " + filename); string rawFilename = filename; if (new FileInfo(filename).Name.Equals("Cmpd.raw")) { rawFilename = FindRawFileName(options.ProteinFile); } string experimental = FileUtils.ChangeExtension(new FileInfo(rawFilename).Name, ""); using (CacheRawFile rawFile = new CacheRawFile(rawFilename)) { int firstScanNumber = rawFile.GetFirstSpectrumNumber(); int lastScanNumber = rawFile.GetLastSpectrumNumber(); List <IIdentifiedSpectrum> peps = filePepMap[filename]; Dictionary <string, DifferentRetentionTimeEnvelopes> peptideChargeMap = new Dictionary <string, DifferentRetentionTimeEnvelopes>(); Dictionary <O18QuanEnvelopes, List <IIdentifiedSpectrum> > pklMpMap = new Dictionary <O18QuanEnvelopes, List <IIdentifiedSpectrum> >(); Progress.SetRange(0, peps.Count); foreach (IIdentifiedSpectrum mphit in peps) { if (Progress.IsCancellationPending()) { throw new UserTerminatedException(); } Progress.Increment(1); IIdentifiedPeptide mp = mphit.Peptide; if (mp.Sequence.EndsWith("-")) { //it cannot be O18 labelled, just skip it continue; } int startScan = mphit.Query.FileScan.FirstScan; double theoreticalMz = GetTheoretialO16Mz(gapO18O16, mphit); int theoreticalMass = (int)(theoreticalMz * mphit.Query.Charge + 0.5); string sequenceCharge = PeptideUtils.GetPureSequence(mphit.Sequence) + "." + mphit.Query.Charge + "." + theoreticalMass; if (!peptideChargeMap.ContainsKey(sequenceCharge)) { peptideChargeMap.Add(sequenceCharge, new DifferentRetentionTimeEnvelopes()); } bool bFound = false; DifferentRetentionTimeEnvelopes pkls = peptideChargeMap[sequenceCharge]; foreach (var pklList in pkls) { if (pklList.Count == 0) { continue; } if (pklList[0].Scan > startScan) { continue; } if (pklList[pklList.Count - 1].Scan < startScan) { continue; } pklMpMap[pklList].Add(mphit); bFound = true; bool findIdentified = false; for (int i = 1; i < pklList.Count; i++) { if (pklList[i].ScanTimes[0].Scan > startScan) { pklList[i - 1].IsIdentified = true; findIdentified = true; break; } } if (!findIdentified) { pklList[pklList.Count - 1].IsIdentified = true; } } if (bFound) { continue; } double mzTolerance = PrecursorUtils.ppm2mz(theoreticalMz, options.PPMTolerance); O18QuanEnvelopes envelopes = new O18QuanEnvelopes(); bool bFirst = true; int count = 0; //backward for (int scan = startScan; scan >= firstScanNumber; scan--) { if (1 == rawFile.GetMsLevel(scan)) { O18QuanEnvelope envelope = GetCorrespondingEnvelope(rawFile, theoreticalMz, mphit.Query.Charge, mzTolerance, scan); //At most one invalid scan inside both pre or post identification scan range. if (!IsValidEnvelope(envelope, mphit.Charge)) { if (count > 0) { envelopes.RemoveAt(0); break; } else { count++; } } else { count = 0; } if (bFirst) { envelope.IsIdentified = true; bFirst = false; } envelopes.Insert(0, envelope); } } if (envelopes.Count == 0) { //If the identified scan has no quantification information ,ignore it. continue; } count = 0; //forward for (int scan = startScan + 1; scan <= lastScanNumber; scan++) { if (1 == rawFile.GetMsLevel(scan)) { var envelope = GetCorrespondingEnvelope(rawFile, theoreticalMz, mphit.Query.Charge, mzTolerance, scan); //At most one invalid scan inside both pre or post identification scan range. if (!IsValidEnvelope(envelope, mphit.Charge)) { if (count > 0) { envelopes.RemoveAt(envelopes.Count - 1); break; } else { count = 1; } } else { count = 0; } envelopes.Add(envelope); } } if (envelopes.Count == 0) { continue; } string scanCurr = envelopes.GetScanRange(); //check scan list again bFound = false; foreach (var pklList in pkls) { if (pklList.Count == 0) { continue; } string scanOld = pklList.GetScanRange(); if (scanOld.Equals(scanCurr)) { pklMpMap[pklList].Add(mphit); bFound = true; break; } } if (bFound) { continue; } pkls.Add(envelopes); pklMpMap.Add(envelopes, new List <IIdentifiedSpectrum>()); pklMpMap[envelopes].Add(mphit); } var detailFilePrefix = options.GetDetailDirectory() + "\\" + new FileInfo(options.ProteinFile).Name; foreach (string sequenceCharge in peptideChargeMap.Keys) { DifferentRetentionTimeEnvelopes pkls = peptideChargeMap[sequenceCharge]; foreach (var envelopes in pkls) { if (0 == envelopes.Count) { continue; } List <IIdentifiedSpectrum> mps = pklMpMap[envelopes]; double mzTolerance = PrecursorUtils.ppm2mz(mps[0].Query.ObservedMz, options.PPMTolerance); O18QuantificationPeptideProcessor processor = new O18QuantificationPeptideProcessor(fileFormat, options.IsPostDigestionLabelling, rawFilename, PeptideUtils.GetPureSequence(mps[0].Sequence), options.PurityOfO18Water, envelopes, mzTolerance, "", options.GetScanPercentageStart() / 100, options.GetScanPercentageEnd() / 100); processor.TheoreticalMz = GetTheoretialO16Mz(gapO18O16, mps[0]); processor.Charge = mps[0].Charge; processor.SoftwareVersion = options.SoftwareVersion; var resultFilename = MyConvert.Format("{0}.{1}.{2}.{3}.{4}.O18", detailFilePrefix, experimental, PeptideUtils.GetPureSequence(mps[0].Sequence), mps[0].Charge, envelopes.GetScanRange()); processor.Process(resultFilename); O18QuantificationSummaryItem item = fileFormat.ReadFromFile(resultFilename); int maxScoreItemIndex = FindMaxScoreItemIndex(mps); var relativeFile = Path.Combine(Path.GetFileName(options.GetDetailDirectory()), Path.GetFileName(resultFilename)); for (int i = 0; i < mps.Count; i++) { if (maxScoreItemIndex == i) { item.AssignToAnnotation(mps[i], relativeFile); } else { item.AssignDuplicationToAnnotation(mps[i], relativeFile); } } } } } } List <IIdentifiedSpectrum> peptides = mr.GetSpectra(); foreach (IIdentifiedSpectrum mphit in peptides) { if (!mphit.Annotations.ContainsKey(O18QuantificationConstants.O18_RATIO_SCANCOUNT)) { mphit.Annotations[O18QuantificationConstants.O18_RATIO_SCANCOUNT] = "-"; } mphit.SetEnabled(calc.HasPeptideRatio(mphit)); } calc.Calculate(mr, m => true); string resultFile = FileUtils.ChangeExtension(optionFile, ".O18summary"); format.InitializeByResult(mr); format.ProteinFormat = format.ProteinFormat.GetLineFormat(O18QuantificationConstants.O18_EXPORT_PROTEIN_HEADER); format.WriteToFile(resultFile, mr); Progress.SetMessage("Finished, result was saved to " + resultFile); return(new[] { resultFile }); }
public void TestWriteToFile() { var item = new O18QuantificationSummaryItem(); item.RawFilename = @"D:\sqh\Science\Project/4NLFOR.raw"; item.PeptideSequence = "AFATDITDAEEDK"; item.PeptideAtomComposition = "C60H92N14O26"; item.PurityOfO18Water = 0.95; item.PeptideProfile = IsotopicBuilderFactory.GetBuilder().GetProfile(new AtomComposition(item.PeptideAtomComposition), 1, 10); item.ObservedEnvelopes = new List <O18QuanEnvelope> (); var pkl1 = new O18QuanEnvelope(); pkl1.ScanTimes.Add(new ScanTime(6741, 12.34)); pkl1.Enabled = true; pkl1.IsIdentified = true; pkl1.Add(new Peak(713.3209, 424345.9)); pkl1.Add(new Peak(713.8254, 198378.3)); pkl1.Add(new Peak(714.3221, 246866.8)); pkl1.Add(new Peak(714.8242, 221648.0)); pkl1.Add(new Peak(715.3243, 107701.3)); pkl1.Add(new Peak(715.8316, 0.0)); item.ObservedEnvelopes.Add(pkl1); var pkl2 = new O18QuanEnvelope(); pkl2.ScanTimes.Add(new ScanTime(6746, 13.34)); pkl2.Enabled = false; pkl2.Add(new Peak(713.3209, 796597.7)); pkl2.Add(new Peak(713.8254, 530778.4)); pkl2.Add(new Peak(714.3221, 456099.6)); pkl2.Add(new Peak(714.8242, 339509.8)); pkl2.Add(new Peak(715.3243, 182812.3)); pkl2.Add(new Peak(715.8316, 0.0)); item.ObservedEnvelopes.Add(pkl2); item.SpeciesAbundance = new SpeciesAbundanceInfo(); item.SpeciesAbundance.O16 = 79184307.0; item.SpeciesAbundance.O181 = 39464386.0; item.SpeciesAbundance.O182 = 440969.4; item.SampleAbundance = new SampleAbundanceInfo(); item.SampleAbundance.LabellingEfficiency = 0.0219; item.SampleAbundance.Ratio = 50; item.SampleAbundance.O16 = 0; item.SampleAbundance.O18 = 922867772.7; item.SpeciesAbundance.RegressionCorrelation = 0.9999; item.SpeciesAbundance.RegressionItems.Clear(); item.SpeciesAbundance.RegressionItems.Add(new SpeciesRegressionItem(713.3209, 36513020.6, 36512100.5)); item.SpeciesAbundance.RegressionItems.Add(new SpeciesRegressionItem(713.8254, 26438219.2, 26447909.0)); item.SpeciesAbundance.RegressionItems.Add(new SpeciesRegressionItem(714.3221, 29639265.4, 29595148.6)); item.SpeciesAbundance.RegressionItems.Add(new SpeciesRegressionItem(714.8242, 16687280.4, 16812006.0)); item.SpeciesAbundance.RegressionItems.Add(new SpeciesRegressionItem(715.3243, 7087781.0, 6823450.3)); item.SpeciesAbundance.RegressionItems.Add(new SpeciesRegressionItem(715.8316, 1799203.9, 2164120.0)); new O18QuantificationSummaryItemXmlFormat().WriteToFile(@TestContext.CurrentContext.TestDirectory + "/../../../data//O18QuantificationInformation.xml.tmp", item); AssertUtils.AssertFileEqual(@TestContext.CurrentContext.TestDirectory + "/../../../data//O18QuantificationInformation.xml.tmp", @TestContext.CurrentContext.TestDirectory + "/../../../data//O18QuantificationInformation.xml"); new FileInfo(@TestContext.CurrentContext.TestDirectory + "/../../../data//O18QuantificationInformation.xml.tmp").Delete(); }