Skip to content

Commit

Permalink
fix memory problem from faidx
Browse files Browse the repository at this point in the history
temporarily, see rust-bio/rust-htslib#401
  • Loading branch information
wdecoster committed Sep 4, 2023
1 parent f1f7aee commit 7872ac3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "STRdust"
version = "0.2.0"
version = "0.2.1"
edition = "2021"


Expand All @@ -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 = "*"
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
21 changes: 12 additions & 9 deletions src/repeats.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<String> {
let fas = faidx::Reader::from_path(fasta).expect("Failed to read fasta");
let repeat_ref_sequence = std::str::from_utf8(
Expand All @@ -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",
Expand Down

0 comments on commit 7872ac3

Please sign in to comment.