annonars 0.45.0

Genome annotation based on Rust and RocksDB
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
//! Merge per-track databases into one unified RocksDB.
//!
//! The native tracks ([`crate::cadd`], [`crate::spliceai`]) were built with
//! compact contig-ID keys against their own contig dictionary, while dbSNP is
//! read from an existing `annonars dbsnp import` database with the classic
//! two-byte chromosome keys. The merge re-keys everything onto a single
//! canonical dictionary (derived from the reference `.fai`) and consolidates
//! all tracks for a coordinate into one [`IntegratedVariantRecord`], so the
//! unified database contains each coordinate exactly once (no key duplication).

use clap::Parser;
use prost::Message;
use rocksdb::{Direction, IteratorMode};

use crate::common;
use crate::common::contig::ContigDict;
use crate::common::keys::{self, Var};
use crate::pbs::seqvars::base::{CaddRecord, IntegratedVariantRecord, SpliceAiRecord};

/// Column family holding the data of an `annonars dbsnp import` database.
const DBSNP_CF_DATA: &str = "dbsnp_data";
/// Column family with the RS ID lookup of an `annonars dbsnp import` database.
const DBSNP_CF_BY_RSID: &str = "dbsnp_by_rsid";

/// Command line arguments for `seqvars unified`.
#[derive(Parser, Debug, Clone)]
#[command(about = "Merge track databases into a unified RocksDB", long_about = None)]
pub struct Args {
    /// Path to a CADD RocksDB (from `cadd import`) to include.
    #[arg(long)]
    pub cadd: Option<String>,
    /// Path to a SpliceAI RocksDB (from `spliceai import`) to include.
    #[arg(long)]
    pub spliceai: Option<String>,
    /// Path to a dbSNP RocksDB (from `dbsnp import`) to include.
    #[arg(long)]
    pub dbsnp: Option<String>,
    /// Path to the reference FASTA index (`.fai`) defining the canonical contig dictionary.
    #[arg(long)]
    pub path_reference_fai: String,
    /// Assembly / genome-release label to record in the meta CF.
    #[arg(long)]
    pub assembly: String,
    /// Path to the output (unified) RocksDB directory.
    #[arg(long)]
    pub path_out_rocksdb: String,
    /// Name of the unified data column family.
    #[arg(long, default_value = "unified")]
    pub cf_name: String,
}

/// A coordinate on the current contig, used to order the k-way merge.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Coord {
    pos: i32,
    reference: String,
    alternative: String,
}

impl From<Var> for Coord {
    fn from(var: Var) -> Self {
        Self {
            pos: var.pos,
            reference: var.reference,
            alternative: var.alternative,
        }
    }
}

/// One track's value at a coordinate.
enum TrackValue {
    Cadd(CaddRecord),
    Spliceai(SpliceAiRecord),
    DbsnpRsId(i32),
}

/// An opened source database and how to read its keys and values.
enum Source {
    /// A native track with compact contig-ID keys plus its contig dictionary.
    Cadd(ContigDict),
    /// A native track with compact contig-ID keys plus its contig dictionary.
    Spliceai(ContigDict),
    /// An `annonars dbsnp` database with classic two-byte chromosome keys.
    Dbsnp,
}

impl Source {
    /// Name of the column family holding the data.
    fn cf(&self) -> &'static str {
        match self {
            Source::Cadd(_) => "cadd",
            Source::Spliceai(_) => "spliceai",
            Source::Dbsnp => DBSNP_CF_DATA,
        }
    }

    /// Key prefix selecting `contig` in this source, or `None` if the source
    /// cannot represent the contig at all.
    fn contig_prefix(&self, contig: &str) -> Option<Vec<u8>> {
        match self {
            Source::Cadd(dict) | Source::Spliceai(dict) => {
                let id_bytes = dict.id_of(contig)?.to_be_bytes();
                Some(id_bytes[id_bytes.len() - keys::CONTIG_ID_LEN..].to_vec())
            }
            // Classic keys only cover the canonical chromosomes.
            Source::Dbsnp => common::cli::is_canonical(contig)
                .then(|| keys::chrom_name_to_key(contig).into_bytes()),
        }
    }

    /// Decode one entry into its coordinate and value.
    fn decode(&self, key: &[u8], value: &[u8]) -> Result<(Coord, TrackValue), anyhow::Error> {
        Ok(match self {
            Source::Cadd(dict) => (
                Var::decode_with_ctx(key, dict.id_to_name()).into(),
                TrackValue::Cadd(CaddRecord::decode(value)?),
            ),
            Source::Spliceai(dict) => (
                Var::decode_with_ctx(key, dict.id_to_name()).into(),
                TrackValue::Spliceai(SpliceAiRecord::decode(value)?),
            ),
            Source::Dbsnp => {
                // The dbSNP record carries its own coordinate, so the classic
                // key does not have to be decoded.
                let record = crate::dbsnp::pbs::Record::decode(value)?;
                let coord = Coord {
                    pos: record.pos,
                    reference: record.ref_allele,
                    alternative: record.alt_allele,
                };
                (coord, TrackValue::DbsnpRsId(record.rs_id))
            }
        })
    }
}

