Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arrayify"
version = "0.1.2"
version = "0.2.0"
edition = "2024"

[dependencies]
Expand Down
18 changes: 13 additions & 5 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ use clap::{Arg, ArgMatches, Command as ClapCommand};

pub fn parse_args() -> ArgMatches {
ClapCommand::new("arrayify")
.version("0.1.2")
.version("0.2.0")
.author("Sam Dougan")
.about("Submits and checks bsub job arrays from a CSV file")
.about("Submits and checks bsub job arrays from a CSV file or directory")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
ClapCommand::new("sub")
.about("Submit job array from CSV")
.about("Submit job array from CSV or directory")
.arg(Arg::new("csv")
.short('s')
.long("csv")
.value_name("CSV_FILE")
.help("Path to the CSV file")
.required(true))
.conflicts_with("dir")
.required_unless_present("dir"))
.arg(Arg::new("dir")
.short('d')
.long("dir")
.value_name("DIRECTORY")
.help("Path to the input directory")
.conflicts_with("csv")
.required_unless_present("csv"))
.arg(Arg::new("command")
.short('c')
.long("command")
Expand Down Expand Up @@ -44,7 +52,7 @@ pub fn parse_args() -> ArgMatches {
.short('b')
.long("batch")
.value_name("BATCH_SIZE")
.help("Batch size of actively running jobs normally signified by %N default is 20% of the array")
.help("Batch size of actively running jobs normally signified by %N (default: 20% of the array)")
.default_value("auto")))
.subcommand(
ClapCommand::new("check")
Expand Down
100 changes: 100 additions & 0 deletions src/jobs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use csv::ReaderBuilder;
use std::collections::HashMap;
use std::fs::{self};
use std::io::{self};
use std::path::{Path, PathBuf};

pub fn read_jobs_from_csv(csv_file: &str, command_template: &str) -> io::Result<Vec<String>> {
let mut rdr = ReaderBuilder::new()
.has_headers(true)
.from_path(csv_file)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;

let headers = rdr
.headers()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
.clone();
let mut jobs = Vec::new();

for result in rdr.records() {
let record = result.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
let mut job_command = command_template.to_string();

for (i, header) in headers.iter().enumerate() {
let placeholder = format!("{{{}}}", header);
if let Some(value) = record.get(i) {
job_command = job_command.replace(&placeholder, value);
}
}
jobs.push(job_command);
}

Ok(jobs)
}

pub fn read_jobs_from_dir(
dir_path: &str,
command_template: &str,
) -> io::Result<Vec<std::string::String>> {
let dir = Path::new(dir_path);
if !dir.is_dir() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Provided path is not a directory",
));
}

// Collect all files in the directory
let mut file_map: HashMap<String, (Option<PathBuf>, Option<PathBuf>)> = HashMap::new();

for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
if let Some(file_name) = path.file_name().and_then(|f| f.to_str()) {
if file_name.contains("_1") {
let id = file_name
.split("_1")
.next()
.unwrap_or(file_name)
.to_string();
file_map.entry(id).or_insert((None, None)).0 = Some(path.clone());
} else if file_name.contains("_2") {
let id = file_name
.split("_2")
.next()
.unwrap_or(file_name)
.to_string();
file_map.entry(id).or_insert((None, None)).1 = Some(path.clone());
}
}
}
}

// Validate and collect paired files
let mut jobs = Vec::new();
for (id, (r1, r2)) in file_map {
if let (Some(r1_path), Some(r2_path)) = (r1, r2) {
// Replace placeholders in the command template
let job_command = command_template
.replace("{ID}", &id)
.replace("{R1}", r1_path.to_str().unwrap_or_default())
.replace("{R2}", r2_path.to_str().unwrap_or_default());
jobs.push(job_command);
} else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("Missing R1 or R2 for ID: {}", id),
));
}
}

if jobs.is_empty() {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"No valid file pairs found in the directory",
));
}

Ok(jobs)
}
Loading