public static void ParseReferenceVariantWorks([NotNull] string inputVariant)
        {
            var vcfVariant = VcfVariant.TryParse(inputVariant,
                                                 VcfVariantParserSettings.Create(ImmutableList.Create("NA12878", "haha"), GenomeAssembly.Hg19))
                             .GetOrThrowDebug();

            WittyerType.ParseFromVariant(vcfVariant, false, "NA12878", out var actualType);
            Assert.Equal(WittyerType.CopyNumberReference, actualType);
        }
        private static IWittyerSimpleVariant CreateWittyerVariant([NotNull] string vcfline)
        {
            var baseVariant = VcfVariant.TryParse(vcfline,
                                                  VcfVariantParserSettings.Create(ImmutableList.Create("normal"), GenomeAssembly.Grch37))
                              .GetOrThrowDebug();

            WittyerType.ParseFromVariant(baseVariant, true, null, out var svType);

            if (svType == null)
            {
                throw new NotSupportedException("This method does not handle svType null");
            }

            return(WittyerVariantInternal
                   .Create(baseVariant, baseVariant.Samples["normal"], svType, Bins, PercentDistance, BasepairDistance));
        }
        public static void ParseWittyerVariantType_AssignCorrectType([NotNull] string vcfString, bool isCrossTypeOn,
                                                                     [CanBeNull] string expected)
        {
            const string sampleName = "s1";
            var          variant    = VcfVariant.TryParse(vcfString,
                                                          VcfVariantParserSettings.Create(ImmutableList.Create(sampleName, "s2"), GenomeAssembly.Hg38))
                                      .GetOrThrowDebug();
            var reason = WittyerType.ParseFromVariant(variant, isCrossTypeOn, sampleName, out var assignedType);

            if (expected == null)
            {
                Assert.Equal(FailedReason.UndeterminedCn, reason);
            }
            else
            {
                Assert.Equal(WittyerType.Parse(expected), assignedType);
            }
        }
        public static void WittyerVariantCreateCorrectly([NotNull] string variant, uint posStart, uint posEnd, string sampleName,
                                                         uint endStart, uint endEnd, string winner)
        {
            var vcfVariant = VcfVariant.TryParse(variant,
                                                 VcfVariantParserSettings.Create(ImmutableList.Create("normal", "tumor"), GenomeAssembly.Hg38))
                             .GetOrThrowDebug();

            var _ = WittyerType.ParseFromVariant(vcfVariant, false, sampleName, out var type);

            if (type == null)
            {
                throw new NotSupportedException("This test does not handle svType null");
            }
            var wittyerVariant = WittyerVariantInternal
                                 .Create(vcfVariant, vcfVariant.Samples[sampleName], type, Bins, PercentDistance, BasepairDistance);

            var expectedStart = ContigAndInterval.Create(vcfVariant.Contig, posStart, posEnd);
            var expectedEnd   = ContigAndInterval.Create(vcfVariant.Contig, endStart, endEnd);

            MultiAssert.Equal(expectedStart, wittyerVariant.PosInterval);
            MultiAssert.Equal(expectedEnd, wittyerVariant.EndInterval);
            MultiAssert.Equal(winner, wittyerVariant.Win.ToString());
            MultiAssert.AssertAll();
        }
        internal static IContigProvider CreateVariant([NotNull] IVcfVariant vcfVariant, [CanBeNull] IVcfSample sample, bool isTruth,
                                                      [CanBeNull] string sampleName, IReadOnlyDictionary <WittyerType, InputSpec> inputSpecDict,
                                                      IDictionary <IGeneralBnd, IVcfVariant> bndSet, List <string> errorList, bool isCrossTypeOn)
        {
            var failedReason = WittyerType.ParseFromVariant(vcfVariant, isCrossTypeOn, sampleName, out var svType);

            if (failedReason != null)
            {
                return(CreateUnsupportedVariant(vcfVariant, sample,
                                                failedReason.Value == FailedReason.Unset
                        ? throw new ArgumentOutOfRangeException(
                                                    $"Got {nameof(FailedReason)}.{FailedReason.Unset} which means bug in {nameof(WittyerType.TryParse)}")
                        : failedReason.Value,
                                                isTruth));
            }

            if (svType == null)
            {
                throw new InvalidDataException("svType should not be null with no failed reason");
            }

            //User does not specify this SVTYPE in input spec, consider user want to exlude this particular SVTYPE comparison entirely
            if (!inputSpecDict.TryGetValue(svType, out var inputSpec))
            {
                return(CreateUnsupportedVariant(vcfVariant, sample, FailedReason.VariantTypeSkipped, isTruth));
            }

            var isSupportedVariant = IsSupportedVariant();

            if (!isSupportedVariant.Equals(FailedReason.Unset))
            {
                return(CreateUnsupportedVariant(vcfVariant, sample, isSupportedVariant, isTruth));
            }

            var bpd = inputSpec.BasepairDistance;
            var pd  = inputSpec.PercentDistance;

            var bins = inputSpec.BinSizes;

            if (svType == WittyerType.Insertion)
            {
                //insertion is basically using one same record as the both entries of the breakend pair
                return(WittyerBndInternal.Create(vcfVariant,
                                                 sample, inputSpec.VariantType, bins.Select(sizeSkipTuple => sizeSkipTuple.size).ToReadOnlyList(), bpd, pd, vcfVariant));
            }

            if (svType == WittyerType.CopyNumberReference &&
                vcfVariant.Info.TryGetValue(VcfConstants.SvTypeKey, out var svTypeString) &&
                !WittyerConstants.BaseLevelStatsTypeStrings.Contains(svTypeString))
            {
                // any non-DEL/DUP/CNV that is determined to be reference copy number is not supported.
                return(CreateUnsupportedVariant(vcfVariant, sample,
                                                FailedReason.UnsupportedRefCall, isTruth));
            }

            if (svType == WittyerType.TranslocationBreakend ||
                svType == WittyerType.IntraChromosomeBreakend)
            {
                var currentBnd = GeneralBnd.CreateFromVariant(vcfVariant);

                //Note: this means the paired BND is found as a key in dictionary. Checkout the comparer for details
                if (bndSet.TryGetValue(currentBnd, out var secondVariant))
                {
                    if (!bndSet.Remove(currentBnd))
                    {
                        throw new InvalidOperationException(
                                  $"Cannot remove {secondVariant} from breakend dictionary when pair is found: {vcfVariant}! Find a developer to debug!");
                    }
                    return(WittyerBndInternal.Create(vcfVariant, sample, svType, bins.Select(sizeSkipTuple => sizeSkipTuple.size).ToReadOnlyList(), bpd, pd, secondVariant));
                }

                bndSet.Add(currentBnd, vcfVariant);
                return(currentBnd);
            }

            try
            {
                return(WittyerVariantInternal.Create(vcfVariant, sample, svType, bins.Select(sizeSkipTuple => sizeSkipTuple.size).ToReadOnlyList(), pd, bpd));
            }
            catch (Exception e)
            {
                if (errorList.Count <= MaxNonSupportedVariantToPrint)
                {
                    errorList.Add(
                        new[] { "Exception caught:", e.ToString(), vcfVariant.ToString() }
                        .StringJoin(Environment.NewLine));
                }
                return(CreateUnsupportedVariant(vcfVariant, sample, FailedReason.Other, isTruth));
            }

            FailedReason IsSupportedVariant()
            {
                // Check filters.
                IReadOnlyCollection <string> includedFilters, excludedFilters;

                if (isTruth)
                {
                    includedFilters = WittyerConstants.DefaultIncludeFilters;
                    excludedFilters = WittyerConstants.DefaultExcludeFilters;
                }
                else
                {
                    includedFilters = inputSpec.IncludedFilters;
                    excludedFilters = inputSpec.ExcludedFilters;
                }

                if (vcfVariant.Filters.Any(excludedFilters.Contains) ||
                    includedFilters.Count > 0 &&
                    (vcfVariant.Filters.Count == 0 || !vcfVariant.Filters.Any(includedFilters.Contains)))
                {
                    return(FailedReason.FilteredBySettings);
                }

                // SVLEN = 0 when they are supposed to have overlaps (svlen is needed for overlapping windows) are ignored
                if (svType.HasOverlappingWindows &&
                    (vcfVariant.Info.TryGetValue(VcfConstants.EndTagKey, out var endString) &&
                     vcfVariant.Position.ToString() == endString ||
                     vcfVariant.Info.TryGetValue(VcfConstants.SvLenKey, out var svLenString) &&
                     svLenString == "0"))
                {
                    return(FailedReason.InvalidSvLen);
                }

                // Bnd with pos and alt to be same position (temporarily to accomendate the situation of SVLEN=0 INV representing as bnd)
                if (svType == WittyerType.IntraChromosomeBreakend)
                {
                    var mate = SimpleBreakEnd.TryParse(vcfVariant.GetSingleAlt()).GetOrThrow();
                    return(vcfVariant.Position == mate.Position ? FailedReason.InvalidSvLen : FailedReason.Unset);
                }

                // todo: truth does not care about Sample FT tag, is that ok?
                var sampleFilterOk = isTruth || !includedFilters.Contains(VcfConstants.PassFilter) || !vcfVariant.IsPassFilter() || IsSampleFtPassFilter();

                if (!sampleFilterOk)
                {
                    return(FailedReason.FailedSampleFilter);
                }

                // used include bed and variant is completely within a single contig and the bed doesn't include the contig
                if (inputSpec.IncludedRegions != null && svType != WittyerType.TranslocationBreakend &&
                    !inputSpec.IncludedRegions.IntervalTree.ContainsKey(vcfVariant.Contig))
                {
                    return(FailedReason.OutsideBedRegion);
                }

                return(FailedReason.Unset);

                bool IsSampleFtPassFilter()
                => sample != null && (!sample.SampleDictionary.TryGetValue(WittyerConstants.Ft, out var ft) ||
                                      ft.Equals(VcfConstants.PassFilter));
            }
        }