/// An opened source database together with its reader.
struct Input {
    source: Source,
    db: rocksdb::DB,
}

/// The current entry of one input within the contig being merged.
struct Front<'a> {
    source: &'a Source,
    iter: rocksdb::DBIteratorWithThreadMode<'a, rocksdb::DB>,
    prefix: Vec<u8>,
    /// Current entry; `None` once the input is past the contig.
    head: Option<(Coord, TrackValue)>,
}

impl Front<'_> {
    /// Read the next entry within the contig prefix into `head`.
    fn advance(&mut self) -> Result<(), anyhow::Error> {
        self.head = match self.iter.next().transpose()? {
            Some((k, v)) if k.starts_with(&self.prefix) => Some(self.source.decode(&k, &v)?),
            _ => None,
        };
        Ok(())
    }
}

/// Open a native track database (compact keys plus contig dictionary) read-only.
fn open_native(
    path: &str,
    cf: &str,
    make: impl FnOnce(ContigDict) -> Source,
) -> Result<Input, anyhow::Error> {
    let db = rocksdb::DB::open_cf_for_read_only(
        &rocksdb::Options::default(),
        common::readlink_f(path)?,
        ["meta", cf],
        false,
    )?;
    let dict = crate::seqvars::read_contig_dict(&db)?;
    Ok(Input {
        source: make(dict),
        db,
    })
}

/// Open an `annonars dbsnp import` database read-only.
fn open_dbsnp(path: &str) -> Result<Input, anyhow::Error> {
    let db = rocksdb::DB::open_cf_for_read_only(
        &rocksdb::Options::default(),
        common::readlink_f(path)?,
        ["meta", DBSNP_CF_DATA, DBSNP_CF_BY_RSID],
        false,
    )?;
    Ok(Input {
        source: Source::Dbsnp,
        db,
    })
}

