Skip to content

Commit

Permalink
Change BigBedRead::open_file to take &str, and cleanup autosql
Browse files Browse the repository at this point in the history
  • Loading branch information
jackh726 committed Aug 15, 2023
1 parent 144993e commit c0df345
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 80 deletions.
20 changes: 20 additions & 0 deletions src/bbi/bbiread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ pub enum BBIReadError {
IoError(#[from] io::Error),
}

#[derive(Error, Debug)]
pub enum ZoomIntervalError {
#[error("The passed reduction level was not found")]
ReductionLevelNotFound,
#[error("{}", .0)]
BBIReadError(BBIReadError),
}

impl From<ChromIdNotFound> for ZoomIntervalError {
fn from(e: ChromIdNotFound) -> Self {
ZoomIntervalError::BBIReadError(BBIReadError::InvalidChromosome(e.0))
}
}

impl From<CirTreeSearchError> for ZoomIntervalError {
fn from(e: CirTreeSearchError) -> Self {
ZoomIntervalError::BBIReadError(BBIReadError::CirTreeSearchError(e))
}
}

pub trait BBIRead {
type Read: SeekableRead;

Expand Down
28 changes: 4 additions & 24 deletions src/bbi/bigbedread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::bbiread::{
ChromAndSize, ZoomIntervalIter,
};
use crate::utils::reopen::{Reopen, ReopenableFile, SeekableRead};
use crate::{ChromIdNotFound, CirTreeSearchError};
use crate::ZoomIntervalError;

struct IntervalIter<I, R, B>
where
Expand Down Expand Up @@ -136,32 +136,12 @@ impl<R: SeekableRead> BBIRead for BigBedRead<R> {
}
}

#[derive(Error, Debug)]
pub enum ZoomIntervalError {
#[error("The passed reduction level was not found")]
ReductionLevelNotFound,
#[error("{}", .0)]
BBIReadError(BBIReadError),
}

impl From<ChromIdNotFound> for ZoomIntervalError {
fn from(e: ChromIdNotFound) -> Self {
ZoomIntervalError::BBIReadError(BBIReadError::InvalidChromosome(e.0))
}
}

impl From<CirTreeSearchError> for ZoomIntervalError {
fn from(e: CirTreeSearchError) -> Self {
ZoomIntervalError::BBIReadError(BBIReadError::CirTreeSearchError(e))
}
}

impl BigBedRead<ReopenableFile> {
/// Opens a new `BigBedRead` from a given path as a file.
pub fn open_file(path: String) -> Result<Self, BigBedReadAttachError> {
pub fn open_file(path: &str) -> Result<Self, BigBedReadAttachError> {
let reopen = ReopenableFile {
path: path.clone(),
file: File::open(&path)?,
path: path.to_string(),
file: File::open(path)?,
};
let b = BigBedRead::open(reopen);
if b.is_err() {
Expand Down
22 changes: 1 addition & 21 deletions src/bbi/bigwigread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use crate::bbiread::{
ChromAndSize, ZoomIntervalIter,
};
use crate::utils::reopen::{Reopen, ReopenableFile, SeekableRead};
use crate::{ChromIdNotFound, CirTreeSearchError};
use crate::ZoomIntervalError;

struct IntervalIter<I, R, B>
where
Expand Down Expand Up @@ -178,26 +178,6 @@ impl<R: SeekableRead> BBIRead for BigWigRead<R> {
}
}

#[derive(Error, Debug)]
pub enum ZoomIntervalError {
#[error("The passed reduction level was not found")]
ReductionLevelNotFound,
#[error("{}", .0)]
BBIReadError(BBIReadError),
}

impl From<ChromIdNotFound> for ZoomIntervalError {
fn from(e: ChromIdNotFound) -> Self {
ZoomIntervalError::BBIReadError(BBIReadError::InvalidChromosome(e.0))
}
}

impl From<CirTreeSearchError> for ZoomIntervalError {
fn from(e: CirTreeSearchError) -> Self {
ZoomIntervalError::BBIReadError(BBIReadError::CirTreeSearchError(e))
}
}

impl BigWigRead<ReopenableFile> {
/// Opens a new `BigWigRead` from a given path as a file.
pub fn open_file(path: &str) -> Result<Self, BigWigReadAttachError> {
Expand Down
82 changes: 52 additions & 30 deletions src/bed/autosql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ table bed
}

// Defined by https://github.com/ucscGenomeBrowser/kent/blob/c26640b68ba8ad219e7d79c3f8251ea20f9f57e0/src/hg/autoSql/autoSql.doc
mod parse {
pub mod parse {
mod parser {
pub(super) struct Parser<'a> {
pub(super) data: &'a str,
Expand Down Expand Up @@ -321,7 +321,7 @@ mod parse {
}

#[derive(Debug)]
enum ParseError {
pub enum ParseError {
InvalidDeclareType(String),
InvalidDeclareName(String),
InvalidDeclareBrackets(String),
Expand All @@ -332,27 +332,36 @@ mod parse {
}

#[derive(Copy, Clone, Debug)]
enum DeclarationType {
pub enum DeclarationType {
Simple,
Object,
Table,
}

#[derive(Clone, Debug)]
enum IndexType {
pub enum IndexType {
Primary,
Index(Option<String>),
Unique,
}

#[derive(Clone, Debug)]
struct DeclareName(String, Option<IndexType>, bool);
pub struct DeclareName {
pub name: String,
pub index_type: Option<IndexType>,
pub auto: bool,
}

#[derive(Clone, Debug)]
struct Declaration(DeclarationType, DeclareName, String, Vec<Field>);
pub struct Declaration {
pub declaration_type: DeclarationType,
pub name: DeclareName,
pub comment: String,
pub fields: Vec<Field>,
}

#[derive(Clone, Debug)]
enum FieldType {
pub enum FieldType {
Int,
Uint,
Short,
Expand Down Expand Up @@ -462,18 +471,24 @@ mod parse {
}

#[derive(Clone, Debug)]
struct Field(
FieldType,
Option<String>,
String,
Option<IndexType>,
bool,
String,
);
pub struct Field {
pub field_type: FieldType,
pub field_size: Option<String>,
pub name: String,
pub index_type: Option<IndexType>,
pub auto: bool,
pub comment: String,
}

fn parse_declaration_list(data: &str) -> Result<Vec<Declaration>, ParseError> {
pub fn parse_autosql(data: &str) -> Result<Vec<Declaration>, ParseError> {
let mut parser = parser::Parser::of(data);

parse_declaration_list(&mut parser)
}

fn parse_declaration_list(
parser: &mut parser::Parser<'_>,
) -> Result<Vec<Declaration>, ParseError> {
let mut declarations = vec![];

let mut i = 0;
Expand All @@ -482,7 +497,7 @@ mod parse {
break;
}
i += 1;
let dec = parse_declaration(&mut parser)?;
let dec = parse_declaration(parser)?;
match dec {
Some(d) => declarations.push(d),
None => break,
Expand Down Expand Up @@ -526,12 +541,12 @@ mod parse {
));
}

Ok(Some(Declaration(
Ok(Some(Declaration {
declaration_type,
declare_name,
name: declare_name,
comment,
fields,
)))
}))
}

fn parse_declare_name(parser: &mut parser::Parser<'_>) -> Result<DeclareName, ParseError> {
Expand Down Expand Up @@ -581,7 +596,11 @@ mod parse {
} else {
false
};
Ok(DeclareName(declare_name, index_type, auto))
Ok(DeclareName {
name: declare_name,
index_type,
auto,
})
}

fn parse_field_list(parser: &mut parser::Parser<'_>) -> Result<Vec<Field>, ParseError> {
Expand Down Expand Up @@ -656,9 +675,14 @@ mod parse {

let comment = parser.eat_quoted_string().to_string();

fields.push(Field(
field_type, field_size, field_name, index_type, auto, comment,
));
fields.push(Field {
field_type,
field_size,
name: field_name,
index_type,
auto,
comment,
});

if parser.peek_one() == ")" {
break;
Expand All @@ -668,11 +692,9 @@ mod parse {
}

mod test {
use crate::bed::autosql::BED3;

#[test]
fn test_bed3() {
super::parse_declaration_list(BED3).unwrap();
super::parse_autosql(super::super::BED3).unwrap();
}

#[test]
Expand Down Expand Up @@ -731,7 +753,7 @@ mod parse {
)
"#;

super::parse_declaration_list(main_test).unwrap();
super::parse_autosql(main_test).unwrap();
}
#[test]
fn test_hardtest() {
Expand Down Expand Up @@ -766,7 +788,7 @@ mod parse {
)
"#;

super::parse_declaration_list(hard_test).unwrap();
super::parse_autosql(hard_test).unwrap();
}

#[test]
Expand All @@ -792,7 +814,7 @@ mod parse {
)
"#;

super::parse_declaration_list(index_test).unwrap();
super::parse_autosql(index_test).unwrap();
}
}
}
2 changes: 1 addition & 1 deletion src/bin/bigbedtobed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn main() -> Result<(), Box<dyn Error>> {

let nthreads = *matches.get_one::<usize>("nthreads").unwrap();

let bigbed = BigBedRead::open_file(bigbedpath)?;
let bigbed = BigBedRead::open_file(&bigbedpath)?;
let bed = File::create(bedpath)?;

write_bed(bigbed, bed, nthreads)?;
Expand Down
6 changes: 3 additions & 3 deletions src/bin/bigtools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn intersect<R: SeekableRead + 'static>(
mut b: BigBedRead<R>,
_options: IntersectOptions,
) -> io::Result<()> {
let bedin = File::open(apath)?;
let bedin = File::open(&apath)?;
let mut bedstream = StreamingLineReader::new(BufReader::with_capacity(64 * 1024, bedin));

let stdout = io::stdout();
Expand Down Expand Up @@ -95,7 +95,7 @@ fn intersect<R: SeekableRead + 'static>(
fn chromintersect(apath: String, bpath: String, outpath: String) -> io::Result<()> {
let chroms = match BigWigRead::open_file(&bpath) {
Ok(bigwig) => bigwig.info.chrom_info,
Err(BigWigReadAttachError::NotABigWig) => match BigBedRead::open_file(bpath) {
Err(BigWigReadAttachError::NotABigWig) => match BigBedRead::open_file(&bpath) {
Ok(bigbed) => bigbed.info.chrom_info,
Err(BigBedReadAttachError::NotABigBed) => {
return Err(io::Error::new(
Expand Down Expand Up @@ -201,7 +201,7 @@ fn main() -> Result<(), BigBedReadAttachError> {
let apath = matches.get_one::<String>("a").unwrap().to_owned();
let bpath = matches.get_one::<String>("b").unwrap().to_owned();

let b = BigBedRead::open_file(bpath)?;
let b = BigBedRead::open_file(&bpath)?;

intersect(apath, b, IntersectOptions {})?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn Error>> {

let bigwigpath = matches.get_one::<String>("bigbed").unwrap().to_owned();

let mut bigwig = BigBedRead::open_file(bigwigpath)?;
let mut bigwig = BigBedRead::open_file(&bigwigpath)?;
println!("info: {:?}", bigwig.info);
println!("Header: {:?}", bigwig.info.header);

Expand Down

0 comments on commit c0df345

Please sign in to comment.