# Filter Outcome Aggregation And Counting

> Use when after applying one or more mpactr filters (mispicked, group, cv, insource) to a feature table, you need to quantify the distribution of ions by their pass/fail status across filters to understand filtering impact, identify potential over-filtering, or communicate QC results via treemap or.

- Skill: `holobiomicslab/filter-outcome-aggregation-and-counting` (Agent Skill)
- Install (CLI): `npx skillmds@latest add holobiomicslab/filter-outcome-aggregation-and-counting`
- Raw SKILL.md: https://api.skillmd.com/api/skills/holobiomicslab/filter-outcome-aggregation-and-counting/raw
- Safety review: PASS (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: CC-BY-4.0
- Author: HolobiomicsLab (https://skillmd.com/u/holobiomicslab)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/holobiomicslab/filter-outcome-aggregation-and-counting

---


# filter-outcome-aggregation-and-counting

## Summary

Aggregate and count ion filtering outcomes (passed/failed status) across all ions and filter types to enable downstream summarization and visualization of quality control results. This skill transforms per-ion filter status into group counts and percentages, supporting transparent reporting of how many ions survive each filtering stage.

## When to use

After applying one or more mpactr filters (mispicked, group, cv, insource) to a feature table, you need to quantify the distribution of ions by their pass/fail status across filters to understand filtering impact, identify potential over-filtering, or communicate QC results via treemap or summary plots.

## When NOT to use

- Input data has not yet been filtered; apply at least one filter (e.g., filter_mispicked_ions, filter_group) before aggregating outcomes.
- You only need to report pass/fail counts for a single ion; this skill is for population-level aggregation, not per-ion inspection.
- qc_summary() output is unavailable or empty (indicates filtering workflow was not completed).

## Inputs

- filtered mpactr object (after applying one or more filters)
- qc_summary() output (per-ion filter status: passed/failed for mispicked, group, replicability, insource)

## Outputs

- aggregated summary data frame (status, count, percentage columns)
- treemap visualization (ggplot2 object or exported PNG)
- summary statistics (total ions, count by status)

## How to apply

Extract per-ion filter status from qc_summary() output, which reports whether each ion passed or failed each of the four filter types (mispicked, group, replicability, insource). Use data.table grouping operations to count ions per status category and calculate the percentage of total ions for each category. Construct a summary data frame with columns for status, count, and percentage. Render this aggregated result as a treemap using ggplot2's geom_treemap() and geom_treemap_text() to visualize tile areas proportional to ion counts and label each tile with status, count, and percentage. The treemap enables rapid visual assessment of filtering selectivity and identifies which filters are most stringent.

## Related tools

- **mpactr** (source of qc_summary() method and filtered feature table with per-ion filter status) — https://github.com/mums2/mpactr
- **data.table** (grouping and aggregation of ion counts by filter status)
- **ggplot2** (rendering treemap visualization with geom_treemap and geom_treemap_text layers)
- **R** (execution environment for qc_summary extraction, data.table operations, and ggplot rendering)

## Examples

```
qc_out <- qc_summary(filtered_mpactr_obj); summary_df <- qc_out[, .(count = .N), by = status][, percentage := 100 * count / sum(count)]; ggplot(summary_df, aes(area = count, fill = status, label = paste(status, count, percentage, sep = "\n"))) + geom_treemap() + geom_treemap_text()
```

## Evaluation signals

- Aggregated ion counts sum to the total number of ions in the filtered object (invariant check).
- All ions are assigned to exactly one status category (no missing or duplicate counts).
- Percentages sum to 100% or very close (allowing for rounding); spot-check one or two status percentages by hand calculation from counts.
- Treemap tiles are visually proportional to reported counts; larger counts occupy larger areas.
- Treemap labels are readable and include status name, count, and percentage for each tile; no labels are truncated or missing.

## Limitations

- qc_summary() reports cumulative pass/fail status for each ion across all applied filters; it does not isolate the contribution of individual filters, so you cannot determine which single filter is most selective from aggregated counts alone.
- If filters are applied with copy_object=TRUE (deep copying), memory usage may be high for large datasets; the article recommends copy_object=FALSE for efficiency, but this does not affect aggregation accuracy.
- Treemap visualization may become crowded if there are many filter status categories; consider filtering or grouping status labels if the number of unique categories is very high.

## Evidence

- [methods] qc_summary() output and per-ion filter status extraction: "Pivot qc_summary output to count ions per status category and calculate percentage of total ions for each status."
- [methods] data.table-based aggregation workflow: "Ion counts and percentages by status can be computed from qc_summary() output using data.table grouping"
- [methods] treemap rendering as visualization: "Render treemap using ggplot2 with geom_treemap() for tile areas (scaled by count) and geom_treemap_text() for status labels and percentages."
- [methods] filter types included in qc_summary: "per-ion filter status (passed/failed for mispicked, group, replicability, insource filters)"
- [readme] mpactr filter overview from README: "mpactr is a collection of filters for the purpose of identifying high quality MS1 features by correcting peak selection errors"

