Skip to content

Commit

Permalink
Merge pull request #3 from nvnieuwk/fix/big-calculations
Browse files Browse the repository at this point in the history
Fix/big calculations
  • Loading branch information
nvnieuwk authored Apr 30, 2024
2 parents 9905510 + 295b7f7 commit aaa69d0
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 92 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# bedgovcf changelog

## v0.1.1 - The Second One

### Fixes

1. Big numbers are now printed correctly using `~round`, `~min` and `~sum`

## v0.1.0 - The First One

The first release of bedgovcf
2 changes: 1 addition & 1 deletion bedgovcf.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
},
Action: func(c *cli.Context) error {
logger := log.New(os.Stderr, "", 0)
err, config := bedgovcf.ReadConfig(c.String("config"))
config, err := bedgovcf.ReadConfig(c.String("config"))
if err != nil {
logger.Fatal(err)
}
Expand Down
9 changes: 4 additions & 5 deletions convert/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bedgovcf

import (
"errors"
"fmt"
"log"
"os"
Expand All @@ -11,20 +10,20 @@ import (
)

// Read the configuration file, cast it to its struct and validate
func ReadConfig(configString string) (error, Config) {
func ReadConfig(configString string) (Config, error) {
configFile, err := os.ReadFile(configString)
if err != nil {
return errors.New(fmt.Sprintf("Failed to open the config file: %v", err)), Config{}
return Config{}, fmt.Errorf("failed to open the config file: %v", err)
}

var config Config

if err := yaml.Unmarshal(configFile, &config); err != nil {
return errors.New(fmt.Sprintf("Failed to open the config file: %v", err)), Config{}
return Config{}, fmt.Errorf("failed to open the config file: %v", err)
}

config.validate()
return nil, config
return config, nil
}

// Validate the config
Expand Down
51 changes: 25 additions & 26 deletions convert/resolve.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bedgovcf

import (
"errors"
"fmt"
"log"
"math"
Expand All @@ -10,7 +9,7 @@ import (
"strings"
)

func resolveField(configValues []string, bedValues []string, bedHeader []string) (error, string) {
func resolveField(configValues []string, bedValues []string, bedHeader []string) (string, error) {

input := []string{}
for _, v := range configValues {
Expand All @@ -33,38 +32,38 @@ func resolveField(configValues []string, bedValues []string, bedHeader []string)
if strings.HasPrefix(input[0], "~") {
function = configValues[0][1:]
} else {
return nil, strings.Join(input, " ")
return strings.Join(input, " "), nil
}

switch function {
case "round":
// ~round <value>
float, err := strconv.ParseFloat(input[1], 64)
if err != nil {
return errors.New(fmt.Sprintf("Failed to parse the value (%v) to a float: %v", input[1], err)), ""
return "", fmt.Errorf("failed to parse the value (%v) to a float: %v", input[1], err)
}
round := math.Round(float)
if round == -0 {
round = 0
}
return nil, fmt.Sprintf("%v", round)
return strconv.FormatFloat(round, 'f', -1, 64), nil
case "sum":
// ~sum <value1> <value2> ...
var sum float64
for _, v := range input[1:] {
float, err := strconv.ParseFloat(v, 64)
if err != nil {
return errors.New(fmt.Sprintf("Failed to parse the value (%v) to a float: %v", v, err)), ""
return "", fmt.Errorf("failed to parse the value (%v) to a float: %v", v, err)
}
sum += float
}

return nil, strconv.FormatFloat(sum, 'g', -1, 64)
return strconv.FormatFloat(sum, 'f', -1, 64), nil
case "min":
// ~min <startValue> <valueToSubstract1> <valueToSubstract2> ...
min, err := strconv.ParseFloat(input[1], 64)
if err != nil {
return errors.New(fmt.Sprintf("Failed to parse the value (%v) to a float: %v", input[1], err)), ""
return "", fmt.Errorf("failed to parse the value (%v) to a float: %v", input[1], err)
}
for _, v := range input[2:] {
float, err := strconv.ParseFloat(v, 64)
Expand All @@ -73,7 +72,7 @@ func resolveField(configValues []string, bedValues []string, bedHeader []string)
}
min -= float
}
return nil, strconv.FormatFloat(min, 'g', -1, 64)
return strconv.FormatFloat(min, 'f', -1, 64), nil
case "if":
// ~if <value1> <operator> <value2> <value_if_true> <value_if_false>
// supported operators: > < >= <= ==
Expand All @@ -88,15 +87,15 @@ func resolveField(configValues []string, bedValues []string, bedHeader []string)

floatOperators := []string{"<", ">", "<=", ">="}
if slices.Contains(floatOperators, operator) && (err1 != nil || err2 != nil) {
return errors.New(fmt.Sprintf("Failed to parse the values (%v and %v) to a float: %v and %v", v1, v2, err1, err2)), ""
return "", fmt.Errorf("failed to parse the values (%v and %v) to a float: %v and %v", v1, v2, err1, err2)
}

vFalseResolved := ""
var err error
if strings.HasPrefix(vFalse[0], "~") {
err, vFalseResolved = resolveField(vFalse, bedValues, bedHeader)
vFalseResolved, err = resolveField(vFalse, bedValues, bedHeader)
if err != nil {
return err, ""
return "", err
}
} else {
vFalseResolved = strings.Join(vFalse, " ")
Expand All @@ -105,43 +104,43 @@ func resolveField(configValues []string, bedValues []string, bedHeader []string)
switch operator {
case "<":
if floatV1 < floatV2 {
return nil, vTrue
return vTrue, nil
} else {
return nil, vFalseResolved
return vFalseResolved, nil
}
case ">":
if floatV1 > floatV2 {
return nil, vTrue
return vTrue, nil
} else {
return nil, vFalseResolved
return vFalseResolved, nil
}
case ">=":
if floatV1 >= floatV2 {
return nil, vTrue
return vTrue, nil
} else {
return nil, vFalseResolved
return vFalseResolved, nil
}
case "<=":
if floatV1 <= floatV2 {
return nil, vTrue
return vTrue, nil
} else {
return nil, vFalseResolved
return vFalseResolved, nil
}
case "==":
if v1 == v2 {
return nil, vTrue
return vTrue, nil
} else {
return nil, vFalseResolved
return vFalseResolved, nil
}
case "!=":
if v1 != v2 {
return nil, vTrue
return vTrue, nil
} else {
return nil, vFalseResolved
return vFalseResolved, nil
}
}
}

err := errors.New(fmt.Sprintf("The function %v is not supported", function))
return err, ""
err := fmt.Errorf("the function %v is not supported", function)
return "", err
}
Loading

0 comments on commit aaa69d0

Please sign in to comment.