Esempio n. 1
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (rawFile.Exists)
            {
                if (reader != null)
                {
                    reader.Close();
                }
                reader   = RawFileFactory.GetRawFileReader(rawFile.FullName);
                lastScan = reader.GetLastSpectrumNumber();
            }

            if (ms3MatchedFile.Exists)
            {
                items = (from line in File.ReadAllLines(ms3MatchedFile.FullName).Skip(1)
                         let parts = line.Split('\t')
                                     let pm = int.Parse(parts[6])
                                              where pm > 0
                                              select new MS3MatchItem()
                {
                    Category = parts[0],
                    Sequence1 = parts[1],
                    Scan1 = int.Parse(parts[2]),
                    Sequence2 = parts[3],
                    Scan2 = int.Parse(parts[4]),
                    MinMz = parts[5].StringAfter(">=").Trim(),
                    PrecursorMatched = pm,
                    MS3Matched = int.Parse(parts[7])
                }).ToList();

                gvPeptides.DataSource = items;
            }
        }
        public QuantificationChromotograph Build(IIdentifiedSpectrum mphit, IRawFile reader)
        {
            QuantificationChromotograph result = new QuantificationChromotograph();

            var envelopes = builder.Build(mphit);

            int startScan = mphit.Query.FileScan.FirstScan;

            double mzTolerance = PrecursorUtils.ppm2mz(envelopes[0][0], ppmTolerance);

            bool bFirst = true;

            int firstScanNumber = reader.GetFirstSpectrumNumber();
            int lastScanNumber  = reader.GetLastSpectrumNumber();

            //backward
            for (int scan = startScan; scan >= firstScanNumber; scan--)
            {
                if (1 == reader.GetMsLevel(scan))
                {
                    QuantificationScan qscan = reader.GetPeakList(scan).GetQuantificationScan(envelopes, mzTolerance);

                    if (!validate(qscan))
                    {
                        break;
                    }

                    if (bFirst)
                    {
                        qscan.IsIdentified = true;
                        bFirst             = false;
                    }
                    result.Insert(0, qscan);
                }
            }

            //forward
            for (int scan = startScan + 1; scan <= lastScanNumber; scan++)
            {
                if (1 == reader.GetMsLevel(scan))
                {
                    QuantificationScan qscan = reader.GetPeakList(scan).GetQuantificationScan(envelopes, mzTolerance);

                    if (!validate(qscan))
                    {
                        break;
                    }

                    result.Add(qscan);
                }
            }

            if (result.Count > 0)
            {
                result.IdentifiedSpectra.Add(mphit);
            }

            return(result);
        }
        protected override void DoRealGo()
        {
            if (_reader != null)
            {
                _reader.Close();
            }

            _reader = RawFileFactory.GetRawFileReader(GetOriginFile());

            _firstScan = _reader.GetFirstSpectrumNumber();
            _lastScan  = _reader.GetLastSpectrumNumber();

            txtScan.Text = _firstScan.ToString();
            DisplayScan(0);
        }
        public static void FillScanLevels(List <ScanLevel> levels, IRawFile rawFile)
        {
            var firstScan = rawFile.GetFirstSpectrumNumber();
            var lastScan  = rawFile.GetLastSpectrumNumber();

            for (var scan = firstScan; scan <= lastScan; scan++)
            {
                var mslevel = rawFile.GetMsLevel(scan);
                levels.Add(new ScanLevel()
                {
                    Scan  = scan,
                    Level = mslevel
                });
            }
        }
Esempio n. 5
0
        public static List <PeakList <Peak> > GetMS1List(this IRawFile reader)
        {
            var result = new List <PeakList <Peak> >();
            var first  = reader.GetFirstSpectrumNumber();
            var last   = reader.GetLastSpectrumNumber();

            for (int i = first; i <= last; i++)
            {
                var mslevel = reader.GetMsLevel(i);
                if (mslevel == 1)
                {
                    var pkl = reader.GetPeakList(i);
                    result.Add(pkl);
                }
            }
            return(result);
        }
