Skip to content

Commit

Permalink
work on #9
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Apr 1, 2024
1 parent 168eede commit 9e6f440
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/intersections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl Intersections {

#[inline]
fn calculate_overlap(&self, interval_a: Arc<Position>, interval_b: Arc<Position>) -> u64 {
// TODO: we don't handle the case where there is no overlap. possible underflow. But we should
// NOTE!: we don't handle the case where there is no overlap. possible underflow. But we should
// only get overlapping intervals here.
interval_a.stop().min(interval_b.stop()) - interval_a.start().max(interval_b.start())
}
Expand Down
21 changes: 17 additions & 4 deletions src/sniff.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use flate2::read::GzDecoder;
use noodles::bam::reader;
use std::io::{BufRead, Read, Seek};
use std::path::Path;

Expand All @@ -8,7 +9,7 @@ use crate::position::PositionedIterator;
use noodles::bgzf;

/// File formats supported by this file detector.
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub enum FileFormat {
VCF,
BCF,
Expand All @@ -34,7 +35,7 @@ where
P: AsRef<Path>,
{
let file = std::fs::File::open(&path)?;

open_reader(file, path)
}

Expand All @@ -51,8 +52,20 @@ where
format,
compression
);
/*
*/

return open_format(reader, path, format, compression);
}

pub fn open_format<R, P>(
reader: R,
path: P,
format: FileFormat,
compression: Compression,
) -> std::io::Result<Box<dyn PositionedIterator>>
where
R: BufRead + Seek + 'static,
P: AsRef<Path>,
{
match format {
FileFormat::VCF | FileFormat::BCF => {
// get &str from path
Expand Down
71 changes: 49 additions & 22 deletions src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use crate::report::Report;
use crate::sniff::Compression;
use crate::sniff::FileFormat;
use crate::report::Report;
use std::string::String;

pub struct Writer {
in_fmt: FileFormat,
out_fmt: FileFormat,
compression: Compression,
}

pub enum Type {
Integer,
Float,
Expand All @@ -17,6 +11,7 @@ pub enum Type {
Flag,
}

/// The number of Values to expect (similar to Number attribute in VCF INFO/FMT fields)
pub enum Number {
Not,
One,
Expand All @@ -25,20 +20,36 @@ pub enum Number {
Dot,
}

pub enum Value {
Int(i32),
Float(f32),
String(String),
Flag(bool),
VecInt(Vec<i32>),
VecFloat(Vec<f32>),
VecString(Vec<String>),
}

pub trait ColumnReporter {
/// report the name, e.g. `count` for the INFO field of the VCF
fn name(&self) -> String;
/// report the type, for the INFO field of the VCF
fn ftype(&self) -> Type; // Type is some enum from noodles or here that limits to relevant types
fn description(&self) -> String;
fn number(&self) -> Number;
/// report the name, e.g. `count` for the INFO field of the VCF
fn name(&self) -> String;
/// report the type, for the INFO field of the VCF
fn ftype(&self) -> Type; // Type is some enum from noodles or here that limits to relevant types
fn description(&self) -> String;
fn number(&self) -> Number;

fn value(&self, r: &Report) -> Value // Value probably something from noodles that encapsulates Float/Int/Vec<Float>/String/...
fn value(&self, r: &Report) -> Value; // Value probably something from noodles that encapsulates Float/Int/Vec<Float>/String/...
}

#[derive(Debug)]
pub enum FormatConversionError {
IncompatibleFormats(FileFormat, FileFormat, String),
}
pub struct Writer {
in_fmt: FileFormat,
out_fmt: FileFormat,
compression: Compression,
}

impl Writer {
pub fn init(
Expand All @@ -50,18 +61,19 @@ impl Writer {
Some(f) => f,
// TODO: may want, e.g. BAM/CRAM to default to SAM
// and BCF to default to VCF.
None => in_fmt,
None => in_fmt.clone(),
};

// if out_fmt is the same as in_fmt, then we can just pass through
if in_fmt == out_fmt {
return Ok(Self {
in_fmt,
out_fmt,
compression,
});
}
//if in_fmt == out_fmt {
return Ok(Self {
in_fmt: in_fmt.clone(),
out_fmt,
compression,
});
//}

/*
// if out_fmt is different from in_fmt, then we need to convert
match (in_fmt, out_fmt) {
(FileFormat::VCF, FileFormat::BED) => {
Expand All @@ -76,5 +88,20 @@ impl Writer {
String::from("No conversion yet available. Please report"),
)),
}
*/
}

pub fn write(&self, report: &Report, crs: Vec<Box<dyn ColumnReporter>>) {
// match self.out_fmt {
// FileFormat::VCF => {
// // write vcf
// }
// FileFormat::BED => {
// // write bed
// }
// _ => {
// // write something else
// }
// }
}
}

0 comments on commit 9e6f440

Please sign in to comment.