Skip to content

Commit 55b9b47

Browse files
authored
Merge branch 'gh-pages' into 140-ask-better-questions
2 parents 8f38f81 + 5d25e88 commit 55b9b47

File tree

5 files changed

+224
-9
lines changed

5 files changed

+224
-9
lines changed

_episodes/02-1st-example.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ keypoints:
1414
- "The tool description and input files are provided as arguments to a CWL runner."
1515
---
1616
The simplest "hello world" program. This accepts one input parameter, writes a message to the terminal or job log, and produces
17-
no permanent output. CWL documents are written in [JSON][json] or [YAML][yaml], or a mix of the two.
17+
no permanent output.
18+
CWL documents are written in [JSON][json] or [YAML][yaml-homepage], or a mix of the two.
19+
We will use YAML throughout this guide.
20+
If you are not familiar with YAML,
21+
you may find it helpful to refer to
22+
[this quick tutorial for the subset of YAML used in CWL](/yaml).
1823

1924
First, create a file called `1st-tool.cwl`, containing the boxed text below. It will help you to use a text editor that can be
2025
specified to produce text in YAML or JSON. Whatever text editor you use, the indents you see should not be created using tabs.
@@ -79,9 +84,16 @@ inputs:
7984
~~~
8085
{: .source}
8186

82-
The `inputs` section describes the inputs of the tool. This is a list of input parameters and each parameter includes an
83-
identifier, a data type, and optionally an `inputBinding` which describes how this input parameter should appear on the command
84-
line. In this example, the `position` field indicates where it should appear on the command line.
87+
The `inputs` section describes the inputs of the tool.
88+
This is a mapped list of input parameters
89+
(see the [YAML Guide](/yaml#mapped-objects) for more about the format)
90+
and each parameter includes an identifier,
91+
a data type,
92+
and optionally an `inputBinding`.
93+
The `inputBinding` describes how this input parameter should appear
94+
on the command line.
95+
In this example,
96+
the `position` field indicates where it should appear on the command line.
8597

8698
~~~
8799
outputs: []
@@ -90,8 +102,6 @@ outputs: []
90102

91103
This tool has no formal output, so the `outputs` section is an empty list.
92104

93-
[json]: http://json.org
94-
[yaml]: http://yaml.org
95105
[echo]: http://www.linfo.org/echo.html
96106

97107
{% include links.md %}

_episodes/09-array-inputs.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ separator string.
7575

7676
Note that the arrays of inputs are specified inside square brackets `[]` in `array-inputs-job.yml`. Arrays can also be expressed over multiple lines, where
7777
array values that are not defined with an associated key is marked by a leading
78-
`-`, as demonstrated in the next lesson.
78+
This will be demonstrated in the next lesson
79+
and is discussed in more detail in the [YAML Guide](/yaml#arrays).
7980
You can specify arrays of arrays, arrays of records, and other complex types.
8081

8182
{% include links.md %}

_episodes/10-array-outputs.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ Final process status is success
5454
~~~
5555
{: .output}
5656

57-
The array of expected outputs is specified in `array-outputs-job.yml` with each
57+
As described in the [YAML Guide](/yaml#arrays),
58+
the array of expected outputs is specified in `array-outputs-job.yml` with each
5859
entry marked by a leading `-`. This format can also be used in CWL descriptions
5960
to mark entries in arrays, as demonstrated in several of the upcoming sections.
6061

_extras/yaml.md

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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 %}

_includes/links.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
[jekyll-windows]: http://jekyll-windows.juthilo.com/
1919
[jekyll]: https://jekyllrb.com/
2020
[jupyter]: https://jupyter.org/
21+
[json]: http://json.org
2122
[lc-lessons]: https://librarycarpentry.org/#portfolio
2223
[lesson-example]: https://carpentries.github.io/lesson-example/
2324
[mit-license]: https://opensource.org/licenses/mit-license.html
@@ -37,4 +38,4 @@
3738
[swc-lessons]: https://software-carpentry.org/lessons/
3839
[swc-releases]: https://github.com/swcarpentry/swc-releases
3940
[workshop-repo]: {{ site.workshop_repo }}
40-
[yaml]: http://yaml.org/
41+
[yaml-homepage]: http://yaml.org/

0 commit comments

Comments
 (0)