I have a config file that looks like this:
{
"codes": {
"0004F--0004R": {
"Forward code Name": "0004F",
"Forward code Info": "xxxyyy4",
"Rev code Name": "0004R",
"Rev code Info": "xxxyyy3"
},
"0014F--0014R": {
"Forward code Name": "0014F",
"Forward code Info": "xxxyyy1",
"Rev Barcode Name": "0014R",
"Rev Barcode Info": "xxxyyy2"
}
}
}
I need to process this json so that I get an output "fasta" file that looks like this:
>0004F
xxxyyy4
>0004R
xxxyyy3
>0014F
xxxyyy1
>0014R
xxxyyy2
I am essentially a python programmer, so in python my code looks like this:
with open('codes.fasta', 'w') as f:
for k, v in json_object.get('codes', {}).items():
fname, revname = k.split('--')
print(f'>{fname}\n{v["Forward code Info"]}', file=f)
print(f'>{revname}\n{v["Rev code Info"]}', file=f)
I need to write a similar function in Groovy.
In pseudo code: 1. Give config.json 2. Groovy reads the JSON 3. Parses JSON accordingly 4. Outputs a "fasta" file
Any Groovy coders out here?
import groovy.json.*
def jsonObject = new JsonSlurper().parseText '''{
"codes": {
"0004F--0004R": {
"Forward code Name": "0004F",
"Forward code Info": "xxxyyy4",
"Rev code Name": "0004R",
"Rev code Info": "xxxyyy3"
},
"0014F--0014R": {
"Forward code Name": "0014F",
"Forward code Info": "xxxyyy1",
"Rev Barcode Name": "0014R",
"Rev Barcode Info": "xxxyyy2"
}
}
}'''
new File('codes.fasta').withOutputStream { out ->
jsonObject.codes.each { code ->
def (fname, revname) = code.key.split('--')
out << ">$fname\n${code.value['Forward code Info']}\n"
out << ">$revname\n${code.value['Rev Barcode Info']}\n"
}
}
You can write Groovy in a very analogous way.
Instead of parsing using parseText
you'll likely want to call parse(new File(config.json)
or some other similar API method.