Skip to content

Commit

Permalink
use xcf
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Sep 22, 2023
1 parent ed1c7c4 commit 90247ce
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ env_logger = "0.10.0"
log = "0.4.19"
linear-map = "1.2.0"
hashbrown = "0.14.0"
xvcf = { version = "0.1.1", git = "https://github.com/brentp/xvcf-rs" }
xvcf = { version = "0.1.2", git = "https://github.com/brentp/xvcf-rs" }

[features]
default = ["bed", "vcf", "bcf", "csi", "core", "bam", "sam", "bgzf"]
Expand Down
2 changes: 2 additions & 0 deletions src/bedder_bed.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::useless_conversion)] // these are needed to support e.g. smartstring

use crate::position::{Field, FieldError, Position, Positioned, Value};
use crate::string::String;
pub use bed::record::Record;
Expand Down
15 changes: 10 additions & 5 deletions src/bedder_vcf.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![allow(clippy::useless_conversion)] // these are needed to support e.g. smartstring
use crate::position::{Field, FieldError, Position, Positioned, Value};
use crate::string::String;
use noodles::bcf;
use noodles::vcf::{self, record::Chromosome};
use std::io::{self, BufRead, Read};
use std::io::{self, Read};
use std::result;
use vcf::record::info::field;
use vcf::record::QualityScore;
Expand All @@ -12,31 +12,36 @@ pub use xvcf;
pub struct BedderVCF<R> {
reader: xvcf::Reader<R>,
record_number: u64,
header: vcf::Header,
}

impl<R> BedderVCF<R>
where
R: Read + 'static,
{
pub fn new(r: xvcf::Reader<R>) -> io::Result<BedderVCF<R>> {
let h = r.header().clone();
let v = BedderVCF {
reader: r,
record_number: 0,
header: h,
};
Ok(v)
}
}

pub fn from_reader<R: Read>(r: Box<R>) -> io::Result<BedderVCF<R>> {
/*
pub fn _from_reader<R: Read>(r: Box<R>) -> io::Result<BedderVCF<R>> {
let reader = xvcf::Reader::from_reader(r, None)?;
BedderVCF::new(reader)
}
*/

fn match_info_value(info: &vcf::record::Info, name: &str) -> result::Result<Value, FieldError> {
//let info = record.info();
let key: vcf::record::info::field::Key = name
.parse()
.map_err(|_| FieldError::InvalidFieldName(String::from(name.clone())))?;
.map_err(|_| FieldError::InvalidFieldName(String::from(name)))?;

match info.get(&key) {
Some(value) => match value {
Expand Down Expand Up @@ -142,7 +147,7 @@ where
) -> Option<std::result::Result<Position, std::io::Error>> {
let mut v = vcf::Record::default();

match self.reader.next_record(&self.reader.header(), &mut v) {
match self.reader.next_record(&self.header, &mut v) {
Ok(0) => None, // EOF
Ok(_) => {
self.record_number += 1;
Expand Down
54 changes: 27 additions & 27 deletions src/sniff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use std::io::{BufRead, Read};
use std::path::Path;

use crate::bedder_bed::BedderBed;
use crate::bedder_vcf::{BedderVCF, VCF};
use crate::bedder_vcf::BedderVCF;
use crate::position::PositionedIterator;
use noodles::bgzf;
use noodles::vcf;

/// File formats supported by this file detector.
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -52,35 +51,36 @@ where
format,
compression
);
let br: Box<dyn BufRead> = match compression {
Compression::None => Box::new(reader),
Compression::GZ => Box::new(std::io::BufReader::new(GzDecoder::new(reader))),
Compression::BGZF => match format {
// BCF|BAM will appear as bgzf so we don't want to do this outside
FileFormat::BCF | FileFormat::BAM => Box::new(reader),
_ => Box::new(bgzf::Reader::new(reader)),
},
Compression::RAZF => unimplemented!(),
};
/*
*/
match format {
FileFormat::VCF => {
let mut vcf = vcf::reader::Builder.build_from_reader(br)?;
let hdr = vcf.read_header()?;
let bed_vcf = BedderVCF::new(VCF::VCF(vcf), hdr)?;
Ok(Box::new(bed_vcf))
}
FileFormat::BCF => {
let mut bcf = noodles::bcf::Reader::new(br);
let hdr = bcf.read_header()?;
let bed_vcf = BedderVCF::new(VCF::BCF(bcf), hdr)?;
FileFormat::VCF | FileFormat::BCF => {
// get &str from path
let path = path.as_ref().to_str().unwrap();
let x = xvcf::Reader::from_reader(Box::new(reader), Some(path))?;
let bed_vcf = BedderVCF::new(x)?;
Ok(Box::new(bed_vcf))
}

FileFormat::BED => {
let reader = BedderBed::new(br);
Ok(Box::new(reader))
_ => {
let br: Box<dyn BufRead> = match compression {
Compression::None => Box::new(reader),
Compression::GZ => Box::new(std::io::BufReader::new(GzDecoder::new(reader))),
Compression::BGZF => match format {
// BCF|BAM will appear as bgzf so we don't want to do this outside
FileFormat::BCF | FileFormat::BAM => Box::new(reader),
_ => Box::new(bgzf::Reader::new(reader)),
},
Compression::RAZF => unimplemented!(),
};

match format {
FileFormat::BED => {
let reader = BedderBed::new(br);
Ok(Box::new(reader))
}
_ => unimplemented!("{format:?} not yet supported"),
}
}
_ => unimplemented!("{format:?} not yet supported"),
}
}

Expand Down

0 comments on commit 90247ce

Please sign in to comment.