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

Improve samplesheet typecasting #113

Merged
merged 7 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'] ?
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'] ?
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we catch an error here too? And maybe throw a more explanatory message saying that the input is not a value. Although this will in theory be detected by the validation before the channel conversion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that was my reasoning too, the values should be compatible when they are validated so they shouldn't really fail

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 @@ -76,8 +76,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:5, integer2:5, 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:5, integer2:5, 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 @@ -102,8 +102,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:5, integer2:5, 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:5, integer2:5, 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 @@ -128,8 +128,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:5, integer2:5, 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:5, integer2:5, 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