|
| 1 | +# GitHub Copilot Instructions for mosdepth |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +mosdepth is a fast BAM/CRAM depth calculation tool for WGS, exome, or targeted sequencing written in Nim. It provides per-base, per-window, and per-region depth calculations about 2x faster than samtools depth. |
| 5 | + |
| 6 | +## Technologies |
| 7 | +- **Language**: Nim (requires Nim >= 1.0.0) |
| 8 | +- **Key Dependencies**: |
| 9 | + - hts-nim (wrapper around htslib for BAM/CRAM parsing) |
| 10 | + - htslib (requires version 1.4+) |
| 11 | + - d4-nim (for D4 format support) |
| 12 | + - docopt (for command-line parsing) |
| 13 | +- **Build Requirements**: |
| 14 | + - Must be built with `--mm:refc` flag for optimal performance and correct operation |
| 15 | + - Release builds use: `nim c --mm:refc -d:danger -d:release` |
| 16 | + |
| 17 | +## Architecture |
| 18 | +- **Core Algorithm**: Uses a cumulative array approach where starts/stops are tracked in an array the length of each chromosome |
| 19 | + - Increments for read starts, decrements for read stops |
| 20 | + - Cumulative sum provides depth at any position |
| 21 | + - This is faster than pileup-based approaches but uses more memory (~32-bits * longest chromosome length) |
| 22 | +- **Key Files**: |
| 23 | + - `mosdepth.nim`: Main entry point and core depth calculation logic |
| 24 | + - `depthstat.nim`: Statistics and distribution calculations |
| 25 | + - `int2str.nim`: Integer to string conversion utilities |
| 26 | + |
| 27 | +## Coding Standards |
| 28 | +- Follow Nim naming conventions: |
| 29 | + - `snake_case` for variables, functions, and types |
| 30 | + - Use type suffixes: `_t` for tuple types, `_s` for string tuple variants |
| 31 | + - Prefix for objects: use `*` for public fields |
| 32 | +- Prefer explicit types for clarity in performance-critical code |
| 33 | +- Use inline pragmas (`{.inline.}`) for hot-path functions |
| 34 | +- Use shallow copies (`{.shallow.}`) for large sequences when appropriate |
| 35 | + |
| 36 | +## Testing |
| 37 | +- **Unit Tests**: Located in `tests/` directory |
| 38 | + - Run with: `nim c -r tests/all.nim` |
| 39 | + - Uses the standard `unittest` module |
| 40 | + - Test file: `tests/funcs.nim` |
| 41 | +- **Functional Tests**: Shell-based tests in `functional-tests.sh` |
| 42 | + - Uses ssshtest framework |
| 43 | + - Tests real BAM/CRAM files with expected outputs |
| 44 | + - Run with: `bash ./functional-tests.sh` |
| 45 | +- **Test Data**: Test BAM/CRAM files are in the `tests/` directory |
| 46 | + |
| 47 | +## Building |
| 48 | +```bash |
| 49 | +# Development build |
| 50 | +nimble build -Y mosdepth.nimble |
| 51 | + |
| 52 | +# Release build |
| 53 | +nim c --mm:refc -d:danger -d:release mosdepth.nim |
| 54 | +``` |
| 55 | + |
| 56 | +## Key Features to Preserve |
| 57 | +- **Mate-pair overlap detection**: Avoids double-counting overlapping mate-pairs (unless --fast-mode is used) |
| 58 | +- **CIGAR-aware counting**: Tracks every aligned part using CIGAR operations |
| 59 | +- **Environment variables**: |
| 60 | + - `MOSDEPTH_PRECISION`: Controls decimal precision in output (default: 2) |
| 61 | + - `MOSDEPTH_Q0`, `MOSDEPTH_Q1`, etc.: Custom labels for quantize bins |
| 62 | +- **Output formats**: per-base BED, region BED, quantized BED, threshold BED, summary TXT, distribution TXT, D4 format |
| 63 | + |
| 64 | +## Performance Considerations |
| 65 | +- Memory usage is ~32-bits per base for longest chromosome (e.g., 1GB for 249MB chr1) |
| 66 | +- Threading applies to BAM decompression only (diminishing returns after ~4 threads) |
| 67 | +- Per-base output is expensive; prefer quantized or window-based when possible |
| 68 | +- `--fast-mode` skips mate overlap correction and CIGAR operations for speed |
| 69 | + |
| 70 | +## Common Pitfalls |
| 71 | +- Must handle empty chromosomes correctly (see tests/empty-tids.bam) |
| 72 | +- Region files must be sorted by chromosome order matching BAM header |
| 73 | +- CRAM files require reference FASTA with `-f/--fasta` flag |
| 74 | +- Need to handle both BAM and CRAM formats appropriately |
| 75 | + |
| 76 | +## External Resources |
| 77 | +- [hts-nim documentation](https://github.com/brentp/hts-nim/) |
| 78 | +- [Nim language documentation](https://nim-lang.org/docs/) |
| 79 | +- [D4 format specification](https://github.com/38/d4-format) |
0 commit comments