public AbstractO18ProteinRatioRCalculatorPCA(IGetRatioIntensity intensityFunc, IO18QuantificationOption option)
 {
     this.intensityFunc = intensityFunc;
     this.Option        = option;
     this.rExecute      = ExternalProgramConfig.GetExternalProgram("R");
     if (this.rExecute == null)
     {
         throw new Exception("Define R location first!");
     }
 }
        public string GetRCommand()
        {
            var result = ExternalProgramConfig.GetExternalProgram("R");

            if (result == null)
            {
                throw new Exception("Define R location first!");
            }

            return(result);
        }
コード例 #3
0
        public RTemplateProcessor(IRTemplateProcessorOptions options)
        {
            this.options = options;

            if (string.IsNullOrEmpty(options.RExecute))
            {
                options.RExecute = ExternalProgramConfig.GetExternalProgram("R");
            }

            if (string.IsNullOrEmpty(options.RExecute))
            {
                throw new Exception("Define R location first!");
            }
        }
        private void WriteMap(Dictionary <int, int> scanCounts, List <int> keys, string filename, Dictionary <int, Dictionary <int, List <PeakEntry> > > curMaps, bool exportIndividualIon)
        {
            foreach (var key in keys)
            {
                var    totalCount = scanCounts[key];
                string subfile    = string.Empty;

                if (key == FULLMS_CHARGE)
                {
                    subfile = filename + ".fullms";
                }
                else if (key == 0)
                {
                    subfile = filename + ".unknown";
                }
                else
                {
                    subfile = filename + ".ms2charge" + key.ToString();
                }
                var map = curMaps[key];

                foreach (var e in map.Values)
                {
                    MergeIons(e);
                }

                var ens  = (from e in map.Values from en in e select en).ToList();
                var map2 = ens.GroupBy(m => (int)Math.Round(m.Ion.Mz + 0.5)).ToDictionary(m => m.Key, m => m.ToList());
                foreach (var e in map2.Values)
                {
                    MergeIons(e);
                }

                ens = (from e in map2.Values from en in e select en).ToList();

                //remove the duplication
                foreach (var ee in ens)
                {
                    ee.Intensities = (from intt in ee.Intensities.GroupBy(m => m.Scan)
                                      select(from inttt in intt
                                             orderby inttt.Intensity descending
                                             select inttt).First()).ToList();
                }

                using (var sw2 = new StreamWriter(subfile))
                {
                    sw2.WriteLine("Ion\tCount\tFrequency\tMeanIntensity\tSD\tMedianIntensity");

                    var totalentries = (from en in ens
                                        orderby en.Intensities.Count descending
                                        select en).ToList();

                    totalentries.ForEach(m =>
                    {
                        var ints   = (from i in m.Intensities select i.Intensity).ToArray();
                        var mean   = Statistics.Mean(ints);
                        var sd     = Statistics.StandardDeviation(ints);
                        var median = Statistics.Median(ints);

                        sw2.WriteLine("{0:0.0000}\t{1}\t{2:0.0000}\t{3:0.000}\t{4:0.000}\t{5:0.000}", m.Ion.Mz, m.Intensities.Count, m.Intensities.Count * 1.0 / totalCount, mean, sd, median);
                    });
                    sw2.WriteLine();
                }

                var options = new RTemplateProcessorOptions();

                options.InputFile  = subfile;
                options.OutputFile = subfile + ".sig.tsv";
                options.RExecute   = ExternalProgramConfig.GetExternalProgram("R");
                options.RTemplate  = FileUtils.GetTemplateDir() + "/DetectSignificantIon.r";
                options.Parameters.Add("minfreq<-0.01");
                options.Parameters.Add("probability<-0.95");
                options.Parameters.Add("minMedianIntensity<-0.05");

                new RTemplateProcessor(options)
                {
                    Progress = this.Progress
                }.Process();
            }
        }
