Ejemplo n.º 1
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 });
        }
Ejemplo 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);
        }