Skip to content

Commit

Permalink
Merge pull request #294 from ewels/intro-example-python
Browse files Browse the repository at this point in the history
Extend the 'Your first script' section.
  • Loading branch information
ewels authored Dec 11, 2023
2 parents 5c98afd + cace0e4 commit 6667683
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 25 deletions.
23 changes: 16 additions & 7 deletions .github/gitpod.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM gitpod/workspace-base
USER root

# Install util tools.
# software-properties-common is needed to add ppa support for Apptainer installation
RUN apt-get update --quiet && \
apt-get install --quiet --yes \
apt-transport-https \
Expand All @@ -13,7 +14,8 @@ RUN apt-get update --quiet && \
wget \
curl \
tree \
graphviz
graphviz \
software-properties-common

# Install Conda
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
Expand All @@ -24,12 +26,11 @@ ENV PATH="/opt/conda/bin:$PATH"

# User permissions
RUN mkdir -p /workspace/data \
&& chown -R gitpod:gitpod /workspace/data \
&& chown -R gitpod:gitpod /opt/conda
&& chown -R gitpod:gitpod /opt/conda /workspace/data

# Change user to gitpod
USER gitpod


# Uncomment if we need to pin the Nextflow version
# ENV NXF_EDGE=1
# ENV NXF_VER=22.09.7-edge
Expand All @@ -39,12 +40,20 @@ RUN conda config --add channels defaults && \
conda config --add channels bioconda && \
conda config --add channels conda-forge && \
conda config --set channel_priority strict && \
conda install --quiet --yes --name base mamba && \
mamba install --quiet --yes --name base \
conda update --quiet --yes --all && \
conda install --quiet --yes --name base \
mamba \
nextflow \
nf-core \
nf-test \
black \
prettier \
pre-commit \
pytest-workflow && \
mamba clean --all -f -y
conda clean --all --force-pkgs-dirs --yes

# Update Nextflow
RUN nextflow self-update && nextflow -version

RUN unset JAVA_TOOL_OPTIONS

Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# - Only pushes if push or release
# - Builds without push for PRs to check the Dockerfile

name: Build Docker image
name: Build GitPod Docker image

on:
pull_request:
Expand All @@ -14,29 +14,29 @@ on:
jobs:
push_to_registry:
if: github.repository == 'nextflow-io/training'
name: Build + Push Docker image
name: Build + Push Gitpod Docker image
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build / push latest image
uses: docker/build-push-action@v3
uses: docker/build-push-action@v5
if: github.event_name != 'release'
with:
file: .github/gitpod.Dockerfile
push: ${{ github.event_name == 'push' }}
tags: ghcr.io/nextflow-io/training:latest

