Skip to content

Commit

Permalink
Refactor module imports and fix module paths
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaccha committed Feb 4, 2024
1 parent 1972761 commit 689faa5
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 52 deletions.
12 changes: 6 additions & 6 deletions src/ext_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::{Path, PathBuf};
use tempfile::{Builder, NamedTempFile, TempDir};

use crate::edam;
use crate::run::ModuleResult;
use crate::module::ModuleResult;

const CWL_INSPECTOR_DOCKER_IMAGE: &str = "ghcr.io/tom-tan/cwl-inspector:v0.1.1";
const LABEL_KEY: &str = "label";
Expand All @@ -29,10 +29,10 @@ pub fn invoke(
// make sure that the both paths are canonicalized.
let target_file_path = target_file_path
.canonicalize()
.with_context(|| format!("Failed to canonicalize {}", target_file_path.display()))?;
.with_context(|| format!("The specified path of CWL document '{}' does not exist. Please check the path for typos and try again.", target_file_path.display()))?;
let cwl_file_path = cwl_file_path
.canonicalize()
.with_context(|| format!("Failed to canonicalize {}", cwl_file_path.display()))?;
.with_context(|| format!("The specified path of CWL document '{}' does not exist. Please check the path for typos and try again.", cwl_file_path.display()))?;

// get the EDAM_ID and LABEL from the comment lines in the CWL file.
let mut cwl_metadatas = get_metadata_fields_from_cwl_file(&cwl_file_path)?;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn invoke(

// remove the "-v" options and split the docker command on the "-v" option.
let mut parts_iter = tmp_cwl_docker_command_split.into_iter().peekable();
let cwl_docker_commandname = parts_iter.next().unwrap();
let cwl_docker_command_name = parts_iter.next().unwrap();
let mut cwl_docker_args_before_v: Vec<String> = Vec::new();
let mut cwl_docker_args_after_v: Vec<String> = Vec::new();
let mut encountered_v = false;
Expand Down Expand Up @@ -110,12 +110,12 @@ pub fn invoke(
// run the docker command created by cwl-inspector.
debug!(
"Running the docker command: '{} {} {}'",
cwl_docker_commandname,
cwl_docker_command_name,
cwl_docker_args_before_v.join(" "),
cwl_docker_args_after_v.join(" ")
);

let cwl_docker_process = std::process::Command::new(cwl_docker_commandname)
let cwl_docker_process = std::process::Command::new(cwl_docker_command_name)
.args(cwl_docker_args_before_v)
.args(cwl_docker_args_after_v)
.stdout(std::process::Stdio::piped())
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod args;
mod edam;
mod ext_tools;
mod fetch;
mod logger;
pub mod module;
mod parser;
5 changes: 0 additions & 5 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io::Write;

pub fn init_logger(verbose: bool, quiet: bool) {
let env = env_logger::Env::default().filter_or(
env_logger::DEFAULT_FILTER_ENV,
Expand All @@ -12,8 +10,5 @@ pub fn init_logger(verbose: bool, quiet: bool) {
},
);
let mut builder = env_logger::Builder::from_env(env);
if !verbose {
builder.format(|buf, record| writeln!(buf, "{}", record.args()));
}
builder.init();
}
24 changes: 4 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
mod args;
mod edam;
mod ext_tools;
mod fetch;
mod logger;
mod parser;
mod run;

use anyhow::{Context, Result};
use clap::Parser;
use log::{debug, info};
use std::fs::File;
use std::io::BufReader;

use crate::args::OutputFormat;
use crate::run::Config;

fn main() -> Result<()> {
// parse arguments and options with clap
let args = args::Args::parse();
logger::init_logger(args.verbose, args.quiet);
let args = tataki::args::Args::parse();

let config: Config = match &args.conf {
let config: tataki::module::Config = match &args.conf {
None => {
let config_str = include_str!("./tataki.conf");
serde_yaml::from_str(config_str)?
Expand All @@ -38,12 +25,9 @@ fn main() -> Result<()> {
};

if args.dry_run {
run::dry_run(config)?;
tataki::module::dry_run(config)?;
} else {
info!("tataki started");
debug!("Args: {:?}", args);
debug!("Output format: {:?}", args.get_output_format());
run::run(config, args)?;
tataki::module::run(config, args)?;
}

Ok(())
Expand Down
19 changes: 10 additions & 9 deletions src/run.rs → src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use std::path::{Path, PathBuf};
use tempfile::{NamedTempFile, TempDir};
use url::Url;

use crate::args::Args;
use crate::ext_tools;
use crate::fetch;
use crate::parser;
use crate::OutputFormat;
use crate::args::{Args, OutputFormat};

// Struct to store the result of Parser invocation and ExtTools invocation.
#[derive(Debug)]
Expand Down Expand Up @@ -117,7 +113,12 @@ pub struct Config {
}

pub fn run(config: Config, args: Args) -> Result<()> {
let temp_dir = fetch::create_temporary_dir(&args.cache_dir)?;
crate::logger::init_logger(args.verbose, args.quiet);
info!("tataki started");
debug!("Args: {:?}", args);
debug!("Output format: {:?}", args.get_output_format());

let temp_dir = crate::fetch::create_temporary_dir(&args.cache_dir)?;
info!("Created temporary directory: {}", temp_dir.path().display());

let mut module_results: Vec<ModuleResult> = Vec::new();
Expand Down Expand Up @@ -193,7 +194,7 @@ fn run_modules(
) -> Result<ModuleResult> {
// create an input file for CWL modules if there is any CWL module in the config file.
let cwl_input_file_path: Option<NamedTempFile> = if cwl_module_exists(config)? {
Some(ext_tools::make_cwl_input_file(
Some(crate::ext_tools::make_cwl_input_file(
target_file_path.clone(),
temp_dir,
)?)
Expand All @@ -212,8 +213,8 @@ fn run_modules(
.unwrap_or("");

let result = match module_extension {
"" => parser::invoke(module, &target_file_path),
"cwl" => ext_tools::invoke(
"" => crate::parser::invoke(module, &target_file_path),
"cwl" => crate::ext_tools::invoke(
module_path,
&target_file_path,
cwl_input_file_path.as_ref().unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use anyhow::{bail, Result};
use log::info;
use std::path::Path;

use crate::run::ModuleResult;
use crate::module::ModuleResult;

pub trait Parser {
/// Determine if the provided file is in a format that this parser can interpret.
Expand Down
2 changes: 1 addition & 1 deletion src/parser/bam.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Bam;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/bcf.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Bcf;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/bed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Bed;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/cram.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Cram;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/empty.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fs;
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Empty;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/fasta.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Fasta;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/fastq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Fastq;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/gff3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Gff3;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/gtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Gtf;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/sam.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Sam;

Expand Down
2 changes: 1 addition & 1 deletion src/parser/vcf.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use crate::module::ModuleResult;
use crate::parser::Parser;
use crate::run::ModuleResult;

pub struct Vcf;

Expand Down

0 comments on commit 689faa5

Please sign in to comment.