Skip to content

Commit

Permalink
优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
eric committed Apr 22, 2024
1 parent d8b22dd commit a2a6740
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
6 changes: 3 additions & 3 deletions kr2r/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ pub struct Build {

/// Kraken 2 hash table filename
#[clap(short = 'H', required = true)]
pub hashtable_filename: PathBuf,
pub hashtable_filename: Option<PathBuf>,

/// Kraken 2 options filename
#[clap(short = 'o', required = true)]
pub options_filename: PathBuf,
pub options_filename: Option<PathBuf>,

/// 包含原始配置
#[clap(flatten)]
Expand All @@ -43,7 +43,7 @@ pub struct Build {
pub struct Taxo {
/// Kraken 2 taxonomy filename
#[clap(short = 't', required = true)]
pub taxonomy_filename: PathBuf,
pub taxonomy_filename: Option<PathBuf>,

/// Sequence ID to taxon map filename
#[clap(short = 'm', required = true)]
Expand Down
18 changes: 15 additions & 3 deletions kr2r/src/bin/build_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let id_to_taxon_map = read_id_to_taxon_map(&args.taxo.id_to_taxon_map_filename)?;

let taxonomy_filename = args
.taxo
.taxonomy_filename
.unwrap_or(args.build.source.join("taxo.k2d"));
let taxonomy = generate_taxonomy(
&args.taxo.ncbi_taxonomy_directory,
&args.taxo.taxonomy_filename,
&taxonomy_filename,
&id_to_taxon_map,
)?;

Expand All @@ -46,8 +50,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let capacity = args.required_capacity as usize;

let hashtable_filename = args
.build
.hashtable_filename
.unwrap_or(args.build.source.clone().join("hash.k2d"));
let hash_config = HashConfig::<u32>::new(capacity, value_bits, 0, 0, 0);
let mut chtm = CHTableMut::new(args.build.hashtable_filename, hash_config, 0, capacity)?;
let mut chtm = CHTableMut::new(hashtable_filename, hash_config, 0, capacity)?;

let source: PathBuf = args.build.source.clone();
let fna_files = if source.is_file() {
Expand Down Expand Up @@ -75,7 +83,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("build db took: {:?}", duration);

let idx_opts = IndexOptions::from_meros(meros);
idx_opts.write_to_file(args.build.options_filename)?;
let options_filename = args
.build
.options_filename
.unwrap_or(source.join("opts.k2d"));
idx_opts.write_to_file(options_filename)?;

Ok(())
}
21 changes: 17 additions & 4 deletions kr2r/src/bin/build_k2_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Args {
#[clap(long)]
pub chunk_dir: PathBuf,

/// chunk size 1-4(GB) [1073741824-4294967295]
/// chunk size 1-4(GB) [1073741824-4294967295] default: 1GB
#[clap(long, value_parser = clap::value_parser!(u64).range(ONEGB..U32MAXPLUS), default_value_t = ONEGB)]
pub chunk_size: u64,
}
Expand All @@ -41,9 +41,14 @@ pub fn run(args: Args, required_capacity: usize) -> Result<(), Box<dyn std::erro

let id_to_taxon_map = read_id_to_taxon_map(&args.taxo.id_to_taxon_map_filename)?;

let taxonomy_filename = args
.taxo
.taxonomy_filename
.unwrap_or(args.build.source.join("taxo.k2d"));

let taxonomy = generate_taxonomy(
&args.taxo.ncbi_taxonomy_directory,
&args.taxo.taxonomy_filename,
&taxonomy_filename,
&id_to_taxon_map,
)?;

Expand Down Expand Up @@ -93,7 +98,11 @@ pub fn run(args: Args, required_capacity: usize) -> Result<(), Box<dyn std::erro
);
}

let hash_filename = args.build.hashtable_filename.clone();
let hash_filename = args
.build
.hashtable_filename
.unwrap_or(source.join("hash.k2d"))
.clone();
let partition = chunk_files.len();
for i in 0..partition {
let mut chtm = CHTableMut::new(&hash_filename, hash_config, i, chunk_size)?;
Expand All @@ -104,8 +113,12 @@ pub fn run(args: Args, required_capacity: usize) -> Result<(), Box<dyn std::erro
// 打印运行时间
println!("build k2 db took: {:?}", duration);

let options_filename = args
.build
.options_filename
.unwrap_or(source.clone().join("opts.k2d"));
let idx_opts = IndexOptions::from_meros(meros);
idx_opts.write_to_file(args.build.options_filename)?;
idx_opts.write_to_file(options_filename)?;

Ok(())
}
Expand Down

0 comments on commit a2a6740

Please sign in to comment.