Skip to content

Add "bedtools map" with a new group_by logic#17

Draft
mrvollger wants to merge 5 commits into
quinlan-lab:mainfrom
mrvollger:groupmap
Draft

Add "bedtools map" with a new group_by logic#17
mrvollger wants to merge 5 commits into
quinlan-lab:mainfrom
mrvollger:groupmap

Conversation

@mrvollger
Copy link
Copy Markdown

@mrvollger mrvollger commented Jan 30, 2026

Hi @brentp, per our conversation this morning plus one new feature I always wanted in bedtools map :)

Summary

  • Adds a map subcommand that aggregates values from overlapping B intervals for each A interval (like bedtools map)
  • Supports composable filtering flags:
    • -G/--group-by-b: for each A, group its overlapping B intervals by name, then summarize each group separately (like dplyr's group_by() %>% summarize())
    • -n/--name-match: only summarize B intervals whose name matches A's name
  • Supports multiple comma-separated columns (-c) and operations (-O) with bedtools-compatible pairing rules
  • Available operations: count, sum, mean, min, max, median
  • Adds name() and column_as_f64() methods to Position for extracting BED fields

Test plan

  • Unit tests for all aggregation operations (count, sum, mean, min, max, median) including empty inputs
  • Unit tests for format_number (integer vs float output)
  • cargo build compiles without warnings
  • cargo test — all 90 tests pass
  • Manual verification of all 5 help examples against actual output

Mitchell R. Vollger and others added 2 commits January 29, 2026 20:58
Without this, cargo test fails on macOS with "Library not loaded:
libpython3.13.dylib" because the dynamic linker can't find the
Python shared library. The build script uses pyo3-build-config
(already a build-dependency) to discover the Python lib directory
and passes it as an rpath to the linker.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implements bedtools-map-style functionality with composable flags:
- Default: aggregate all overlapping B intervals (one row per A)
- -G/--group-by-b: group B intervals by name and summarize separately
- -n/--name-match: only summarize B intervals whose name matches A
- Supports multiple comma-separated columns (-c) and operations (-O)
  with bedtools-compatible pairing rules (1:N, N:1, N:N)
- Operations: count, sum, mean, min, max, median
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @mrvollger, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly extends the bedder tool by introducing a new map subcommand. This feature allows users to aggregate numerical values from genomic intervals (B) that overlap with other intervals (A), offering a highly flexible and powerful way to summarize genomic data. It includes advanced options for grouping and filtering intervals based on their names, and supports a variety of statistical operations across multiple columns, making it a versatile addition for complex genomic analyses.

Highlights

  • New map subcommand: Introduces a powerful new map subcommand for aggregating values from overlapping B intervals for each A interval, similar to bedtools map.
  • Flexible Aggregation Operations: Supports count, sum, mean, min, max, and median operations, with robust handling for empty input data.
  • Advanced Grouping and Filtering: Implements -G/--group-by-b to group overlapping B intervals by name and -n/--name-match to filter B intervals whose name matches A's name.
  • Multi-column and Multi-operation Support: Allows specifying multiple comma-separated columns (-c) and operations (-O) with bedtools-compatible pairing rules.
  • Enhanced BED Field Extraction: Adds name() and column_as_f64() methods to the Position struct, enabling easier extraction of BED name and numerical column values.
  • Build System Improvement: Includes a build.rs script to correctly configure rpath for libpython on macOS, addressing potential runtime library linking issues.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a powerful new map subcommand, which is a great addition. The implementation is well-structured and includes comprehensive command-line help and tests. I've identified a few areas for improvement, mainly concerning portability, API design, error handling, and a logic bug in the name-matching feature. My comments provide specific suggestions to address these points.

Comment thread src/cli/map.rs Outdated
Comment thread src/cli/map.rs
Comment thread src/cli/map.rs
Comment thread src/cli/map.rs Outdated
@mrvollger mrvollger marked this pull request as draft January 30, 2026 14:18
Mitchell R. Vollger and others added 3 commits January 30, 2026 07:22
… stdout portability

- Change compute() to take &[f64] instead of &mut Vec<f64> so Median
  no longer sorts the caller's data in place; clones to a local vec.
- Fix name-match in non-grouped path: B intervals with no name were
  incorrectly included when A had a name. Now correctly excludes B
  intervals whose name differs from A's (including when B has no name).
- Remove map_err wrappers on write_record/flush/BedWriter::new; BedError
  implements std::error::Error so ? propagates directly without losing
  the original error type and context.
- Replace /dev/stdout with BedWriter::from_writer(stdout()) for portable
  stdout handling, and pass PathBuf directly to BedWriter::new to avoid
  unwrap() on non-UTF-8 paths.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Consolidate inline unit tests and move end-to-end map_command tests
to a separate integration test file using the Command-based pattern
matching integration_skip_position.rs. Inline tests now cover only
pure functions (AggOp, format_number, expand_ops).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…-numeric warnings

- Treat missing A name as "." for --name-match (previously silently skipped filtering)
- Reject -c 0 with a clear error (columns are 1-indexed, matching bedtools)
- Warn once per column on first non-numeric value (matching bedtools behavior)
- Add tests for zero overlaps, A-with-no-name + name-match, column 0 error, single-element median

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@brentp brentp self-assigned this Feb 5, 2026
@brentp
Copy link
Copy Markdown
Member

brentp commented Feb 13, 2026

@codex review and note how we can support python functions in the map sub-command

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b60954f0dd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread build.rs
// then emit an rpath so the test/binary can find libpython at runtime on macOS.
let config = pyo3_build_config::get();
if let Some(lib_dir) = &config.lib_dir {
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib_dir);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Gate macOS rpath linker flag by target platform

This build script emits cargo:rustc-link-arg=-Wl,-rpath,... for every target whenever lib_dir is present, but that linker syntax is not portable (notably MSVC/Windows), so cross-platform builds can fail at link time even though this rpath workaround is only needed on macOS. Add an explicit target check (for example macOS-only, or at least non-MSVC) before printing this flag.

Useful? React with 👍 / 👎.

Comment thread src/cli/map.rs
Comment on lines +195 to +203
#[arg(
help = "aggregation operation(s) to apply. Comma-separated for multiple.",
short = 'O',
long = "operation",
default_value = "sum",
value_delimiter = ',',
value_enum
)]
pub operations: Vec<AggOp>,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Support Python callbacks in map aggregations

The map CLI currently restricts -O/--operation to the fixed AggOp enum and never exposes the existing Python-function loading flow used by other commands, so users cannot run custom aggregation logic in map without modifying Rust code. This creates a feature gap for workflows that depend on Python-defined functions; adding a Python-backed operation path (reusing the existing bedder::py compilation/introspection utilities) would make map extensible and consistent with the rest of the tool.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codex sketch up and example of what python functions owuld look like here. Update the enum and add other stubs as needed

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codex, I added an environment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex couldn't complete this request. Try again later.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codex don't look for the groupmap branch that's from the person who create the pull request. just checkout the PR

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex couldn't complete this request. Try again later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants