Search code examples
pythonrcommon-workflow-language

How to read .cwl file in R/Python?


I try to write a script in R, which requires to modify a .cwl file. Take a minimal example of test.cwl file:

#!/usr/bin/env cwl-runner

cwlVersion: v1.0
class: Workflow

requirements:
  - class: StepInputExpressionRequirement

inputs:
  - id: submissionId
    type: int

outputs: []

Ideally, I want to read this test.cwl and modify the inputs$id. Finally, write out to an updated new_test.cwl file. However, I can't find a way to read this test.cwl file in R? I have tried tidycwl, but it can only read files with ymal or json extension.

If any packages from python will do the trick, I would also be happy to use it with reticulate.

Thank you!


Solution

  • Based on @nuno-carvalho's answer, I added ruamel.yaml to fix indent for arrays:

    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: []
    
    

    I am not good at python, please feel to suggest