|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "YAML Guide" |
| 4 | +permalink: /yaml/ |
| 5 | +--- |
| 6 | + |
| 7 | +<script src="https://unpkg.com/mermaid@8.0.0/dist/mermaid.min.js"></script> |
| 8 | +<script>mermaid.initialize({startOnLoad:true});</script> |
| 9 | + |
| 10 | +[YAML][yaml-homepage] is a file format |
| 11 | +designed to be readable by both computers and humans. |
| 12 | +This guide introduces the features of YAML |
| 13 | +relevant when writing CWL descriptions and input parameter files. |
| 14 | + |
| 15 | +### Contents |
| 16 | + |
| 17 | +- [Key-Value Pairs](/yaml/#key-value-pairs) |
| 18 | +- [Comments](/yaml/#comments) |
| 19 | +- [Mapped Objects](/yaml/#mapped-objects) |
| 20 | +- [Arrays](/yaml/#arrays) |
| 21 | +- [JSON Style](/yaml/#json-style) |
| 22 | + |
| 23 | +### Key-Value Pairs |
| 24 | + |
| 25 | +Fundamentally, a file written in YAML consists of a set of _key-value pairs_. |
| 26 | +Each pair is written as `key: value`, |
| 27 | +where whitespace after the `:` is optional. |
| 28 | +Key names in CWL files should not contain whitespace - |
| 29 | +We use [_camelCase_][camelCase] for multi-word key names |
| 30 | +that have special meaning in the CWL specification |
| 31 | +and underscored key names otherwise. |
| 32 | +For example: |
| 33 | + |
| 34 | +```yaml |
| 35 | +first_name: Bilbo |
| 36 | +last_name: Baggins |
| 37 | +age_years: 111 |
| 38 | +home: Bag End, Hobbiton |
| 39 | +``` |
| 40 | +
|
| 41 | +The YAML above defines four keys - |
| 42 | +`first_name`, `last_name`, `age_years`, and `home` - |
| 43 | +with their four respective values. |
| 44 | +Values can be |
| 45 | +character strings, |
| 46 | +numeric (integer, floating point, or scientfic representation), |
| 47 | +Boolean (`true` or `false`), |
| 48 | +or more complex nested types (see below). |
| 49 | + |
| 50 | +Values may be wrapped in quotation marks |
| 51 | +but be aware that this may change the way that they are interpreted |
| 52 | +i.e. `"1234"` will be treated as a character string |
| 53 | +, while `1234` will be treated as an integer. |
| 54 | +This distinction can be important, |
| 55 | +for example when describing parameters to a command: |
| 56 | +in CWL all parts of `baseCommand` must be strings so, |
| 57 | +if you want to specify a fixed numeric value to a command, |
| 58 | +make sure that you wrap that numeric value in quotes: `baseCommand: [echo, "42"]`. |
| 59 | + |
| 60 | +### Comments |
| 61 | + |
| 62 | +You may use `#` to add comments to your CWL and parameter files. |
| 63 | +Any characters to the right of ` #` will be ignored by the program interpreting |
| 64 | +the YAML. |
| 65 | +For example: |
| 66 | + |
| 67 | +```yaml |
| 68 | +first_name: Bilbo |
| 69 | +last_name: Baggins |
| 70 | +age_years: 111 |
| 71 | +# this line will be ignored by the interpreter |
| 72 | +home: Bag End, Hobbiton # this is ignored too |
| 73 | +``` |
| 74 | + |
| 75 | +If there is anything on the line before the comment, |
| 76 | +be sure to add at least one space before the `#`! |
| 77 | + |
| 78 | +### Maps |
| 79 | + |
| 80 | +When describing a tool or workflow with CWL, |
| 81 | +it is usually necessary to construct more complex, nested representations. |
| 82 | +Called _maps_, |
| 83 | +these hierarchical structures are described in YAML by providing |
| 84 | +additional key-value pairs as the value of any key. |
| 85 | +These pairs (sometimes referred to as "children") are written |
| 86 | +on new lines under the key to which they belong (the "parent"), |
| 87 | +and should be indented with two spaces |
| 88 | +(⇥tab characters are not allowed). |
| 89 | +For example: |
| 90 | + |
| 91 | +```yaml |
| 92 | +cwlVersion: v1.0 |
| 93 | +class: CommandLineTool |
| 94 | +baseCommand: echo |
| 95 | +inputs: # this key has an object value |
| 96 | + example_flag: # so does this one |
| 97 | + type: boolean |
| 98 | + inputBinding: # and this one too |
| 99 | + position: 1 |
| 100 | + prefix: -f |
| 101 | +``` |
| 102 | + |
| 103 | +The YAML above illustrates how you can build up complex nested object |
| 104 | +descriptions relatively quickly. |
| 105 | +The `inputs` map contains a single key, `example_flag`, |
| 106 | +which itself contains two keys, `type` and `inputBinding`, |
| 107 | +while one of these children, `inputBinding`, |
| 108 | +contains a further two key-value pairs (`position` and `prefix`). |
| 109 | +See the "Arrays" section below for more information about providing multiple |
| 110 | +values/key-value pairs for a single key. |
| 111 | +For comparison with the example YAML above, |
| 112 | +here is a graphical representation of the `inputs` object it describes. |
| 113 | + |
| 114 | +<div class="mermaid"> |
| 115 | +graph TD |
| 116 | + inputs --> example_flag |
| 117 | + example_flag --> type |
| 118 | + type --- bool((boolean)) |
| 119 | + example_flag --> inputBinding |
| 120 | + inputBinding --> position |
| 121 | + inputBinding --> prefix |
| 122 | + position --- posval((1)) |
| 123 | + prefix --- prefval(('-f')) |
| 124 | +</div> |
| 125 | + |
| 126 | +### Arrays |
| 127 | + |
| 128 | +In certain circumstances it is necessary to provide |
| 129 | +multiple values or objects for a single key. |
| 130 | +As we've already seen in the "Mapped Objects" section above, |
| 131 | +more than one key-value pair can be mapped to a single key. |
| 132 | +However, it is also possible to define multiple values for a key |
| 133 | +without having to provide a unique key for each value. |
| 134 | +We can achieve this with an _array_, |
| 135 | +where each value is defined on its own line and preceded by `-`. |
| 136 | +For example: |
| 137 | + |
| 138 | +```yaml |
| 139 | +touchfiles: |
| 140 | + - foo.txt |
| 141 | + - bar.dat |
| 142 | + - baz.txt |
| 143 | +``` |
| 144 | + |
| 145 | +and a more complex example combining maps and arrays: |
| 146 | + |
| 147 | +```yaml |
| 148 | +exclusive_parameters: |
| 149 | + type: |
| 150 | + - type: record |
| 151 | + name: itemC |
| 152 | + fields: |
| 153 | + itemC: |
| 154 | + type: string |
| 155 | + inputBinding: |
| 156 | + prefix: -C |
| 157 | + - type: record |
| 158 | + name: itemD |
| 159 | + fields: |
| 160 | + itemD: |
| 161 | + type: string |
| 162 | + inputBinding: |
| 163 | + prefix: -D |
| 164 | +``` |
| 165 | + |
| 166 | +### JSON Style |
| 167 | + |
| 168 | +YAML is based on [JavaScript Object Notation (JSON)][json] |
| 169 | +and maps and arrays can also be defined in YAML using the native JSON syntax. |
| 170 | +For example: |
| 171 | + |
| 172 | +```yaml |
| 173 | +touchfiles: [foo.txt, bar.dat, baz.txt] # equivalent to first Arrays example |
| 174 | +``` |
| 175 | + |
| 176 | +and: |
| 177 | + |
| 178 | +```yaml |
| 179 | +# equivalent to the `inputs` example in "Mapped Objects" above |
| 180 | +inputs: {example_flag: {type: boolean, inputBinding: {position: 1, prefix: -f}}} |
| 181 | +``` |
| 182 | +
|
| 183 | +Native JSON can be useful |
| 184 | +to indicate where a field is being left intentionally empty |
| 185 | +(such as `[]` for an empty array), |
| 186 | +and where it makes more sense |
| 187 | +for the values to be located on the same line |
| 188 | +(such as when providing option flags and their values in a shell command). |
| 189 | +However, as the second example above shows, |
| 190 | +it can severely affect the readability of a YAML file |
| 191 | +and should be used sparingly. |
| 192 | + |
| 193 | +### Reference |
| 194 | + |
| 195 | +The [Learn YAML in Y Minutes][yaml-y-mins] reference was very helpful for us |
| 196 | +while we wrote this guide, |
| 197 | +though it also covers features that are not valid in CWL. |
| 198 | + |
| 199 | +[camelCase]: https://en.wikipedia.org/wiki/Camel_case |
| 200 | +[yaml-y-mins]: https://learnxinyminutes.com/docs/yaml/ |
| 201 | + |
| 202 | +{% include links.md %} |
0 commit comments