-
Notifications
You must be signed in to change notification settings - Fork 86
feat: added sam_flag module #440
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
Open
rLannes
wants to merge
12
commits into
rust-bio:master
Choose a base branch
from
rLannes:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a0a813e
added SamFlag module
rLannes f76747c
Update mod.rs
rLannes 2e43bb2
Merge branch 'master' into master
johanneskoester 82f1e90
refactored the module and struct name, and ran cargo fmt
rLannes cdcdf9d
Merge branch 'master' into master
rLannes 42a83ca
with module name change the docstring import was not working. Fixed.
rLannes 01383b5
typo
rLannes 30dfef4
re-ran cargo fmt
rLannes bfb2db1
added 2 function is_in_flag() and is_not_in_flag() // updated the doc…
rLannes 1388663
Removed sam_flag.rs file
rLannes 61b391f
removed deprecated code
rLannes ee85659
Merge branch 'master' into master
rLannes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//! A module that provides an easier way to work with SAM flags. | ||
//! It achieves this by providing a struct (`Flag`) with associated constants representing | ||
//! flags. And the `is_in_flag()` and `is_not_in_flag()` functions that allow testing if specific flags are set or not set. | ||
//! | ||
//! ``` | ||
//! use rust_htslib::bam::flags{Flag, is_in_flag, is_not_in_flag}; | ||
//! let read_flag = record.flag(); // in general this is the way to obtain a flag. | ||
//! let read_flag = 64; | ||
//! assert_eq!(is_in_flag(read_flag, Flag::FIRST_IN_PAIR), true); | ||
//! assert_eq!(is_not_in_flag(read_flag, Flag::MATE_UNMAPPED), true); | ||
//! ``` | ||
/// | ||
/// This structure contains constants representing SAM flag values as u16. | ||
/// Using this structure incurs no runtime cost. | ||
/// | ||
/// ``` | ||
/// use rust_htslib::bam::flags::Flag; | ||
/// // to get the value of a flag representing a read paired, and reversly mapped. | ||
/// let flag = Flag::PAIRED + Flag::READ_RERVERSE; | ||
/// | ||
/// ``` | ||
pub struct Flag; | ||
|
||
impl Flag { | ||
pub const PAIRED: u16 = 1; | ||
pub const PROPERLY_PAIRED: u16 = 2; | ||
pub const READ_UNMAPPED: u16 = 4; | ||
pub const MATE_UNMAPPED: u16 = 8; | ||
pub const READ_RERVERSE: u16 = 16; | ||
pub const MATE_REVERSE: u16 = 32; | ||
pub const FIRST_IN_PAIR: u16 = 64; | ||
pub const SECOND_IN_PAIR: u16 = 128; | ||
pub const NOT_PRIMARY_ALN: u16 = 256; | ||
pub const FAIL_QC: u16 = 512; | ||
pub const DUPLICATE: u16 = 1024; | ||
pub const SUPPLEMENTARY: u16 = 2048; | ||
} | ||
|
||
pub fn is_not_in_flag(flag: u16, not_in: u16) -> bool { | ||
//! This function uses bitwise operations to test if flags are not set | ||
//! # Arguments | ||
//! * `flag`: u16 - The record flag you want to test | ||
//! * `not_in`: u16 - The flags you want to check if they are not set (use 0 for no test) | ||
//! | ||
//! # Usage: | ||
//! example: let test if a flag is primary alignment and did not fail QC | ||
//! ``` | ||
//! use rust_htslib::bam::flags; | ||
//! use rust_htslib::bam::flags::Flag; | ||
//! let read_flag = 65; | ||
//! assert_eq!(flags::is_not_in_flag(read_flag, Flag::NOT_PRIMARY_ALN + Flag::FAIL_QC), true); | ||
//! ``` | ||
//! let test that the read is mapped. | ||
//! ``` | ||
//! | ||
//! use rust_htslib::bam::flags::{Flag, is_not_in_flag}; | ||
//! let read_flag = 18; | ||
//! assert_eq!(is_not_in_flag(read_flag, Flag::READ_UNMAPPED), true); | ||
//! ``` | ||
//! | ||
if (not_in & flag) != 0 { | ||
return false; | ||
} | ||
true | ||
} | ||
pub fn is_in_flag(flag: u16, in_: u16) -> bool { | ||
//! This function uses bitwise operations to test if flags are set | ||
//! # Arguments | ||
//! * `flag`: u16 - The record flag you want to test | ||
//! * `in_`: u16 - The flags you want to check if they are set (use 0 for no test) | ||
//! | ||
//! # Usage: | ||
//! example: let test if a flag is both paired and first in pair | ||
//! ``` | ||
//! use rust_htslib::bam::flags::{Flag, is_in_flag}; | ||
//! let read_flag = 65; | ||
//! assert_eq!(is_in_flag(read_flag, Flag::PAIRED + Flag::FIRST_IN_PAIR), true); | ||
//! ``` | ||
|
||
if (in_ & flag) != in_ { | ||
return false; | ||
} | ||
true | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
pub mod buffer; | ||
pub mod ext; | ||
pub mod flags; | ||
pub mod header; | ||
pub mod index; | ||
pub mod pileup; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comments should be put above the function, without the exclamation mark but with three slashes instead: https://doc.rust-lang.org/rust-by-example/meta/doc.html