fgumi 0.2.0

High-performance tools for UMI-tagged sequencing data: extraction, grouping, and consensus calling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# fgumi simulate CLI Reference

Generate synthetic sequencing data for testing and benchmarking the fgumi pipeline.

**Requires:** `cargo build --features simulate`

## Commands Overview

| Command | Output | Pipeline Stage | Reference FASTA |
|---------|--------|----------------|-----------------|
| `fgumi simulate fastq-reads` | R1/R2 FASTQ.gz | Input for `extract` | Required |
| `fgumi simulate mapped-reads` | Template-coord sorted BAM | Input for `group` | Required |
| `fgumi simulate grouped-reads` | Template-coord sorted BAM with MI tags | Input for `simplex`/`duplex`/`codec` | Required |
| `fgumi simulate consensus-reads` | Mapped BAM with consensus tags | Input for `filter` | Required |
| `fgumi simulate correct-reads` | Unmapped BAM + includelist | Input for `correct` | Not used |

**Note:** All simulate subcommands (except `correct-reads`) require `--reference` / `-r` pointing to a reference FASTA file. Positions are sampled from real chromosomes, template sequences are extracted from the reference, and BAM headers contain actual contig names and lengths. Read orientations are a 50/50 mix of F1R2 and R1F2 (strand coin flip per molecule).

---

## fgumi simulate fastq-reads

Generate paired-end FASTQ files with UMI sequences for input to `fgumi extract`.

### Usage

```bash
fgumi simulate fastq-reads \
    --r1 output_R1.fastq.gz \
    --r2 output_R2.fastq.gz \
    [OPTIONS]
```

### Required Arguments

| Option | Type | Description |
|--------|------|-------------|
| `-1, --r1` | PATH | Output R1 FASTQ.gz file |
| `-2, --r2` | PATH | Output R2 FASTQ.gz file |
| `--truth` | PATH | Output truth TSV file (for validation) |
| `-r, --reference` | PATH | Reference FASTA file (sequences sampled from here) |

### Simulation Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `-n, --num-molecules` | INT | 1000 | Number of unique molecules to simulate |
| `-l, --read-length` | INT | 150 | Length of each read in bases |
| `-u, --umi-length` | INT | 8 | Length of UMI sequence in bases |
| `--read-structure-r1` | STRING | `8M+T` | Read structure for R1 (fgbio notation) |
| `--read-structure-r2` | STRING | `+T` | Read structure for R2 (fgbio notation) |
| `--seed` | INT | (random) | Random seed for reproducibility |

### Quality Model Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--warmup-bases` | INT | 10 | Number of bases before peak quality is reached |
| `--warmup-quality` | INT | 25 | Starting quality score during warmup phase |
| `--peak-quality` | INT | 37 | Peak quality score (Phred) |
| `--decay-start` | INT | 100 | Position where quality decay begins |
| `--decay-rate` | FLOAT | 0.08 | Quality drop per base after decay starts |
| `--quality-noise` | FLOAT | 2.0 | Standard deviation of quality noise |
| `--r2-quality-offset` | INT | -2 | Quality offset for R2 reads (typically negative) |

### Family Size Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--family-size-dist` | STRING | `lognormal` | Distribution: `lognormal`, `negbin`, or path to histogram |
| `--family-size-mean` | FLOAT | 3.0 | Mean family size (for lognormal) |
| `--family-size-stddev` | FLOAT | 2.0 | Family size standard deviation (for lognormal) |
| `--family-size-r` | FLOAT | 2.0 | r parameter (for negative binomial) |
| `--family-size-p` | FLOAT | 0.5 | p parameter (for negative binomial) |
| `--min-family-size` | INT | 1 | Minimum reads per family |

### Insert Size Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--insert-size-mean` | FLOAT | 300.0 | Mean insert size |
| `--insert-size-stddev` | FLOAT | 50.0 | Insert size standard deviation |
| `--insert-size-min` | INT | 50 | Minimum insert size |
| `--insert-size-max` | INT | 800 | Maximum insert size |

