Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldcampbelljr committed Jan 9, 2025
1 parent 5f5973b commit 8ae3d41
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
5 changes: 2 additions & 3 deletions gtars/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub fn get_dynamic_reader(path: &Path) -> Result<BufReader<Box<dyn Read>>> {
///
/// - file_path: path to the file to read, or '-' for stdin
///
/// # Returns
///
/// # Returns
///
/// A `BufReader` object for a given file path or stdin.
pub fn get_dynamic_reader_w_stdin(file_path_str: &str) -> Result<BufReader<Box<dyn Read>>> {
if file_path_str == "-" {
Expand All @@ -51,7 +51,6 @@ pub fn get_dynamic_reader_w_stdin(file_path_str: &str) -> Result<BufReader<Box<d
}
}


///
/// Create a region-to-id hash-map from a list of regions
///
Expand Down
18 changes: 9 additions & 9 deletions gtars/src/digests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
//! # Usage
//!
//! The `sha512t24u` function can be used to compute the GA4GH sha512t24 checksum of a string.
//!
//!
//! ```rust
//! use gtars::digests::sha512t24u;
//!
//! let digest = sha512t24u("hello world");
//! ```
use std::io::prelude::{Read, Write};
use std::io;
use std::fs::File;
use std::io;
use std::io::prelude::{Read, Write};
use std::path::Path;

use anyhow::Result;
use md5::Md5;
use seq_io::fasta::{Reader, Record, RefRecord};
use sha2::{Digest, Sha512};
use seq_io::fasta::{Reader, RefRecord, Record};

use crate::common::utils::get_dynamic_reader;

Expand Down Expand Up @@ -75,7 +75,6 @@ pub fn md5(string: &str) -> String {
format!("{:x}", result)
}


/// Processes a FASTA file to compute the digests of each sequence in the file.
///
/// This function reads a FASTA file, computes the SHA-512 and MD5 digests for each sequence,
Expand Down Expand Up @@ -103,7 +102,8 @@ pub fn digest_fasta(file_path: &str) -> Result<Vec<DigestResult>> {
let file_reader = get_dynamic_reader(&path)?;
let mut fasta_reader = Reader::new(file_reader);
let mut results = Vec::new();
while let Some(record) = fasta_reader.next() { // returns a RefRecord object
while let Some(record) = fasta_reader.next() {
// returns a RefRecord object
let record = record.expect("Error found when retrieving next record.");
let id = record.id().expect("No ID found for the FASTA record");
let mut sha512_hasher = Sha512::new();
Expand All @@ -123,7 +123,7 @@ pub fn digest_fasta(file_path: &str) -> Result<Vec<DigestResult>> {
id: id.to_string(),
length: length,
sha512t24u: sha512,
md5: md5
md5: md5,
});
}
Ok(results)
Expand Down Expand Up @@ -169,10 +169,10 @@ mod tests {
assert_eq!(results[0].sha512t24u, "iYtREV555dUFKg2_agSJW6suquUyPpMw");
assert_eq!(results[0].md5, "5f63cfaa3ef61f88c9635fb9d18ec945");
}

#[test]
fn bogus_fasta_file() {
let result = digest_fasta("tests/data/bogus.fa");
assert!(result.is_err(), "Expected an error for a bogus fasta file");
}
}
}
1 change: 0 additions & 1 deletion gtars/src/uniwig/counting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub fn start_end_counts(
smoothsize: i32,
stepsize: i32,
) -> (Vec<u32>, Vec<i32>) {

let mut v_coordinate_positions: Vec<i32> = Vec::new(); // these are the final coordinates after any adjustments
let mut v_coord_counts: Vec<u32> = Vec::new(); // u8 stores 0:255 This may be insufficient. u16 max is 65535

Expand Down
43 changes: 23 additions & 20 deletions gtars/src/uniwig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub fn run_uniwig(matches: &ArgMatches) {
}

/// Ensures that the start position is at a minimum equal to `1`
fn clamped_start_position(start: i32, smoothsize: i32, wig_shift:i32) -> i32 {
fn clamped_start_position(start: i32, smoothsize: i32, wig_shift: i32) -> i32 {
std::cmp::max(1, start - smoothsize + wig_shift)
}
/// Ensure that the start position is at a minimum equal to `0`
Expand Down Expand Up @@ -222,7 +222,6 @@ pub fn uniwig_main(
.build()
.unwrap();


// Determine Input File Type
let input_filetype = FileType::from_str(filetype.to_lowercase().as_str());
// Set up output file names
Expand Down Expand Up @@ -319,7 +318,7 @@ pub fn uniwig_main(
clamped_start_position(
primary_start.0,
smoothsize,
1 //must shift wiggle starts and core by 1 since it is 1 based
1, //must shift wiggle starts and core by 1 since it is 1 based
),
stepsize,
current_chrom_size,
Expand Down Expand Up @@ -449,7 +448,7 @@ pub fn uniwig_main(
clamped_start_position(
primary_end.0,
smoothsize,
0
0,
),
stepsize,
meta_data_file_names[1].clone(),
Expand All @@ -468,7 +467,7 @@ pub fn uniwig_main(
clamped_start_position(
primary_end.0,
smoothsize,
0
0,

Check warning on line 470 in gtars/src/uniwig/mod.rs

View check run for this annotation

Codecov / codecov/patch

gtars/src/uniwig/mod.rs#L470

Added line #L470 was not covered by tests
),
stepsize,
meta_data_file_names[1].clone(),
Expand Down Expand Up @@ -520,7 +519,7 @@ pub fn uniwig_main(
&core_results.0,
file_name.clone(),
chrom_name.clone(),
clamped_start_position(primary_start.0, 0,1), //starts are 1 based must be shifted by 1
clamped_start_position(primary_start.0, 0, 1), //starts are 1 based must be shifted by 1
stepsize,
current_chrom_size,
);
Expand Down Expand Up @@ -589,9 +588,9 @@ pub fn uniwig_main(
);
}
}
"npy" =>{
"npy" => {
// populate hashmap for the npy meta data
for chromosome in final_chromosomes.iter(){
for chromosome in final_chromosomes.iter() {
let chr_name = chromosome.chrom.clone();
let current_chrom_size =
*chrom_sizes.get(&chromosome.chrom).unwrap() as i32;
Expand All @@ -605,42 +604,46 @@ pub fn uniwig_main(
}

for location in vec_count_type.iter() {

let temp_meta_file_name = format!("{}{}.{}", bwfileheader, *location, "meta");
let temp_meta_file_name =
format!("{}{}.{}", bwfileheader, *location, "meta");

if let Ok(file) = File::open(&temp_meta_file_name) {

let reader = BufReader::new(file);

for line in reader.lines() {
let line = line.unwrap();
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 3 {
let chrom = parts[1].split('=')
.nth(1)
.expect("Processing npy metadata file: Missing chromosome in line");
let chrom = parts[1].split('=').nth(1).expect(
"Processing npy metadata file: Missing chromosome in line",
);
let start_str = parts[2].split('=')
.nth(1)
.expect("Processing npy metadata file: Missing start position in line");
let starting_position: i32 = start_str.parse().expect("Processing npy metadata file: Invalid start position");
let starting_position: i32 = start_str.parse().expect(
"Processing npy metadata file: Invalid start position",
);

if let Some(current_chr_data) = npy_meta_data_map.get_mut(chrom) {
current_chr_data.insert((*location.to_string()).parse().unwrap(), starting_position);
if let Some(current_chr_data) = npy_meta_data_map.get_mut(chrom)
{
current_chr_data.insert(
(*location.to_string()).parse().unwrap(),
starting_position,
);
}
}

Check warning on line 634 in gtars/src/uniwig/mod.rs

View check run for this annotation

Codecov / codecov/patch

gtars/src/uniwig/mod.rs#L634

Added line #L634 was not covered by tests
}
// Remove the file after it is used.
let path = std::path::Path::new(&temp_meta_file_name);
let _ = remove_file(path).unwrap();
}

}
//write combined metadata as json
let json_string = serde_json::to_string_pretty(&npy_meta_data_map).unwrap();
let combined_npy_meta_file_path = format!("{}{}.{}", bwfileheader, "npy_meta", "json");
let combined_npy_meta_file_path =
format!("{}{}.{}", bwfileheader, "npy_meta", "json");
let mut file = File::create(combined_npy_meta_file_path).unwrap();
file.write_all(json_string.as_bytes()).unwrap();

}
_ => {}
}
Expand Down

0 comments on commit 8ae3d41

Please sign in to comment.