/// <summary>
        ///  Write out the ploidy bed file if ploidy information is available from the vcf header
        /// Only create the normal XX or XY ploidy bed file so that Canvas can properly classify any abnormalities as variant.
        /// If ploidy Y is > 1 produce the XY ploidy bed file, otherwise produce the XX ploidy bed file
        /// </summary>
        public IFileLocation CreateGermlinePloidyBed(Vcf vcf, GenomeMetadata genomeMetadata, IDirectoryLocation sampleSandbox)
        {
            string sexKaryotype = PloidyCorrector.GetSexChromosomeKaryotypeFromVcfHeader(vcf.VcfFile.FullName);

            if (sexKaryotype == null)
            {
                _logger.Warn($"Sex chromosome ploidy not found in {vcf.VcfFile} header. No ploidy will be provided to Canvas.");
                return(null);
            }
            _logger.Info($"Found sex chromosome ploidy {PloidyCorrector.PrintPloidy(sexKaryotype)} in {vcf.VcfFile}");
            var           ploidyInfo = new SamplePloidyInfo();
            IFileLocation ploidyBed  = sampleSandbox.GetFileLocation("ploidy.bed.gz");

            if (sexKaryotype.ToLower().Contains("y"))
            {
                ploidyInfo.ProvidedPloidy = SexPloidyInfo.NormalMale;
                _logger.Info($"Creating male ploidy bed file at {ploidyBed}.");
            }
            else
            {
                ploidyInfo.ProvidedPloidy = SexPloidyInfo.NormalFemale;
                _logger.Info($"Creating female ploidy bed file at {ploidyBed}.");
            }
            string headerLine = $"{PloidyCorrector.ReferenceSexChromosomeKaryotype}={PloidyCorrector.PrettyPrintPloidy(ploidyInfo.ProvidedPloidyX.Value, ploidyInfo.ProvidedPloidyY.Value)}";

            _ploidyFixer.WritePloidyBedFile(ploidyInfo, genomeMetadata, _ploidyFixer.GetParRegions(genomeMetadata),
                                            ploidyBed.FullName, headerLine, ploidy => true);
            return(ploidyBed);
        }
        private CanvasEnrichmentOutput RunSingleSample(string sampleId, CanvasEnrichmentInput input, IFileLocation dbSnpVcf, IFileLocation manifest, IDirectoryLocation sampleSandbox)
        {
            if (!_annotationFileProvider.IsSupported(input.GenomeMetadata))
            {
                _logger.Info($"Skipping Canvas for sample {sampleId}: unsupported reference genome '{input.GenomeMetadata.Name}'");
                return(null);
            }

            var           moreCustomParameters = new Dictionary <string, string>();
            StringBuilder commandLine          = new StringBuilder("Somatic-Enrichment");

            commandLine.Append(_singleSampleInputCommandLineBuilder.GetSingleSampleCommandLine(sampleId, input.Bam, input.GenomeMetadata, sampleSandbox));

            string sexChromosomeKaryotype = null;

            if (input.PloidyInfo.IsPloidyAvailable)
            {
                sexChromosomeKaryotype = input.PloidyInfo.IsXYMale.Value ? PloidyCorrector.PrettyPrintPloidy(1, 1)
                    : (input.PloidyInfo.IsXXFemale.Value ? PloidyCorrector.PrettyPrintPloidy(2, 0) : null);
            }
            string controlSexChromosomeKaryotype = null;

            if (input.IsCanvasNormalizePcaMode && input.PloidyInfo.IsPloidyAvailable)
            {
                IFileLocation pcaModelFile = input.PloidyInfo.IsXYMale.Value ? input.PcaModels.MaleModelFile
                    : (input.PloidyInfo.IsXXFemale.Value ? input.PcaModels.FemaleModelFile : null);
                if (pcaModelFile == null)
                {
                    string sampleSex = input.PloidyInfo.IsXYMale.Value ? "male"
                        : (input.PloidyInfo.IsXXFemale.Value ? "female" : "sex unknown");
                    _logger.Info($"Skipping Canvas for sample {sampleId}: PCA model file not available for {sampleSex} samples.");
                    return(null);
                }
                moreCustomParameters["CanvasNormalize"] = "-m PCA";
                commandLine.Append($" --control-binned \"{pcaModelFile}\"");
                commandLine.Append($" --control-bin-size 100"); // use a dummy bin size for now
                controlSexChromosomeKaryotype = sexChromosomeKaryotype;
            }
            else if (input.PrecomputedControl?.BinnedPath != null)
            {
                commandLine.Append($" --control-binned \"{input.PrecomputedControl.BinnedPath}\"");
                commandLine.Append($" --control-bin-size {input.PrecomputedControl.BinSize}");
                controlSexChromosomeKaryotype = input.PrecomputedControl.SexChromosomeKaryotype;
            }
            else
            {
                foreach (var normalBam in input.NormalBamPaths)
                {
                    commandLine.Append($" --control-bam \"{normalBam.BamFile}\"");
                }
            }

            AddControlPloidyVcf(commandLine, input, controlSexChromosomeKaryotype, sampleId, sampleSandbox);

            commandLine.Append($" --{SingleSampleCommonOptionsParser.PopulationBAlleleVcfOptionName} \"{dbSnpVcf}\"");
            commandLine.Append($" --manifest \"{manifest}\"");

            AddSamplePloidyVcf(commandLine, input, sexChromosomeKaryotype, sampleId, sampleSandbox);

            if (input.PredefinedBinsFile != null)
            {
                moreCustomParameters["CanvasBin"] = $"-n={input.PredefinedBinsFile.WrapWithEscapedShellQuote()}";
            }
            commandLine.Append(_singleSampleInputCommandLineBuilder.GetCustomParameters(moreCustomParameters));
            commandLine = _singleSampleInputCommandLineBuilder.MergeCustomCanvasParameters(commandLine);

            var job = new JobInfo(_runtimeExecutable.FullName, _canvasExe + " " + commandLine, "Canvas_" + sampleId);

            return(_workDoer.DoWork(WorkResourceRequest.CreateExact(1, 8), job, GetCanvasOutput(sampleId, sampleSandbox)).Await());
        }
 public CanvasPloidyBedCreator(ILogger logger, IWorkManager workManager, PloidyCorrector ploidyFixer)
 {
     _logger      = logger;
     _workManager = workManager;
     _ploidyFixer = ploidyFixer;
 }
        private CanvasEnrichmentOutput RunSingleSample(string sampleId, CanvasEnrichmentInput input, IFileLocation dbSnpVcf, IFileLocation manifest, IDirectoryLocation sampleSandbox)
        {
            if (!_annotationFileProvider.IsSupported(input.GenomeMetadata))
            {
                _logger.Info($"Skipping Canvas for sample {sampleId}: unsupported reference genome '{input.GenomeMetadata.Name}'");
                return(null);
            }

            var           moreCustomParameters = new Dictionary <string, string>();
            StringBuilder commandLine          = new StringBuilder("Somatic-Enrichment");

            commandLine.Append(_singleSampleInputCommandLineBuilder.GetSingleSampleCommandLine(sampleId, input.Bam, input.GenomeMetadata, sampleSandbox));

            string sexChromosomeKaryotype = null;

            if (input.PloidyInfo.IsPloidyAvailable)
            {
                sexChromosomeKaryotype = input.PloidyInfo.IsXYMale.Value ? PloidyCorrector.PrettyPrintPloidy(1, 1)
                    : (input.PloidyInfo.IsXXFemale.Value ? PloidyCorrector.PrettyPrintPloidy(2, 0) : null);
            }
            string controlSexChromosomeKaryotype = null;

            if (input.IsCanvasNormalizePcaMode && input.PloidyInfo.IsPloidyAvailable)
            {
                IFileLocation pcaModelFile = input.PloidyInfo.IsXYMale.Value ? input.PcaModels.MaleModelFile
                    : (input.PloidyInfo.IsXXFemale.Value ? input.PcaModels.FemaleModelFile : null);
                if (pcaModelFile == null)
                {
                    string sampleSex = input.PloidyInfo.IsXYMale.Value ? "male"
                        : (input.PloidyInfo.IsXXFemale.Value ? "female" : "sex unknown");
                    _logger.Info($"Skipping Canvas for sample {sampleId}: PCA model file not available for {sampleSex} samples.");
                    return(null);
                }
                moreCustomParameters["CanvasNormalize"] = "-m PCA";
                commandLine.Append($" --control-binned \"{pcaModelFile}\"");
                commandLine.Append($" --control-bin-size 100"); // use a dummy bin size for now
                controlSexChromosomeKaryotype = sexChromosomeKaryotype;
            }
            else if (input.PrecomputedControl?.BinnedPath != null)
            {
                commandLine.Append($" --control-binned \"{input.PrecomputedControl.BinnedPath}\"");
                commandLine.Append($" --control-bin-size {input.PrecomputedControl.BinSize}");
                controlSexChromosomeKaryotype = input.PrecomputedControl.SexChromosomeKaryotype;
            }
            else
            {
                foreach (var normalBam in input.NormalBamPaths)
                {
                    commandLine.Append($" --control-bam \"{normalBam.BamFile}\"");
                }
            }
            if (controlSexChromosomeKaryotype != null)
            {
                var controlPloidyBed = sampleSandbox.GetFileLocation("control-ploidy.bed.gz");
                if (_canvasPloidyBedCreator.GeneratePloidyBedFileFromSexChromosomeKaryotype(input.GenomeMetadata, input.GenomeMetadata.Sequences.First().FastaPath,
                                                                                            controlSexChromosomeKaryotype, controlPloidyBed.FullName, sampleSandbox.FullName))
                {
                    commandLine.Append($" --{SomaticEnrichmentOptionsParser.ControlPloidyBedOptionName} \"{controlPloidyBed}\"");
                }
            }
            commandLine.Append($" --{SingleSampleCommonOptionsParser.PopulationBAlleleVcfOptionName} \"{dbSnpVcf}\"");
            commandLine.Append($" --manifest \"{manifest}\"");

            if (sexChromosomeKaryotype != null)
            {
                var ploidyBed = sampleSandbox.GetFileLocation("ploidy.bed.gz");
                if (_canvasPloidyBedCreator.GeneratePloidyBedFileFromSexChromosomeKaryotype(input.GenomeMetadata, input.GenomeMetadata.Sequences.First().FastaPath,
                                                                                            sexChromosomeKaryotype, ploidyBed.FullName, sampleSandbox.FullName))
                {
                    commandLine.Append($" --{SingleSampleCommonOptionsParser.PloidyBedOptionName} \"{ploidyBed}\"");
                }
            }

            if (input.PredefinedBinsFile != null)
            {
                moreCustomParameters["CanvasBin"] = $"-n={input.PredefinedBinsFile.WrapWithEscapedShellQuote()}";
            }
            commandLine.Append(_singleSampleInputCommandLineBuilder.GetCustomParameters(moreCustomParameters));
            commandLine = _singleSampleInputCommandLineBuilder.MergeCustomCanvasParameters(commandLine);

            UnitOfWork singleSampleJob = new UnitOfWork()
            {
                ExecutablePath = CrossPlatform.IsThisLinux() ? _runtimeExecutable.FullName : _canvasExe.FullName,
                CommandLine    = CrossPlatform.IsThisLinux() ? _canvasExe + " " + commandLine : commandLine.ToString(),
                LoggingFolder  = _workManager.LoggingFolder.FullName,
                LoggingStub    = "Canvas_" + sampleId,
            };

            _workManager.DoWorkSingleThread(singleSampleJob);
            return(GetCanvasOutput(sampleId, sampleSandbox));
        }
