From f483d5a6fa8b09937f8b73edb1ea4ac087cb4d65 Mon Sep 17 00:00:00 2001 From: clintval Date: Wed, 9 Sep 2026 18:09:47 -0400 Subject: [PATCH 1/6] feat: fold related samples into one General Statistics row per group A MultiQC plugin registered through the multiqc.hooks.v1 entry points. The after_modules hook classifies every General Statistics row with the regexes of the sample_pivot config block, renames the columns of labelled rows after their label and folds them onto one row per group, folds unlabelled rows onto that row as they are, and moves rows for a table level into their own table with their grouping intact. Unit tests cover the routing and header handling against MultiQC's data model; an end-to-end test renders a report from custom content fixtures and checks the HTML. Tooling follows fulcrumgenomics/multifg: uv, ruff, mypy, basedpyright, pytest with coverage, and GitHub Actions for tests, wheels and tagged releases. The lock file is resolved with UV_NO_CONFIG so user-level uv settings cannot leak into it. --- .github/CODEOWNERS | 1 + .github/workflows/publish_multiqc_pivot.yml | 133 ++ .github/workflows/tests.yml | 37 + .github/workflows/wheels.yml | 37 + .gitignore | 1 - CONTRIBUTING.md | 125 + README.md | 94 +- docs/pivot.png | Bin 0 -> 111795 bytes multiqc_pivot/__init__.py | 0 multiqc_pivot/config.py | 107 + multiqc_pivot/pivot.py | 245 ++ multiqc_pivot/plugin.py | 89 + multiqc_pivot/py.typed | 0 pyproject.toml | 211 ++ tests/__init__.py | 0 tests/data/multiqc_config.yml | 17 + tests/data/report/alignment_mqc.yaml | 16 + tests/data/report/concordance_mqc.yaml | 12 + tests/data/report/coverage_mqc.yaml | 19 + tests/data/report/libraries_mqc.yaml | 22 + tests/test_config.py | 73 + tests/test_pivot.py | 210 ++ tests/test_plugin.py | 77 + uv.lock | 2293 +++++++++++++++++++ 24 files changed, 3817 insertions(+), 2 deletions(-) create mode 100644 .github/CODEOWNERS create mode 100644 .github/workflows/publish_multiqc_pivot.yml create mode 100644 .github/workflows/tests.yml create mode 100644 .github/workflows/wheels.yml create mode 100644 CONTRIBUTING.md create mode 100644 docs/pivot.png create mode 100644 multiqc_pivot/__init__.py create mode 100644 multiqc_pivot/config.py create mode 100644 multiqc_pivot/pivot.py create mode 100644 multiqc_pivot/plugin.py create mode 100644 multiqc_pivot/py.typed create mode 100644 pyproject.toml create mode 100644 tests/__init__.py create mode 100644 tests/data/multiqc_config.yml create mode 100644 tests/data/report/alignment_mqc.yaml create mode 100644 tests/data/report/concordance_mqc.yaml create mode 100644 tests/data/report/coverage_mqc.yaml create mode 100644 tests/data/report/libraries_mqc.yaml create mode 100644 tests/test_config.py create mode 100644 tests/test_pivot.py create mode 100644 tests/test_plugin.py create mode 100644 uv.lock diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..3f9d317 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @clintval diff --git a/.github/workflows/publish_multiqc_pivot.yml b/.github/workflows/publish_multiqc_pivot.yml new file mode 100644 index 0000000..2326cca --- /dev/null +++ b/.github/workflows/publish_multiqc_pivot.yml @@ -0,0 +1,133 @@ +name: publish multiqc-pivot + +on: + push: + tags: '[0-9]+.[0-9]+.[0-9]+' + +permissions: {} + +env: + UV_VERSION: 0.11.16 + +jobs: + on-main-branch-check: + runs-on: ubuntu-latest + permissions: + contents: read + outputs: + on_main: ${{ steps.contains_tag.outputs.retval }} + steps: + # action-contains-tag lists remote branches to find the tag; current git follows the remote + # HEAD by default, which breaks that lookup: https://github.com/rickstaa/action-contains-tag/pull/18 + - name: git config --global remote.origin.followRemoteHEAD never + shell: bash + run: git config --global remote.origin.followRemoteHEAD never + + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + + - uses: rickstaa/action-contains-tag@a9ff27d505ba2bf074a2ebb48b208e76d35ff308 # v1.2.10 + id: contains_tag + with: + reference: "main" + tag: "${{ github.ref_name }}" + + unit-tests: + needs: on-main-branch-check + if: ${{ needs.on-main-branch-check.outputs.on_main == 'true' }} + permissions: + contents: read + uses: "./.github/workflows/tests.yml" + + build-wheels: + needs: unit-tests + permissions: + contents: read + uses: "./.github/workflows/wheels.yml" + + build-sdist: + name: build source distribution + needs: unit-tests + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Install uv + uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1 + with: + version: ${{ env.UV_VERSION }} + python-version: "3.11" + enable-cache: 'false' + + - name: Build package + run: uv build --sdist --out-dir dist + + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: multiqc-pivot-sdist + path: dist/*.tar.gz + + publish-to-pypi: + runs-on: ubuntu-latest + needs: [build-wheels, build-sdist] + environment: pypi + permissions: + id-token: write + steps: + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + path: packages + pattern: 'multiqc-pivot-*' + merge-multiple: true + + - uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2 + with: + packages-dir: packages/ + skip-existing: true + verbose: true + + make-changelog: + runs-on: ubuntu-latest + needs: publish-to-pypi + permissions: + contents: read + outputs: + release_body: ${{ steps.git-cliff.outputs.content }} + steps: + - name: Checkout the Repository at the Tagged Commit + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + ref: ${{ github.ref_name }} + persist-credentials: false + + - name: Generate a Changelog + uses: orhun/git-cliff-action@3d96a18cc4ec17e9dc69ddcc424ccafaf1f78ce2 # v4.9.0 + id: git-cliff + with: + config: pyproject.toml + args: --latest --verbose + env: + GITHUB_REPO: ${{ github.repository }} + + make-github-release: + runs-on: ubuntu-latest + environment: github + permissions: + contents: write + pull-requests: read + needs: make-changelog + steps: + - name: Create Release + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ github.ref_name }} + NOTES: ${{ needs.make-changelog.outputs.release_body }} + run: gh release create "$TAG" --repo "$GITHUB_REPOSITORY" --title "$TAG" --notes "$NOTES" diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..4e119db --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,37 @@ +name: tests + +on: + push: + branches: + - "**" + tags: + - "!**" + workflow_call: + +permissions: + contents: read + +env: + UV_VERSION: 0.11.16 + +jobs: + unit-tests: + runs-on: ubuntu-latest + strategy: + matrix: + PYTHON_VERSION: ["3.10", "3.11", "3.12", "3.13"] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Install uv + uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1 + with: + version: ${{ env.UV_VERSION }} + python-version: ${{ matrix.PYTHON_VERSION }} + enable-cache: 'false' + + - name: Test the library + run: | + uv run --locked poe check-all diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml new file mode 100644 index 0000000..1f4c741 --- /dev/null +++ b/.github/workflows/wheels.yml @@ -0,0 +1,37 @@ +name: wheels + +on: + pull_request: + workflow_call: + workflow_dispatch: + +permissions: + contents: read + +env: + UV_VERSION: 0.11.16 + +jobs: + build-wheels: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Install uv + uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1 + with: + version: ${{ env.UV_VERSION }} + python-version: "3.11" + enable-cache: 'false' + + - name: Build wheels + run: uv build --wheel --out-dir wheelhouse + + - name: Upload wheels + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: multiqc-pivot-wheels + path: ./wheelhouse/multiqc_pivot*.whl + if-no-files-found: error diff --git a/.gitignore b/.gitignore index 7c706c9..1ceb2c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -data/ *.html .DS_Store .vscode/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..4399f05 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,125 @@ +# Development and Testing + +## Primary Development Commands + +To check and resolve linting issues in the codebase, run: + +```console +uv run ruff check --fix +``` + +To check and resolve formatting issues in the codebase, run: + +```console +uv run ruff format +``` + +To check the unit tests in the codebase, run: + +```console +uv run pytest +``` + +To check the typing in the codebase, run: + +```console +uv run mypy +``` + +To generate a code coverage report after testing locally, run: + +```console +uv run coverage html +``` + +To check the lock file is up to date: + +```console +uv lock --check +``` + +The lock file is resolved without any user-level uv configuration, which is how CI resolves it. If your `~/.config/uv/uv.toml` sets `exclude-newer` or similar, uv will want to re-resolve and will rewrite `uv.lock`. Set `UV_NO_CONFIG=1` in your shell while working here to match CI; the environment variable matters because `poe` runs each task through its own `uv run`, which a `--no-config` flag on the outer command does not reach. + +## Shortcut Task Commands + +###### For Running Individual Checks + +```console +uv run poe check-format +uv run poe check-lint +uv run poe check-tests +uv run poe check-typing +``` + +###### For Running All Checks + +```console +uv run poe check-all +``` + +###### For Running Individual Fixes + +```console +uv run poe fix-format +uv run poe fix-lint +``` + +###### For Running All Fixes + +```console +uv run poe fix-all +``` + +###### For Running All Fixes and Checks + +```console +uv run poe fix-and-check-all +``` + +## Trying a change against a real report + +Point MultiQC at any directory of QC outputs with a config that has a `sample_pivot` block. The +plugin is picked up automatically once the package is installed in the same environment: + +```console +uv run multiqc --config my_config.yml ./path-to-data +``` + +## Creating a release on PyPI + +> [!NOTE] +> This project follows [Semantic Versioning](https://semver.org/), aka SemVer. In brief: +> +> - MAJOR version when you make incompatible API changes +> - MINOR version when you add functionality in a backwards compatible manner +> - PATCH version when you make backwards compatible bug fixes + +> [!IMPORTANT] +> Consider editing the changelog if there are any errors or necessary enhancements. + +1. Clone the repository, ensure you are on the main branch, and that the working directory is clean. +2. Check out a new branch to prepare the library for release. +3. Bump the version of the library to the desired SemVer, e.g.: + ```console + # Increment the minor segment + uv version --bump minor + + # Update to a specific version number + uv version 0.2.0 + ``` +4. Commit the version bump changes with a Git commit message like `chore(release): bump to #.#.#`. +5. Push the commit, open a PR, ensure tests pass, and seek reviews. +6. Squash merge the PR into the `main` branch. +7. Tag the new commit on the main branch with the bumped version number. + +> [!WARNING] +> The tag **must** be a valid SemVer version number and **must** match the version set in (3). The [publishing GitHub Action](.github/workflows/publish_multiqc_pivot.yml) is activated by a new tag on the `main` branch containing a valid SemVer version. + +GitHub Actions will take care of the remainder of the deployment and release process: + +1. Unit tests will be re-run. +2. A source distribution will be built. +3. A wheel distribution will be built. +4. Assets will be deployed to PyPI with the new version. +5. A [Conventional Commit](https://www.conventionalcommits.org/en/v1.0.0/)-aware changelog will be drafted. +6. A GitHub release will be created with the new version tag and the drafted changelog. diff --git a/README.md b/README.md index d39ea02..ac2a72e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,95 @@ # multiqc-pivot -A MultiQC plugin that folds related samples into one General Statistics row per group, with the metric columns labelled by what they came from. +[![CI](https://github.com/clintval/multiqc-pivot/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/clintval/multiqc-pivot/actions/workflows/tests.yml?query=branch%3Amain) +[![Python Versions](https://img.shields.io/badge/python-3.10_|_3.11_|_3.12_|_3.13-blue)](https://github.com/clintval/multiqc-pivot) +[![basedpyright](https://img.shields.io/badge/basedpyright-checked-42b983)](https://docs.basedpyright.com/latest/) +[![mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/) +[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://docs.astral.sh/ruff/) + +A [MultiQC](https://multiqc.info) plugin that folds related samples into one General Statistics row per group, with each metric column labelled by the sample it came from. + +MultiQC gives every sample its own row. When one subject yields several samples that are measured by different tools, say a tumour and a normal, or two tissues and a paired-genotype check, the General Statistics table ends up with a block of half-empty rows per subject. MultiQC's own [sample grouping](https://docs.seqera.io/multiqc/reports/customisation#sample-grouping) only fills the group's row for the handful of modules that know how to merge their metrics. + +This plugin runs after every module has reported and rebuilds the table: + +- rows for one group fold into a single row, and every folded column is renamed after where it came from, so `Median` becomes `Tumour Median` and `Normal Median`; +- the original rows stay beneath the group row, so it still expands; +- rows for a level that does not belong in the table, such as per-library read QC, move out into their own table under General Statistics with whatever grouping they already had; +- hover text, colour scales, formats and hidden-by-default state carry over from the module that produced each column. + +![General Statistics with one row per subject and a Library statistics table beneath it](docs/pivot.png) + +The report above comes from the [test fixtures](tests/data/report) and the [configuration](tests/data/multiqc_config.yml) shown in the usage section. + +## Installation + +```console +pip install multiqc-pivot +``` + +MultiQC discovers the plugin through its entry points; nothing else is needed. Until a release is on PyPI, install from GitHub: + +```console +pip install git+https://github.com/clintval/multiqc-pivot +``` + +## Usage + +Add a `sample_pivot` block to any MultiQC config, for example with `--config my_config.yml`. With these sample names: + +```text +101.subject +101.tissueA +101.tissueB +101.tissueB (filtered) +101.tissueA.library.L1 +101.tissueA.library.L2 +``` + +this configuration produces one row named `101` carrying `Concordance`, `TissueA Median`, `TissueB Median`, `TissueB (filtered) % Aligned` and so on, and moves the library rows into a separate table: + +```yaml +sample_pivot: + group: '^(?P[^. ]+)\.' + levels: + - match: '\.subject$' + - match: '\.(?PtissueA|tissueB)$' + label: '{analyte}' + - match: '\.(?PtissueA|tissueB) \(filtered\)$' + label: '{analyte} (filtered)' + - match: '\.library\.' + table: Library statistics + label_order: [tissueA, tissueB, tissueB (filtered)] + tables: + Library statistics: + description: Per-library read QC; read pairs nest under their library. +``` + +### Reference + +| Key | Meaning | +| --- | --- | +| `group` | A regular expression searched in every matched sample name. Its `(?P...)` capture names the folded row. Required. | +| `levels` | An ordered list; the first level whose `match` is found in a sample name wins. Required. | +| `levels[].match` | A regular expression searched in the sample name. Named captures are available to `label`. | +| `levels[].label` | A format string built from the captures of `match`. Columns of matching rows are renamed with it and folded onto the group row; the row itself stays beneath. Omit it, and omit `table`, to fold the row's columns onto the group row unchanged. | +| `levels[].table` | The name of a table that receives matching rows instead of General Statistics. Rows keep their grouping, so paired reads stay nested under their library. | +| `column_title` | How a pivoted column is titled. `{label}` is the label as written, `{Label}` has its first letter upper-cased, `{title}` is the module's title. Default `{Label} {title}`. | +| `label_order` | Labels in the order their column blocks should appear. Labels not listed follow in order of first appearance. | +| `tables` | Presentation of the tables named by `levels[].table`, currently a `description` each. | + +A sample that matches no level is left exactly where it was. A sample that matches a level but not `group` is left alone as well, with a warning in the log. Columns that a module did not declare a header for are dropped from folded rows, as MultiQC would have dropped them anyway. + +### Where the columns land + +Pivoted columns are placed after every column that was not pivoted, grouped by label in `label_order`. Within a label they keep MultiQC's module order. In the exported `multiqc_general_stats.txt` a pivoted column is named `__