# epi2me-labs/wf-flu architecture

> Architecture diagram of the GitHub repository epi2me-labs/wf-flu, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/epi2me-labs/wf-flu
- Repository: https://github.com/epi2me-labs/wf-flu

## Overview

##### Project Type and Purpose
- This is a bioinformatics data‐processing workflow built with Nextflow.
- Its goal is to take Nanopore FASTQ reads from Influenza A/B amplification, run them through a modular pipeline of filtering, alignment, variant calling, consensus building, typing and reporting, and produce per-sample and aggregated outputs.
- It is intended to run either on a local machine or HPC/cluster via Nextflow, using Docker or Singularity containers for reproducibility.

##### Key Architectural Components
a. Orchestrator
- Nextflow Engine (main.nf + lib/*.nf + Groovy helper classes in lib/*.groovy + nextflow.config).
- Profiles for Docker/Singularity, resource settings, parameter definitions.
b. Workflow Definition
- main.nf: core pipeline steps defined in Nextflow DSL.
- lib/common.nf, lib/ingress.nf: shared DSL modules.
- lib/WorkflowMain.groovy, lib/ArgumentParser.groovy, etc.: Groovy classes to parse parameters and bootstrap.
c. Glue & Utility Scripts
- bin/workflow_glue/*.py: Python scripts for database creation, parsing, report scaffolding, INSaFLU abricate helper, etc.
- bin/workflow_glue/wfg_helpers/*.py: small utilities for checking inputs, reheadering, depth calculations.
d. Reference Data & Resources
- data/primer_schemes: primer definitions and blastdb files.
- data/nextclade.csv: clade assignment reference.
- output_definition.json & nextflow_schema.json: JSON schemas for validation and output manifest.
e. External Command-Line Tools (invoked within pipeline)
- minimap2, samtools, medaka, bcftools, abricate (INSaFLU DB), nextclade.
f. Input & Test Data
- User-provided FASTQ files (single, directory or multiplexed with barcodes + sample_sheet).
- test_data/: sample FASTQs and sample sheets for automated tests.
g. Reporting & Output
- HTML report (wf-flu-report.html), CSV results (wf-flu-results.csv), per-sample BAMs, VCFs, FASTAs, depth metrics.
h. CI & Quality
- .gitlab-ci.yml, pre-commit config, tests in bin/workflow_glue/tests.

##### Relationships and Data Flow
- User invokes `nextflow run main.nf` with parameters → Nextflow Engine reads nextflow.config → bootstrap DSL modules (lib/) → ingest input FASTQs and sample_sheet.
- Nextflow schedules processes as a DAG:
1. concatenate_and_filter_reads (filters reads <200 bases)
2. align_reads (minimap2 → SAM/BAM)
3. calculate_coverage (samtools depth)
4. variant_calling (medaka)
5. build_consensus (bcftools)
6. typing_insaflu (abricate + INSaFLU DB)
7. nextclade_assignment
8. assemble_report (Python scripts gather outputs and generate HTML/CSV)
- Each process runs inside a container (Docker/Singularity) as defined in the profile.

##### Architectural Patterns & Principles
- Pipeline (DAG) pattern: clearly separated stages with defined inputs/outputs.
- Modularization: Nextflow DSL modules + external helper scripts keep logic decoupled.
- Containerization: ensures reproducible environments.
- Schema-driven I/O: JSON schemas validate outputs, sample sheets map barcodes → aliases.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Input Layer
    subgraph "Input Layer"
        direction TB
        FASTQ["Input FASTQ (fastq/, rbk_fastq/)"]:::external
        SampleSheet["Sample Sheet (CSV)"]:::external
        TestData["Test Data"]:::external
    end

    %% Orchestrator
    subgraph "Orchestrator"
        direction TB
        NFEngine["Nextflow Engine"]:::orchestrator
        Config["nextflow.config"]:::orchestrator
        DSLCommon["lib/common.nf"]:::orchestrator
        DSLIngress["lib/ingress.nf"]:::orchestrator
        WFMain["lib/WorkflowMain.groovy"]:::orchestrator
        ArgParser["lib/ArgumentParser.groovy"]:::orchestrator
        CWUtil["lib/CWUtil.groovy"]:::orchestrator
        Schema["lib/NfcoreSchema.groovy"]:::orchestrator
        Template["lib/NfcoreTemplate.groovy"]:::orchestrator
        Pinguscript["lib/Pinguscript.groovy"]:::orchestrator
        ExternalDeps["lib/nfcore_external_java_deps.jar"]:::orchestrator
    end

    %% Reference Data & Resources
    subgraph "Reference Data & Schemas"
        direction TB
        Primer["data/primer_schemes/"]:::datastore
        CladeRef["data/nextclade.csv"]:::datastore
        OutputSchema["output_definition.json"]:::datastore
        NFSchema["nextflow_schema.json"]:::datastore
    end

    %% Pipeline Processes
    subgraph "Pipeline Stages"
        direction TB
        Filter["1. Filter Reads<br/>(min length ≥200)"]:::process
        Align["2. Align Reads<br/>(minimap2 → BAM)"]:::process
        Coverage["3. Calculate Coverage<br/>(samtools depth)"]:::process
        Variant["4. Variant Calling<br/>(medaka)"]:::process
        Consensus["5. Build Consensus<br/>(bcftools)"]:::process
        Typing["6. Typing (INSaFLU)"]:::process
        Clade["7. Nextclade Assignment"]:::process
        Report["8. Assemble Report"]:::process
    end

    %% Glue Scripts
    subgraph "Glue & Utility Scripts"
        direction TB
        CreateDB["create_ncbi_database.py"]:::script
        NextcladeHelper["nextclade_helper.py"]:::script
        PrepareINS["prepare_insaflu.py"]:::script
        ProcessAbr["process_abricate.py"]:::script
        ReportPy["report.py"]:::script
        UtilPy["util.py"]:::script
        Models["models/common.py"]:::script
        WFGHelpers["wfg_helpers/"]:::script
    end

    %% Output Layer
    subgraph "Output"
        direction TB
        PerSample["Per-sample BAM/VCF/FASTA"]:::output
        CSVReport["wf-flu-results.csv"]:::output
        HTMLReport["wf-flu-report.html"]:::output
        DepthMetrics["Depth Metrics CSV"]:::output
    end

    %% Data Flow
    FASTQ -->|FASTQ + CSV| NFEngine
    SampleSheet --> NFEngine
    TestData --> NFEngine

    NFEngine -->|reads config| Config
    NFEngine --> DSLCommon
    NFEngine --> DSLIngress
    NFEngine --> WFMain
    NFEngine --> ArgParser

    Primer --> Filter
    Filter --> Align
    Align --> Coverage
    Coverage --> Variant
    Variant --> Consensus
    Consensus --> Typing
    Typing --> Clade
    Clade --> Report

    %% Glue scripts invoked at specific stages
    Filter --> CreateDB
    Clade --> NextcladeHelper
    Typing --> ProcessAbr
    Report --> ReportPy
    Report --> UtilPy
    Report --> PrepareINS
    Report --> Models
    Report --> WFGHelpers

    Report --> PerSample
    Report --> CSVReport
    Report --> HTMLReport
    Coverage --> DepthMetrics

    %% Click events
    click FASTQ "https://github.com/epi2me-labs/wf-flu/tree/master/test_data/"
    click SampleSheet "https://github.com/epi2me-labs/wf-flu/tree/master/test_data/"
    click NFEngine "https://github.com/epi2me-labs/wf-flu/blob/master/main.nf"
    click Config "https://github.com/epi2me-labs/wf-flu/blob/master/nextflow.config"
    click DSLCommon "https://github.com/epi2me-labs/wf-flu/blob/master/lib/common.nf"
    click DSLIngress "https://github.com/epi2me-labs/wf-flu/blob/master/lib/ingress.nf"
    click WFMain "https://github.com/epi2me-labs/wf-flu/blob/master/lib/WorkflowMain.groovy"
    click ArgParser "https://github.com/epi2me-labs/wf-flu/blob/master/lib/ArgumentParser.groovy"
    click CWUtil "https://github.com/epi2me-labs/wf-flu/blob/master/lib/CWUtil.groovy"
    click Schema "https://github.com/epi2me-labs/wf-flu/blob/master/lib/NfcoreSchema.groovy"
    click Template "https://github.com/epi2me-labs/wf-flu/blob/master/lib/NfcoreTemplate.groovy"
    click Pinguscript "https://github.com/epi2me-labs/wf-flu/blob/master/lib/Pinguscript.groovy"
    click ExternalDeps "https://github.com/epi2me-labs/wf-flu/blob/master/lib/nfcore_external_java_deps.jar"
    click CreateDB "https://github.com/epi2me-labs/wf-flu/blob/master/bin/workflow_glue/create_ncbi_database.py"
    click NextcladeHelper "https://github.com/epi2me-labs/wf-flu/blob/master/bin/workflow_glue/nextclade_helper.py"
    click PrepareINS "https://github.com/epi2me-labs/wf-flu/blob/master/bin/workflow_glue/prepare_insaflu.py"
    click ProcessAbr "https://github.com/epi2me-labs/wf-flu/blob/master/bin/workflow_glue/process_abricate.py"
    click ReportPy "https://github.com/epi2me-labs/wf-flu/blob/master/bin/workflow_glue/report.py"
    click UtilPy "https://github.com/epi2me-labs/wf-flu/blob/master/bin/workflow_glue/util.py"
    click Models "https://github.com/epi2me-labs/wf-flu/blob/master/bin/workflow_glue/models/common.py"
    click WFGHelpers "https://github.com/epi2me-labs/wf-flu/tree/master/bin/workflow_glue/wfg_helpers/"

    %% Styles
    classDef orchestrator fill:#d3d3d3,stroke:#333,stroke-width:1px
    classDef datastore fill:#add8e6,stroke:#333,stroke-width:1px
    classDef process fill:#90ee90,stroke:#333,stroke-width:1px
    classDef script fill:#ffdab9,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef external fill:#fffacd,stroke:#333,stroke-width:1px
    classDef output fill:#dda0dd,stroke:#333,stroke-width:1px
```