- name: Push release image
uses: docker/build-push-action@v3
uses: docker/build-push-action@v5
if: github.event_name == 'release'
with:
file: .github/gitpod.Dockerfile
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/mkdocs-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build / push latest image
uses: docker/build-push-action@v3
uses: docker/build-push-action@v5
with:
file: .github/mkdocs.Dockerfile
push: ${{ github.event_name == 'push' }}
Expand Down
4 changes: 2 additions & 2 deletions docs/advanced/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ The latest releases can be viewed on GitHub [here](https://github.com/nextflow-i
If you want to use a specific version of Nextflow, you can set the `NXF_VER` variable as shown below:

```bash
export NXF_VER=23.04.1
export NXF_VER=23.10.0
```

!!! Note

This tutorial workshop requires `NXF_VER=23.04.1`, or later. This version will use DSL2 as default.
This tutorial workshop requires `NXF_VER=23.10.0`, or later.

Run `nextflow -version` again to confirm that the change has taken effect.
20 changes: 19 additions & 1 deletion docs/basic_training/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,25 @@ workflow { // (18)!
21. The final output (in the `results_ch` channel) is printed to screen using the `view` operator (appended onto the channel name).
22. End of the workflow scope.

The use of the operator `.flatten()` here is to split the two files into two separate items to be put through the next process (else they would be treated as a single element).
This pipeline takes `params.greeting`, which defaults to the string `Hello world!`, and splits it into indiviudual words in the `SPLITLETTERS` process. Each word is written to a separate file, named `chunk_aa`, `chunk_ab`, `chunk_ac`and so on. These files are picked up as the process `output`.

The second process `CONVERTTOUPPER` takes the output channel from the first process as its input.
The use of the operator `.flatten()` here is to split the two files into two separate items to be put through the this process, else they would be treated as a single element.
The `CONVERTTOUPPER` process thus launches two tasks, one for each element. The bash script uses `cat` to print the file contents and `tr` to convert to upper-case. It takes the resulting standard-out as the process output channel.

#### Python instead of bash

If you're not completely comfortable with the bash code used in the example, don't worry! You can use whatever programming language you like within Nextflow `script` blocks. For example, here is the same example using Python code:

```python title="nf-training/hello_py.nf" linenums="14"
--8<-- "nf-training/hello_py.nf:14:18"
```

```python title="nf-training/hello_py.nf" linenums="30"
--8<-- "nf-training/hello_py.nf:30:32"
```

Note that the `$x` and `$y` variables are interpolated by Nextflow, so the resulting Python scripts will have fixed strings here (`#!python x="Hello world!"`).

### In practice

Expand Down
4 changes: 2 additions & 2 deletions docs/basic_training/setup.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ Les dernières versions peuvent être consultées sur GitHub [ici](https://githu
Si vous souhaitez utiliser une version spécifique de Nextflow, vous pouvez définir la variable `NXF_VER` comme indiqué ci-dessous :

```bash
export NXF_VER=23.04.1
export NXF_VER=23.10.0
```

!!! Remarque

Cet atelier tutoriel nécessite `NXF_VER=23.04.1`, ou une version plus récente. Cette version utilise DSL2 par défaut.
Cet atelier tutoriel nécessite `NXF_VER=23.10.0`, ou une version plus récente.

Exécutez `nextflow -version` à nouveau pour confirmer que le changement a pris effet.
4 changes: 2 additions & 2 deletions docs/basic_training/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ The latest releases can be viewed on GitHub [here](https://github.com/nextflow-i
If you want to use a specific version of Nextflow, you can set the `NXF_VER` variable as shown below:

```bash
export NXF_VER=23.04.1
export NXF_VER=23.10.0
```

!!! Note

This tutorial workshop requires `NXF_VER=23.04.1`, or later. This version will use DSL2 as default.
This tutorial workshop requires `NXF_VER=23.10.0`, or later.

Run `nextflow -version` again to confirm that the change has taken effect.
4 changes: 2 additions & 2 deletions docs/basic_training/setup.pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ Os últimos lançamentos podem ser vistos no GitHub [aqui](https://github.com/ne
Se você deseja usar uma versão específica do Nextflow, pode definir a variável `NXF_VER` conforme mostrado abaixo:

```bash
export NXF_VER=23.04.1
export NXF_VER=23.10.0
```

!!! Note

Este treinamento requer `NXF_VER=23.04.1`, ou posterior. Esta versão usará a DSL2 como padrão.
Este treinamento requer `NXF_VER=23.10.0`, ou posterior. Esta versão usará a DSL2 como padrão.

Execute `nextflow -version` novamente para confirmar que a alteração entrou em vigor.
41 changes: 41 additions & 0 deletions nf-training/hello_py.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env nextflow

params.greeting = 'Hello world!'
greeting_ch = Channel.of(params.greeting)

process SPLITLETTERS {
input:
val x

output:
path 'chunk_*'

"""
#!/usr/bin/env python
x="$x"
for i, word in enumerate(x.split()):
with open(f"chunk_{i}", "w") as f:
f.write(word)
"""
}

process CONVERTTOUPPER {
input:
path y

output:
stdout

"""
#!/usr/bin/env python
with open("$y") as f:
print(f.read().upper())
"""
}

workflow {
letters_ch = SPLITLETTERS(greeting_ch)
results_ch = CONVERTTOUPPER(letters_ch.flatten())
results_ch.view{ it }
}

0 comments on commit 6667683

Please sign in to comment.