Skip to content

Commit 88945f3

Browse files
committed
work on using writer
1 parent 4e06507 commit 88945f3

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/main.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
extern crate bedder;
22
use bedder::column::{Column, ColumnReporter};
3+
use bedder::hts_format::Format;
34
use bedder::report_options::{IntersectionMode, IntersectionPart, OverlapAmount, ReportOptions};
5+
use bedder::writer::{InputHeader, Writer};
46
use clap::Parser;
57
use pyo3::prelude::*;
68
use std::env;
@@ -92,11 +94,20 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
9294

9395
let ii = bedder::intersection::IntersectionIterator::new(aiter, b_iters, &chrom_order)?;
9496

97+
/*
9598
let mut output: Box<dyn Write> = if args.output_path.to_str().unwrap() == "-" {
9699
Box::new(BufWriter::new(std::io::stdout().lock()))
97100
} else {
98101
Box::new(BufWriter::new(File::create(&args.output_path)?))
99102
};
103+
*/
104+
105+
let mut output = Writer::init(
106+
args.output_path.to_str().unwrap(),
107+
Some(Format::Bed),
108+
None,
109+
InputHeader::None,
110+
)?;
100111

101112
// Parse columns
102113
let columns: Vec<Column> = args
@@ -106,6 +117,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
106117
.collect::<Result<Vec<_>, _>>()?;
107118

108119
// Print header
120+
/*
109121
writeln!(
110122
output,
111123
"#{}",
@@ -115,8 +127,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
115127
.collect::<Vec<_>>()
116128
.join("\t")
117129
)?;
130+
*/
118131

119-
// TODO: start here. add the appropriate argugments above and get a report options struct here.
120132
let report_options = Arc::new(
121133
ReportOptions::builder()
122134
.a_mode(args.intersection_mode)
@@ -128,7 +140,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
128140
// Use Python for columns that need it
129141
Python::with_gil(|py| {
130142
// Initialize Python expressions in columns if needed
131-
let py_columns: Vec<Column> = columns
143+
let py_columns: Vec<Column<'_>> = columns
132144
.into_iter()
133145
.map(|mut col| {
134146
if let Some(bedder::column::ValueParser::PythonExpression(expr)) = &col.value_parser
@@ -140,13 +152,16 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
140152
col
141153
})
142154
.collect();
143-
log::info!("py_columns: {:?}", py_columns);
155+
//log::info!("py_columns: {:?}", py_columns);
144156
bedder::py::initialize_python(py).expect("Failed to initialize Python environment");
145157

146158
// Process intersections with columns
147159
for intersection in ii {
148160
// TODO: brent start here. need to auto call report when needed.
149-
let intersection = intersection.expect("error getting intersection");
161+
let mut intersection = intersection.expect("error getting intersection");
162+
163+
output.write(&mut intersection, report_options.clone(), &py_columns)?;
164+
/*
150165
let values: Vec<String> = py_columns
151166
.iter()
152167
.map(
@@ -167,8 +182,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
167182
}
168183
}
169184
}
185+
*/
170186
}
171-
});
172-
173-
Ok(())
187+
Ok::<(), Box<dyn std::error::Error>>(())
188+
})
174189
}

src/writer.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rust_htslib::bam;
77
use rust_htslib::bcf::{self, header::HeaderView};
88
use rust_htslib::htslib as hts;
99
use simplebed::{self, BedValue};
10+
use std::fmt;
1011
use std::mem;
1112
use std::rc::Rc;
1213
use std::result::Result;
@@ -20,6 +21,27 @@ pub enum FormatConversionError {
2021
IoError(std::io::Error),
2122
}
2223

24+
impl fmt::Display for FormatConversionError {
25+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26+
match self {
27+
FormatConversionError::HtslibError(msg) => write!(f, "HTSlib error: {}", msg),
28+
FormatConversionError::UnsupportedFormat(format) => {
29+
write!(f, "Unsupported format: {:?}", format)
30+
}
31+
FormatConversionError::IoError(e) => write!(f, "IO error: {}", e),
32+
}
33+
}
34+
}
35+
36+
impl std::error::Error for FormatConversionError {
37+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
38+
match self {
39+
FormatConversionError::IoError(e) => Some(e),
40+
_ => None,
41+
}
42+
}
43+
}
44+
2345
impl From<std::io::Error> for FormatConversionError {
2446
fn from(error: std::io::Error) -> Self {
2547
FormatConversionError::IoError(error)
@@ -188,11 +210,11 @@ impl Writer {
188210
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))
189211
}
190212

191-
fn update(
213+
fn update<T: ColumnReporter>(
192214
format: Format,
193215
intersections: &mut Intersections,
194216
report_options: Arc<ReportOptions>,
195-
crs: &[Box<dyn ColumnReporter>],
217+
crs: &[T],
196218
) -> Result<(), std::io::Error> {
197219
match format {
198220
Format::Vcf => {
@@ -258,11 +280,11 @@ impl Writer {
258280
Ok(())
259281
}
260282

261-
pub fn write(
283+
pub fn write<T: ColumnReporter>(
262284
&mut self,
263285
intersections: &mut Intersections,
264286
report_options: Arc<ReportOptions>,
265-
crs: &[Box<dyn ColumnReporter>],
287+
crs: &[T],
266288
) -> Result<(), std::io::Error> {
267289
match self.format {
268290
Format::Vcf => {

0 commit comments

Comments
 (0)