/// Main entry point for `seqvars unified`.
pub fn run(_common: &common::cli::Args, args: &Args) -> Result<(), anyhow::Error> {
    tracing::info!("Merging track databases into {}", &args.path_out_rocksdb);
    let canonical = ContigDict::from_fai(&args.path_reference_fai)?;

    let mut inputs = Vec::new();
    if let Some(path) = &args.cadd {
        tracing::info!("  loading CADD track from {}", path);
        inputs.push(open_native(path, "cadd", Source::Cadd)?);
    }
    if let Some(path) = &args.spliceai {
        tracing::info!("  loading SpliceAI track from {}", path);
        inputs.push(open_native(path, "spliceai", Source::Spliceai)?);
    }
    if let Some(path) = &args.dbsnp {
        tracing::info!("  loading dbSNP database from {}", path);
        inputs.push(open_dbsnp(path)?);
    }
    if inputs.is_empty() {
        anyhow::bail!("no input tracks given (provide at least one of --cadd/--spliceai/--dbsnp)");
    }

    let (out_db, cf_names) =
        crate::seqvars::open_track_db_for_write(&args.path_out_rocksdb, &args.cf_name, None)?;
    crate::seqvars::write_track_meta(&out_db, "unified", "1.0", &args.assembly, &canonical)?;
    let cf_out = out_db
        .cf_handle(&args.cf_name)
        .ok_or_else(|| anyhow::anyhow!("output column family {} missing", args.cf_name))?;

    let mut count: u64 = 0;
    let mut batch = rocksdb::WriteBatch::default();
    // Process contig by contig, in canonical (output) ID order.
    for out_id in 0..canonical.len() as u32 {
        let contig = canonical
            .name_of(out_id)
            .ok_or_else(|| anyhow::anyhow!("canonical contig id {} out of range", out_id))?
            .to_string();

        // One forward prefix iterator per input that has this contig.
        let mut fronts: Vec<Front> = Vec::new();
        for input in &inputs {
            let Some(prefix) = input.source.contig_prefix(&contig) else {
                continue;
            };
            let cf = input
                .db
                .cf_handle(input.source.cf())
                .ok_or_else(|| anyhow::anyhow!("column family {} missing", input.source.cf()))?;
            let iter = input.db.iterator_cf_opt(
                &cf,
                rocksdb::ReadOptions::default(),
                IteratorMode::From(&prefix, Direction::Forward),
            );
            let mut front = Front {
                source: &input.source,
                iter,
                prefix,
                head: None,
            };
            front.advance()?;
            if front.head.is_some() {
                fronts.push(front);
            }
        }

        // k-way merge: repeatedly consume all fronts at the lowest coordinate.
        while let Some(min) = fronts
            .iter()
            .filter_map(|f| f.head.as_ref().map(|(coord, _)| coord))
            .min()
            .cloned()
        {
            let mut record = IntegratedVariantRecord::default();
            for front in fronts.iter_mut() {
                if front.head.as_ref().map(|(coord, _)| coord) != Some(&min) {
                    continue;
                }
                if let Some((_, value)) = front.head.take() {
                    match value {
                        TrackValue::Cadd(r) => record.cadd = Some(r),
                        TrackValue::Spliceai(r) => record.splice_ai = Some(r),
                        TrackValue::DbsnpRsId(rs_id) => record.dbsnp_rs_id = Some(rs_id),
                    }
                }
                front.advance()?;
            }

            let out_var = Var::new(contig.clone(), min.pos, min.reference, min.alternative);
            batch.put_cf(
                &cf_out,
                out_var.encode_with_id(out_id),
                record.encode_to_vec(),
            );
            count += 1;
            if count.is_multiple_of(crate::seqvars::WRITE_BATCH_SIZE) {
                out_db.write(std::mem::take(&mut batch))?;
            }
        }
    }
    out_db.write(batch)?;
    tracing::info!("  wrote {} unified records", count);

    let cf_refs = cf_names.iter().map(String::as_str).collect::<Vec<_>>();
    rocksdb_utils_lookup::force_compaction_cf(&out_db, &cf_refs, Some("  "), true)?;

    tracing::info!("All done. Have a nice day!");
    Ok(())
}

#[cfg(test)]
mod test {
    use super::*;
    use clap_verbosity_flag::Verbosity;
    use temp_testdir::TempDir;

    /// A dbSNP database built by `annonars dbsnp import` (BRCA1 excerpt, GRCh37).
    const DBSNP_DB: &str = "tests/dbsnp/example/dbsnp.brca1.vcf.bgz.db";

    fn common() -> common::cli::Args {
        common::cli::Args {
            verbose: Verbosity::new(0, 0),
        }
    }

    /// Build a CADD track database from the given `.fai` and TSV content.
    fn build_cadd(dir: &std::path::Path, fai: &str, tsv: &str) -> String {
        let path_fai = dir.join("cadd-ref.fa.fai");
        let path_tsv = dir.join("cadd.tsv");
        let path_db = dir.join("cadd-db");
        std::fs::write(&path_fai, fai).unwrap();
        std::fs::write(&path_tsv, tsv).unwrap();
        crate::cadd::cli::import::run(
            &common(),
            &crate::cadd::cli::import::Args {
                path_in_tsv: format!("{}", path_tsv.display()),
                path_reference_fai: format!("{}", path_fai.display()),
                assembly: "GRCh37".into(),
                path_out_rocksdb: format!("{}", path_db.display()),
                cf_name: "cadd".into(),
                path_wal_dir: None,
            },
        )
        .unwrap();
        format!("{}", path_db.display())
    }

