Ejemplo n.º 1
0
        private bool AcceptScan(SilacPeakListPair pair, SilacCompoundInfo sci, int lightMaxIndex, double lightMinPPM, double lightMaxPPM, int heavyMaxIndex, double heavyMinPPM, double heavyMaxPPM)
        {
            //如果轻重标中任意一个最高峰不存在,循环结束。
            if (pair.Light[lightMaxIndex].Intensity == 0 || pair.Heavy[heavyMaxIndex].Intensity == 0)
            {
                return(false);
            }

            //轻标最高峰的ppm
            double lightPPM = PrecursorUtils.mz2ppm(pair.Light[lightMaxIndex].Mz, sci.Light.Profile[lightMaxIndex].Mz - pair.Light[lightMaxIndex].Mz);

            if (lightPPM < lightMinPPM || lightPPM > lightMaxPPM)
            {
                return(false);
            }

            //重标最高峰的ppm
            double heavyPPM = PrecursorUtils.mz2ppm(pair.Heavy[heavyMaxIndex].Mz, sci.Heavy.Profile[heavyMaxIndex].Mz - pair.Heavy[heavyMaxIndex].Mz);

            if (heavyPPM < heavyMinPPM || heavyPPM > heavyMaxPPM)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
0
        public SilacQuantificationSummaryItem ReadFromFile(string filename)
        {
            XElement root = XElement.Load(filename);

            var result = new SilacQuantificationSummaryItem(root.GetChildValue("SampleIsLight", true));

            result.RawFilename              = root.GetChildValue("RawFilename", "");
            result.SoftwareVersion          = root.GetChildValue("SoftwareVersion", "");
            result.PeptideSequence          = root.GetChildValue("PeptideSequence");
            result.Charge                   = root.GetChildValue("Charge", 2);
            result.SampleAtomComposition    = root.GetChildValue("SampleAtomComposition");
            result.ReferenceAtomComposition = root.GetChildValue("ReferenceAtomComposition");
            result.Ratio = root.GetChildValue("Ratio", 0.0);
            result.RegressionCorrelation = root.GetChildValue("Correlation", 0.0);
            result.SampleAbundance       = root.GetChildValue("SampleAbundance", 0.0);
            result.ReferenceAbundance    = root.GetChildValue("ReferenceAbundance", 0.0);

            result.SampleProfile = new List <Peak>();
            ReadProfile(root, "SampleProfile", result.SampleProfile);

            result.ReferenceProfile = new List <Peak>();
            ReadProfile(root, "ReferenceProfile", result.ReferenceProfile);

            var observed = root.Element("ObservedEnvelopes");

            result.ObservedEnvelopes = new SilacEnvelopes();
            foreach (var pkl in observed.Elements())
            {
                var    peakList      = new SilacPeakListPair();
                int    scan          = Convert.ToInt32(pkl.Attribute("Scan").Value);
                double retentiontime = MyConvert.ToDouble(pkl.Attribute("Retentiontime").Value);
                peakList.Enabled      = Convert.ToBoolean(pkl.Attribute("Enabled").Value);
                peakList.IsIdentified = Convert.ToBoolean(pkl.Attribute("Identified").Value);

                var extended = pkl.Attribute("ExtendedIdentification");
                if (extended != null)
                {
                    peakList.IsExtendedIdentification = Convert.ToBoolean(pkl.Attribute("ExtendedIdentification").Value);
                }
                else
                {
                    peakList.IsExtendedIdentification = false;
                }

                peakList.LightIntensity = MyConvert.ToDouble(pkl.Attribute("LightIntensity").Value);
                peakList.HeavyIntensity = MyConvert.ToDouble(pkl.Attribute("HeavyIntensity").Value);

                peakList.Light = new PeakList <Peak>();
                peakList.Light.ScanTimes.Add(new ScanTime(scan, retentiontime));

                peakList.Heavy = new PeakList <Peak>();
                peakList.Heavy.ScanTimes.Add(new ScanTime(scan, retentiontime));

                ReadPeakList(pkl, "LightPeakList", peakList.Light);
                ReadPeakList(pkl, "HeavyPeakList", peakList.Heavy);

                result.ObservedEnvelopes.Add(peakList);
            }

            if (result.LightProfile[0].Mz == 0.0)
            {
                EmassProfileBuilder builder = new EmassProfileBuilder();

                double minPercentage = 0.0001;

                AtomComposition acSample = new AtomComposition(result.SampleAtomComposition);
                result.SampleProfile = builder.GetProfile(acSample, result.Charge, minPercentage);

                AtomComposition acRef = new AtomComposition(result.ReferenceAtomComposition);
                result.ReferenceProfile = builder.GetProfile(acRef, result.Charge, minPercentage);
            }

            result.CalculateCorrelation();

            return(result);
        }
        public SilacQuantificationSummaryItem ReadFromFile(string filename)
        {
            var doc = new XmlDocument();

            doc.Load(filename);

            XmlNode root      = doc.DocumentElement;
            var     xmlHelper = new XmlHelper(doc);

            var result = new SilacQuantificationSummaryItem(bool.Parse(xmlHelper.GetValidChild(root, "SampleIsLight").InnerText));

            result.RawFilename = xmlHelper.GetValidChild(root, "RawFilename").InnerText;

            XmlNode n = xmlHelper.GetFirstChild(root, "SoftwareVersion");

            if (n != null)
            {
                result.SoftwareVersion = n.InnerText;
            }

            result.PeptideSequence          = xmlHelper.GetValidChild(root, "PeptideSequence").InnerText;
            result.Charge                   = Convert.ToInt32(xmlHelper.GetValidChild(root, "Charge").InnerText);
            result.SampleAtomComposition    = xmlHelper.GetValidChild(root, "SampleAtomComposition").InnerText;
            result.ReferenceAtomComposition = xmlHelper.GetValidChild(root, "ReferenceAtomComposition").InnerText;
            result.Ratio = MyConvert.ToDouble(xmlHelper.GetValidChild(root, "Ratio").InnerText);
            result.RegressionCorrelation = MyConvert.ToDouble(xmlHelper.GetValidChild(root, "Correlation").InnerText);
            result.SampleAbundance       = MyConvert.ToDouble(xmlHelper.GetValidChild(root, "SampleAbundance").InnerText);
            result.ReferenceAbundance    = MyConvert.ToDouble(xmlHelper.GetValidChild(root, "ReferenceAbundance").InnerText);

            result.SampleProfile = new List <Peak>();
            ReadProfile(xmlHelper, root, "SampleProfile", result.SampleProfile);

            result.ReferenceProfile = new List <Peak>();
            ReadProfile(xmlHelper, root, "ReferenceProfile", result.ReferenceProfile);

            XmlNode observed = xmlHelper.GetValidChild(root, "ObservedEnvelopes");

            result.ObservedEnvelopes = new SilacEnvelopes();
            foreach (XmlNode pkl in observed.ChildNodes)
            {
                var    peakList      = new SilacPeakListPair();
                int    scan          = int.Parse(pkl.Attributes["Scan"].InnerText);
                double retentiontime = MyConvert.ToDouble(pkl.Attributes["Retentiontime"].InnerText);
                peakList.Enabled      = bool.Parse(pkl.Attributes["Enabled"].InnerText);
                peakList.IsIdentified = bool.Parse(pkl.Attributes["Identified"].InnerText);

                var extended = pkl.Attributes["ExtendedIdentification"];
                if (extended != null)
                {
                    peakList.IsExtendedIdentification = bool.Parse(pkl.Attributes["ExtendedIdentification"].InnerText);
                }
                else
                {
                    peakList.IsExtendedIdentification = false;
                }

                peakList.LightIntensity = MyConvert.ToDouble(pkl.Attributes["LightIntensity"].InnerText);
                peakList.HeavyIntensity = MyConvert.ToDouble(pkl.Attributes["HeavyIntensity"].InnerText);

                peakList.Light = new PeakList <Peak>();
                peakList.Light.ScanTimes.Add(new ScanTime(scan, retentiontime));

                peakList.Heavy = new PeakList <Peak>();
                peakList.Heavy.ScanTimes.Add(new ScanTime(scan, retentiontime));

                ReadPeakList(xmlHelper, pkl, "LightPeakList", peakList.Light);
                ReadPeakList(xmlHelper, pkl, "HeavyPeakList", peakList.Heavy);

                result.ObservedEnvelopes.Add(peakList);
            }

            result.CalculateCorrelation();

            return(result);
        }