Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #113 from nvnieuwk/improve-samplesheet-typecasting
Browse files Browse the repository at this point in the history
Improve samplesheet typecasting
  • Loading branch information
nvnieuwk authored Oct 16, 2023
2 parents e737858 + 6aba24c commit 9d5060b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 26 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# nextflow-io/nf-validation: Changelog

# Version 1.1.0

## Bug fixes

- Floats and doubles should now be created when using the `number` type in the schema ([#113](https://github.com/nextflow-io/nf-validation/pull/113/))
- When `0` is used as a default value in the schema, a `0` will now be used as the value in the `.fromSamplesheet()` channel instead of `null` ([#114](https://github.com/nextflow-io/nf-validation/pull/114))

# Version 1.0.0

The nf-validation plugin is now in production use across many pipelines and has (we hope) now reached a point of relative stability. The bump to major version v1.0.0 signifies that it is suitable for use in production pipelines.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@ class SamplesheetConverter {
if(metaNames) {
for(name : metaNames) {
meta[name] = (input != '' && input) ?
transform(input, field) :
castToType(input, field) :
field['value']['default'] != null ?
transform(field['value']['default'] as String, field) :
castToType(field['value']['default'] as String, field) :
null
}
}
else {
def inputFile = (input != '' && input) ?
transform(input, field) :
castToType(input, field) :
field['value']['default'] != null ?
transform(field['value']['default'] as String, field) :
castToType(field['value']['default'] as String, field) :
[]
output.add(inputFile)
}
Expand Down Expand Up @@ -240,14 +240,14 @@ class SamplesheetConverter {
}

// Function to transform an input field from the samplesheet to its desired type
private static transform(
private static castToType(
String input,
Map.Entry<String, Map> field
) {
def String type = field['value']['type']
def String key = field.key

// Check and convert string values
// Convert string values
if(type == "string" || !type) {
def String result = input as String

Expand All @@ -262,26 +262,47 @@ class SamplesheetConverter {
return result
}

// Check and convert integer values
else if(type == "integer" || type == "number") {
// Convert number values
else if(type == "number") {
try {
def int result = input as int
return result
}
catch (NumberFormatException e) {
log.debug("Could not convert ${input} to an integer. Trying to convert to a float.")
}

try {
def float result = input as float
return result
}
catch (NumberFormatException e) {
log.debug("Could not convert ${input} to a float. Trying to convert to a double.")
}

def double result = input as double
return result
}

// Stop conversion if there are errors (prevents unwanted exceptions)
if(this.getErrors()){ return }
// Convert integer values
else if(type == "integer") {

// Convert the string value to an integer value and return it
def Integer result = input as Integer
def int result = input as int
return result
}

// Check and convert boolean values
// Convert boolean values
else if(type == "boolean") {

// Convert and return the boolean value
if(input.toLowerCase() == "true") {
return true
}
return false
}

else if(type == "null") {
return null
}
}

private static String addSample (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class SamplesheetConverterTest extends Dsl2Spec{

then:
noExceptionThrown()
stdout.contains("[[string1:fullField, string2:fullField, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25, false, ${this.getRootString()}/src/testResources/test.txt, ${this.getRootString()}/src/testResources/testDir, ${this.getRootString()}/src/testResources/test.txt, unique1, 1, itDoesExist]" as String)
stdout.contains("[[string1:value, string2:value, integer1:0, integer2:0, boolean1:true, boolean2:true], string1, 25, false, [], [], [], [], [], itDoesExist]")
stdout.contains("[[string1:fullField, string2:fullField, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25.12, false, ${this.getRootString()}/src/testResources/test.txt, ${this.getRootString()}/src/testResources/testDir, ${this.getRootString()}/src/testResources/test.txt, unique1, 1, itDoesExist]" as String)
stdout.contains("[[string1:value, string2:value, integer1:0, integer2:0, boolean1:true, boolean2:true], string1, 25.08, false, [], [], [], [], [], itDoesExist]")
stdout.contains("[[string1:dependentRequired, string2:dependentRequired, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25, false, [], [], [], unique2, 1, itDoesExist]")
stdout.contains("[[string1:extraField, string2:extraField, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25, false, ${this.getRootString()}/src/testResources/test.txt, ${this.getRootString()}/src/testResources/testDir, ${this.getRootString()}/src/testResources/testDir, unique3, 1, itDoesExist]" as String)
}
Expand All @@ -104,8 +104,8 @@ class SamplesheetConverterTest extends Dsl2Spec{

then:
noExceptionThrown()
stdout.contains("[[string1:fullField, string2:fullField, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25, false, ${this.getRootString()}/src/testResources/test.txt, ${this.getRootString()}/src/testResources/testDir, ${this.getRootString()}/src/testResources/test.txt, unique1, 1, itDoesExist]" as String)
stdout.contains("[[string1:value, string2:value, integer1:0, integer2:0, boolean1:true, boolean2:true], string1, 25, false, [], [], [], [], [], itDoesExist]")
stdout.contains("[[string1:fullField, string2:fullField, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25.12, false, ${this.getRootString()}/src/testResources/test.txt, ${this.getRootString()}/src/testResources/testDir, ${this.getRootString()}/src/testResources/test.txt, unique1, 1, itDoesExist]" as String)
stdout.contains("[[string1:value, string2:value, integer1:0, integer2:0, boolean1:true, boolean2:true], string1, 25.08, false, [], [], [], [], [], itDoesExist]")
stdout.contains("[[string1:dependentRequired, string2:dependentRequired, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25, false, [], [], [], unique2, 1, itDoesExist]")
stdout.contains("[[string1:extraField, string2:extraField, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25, false, ${this.getRootString()}/src/testResources/test.txt, ${this.getRootString()}/src/testResources/testDir, ${this.getRootString()}/src/testResources/testDir, unique3, 1, itDoesExist]" as String)
}
Expand All @@ -131,8 +131,8 @@ class SamplesheetConverterTest extends Dsl2Spec{

then:
noExceptionThrown()
stdout.contains("[[string1:fullField, string2:fullField, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25, false, ${this.getRootString()}/src/testResources/test.txt, ${this.getRootString()}/src/testResources/testDir, ${this.getRootString()}/src/testResources/test.txt, unique1, 1, itDoesExist]" as String)
stdout.contains("[[string1:value, string2:value, integer1:0, integer2:0, boolean1:true, boolean2:true], string1, 25, false, [], [], [], [], [], itDoesExist]")
stdout.contains("[[string1:fullField, string2:fullField, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25.12, false, ${this.getRootString()}/src/testResources/test.txt, ${this.getRootString()}/src/testResources/testDir, ${this.getRootString()}/src/testResources/test.txt, unique1, 1, itDoesExist]" as String)
stdout.contains("[[string1:value, string2:value, integer1:0, integer2:0, boolean1:true, boolean2:true], string1, 25.08, false, [], [], [], [], [], itDoesExist]")
stdout.contains("[[string1:dependentRequired, string2:dependentRequired, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25, false, [], [], [], unique2, 1, itDoesExist]")
stdout.contains("[[string1:extraField, string2:extraField, integer1:10, integer2:10, boolean1:true, boolean2:true], string1, 25, false, ${this.getRootString()}/src/testResources/test.txt, ${this.getRootString()}/src/testResources/testDir, ${this.getRootString()}/src/testResources/testDir, unique3, 1, itDoesExist]" as String)
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/nf-validation/src/testResources/correct.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
field_1,field_2,field_3,field_4,field_5,field_6,field_7,field_8,field_9,field_10,field_11
fullField,10,true,string1,25,false,src/testResources/test.txt,src/testResources/testDir,src/testResources/test.txt,unique1,1
,,,string1,25,false,,,,,,
fullField,10,true,string1,25.12,false,src/testResources/test.txt,src/testResources/testDir,src/testResources/test.txt,unique1,1
,,,string1,25.08,false,,,,,,
dependentRequired,10,true,string1,25,false,,,,unique2,1
extraField,10,true,string1,25,false,src/testResources/test.txt,src/testResources/testDir,src/testResources/testDir,unique3,1
4 changes: 2 additions & 2 deletions plugins/nf-validation/src/testResources/correct.tsv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
field_1 field_2 field_3 field_4 field_5 field_6 field_7 field_8 field_9 field_10 field_11
fullField 10 true string1 25 false src/testResources/test.txt src/testResources/testDir src/testResources/test.txt unique1 1
string1 25 false
fullField 10 true string1 25.12 false src/testResources/test.txt src/testResources/testDir src/testResources/test.txt unique1 1
string1 25.08 false
dependentRequired 10 true string1 25 false unique2 1
extraField 10 true string1 25 false src/testResources/test.txt src/testResources/testDir src/testResources/testDir unique3 1
4 changes: 2 additions & 2 deletions plugins/nf-validation/src/testResources/correct.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
field_2: 10
field_3: true
field_4: string1
field_5: 25
field_5: 25.12
field_6: false
field_7: src/testResources/test.txt
field_8: src/testResources/testDir
field_9: src/testResources/test.txt
field_10: unique1
field_11: 1
- field_4: string1
field_5: 25
field_5: 25.08
field_6: false
- field_1: dependentRequired
field_2: 10
Expand Down

0 comments on commit 9d5060b

Please sign in to comment.