将 JSON 处理为 Fasta,将 Python 代码转换为 Groovy

Process JSON to Fasta, convert Python code to Groovy

我有一个如下所示的配置文件:

{
    "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.json 2. Groovy 读取 JSON 3.相应地解析JSON 4.输出一个"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.