コード例 #5
0
 protected override void ValidateComponents()
 {
     base.ValidateComponents();
     ExternalProgramConfig.GetExternalProgram("R");
 }
        public override IEnumerable <string> Process()
        {
            var boundaryInput = Path.ChangeExtension(options.OutputFile, ".chros.tsv");

            if (!File.Exists(boundaryInput) || options.Overwrite)
            {
                var format           = GetPeptideReader();
                var spectra          = format.ReadFromFile(options.InputFile);
                var peptideMap       = spectra.ToGroupDictionary(m => m.Query.FileScan.Experimental.ToLower());
                var rawfiles         = options.RawFiles.ToDictionary(m => RawFileFactory.GetExperimental(m).ToLower());
                var rententionWindow = options.MaximumRetentionTimeWindow;

                var missed = peptideMap.Keys.Except(rawfiles.Keys).ToArray();
                if (missed.Length > 0)
                {
                    throw new Exception(string.Format("Cannot find raw file of {0} in file list", missed.Merge("/")));
                }

                var optionThreadCount = options.ThreadCount == 0 ? Environment.ProcessorCount : options.ThreadCount;
                var option            = new ParallelOptions()
                {
                    MaxDegreeOfParallelism = Math.Min(optionThreadCount, peptideMap.Count),
                };

                var chroMap = new List <Tuple <string, List <ChromatographProfile> > >();
                foreach (var raw in peptideMap)
                {
                    var peptides = raw.Value;

                    var waitingPeaks = new List <ChromatographProfile>();
                    foreach (var peptide in peptides)
                    {
                        var chro = new ChromatographProfile()
                        {
                            Experimental            = peptide.Query.FileScan.Experimental,
                            IdentifiedScan          = peptide.Query.FileScan.FirstScan,
                            IdentifiedRetentionTime = peptide.Query.FileScan.RetentionTime,
                            ObservedMz    = peptide.GetPrecursorMz(),
                            TheoreticalMz = peptide.GetTheoreticalMz(),
                            Charge        = peptide.Query.Charge,
                            Sequence      = peptide.Peptide.PureSequence,
                            FileName      = GetTargetFile(peptide),
                            SubFileName   = GetTargetSubFile(peptide)
                        };
                        chro.InitializeIsotopicIons(options.MzTolerancePPM, options.MinimumIsotopicPercentage);
                        waitingPeaks.Add(chro);
                    }

                    chroMap.Add(new Tuple <string, List <ChromatographProfile> >(raw.Key, waitingPeaks));
                }

                ConcurrentBag <ChromatographProfile> detected = new ConcurrentBag <ChromatographProfile>();

                Parallel.ForEach(chroMap, option, raw =>
                {
                    var rawFileName  = raw.Item1;
                    var waitingPeaks = raw.Item2;

                    Dictionary <string, List <ChromatographProfile> > resultMap = new Dictionary <string, List <ChromatographProfile> >();

                    List <FullMS> fullMSList = new List <FullMS>();
                    Progress.SetMessage("Reading full ms list from " + rawfiles[rawFileName] + "...");
                    using (var rawReader = new CacheRawFile(RawFileFactory.GetRawFileReader(rawfiles[rawFileName])))
                    {
                        var firstScan = rawReader.GetFirstSpectrumNumber();
                        var lastScan  = rawReader.GetLastSpectrumNumber();
                        for (int scan = firstScan; scan <= lastScan; scan++)
                        {
                            var mslevel = rawReader.GetMsLevel(scan);
                            if (mslevel == 1)
                            {
                                fullMSList.Add(new FullMS()
                                {
                                    Scan          = scan,
                                    RetentionTime = rawReader.ScanToRetentionTime(scan),
                                    Peaks         = null
                                });
                            }
                        }

                        foreach (var chro in waitingPeaks)
                        {
                            if (chro.IdentifiedScan == 0 && chro.IdentifiedRetentionTime > 0)
                            {
                                for (int i = 1; i < fullMSList.Count; i++)
                                {
                                    if (chro.IdentifiedRetentionTime < fullMSList[i].RetentionTime)
                                    {
                                        break;
                                    }
                                    chro.IdentifiedScan = fullMSList[i].Scan + 1;
                                }
                            }
                        }

                        var chroGroups = waitingPeaks.GroupBy(chro => chro.GetPeptideId());
                        foreach (var chroGroup in chroGroups)
                        {
                            List <ChromatographProfile> profileChros = new List <ChromatographProfile>();
                            foreach (var chro in chroGroup.OrderBy(m => m.IdentifiedScan))
                            {
                                var masterScanIndex = 0;
                                for (int i = 1; i < fullMSList.Count; i++)
                                {
                                    if (chro.IdentifiedScan < fullMSList[i].Scan)
                                    {
                                        break;
                                    }
                                    masterScanIndex = i;
                                }
                                var masterScan          = fullMSList[masterScanIndex].Scan;
                                var masterRetentionTime = fullMSList[masterScanIndex].RetentionTime;

                                bool bExist = false;
                                foreach (var profileChro in profileChros)
                                {
                                    foreach (var pkl in profileChro.Profiles)
                                    {
                                        if (pkl.Scan == fullMSList[masterScanIndex].Scan)
                                        {
                                            pkl.Identified = true;
                                            bExist         = true;
                                            break;
                                        }
                                    }

                                    if (bExist)
                                    {
                                        break;
                                    }
                                }

                                if (bExist)
                                {
                                    continue;
                                }

                                //Progress.SetMessage("Processing {0} : {1:0.#####} : {2} : {3}", chro.Sequence, chro.ObservedMz, chro.IdentifiedScan, Path.GetFileName(chro.FileName));

                                //allow one missed scan
                                int naCount = 2;
                                for (int scanIndex = masterScanIndex; scanIndex >= 0; scanIndex--)
                                {
                                    if (Progress.IsCancellationPending())
                                    {
                                        throw new UserTerminatedException();
                                    }

                                    var curRetentionTime = fullMSList[scanIndex].RetentionTime;
                                    if (masterRetentionTime - curRetentionTime > rententionWindow)
                                    {
                                        break;
                                    }

                                    if (!AddEnvelope(chro, rawReader, fullMSList, scanIndex))
                                    {
                                        naCount--;
                                        if (naCount == 0)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }

                                    if (scanIndex == masterScanIndex)
                                    {
                                        chro.Profiles.Last().Identified = true;
                                    }
                                }
                                chro.Profiles.Reverse();

                                naCount = 2;
                                for (int scanIndex = masterScanIndex + 1; scanIndex < fullMSList.Count; scanIndex++)
                                {
                                    if (Progress.IsCancellationPending())
                                    {
                                        throw new UserTerminatedException();
                                    }

                                    var curRetentionTime = fullMSList[scanIndex].RetentionTime;
                                    if (curRetentionTime - masterRetentionTime > rententionWindow)
                                    {
                                        break;
                                    }

                                    if (!AddEnvelope(chro, rawReader, fullMSList, scanIndex))
                                    {
                                        naCount--;
                                        if (naCount == 0)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                }

                                profileChros.Add(chro);
                            }

                            profileChros.RemoveAll(l => l.Profiles.Count < options.MinimumScanCount);
                            profileChros.Sort((m1, m2) => m2.Profiles.Count.CompareTo(m1.Profiles.Count));

                            bool bMain = true;
                            foreach (var chro in profileChros)
                            {
                                var filename = bMain ? chro.FileName : chro.SubFileName;
                                if (bMain)
                                {
                                    detected.Add(chro);
                                }

                                bMain = false;

                                new ChromatographProfileTextWriter().WriteToFile(filename, chro);
                                new ChromatographProfileXmlFormat().WriteToFile(filename + ".xml", chro);
                            }
                        }
                    }
                }
                                 );

                var chroList = new List <ChromatographProfile>(detected);
                chroList.Sort((m1, m2) => m1.FileName.CompareTo(m2.FileName));

                if (chroList.Count == 0)
                {
                    throw new Exception("Cannot find chromotograph!");
                }

                using (var sw = new StreamWriter(boundaryInput))
                {
                    sw.WriteLine("ChroDirectory\tChroFile\tSample\tPeptideId\tTheoreticalMz\tCharge\tIdentifiedScan");
                    foreach (var chro in chroList)
                    {
                        sw.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
                                     Path.GetDirectoryName(chro.FileName).Replace("\\", "/"),
                                     Path.GetFileNameWithoutExtension(chro.FileName),
                                     chro.Experimental,
                                     chro.GetPeptideId(),
                                     chro.TheoreticalMz,
                                     chro.Charge,
                                     chro.IdentifiedScan);
                    }
                }
            }

            if (!File.Exists(options.OutputFile) || options.Overwrite)
            {
                Progress.SetMessage("Finding boundaries ...");
                var boundaryOptions = new RTemplateProcessorOptions()
                {
                    InputFile      = boundaryInput,
                    OutputFile     = options.OutputFile,
                    RTemplate      = BoundaryR,
                    RExecute       = ExternalProgramConfig.GetExternalProgram("R"),
                    CreateNoWindow = true
                };
                boundaryOptions.Parameters.Add("outputImage<-" + (options.DrawImage ? "1" : "0"));
                boundaryOptions.Parameters.Add("maximumProfileDistance<-" + options.MaximumProfileDistance.ToString());
                new RTemplateProcessor(boundaryOptions)
                {
                    Progress = this.Progress
                }.Process();
            }

            //if (options.DrawImage)
            //{
            //  Progress.SetMessage("Drawing images ...");

            //  var imageOptions = new RTemplateProcessorOptions()
            //  {
            //    InputFile = options.OutputFile,
            //    OutputFile = Path.ChangeExtension(options.OutputFile, ".image"),
            //    RTemplate = ImageR,
            //    RExecute = SystemUtils.GetRExecuteLocation(),
            //    CreateNoWindow = true,
            //    NoResultFile = true
            //  };
            //  new RTemplateProcessor(imageOptions) { Progress = this.Progress }.Process();
            //}

            return(new string[] { options.OutputFile });
        }
コード例 #7
0
 public IsobaricPurityCorrectionRCalculator(IsobaricType plexType, List <UsedChannel> used, bool performPurityCorrection, bool performGraph)
     : this(plexType, used, ExternalProgramConfig.GetExternalProgram("R"), performPurityCorrection, performGraph)
 {
 }