### Methylation Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--methylation-mode` | `em-seq` or `taps` | (disabled) | Methylation chemistry mode; disabled by default |
| `--cpg-methylation-rate` | FLOAT | 0.75 | Fraction of CpG cytosines that are methylated [0.0-1.0] |
| `--conversion-rate` | FLOAT | 0.98 | Enzymatic conversion efficiency for target cytosines [0.0-1.0] |

### Truth File Format

The truth TSV file contains ground truth for validation:

| Column | Description |
|--------|-------------|
| `read_name` | Read name (matches FASTQ header) |
| `true_umi` | The true UMI sequence (before any errors) |
| `molecule_id` | Unique molecule identifier |
| `family_id` | Family within the molecule |
| `strand` | Strand (A or B) |
| `chrom` | Chromosome/contig name |
| `pos` | 0-based genomic position |

### Example

```bash
# Generate 10,000 molecules with 8bp UMIs
fgumi simulate fastq-reads \
    --r1 sim_R1.fastq.gz \
    --r2 sim_R2.fastq.gz \
    --truth sim_truth.tsv \
    --reference hg38.fa \
    --num-molecules 10000 \
    --umi-length 8 \
    --read-structure-r1 "8M142T" \
    --read-structure-r2 "150T" \
    --seed 42
```

---

## fgumi simulate mapped-reads

Generate template-coordinate sorted BAM with paired alignments for input to `fgumi group`.

### Usage

```bash
fgumi simulate mapped-reads \
    --output output.bam \
    [OPTIONS]
```

### Required Arguments

| Option | Type | Description |
|--------|------|-------------|
| `-o, --output` | PATH | Output BAM file (template-coordinate sorted) |
| `--truth` | PATH | Output truth TSV file (for validation) |
| `-r, --reference` | PATH | Reference FASTA file (sequences sampled from here) |

### Simulation Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `-n, --num-molecules` | INT | 1000 | Number of unique molecules to simulate |
| `-l, --read-length` | INT | 150 | Length of each read in bases |
| `-u, --umi-length` | INT | 8 | Length of UMI sequence in bases |
| `--seed` | INT | (random) | Random seed for reproducibility |
| `-t, --threads` | INT | 1 | Number of writer threads |

Positions are sampled from real chromosomes (weighted by length), read sequences are extracted from the reference, and the BAM header contains actual contig names and lengths.

### Alignment Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--mapq` | INT | 60 | Mapping quality for aligned reads |
| `--unmapped-fraction` | FLOAT | 0.0 | Fraction of reads to leave unmapped |

### Position Distribution Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--num-positions` | INT | (derived) | Number of genomic positions to use (default: num-molecules) |
| `--umis-per-position` | INT | 1 | Number of unique UMIs per position |

By default, each molecule gets a unique position. For high-depth benchmarking (testing MIH optimization in `group`), use fewer positions with many UMIs per position.

### Quality Model Options

(Same as fastq-reads)

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--warmup-bases` | INT | 10 | Number of bases before peak quality |
| `--warmup-quality` | INT | 25 | Starting quality during warmup |
| `--peak-quality` | INT | 37 | Peak quality score |
| `--decay-start` | INT | 100 | Position where decay begins |
| `--decay-rate` | FLOAT | 0.08 | Quality drop per base |
| `--quality-noise` | FLOAT | 2.0 | Quality noise std dev |
| `--r2-quality-offset` | INT | -2 | R2 quality offset |

### Family Size Options

(Same as fastq-reads)

### Insert Size Options

(Same as fastq-reads)

### Methylation Options

(Same as fastq-reads)

### Output Tags

| Tag | Type | Description |
|-----|------|-------------|
| `RX` | String | Raw UMI sequence |
| `RG` | String | Read group (default: "A") |

### Truth File Format

| Column | Description |
|--------|-------------|
| `read_name` | Read name (matches BAM QNAME) |
| `true_umi` | The true UMI sequence (before any errors) |
| `molecule_id` | Unique molecule identifier |
| `chrom` | Chromosome/contig name |
| `position` | 0-based genomic position |
| `strand` | Strand (`+` or `-`) |

### Example

```bash
# Generate mapped reads from a reference FASTA
fgumi simulate mapped-reads \
    --output sim_mapped.bam \
    --truth sim_truth.tsv \
    --reference hg38.fa \
    --num-molecules 5000 \
    --seed 42 \
    --threads 4

