How to read .cwl file in R/Python?

Solution 1:

Install PyYAML:

pip install PyYAML==6.0

Run this script:

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)

Later Edit suggested by WenliL to fix identation

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)

Output:

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