name: deseq2
namespace: differential_expression
description: |
  Performs differential expression analysis using DESeq2 on bulk samples or pseudobulk samples aggregated from single-cell data.
  Note that this component only considers factors as explanatory variables, and excludes covariates from the analysis.

authors:
  - __merge__: /src/authors/jakub_majercik.yaml
    roles: [ author ]
  - __merge__: /src/authors/dorien_roosen.yaml
    roles: [ author ]
  - __merge__: /src/authors/dries_de_maeyer.yaml
    roles: [ contributor ]
  - __merge__: /src/authors/weiwei_schultz.yaml
    roles: [ contributor ]

argument_groups:
  - name: Inputs
    arguments:
      - name: "--input"
        alternatives: ["-i"]
        type: file
        description: Input h5mu file containing (pseudo-)bulk transcriptomic samples.
        direction: input
        required: true
      - name: "--modality"
        description: |
          Which modality from the input MuData file to process.
        type: string
        default: "rna"
        required: false
      - name: "--input_layer"
        type: string
        required: false
        description: "Input layer to use. If None, X is used. This layer must contain raw counts."
      - name: "--var_gene_names"
        type: string
        description: |
          Name of the .var field that contains gene symbols. If not provided, .var.index will be used.
        required: false
        example: "gene_symbol"
      - name: "--obs_cell_group"
        type: string
        description: |
          .obs field containing the cell group information, for example per cell type or per cell cluster.
          If true, performs per-cell-group analysis with cell-group-specific results.

  - name: Arguments
    arguments:
      - name: "--design_formula"
        type: string
        description: |
          The formula should be a tilde (~) followed by the variables with plus signs between them. 
          The design formula is used to estimate the dispersions and to estimate the log2 fold changes of the model.
        required: true
        example: "~ disease + treatment"
      - name: "--contrast_column"
        type: string
        description: |
          Column in the metadata to use for the contrast.
          This column should contain the conditions to compare.
        required: true
        example: "treatment"
      - name: "--contrast_values"
        type: string
        multiple: true
        description: |
          Values to compare in the contrast column.
          First value is the control group, following values are comparison groups. 
          Values must be present in the fields specified for the design formula.
        required: true
        example: ["ctrl", "stim"]
      - name: "--p_adj_threshold"
        type: double
        min: 0
        max: 1
        description: |
          Adjusted p-value threshold for significance, corrected with the Benjamini and Hochberg method.
          Genes with adjusted p-values below this threshold will be considered significant.
        default: 0.05
        required: false
      - name: "--log2fc_threshold"
        type: double
        min: 0
        description: |
          Log2 fold change threshold for significance.
          Genes with absolute log2 fold change above this threshold will be considered significant.
        default: 0.0
        required: false

  - name: Outputs
    arguments:
      - name: "--output_dir"
        alternatives: ["-o"]
        type: file
        description: |
          Output directory for DESeq2 results. 
          If cell groups are defined (using `--obs_cell_group`), the output folder will contain one CSV per cell group, otherwise it will contain a single output file.
        direction: output
        required: true
      - name: "--output_prefix"
        type: string
        description: |
          Prefix for output CSV files. 
          If no cell groups are specified, the output file will be named "{prefix}.csv".
          If cell groups are specified, files will be named "{prefix}_{cell_group}.csv".
        default: "deseq2_analysis"
        required: false

resources:
  - type: r_script
    path: script.R
test_resources:
  - type: python_script
    path: test.py
  - path: /resources_test/annotation_test_data/TS_Blood_filtered_pseudobulk.h5mu

engines:
- type: docker
  image: rocker/r2u:24.04
  setup:
    - type: docker
    - type: apt
      packages: [ libhdf5-dev, libgeos-dev, hdf5-tools ]
    - type: r
      cran: [ hdf5r ]
      bioc: [ DESeq2 ]
      # Use pinned version to avoid H5 file access issues with latest stable release (0.99.0)
      # Until viash r requirements supports passing secrets
      # --mount only applies to ONE single docker RUN instruction  
      # github: scverse/anndataR@36f3caad9a7f360165c1510bbe0c62657580415a
    - type: docker
      run:
        - --mount=type=secret,id=GITHUB_TOKEN,env=GITHUB_TOKEN
        - Rscript -e 'options(warn = 2); remotes::install_github(c("scverse/anndataR@36f3caad9a7f360165c1510bbe0c62657580415a"), repos = "https://cran.rstudio.com")'
  test_setup:
    - type: docker
      env: 
        - PIP_BREAK_SYSTEM_PACKAGES=1
    - type: apt
      packages: [ python3, python3-pip, python3-dev, python-is-python3, git ]
    - type: python
      user: true
      __merge__:
        - /src/base/requirements/viashpy.yaml
        - /src/base/requirements/openpipeline_testutils.yaml

runners:
- type: executable
- type: nextflow