bse 0.1.4

Basis Set Exchange (BSE) library in Rust for quantum chemistry applications.
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! Handlers for CLI subcommands.
//!
//! This module contains the implementation of each CLI subcommand,
//! following the pattern of Python's `bse_handlers.py`.

use std::path::PathBuf;

use crate::prelude::*;
use crate::{bse_raise, misc::compact_elements, BseDataSource, BseError};

use super::check::{detect_dir_format_from_files, detect_format_from_extension};
use super::common::{format_columns, format_map_columns, format_table, get_cli_only_formats, resolve_cli_format};

/// Handle the `list-writer-formats` subcommand.
pub fn handle_list_writer_formats(no_description: bool) -> Result<String, BseError> {
    let formats = get_writer_formats_with_aliases(None);

    if no_description {
        let names: Vec<String> = formats
            .iter()
            .flat_map(|(name, (_, _, aliases))| std::iter::once(name.clone()).chain(aliases.clone()))
            .chain(get_cli_only_formats().iter().map(|(name, _, _, _)| name.clone()))
            .collect();
        // Sort alphabetically
        let mut sorted_names = names;
        sorted_names.sort();
        Ok(sorted_names.join("\n"))
    } else {
        // Format as table: name | extension | aliases | display
        let headers = ["Name", "Extension", "Aliases", "Display"];
        let mut rows: Vec<Vec<String>> = formats
            .iter()
            .map(|(name, (display, extension, aliases))| {
                vec![name.clone(), extension.clone(), aliases.join(", "), display.clone()]
            })
            .collect();
        // Add CLI-only formats
        for (name, extension, aliases, display) in get_cli_only_formats() {
            rows.push(vec![name, extension, aliases, display]);
        }
        // Sort alphabetically by name
        rows.sort_by(|a, b| a[0].cmp(&b[0]));
        Ok(format_table(&headers, &rows).join("\n"))
    }
}

/// Handle the `list-reader-formats` subcommand.
pub fn handle_list_reader_formats(no_description: bool) -> Result<String, BseError> {
    let formats = get_reader_formats_with_aliases();

    if no_description {
        let names: Vec<String> = formats
            .iter()
            .flat_map(|(name, (_, _, aliases))| std::iter::once(name.clone()).chain(aliases.clone()))
            .chain(get_cli_only_formats().iter().map(|(name, _, _, _)| name.clone()))
            .collect();
        // Sort alphabetically
        let mut sorted_names = names;
        sorted_names.sort();
        Ok(sorted_names.join("\n"))
    } else {
        // Format as table: name | extension | aliases | display
        let headers = ["Name", "Extension", "Aliases", "Display"];
        let mut rows: Vec<Vec<String>> = formats
            .iter()
            .map(|(name, (display, extension, aliases))| {
                vec![name.clone(), extension.clone(), aliases.join(", "), display.clone()]
            })
            .collect();
        // Add CLI-only formats
        for (name, extension, aliases, display) in get_cli_only_formats() {
            rows.push(vec![name, extension, aliases, display]);
        }
        // Sort alphabetically by name
        rows.sort_by(|a, b| a[0].cmp(&b[0]));
        Ok(format_table(&headers, &rows).join("\n"))
    }
}

/// Handle the `list-ref-formats` subcommand.
pub fn handle_list_ref_formats(no_description: bool) -> Result<String, BseError> {
    let formats = get_reference_formats();

    if no_description {
        Ok(formats.keys().cloned().collect::<Vec<_>>().join("\n").to_string())
    } else {
        let items: Vec<(String, String)> = formats.into_iter().collect();
        Ok(format_map_columns(&items, "").join("\n"))
    }
}

/// Handle the `list-roles` subcommand.
pub fn handle_list_roles(no_description: bool) -> Result<String, BseError> {
    let roles = get_roles();

    if no_description {
        Ok(roles.keys().cloned().collect::<Vec<_>>().join("\n").to_string())
    } else {
        let items: Vec<(&str, &str)> = roles.into_iter().collect();
        Ok(format_map_columns(&items, "").join("\n"))
    }
}