Esempio n. 6
0
        protected List <ScanLevel> GetRawScanLevels(IRawFile rawFile)
        {
            var result    = new List <ScanLevel>();
            var firstScan = rawFile.GetFirstSpectrumNumber();
            var lastScan  = rawFile.GetLastSpectrumNumber();

            for (var scan = firstScan; scan <= lastScan; scan++)
            {
                var mslevel = rawFile.GetMsLevel(scan);
                result.Add(new ScanLevel()
                {
                    Scan  = scan,
                    Level = mslevel
                });
            }
            return(result);
        }
Esempio n. 7
0
        protected void DoGetOffsets(List <OffsetEntry> result, IRawFile reader, string msg)
        {
            var firstScan = reader.GetFirstSpectrumNumber();
            var lastScan  = reader.GetLastSpectrumNumber();

            SetMessage(msg);
            SetRange(firstScan, lastScan);

            for (int scan = firstScan; scan <= lastScan; scan++)
            {
                if (reader.GetMsLevel(scan) == 1)
                {
                    SetPosition(scan);

                    if (IsLoopStopped)
                    {
                        return;
                    }

                    if (Progress.IsCancellationPending())
                    {
                        throw new UserTerminatedException();
                    }

                    var pkl = reader.GetPeakList(scan);
                    var rt  = reader.ScanToRetentionTime(scan);

                    var offsetValues = GetOffsetsFromPeakList(pkl);

                    if (offsetValues.Count > 0)
                    {
                        var weightedPPM = GetWeightedPPM(offsetValues);
                        result.Add(new OffsetEntry(scan, rt, weightedPPM, offsetValues.Count));
                    }
                    else
                    {
                        result.Add(new OffsetEntry(scan, rt, 0, 0));
                    }
                }
            }
            CalculateOffset(result, m => m.InitValue);
        }
        private StreamWriter GetStreamWriter(IRawFile rawReader, string peakMode, int msLevel, string fileName)
        {
            string resultFile = GetPeakModeFileName(rawReader, peakMode, msLevel, fileName);
            string tmpFile    = resultFile + ".tmp";

            if (swMap.ContainsKey(tmpFile))
            {
                return(swMap[tmpFile]);
            }

            outputFiles.Add(tmpFile);

            var result = new StreamWriter(tmpFile);

            swMap[tmpFile] = result;

            if (1 == msLevel)
            {
                int firstScan = rawReader.GetFirstSpectrumNumber();
                int lastScan  = rawReader.GetLastSpectrumNumber();

                result.Write("H\tCreationDate\t{0:m/dd/yyyy HH:mm:ss}\n", DateTime.Now);
                result.Write("H\tExtractor\tProteomicsTools\n");
                result.Write("H\tExtractorVersion\t4.1.9\n");
                result.Write("H\tComments\tProteomicsTools written by Quanhu Sheng, 2008-2018\n");
                result.Write("H\tExtractorOptions\tMS1\n");
                result.Write("H\tAcquisitionMethod\tData-Dependent\n");
                result.Write("H\tDataType\tCentriod\n");
                result.Write("H\tScanType\tMS1\n");
                result.Write("H\tFirstScan\t{0}\n", firstScan);
                result.Write("H\tLastScan\t{0}\n", lastScan);

                var ms1IndexFilename = resultFile + ".index";
                sw1Index = new StreamWriter(ms1IndexFilename);
                swMap[ms1IndexFilename] = sw1Index;
            }

            return(result);
        }
        public FileData3 ReadFromFile(string fileName)
        {
            FileData3 result = new FileData3();

            result.FileName = new FileInfo(fileName).Name;

            rawFile.Open(fileName);
            try
            {
                for (int scan = rawFile.GetFirstSpectrumNumber(); scan <= rawFile.GetLastSpectrumNumber(); scan++)
                {
                    PeakList <Peak> pkl = rawFile.GetPeakList(scan, minPeakMz - 1, maxPeakMz + 1);

                    result.AddPeaks(minPeakMz, maxPeakMz, scan, pkl);
                }
            }
            finally
            {
                rawFile.Close();
            }

            return(result);
        }
