我有一个看起来像这样的配置文件:
{
"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"
}
}
}
我需要处理此json,以便获得如下所示的输出“ fasta”文件:
>0004F
xxxyyy4
>0004R
xxxyyy3
>0014F
xxxyyy1
>0014R
xxxyyy2
我本质上是一个python程序员,所以在python中我的代码看起来像这样:
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)
我需要在Groovy中编写类似的函数。
使用伪代码:1.给config.json2. Groovy读取JSON3.相应地解析JSON4.输出一个“ fasta”文件
这里有Groovy编码员吗?
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"
}
}
您可以以非常类似的方式编写Groovy。
代替使用parseText
进行解析,您可能希望调用parse(new File(config.json)
或其他类似的API method。