# High-depth mode: many UMIs at few positions (for MIH/group benchmarking)
fgumi simulate mapped-reads \
    --output sim_high_depth.bam \
    --truth sim_truth.tsv \
    --reference hg38.fa \
    --num-molecules 50000 \
    --num-positions 100 \
    --umis-per-position 500 \
    --seed 42
```

---

## fgumi simulate grouped-reads

Generate template-coordinate sorted BAM with MI (molecule ID) tags for input to consensus callers (`simplex`, `duplex`, `codec`).

### Usage

```bash
fgumi simulate grouped-reads \
    --output output.bam \
    [OPTIONS]
```

### Required Arguments

| Option | Type | Description |
|--------|------|-------------|
| `-o, --output` | PATH | Output BAM file (template-coordinate sorted) |
| `--truth` | PATH | Output truth TSV file (for validation) |
| `-r, --reference` | PATH | Reference FASTA file (sequences sampled from here) |

### Simulation Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `-n, --num-molecules` | INT | 1000 | Number of unique molecules to simulate |
| `-l, --read-length` | INT | 150 | Length of each read in bases |
| `-u, --umi-length` | INT | 8 | Length of UMI sequence in bases |
| `--seed` | INT | (random) | Random seed for reproducibility |
| `-t, --threads` | INT | 1 | Number of writer threads |

### Duplex Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--duplex` | FLAG | false | Generate duplex-style MI tags (e.g., "1/A", "1/B") |
| `--strand-alpha` | FLOAT | 5.0 | Beta distribution alpha for A/B strand ratio |
| `--strand-beta` | FLOAT | 5.0 | Beta distribution beta for A/B strand ratio |

### Quality Model Options

(Same as fastq-reads)

### Family Size Options

(Same as fastq-reads)

### Insert Size Options

(Same as fastq-reads)

### Methylation Options

(Same as fastq-reads)

### Output Tags

| Tag | Type | Description |
|-----|------|-------------|
| `RX` | String | Raw UMI sequence |
| `MI` | String | Molecule ID (integer for simplex, "N/A" or "N/B" for duplex) |
| `RG` | String | Read group (default: "A") |

### Truth File Format

| Column | Description |
|--------|-------------|
| `read_name` | Read name (matches BAM QNAME) |
| `true_umi` | The true UMI sequence (before any errors) |
| `molecule_id` | Unique molecule identifier |
| `expected_mi` | Expected MI tag value after grouping |
| `chrom` | Chromosome/contig name |
| `position` | 0-based genomic position |
| `strand` | Strand (`+` or `-`) |

### Example

```bash
# Generate simplex grouped reads
fgumi simulate grouped-reads \
    --output sim_grouped.bam \
    --truth sim_truth.tsv \
    --reference hg38.fa \
    --num-molecules 5000 \
    --seed 42

# Generate duplex grouped reads with strand bias
fgumi simulate grouped-reads \
    --output sim_duplex_grouped.bam \
    --truth sim_truth.tsv \
    --reference hg38.fa \
    --num-molecules 5000 \
    --duplex \
    --strand-alpha 5.0 \
    --strand-beta 5.0 \
    --seed 42
```

---

## fgumi simulate consensus-reads

Generate mapped BAM with consensus tags (cD, cM, cE, etc.) for input to `fgumi filter`.

### Usage

```bash
fgumi simulate consensus-reads \
    --output output.bam \
    --reference ref.fa \
    [OPTIONS]
```

### Required Arguments

| Option | Type | Description |
|--------|------|-------------|
| `-o, --output` | PATH | Output BAM file (mapped) |
| `-r, --reference` | PATH | Reference FASTA file (sequences sampled from here) |

### Simulation Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `-n, --num-reads` | INT | 1000 | Number of consensus read pairs to generate |
| `-l, --read-length` | INT | 150 | Length of each read in bases |
| `--seed` | INT | (random) | Random seed for reproducibility |
| `-t, --threads` | INT | 1 | Number of writer threads |

### Consensus Tag Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--min-depth` | INT | 1 | Minimum consensus depth (cM tag) |
| `--max-depth` | INT | 10 | Maximum consensus depth (cD tag) |
| `--depth-mean` | FLOAT | 5.0 | Mean depth for sampling |
| `--depth-stddev` | FLOAT | 2.0 | Depth standard deviation |
| `--error-rate-mean` | FLOAT | 0.01 | Mean error rate (cE tag) |
| `--error-rate-stddev` | FLOAT | 0.005 | Error rate standard deviation |

### Duplex Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--duplex` | FLAG | false | Generate duplex consensus tags (aD, bD, aM, bM, aE, bE) |
| `--strand-alpha` | FLOAT | 5.0 | Beta distribution alpha for A/B depth ratio |
| `--strand-beta` | FLOAT | 5.0 | Beta distribution beta for A/B depth ratio |

### Quality Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--consensus-quality` | INT | 40 | Base quality for consensus reads |

### Methylation Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--methylation-mode` | `em-seq` or `taps` | (disabled) | Methylation chemistry mode; disabled by default |
| `--cpg-methylation-rate` | FLOAT | 0.75 | Fraction of CpG cytosines that are methylated [0.0-1.0] |
| `--conversion-rate` | FLOAT | 0.98 | Enzymatic conversion efficiency for target cytosines [0.0-1.0] |
| `--methylation-depth-mean` | FLOAT | 5.0 | Mean depth for methylation count sampling (cu + ct per position) |

### Output Tags (Simplex)

| Tag | Type | Description |
|-----|------|-------------|
| `cD` | Int | Maximum per-base depth |
| `cM` | Int | Minimum per-base depth |
| `cE` | Float | Consensus error rate |
| `cd` | IntArray | Per-base depth array |
| `ce` | IntArray | Per-base error count array |

### Output Tags (Duplex, in addition to above)

| Tag | Type | Description |
|-----|------|-------------|
| `aD` | Int | A-strand maximum depth |
| `aM` | Int | A-strand minimum depth |
| `aE` | Float | A-strand error rate |
| `bD` | Int | B-strand maximum depth |
| `bM` | Int | B-strand minimum depth |
| `bE` | Float | B-strand error rate |
| `ad` | IntArray | A-strand per-base depth |
| `bd` | IntArray | B-strand per-base depth |

### Example

```bash
# Generate simplex consensus reads
fgumi simulate consensus-reads \
    --output sim_consensus.bam \
    --reference hg38.fa \
    --num-reads 10000 \
    --min-depth 2 \
    --max-depth 20 \
    --seed 42

# Generate duplex consensus reads
fgumi simulate consensus-reads \
    --output sim_duplex_consensus.bam \
    --reference hg38.fa \
    --num-reads 10000 \
    --duplex \
    --seed 42
```

---

## fgumi simulate correct-reads

Generate unmapped BAM and UMI includelist for input to `fgumi correct`.

### Usage

```bash
fgumi simulate correct-reads \
    --output output.bam \
    --includelist umis.txt \
    [OPTIONS]
```

### Required Arguments

| Option | Type | Description |
|--------|------|-------------|
| `-o, --output` | PATH | Output BAM file (unmapped) |
| `-i, --includelist` | PATH | Output UMI includelist file (one UMI per line) |
| `--truth` | PATH | Output truth TSV file (for validation) |

### Simulation Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `-n, --num-reads` | INT | 10000 | Number of reads to generate |
| `--num-umis` | INT | 1000 | Number of unique UMIs in includelist |
| `-u, --umi-length` | INT | 8 | Length of UMI sequence in bases |
| `--read-length` | INT | 100 | Length of template sequence |
| `--seed` | INT | (random) | Random seed for reproducibility |
| `-t, --threads` | INT | 1 | Number of writer threads |

