diff --git a/plugins/nf-iridanext/src/main/nextflow/iridanext/IridaNextJSONOutput.groovy b/plugins/nf-iridanext/src/main/nextflow/iridanext/IridaNextJSONOutput.groovy index 66c593a..77e8662 100644 --- a/plugins/nf-iridanext/src/main/nextflow/iridanext/IridaNextJSONOutput.groovy +++ b/plugins/nf-iridanext/src/main/nextflow/iridanext/IridaNextJSONOutput.groovy @@ -31,6 +31,7 @@ import net.jimblackler.jsonschemafriend.ValidationException import nextflow.iridanext.MetadataPostProcessor import groovy.transform.CompileStatic +import groovy.transform.Synchronized import groovy.json.JsonOutput import groovy.util.logging.Slf4j @@ -40,9 +41,11 @@ class IridaNextJSONOutput { public static final Schema defaultSchema = loadDefaultOutputSchema() public static final String SAMPLES = "samples" + private static IridaNextJSONOutput instance; + private Map files = ["global": [], (SAMPLES): [:]] private Map metadata = [(SAMPLES): [:]] - private static Map> scopeIds = [(SAMPLES): [] as Set] + private Map> scopeIds = [(SAMPLES): [] as Set] private Path relativizePath private Boolean shouldRelativize private Schema jsonSchema @@ -50,7 +53,7 @@ class IridaNextJSONOutput { private MetadataPostProcessor metadataPostProcessor - public IridaNextJSONOutput(Path relativizePath = null, + private IridaNextJSONOutput(Path relativizePath = null, Schema jsonSchema = null, Boolean validate = false) { this.relativizePath = relativizePath this.shouldRelativize = (this.relativizePath != null) @@ -58,6 +61,15 @@ class IridaNextJSONOutput { this.validate = validate } + @Synchronized + public static IridaNextJSONOutput getInstance() { + if(this.instance == null) { + this.instance = new IridaNextJSONOutput() + } + + return this.instance + } + public void setMetadataPostProcessor(MetadataPostProcessor processor) { this.metadataPostProcessor = processor } @@ -75,10 +87,18 @@ class IridaNextJSONOutput { return validate } + public void setValidate(Boolean validate) { + this.validate = validate + } + public Schema getOutputSchema() { return jsonSchema } + public void setOutputSchema(Schema jsonSchema) { + this.jsonSchema = jsonSchema + } + public Boolean getShouldRelativize() { return shouldRelativize } @@ -87,6 +107,10 @@ class IridaNextJSONOutput { return relativizePath } + public void setRelativizePath(Path relativizePath) { + this.relativizePath = relativizePath + } + public void appendMetadata(String scope, Map data) { if (scope in metadata.keySet()) { Map validMetadata = data.collectEntries { k, v -> @@ -101,7 +125,8 @@ class IridaNextJSONOutput { } } - public static void addId(String scope, String id) { + @Synchronized + public void addId(String scope, String id) { log.trace "Adding scope=${scope} id=${id}" scopeIds[scope].add(id) } diff --git a/plugins/nf-iridanext/src/main/nextflow/iridanext/IridaNextObserver.groovy b/plugins/nf-iridanext/src/main/nextflow/iridanext/IridaNextObserver.groovy index ca3e3ba..68ea54a 100644 --- a/plugins/nf-iridanext/src/main/nextflow/iridanext/IridaNextObserver.groovy +++ b/plugins/nf-iridanext/src/main/nextflow/iridanext/IridaNextObserver.groovy @@ -211,7 +211,11 @@ class IridaNextObserver implements TraceObserver { this.samplesMetadataParsers = this.samplesMetadataParsers.findAll() } - iridaNextJSONOutput = new IridaNextJSONOutput(relativizePath, jsonSchema, validate) + iridaNextJSONOutput = IridaNextJSONOutput.getInstance() + iridaNextJSONOutput.setRelativizePath(relativizePath) + iridaNextJSONOutput.setOutputSchema(jsonSchema) + iridaNextJSONOutput.setValidate(validate) + iridaNextJSONOutput.setMetadataPostProcessor(metadataPostProcessor) } diff --git a/plugins/nf-iridanext/src/main/nextflow/iridanext/SamplesheetParser.groovy b/plugins/nf-iridanext/src/main/nextflow/iridanext/SamplesheetParser.groovy index bb5b7e4..d688d87 100644 --- a/plugins/nf-iridanext/src/main/nextflow/iridanext/SamplesheetParser.groovy +++ b/plugins/nf-iridanext/src/main/nextflow/iridanext/SamplesheetParser.groovy @@ -20,15 +20,17 @@ class SamplesheetParser extends PluginExtensionPoint { } @Operator - DataflowWriteChannel parseSamplesheet( DataflowReadChannel source ) { + DataflowWriteChannel loadIridaSampleIds( DataflowReadChannel source ) { final target = CH.createBy(source) final String scope = IridaNextJSONOutput.SAMPLES + final IridaNextJSONOutput iridaNextJSONOutput = IridaNextJSONOutput.getInstance() final next = { it -> - def meta = it[0] + // Check that it's a map and the id_key exists, ignore and warn otherwise / trace + def meta = it[0] // TODO: Check with workflow that's not just metadata def id = meta[this.id_key] - IridaNextJSONOutput.addId(scope, id) + iridaNextJSONOutput.addId(scope, id) target.bind(it) }