Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tibvdm committed May 18, 2024
1 parent f26cf34 commit 679043f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 42 deletions.
56 changes: 32 additions & 24 deletions bitarray/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
mod binary;

use std::{cmp::max, io::{
Result,
Write
}};
use std::{
cmp::max,
io::{
Result,
Write
}
};

/// Re-export the `Binary` trait.
pub use binary::Binary;
Expand Down Expand Up @@ -34,7 +37,11 @@ impl BitArray {
///
/// A new `BitArray` with the specified capacity.
pub fn with_capacity(capacity: usize, bits_per_value: usize) -> Self {
let extra = if capacity * bits_per_value % 64 == 0 { 0 } else { 1 };
let extra = if capacity * bits_per_value % 64 == 0 {
0
} else {
1
};
Self {
data: vec![0; capacity * bits_per_value / 64 + extra],
mask: (1 << bits_per_value) - 1,
Expand Down Expand Up @@ -160,9 +167,11 @@ pub fn data_to_writer(
// Update the max capacity to be a multiple of the greatest common divisor of the bits per value
// and 64. This is done to ensure that the bit array can store the data entirely
let greates_common_divisor = gcd(bits_per_value, 64);
let capacity = max(greates_common_divisor, max_capacity / greates_common_divisor * greates_common_divisor);
let capacity =
max(greates_common_divisor, max_capacity / greates_common_divisor * greates_common_divisor);

// If amount of data is less than the max capacity, write the data to the writer in a single chunk
// If amount of data is less than the max capacity, write the data to the writer in a single
// chunk
if data.len() <= capacity {
let mut bitarray = BitArray::with_capacity(data.len(), bits_per_value);

Expand Down Expand Up @@ -203,21 +212,21 @@ pub fn data_to_writer(
}

/// Calculates the greatest common divisor of two numbers.
///
///
/// # Arguments
///
///
/// * `a` - The first number.
/// * `b` - The second number.
///
///
/// # Returns
///
///
/// The greatest common divisor of the two numbers.
fn gcd(mut a: usize, mut b: usize) -> usize {
while b != 0 {
if b < a {
std::mem::swap(&mut b, &mut a);
}
b %= a;
if b < a {
std::mem::swap(&mut b, &mut a);
}
b %= a;
}
a
}
Expand Down Expand Up @@ -305,7 +314,7 @@ mod tests {
fn test_data_to_writer_chunks_needed_no_remainder() {
let data = vec![
0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555, 0x66666666, 0x77777777,
0x88888888
0x88888888,
];
let mut writer = Vec::new();

Expand All @@ -314,9 +323,9 @@ mod tests {
assert_eq!(
writer,
vec![
0x22, 0x22, 0x22, 0x22, 0x11, 0x11, 0x11, 0x11, 0x44, 0x44, 0x44, 0x44, 0x33,
0x33, 0x33, 0x33, 0x66, 0x66, 0x66, 0x66, 0x55, 0x55, 0x55, 0x55, 0x88, 0x88,
0x88, 0x88, 0x77, 0x77, 0x77, 0x77
0x22, 0x22, 0x22, 0x22, 0x11, 0x11, 0x11, 0x11, 0x44, 0x44, 0x44, 0x44, 0x33, 0x33,
0x33, 0x33, 0x66, 0x66, 0x66, 0x66, 0x55, 0x55, 0x55, 0x55, 0x88, 0x88, 0x88, 0x88,
0x77, 0x77, 0x77, 0x77
]
);
}
Expand All @@ -325,7 +334,7 @@ mod tests {
fn test_data_to_writer_chunks_needed_plus_remainder() {
let data = vec![
0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555, 0x66666666, 0x77777777,
0x88888888, 0x99999999
0x88888888, 0x99999999,
];
let mut writer = Vec::new();

Expand All @@ -334,10 +343,9 @@ mod tests {
assert_eq!(
writer,
vec![
0x22, 0x22, 0x22, 0x22, 0x11, 0x11, 0x11, 0x11, 0x44, 0x44, 0x44, 0x44, 0x33,
0x33, 0x33, 0x33, 0x66, 0x66, 0x66, 0x66, 0x55, 0x55, 0x55, 0x55, 0x88, 0x88,
0x88, 0x88, 0x77, 0x77, 0x77, 0x77, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99, 0x99,
0x99
0x22, 0x22, 0x22, 0x22, 0x11, 0x11, 0x11, 0x11, 0x44, 0x44, 0x44, 0x44, 0x33, 0x33,
0x33, 0x33, 0x66, 0x66, 0x66, 0x66, 0x55, 0x55, 0x55, 0x55, 0x88, 0x88, 0x88, 0x88,
0x77, 0x77, 0x77, 0x77, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99, 0x99, 0x99
]
);
}
Expand Down
27 changes: 19 additions & 8 deletions sa-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,17 @@ mod tests {
#[test]
fn test_arguments() {
let args = Arguments::parse_from(&[
"sa-builder",
"--database-file", "database.fa",
"--taxonomy", "taxonomy.tsv",
"--output", "output.fa",
"--sparseness-factor", "2",
"--construction-algorithm", "lib-div-suf-sort",
"sa-builder",
"--database-file",
"database.fa",
"--taxonomy",
"taxonomy.tsv",
"--output",
"output.fa",
"--sparseness-factor",
"2",
"--construction-algorithm",
"lib-div-suf-sort",
"--compress-sa"
]);

Expand All @@ -141,8 +146,14 @@ mod tests {

#[test]
fn test_sa_construction_algorithm() {
assert_eq!(SAConstructionAlgorithm::from_str("lib-div-suf-sort", false), Ok(SAConstructionAlgorithm::LibDivSufSort));
assert_eq!(SAConstructionAlgorithm::from_str("lib-sais", false), Ok(SAConstructionAlgorithm::LibSais));
assert_eq!(
SAConstructionAlgorithm::from_str("lib-div-suf-sort", false),
Ok(SAConstructionAlgorithm::LibDivSufSort)
);
assert_eq!(
SAConstructionAlgorithm::from_str("lib-sais", false),
Ok(SAConstructionAlgorithm::LibSais)
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion sa-compression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {
valid_write_count: 3
};

dump_compressed_suffix_array(vec![ 1 ], 1, 8, &mut writer).unwrap();
dump_compressed_suffix_array(vec![1], 1, 8, &mut writer).unwrap();
}

#[test]
Expand Down
6 changes: 4 additions & 2 deletions sa-index/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ mod tests {

#[test]
fn test_fill_buffer_read_error() {
let mut input = FailingReader { valid_read_count: 0 };
let mut input = FailingReader {
valid_read_count: 0
};
let mut buffer = vec![0; 800];

assert!(fill_buffer(&mut input, &mut buffer).is_err());
Expand Down Expand Up @@ -358,7 +360,7 @@ mod tests {
valid_write_count: 3
};

dump_suffix_array(&vec![ 1 ], 1, &mut writer).unwrap();
dump_suffix_array(&vec![1], 1, &mut writer).unwrap();
}

#[test]
Expand Down
14 changes: 10 additions & 4 deletions sa-index/src/suffix_to_protein_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ mod tests {

use crate::{
suffix_to_protein_index::{
SuffixToProteinMappingStyle,
DenseSuffixToProtein,
SparseSuffixToProtein,
SuffixToProteinIndex
SuffixToProteinIndex,
SuffixToProteinMappingStyle
},
Nullable
};
Expand All @@ -140,8 +140,14 @@ mod tests {

#[test]
fn test_suffix_to_protein_mapping_style() {
assert_eq!(SuffixToProteinMappingStyle::Dense, SuffixToProteinMappingStyle::from_str("dense", false).unwrap());
assert_eq!(SuffixToProteinMappingStyle::Sparse, SuffixToProteinMappingStyle::from_str("sparse", false).unwrap());
assert_eq!(
SuffixToProteinMappingStyle::Dense,
SuffixToProteinMappingStyle::from_str("dense", false).unwrap()
);
assert_eq!(
SuffixToProteinMappingStyle::Sparse,
SuffixToProteinMappingStyle::from_str("sparse", false).unwrap()
);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions sa-mappings/src/functionality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ impl FunctionAggregator {
Some('E') => {
proteins_with_ec.insert(protein.uniprot_id.clone());
proteins_with_annotations.insert(protein.uniprot_id.clone());
},
}
Some('G') => {
proteins_with_go.insert(protein.uniprot_id.clone());
proteins_with_annotations.insert(protein.uniprot_id.clone());
},
}
Some('I') => {
proteins_with_ipr.insert(protein.uniprot_id.clone());
proteins_with_annotations.insert(protein.uniprot_id.clone());
},
}
_ => {}
};

Expand Down

0 comments on commit 679043f

Please sign in to comment.