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

VCF/BCF Writer - auto-recognize format and compression based on file extension #307

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nh13
Copy link
Contributor

@nh13 nh13 commented May 18, 2021

This is my first every PR in rust, in any repo, so please forgive my non-idiomatic way of writing Rust.

I was surprised to find that there was no way to create a writer that would auto-recognize the compression and format based on file extension, so here's a first incomplete path. I am happy to write some tests, clean up the code to be more idiomatic (with help). But before I do this, I wanted to get the maintainers' thoughts.

/// * `.vcf.gz`. -> compressed VCF
/// * `.vcf.gzip` -> compressed VCF
/// * `.vcf.bgzf` -> compressed VCF
/// * `.bcf` -> compressed BCF
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +690 to +693
if (fields[length-1] == "gz" || fields[length-1] == "gzip" || fields[length-1] == "bgzf") { false }
else if (fields[length-1] == "bcf") { false }
else if (fields[length-1] == "vcf") { true }
else { true } // FIXME
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd try to use the extension method of Path. It returns Option<&OsStr>, which is a bit cumbersome to use since it's no "regular" String/&str, but can be transformed into one, see here.

let length = fields.len();
// FIXME: check that we have enough fields
let uncompressed: bool = {
if (fields[length-1] == "gz" || fields[length-1] == "gzip" || fields[length-1] == "bgzf") { false }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to @tedil's recommendation, also have a look at the match statement for a more idiomatic construct when checking these different extensions:
https://doc.rust-lang.org/rust-by-example/flow_control/match.html

@dlaehnemann
Copy link
Member

Good to see you over here! This is definitely a good addition that will make it easier to write VCF/BCF.

As for pointers for more idiomatic Rust, beyond those provided in the code, one great feature of Rust are doctests. For all the relevant links, have a look at the rust-bio documentation guidelines:
https://github.com/rust-bio/rust-bio#documentation-guidelines

And finally, if you want some automated recommendations on your code, you could check out cargo clippy, which is the Linting step in the CI.

Also, don't hesitate to ask anything!

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

Successfully merging this pull request may close these issues.

3 participants