/// Handle the `get-data-dir` subcommand.
pub fn handle_get_data_dir() -> Result<String, BseError> {
    match get_bse_data_dir() {
        Some(dir) => Ok(dir),
        None => bse_raise!(ValueError, "No data directory available. Set BSE_DATA_DIR environment variable."),
    }
}

/// Handle the `list-basis-sets` subcommand.
///
/// Lists basis sets with optional filtering by family, role, substring, or
/// elements. Output is grouped by family and sorted alphabetically within
/// each family. Basis sets without a family are shown at the end.
pub fn handle_list_basis_sets(
    substr: Option<String>,
    family: Option<String>,
    role: Option<String>,
    elements: Option<String>,
    data_dir: Option<String>,
    no_description: bool,
) -> Result<String, BseError> {
    let args = BseFilterArgsBuilder::default()
        .substr(substr)
        .family(family)
        .role(role)
        .elements(elements)
        .data_dir(data_dir)
        .build()?;

    let metadata = filter_basis_sets(args);

    // Group by family, separating empty families
    let mut by_family: std::collections::BTreeMap<String, Vec<(String, String)>> = std::collections::BTreeMap::new();
    let mut no_family: Vec<(String, String)> = Vec::new();

    for v in metadata.values() {
        if v.family.is_empty() {
            no_family.push((v.display_name.clone(), v.description.clone()));
        } else {
            by_family.entry(v.family.clone()).or_default().push((v.display_name.clone(), v.description.clone()));
        }
    }

    // Sort within each family
    for items in by_family.values_mut() {
        items.sort_by(|a, b| a.0.cmp(&b.0));
    }
    no_family.sort_by(|a, b| a.0.cmp(&b.0));

    if no_description {
        // Simple list: just names, sorted by family then alphabetically
        let mut result = Vec::new();
        for items in by_family.values() {
            for (name, _) in items {
                result.push(name.clone());
            }
        }
        for (name, _) in &no_family {
            result.push(name.clone());
        }
        Ok(result.join("\n"))
    } else {
        // Format as multi-table with borders
        let mut result = Vec::new();

        // Process families in order (BTreeMap is sorted)
        for (family, items) in by_family {
            result.push(format!("\n[{}]", family));
            let headers = ["Name", "Description"];
            let rows: Vec<Vec<String>> = items.into_iter().map(|(n, d)| vec![n, d]).collect();
            result.extend(format_table(&headers, &rows));
        }

        // Add uncategorized at the end if any
        if !no_family.is_empty() {
            result.push("\n[uncategorized]".to_string());
            let headers = ["Name", "Description"];
            let rows: Vec<Vec<String>> = no_family.into_iter().map(|(n, d)| vec![n, d]).collect();
            result.extend(format_table(&headers, &rows));
        }

        // Remove leading newline if present
        if !result.is_empty() && result[0].starts_with('\n') {
            result[0] = result[0][1..].to_string();
        }
        Ok(result.join("\n"))
    }
}

/// Handle the `list-families` subcommand.
pub fn handle_list_families(data_dir: Option<String>) -> Result<String, BseError> {
    let families = get_families(data_dir);
    Ok(families.join("\n"))
}

/// Handle the `lookup-by-role` subcommand.
pub fn handle_lookup_by_role(basis: String, role: String, data_dir: Option<String>) -> Result<String, BseError> {
    let aux_names = lookup_basis_by_role(&basis, &role, data_dir);
    Ok(aux_names.join("\n"))
}

