public List <IResult> Search(ISpectrum spectrum) { List <IResult> results = new(); ISpectrum spec = spectrumProcessor.Process(spectrum); envelopeProcessor.Init(spec); foreach (IGlycanPeak glycan in glycans) { IResult result = null; double bestIntensity = 0; double bestScore = 0; for (int charge = 1; charge <= maxCharge; charge++) { List <double> mzList = Calculator.To.ComputeMZ(glycan.HighestPeak(), charge); foreach (double mz in mzList) { List <IPeak> targets = envelopeProcessor.Search(mz); if (targets.Count == 0) { continue; } SortedDictionary <int, List <IPeak> > clusters = envelopeProcessor.Cluster(mz, charge); IResult temp = monoisotopicSearcher.Match(glycan, clusters); double score = temp.Score(); double intensity = temp.Matches().Select(m => m.GetIntensity()).Sum(); if (score > cutoff) { if (intensity > bestIntensity) { bestIntensity = intensity; } else if (intensity == bestIntensity && score > bestScore) { bestScore = score; } else { continue; } result = temp; result.SetMZ(mz); result.SetRetention(spectrum.GetRetention()); result.SetCharge(charge); result.SetScan(spectrum.GetScanNum()); } } } if (result != null) { results.Add(result); } } return(results); }
public void ParallelRun(string path, string outputDir, AveragineType type, ChargerType chargerType) { string file = Path.GetFileNameWithoutExtension(path) + ".mgf"; string output = Path.Combine(outputDir, file); ThermoRawSpectrumReader reader = new ThermoRawSpectrumReader(); LocalMaximaPicking picking = new LocalMaximaPicking(ms1PrcisionPPM); reader.Init(path); Dictionary <int, List <int> > scanGroup = new Dictionary <int, List <int> >(); int current = -1; int start = reader.GetFirstScan(); int end = reader.GetLastScan(); for (int i = start; i < end; i++) { if (reader.GetMSnOrder(i) == 1) { current = i; scanGroup[i] = new List <int>(); } else if (reader.GetMSnOrder(i) == 2) { scanGroup[current].Add(i); } } List <MS2Info> ms2Infos = new List <MS2Info>(); Parallel.ForEach(scanGroup, (scanPair) => { if (scanPair.Value.Count > 0) { ISpectrum ms1 = reader.GetSpectrum(scanPair.Key); foreach (int i in scanPair.Value) { double mz = reader.GetPrecursorMass(i, reader.GetMSnOrder(i)); List <IPeak> ms1Peaks = FilterPeaks(ms1.GetPeaks(), mz, searchRange); if (ms1Peaks.Count() == 0) { continue; } // insert pseudo peaks for large gap List <IPeak> peaks = new List <IPeak>(); double precision = 0.02; double last = ms1Peaks.First().GetMZ(); foreach (IPeak peak in ms1Peaks) { if (peak.GetMZ() - last > precision) { peaks.Add(new GeneralPeak(last + precision / 2, 0)); peaks.Add(new GeneralPeak(peak.GetMZ() - precision / 2, 0)); } peaks.Add(peak); last = peak.GetMZ(); } List <IPeak> majorPeaks = picking.Process(peaks); ICharger charger = new Patterson(); if (chargerType == ChargerType.Fourier) { charger = new Fourier(); } else if (chargerType == ChargerType.Combined) { charger = new PattersonFourierCombine(); } int charge = charger.Charge(peaks, mz - searchRange, mz + searchRange); // find evelope cluster EnvelopeProcess envelope = new EnvelopeProcess(); var cluster = envelope.Cluster(majorPeaks, mz, charge); if (cluster.Count == 0) { continue; } // find monopeak Averagine averagine = new Averagine(type); BrainCSharp braincs = new BrainCSharp(); MonoisotopicSearcher searcher = new MonoisotopicSearcher(averagine, braincs); MonoisotopicScore result = searcher.Search(mz, charge, cluster); double precursorMZ = result.GetMZ(); // write mgf ISpectrum ms2 = reader.GetSpectrum(i); IProcess processer = new WeightedAveraging(new LocalNeighborPicking()); ms2 = processer.Process(ms2); MS2Info ms2Info = new MS2Info { PrecursorMZ = result.GetMZ(), PrecursorCharge = charge, Scan = ms2.GetScanNum(), Retention = ms2.GetRetention(), Peaks = ms2.GetPeaks() }; lock (resultLock) { ms2Infos.Add(ms2Info); } } } readingProgress.Add(scanGroup.Count); }); ms2Infos = ms2Infos.OrderBy(m => m.Scan).ToList(); using (FileStream ostrm = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write)) { using (StreamWriter writer = new StreamWriter(ostrm)) { foreach (MS2Info ms2 in ms2Infos) { WriteMGF(writer, path + ",SCANS=" + ms2.Scan.ToString() + ",PRECURSOR=" + ms2.PrecursorMZ, ms2.PrecursorMZ, ms2.PrecursorCharge, ms2.Scan, ms2.Retention * 60, reader.GetActivation(ms2.Scan), ms2.Peaks); writer.Flush(); } } } // update progress progress.Add(); }
void GenerateTasks() { if (Path.GetExtension(msPath) == ".mgf") { MGFSpectrumReader reader = new MGFSpectrumReader(); reader.Init(msPath); Dictionary <int, MS2Spectrum> spectraData = reader.GetSpectrum(); foreach (int scan in spectraData.Keys) { MS2Spectrum spectrum = spectraData[scan]; SearchTask searchTask = new SearchTask(spectrum, spectrum.PrecursorMZ(), spectrum.PrecursorCharge()); tasks.Enqueue(searchTask); readingCounter.Add(spectraData.Count); } } else { ISpectrumReader reader = new ThermoRawSpectrumReader(); LocalMaximaPicking picking = new LocalMaximaPicking(); IProcess process = new WeightedAveraging(new LocalNeighborPicking()); reader.Init(msPath); int start = reader.GetFirstScan(); int end = reader.GetLastScan(); Dictionary <int, List <int> > scanGroup = new Dictionary <int, List <int> >(); int current = -1; for (int i = start; i < end; i++) { if (reader.GetMSnOrder(i) == 1) { current = i; scanGroup[i] = new List <int>(); } else if (reader.GetMSnOrder(i) == 2) { scanGroup[current].Add(i); } } Parallel.ForEach(scanGroup, new ParallelOptions { MaxDegreeOfParallelism = SearchingParameters.Access.ThreadNums }, (scanPair) => { if (scanPair.Value.Count > 0) { ISpectrum ms1 = reader.GetSpectrum(scanPair.Key); foreach (int i in scanPair.Value) { double mz = reader.GetPrecursorMass(i, reader.GetMSnOrder(i)); List <IPeak> ms1Peaks = FilterPeaks(ms1.GetPeaks(), mz, searchRange); if (ms1Peaks.Count() == 0) { continue; } ICharger charger = new Patterson(); int charge = charger.Charge(ms1Peaks, mz - searchRange, mz + searchRange); // insert pseudo peaks for large gap List <IPeak> peaks = new List <IPeak>(); double precision = 0.02; double last = ms1Peaks.First().GetMZ(); foreach (IPeak peak in ms1Peaks) { if (peak.GetMZ() - last > precision) { peaks.Add(new GeneralPeak(last + precision / 2, 0)); peaks.Add(new GeneralPeak(peak.GetMZ() - precision / 2, 0)); } peaks.Add(peak); last = peak.GetMZ(); } List <IPeak> majorPeaks = picking.Process(peaks); // find evelope cluster EnvelopeProcess envelope = new EnvelopeProcess(); var cluster = envelope.Cluster(majorPeaks, mz, charge); if (cluster.Count == 0) { continue; } // find monopeak Averagine averagine = new Averagine(AveragineType.GlycoPeptide); BrainCSharp braincs = new BrainCSharp(); MonoisotopicSearcher searcher = new MonoisotopicSearcher(averagine, braincs); MonoisotopicScore result = searcher.Search(mz, charge, cluster); double precursorMZ = result.GetMZ(); // search ISpectrum ms2 = reader.GetSpectrum(i); ms2 = process.Process(ms2); SearchTask searchTask = new SearchTask(ms2, precursorMZ, charge); tasks.Enqueue(searchTask); } } readingCounter.Add(scanGroup.Count); }); } }
public void Test1() { string path = @"C:\Users\Rui Zhang\Downloads\ZC_20171218_C16_R1.raw"; string fasta = @"C:\Users\Rui Zhang\Downloads\haptoglobin.fasta"; // peptides IProteinReader proteinReader = new FastaReader(); List <IProtein> proteins = proteinReader.Read(fasta); List <IProtein> decoyProteins = new List <IProtein>(); foreach (IProtein protein in proteins) { IProtein p = new BaseProtein(); p.SetSequence(Reverse(protein.Sequence())); decoyProteins.Add(p); } List <Proteases> proteases = new List <Proteases>() { Proteases.Trypsin, Proteases.GluC }; HashSet <string> peptides = new HashSet <string>(); ProteinDigest proteinDigest = new ProteinDigest(2, 5, proteases[0]); foreach (IProtein protein in decoyProteins) { peptides.UnionWith(proteinDigest.Sequences(protein.Sequence(), ProteinPTM.ContainsNGlycanSite)); } for (int i = 1; i < proteases.Count; i++) { proteinDigest.SetProtease(proteases[i]); List <string> peptidesList = peptides.ToList(); foreach (string seq in peptidesList) { peptides.UnionWith(proteinDigest.Sequences(seq, ProteinPTM.ContainsNGlycanSite)); } } Assert.True(peptides.Contains("KDNLTYVGDGETR")); // build glycan GlycanBuilder glycanBuilder = new GlycanBuilder(); glycanBuilder.Build(); // search List <SearchResult> searchResults = new List <SearchResult>(); ThermoRawSpectrumReader reader = new ThermoRawSpectrumReader(); LocalMaximaPicking picking = new LocalMaximaPicking(); IProcess process = new LocalNeighborPicking(); reader.Init(path); double searchRange = 2; ISpectrum ms1 = null; List <IPeak> majorPeaks = new List <IPeak>(); ISearch <string> oneSearcher = new BucketSearch <string>(ToleranceBy.PPM, 10); PrecursorMatch precursorMatcher = new PrecursorMatch(oneSearcher); precursorMatcher.Init(peptides.ToList(), glycanBuilder.GlycanMaps()); ISearch <string> moreSearcher = new BucketSearch <string>(ToleranceBy.Dalton, 0.01); SequenceSearch sequenceSearcher = new SequenceSearch(moreSearcher); ISearch <int> extraSearcher = new BucketSearch <int>(ToleranceBy.Dalton, 0.01); GlycanSearch glycanSearcher = new GlycanSearch(extraSearcher, glycanBuilder.GlycanMaps()); SearchAnalyzer searchAnalyzer = new SearchAnalyzer(); for (int i = reader.GetFirstScan(); i < reader.GetLastScan(); i++) { if (reader.GetMSnOrder(i) < 2) { ms1 = reader.GetSpectrum(i); majorPeaks = picking.Process(ms1.GetPeaks()); } else { double mz = reader.GetPrecursorMass(i, reader.GetMSnOrder(i)); if (ms1.GetPeaks() .Where(p => p.GetMZ() > mz - searchRange && p.GetMZ() < mz + searchRange) .Count() == 0) { continue; } Patterson charger = new Patterson(); int charge = charger.Charge(ms1.GetPeaks(), mz - searchRange, mz + searchRange); // find evelope cluster EnvelopeProcess envelope = new EnvelopeProcess(); var cluster = envelope.Cluster(majorPeaks, mz, charge); if (cluster.Count == 0) { continue; } // find monopeak Averagine averagine = new Averagine(AveragineType.GlycoPeptide); BrainCSharp braincs = new BrainCSharp(); MonoisotopicSearcher searcher = new MonoisotopicSearcher(averagine, braincs); MonoisotopicScore result = searcher.Search(mz, charge, cluster); double precursorMZ = result.GetMZ(); // search ISpectrum ms2 = reader.GetSpectrum(i); ms2 = process.Process(ms2); //precursor match var pre_results = precursorMatcher.Match(precursorMZ, charge); if (pre_results.Count == 0) { continue; } // spectrum search var peptide_results = sequenceSearcher.Search(ms2.GetPeaks(), charge, pre_results); if (peptide_results.Count == 0) { continue; } var glycan_results = glycanSearcher.Search(ms2.GetPeaks(), charge, pre_results); if (glycan_results.Count == 0) { continue; } var temp_results = searchAnalyzer.Analyze(i, ms2.GetPeaks(), peptide_results, glycan_results); break; } } }
public static Spectrum GetMS2Spectrum(ref ThermoRawSpectrumReader reader, int scan, AveragineType type, ChargerType chargerType, LocalMaximaPicking picking, IProcess process, ISpectrum ms1) { // scan header Spectrum spectrum = new Spectrum { id = "scan=" + scan.ToString() }; double dLowMass = 0; double dHighMass = 0; double dTIC = 0; double dBasePeakMass = 0; double dBasePeakIntensity = 0; reader.GetScanHeaderInfoForScanNum(scan, ref dLowMass, ref dHighMass, ref dTIC, ref dBasePeakMass, ref dBasePeakIntensity); SetScanHeader(spectrum, dLowMass, dHighMass, dTIC, dBasePeakMass, dBasePeakIntensity); // binary data spectrum.binaryDataArrayList = new BinaryDataArrayList(); SetBinaryDataArrayHeader(spectrum.binaryDataArrayList); spectrum.cvParam[0] = new Component.CVParam() { cvRef = "MS", accession = "MS:1000511", name = "ms level", value = "2", }; double mz = reader.GetPrecursorMass(scan, reader.GetMSnOrder(scan)); List <IPeak> ms1Peaks = FilterPeaks(ms1.GetPeaks(), mz, searchRange); if (ms1Peaks.Count() == 0) { return(null); } // insert pseudo peaks for large gaps List <IPeak> peaks = new List <IPeak>(); double precision = 0.02; double last = ms1Peaks.First().GetMZ(); foreach (IPeak peak in ms1Peaks) { if (peak.GetMZ() - last > precision) { peaks.Add(new GeneralPeak(last + precision / 2, 0)); peaks.Add(new GeneralPeak(peak.GetMZ() - precision / 2, 0)); } peaks.Add(peak); last = peak.GetMZ(); } List <IPeak> majorPeaks = picking.Process(peaks); ICharger charger = new Patterson(); if (chargerType == ChargerType.Fourier) { charger = new Fourier(); } else if (chargerType == ChargerType.Combined) { charger = new PattersonFourierCombine(); } int charge = charger.Charge(peaks, mz - searchRange, mz + searchRange); // find evelope cluster EnvelopeProcess envelope = new EnvelopeProcess(); var cluster = envelope.Cluster(majorPeaks, mz, charge); if (cluster.Count == 0) { return(null); } // find monopeak Averagine averagine = new Averagine(type); BrainCSharp braincs = new BrainCSharp(); MonoisotopicSearcher searcher = new MonoisotopicSearcher(averagine, braincs); MonoisotopicScore result = searcher.Search(mz, charge, cluster); // process spectrum ISpectrum ms2 = reader.GetSpectrum(scan); List <IPeak> ms2Peaks = process.Process(ms2).GetPeaks(); spectrum.binaryDataArrayList.binaryDataArray[0].binary = ms2Peaks.SelectMany(p => BitConverter.GetBytes(p.GetMZ())).ToArray(); spectrum.binaryDataArrayList.binaryDataArray[1].binary = ms2Peaks.SelectMany(p => BitConverter.GetBytes(p.GetIntensity())).ToArray(); spectrum.defaultArrayLength = ms2Peaks.Count.ToString(); spectrum.precursorList = new PrecursorList { count = "1", precursor = new Precursor[1] }; for (int i = 0; i < spectrum.precursorList.precursor.Length; i++) { spectrum.precursorList.precursor[i] = new Precursor(); } spectrum.precursorList.precursor[0].selectedIonList = new SelectedIonList { count = "1", selectedIon = new SelectedIon[1] }; for (int i = 0; i < spectrum.precursorList.precursor[0].selectedIonList.selectedIon.Length; i++) { spectrum.precursorList.precursor[0].selectedIonList.selectedIon[i] = new SelectedIon(); } spectrum.precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam = new Component.CVParam[2]; spectrum.precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam[0] = new Component.CVParam() { cvRef = "MS", accession = "MS:1000744", name = "selected ion m/z", value = result.GetMZ().ToString(), unitCvRef = "MS", unitAccession = "MS:1000040", unitName = "m/z" }; spectrum.precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam[1] = new Component.CVParam() { cvRef = "MS", accession = "MS:1000041", name = "charge state", value = charge.ToString() }; spectrum.precursorList.precursor[0].activation = new Activation { cvParam = new Component.CVParam[1] }; spectrum.precursorList.precursor[0].activation.cvParam[0] = ActivationCVParam(reader.GetActivation(scan)); spectrum.binaryDataArrayList.binaryDataArray[0].encodedLength = Convert.ToBase64String(spectrum.binaryDataArrayList.binaryDataArray[0].binary).Length.ToString(); spectrum.binaryDataArrayList.binaryDataArray[1].encodedLength = Convert.ToBase64String(spectrum.binaryDataArrayList.binaryDataArray[1].binary).Length.ToString(); return(spectrum); }