rype 1.0.0-rc.1

High-performance genomic sequence classification using minimizer-based k-mer sketching in RY space
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
use anyhow::Result;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;

#[test]
fn test_config_based_index_building() -> Result<()> {
    let dir = tempdir()?;

    // Create test FASTA files
    let ref1_path = dir.path().join("ref1.fa");
    let ref2_path = dir.path().join("ref2.fa");
    let ref3_path = dir.path().join("ref3.fa");

    // Write simple test sequences
    let mut file1 = File::create(&ref1_path)?;
    writeln!(file1, ">seq1")?;
    writeln!(
        file1,
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
    )?;

    let mut file2 = File::create(&ref2_path)?;
    writeln!(file2, ">seq2")?;
    writeln!(
        file2,
        "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"
    )?;

    let mut file3 = File::create(&ref3_path)?;
    writeln!(file3, ">seq3")?;
    writeln!(
        file3,
        "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG"
    )?;

    // Create TOML config file
    let config_path = dir.path().join("test_config.toml");
    let output_path = dir.path().join("test_output.ryidx");

    let config_content = format!(
        r#"
[index]
window = 50
salt = 0x5555555555555555
output = "{}"

[buckets.BucketA]
files = ["ref1.fa", "ref2.fa"]

[buckets.BucketB]
files = ["ref3.fa"]
"#,
        output_path.display()
    );

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    // Run the index-from-config command via the CLI module
    // Since we can't easily call main(), we'll test the config module directly

    // Parse config
    let cfg = rype::config::parse_config(&config_path)?;

    // Validate
    rype::config::validate_config(&cfg, dir.path())?;

    // Verify config parsed correctly
    assert_eq!(cfg.index.window, 50);
    assert_eq!(cfg.buckets.len(), 2);
    assert!(cfg.buckets.contains_key("BucketA"));
    assert!(cfg.buckets.contains_key("BucketB"));
    assert_eq!(cfg.buckets["BucketA"].files.len(), 2);
    assert_eq!(cfg.buckets["BucketB"].files.len(), 1);

    Ok(())
}

#[test]
fn test_config_validation_missing_file() -> Result<()> {
    let dir = tempdir()?;

    // Create config referencing non-existent file
    let config_path = dir.path().join("test_config.toml");
    let config_content = r#"
[index]
window = 50
salt = 0x5555555555555555
output = "test.ryidx"

[buckets.TestBucket]
files = ["nonexistent.fa"]
"#;

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    // Parse should succeed
    let cfg = rype::config::parse_config(&config_path)?;

    // But validation should fail
    let result = rype::config::validate_config(&cfg, dir.path());
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("File not found"));

    Ok(())
}

#[test]
fn test_config_empty_buckets() -> Result<()> {
    let dir = tempdir()?;

    // Create config with no buckets
    let config_path = dir.path().join("test_config.toml");
    let config_content = r#"
[index]
window = 50
salt = 0x5555555555555555
output = "test.ryidx"
"#;

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    // Parse should fail because buckets field is missing
    let result = rype::config::parse_config(&config_path);
    assert!(result.is_err());
    // The error will be about parsing TOML (missing field)
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Failed to parse TOML"));

    Ok(())
}

// --- BUCKET ADD CONFIG TESTS ---

#[test]
fn test_bucket_add_config_parse_new_bucket() -> Result<()> {
    let dir = tempdir()?;

    // Create a dummy index file
    let index_path = dir.path().join("test.ryidx");
    File::create(&index_path)?;

    // Create a test FASTA file
    let ref_path = dir.path().join("ref.fa");
    let mut file = File::create(&ref_path)?;
    writeln!(
        file,
        ">seq1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
    )?;

    // Create bucket-add config
    let config_path = dir.path().join("add_config.toml");
    let config_content = format!(
        r#"
[target]
index = "{}"

[assignment]
mode = "new_bucket"
bucket_name = "NewBucket"

[files]
paths = ["ref.fa"]
"#,
        index_path.display()
    );

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    let cfg = rype::config::parse_bucket_add_config(&config_path)?;

    assert_eq!(cfg.files.paths.len(), 1);
    match &cfg.assignment {
        rype::config::AssignmentSettings::NewBucket { bucket_name } => {
            assert_eq!(bucket_name.as_deref(), Some("NewBucket"));
        }
        _ => panic!("Expected NewBucket mode"),
    }

    rype::config::validate_bucket_add_config(&cfg, dir.path())?;

    Ok(())
}

#[test]
fn test_bucket_add_config_parse_existing_bucket() -> Result<()> {
    let dir = tempdir()?;

    // Create a dummy index file
    let index_path = dir.path().join("test.ryidx");
    File::create(&index_path)?;

    // Create a test FASTA file
    let ref_path = dir.path().join("ref.fa");
    let mut file = File::create(&ref_path)?;
    writeln!(
        file,
        ">seq1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
    )?;

    // Create bucket-add config
    let config_path = dir.path().join("add_config.toml");
    let config_content = format!(
        r#"
[target]
index = "{}"

[assignment]
mode = "existing_bucket"
bucket_name = "ExistingBucket"

[files]
paths = ["ref.fa"]
"#,
        index_path.display()
    );

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    let cfg = rype::config::parse_bucket_add_config(&config_path)?;

    match &cfg.assignment {
        rype::config::AssignmentSettings::ExistingBucket { bucket_name } => {
            assert_eq!(bucket_name, "ExistingBucket");
        }
        _ => panic!("Expected ExistingBucket mode"),
    }

    Ok(())
}