Esempio n. 10
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (rawFile.Exists)
            {
                if (reader != null)
                {
                    reader.Close();
                }
                reader   = RawFileFactory.GetRawFileReader(rawFile.FullName);
                lastScan = reader.GetLastSpectrumNumber();
            }

            if (ms3xcorrFile.Exists)
            {
                items = (from line in File.ReadAllLines(ms3xcorrFile.FullName).Skip(1)
                         let parts = line.Split('\t')
                                     select new MS3XcorrItem()
                {
                    Category = parts[0],
                    Sequence1 = parts[1],
                    MS2Scan1 = int.Parse(parts[2]),
                    MS2Precursor1 = double.Parse(parts[3]),
                    MS3Scan1 = int.Parse(parts[4]),
                    MS3Precursor1 = double.Parse(parts[5]),
                    Sequence2 = parts[6],
                    MS2Scan2 = int.Parse(parts[7]),
                    MS2Precursor2 = double.Parse(parts[8]),
                    MS3Scan2 = int.Parse(parts[9]),
                    MS3Precursor2 = double.Parse(parts[10]),
                    Xcorr = double.Parse(parts[11])
                }).ToList();

                items.RemoveAll(m => m.MS3Precursor1 < 250);
                items = items.OrderBy(m => m.Category).ThenBy(m => m.Sequence1).ThenBy(m => m.MS3Precursor1).ToList();
                gvPeptides.DataSource = items;
            }
        }
        public override IEnumerable <string> Process(string fileName)
        {
            string resultFile = new FileInfo(targetDir + "\\" + FileUtils.ChangeExtension(new FileInfo(fileName).Name, ".txt")).FullName;

            reader.Open(fileName);
            try
            {
                using (StreamWriter sw = new StreamWriter(resultFile))
                {
                    sw.NewLine = "\n";
                    sw.WriteLine();
                    sw.WriteLine("FUNCTION 1");

                    int firstScan = reader.GetFirstSpectrumNumber();
                    int lastScan  = reader.GetLastSpectrumNumber();

                    for (int i = firstScan; i <= lastScan; i++)
                    {
                        var pkl = reader.GetPeakList(i);
                        sw.WriteLine("");
                        sw.WriteLine("Scan\t\t{0}", i);
                        sw.WriteLine("Retention Time\t{0:0.000}", reader.ScanToRetentionTime(i));
                        sw.WriteLine("");
                        foreach (var peak in pkl)
                        {
                            sw.WriteLine("{0:0.0000}\t{1:0}", peak.Mz, peak.Intensity);
                        }
                    }
                }
            }
            finally
            {
                reader.Close();
            }
            return(new string[] { resultFile });
        }
        public override IEnumerable <string> Process(string targetDir)
        {
            foreach (var raw in rawFiles)
            {
                List <RetentionTimePeak> waitingPeaks = new List <RetentionTimePeak>();
                foreach (var peak in targetPeaks)
                {
                    string file = GetTargetFile(targetDir, raw, peak);
                    if (force || !File.Exists(file))
                    {
                        waitingPeaks.Add(peak);
                    }
                }

                if (waitingPeaks.Count == 0)
                {
                    continue;
                }

                rawReader.Open(raw);
                try
                {
                    int firstScan = rawReader.GetFirstSpectrumNumber();
                    int lastScan  = rawReader.GetLastSpectrumNumber();

                    waitingPeaks.ForEach(m => m.Chromotographs.Clear());

                    for (int scan = firstScan; scan <= lastScan; scan++)
                    {
                        if (rawReader.GetMsLevel(scan) != 1)
                        {
                            continue;
                        }

                        if (Progress.IsCancellationPending())
                        {
                            throw new UserTerminatedException();
                        }

                        PeakList <Peak> pkl = rawReader.GetPeakList(scan);

                        double rt = rawReader.ScanToRetentionTime(scan);
                        var    st = new ScanTime(scan, rt);

                        foreach (var peak in waitingPeaks)
                        {
                            var env = pkl.FindEnvelopeDirectly(peak.PeakEnvelope, peak.MzTolerance, () => new Peak());
                            env.ScanTimes.Add(st);
                            peak.Chromotographs.Add(env);
                        }
                    }

                    waitingPeaks.ForEach(m => m.TrimPeaks());
                }
                finally
                {
                    rawReader.Close();
                }

                foreach (var peak in waitingPeaks)
                {
                    string file = GetTargetFile(targetDir, raw, peak);
                    new RetentionTimePeakXmlFormat().WriteToFile(file, peak);
                }
            }

            return(new string[] { targetDir });
        }
 public void TestGetLastSpectrumNumber()
 {
     Assert.AreEqual(15, reader.GetLastSpectrumNumber());
 }
