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!
CodePudding user response:
Just found yaml
package in r:
yaml::yaml.load_file("test.cwl")
But please feel to post any better solution.
CodePudding user response:
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
CodePudding user response:
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)