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

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

            var mutationList = GetValidationList();

            result.TotalCount = mutationList.Items.Length;

            var map = mutationList.Items.ToDictionary(m => GenomeUtils.GetKey(m.Chr, m.Pos));

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

            case DataSourceType.BAM:
                var posFile = Path.Combine(_options.CandidatesDirectory, "pos.bed");
                mutationList.WriteToFile(posFile, 500);
                var proc = new MpileupProcessor(_options).ExecuteSamtools(new[] { _options.NormalBam, _options.TumorBam }, "", posFile);
                if (proc == null)
                {
                    throw new Exception("Cannot execute mpileup.");
                }

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

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

            Progress.SetMessage("Total {0} entries in validation list", mutationList.Items.Length);
            foreach (var m in map)
            {
                Console.WriteLine(m.Key);
            }

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

                    string line;
                    while ((line = pfile.ReadLine()) != null)
                    {
                        try
                        {
                            var locus    = parser.GetSequenceIdentifierAndPosition(line);
                            var locusKey = GenomeUtils.GetKey(locus.SequenceIdentifier, locus.Position);

                            //Console.WriteLine(locusKey);
                            ValidationItem vitem = null;
                            if (!map.TryGetValue(locusKey, out vitem))
                            {
                                continue;
                            }

                            //Console.WriteLine("Parsing " + line);

                            var parres = proc.Parse(line, true);
                            if (!string.IsNullOrEmpty(parres.FailedReason))
                            {
                                Progress.SetMessage("{0}\t{1}\t{2} ~ {3}\t{4}", parres.Item.SequenceIdentifier, parres.Item.Position, parres.Group.Sample1, parres.Group.Sample2, parres.FailedReason);
                            }
                            result.Results.Add(parres);
                        }
                        catch (Exception ex)
                        {
                            var error = string.Format("parsing error {0}\n{1}", ex.Message, line);
                            Progress.SetMessage(error);
                            Console.Error.WriteLine(ex.StackTrace);
                            throw new Exception(error);
                        }
                    }
                }
                finally
                {
                    if (pfile.Samtools != null)
                    {
                        try
                        {
                            pfile.Samtools.Kill();
                        }
                        // ReSharper disable once EmptyGeneralCatchClause
                        catch (Exception)
                        {
                        }
                    }
                }
            }

            result.NotCovered = result.TotalCount - result.Results.Count;

            return(result);
        }
Esempio n. 2
0
    protected override MpileupResult GetMpileupResult()
    {
      var result = new MpileupResult(string.Empty, _options.CandidatesDirectory);

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

      var mutationList = GetValidationList();
      result.TotalCount = mutationList.Items.Length;

      var map = mutationList.Items.ToDictionary(m => GenomeUtils.GetKey(m.Chr, m.Pos));

      switch (_options.From)
      {
        case DataSourceType.Mpileup:
          pfile.Open(_options.MpileupFile);
          break;
        case DataSourceType.BAM:
          var posFile = Path.Combine(_options.CandidatesDirectory, "pos.bed");
          mutationList.WriteToFile(posFile, 500);
          var proc = new MpileupProcessor(_options).ExecuteSamtools(new[] { _options.NormalBam, _options.TumorBam }, "", posFile);
          if (proc == null)
          {
            throw new Exception("Cannot execute mpileup.");
          }

          pfile.Open(proc.StandardOutput);
          pfile.Samtools = proc;
          break;
        case DataSourceType.Console:
          pfile.Open(Console.In);
          break;
      }

      Progress.SetMessage("Total {0} entries in validation list", mutationList.Items.Length);
      foreach (var m in map)
      {
        Console.WriteLine(m.Key);
      }

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

          string line;
          while ((line = pfile.ReadLine()) != null)
          {
            try
            {
              var locus = parser.GetSequenceIdentifierAndPosition(line);
              var locusKey = GenomeUtils.GetKey(locus.SequenceIdentifier, locus.Position);

              //Console.WriteLine(locusKey);
              ValidationItem vitem = null;
              if (!map.TryGetValue(locusKey, out vitem))
              {
                continue;
              }

              //Console.WriteLine("Parsing " + line);

              var parres = proc.Parse(line, true);
              if (!string.IsNullOrEmpty(parres.FailedReason))
              {
                Progress.SetMessage("{0}\t{1}\t{2} ~ {3}\t{4}", parres.Item.SequenceIdentifier, parres.Item.Position, parres.Group.Sample1, parres.Group.Sample2, parres.FailedReason);
              }
              result.Results.Add(parres);
            }
            catch (Exception ex)
            {
              var error = string.Format("parsing error {0}\n{1}", ex.Message, line);
              Progress.SetMessage(error);
              Console.Error.WriteLine(ex.StackTrace);
              throw new Exception(error);
            }
          }
        }
        finally
        {
          if (pfile.Samtools != null)
          {
            try
            {
              pfile.Samtools.Kill();
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch (Exception)
            {
            }
          }
        }
      }

      result.NotCovered = result.TotalCount - result.Results.Count;

      return result;
    }