/// Handle the `get-basis` subcommand.
///
/// Outputs a formatted basis set with all optional manipulations.
/// Supports both single-file and directory formats.
#[allow(clippy::too_many_arguments)]
pub fn handle_get_basis(
    basis: String,
    fmt: String,
    elements: Option<String>,
    version: Option<String>,
    noheader: bool,
    unc_gen: bool,
    unc_spdf: bool,
    unc_seg: bool,
    rm_free: bool,
    opt_gen: bool,
    make_gen: bool,
    aug_diffuse: i32,
    aug_steep: i32,
    get_aux: i32,
    data_dir: Option<String>,
    output_path: Option<PathBuf>,
    source: BseDataSource,
) -> Result<String, BseError> {
    let args = BseGetBasisArgsBuilder::default()
        .elements(elements)
        .version(version)
        .header(!noheader)
        .uncontract_general(unc_gen)
        .uncontract_spdf(unc_spdf)
        .uncontract_segmented(unc_seg)
        .remove_free_primitives(rm_free)
        .optimize_general(opt_gen)
        .make_general(make_gen)
        .augment_diffuse(aug_diffuse)
        .augment_steep(aug_steep)
        .get_aux(get_aux)
        .data_dir(data_dir)
        .source(source)
        .build()?;

    // Resolve CLI-only format aliases (e.g., "rest" -> "dir-json")
    let resolved_fmt = resolve_cli_format(&fmt);

    // Check if directory format
    if is_dir_format(&resolved_fmt) {
        // For directory format, output_path is required
        let dir_path = output_path.unwrap_or(basis.as_str().into());

        let underlying_fmt = strip_dir_prefix(&resolved_fmt);
        let basis_data = get_basis(&basis, args);
        write_basis_to_dir_f(&basis_data, &dir_path, underlying_fmt)?;

        Ok(format!("Basis set '{}' written to {}", basis, dir_path.display()))
    } else {
        Ok(get_formatted_basis(&basis, &resolved_fmt, args))
    }
}

/// Handle the `get-refs` subcommand.
pub fn handle_get_refs(
    basis: String,
    reffmt: String,
    elements: Option<String>,
    version: Option<String>,
    _data_dir: Option<String>,
) -> Result<String, BseError> {
    Ok(get_references_formatted(&basis, elements.as_deref(), version.as_deref(), &reffmt))
}

/// Handle the `get-info` subcommand.
///
/// Outputs detailed metadata about a basis set.
pub fn handle_get_info(basis: String, data_dir: Option<String>) -> Result<String, BseError> {
    let resolved_data_dir = data_dir.clone().or(get_bse_data_dir());
    if resolved_data_dir.is_none() {
        return bse_raise!(ValueError, "No data directory available. Set BSE_DATA_DIR environment variable.");
    }

    let metadata = get_metadata(&resolved_data_dir.unwrap());
    let tr_name = crate::misc::transform_basis_name(&basis);

    if !metadata.contains_key(&tr_name) {
        return bse_raise!(ValueError, "Basis set '{}' does not exist.", basis);
    }

    let bs_meta = &metadata[&tr_name];
    let mut ret = Vec::new();

    ret.push("-".repeat(80));
    ret.push(basis.clone());
    ret.push("-".repeat(80));
    ret.push(format!("    Display Name: {}", bs_meta.display_name));
    ret.push(format!("     Description: {}", bs_meta.description));
    ret.push(format!("            Role: {}", bs_meta.role));
    ret.push(format!("          Family: {}", bs_meta.family));
    ret.push(format!("  Function Types: {}", bs_meta.function_types.join(",")));
    ret.push(format!("  Latest Version: {}", bs_meta.latest_version));
    ret.push(String::new());

    // Auxiliary basis sets
    if bs_meta.auxiliaries.is_empty() {
        ret.push("Auxiliary Basis Sets: None".to_string());
    } else {
        ret.push("Auxiliary Basis Sets:".to_string());
        let aux_items: Vec<(String, String)> = bs_meta
            .auxiliaries
            .iter()
            .map(|(k, v)| match v {
                BseAuxiliary::Str(s) => (k.clone(), s.clone()),
                BseAuxiliary::Vec(v) => (k.clone(), v.join(", ")),
            })
            .collect();
        ret.extend(format_map_columns(&aux_items, "    "));
    }

    // Versions
    ret.push(String::new());
    ret.push("Versions:".to_string());
    let version_items: Vec<Vec<String>> = bs_meta
        .versions
        .iter()
        .map(|(k, v)| {
            vec![
                k.clone(),
                v.revdate.clone(),
                compact_elements(&v.elements.iter().filter_map(|e| e.parse::<i32>().ok()).collect::<Vec<_>>()),
                v.revdesc.clone(),
            ]
        })
        .collect();
    ret.extend(format_columns(&version_items, "    "));

    Ok(ret.join("\n"))
}

