Git Product home page Git Product logo

Comments (3)

d-cameron avatar d-cameron commented on August 25, 2024

The function signature is:

pub fn write_to<W: io::Write>(
    mut writer: W,

Shouldn't it be?

pub fn write_to<W: std::io::Write>(
    writer: &mut W,

from seq_io.

markschl avatar markschl commented on August 25, 2024

I'm sorry if writing isn't documented well. I'm working on a next version with better docs, but it is progressing slowly.

The following should work, because io::Write is also implemented for mutable references to Write instances:

seq_io::fastq::write_to(&mut writer, &r.head, &r.seq, &r.qual)?;

Like this, the writer is not moved to the writing function. Also note the ?, since it is necessary to handle write errors.

It is even simpler to use the write method of records (it's a bit hidden because it's in the Record trait:

r.write(&mut writer)?;

Did you use the records() method on purpose for iterating over records? It will yield OwnedRecord instances, which is comparable to the Rust-Bio reading methods. In case reading performance is important for you and you don't need to store the record objects for later use, I would advise to use the while let Some(r) = reader.next() construct. It returns a RefRecord borrowing its data from the underlying reader, and consequently no vector allocations and (mostly) no copying are required. See the bottom of the GitHub page for how the two compare (borrowed on top, owned below).

Your code would then be:

let mut reader = Reader::from_path("in.fastq")?;
let mut writer = BufWriter::new(File::create("out.fastq")?);
wile let Some(r) = reader.next() {
    let r = r?;
    r.write(&mut writer)?;
}

Also note that you need to use File::create instead of File::open to open a file in write mode, and that constructing a Path instance is not necessary because File::create accepts AsRef<Path> which &str implements.

from seq_io.

d-cameron avatar d-cameron commented on August 25, 2024

The following should work, because io::Write is also implemented for mutable references to Write instances:

Aah, it's the magic in the Write trait itself that I missed. Add the expected &mut and the magic happens.

Thanks for the explanation :)

Also note that you need to use File::create instead of File::open to open a file in write mode

That one got picked up in my testing pretty quickly.

Did you use the records() method on purpose for iterating over records?

Mostly it was because the borrowed version didn't appear to implement Iter and I was switching from another fastq parsing library.

from seq_io.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.