Esempio n. 14
0
        public override IEnumerable <string> Process(string rawFilename)
        {
            string ms1Filename      = GetResultFilename(rawFilename);
            string ms1IndexFilename = ms1Filename + ".index";

            rawFile.Open(rawFilename);
            try
            {
                int firstScan = rawFile.GetFirstSpectrumNumber();
                int lastScan  = rawFile.GetLastSpectrumNumber();

                int firstFullScan = 0;
                for (firstFullScan = firstScan; firstFullScan < lastScan; firstFullScan++)
                {
                    if (rawFile.GetMsLevel(firstFullScan) == 1)
                    {
                        break;
                    }
                }

                if (firstFullScan >= lastScan)
                {
                    return(new List <string>());
                }

                Progress.SetRange(firstScan, lastScan);

                using (var sw = new StreamWriter(ms1Filename))
                {
                    //write header
                    sw.Write("H\tCreationDate\t{0:m/dd/yyyy HH:mm:ss}\n", DateTime.Now);
                    sw.Write("H\tExtractor\t{0}\n", GetProgramName());
                    sw.Write("H\tExtractorVersion\t{0}\n", GetProgramVersion());
                    sw.Write("H\tComments\t{0} written by Quanhu Sheng, 2008-2009\n", GetProgramName());
                    sw.Write("H\tExtractorOptions\tMS1\n");
                    sw.Write("H\tAcquisitionMethod\tData-Dependent\n");
                    sw.Write("H\tDataType\tCentriod\n");
                    sw.Write("H\tScanType\tMS1\n");
                    sw.Write("H\tFirstScan\t{0}\n", firstScan);
                    sw.Write("H\tLastScan\t{0}\n", lastScan);

                    using (var swIndex = new StreamWriter(ms1IndexFilename))
                    {
                        for (int scan = firstScan; scan <= lastScan; scan++)
                        {
                            if (Progress.IsCancellationPending())
                            {
                                throw new UserTerminatedException();
                            }

                            Progress.SetPosition(scan);

                            if (rawFile.GetMsLevel(scan) == 1)
                            {
                                sw.Flush();

                                swIndex.Write("{0}\t{1}\n", scan, sw.BaseStream.Position);

                                sw.Write("S\t{0}\t{0}\n", scan);
                                sw.Write("I\tRetTime\t{0:0.####}\n", rawFile.ScanToRetentionTime(scan));

                                PeakList <Peak> pkl = rawFile.GetPeakList(scan);
                                foreach (Peak p in pkl)
                                {
                                    sw.Write("{0:0.0000} {1:0.0} {2}\n", p.Mz, p.Intensity, p.Charge);
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                rawFile.Close();
            }

            Progress.End();

            return(new[] { ms1Filename });
        }
Esempio n. 15
0
        private List <SrmScan> GetMRMScans(string directory)
        {
            var fileName = GetPeakFileName(directory);

            if (File.Exists(fileName))
            {
                SetProgressMessage("Reading scan from " + fileName + " ...");
                return(new SrmScanFileFormat().ReadFromFile(fileName));
            }
            else
            {
                List <PeakList <Peak> > pkls = new List <PeakList <Peak> >();

                SetProgressMessage("Reading scan from " + directory + " ...");
                IRawFile reader = RawFileFactory.GetRawFileReader(directory);
                try
                {
                    var firstCount = reader.GetFirstSpectrumNumber();
                    var lastCount  = reader.GetLastSpectrumNumber();

                    Progress.SetRange(firstCount, lastCount);
                    Progress.SetPosition(firstCount);
                    for (int i = firstCount; i <= lastCount; i++)
                    {
                        if (reader.GetMsLevel(i) != 2)
                        {
                            continue;
                        }

                        if (Progress.IsCancellationPending())
                        {
                            throw new UserTerminatedException();
                        }

                        Progress.Increment(1);

                        var pkl       = reader.GetPeakList(i);
                        var precursor = reader.GetPrecursorPeak(i);
                        if (precursor != null)
                        {
                            pkl.PrecursorMZ     = precursor.Mz;
                            pkl.PrecursorCharge = precursor.Charge;
                        }
                        pkl.ScanTimes.Add(new ScanTime(i, reader.ScanToRetentionTime(i)));

                        pkls.Add(pkl);
                    }

                    List <SrmScan> result = new List <SrmScan>();

                    pkls.ForEach(m => m.ForEach(n => result.Add(new SrmScan(m.PrecursorMZ, n.Mz, m.ScanTimes[0].RetentionTime, n.Intensity, true))));

                    new SrmScanFileFormat().WriteToFile(fileName, result);

                    SetProgressMessage("finished.");

                    return(result);
                }
                finally
                {
                    reader.Close();
                }
            }
        }
Esempio n. 16
0
        public override IEnumerable <string> Process(string targetDir)
        {
            foreach (var raw in rawFiles)
            {
                List <SimplePeakChro> waitingPeaks = new List <SimplePeakChro>();
                foreach (var peak in targetPeaks)
                {
                    string file = GetTargetFile(targetDir, raw, peak);
                    if (force || !File.Exists(file))
                    {
                        waitingPeaks.Add(peak);
                    }
                    else
                    {
                        var p = new SimplePeakChroXmlFormat().ReadFromFile(file);
                        peak.MaxRetentionTime = p.MaxRetentionTime;
                        peak.Peaks            = p.Peaks;
                    }
                }

                if (waitingPeaks.Count == 0)
                {
                    continue;
                }

                rawReader.Open(raw);
                try
                {
                    int firstScan = rawReader.GetFirstSpectrumNumber();
                    int lastScan  = rawReader.GetLastSpectrumNumber();

                    var lastRt = rawReader.ScanToRetentionTime(lastScan);

                    waitingPeaks.ForEach(m =>
                    {
                        m.Peaks.Clear();
                        m.MaxRetentionTime = lastRt;
                    });

                    Progress.SetMessage("Processing chromotograph extracting...");
                    Progress.SetRange(firstScan, lastScan);
                    for (int scan = firstScan; scan <= lastScan; scan++)
                    {
                        if (rawReader.GetMsLevel(scan) != 1)
                        {
                            continue;
                        }
                        Progress.SetPosition(scan);

                        if (Progress.IsCancellationPending())
                        {
                            throw new UserTerminatedException();
                        }

                        PeakList <Peak> pkl = rawReader.GetPeakList(scan);
                        double          rt  = rawReader.ScanToRetentionTime(scan);

                        foreach (var peak in waitingPeaks)
                        {
                            var env = pkl.FindPeak(peak.Mz, peak.MzTolerance);

                            Peak findPeak = new Peak(peak.Mz, 0.0, 0);
                            if (env.Count > 0)
                            {
                                if (env.Count == 1)
                                {
                                    findPeak = env[0];
                                }
                                else
                                {
                                    var charge = env.FindCharge(peak.Charge);
                                    if (charge.Count > 0)
                                    {
                                        if (charge.Count == 1)
                                        {
                                            findPeak = charge[0];
                                        }
                                        else
                                        {
                                            findPeak = charge.FindMaxIntensityPeak();
                                        }
                                    }
                                    else
                                    {
                                        findPeak = env.FindMaxIntensityPeak();
                                    }
                                }
                            }

                            peak.Peaks.Add(new ScanPeak()
                            {
                                Mz            = findPeak.Mz,
                                Intensity     = findPeak.Intensity,
                                Scan          = scan,
                                RetentionTime = rt,
                                Charge        = findPeak.Charge,
                                PPMDistance   = PrecursorUtils.mz2ppm(peak.Mz, findPeak.Mz - peak.Mz)
                            });
                        }
                    }

                    waitingPeaks.ForEach(m => m.TrimPeaks(minRetentionTime));
                }
                finally
                {
                    rawReader.Close();
                }

                Progress.SetMessage("Saving ... ");
                foreach (var peak in waitingPeaks)
                {
                    string file = GetTargetFile(targetDir, raw, peak);
                    new SimplePeakChroXmlFormat().WriteToFile(file, peak);
                }
                Progress.SetMessage("Finished.");
                Progress.End();
            }

            return(new string[] { targetDir });
        }
Esempio n. 17
0
        public void Quantify(string rawFileName, List <IIdentifiedSpectrum> spectra, string detailDir)
        {
            if (!Directory.Exists(detailDir))
            {
                Directory.CreateDirectory(detailDir);
            }

            var experimental = RawFileFactory.GetExperimental(rawFileName);

            Dictionary <string, DifferentRetentionTimeEnvelopes> spectrumKeyMap = new Dictionary <string, DifferentRetentionTimeEnvelopes>();

            Dictionary <SilacEnvelopes, List <IIdentifiedSpectrum> > envelopeSpectrumGroup = new Dictionary <SilacEnvelopes, List <IIdentifiedSpectrum> >();

            double precursorPPM = GetPrecursorPPM(spectra);

            try
            {
                _rawReader.Open(rawFileName);

                int firstScanNumber = _rawReader.GetFirstSpectrumNumber();
                int lastScanNumber  = _rawReader.GetLastSpectrumNumber();

                Progress.SetRange(1, spectra.Count);
                int pepCount = 0;

                for (int s = 0; s < spectra.Count; s++)
                {
                    Console.WriteLine(s);
                    IIdentifiedSpectrum spectrum = spectra[s];

                    SilacQuantificationSummaryItem.ClearAnnotation(spectrum);

                    if (Progress.IsCancellationPending())
                    {
                        throw new UserTerminatedException();
                    }

                    int startScan = spectrum.Query.FileScan.FirstScan;
                    if (startScan > lastScanNumber)
                    {
                        spectrum.GetOrCreateQuantificationItem().RatioStr = "OUT_OF_RANGE";
                        continue;
                    }

                    Progress.SetPosition(pepCount++);

                    IIdentifiedPeptide sp = spectrum.Peptide;

                    string seq = GetMatchSequence(spectrum);

                    IPeptideInfo peptideInfo = new IdentifiedPeptideInfo(seq, spectrum.TheoreticalMH, spectrum.Query.Charge);

                    SilacCompoundInfo sci = GetSilacCompoundInfo(peptideInfo);

                    //如果轻重离子理论质荷比一样,忽略
                    if (!sci.IsSilacData())
                    {
                        spectrum.GetOrCreateQuantificationItem().RatioStr = "NOT_SILAC";
                        continue;
                    }

                    //如果轻重离子理论质荷比与观测值不一致,忽略
                    if (!sci.IsMzEquals(spectrum.ObservedMz, MAX_DELTA_MZ))
                    {
                        ValidateModifications(seq);

                        spectrum.GetOrCreateQuantificationItem().RatioStr = "WRONG_IDENTIFICATION";
                        continue;
                    }

                    //如果没有找到相应的FullScan,忽略
                    int identifiedFullScan = _rawReader.FindPreviousFullScan(startScan, firstScanNumber);
                    if (-1 == identifiedFullScan)
                    {
                        spectrum.GetOrCreateQuantificationItem().RatioStr = "NO_PROFILE";
                        continue;
                    }

                    DifferentRetentionTimeEnvelopes pkls = FindEnvelopes(spectrumKeyMap, spectrum, sci);

                    SilacEnvelopes envelope = pkls.FindSilacEnvelope(identifiedFullScan);

                    //如果该scan被包含在已经被定量的结果中,忽略
                    if (envelope != null)
                    {
                        envelope.SetScanIdentified(identifiedFullScan, spectrum.IsExtendedIdentification());
                        envelopeSpectrumGroup[envelope].Add(spectrum);
                        continue;
                    }

                    //从原始文件中找出该spectrum的定量信息
                    int maxIndex = Math.Min(option.ProfileLength - 1, pkls.LightProfile.FindMaxIndex());

                    double mzTolerance = PrecursorUtils.ppm2mz(sci.Light.Mz, option.PPMTolerance);

                    //如果FullScan没有相应的离子,忽略。(鉴定错误或者扩展定量时候,会出现找不到pair的现象)
                    SilacPeakListPair splp = GetLightHeavyPeakList(_rawReader, sci, maxIndex, mzTolerance, identifiedFullScan);
                    if (null == splp)
                    {
                        spectrum.GetOrCreateQuantificationItem().RatioStr = "NO_PROFILE";
                        continue;
                    }

                    splp.IsIdentified             = true;
                    splp.IsExtendedIdentification = spectrum.IsExtendedIdentification();

                    SilacEnvelopes envelopes = new SilacEnvelopes();
                    envelopes.Add(splp);

                    //向前查找定量信息
                    int fullScan   = identifiedFullScan;
                    int scanNumber = 0;
                    while ((fullScan = _rawReader.FindPreviousFullScan(fullScan - 1, firstScanNumber)) != -1)
                    {
                        if (_rawReader.IsBadDataScan(fullScan))
                        {
                            continue;
                        }
                        scanNumber++;
                        var item = GetLightHeavyPeakList(_rawReader, sci, maxIndex, mzTolerance, fullScan, scanNumber <= MinScanNumber);
                        if (null == item)
                        {
                            break;
                        }

                        envelopes.Add(item);
                    }
                    envelopes.Reverse();

                    //向后查找定量信息
                    fullScan   = identifiedFullScan;
                    scanNumber = 0;
                    while ((fullScan = _rawReader.FindNextFullScan(fullScan + 1, lastScanNumber)) != -1)
                    {
                        if (_rawReader.IsBadDataScan(fullScan))
                        {
                            continue;
                        }
                        scanNumber++;
                        var item = GetLightHeavyPeakList(_rawReader, sci, maxIndex, mzTolerance, fullScan, scanNumber <= MinScanNumber);
                        if (null == item)
                        {
                            break;
                        }

                        envelopes.Add(item);
                    }

                    //对每个scan计算轻重的离子丰度
                    envelopes.ForEach(m => m.CalculateIntensity(pkls.LightProfile, pkls.HeavyProfile));

                    pkls.Add(envelopes);

                    envelopeSpectrumGroup.Add(envelopes, new List <IIdentifiedSpectrum>());
                    envelopeSpectrumGroup[envelopes].Add(spectrum);
                }
            }
            finally
            {
                _rawReader.Close();
            }

            foreach (string key in spectrumKeyMap.Keys)
            {
                DifferentRetentionTimeEnvelopes pkls = spectrumKeyMap[key];

                foreach (SilacEnvelopes envelopes in pkls)
                {
                    if (0 == envelopes.Count)
                    {
                        continue;
                    }

                    List <IIdentifiedSpectrum> mps = envelopeSpectrumGroup[envelopes];

                    double mzTolerance = PrecursorUtils.ppm2mz(mps[0].Query.ObservedMz, option.PPMTolerance);

                    string scanStr = GetScanRange(envelopes);

                    string resultFilename = detailDir + "\\" + mps[0].Query.FileScan.Experimental + "." + PeptideUtils.GetPureSequence(mps[0].Sequence) + "." + mps[0].Query.Charge + scanStr + ".silac";

                    IPeptideInfo      peptideInfo = new IdentifiedPeptideInfo(mps[0].GetMatchSequence(), mps[0].TheoreticalMH, mps[0].Query.Charge);
                    SilacCompoundInfo sci         = GetSilacCompoundInfo(peptideInfo);

                    SilacQuantificationSummaryItem item = new SilacQuantificationSummaryItem(sci.Light.IsSample);
                    item.RawFilename          = rawFileName;
                    item.SoftwareVersion      = this.SoftwareVersion;
                    item.PeptideSequence      = mps[0].Sequence;
                    item.Charge               = mps[0].Charge;
                    item.LightAtomComposition = sci.Light.Composition.ToString();
                    item.HeavyAtomComposition = sci.Heavy.Composition.ToString();
                    item.LightProfile         = pkls.LightProfile;
                    item.HeavyProfile         = pkls.HeavyProfile;
                    item.ObservedEnvelopes    = envelopes;

                    item.ValidateScans(sci, precursorPPM);

                    item.Smoothing();
                    item.CalculateRatio();

                    new SilacQuantificationSummaryItemXmlFormat().WriteToFile(resultFilename, item);

                    int maxScoreItemIndex = FindMaxScoreItemIndex(mps);

                    for (int i = 0; i < mps.Count; i++)
                    {
                        if (maxScoreItemIndex == i)
                        {
                            item.AssignToAnnotation(mps[i], resultFilename);
                        }
                        else
                        {
                            item.AssignDuplicationToAnnotation(mps[i], resultFilename);
                        }
                    }
                }
            }

            foreach (IIdentifiedSpectrum mph in spectra)
            {
                mph.InitializeRatioEnabled();
            }
        }
Esempio n. 18
0
        public List <PrecursorItem> QueryPrecursorFromProductIon(QueryItem productIon, double ppmProductTolerance, double ppmPrecursorTolerance)
        {
            List <PrecursorItem> result = new List <PrecursorItem>();

            int firstScan = reader.GetFirstSpectrumNumber();
            int lastScan  = reader.GetLastSpectrumNumber();

            double mzTolerance = PrecursorUtils.ppm2mz(productIon.ProductIonMz, ppmProductTolerance);

            PeakList <Peak> lastFullScan = new PeakList <Peak>();

            Progress.SetRange(firstScan, lastScan);
            for (int scan = firstScan; scan <= lastScan; scan++)
            {
                Progress.SetPosition(scan);

                if (Progress.IsCancellationPending())
                {
                    throw new UserTerminatedException();
                }

                //ignore ms 1
                if (reader.GetMsLevel(scan) == 1)
                {
                    lastFullScan = ReadScan(scan);
                    continue;
                }

                //read all peaks
                PeakList <Peak> pkl = ReadScan(scan);

                //find product ions
                PeakList <Peak> productIons = pkl.FindPeak(productIon.ProductIonMz, mzTolerance);

                if (productIons.Count == 0)
                {
                    continue;
                }

                //get minimum product ion intensity
                double maxIntensity      = pkl.FindMaxIntensityPeak().Intensity;
                double relativeIntensity = productIons.FindMaxIntensityPeak().Intensity / maxIntensity;
                if (relativeIntensity < productIon.MinRelativeIntensity)
                {
                    continue;
                }

                Peak peak = reader.GetPrecursorPeak(scan);

                double precursorMzTolerance = PrecursorUtils.ppm2mz(peak.Mz, ppmPrecursorTolerance);

                var precursorPkl = lastFullScan.FindPeak(peak.Mz, precursorMzTolerance);

                bool isotopic = false;
                if (precursorPkl.Count == 0)
                {
                    isotopic     = true;
                    precursorPkl = lastFullScan.FindPeak(peak.Mz - 1.0, precursorMzTolerance);
                }

                if (precursorPkl.Count == 0)
                {
                    precursorPkl = lastFullScan.FindPeak(peak.Mz + 1.0, precursorMzTolerance);
                }

                var precursorInFullMs = precursorPkl.FindMaxIntensityPeak();

                var precursor = new PrecursorItem()
                {
                    Scan = scan,
                    ProductIonRelativeIntensity = relativeIntensity
                };

                if (isotopic && precursorInFullMs != null)
                {
                    precursor.PrecursorMZ = precursorInFullMs.Mz;
                    precursor.IsIsotopic  = true;
                }
                else
                {
                    precursor.PrecursorMZ = peak.Mz;
                    precursor.IsIsotopic  = false;
                }

                if (precursorInFullMs != null)
                {
                    precursor.PrecursorIntensity = precursorInFullMs.Intensity;
                }

                result.Add(precursor);
            }

            var precursorMzs = from item in result
                               group item by item.PrecursorMZ into mzGroup
                               let mzcount = mzGroup.Where(m => m.PrecursorIntensity > 0).Count()
                                             orderby mzcount descending
                                             select mzGroup.Key;

            foreach (var mz in precursorMzs)
            {
                double mzPrecursorTolerance = PrecursorUtils.ppm2mz(mz, ppmPrecursorTolerance);
                foreach (var item in result)
                {
                    if (!item.IsIsotopic)
                    {
                        continue;
                    }

                    if (Math.Abs(item.PrecursorMZ - mz) <= mzPrecursorTolerance)
                    {
                        item.PrecursorMZ = mz;
                    }
                }
            }

            return(result);
        }