How to read .cwl file in R/Python?

159 views Asked by At

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!

3

There are 3 answers

0
WenliL On BEST ANSWER

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

4
Nuno Carvalho On

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: []
1
Konstantinos K. On
with open(cwl_file_path, 'r') as cwl_file:  
    cwl_dict = yaml.safe_load(cwl_file)

More information for yaml:

https://pyyaml.org/wiki/PyYAMLDocumentation