From 7872ac35045f063caffb95b7f4a84184fc4ab2cd Mon Sep 17 00:00:00 2001 From: wdecoster Date: Mon, 4 Sep 2023 14:01:02 +0200 Subject: [PATCH] fix memory problem from faidx temporarily, see https://github.com/rust-bio/rust-htslib/issues/401 --- Cargo.toml | 3 ++- src/main.rs | 2 +- src/repeats.rs | 21 ++++++++++++--------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e42cf7..027ad97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "STRdust" -version = "0.2.0" +version = "0.2.1" edition = "2021" @@ -26,6 +26,7 @@ distance = "0.4.0" levenshtein = "1.0.5" rand = "0.8.5" libz-sys = "1.1.12" +libc = "0.2.147" [dev-dependencies] ctor = "*" diff --git a/src/main.rs b/src/main.rs index 0bdc891..d973406 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,7 +64,7 @@ pub struct Cli { fn is_file(pathname: &str) -> Result<(), String> { let path = PathBuf::from(pathname); - if path.is_file() { + if path.is_file() || pathname.starts_with("http") { Ok(()) } else { Err(format!("Input file {} is invalid", path.display())) diff --git a/src/repeats.rs b/src/repeats.rs index 6a5ce8e..ed52cc8 100644 --- a/src/repeats.rs +++ b/src/repeats.rs @@ -1,6 +1,6 @@ use log::error; use rust_htslib::faidx; -use std::{fmt, io::Write}; +use std::fmt; pub struct RepeatInterval { pub chrom: String, @@ -45,16 +45,18 @@ impl RepeatInterval { (self.end + flanking - 2) as usize, ) .expect("Failed to extract fas_right sequence from fasta for {chrom}:{start}-{end}"); - // write the new reference sequence to a file - let mut newref_file = - std::fs::File::create("newref.fa").expect("Unable to create newref.fa"); - newref_file - .write_all(&[fas_left, fas_right].concat()) - .expect("Unable to write to newref.fa"); - [fas_left, fas_right].concat() + // // write the new reference sequence to a file + // let mut newref_file = + // std::fs::File::create("newref.fa").expect("Unable to create newref.fa"); + // newref_file + // .write_all(&[fas_left, fas_right].concat()) + // .expect("Unable to write to newref.fa"); + let newref = [fas_left, fas_right].concat(); + unsafe { libc::free(fas_left.as_ptr() as *mut std::ffi::c_void) }; // Free up memory + unsafe { libc::free(fas_right.as_ptr() as *mut std::ffi::c_void) }; // Free up memory + newref } - /// If the repeat sequence is out of bounds, None is returned pub fn reference_repeat_sequence(&self, fasta: &String) -> Option { let fas = faidx::Reader::from_path(fasta).expect("Failed to read fasta"); let repeat_ref_sequence = std::str::from_utf8( @@ -63,6 +65,7 @@ impl RepeatInterval { ) .expect("Failed to convert repeat sequence to string for {chrom}:{start}-{end}") .to_string(); + // If the repeat sequence is out of bounds, None is returned if repeat_ref_sequence == "N" { eprintln!( "Cannot genotype repeat at {self} because it is out of bounds for the fasta file",