protected override IFileProcessor GetFileProcessor()
        {
            O18QuantificationFileProcessorOptions options = new O18QuantificationFileProcessorOptions();
              options.PPMTolerance = precursorPPMTolerance.Value;
              options.PurityOfO18Water = purityOfWater.Value;
              options.ProteinFile = base.GetOriginFile();
              if (rawDir.FullName == "")
              {
            options.RawDirectory = new FileInfo(options.ProteinFile).DirectoryName;
              }
              else
              {
            options.RawDirectory = rawDir.FullName;
              }
              options.RawExtension = rawExtensions.SelectedItem;

              options.SoftwareVersion = this.Text;
              options.IsPostDigestionLabelling = postDigestionLabelling.Checked;
              options.IsScanLimited = limitScanRange.Checked;
              if (options.IsScanLimited)
              {
            options.ScanPercentageStart = scanStart.Value;
            options.ScanPercentageEnd = scanEnd.Value;
              }
              options.Save(this.GetOriginFile());

              return new O18QuantificationFileProcessor();
        }
        public static O18QuantificationFileProcessorOptions Load(string fileName)
        {
            O18QuantificationFileProcessorOptions result = new O18QuantificationFileProcessorOptions();

            RcpaOptionUtils.LoadFromXml(result, fileName);
            return(result);
        }
Example #3
0
        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 });
        }