protected override MpileupResult GetMpileupResult()
        {
            var result = new MpileupResult(string.Empty, _options.CandidatesDirectory);

            Progress.SetMessage("Single thread mode ...");
            var parser = _options.GetPileupItemParser();
            var pfile  = new PileupFile(parser);

            switch (_options.From)
            {
            case DataSourceType.Mpileup:
                pfile.Open(_options.MpileupFile);
                break;

            case DataSourceType.BAM:
                var proc = new MpileupProcessor(_options).ExecuteSamtools(new[] { _options.NormalBam, _options.TumorBam }.ToList(), _options.GetMpileupChromosomes());
                if (proc == null)
                {
                    return(null);
                }

                pfile.Open(proc.StandardOutput);
                pfile.Samtools = proc;
                break;

            case DataSourceType.Console:
                pfile.Open(Console.In);
                break;
            }

            using (pfile)
            {
                try
                {
                    IMpileupParser proc = new MpileupParser(_options, result);

                    string line;
                    while ((line = pfile.ReadLine()) != null)
                    //while ((item = pfile.Next("1", 48901870)) != null)
                    {
                        result.TotalCount++;

                        try
                        {
                            var item = proc.Parse(line, true);
                            if (item == null)
                            {
                                continue;
                            }

                            result.Results.Add(item);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(string.Format("parsing error {0}\n{1}", ex.Message, line));
                        }
                    }
                }
                finally
                {
                    if (pfile.Samtools != null)
                    {
                        try
                        {
                            pfile.Samtools.Kill();
                        }
                        // ReSharper disable once EmptyGeneralCatchClause
                        catch (Exception)
                        {
                        }
                    }
                }
            }

            return(result);
        }
Пример #2
0
        public void RunTask(string chr, CancellationTokenSource cts)
        {
            if (cts.IsCancellationRequested)
            {
                throw new UserTerminatedException();
            }

            var result = new MpileupResult(chr, _options.CandidatesDirectory);

            Progress.SetMessage("Processing chromosome {0} in thread {1}", chr, Thread.CurrentThread.ManagedThreadId);
            var process = ExecuteSamtools(new[] { _options.NormalBam, _options.TumorBam }, chr);

            if (process == null)
            {
                throw new Exception(string.Format("Fail to execute samtools for chromosome {0}.", chr));
            }

            var parser = _options.GetPileupItemParser();
            var pfile  = new PileupFile(parser);

            pfile.Open(process.StandardOutput);

            var proc = new MpileupParser(_options, result)
            {
                Progress = this.Progress
            };

            try
            {
                using (pfile)
                {
                    string line;
                    while ((line = pfile.ReadLine()) != null)
                    {
                        result.TotalCount++;

                        if (cts.IsCancellationRequested)
                        {
                            return;
                        }

                        try
                        {
                            proc.Parse(line);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(string.Format("Parsing mpileup entry failed : {0}\n{1}", ex.Message, line), ex);
                        }
                    }

                    new MpileupResultCountFormat(_options, true).WriteToFile(result.CandidateSummary, result);
                    Progress.SetMessage("Processing chromosome {0} in thread {1} finished.", chr, Thread.CurrentThread.ManagedThreadId);
                }
            }
            catch (Exception ex)
            {
                cts.Cancel();
                throw new Exception(string.Format("Processing chromosome {0} failed : {1}", result.Name, ex.Message), ex);
            }
            finally
            {
                try
                {
                    if (process != null)
                    {
                        process.Kill();
                    }
                }
                catch
                {
                }
            }
        }