Exemplo n.º 1
0
    public override IEnumerable<string> Process()
    {
      var bamList = ReadBamList();

      var process = ExecuteSamtools(bamList);
      if (process == null)
      {
        throw new Exception("Fail to execute samtools.");
      }

      var vcfItems = new VcfItemListFormat().ReadFromFile(_options.InputFile);
      vcfItems.Header = vcfItems.Header + "\t" + bamList.ConvertAll(m => m.Value).Merge("\t");

      var vcfMap = vcfItems.Items.ToDictionary(m => GetKey(m.Seqname, m.Start));

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

      try
      {
        using (pfile)
        {
          string line;
          while ((line = pfile.ReadLine()) != null)
          {
            var item = parser.GetSequenceIdentifierAndPosition(line);
            var key = GetKey(item.SequenceIdentifier, item.Position);

            VcfItem vcf;
            if (!vcfMap.TryGetValue(key, out vcf))
            {
              continue;
            }

            item = parser.GetValue(line);
            foreach (var sample in item.Samples)
            {
              var refCount = sample.Count(m => m.Event.Equals(vcf.RefAllele));
              var altCount = sample.Count(m => m.Event.Equals(vcf.AltAllele));
              vcf.Line = vcf.Line + string.Format("\t{0}:{1}", refCount, altCount);
            }
          }
        }
      }
      finally
      {
        try
        {
          if (process != null) process.Kill();
        }
        catch
        { }
      }

      new VcfItemListFormat().WriteToFile(_options.OutputFile, vcfItems);

      return new string[] { _options.OutputFile };
    }
    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;
    }
Exemplo n.º 3
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
        {
        }
      }
    }
Exemplo n.º 4
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;
    }
Exemplo n.º 5
0
    public override IEnumerable<string> Process()
    {
      options.PrintParameter(Console.Out);

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

      var mutationList = new ValidationFile().ReadFromFile(options.BedFile);
      var map = mutationList.Items.ToDictionary(m => GenomeUtils.GetKey(m.Chr, m.Pos));

      var posFile = Path.Combine(options.OutputFile + ".pos.bed");
      mutationList.WriteToFile(posFile, 500);
      var proc = new MpileupProcessor(options).ExecuteSamtools(options.BamFiles, "", posFile);
      if (proc == null)
      {
        return null;
      }

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

      Progress.SetMessage("Total {0} entries in extraction list", mutationList.Items.Length);

      var result = new Dictionary<ValidationItem, PileupItem>();

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

              if (!locus.SequenceIdentifier.Equals(lastChrom))
              {
                Progress.SetMessage("Processing chromosome " + locus.SequenceIdentifier + " ...");
                lastChrom = locus.SequenceIdentifier;
              }

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

              result[vitem] = parser.GetValue(line);
            }
            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)
            {
            }
          }
        }
      }

      if (result.Count == 0)
      {
        throw new Exception("Nothing found. Look at the log file for error please.");
      }

      using (var sw = new StreamWriter(options.OutputFile))
      {
        sw.WriteLine("{0}\t{1}", mutationList.Header, options.GetBamNames().Merge("\t"));
        var emptyevents = new string('\t', options.BamFiles.Count);
        foreach (var mu in mutationList.Items)
        {
          sw.Write("{0}", mu.Line);
          PileupItem item;
          if (result.TryGetValue(mu, out item))
          {
            foreach (var sample in item.Samples)
            {
              sample.InitEventCountList(false);
              if (sample.EventCountList.Count > 0)
              {
                sw.Write("\t{0}", (from ecl in sample.EventCountList
                                   let v = string.Format("{0}:{1}", ecl.Event, ecl.Count)
                                   select v).Merge(","));
              }
              else
              {
                sw.Write("\t");
              }
            }
          }
          else
          {
            sw.Write(emptyevents);
          }
          sw.WriteLine();
        }
      }

      return new[] { options.OutputFile };
    }
Exemplo n.º 6
0
        public override IEnumerable <string> Process()
        {
            var bamList = ReadBamList();

            var process = ExecuteSamtools(bamList);

            if (process == null)
            {
                throw new Exception("Fail to execute samtools.");
            }

            var vcfItems = new VcfItemListFormat().ReadFromFile(_options.InputFile);

            vcfItems.Header = vcfItems.Header + "\t" + bamList.ConvertAll(m => m.Value).Merge("\t");

            var vcfMap = vcfItems.Items.ToDictionary(m => GetKey(m.Seqname, m.Start));

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

            pfile.Open(process.StandardOutput);

            try
            {
                using (pfile)
                {
                    string line;
                    while ((line = pfile.ReadLine()) != null)
                    {
                        var item = parser.GetSequenceIdentifierAndPosition(line);
                        var key  = GetKey(item.SequenceIdentifier, item.Position);

                        VcfItem vcf;
                        if (!vcfMap.TryGetValue(key, out vcf))
                        {
                            continue;
                        }

                        item = parser.GetValue(line);
                        foreach (var sample in item.Samples)
                        {
                            var refCount = sample.Count(m => m.Event.Equals(vcf.RefAllele));
                            var altCount = sample.Count(m => m.Event.Equals(vcf.AltAllele));
                            vcf.Line = vcf.Line + string.Format("\t{0}:{1}", refCount, altCount);
                        }
                    }
                }
            }
            finally
            {
                try
                {
                    if (process != null)
                    {
                        process.Kill();
                    }
                }
                catch
                { }
            }

            new VcfItemListFormat().WriteToFile(_options.OutputFile, vcfItems);

            return(new string[] { _options.OutputFile });
        }