如何读取 R/Python 中的 .cwl 文件?

How to read .cwl file in R/Python?

我尝试用R写一个脚本,需要修改一个.cwl文件。以 test.cwl 文件为例:

#!/usr/bin/env cwl-runner

cwlVersion: v1.0
class: Workflow

requirements:
  - class: StepInputExpressionRequirement

inputs:
  - id: submissionId
    type: int

outputs: []

理想情况下,我想阅读这个test.cwl并修改inputs$id。最后,写入更新的 new_test.cwl 文件。但是,我找不到在 R 中读取此 test.cwl 文件的方法?我试过 tidycwl,但它只能读取扩展名为 ymaljson 的文件。

如果 python 中的任何软件包都可以解决问题,我也很乐意将其与 reticulate 一起使用。

谢谢!

根据@nuno-carvalho的回答,我添加了ruamel.yaml来修复数组的缩进:

pip install ruamel.yaml
from ruamel.yaml import YAML

yaml = YAML()
with open("test.cwl", 'r') as cwl_file:
    cwl_dict = yaml.load(cwl_file)

with open("test-new.cwl", 'w') as cwl_file:
    cwl_dict["inputs"] = [{"id": 2, "type": "ABC"}]
    yaml.indent(mapping=2, sequence=4, offset=2)
    yaml.dump(cwl_dict, cwl_file)

输出:

cwlVersion: v1.0
class: Workflow
requirements:
  - class: StepInputExpressionRequirement
inputs:
  - id: 2
    type: ABC
outputs: []

本人不擅长python,有感请多多指教

安装 PyYAML:

pip install PyYAML==6.0

运行 这个脚本:

import yaml
# Read file
with open("test.cwl", 'r') as cwl_file:  
    cwl_dict = yaml.safe_load(cwl_file)

# Write file
with open("test-new.cwl", 'w') as cwl_file:
    cwl_dict["inputs"] = [{"id" : 2, "type": "ABC"}]
    yaml.dump(cwl_dict, cwl_file)

稍后根据 WenliL 建议进行编辑以修复标识

pip install ruamel.yaml
from ruamel.yaml import YAML

yaml = YAML()
with open("test.cwl", 'r') as cwl_file:
    cwl_dict = yaml.load(cwl_file)

with open("test-new.cwl", 'w') as cwl_file:
    cwl_dict["inputs"] = [{"id": 2, "type": "ABC"}]
    yaml.indent(mapping=2, sequence=4, offset=2)
    yaml.dump(cwl_dict, cwl_file)

输出:

cwlVersion: v1.0
class: Workflow
requirements:
  - class: StepInputExpressionRequirement
inputs:
  - id: 2
    type: ABC
outputs: []
with open(cwl_file_path, 'r') as cwl_file:  
    cwl_dict = yaml.safe_load(cwl_file)

关于 yaml 的更多信息:

https://pyyaml.org/wiki/PyYAMLDocumentation