#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
use clap::{Parser, Subcommand};
use mlua::Lua;
use rust_htslib::bcf::Read;
use vcfexpress::{variant::HeaderMap, vcfexpress::VCFExpress};
#[derive(Parser)]
#[command(version, about, author)]
#[command(arg_required_else_help(true))]
#[command(propagate_version = true)]
#[command(help_template = "
{name} {version}
{author-with-newline}{about-with-newline}
{usage-heading} {usage}
{all-args}{after-help}
")]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
pub enum Commands {
#[command(arg_required_else_help(true))]
#[command(help_template = "
{name} {version}
{about-with-newline}
{usage-heading} {usage}
{all-args}{after-help}
")]
Filter {
path: String,
#[arg(short, long)]
expression: Vec<String>,
#[arg(short = 's', long)]
set_expression: Vec<String>,
#[arg(short, long)]
template: Option<String>,
#[arg(short = 'p', long)]
lua_prelude: Vec<String>,
#[arg(short, long)]
output: Option<String>,
#[arg(short = 'b', long)]
sandbox: bool,
},
}
fn filter_main(
path: String,
expressions: Vec<String>,
set_expression: Vec<String>,
template: Option<String>,
lua_prelude: Vec<String>,
output: Option<String>,
sandbox: bool,
) -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let lua = Lua::new();
let mut vcfexpr = VCFExpress::new(
&lua,
path,
expressions,
set_expression,
template,
lua_prelude,
output,
sandbox,
)?;
let mut reader = vcfexpr.reader();
let mut writer = vcfexpr.writer();
let header_map = HeaderMap::new();
for record in reader.records() {
let mut record = record?;
writer.translate(&mut record);
let mut sob = vcfexpr.evaluate(record, header_map.clone())?;
writer.write(&mut sob)?;
}
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Cli::parse();
match args.command {
Some(Commands::Filter {
path,
expression,
set_expression,
template,
lua_prelude,
output,
sandbox,
}) => {
filter_main(
path,
expression,
set_expression,
template,
lua_prelude,
output,
sandbox,
)?;
}
None => {
println!("No command provided");
}
}
Ok(())
}