/// Handle the `get-notes` subcommand.
pub fn handle_get_notes(basis: String, data_dir: Option<String>) -> Result<String, BseError> {
    Ok(get_basis_notes(&basis, data_dir))
}

/// Handle the `get-family` subcommand.
pub fn handle_get_family(basis: String, data_dir: Option<String>) -> Result<String, BseError> {
    let resolved_data_dir = data_dir.clone().or(get_bse_data_dir());
    if resolved_data_dir.is_none() {
        return bse_raise!(ValueError, "No data directory available. Set BSE_DATA_DIR environment variable.");
    }

    let metadata = get_metadata(&resolved_data_dir.unwrap());
    let tr_name = crate::misc::transform_basis_name(&basis);

    if !metadata.contains_key(&tr_name) {
        return bse_raise!(ValueError, "Basis set '{}' does not exist.", basis);
    }

    Ok(metadata[&tr_name].family.clone())
}

/// Handle the `get-versions` subcommand.
pub fn handle_get_versions(basis: String, data_dir: Option<String>, no_description: bool) -> Result<String, BseError> {
    let resolved_data_dir = data_dir.clone().or(get_bse_data_dir());
    if resolved_data_dir.is_none() {
        return bse_raise!(ValueError, "No data directory available. Set BSE_DATA_DIR environment variable.");
    }

    let metadata = get_metadata(&resolved_data_dir.unwrap());
    let tr_name = crate::misc::transform_basis_name(&basis);

    if !metadata.contains_key(&tr_name) {
        return bse_raise!(ValueError, "Basis set '{}' does not exist.", basis);
    }

    let versions = &metadata[&tr_name].versions;

    if no_description {
        Ok(versions.keys().cloned().collect::<Vec<_>>().join("\n"))
    } else {
        let items: Vec<(String, String)> = versions.iter().map(|(k, v)| (k.clone(), v.revdesc.clone())).collect();
        Ok(format_map_columns(&items, "").join("\n"))
    }
}

/// Handle the `get-family-notes` subcommand.
pub fn handle_get_family_notes(family: String, data_dir: Option<String>) -> Result<String, BseError> {
    Ok(get_family_notes(&family, data_dir))
}