Beispiel #5
0
        public static void Evaluate(string truthSetPath, string cnvCallsPath, string excludedBed, string outputPath, EvaluateCnvOptions options)
        {
            double heterogeneityFraction = options.HeterogeneityFraction;
            var    knownCn = LoadKnownCn(truthSetPath, heterogeneityFraction);

            knownCn = knownCn.SelectValues(
                truthEntries => truthEntries.Where(truthEntry => truthEntry.Length >= options.MinEntrySize).ToList());
            var calls = GetCnvCallsFromVcf(cnvCallsPath, options.DQscoreThreshold);

            calls = calls.SelectValues(
                chromosomeCalls => chromosomeCalls.Where(call => call.Length >= options.MinEntrySize).ToList());

            // LoadRegionsOfInterest(options.RoiBed?.FullName);
            var excludeIntervals = new Dictionary <string, List <CNInterval> >();

            if (!string.IsNullOrEmpty(excludedBed))
            {
                var           excludeIntervalsTmp = LoadIntervalsFromBed(excludedBed, false, 1.0);
                List <string> keys = excludeIntervalsTmp.Keys.ToList();
                foreach (string key in keys)
                {
                    string chr = key;
                    if (!calls.ContainsKey(chr))
                    {
                        chr = key.Replace("chr", "");
                    }
                    if (!calls.ContainsKey(chr))
                    {
                        chr = "chr" + key;
                    }
                    if (!calls.ContainsKey(chr))
                    {
                        Console.WriteLine($"Error: Skipping exclude intervals for chromosome {key} with no truth data." +
                                          $"Check that chromosome names are spelled correctly for exclude intervals");
                        continue;
                    }
                    excludeIntervals[chr] = excludeIntervalsTmp[key];
                }
            }
            Console.WriteLine("TruthSet\t{0}", truthSetPath);
            Console.WriteLine("CNVCalls\t{0}", cnvCallsPath);

            bool includePassingOnly = Path.GetFileName(cnvCallsPath).ToLower().Contains("vcf");
            var  logger             = new Logger(new[] { Console.Out }, new[] { Console.Error });
            var  settings           = IsasConfigurationSettings.GetConfigSettings();
            var  output             = new DirectoryLocation(outputPath);
            var  workerDirectory    = new DirectoryLocation(Isas.Framework.Utilities.Utilities.GetAssemblyFolder(typeof(CNVChecker)));
            var  commandManager     = new CommandManager(new ExecutableProcessor(settings, logger, workerDirectory));

            WorkDoerFactory.RunWithWorkDoer(logger, settings, output, workDoer =>
            {
                var tabixWrapper    = TabixWrapperFactory.GetTabixWrapper(logger, workDoer, commandManager);
                var ploidyCorrector = new PloidyCorrector(logger, workDoer,
                                                          new PloidyEstimator(logger, workDoer, null, false, commandManager), tabixWrapper, false);
                var checker = new CNVChecker(options.DQscoreThreshold, excludeIntervals, ploidyCorrector);
                if (options.PloidyInfo.SexPloidyInfo != null)
                {
                    Console.WriteLine($">>>Getting reference ploidy from provided ploidy information and PAR bed file '{options.PloidyInfo.ParBed}'");

                    var ploidy          = checker.GetPloidy(options.PloidyInfo, output);
                    var referencePloidy = LoadReferencePloidy(options.PloidyInfo.SexPloidyInfo, options.PloidyInfo.ParBed);
                    knownCn             = GetKnownCopyNumberWithReferencePloidy(referencePloidy, knownCn);
                    calls = GetCallsWithRefPloidy(calls, ploidy);
                }
                var cnvEvaluator = new CnvEvaluator(checker);

                if (checker.DQscoreThreshold.HasValue && !Path.GetFileName(cnvCallsPath).ToLower().Contains("vcf"))
                {
                    throw new ArgumentException("CNV.vcf must be in a vcf format when --dqscore option is used");
                }
                cnvEvaluator.ComputeAccuracy(knownCn, cnvCallsPath, outputPath, includePassingOnly, options, calls);
                if (includePassingOnly)
                {
                    cnvEvaluator.ComputeAccuracy(knownCn, cnvCallsPath, outputPath, false, options, calls);
                }
                ComputeCallability(logger, calls, options, output);
                Console.WriteLine(">>>Done - results written to {0}", outputPath);
            });
        }
Beispiel #6
0
 public CanvasPloidyVcfCreator(PloidyCorrector ploidyFixer)
 {
     _ploidyFixer = ploidyFixer;
 }