    /// Number of records in the dbSNP fixture database.
    fn dbsnp_record_count() -> usize {
        let input = open_dbsnp(DBSNP_DB).unwrap();
        let cf = input.db.cf_handle(DBSNP_CF_DATA).unwrap();
        input.db.iterator_cf(&cf, IteratorMode::Start).count()
    }

    /// Merge the given CADD database with the dbSNP fixture and open the result.
    fn merge_with_dbsnp(
        dir: &std::path::Path,
        cadd_db: String,
        fai: &str,
    ) -> (rocksdb::DB, ContigDict) {
        let path_fai = dir.join("canonical.fa.fai");
        let path_out = dir.join("unified-db");
        std::fs::write(&path_fai, fai).unwrap();
        run(
            &common(),
            &Args {
                cadd: Some(cadd_db),
                spliceai: None,
                dbsnp: Some(DBSNP_DB.into()),
                path_reference_fai: format!("{}", path_fai.display()),
                assembly: "GRCh37".into(),
                path_out_rocksdb: format!("{}", path_out.display()),
                cf_name: "unified".into(),
            },
        )
        .unwrap();

        let db = rocksdb::DB::open_cf_for_read_only(
            &rocksdb::Options::default(),
            &path_out,
            ["meta", "unified"],
            false,
        )
        .unwrap();
        let dict = crate::seqvars::read_contig_dict(&db).unwrap();
        (db, dict)
    }

    #[test]
    fn merge_cadd_and_dbsnp() {
        let tmp = TempDir::default();
        // 17:41267746 C>A is also in dbSNP (rs80357446); 17:41267748 T>G is not.
        let cadd_db = build_cadd(
            &tmp,
            "17\t81195210\n",
            "17\t41267746\tC\tA\t0.5\t10.2\n17\t41267748\tT\tG\t-0.3\t3.1\n",
        );
        let (db, dict) = merge_with_dbsnp(&tmp, cadd_db, "17\t81195210\n");

        let cf = db.cf_handle("unified").unwrap();
        let id = dict.id_of("17").unwrap();
        let get = |pos, r: &str, a: &str| {
            db.get_cf(&cf, Var::from("17", pos, r, a).encode_with_id(id))
                .unwrap()
                .map(|raw| IntegratedVariantRecord::decode(&raw[..]).unwrap())
        };

        // Shared coordinate: both tracks in a single unified record.
        let shared = get(41_267_746, "C", "A").expect("shared present");
        assert!(shared.cadd.is_some());
        assert_eq!(shared.dbsnp_rs_id, Some(80_357_446));
        // CADD-only.
        let cadd_only = get(41_267_748, "T", "G").expect("cadd-only present");
        assert!(cadd_only.cadd.is_some() && cadd_only.dbsnp_rs_id.is_none());
        // dbSNP-only.
        let dbsnp_only = get(41_267_747, "A", "C").expect("dbsnp-only present");
        assert_eq!(dbsnp_only.dbsnp_rs_id, Some(80_357_327));
        assert!(dbsnp_only.cadd.is_none());

        // Every dbSNP record plus the single CADD-only one, each exactly once.
        assert_eq!(
            db.iterator_cf(&cf, IteratorMode::Start).count(),
            dbsnp_record_count() + 1
        );
    }

    #[test]
    fn merge_handles_chr_prefix_mismatch() {
        // The CADD track uses "chr17" while dbSNP uses "17"; canonicalization must
        // still land both on the same unified key.
        let tmp = TempDir::default();
        let cadd_db = build_cadd(
            &tmp,
            "chr17\t81195210\n",
            "chr17\t41267746\tC\tA\t0.5\t10.2\n",
        );
        let (db, dict) = merge_with_dbsnp(&tmp, cadd_db, "17\t81195210\n");

        let cf = db.cf_handle("unified").unwrap();
        let key = Var::from("17", 41_267_746, "C", "A").encode_with_id(dict.id_of("17").unwrap());
        let rec = db
            .get_cf(&cf, key)
            .unwrap()
            .map(|raw| IntegratedVariantRecord::decode(&raw[..]).unwrap())
            .expect("merged record present");
        assert!(rec.cadd.is_some());
        assert_eq!(rec.dbsnp_rs_id, Some(80_357_446));
        assert_eq!(
            db.iterator_cf(&cf, IteratorMode::Start).count(),
            dbsnp_record_count()
        );
    }
}