Skip to content

Commit

Permalink
add simple support for CSV and TSV
Browse files Browse the repository at this point in the history
  • Loading branch information
nvnieuwk committed Jan 17, 2025
1 parent b547178 commit c85e6fe
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,21 @@ class ValidationExtension extends PluginExtensionPoint {

@Function
public void listToSamplesheet(
final List inputList,
final List<Map> inputList,
final CharSequence samplesheet,
final Object schema = null
) {
def Path samplesheetFile = Nextflow.file(samplesheet) as Path
listToSamplesheet(inputList, samplesheetFile, schemaFile)
listToSamplesheet(inputList, samplesheetFile, schema)
}

@Function
public void listToSamplesheet(
final List inputList,
final List<Map> inputList,
final Path samplesheet,
final Object schema = null
) {
def Path schemaFile = null
def Path schemaFile
if(schema) {
if(schema instanceof String) {
schemaFile = Nextflow.file(getBasePath(session.baseDir.toString(), schema as String)) as Path
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nextflow.validation.exceptions

import groovy.transform.CompileStatic
import nextflow.exception.AbortOperationException
/**
* Exception thrown to notify issues with the samplesheet creation
*
* @author Nicolas Vannieuwkerke <[email protected]>
*/
@CompileStatic
class SamplesheetCreationException extends AbortOperationException {

SamplesheetCreationException(String message) {
super(message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import groovy.util.logging.Slf4j
import java.nio.file.Path

import nextflow.validation.config.ValidationConfig
import nextflow.validation.exceptions.SamplesheetCreationException
import static nextflow.validation.utils.Files.getFileType

/**
*
Expand All @@ -20,10 +22,67 @@ class ListConverter {
}

public void validateAndConvertToSamplesheet(
List inputList,
List<Map> inputList,
Path samplesheet,
Path schema
) {
println("Hi")
def String fileType = getFileType(samplesheet)
if(["csv", "tsv"].contains(fileType) && isNested(inputList)) {
def String msg = "Cannot create a CSV or TSV samplesheet from a list of nested values."
throw new SamplesheetCreationException(msg)
}

switch(fileType) {
case "csv":
createSeparatedValueFile(inputList, samplesheet, ",")
break
case "tsv":
createSeparatedValueFile(inputList, samplesheet, "\t")
break
}
}

private Boolean isNested(List input) {
def Boolean nested = false
input.each { entry ->
entry.each { key, element ->
if(element instanceof List || element instanceof Map) {
nested = true
return
}
}
if(nested) return
}
return nested
}

private List<String> determineHeader(List<Map> inputList) {
def List<String> headers = []
inputList.each { entry ->
entry.each { key, element ->
if(!headers.contains(key)) {
headers.add(key)
}
}
}
return headers
}

private void createSeparatedValueFile(List<Map> inputList, Path samplesheet, String delimiter) {
def List<String> headers = determineHeader(inputList)
def List<String> content = [headers.join(delimiter)]
inputList.each { entry ->
def List<String> entryContent = []
headers.each { header ->
def Object item = entry.get(header)
if(item == null) {
entryContent.add("")
} else {
entryContent.add(item.toString())
}
}
content.add(entryContent.join(delimiter))
}
samplesheet.text = content.join("\n")
}
}
16 changes: 11 additions & 5 deletions plugins/nf-schema/src/main/nextflow/validation/utils/Files.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import groovy.json.JsonGenerator
import groovy.json.JsonSlurper
import groovy.util.logging.Slf4j
import java.nio.file.Path
import java.nio.file.NoSuchFileException

import nextflow.validation.exceptions.SchemaValidationException
import static nextflow.validation.utils.Common.getValueFromJsonPointer
Expand All @@ -34,13 +35,18 @@ public class Files {
return extension == "yml" ? "yaml" : extension
}

def String header = getFileHeader(file)

def Integer commaCount = header.count(",")
def Integer tabCount = header.count("\t")
def Integer commaCount = 0
def Integer tabCount = 0
try {
def String header = getFileHeader(file)
commaCount = header.count(",")
tabCount = header.count("\t")
} catch (NoSuchFileException e) {
log.debug("${file.toString()} does not exist, cannot infer file type from file content.")
}

if ( commaCount == tabCount ){
log.error("Could not derive file type from ${file}. Please specify the file extension (CSV, TSV, YML, YAML and JSON are supported).".toString())
throw new SchemaValidationException("Could not derive file type from ${file}. Please specify the file extension (CSV, TSV, YML, YAML and JSON are supported).".toString())
}
if ( commaCount > tabCount ){
return "csv"
Expand Down

0 comments on commit c85e6fe

Please sign in to comment.