### Error Distribution Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--exact-fraction` | FLOAT | 0.4 | Fraction with exact UMI match (0 edits) |
| `--edit1-fraction` | FLOAT | 0.3 | Fraction with 1 edit distance |
| `--edit2-fraction` | FLOAT | 0.2 | Fraction with 2 edit distance |
| `--multi-fraction` | FLOAT | 0.1 | Fraction with 3+ edits (should not correct) |

### Quality Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `--quality` | INT | 30 | Base quality for all bases |

### Output Tags

| Tag | Type | Description |
|-----|------|-------------|
| `RX` | String | Observed (possibly erroneous) UMI sequence |

### Includelist Format

The includelist is a plain text file with one UMI per line, sorted alphabetically:

```
AAAACCCC
AAAACCCT
AAAACCGG
...
```

### Truth File Format

| Column | Description |
|--------|-------------|
| `read_name` | Read name (matches BAM QNAME) |
| `true_umi` | The correct UMI from the includelist |
| `observed_umi` | The (possibly erroneous) UMI in the RX tag |
| `expected_correction` | Expected UMI after correction |
| `edit_distance` | Edit distance between observed and true |
| `error_type` | Type: `exact`, `edit1`, `edit2`, or `multi` |

### Example

```bash
# Generate correction test data
fgumi simulate correct-reads \
    --output sim_correct.bam \
    --includelist sim_umis.txt \
    --truth sim_truth.tsv \
    --num-reads 100000 \
    --num-umis 5000 \
    --umi-length 12 \
    --exact-fraction 0.5 \
    --edit1-fraction 0.3 \
    --edit2-fraction 0.15 \
    --multi-fraction 0.05 \
    --seed 42
```

---

## Shared Concepts

### Read Structure Notation

Uses fgbio-style read structure notation:
- `M` = Molecular barcode (UMI)
- `T` = Template
- `S` = Skip (ignored bases)
- `B` = Sample barcode
- `+` = Variable length (consumes remaining bases)

Examples:
- `8M142T` = 8bp UMI followed by 142bp template
- `8M+T` = 8bp UMI followed by variable-length template
- `+T` = All template (no UMI)

### Family Size Distributions

Three distribution types are supported:

1. **Log-normal** (default): Natural for PCR amplification
   - Parameters: `--family-size-mean`, `--family-size-stddev`

2. **Negative binomial**: Alternative PCR model
   - Parameters: `--family-size-r`, `--family-size-p`

3. **Empirical**: Load from `fgumi group -f` histogram output
   - Parameter: `--family-size-dist /path/to/histogram.tsv`

### Quality Score Model

Quality scores follow a three-phase model:

1. **Warmup** (positions 0 to `warmup-bases`): Quality ramps from `warmup-quality` to `peak-quality`
2. **Peak** (positions `warmup-bases` to `decay-start`): Quality stays at `peak-quality`
3. **Decay** (positions after `decay-start`): Quality decreases by `decay-rate` per base

R2 reads have an additional offset (`r2-quality-offset`, typically -2) applied.

### Template-Coordinate Sorting

For `mapped-reads` and `grouped-reads`, output is sorted by template coordinate:
- Primary sort: 5' position of the leftmost read in the pair
- Secondary sort: Read name (for determinism)

BAM header includes: `SO:unsorted`, `GO:query`, `SS:template-coordinate`

### High-Depth Benchmarking

To benchmark the MIH (Multiple Identical Hits) optimization in `fgumi group`, use the position distribution options to create data with many UMIs at few positions:

```bash
# 100 positions, each with 500 unique UMIs, ~10 reads per UMI = 500,000 reads
fgumi simulate mapped-reads \
    --output high_depth.bam \
    --truth high_depth_truth.tsv \
    --reference hg38.fa \
    --num-molecules 500000 \
    --num-positions 100 \
    --umis-per-position 500 \
    --family-size-mean 10 \
    --seed 42
```

This creates the kind of position-clustered data that stresses the UMI assignment algorithm and tests the MIH optimization path in `group`.