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

refactor: inline pieces of bio-rust crate related to FM-index #1194

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

members = [
"packages_rs/*",
"packages_rs/3rdparty/bio_inline",
]

exclude = [
"packages_rs/3rdparty"
"packages_rs/3rdparty",
]
24 changes: 24 additions & 0 deletions packages_rs/3rdparty/bio_inline/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
edition = "2021"
name = "bio_inline"
version = "1.3.1"

[dependencies]
bio-types = "=1.0.0"
bit-set = "=0.5.3"
bv = { version = "=0.11.1", features = ["serde"] }
bytecount = "=0.6.3"
fxhash = "=0.2.1"
lazy_static = "=1.4.0"
num = "=0.4.0"
num-integer = "0.1.45"
num-traits = "=0.2.15"
serde = { version = "=1.0.164", features = ["derive"] }
vec_map = { version = "=0.8.2", features = ["serde"] }

[features]
generic-simd = ["bytecount/generic-simd"]
runtime-dispatch-simd = ["bytecount/runtime-dispatch-simd"]

[dev-dependencies]
rand = "=0.8.5"
9 changes: 9 additions & 0 deletions packages_rs/3rdparty/bio_inline/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2016 Johannes Köster, the Rust-Bio team, Google Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions packages_rs/3rdparty/bio_inline/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# bio_inline

Bits and pieces from "bio" crate (https://github.com/rust-bio/rust-bio), adapted to the needs of this project.

#### How to add more code from bio crate:

- add required source files from `bio` project's `src` directory to `bio_inline/src` directory here, preserving directory structure
- find and replace `bio::` with `bio_inline::` in all new files
- remove unused code and unused `use` imports recursively
- (optional) lint and format the new code
- (optional) modify the new code as needed (this is probably why you are doing this procedure)

#### How to switch project code from using `bio` to using `bio_inline`:

- find and replace `bio::` to `bio_inline::`
- add missing library code recursively (see previous section) until project compiles
114 changes: 114 additions & 0 deletions packages_rs/3rdparty/bio_inline/src/alphabets/dna.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2014-2015 Johannes Köster, Peer Aramillo Irizar.
// Licensed under the MIT license (http://opensource.org/licenses/MIT)
// This file may not be copied, modified, or distributed
// except according to those terms.

//! Implementation of the DNA alphabet.
//!
//! # Example
//!
//! ```
//! use bio_inline::alphabets;
//! let alphabet = alphabets::dna::alphabet();
//! assert!(alphabet.is_word(b"GATTACA"));
//! assert!(alphabet.is_word(b"gattaca"));
//! assert!(!alphabet.is_word(b"ACGU"));
//! ```

use crate::alphabets::Alphabet;
use lazy_static::lazy_static;
use std::borrow::Borrow;

/// The DNA alphabet (uppercase and lowercase).
pub fn alphabet() -> Alphabet {
Alphabet::new(b"ACGTacgt")
}

/// The DNA alphabet including N (uppercase and lowercase).
pub fn n_alphabet() -> Alphabet {
Alphabet::new(b"ACGTNacgtn")
}

/// The IUPAC DNA alphabet (uppercase and lowercase).
pub fn iupac_alphabet() -> Alphabet {
Alphabet::new(b"ACGTRYSWKMBDHVNZacgtryswkmbdhvnz")
}

lazy_static! {
static ref COMPLEMENT: [u8; 256] = {
let mut comp = [0; 256];
for (v, a) in comp.iter_mut().enumerate() {
*a = v as u8;
}
for (&a, &b) in b"AGCTYRWSKMDVHBN".iter().zip(b"TCGARYWSMKHBDVN".iter()) {
comp[a as usize] = b;
comp[a as usize + 32] = b + 32; // lowercase variants
}
comp
};
}

/// Return complement of given DNA alphabet character (IUPAC alphabet supported).
///
/// Casing of input character is preserved, e.g. `t` → `a`, but `T` → `A`.
/// All `N`s remain as they are.
///
/// ```
/// use bio_inline::alphabets::dna;
///
/// assert_eq!(dna::complement(65), 84); // A → T
/// assert_eq!(dna::complement(99), 103); // c → g
/// assert_eq!(dna::complement(78), 78); // N → N
/// assert_eq!(dna::complement(89), 82); // Y → R
/// assert_eq!(dna::complement(115), 115); // s → s
/// ```
#[inline]
pub fn complement(a: u8) -> u8 {
COMPLEMENT[a as usize]
}

/// Calculate reverse complement of given text (IUPAC alphabet supported).
///
/// Casing of characters is preserved, e.g. `b"NaCgT"` → `b"aCgTN"`.
/// All `N`s remain as they are.
///
/// ```
/// use bio_inline::alphabets::dna;
///
/// assert_eq!(dna::revcomp(b"ACGTN"), b"NACGT");
/// assert_eq!(dna::revcomp(b"GaTtaCA"), b"TGtaAtC");
/// assert_eq!(dna::revcomp(b"AGCTYRWSKMDVHBN"), b"NVDBHKMSWYRAGCT");
/// ```
pub fn revcomp<C, T>(text: T) -> Vec<u8>
where
C: Borrow<u8>,
T: IntoIterator<Item = C>,
T::IntoIter: DoubleEndedIterator,
{
text.into_iter().rev().map(|a| complement(*a.borrow())).collect()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn is_word() {
assert!(alphabet().is_word(b"GATTACA"));
}

#[test]
fn is_no_word() {
assert!(!alphabet().is_word(b"gaUUaca"));
}

#[test]
fn symbol_is_no_word() {
assert!(!alphabet().is_word(b"#"));
}

#[test]
fn number_is_no_word() {
assert!(!alphabet().is_word(b"42"));
}
}
Loading