Skip to content

Commit

Permalink
Only try to parse autosql if input is a file
Browse files Browse the repository at this point in the history
  • Loading branch information
jackh726 committed Aug 4, 2024
1 parent f4e3378 commit c7d6320
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion bigtools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bincode = { version = "1.3", optional = true }
attohttpc = { version = "0.25", optional = true, default_features = false, features = ["tls-rustls-native-roots"] }
libdeflater = "0.13"
thiserror = "1"
anyhow = { version = "1", optional = true }
ryu = { version = "1.0", optional = true }
ufmt = { version = "0.2", features = ["std"], optional = true }
bytes = { version = "1.4.0", optional = true }
Expand Down Expand Up @@ -80,6 +81,6 @@ required-features = ["cli"]
[features]
default = ["remote", "read", "write", "cli"]
remote = ["attohttpc", "tempfile"]
cli = ["clap", "ryu", "ufmt", "read", "write"]
cli = ["anyhow", "clap", "ryu", "ufmt", "read", "write"]
read = ["bytes", "itertools"]
write = ["crossbeam-channel", "tempfile", "futures", "serde", "itertools", "bincode"]
5 changes: 4 additions & 1 deletion bigtools/src/bin/bigtools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ fn main() -> Result<(), Box<dyn Error>> {
args: ChromIntersectArgs { a, b, out },
} => chromintersect(a, b, out),
SubCommands::BedGraphToBigWig { args } => bedgraphtobigwig(args),
SubCommands::BedToBigBed { args } => bedtobigbed(args),
SubCommands::BedToBigBed { args } => {
bedtobigbed(args)?;
Ok(())
}
SubCommands::BigBedInfo { args } => bigbedinfo(args),
SubCommands::BigBedToBed { args } => bigbedtobed(args),
SubCommands::BigWigAverageOverBed { args } => {
Expand Down
56 changes: 34 additions & 22 deletions bigtools/src/utils/cli/bedtobigbed.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;

use anyhow::Context;
use clap::Parser;
use tokio::runtime;

Expand Down Expand Up @@ -51,7 +51,7 @@ pub struct BedToBigBedArgs {
pub write_args: BBIWriteArgs,
}

pub fn bedtobigbed(args: BedToBigBedArgs) -> Result<(), Box<dyn Error>> {
pub fn bedtobigbed(args: BedToBigBedArgs) -> anyhow::Result<()> {
let bedpath = args.bed;
let chrom_map = args.chromsizes;
let bigwigpath = args.output;
Expand All @@ -72,7 +72,9 @@ pub fn bedtobigbed(args: BedToBigBedArgs) -> Result<(), Box<dyn Error>> {
}
};

let chrom_map: HashMap<String, u32> = BufReader::new(File::open(chrom_map)?)
let chrom_map = File::open(&chrom_map)
.with_context(|| format!("Failed to open chrom sizes file `{}`", &chrom_map))?;
let chrom_map: HashMap<String, u32> = BufReader::new(chrom_map)
.lines()
.filter(|l| match l {
Ok(s) => !s.is_empty(),
Expand All @@ -88,7 +90,8 @@ pub fn bedtobigbed(args: BedToBigBedArgs) -> Result<(), Box<dyn Error>> {
})
.collect();

let mut outb = BigBedWrite::create_file(bigwigpath, chrom_map)?;
let mut outb = BigBedWrite::create_file(bigwigpath, chrom_map)
.with_context(|| format!("Failed to create bigBed file."))?;
outb.options.max_zooms = args.write_args.nzooms;
outb.options.compress = !args.write_args.uncompressed;
outb.options.input_sort_type = input_sort_type;
Expand All @@ -103,23 +106,26 @@ pub fn bedtobigbed(args: BedToBigBedArgs) -> Result<(), Box<dyn Error>> {
.unwrap()
};

let autosql = match args.autosql.as_ref() {
None => {
let infile = File::open(&bedpath)?;
let mut vals_iter = BedFileStream::from_bed_file(infile);
crate::bed::autosql::bed_autosql(&vals_iter.next().unwrap().unwrap().1.rest)
}
Some(file) => std::fs::read_to_string(file)?,
};
outb.autosql = Some(autosql);

let allow_out_of_order_chroms = !matches!(outb.options.input_sort_type, InputSortType::ALL);
if bedpath == "-" || bedpath == "stdin" {
let stdin = std::io::stdin().lock();
let data = BedParserStreamingIterator::from_bed_file(stdin, allow_out_of_order_chroms);
outb.write(data, runtime)?;
outb.write(data, runtime)
.with_context(|| format!("Failed to write bigBed."))?;
} else {
let infile = File::open(&bedpath)?;
let autosql = match args.autosql.as_ref() {
None => {
let infile = File::open(&bedpath)
.with_context(|| format!("Failed to open bed file `{}`", &bedpath))?;
let mut vals_iter = BedFileStream::from_bed_file(infile);
crate::bed::autosql::bed_autosql(&vals_iter.next().unwrap().unwrap().1.rest)
}
Some(file) => std::fs::read_to_string(file)?,
};
outb.autosql = Some(autosql);

let infile = File::open(&bedpath)
.with_context(|| format!("Failed to open bed file `{}`.", &bedpath))?;
let (parallel, parallel_required) = match (nthreads, args.parallel.as_ref()) {
(1, _) | (_, "no") => (false, false),
(_, "auto") => (infile.metadata()?.len() >= 200_000_000, false),
Expand All @@ -135,7 +141,8 @@ pub fn bedtobigbed(args: BedToBigBedArgs) -> Result<(), Box<dyn Error>> {
let chrom_indices = match parallel {
false => None,
true => {
let index = index_chroms(infile)?;
let index = index_chroms(infile)
.with_context(|| format!("Failed to index chromosomes."))?;
match (index, parallel_required) {
(Some(index), _) => Some(index),
(None, true) => {
Expand All @@ -156,7 +163,8 @@ pub fn bedtobigbed(args: BedToBigBedArgs) -> Result<(), Box<dyn Error>> {
PathBuf::from(bedpath),
parse_bed,
);
outb.write(data, runtime)?;
outb.write(data, runtime)
.with_context(|| format!("Failed to write bigBed."))?;
} else {
outb.write_multipass(
|| {
Expand All @@ -170,14 +178,17 @@ pub fn bedtobigbed(args: BedToBigBedArgs) -> Result<(), Box<dyn Error>> {
Ok(data)
},
runtime,
)?;
)
.with_context(|| format!("Failed to write bigBed."))?;
}
} else {
let infile = File::open(&bedpath)?;
if args.single_pass {
let infile = File::open(&bedpath)
.with_context(|| format!("Failed to open bed file `{}`.", &bedpath))?;
let data =
BedParserStreamingIterator::from_bed_file(infile, allow_out_of_order_chroms);
outb.write(data, runtime)?;
outb.write(data, runtime)
.with_context(|| format!("Failed to write bigBed."))?;
} else {
outb.write_multipass(
|| {
Expand All @@ -190,7 +201,8 @@ pub fn bedtobigbed(args: BedToBigBedArgs) -> Result<(), Box<dyn Error>> {
Ok(data)
},
runtime,
)?;
)
.with_context(|| format!("Failed to write bigBed."))?;
}
}
};
Expand Down

0 comments on commit c7d6320

Please sign in to comment.