/// Handle the `convert-basis` subcommand.
///
/// Converts a basis set file from one format to another.
/// Supports both single-file and directory formats.
pub fn handle_convert_basis(
    input_file: PathBuf,
    output_file: PathBuf,
    in_fmt: Option<String>,
    out_fmt: Option<String>,
    make_gen: bool,
) -> Result<String, BseError> {
    // Check if paths exist and are directories
    let input_is_dir = input_file.is_dir();
    let output_exists = output_file.exists();
    let output_is_dir = output_file.is_dir();

    // Detect input format
    let resolved_in_fmt = in_fmt.map(|f| resolve_cli_format(&f)).or_else(|| {
        if input_is_dir {
            detect_dir_format_from_files(&input_file, true)
        } else {
            detect_format_from_extension(&input_file.to_string_lossy(), true)
        }
    });

    if resolved_in_fmt.is_none() {
        return bse_raise!(
            ValueError,
            "Could not detect input format from '{}'. Specify format with --in-fmt",
            input_file.display()
        );
    }

    // Detect output format with strict validation
    // User-specified format takes precedence
    let resolved_out_fmt = if let Some(fmt) = &out_fmt {
        let resolved = resolve_cli_format(fmt);
        let is_dir_fmt = is_dir_format(&resolved);

        // Validate: dir format can't be written to existing file
        if output_exists && !output_is_dir && is_dir_fmt {
            return bse_raise!(
                ValueError,
                "Cannot write directory format '{}' to a file path '{}'. Use a directory path instead.",
                fmt,
                output_file.display()
            );
        }

        // Validate: non-dir format can't be written to existing directory
        if output_exists && output_is_dir && !is_dir_fmt {
            return bse_raise!(
                ValueError,
                "Cannot write single-file format '{}' to a directory '{}'. Use a file path instead.",
                fmt,
                output_file.display()
            );
        }

        Some(resolved)
    } else {
        // No --out-fmt specified - try auto-detection
        if output_is_dir {
            // Existing directory: try to detect from files inside
            detect_dir_format_from_files(&output_file, false)
        } else if output_exists {
            // Existing file: detect from extension
            detect_format_from_extension(&output_file.to_string_lossy(), false)
        } else {
            // Output doesn't exist: detect from extension in filename
            detect_format_from_extension(&output_file.to_string_lossy(), false)
        }
    };

    if resolved_out_fmt.is_none() {
        if output_is_dir {
            return bse_raise!(
                ValueError,
                "Could not detect output format from directory '{}'. Specify format with --out-fmt",
                output_file.display()
            );
        } else {
            return bse_raise!(
                ValueError,
                "Could not detect output format from filename '{}'. Specify format with --out-fmt",
                output_file.display()
            );
        }
    }

    let in_fmt_resolved = resolved_in_fmt.unwrap();
    let out_fmt_resolved = resolved_out_fmt.unwrap();

    // Handle directory formats
    let input_dir_mode = is_dir_format(&in_fmt_resolved) || input_is_dir;
    let output_dir_mode = is_dir_format(&out_fmt_resolved) || output_is_dir;

    if input_dir_mode && output_dir_mode {
        // Directory to directory conversion
        let underlying_in_fmt = strip_dir_prefix(&in_fmt_resolved);
        let underlying_out_fmt = strip_dir_prefix(&out_fmt_resolved);

        let mut basis = read_basis_from_dir(&input_file, underlying_in_fmt);

        // Apply make_general if requested
        if make_gen {
            let mut full_basis = BseBasis::from_minimal(basis);
            crate::manip::make_general(&mut full_basis, false);
            crate::manip::prune_basis(&mut full_basis);
            basis = BseBasisMinimal {
                molssi_bse_schema: full_basis.molssi_bse_schema,
                elements: full_basis.elements,
                function_types: full_basis.function_types,
                name: full_basis.name,
                description: full_basis.description,
            };
        }

        write_basis_to_dir_f(&BseBasis::from_minimal(basis), &output_file, underlying_out_fmt)?;

        return Ok(format!("Converted {} -> {}", input_file.display(), output_file.display()));
    }

    if input_dir_mode {
        // Directory to file conversion
        let underlying_in_fmt = strip_dir_prefix(&in_fmt_resolved);
        let mut basis = read_basis_from_dir(&input_file, underlying_in_fmt);

        if make_gen {
            let mut full_basis = BseBasis::from_minimal(basis);
            crate::manip::make_general(&mut full_basis, false);
            crate::manip::prune_basis(&mut full_basis);
            basis = BseBasisMinimal {
                molssi_bse_schema: full_basis.molssi_bse_schema,
                elements: full_basis.elements,
                function_types: full_basis.function_types,
                name: full_basis.name,
                description: full_basis.description,
            };
        }

        let full_basis = BseBasis::from_minimal(basis);
        let output_str = write_formatted_basis_str(&full_basis, &out_fmt_resolved, None);
        std::fs::write(&output_file, output_str)?;

        return Ok(format!("Converted {} -> {}", input_file.display(), output_file.display()));
    }

    if output_dir_mode {
        // File to directory conversion
        let underlying_out_fmt = strip_dir_prefix(&out_fmt_resolved);

        let input_str = std::fs::read_to_string(&input_file)?;
        let basis_minimal = read_formatted_basis_str(&input_str, &in_fmt_resolved);

        let mut basis = BseBasis::from_minimal(basis_minimal);

        if make_gen {
            crate::manip::make_general(&mut basis, false);
        }

        write_basis_to_dir_f(&basis, &output_file, underlying_out_fmt)?;

        return Ok(format!("Converted {} -> {}", input_file.display(), output_file.display()));
    }

    // Standard file to file conversion
    let input_str = std::fs::read_to_string(&input_file)?;
    let basis_minimal = read_formatted_basis_str(&input_str, &in_fmt_resolved);

    // Convert to full BseBasis for manipulation
    let mut basis = BseBasis::from_minimal(basis_minimal);

    // Apply make_general if requested
    if make_gen {
        crate::manip::make_general(&mut basis, false);
    }

    // Write the output
    let output_str = write_formatted_basis_str(&basis, &out_fmt_resolved, None);
    std::fs::write(&output_file, output_str)?;

    Ok(format!("Converted {} -> {}", input_file.display(), output_file.display()))
}

