Skip to content

Commit

Permalink
Moving to singleton design.
Browse files Browse the repository at this point in the history
  • Loading branch information
emarinier committed Jan 31, 2025
1 parent 60263fc commit ea4271c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -40,24 +41,35 @@ 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<String,Set<String>> scopeIds = [(SAMPLES): [] as Set<String>]
private Map<String,Set<String>> scopeIds = [(SAMPLES): [] as Set<String>]
private Path relativizePath
private Boolean shouldRelativize
private Schema jsonSchema
private Boolean validate
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)
this.jsonSchema = jsonSchema
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
}
Expand All @@ -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
}
Expand All @@ -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 ->
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit ea4271c

Please sign in to comment.