Skip to content

refactor(sam): reuse shared utilities and avoid inner-loop allocations - #147

Merged
nh13 merged 1 commit into
mainfrom
refactor/nh/simplify-fgumi-sam
Mar 3, 2026
Merged

nh13 merged 1 commit into
mainfrom
refactor/nh/simplify-fgumi-sam

Conversation

@nh13

@nh13 nh13 commented Mar 3, 2026

Copy link
Copy Markdown
Member

Summary

  • Replace hand-rolled revcomp_buf_value complement logic with fgumi_dna::reverse_complement
  • Replace match_count.to_string() allocations in MD tag regeneration inner loop with write!(md_string, "{match_count}") using std::fmt::Write
  • Replace inline reference span calculation in regenerate_alignment_tags_raw with fgumi_raw_bam::reference_length_from_cigar

Test plan

  • cargo nextest run -p fgumi-sam — all tests pass
  • cargo ci-fmt — clean
  • cargo ci-lint — clean

Use fgumi_dna::reverse_complement in revcomp_buf_value instead of
inline reverse-complement logic, and remove the unused complement_base
import.

Replace match_count.to_string() with write!(md_string, ...) in both
alignment tag regeneration functions, avoiding a String allocation per
mismatch in the inner loop.

Replace inline reference span calculation in regenerate_alignment_tags_raw
with fgumi_raw_bam::reference_length_from_cigar.
@nh13
nh13 temporarily deployed to github-actions March 3, 2026 03:26 — with GitHub Actions Inactive
@coderabbitai

coderabbitai Bot commented Mar 3, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The changes refactor MD tag construction in alignment processing to use write! macro-based formatting instead of manual string concatenation. Reference length computation is consolidated via a helper function. Validation and error handling in the raw parsing path is enhanced with explicit bounds checks on sequence, quality, and CIGAR offsets. The reverse complement implementation switches to the fgumi_dna library function with an expect on UTF-8 conversion failures, replacing previous complement mapping logic. Error handling transitions from implicit lossy conversion to explicit panics on non-ASCII data.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main refactoring objectives: reusing shared utilities (reverse_complement, reference_length_from_cigar) and eliminating inner-loop String allocations via write! macro.
Description check ✅ Passed The description clearly maps to the changeset, detailing three specific refactoring goals with evidence of testing (nextest, ci-fmt, ci-lint all passing).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/nh/simplify-fgumi-sam

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Mar 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.61%. Comparing base (3a460ea) to head (5b2e263).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #147   +/-   ##
=======================================
  Coverage   83.61%   83.61%           
=======================================
  Files         126      126           
  Lines       51510    51510           
=======================================
  Hits        43069    43069           
  Misses       8441     8441           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nh13

nh13 commented Mar 3, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Mar 3, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nh13

nh13 commented Mar 3, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Mar 3, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crates/fgumi-sam/src/alignment_tags.rs`:
- Line 297: The cast of reference_length_from_cigar(&cigar_ops) to usize is
unsafe: call usize::try_from(reference_length_from_cigar(&cigar_ops)) and handle
the Err case (return an error or skip/make the record invalid) to avoid
negative-to-usize wrapping for ref_span; then replace direct arithmetic around
ref_span (the math using ref_span and the alignment start/end near where
position is adjusted, e.g., the code around the later coordinate math) with
checked_add/checked_sub (checked_add, checked_sub) and propagate or return an
error if any check returns None so downstream coordinate computation cannot be
corrupted.

In `@crates/fgumi-sam/src/lib.rs`:
- Around line 313-317: The doc claims "Cannot panic" but the code in the reverse
complement path uses String::from_utf8(revcomp).expect(...) which can panic for
invalid UTF-8; change that to use the same lossy conversion pattern as
reverse_buf_value (i.e., call std::string::String::from_utf8_lossy or
from_utf8_lossy(&revcomp).into_owned()) so non-UTF8 bytes are handled without
panicking; update the conversion site where reverse/complement bytes are turned
into a String (referencing the reverse_buf_value behavior at line ~287 and the
String::from_utf8(...) call in the reverse-complement code).

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3a460ea and 5b2e263.

📒 Files selected for processing (2)
  • crates/fgumi-sam/src/alignment_tags.rs
  • crates/fgumi-sam/src/lib.rs

Comment thread crates/fgumi-sam/src/alignment_tags.rs
Comment thread crates/fgumi-sam/src/lib.rs
@nh13
nh13 merged commit bf4b24f into main Mar 3, 2026
7 checks passed
@nh13
nh13 deleted the refactor/nh/simplify-fgumi-sam branch March 3, 2026 18:51

This branch was previously deployed

1 inactive deployment
github-actions — 5b2e263a Deployed Mar 3, 2026 by nh13 via coverage #511
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.

1 participant