#[test]
fn test_bucket_add_config_parse_best_bin() -> Result<()> {
    let dir = tempdir()?;

    // Create a dummy index file
    let index_path = dir.path().join("test.ryidx");
    File::create(&index_path)?;

    // Create a test FASTA file
    let ref_path = dir.path().join("ref.fa");
    let mut file = File::create(&ref_path)?;
    writeln!(
        file,
        ">seq1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
    )?;

    // Create bucket-add config with best_bin mode
    let config_path = dir.path().join("add_config.toml");
    let config_content = format!(
        r#"
[target]
index = "{}"

[assignment]
mode = "best_bin"
threshold = 0.25
fallback = "create_new"

[files]
paths = ["ref.fa"]
"#,
        index_path.display()
    );

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    let cfg = rype::config::parse_bucket_add_config(&config_path)?;

    match &cfg.assignment {
        rype::config::AssignmentSettings::BestBin {
            threshold,
            fallback,
        } => {
            assert!((threshold - 0.25).abs() < 0.001);
            assert_eq!(*fallback, rype::config::BestBinFallback::CreateNew);
        }
        _ => panic!("Expected BestBin mode"),
    }

    Ok(())
}

#[test]
fn test_bucket_add_config_parse_best_bin_skip() -> Result<()> {
    let dir = tempdir()?;

    // Create a dummy index file
    let index_path = dir.path().join("test.ryidx");
    File::create(&index_path)?;

    // Create a test FASTA file
    let ref_path = dir.path().join("ref.fa");
    let mut file = File::create(&ref_path)?;
    writeln!(
        file,
        ">seq1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
    )?;

    // Create bucket-add config with skip fallback
    let config_path = dir.path().join("add_config.toml");
    let config_content = format!(
        r#"
[target]
index = "{}"

[assignment]
mode = "best_bin"
threshold = 0.5
fallback = "skip"

[files]
paths = ["ref.fa"]
"#,
        index_path.display()
    );

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    let cfg = rype::config::parse_bucket_add_config(&config_path)?;

    match &cfg.assignment {
        rype::config::AssignmentSettings::BestBin {
            threshold,
            fallback,
        } => {
            assert!((threshold - 0.5).abs() < 0.001);
            assert_eq!(*fallback, rype::config::BestBinFallback::Skip);
        }
        _ => panic!("Expected BestBin mode"),
    }

    Ok(())
}

#[test]
fn test_bucket_add_config_invalid_threshold() -> Result<()> {
    let dir = tempdir()?;

    // Create a dummy index file
    let index_path = dir.path().join("test.ryidx");
    File::create(&index_path)?;

    // Create config with invalid threshold
    let config_path = dir.path().join("add_config.toml");
    let config_content = format!(
        r#"
[target]
index = "{}"

[assignment]
mode = "best_bin"
threshold = 1.5

[files]
paths = ["nonexistent.fa"]
"#,
        index_path.display()
    );

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    let result = rype::config::parse_bucket_add_config(&config_path);
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("threshold must be between 0.0 and 1.0"));

    Ok(())
}

#[test]
fn test_bucket_add_config_validation_missing_index() -> Result<()> {
    let dir = tempdir()?;

    // Create a test FASTA file
    let ref_path = dir.path().join("ref.fa");
    let mut file = File::create(&ref_path)?;
    writeln!(
        file,
        ">seq1\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
    )?;

    // Create config pointing to non-existent index
    let config_path = dir.path().join("add_config.toml");
    let config_content = r#"
[target]
index = "nonexistent.ryidx"

[assignment]
mode = "new_bucket"

[files]
paths = ["ref.fa"]
"#;

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    let cfg = rype::config::parse_bucket_add_config(&config_path)?;
    let result = rype::config::validate_bucket_add_config(&cfg, dir.path());
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Target index not found"));

    Ok(())
}

#[test]
fn test_bucket_add_config_validation_missing_file() -> Result<()> {
    let dir = tempdir()?;

    // Create a dummy index file
    let index_path = dir.path().join("test.ryidx");
    File::create(&index_path)?;

    // Create config pointing to non-existent file
    let config_path = dir.path().join("add_config.toml");
    let config_content = format!(
        r#"
[target]
index = "{}"

[assignment]
mode = "new_bucket"

[files]
paths = ["nonexistent.fa"]
"#,
        index_path.display()
    );

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    let cfg = rype::config::parse_bucket_add_config(&config_path)?;
    let result = rype::config::validate_bucket_add_config(&cfg, dir.path());
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("File not found"));

    Ok(())
}

#[test]
fn test_bucket_add_config_empty_files() -> Result<()> {
    let dir = tempdir()?;

    // Create config with empty files list
    let config_path = dir.path().join("add_config.toml");
    let config_content = r#"
[target]
index = "test.ryidx"

[assignment]
mode = "new_bucket"

[files]
paths = []
"#;

    let mut config_file = File::create(&config_path)?;
    config_file.write_all(config_content.as_bytes())?;
    drop(config_file);

    let result = rype::config::parse_bucket_add_config(&config_path);
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("at least one file"));

    Ok(())
}