Example #1
0
        internal static string ToString([NotNull] IVcfVariant variant, bool?isTruth)
        {
            var ret = variant.ToStrings().Take(FormatIndex).ToList();

            // order the info fields
            if (ret[InfoIndex] != MissingValueString)
            {
                ret[InfoIndex] = variant.Info.OrderBy(kvp => kvp.Key)
                                 .Select(kvp => kvp.Value.IsNullOrEmpty()
                        ? kvp.Key
                        : $"{kvp.Key}{InfoFieldKeyValueDelimiter}{kvp.Value}")
                                 .StringJoin(InfoFieldDelimiter);
            }

            // add format column
            ret.Add(variant.Samples[0].SampleDictionary.Keys.StringJoin(SampleFieldDelimiter));

            if (isTruth == null)
            {
                var values = variant.Samples[0].SampleDictionary.Values;
                ret.Add(values.All(it => string.IsNullOrEmpty(it) || it == MissingValueString)
                    ? MissingValueString
                    : values.StringJoin(SampleFieldDelimiter));
            }
            else if (isTruth.Value)
            {
                ret.Add(variant.Samples[0].SampleDictionary.Values.StringJoin(SampleFieldDelimiter));
                ret.Add(MissingValueString);
            }
            else
            {
                ret.Add(MissingValueString);
                ret.Add(variant.Samples[1].SampleDictionary.Values.StringJoin(SampleFieldDelimiter));
            }

            return(ret.StringJoin(ColumnDelimiter));
        }
Example #2
0
        internal static IInterval <uint> ConvertPositionToCiInterval(
            this uint position, [NotNull] IVcfVariant variant, [NotNull] string ciInfoTag)
        {
            if (!variant.Info.TryGetValue(ciInfoTag, out var posString))
            {
                return(BedInterval.Create(position > 0 ? position - 1 : 0, position == 0 ? 1 : position));
            }

            var split = posString.Split(WittyerConstants.InfoValueDel);

            if (split.Length != 2)
            {
                throw VcfVariantFormatException.Create(variant.ToString(), ImmutableHashSet.Create(VcfColumn.Info),
                                                       $"Invalid {ciInfoTag} found: {posString}", variant.ToStrings().ToList().AsReadOnly());
            }

            var parsedStart = GetParsedAbsValue(split[0]);

            if (parsedStart == null)
            {
                throw new InvalidOperationException($"Failed to parse {ciInfoTag}={posString}!");
            }
            var parsedStop = GetParsedAbsValue(split[1]);

            if (parsedStop == null)
            {
                throw new InvalidOperationException($"Failed to parse {ciInfoTag}={posString}!");
            }
            var(start, stop) = ConvertPositionToCiInterval(position, (parsedStart.Value, parsedStop.Value));
            return(BedInterval.Create(start, stop));

            uint?GetParsedAbsValue(string val)
            => !int.TryParse(val, out var parsed) ? (uint?)null : (uint)(parsed < 0 ? -parsed : parsed);
        }
Example #3
0
        internal static IInterval <uint> ConvertPositionToCiInterval(
            this uint position, [NotNull] IVcfVariant variant, [NotNull] string ciInfoTag)
        {
            if (!variant.Info.TryGetValue(ciInfoTag, out var posString))
            {
                return(BedInterval.Create(position > 0 ? position - 1 : 0, position == 0 ? 1 : position));
            }

            var split = posString.Split(WittyerConstants.InfoValueDel);

            if (split.Length != 2)
            {
                throw VcfVariantFormatException.Create(variant.ToString(), ImmutableHashSet.Create(VcfColumn.Info),
                                                       $"Invalid {ciInfoTag} found: {posString}", variant.ToStrings().ToList().AsReadOnly());
            }

            var(start, stop) = ConvertPositionToCiInterval(position, (GetParsedAbsValue(split[0]), GetParsedAbsValue(split[1])));
            return(BedInterval.Create(start, stop));

            uint GetParsedAbsValue(string val)
            {
                var parsed = int.Parse(val);

                return((uint)(parsed < 0 ? -parsed : parsed));
            }
        }
Example #4
0
        internal static InclusiveInterval <int> ParseCi([NotNull] this IVcfVariant variant, string tag)
        {
            if (!variant.Info.TryGetValue(tag, out var posString))
            {
                return(new InclusiveInterval <int>(0, 0));
            }

            var split = posString.Split(WittyerConstants.InfoValueDel);

            if (split.Length != 2)
            {
                throw VcfVariantFormatException.Create(variant.ToString(), ImmutableHashSet.Create(VcfColumn.Info),
                                                       $"Invalid {tag} found: {posString}", variant.ToStrings().ToList().AsReadOnly());
            }

            return(new InclusiveInterval <int>(int.Parse(split[0]), int.Parse(split[1])));
        }