Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to operate on Format/Info fields for Records? #405

Open
rickymagner opened this issue Aug 11, 2023 · 2 comments
Open

How to operate on Format/Info fields for Records? #405

rickymagner opened this issue Aug 11, 2023 · 2 comments

Comments

@rickymagner
Copy link

I'm having trouble accessing/using FORMAT/INFO fields from vcf Records and the documentation isn't clear on how to be able to do anything with the data types returned via the API. Here's an example code snippet:

use rust_htslib::bcf::record::Record;

fn test_record(rec: Record) {
    let i = rec.format(b"DP").integer().expect("Unable to convert to integer"); // has type BufferBacked<'_, Vec<&[i32]>, Buffer>
}

It's not clear that i can be used for any computation, and it especially seems like i cannot be coerced into a Vec<u32> in any way I can see. How can I actually do any computation with this sort of value? For example, suppose I wanted to add up the values in the vector of DP values in my vcf. How would I achieve that with these data types? Given that u32 implements Copy, ideally I'd like to be able to copy the wrapped value to an owned type and do something with that.

What's the reason behind wrapping these values in the BufferBacked type, and what is that type actually doing? There are warnings in the docs about keeping it in scope to handle some memory issues, but not much more on how to use these types.

@r-blanchet
Copy link

I am having the exact same issue with string values in INFO fields. I am struggling to convert the BufferBacked to a string type.

@YeHW
Copy link

YeHW commented Sep 15, 2023

I'm having trouble accessing/using FORMAT/INFO fields from vcf Records and the documentation isn't clear on how to be able to do anything with the data types returned via the API. Here's an example code snippet:

use rust_htslib::bcf::record::Record;

fn test_record(rec: Record) {
    let i = rec.format(b"DP").integer().expect("Unable to convert to integer"); // has type BufferBacked<'_, Vec<&[i32]>, Buffer>
}

It's not clear that i can be used for any computation, and it especially seems like i cannot be coerced into a Vec<u32> in any way I can see. How can I actually do any computation with this sort of value? For example, suppose I wanted to add up the values in the vector of DP values in my vcf. How would I achieve that with these data types? Given that u32 implements Copy, ideally I'd like to be able to copy the wrapped value to an owned type and do something with that.

What's the reason behind wrapping these values in the BufferBacked type, and what is that type actually doing? There are warnings in the docs about keeping it in scope to handle some memory issues, but not much more on how to use these types.

I found a way to access the value in BufferBacked.
Might be helpful:

let Some(u) = record.info(b"MQ").float().expect("Error accessing INFO MQ") else { todo!() };
println!("{:#?}", u[0]);

let v = record.format(b"DP").integer().expect("Error accessing FORMAT DP");
println!("{:#?}", v[0]);

use std::str::from_utf8 to trans BufferedBackend to &str
#350 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants