From 034e5496dcd7c51f548b5d375bfb22641af50c28 Mon Sep 17 00:00:00 2001 From: Nicolas Vannieuwkerke Date: Mon, 13 Nov 2023 14:55:10 +0100 Subject: [PATCH] small race condition fix --- convert/structs.go | 21 +++++++++++---------- convert/vcf.go | 21 +++++++++++---------- convert/vcf_test.go | 26 ++++++++++++++++---------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/convert/structs.go b/convert/structs.go index 97aadfc..ea7f116 100644 --- a/convert/structs.go +++ b/convert/structs.go @@ -74,22 +74,23 @@ type HeaderLine struct { // The struct for one variant type Variant struct { - Chrom string // The chromosome - Pos string // The position - Id string // The ID - Ref string // The reference allele - Alt string // The alternative allele - Qual string // The quality - Filter string // The filter - Info MapVariantInfoFormat // The info fields - Format MapVariantInfoFormat // The format fields + Chrom string // The chromosome + Pos string // The position + Id string // The ID + Ref string // The reference allele + Alt string // The alternative allele + Qual string // The quality + Filter string // The filter + Info SliceVariantInfoFormat // The info fields + Format SliceVariantInfoFormat // The format fields } // The map for the info and format fields -type MapVariantInfoFormat map[string]VariantInfoFormat +type SliceVariantInfoFormat []VariantInfoFormat // The struct for one info or format field type VariantInfoFormat struct { + Name string // The name of the current INFO or FORMAT field Number string // The number of values that can be included in the INFO field (e.g. 1, 2, A, R) Type string // The type of the header field (e.g. Integer, Float, Character, Flag) Value string // The value of the field diff --git a/convert/vcf.go b/convert/vcf.go index 177f375..ad945af 100644 --- a/convert/vcf.go +++ b/convert/vcf.go @@ -218,18 +218,19 @@ func (v *Vcf) AddVariants(cCtx *cli.Context, config Config) error { } // Get the values of all info fields and transform them to a map -func (mcifs *SliceConfigInfoFormatStruct) getValues(values []string, header []string) (error, MapVariantInfoFormat) { - infoMap := MapVariantInfoFormat{} +func (mcifs *SliceConfigInfoFormatStruct) getValues(values []string, header []string) (error, SliceVariantInfoFormat) { + infoMap := SliceVariantInfoFormat{} for _, v := range *mcifs { err, value := v.getValue(values, header) if err != nil { return err, nil } - infoMap[v.Name] = VariantInfoFormat{ + infoMap = append(infoMap, VariantInfoFormat{ + Name: v.Name, Number: v.Number, Type: v.Type, Value: value, - } + }) } return nil, infoMap } @@ -312,10 +313,10 @@ func (v Variant) String(count int) string { } // Convert the info map to a string -func (mvif MapVariantInfoFormat) infoString() string { +func (mvif SliceVariantInfoFormat) infoString() string { var infoSlice []string - for k, v := range mvif { - upperInfo := strings.ToUpper(k) + for _, v := range mvif { + upperInfo := strings.ToUpper(v.Name) switch infoType := strings.ToLower(v.Type); infoType { case "flag": infoSlice = append(infoSlice, upperInfo) @@ -328,11 +329,11 @@ func (mvif MapVariantInfoFormat) infoString() string { } // Convert the format map to a string -func (mcifs MapVariantInfoFormat) formatString() string { +func (mvif SliceVariantInfoFormat) formatString() string { var formatField []string var formatValues []string - for k, v := range mcifs { - upperFormat := strings.ToUpper(k) + for _, v := range mvif { + upperFormat := strings.ToUpper(v.Name) formatField = append(formatField, upperFormat) formatValues = append(formatValues, v.Value) } diff --git a/convert/vcf_test.go b/convert/vcf_test.go index f585eff..9b34825 100644 --- a/convert/vcf_test.go +++ b/convert/vcf_test.go @@ -213,15 +213,17 @@ func TestVariantString(t *testing.T) { Alt: "C", Qual: "100", Filter: "PASS", - Info: MapVariantInfoFormat{ - "SVLEN": VariantInfoFormat{ + Info: SliceVariantInfoFormat{ + { + Name: "SVLEN", Number: "1", Type: "Integer", Value: "100", }, }, - Format: MapVariantInfoFormat{ - "GT": VariantInfoFormat{ + Format: SliceVariantInfoFormat{ + { + Name: "GT", Number: "1", Type: "String", Value: "0/1", @@ -241,25 +243,29 @@ func TestVariantString(t *testing.T) { Alt: "C", Qual: "100", Filter: "PASS", - Info: MapVariantInfoFormat{ - "SVLEN": VariantInfoFormat{ + Info: SliceVariantInfoFormat{ + { + Name: "SVLEN", Number: "1", Type: "Integer", Value: "100", }, - "SVTYPE": VariantInfoFormat{ + { + Name: "SVTYPE", Number: "1", Type: "String", Value: "DEL", }, }, - Format: MapVariantInfoFormat{ - "GT": VariantInfoFormat{ + Format: SliceVariantInfoFormat{ + { + Name: "GT", Number: "1", Type: "String", Value: "0/1", }, - "CN": VariantInfoFormat{ + { + Name: "CN", Number: "1", Type: "Integer", Value: "2",