DUDANF
DUDANF

Reputation: 3000

Groovy string manipulation in flat map

I'm writing some code for my Nextflow workflow which happens to use groovy.

I have a JSON config file that has these params:

"barcodes": {
    "004F--004R": {
        "laa_params": {
            "--minLength": "5000",
            "--ignoreEnds": "60",
            "--maxReads": "2500",
            "--maxPhasingReads": "500"
        }
    },
    "014F--014R": {
        "laa_params": {
            "--minLength": "5000",
            "--ignoreEnds": "60",
            "--maxReads": "2500",
            "--maxPhasingReads": "500"
        }
    },

And what I need to do is make a map of barcode:laa_params. Currently I do this by:

transform_barcodes = barcodes.flatMap { it ->  [it] }
results = transform_barcodes.flatMap { it ->  [ [it, analysis_config.barcodes[it].laa_params] ] }

And this gives me these results:

DataflowQueue(queue=[DataflowVariable(value=[004F--004R, {--minLength=5000, --ignoreEnds=60, --maxReads=2500, --maxPhasingReads=500}]), DataflowVariable(value=[014F--014R, {--minLength=5000, --ignoreEnds=60, --maxReads=2500, --maxPhasingReads=500}]),...

What I need eventually is:

[004F--004R, {--minLength 5000, --ignoreEnds 60, --maxReads 2500, --maxPhasingReads 500}]

How can I make it so? Im new to groovy but probably just do a for-loop over the map and replace = with " "? Any help would be appreciated!

Upvotes: 0

Views: 284

Answers (1)

daggett
daggett

Reputation: 28599

def json = '''
{
    "barcodes": {
        "004F--004R": {
            "laa_params": {
                "--minLength": "5000",
                "--ignoreEnds": "60",
                "--maxReads": "2500",
                "--maxPhasingReads": "500"
            }
        },
        "014F--014R": {
            "laa_params": {
                "--minLength": "5000",
                "--ignoreEnds": "60",
                "--maxReads": "2500",
                "--maxPhasingReads": "500"
            }
        }
    }
}
'''
json = new groovy.json.JsonSlurper().parseText(json)
def plainMap = json.barcodes.collectEntries{k0,v0-> [k0, v0.laa_params.collect{k1,v1-> "$k1 $v1" }.join(' ') ]}

plainMap.each{k,v-> 
    println "$k :: $v"
}

prints

004F--004R :: --minLength 5000 --ignoreEnds 60 --maxReads 2500 --maxPhasingReads 500
014F--014R :: --minLength 5000 --ignoreEnds 60 --maxReads 2500 --maxPhasingReads 500

Upvotes: 2

Related Questions