/// Handle the `autoaux-basis` subcommand.
///
/// Generates an AutoAux auxiliary basis from an orbital basis file.
pub fn handle_autoaux_basis(
    input_file: PathBuf,
    output_file: PathBuf,
    in_fmt: Option<String>,
    out_fmt: Option<String>,
) -> Result<String, BseError> {
    // Detect formats from file extensions if not specified
    let resolved_in_fmt = in_fmt.or_else(|| detect_format_from_extension(&input_file.to_string_lossy(), true));
    let resolved_out_fmt = out_fmt.or_else(|| detect_format_from_extension(&output_file.to_string_lossy(), false));

    if resolved_in_fmt.is_none() {
        return bse_raise!(
            ValueError,
            "Could not detect input format from filename '{}'. Specify format with --in-fmt",
            input_file.display()
        );
    }
    if resolved_out_fmt.is_none() {
        return bse_raise!(
            ValueError,
            "Could not detect output format from filename '{}'. Specify format with --out-fmt",
            output_file.display()
        );
    }

    // Read the input file
    let input_str = std::fs::read_to_string(&input_file)?;
    let basis_minimal = read_formatted_basis_str(&input_str, &resolved_in_fmt.unwrap());

    // Convert to full BseBasis
    let basis = BseBasis::from_minimal(basis_minimal);

    // Generate AutoAux basis
    let autoaux = crate::manip::autoaux_basis(&basis);

    // Write the output
    let output_str = write_formatted_basis_str(&autoaux, &resolved_out_fmt.unwrap(), None);
    std::fs::write(&output_file, output_str)?;

    Ok(format!("Orbital basis {} -> AutoAux basis {}", input_file.display(), output_file.display()))
}

/// Handle the `autoabs-basis` subcommand.
///
/// Generates an AutoABS auxiliary basis from an orbital basis file.
pub fn handle_autoabs_basis(
    input_file: PathBuf,
    output_file: PathBuf,
    in_fmt: Option<String>,
    out_fmt: Option<String>,
) -> Result<String, BseError> {
    // Detect formats from file extensions if not specified
    let resolved_in_fmt = in_fmt.or_else(|| detect_format_from_extension(&input_file.to_string_lossy(), true));
    let resolved_out_fmt = out_fmt.or_else(|| detect_format_from_extension(&output_file.to_string_lossy(), false));

    if resolved_in_fmt.is_none() {
        return bse_raise!(
            ValueError,
            "Could not detect input format from filename '{}'. Specify format with --in-fmt",
            input_file.display()
        );
    }
    if resolved_out_fmt.is_none() {
        return bse_raise!(
            ValueError,
            "Could not detect output format from filename '{}'. Specify format with --out-fmt",
            output_file.display()
        );
    }

    // Read the input file
    let input_str = std::fs::read_to_string(&input_file)?;
    let basis_minimal = read_formatted_basis_str(&input_str, &resolved_in_fmt.unwrap());

    // Convert to full BseBasis
    let basis = BseBasis::from_minimal(basis_minimal);

    // Generate AutoABS basis
    let autoabs = crate::manip::autoabs_basis(&basis, 1, 1.5);

    // Write the output
    let output_str = write_formatted_basis_str(&autoabs, &resolved_out_fmt.unwrap(), None);
    std::fs::write(&output_file, output_str)?;

    Ok(format!("Orbital basis {} -> AutoABS basis {}", input_file.display(), output_file.display()))
}