helpers.rs 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. use anyhow::Context;
  2. use bitcode::{Decode, Encode};
  3. use glob::glob;
  4. use log::{debug, error, warn};
  5. use rust_htslib::bam::Read;
  6. use rustc_hash::FxHashMap;
  7. use serde::{Deserialize, Serialize};
  8. use std::{
  9. cmp::Ordering,
  10. collections::{HashMap, HashSet},
  11. fmt, fs,
  12. iter::Sum,
  13. ops::{Add, Div},
  14. path::{Path, PathBuf},
  15. sync::{atomic::AtomicBool, OnceLock},
  16. };
  17. use uuid::Uuid;
  18. pub fn find_unique_file(dir_path: &str, suffix: &str) -> anyhow::Result<String> {
  19. let mut matching_files = Vec::new();
  20. for entry in
  21. fs::read_dir(dir_path).with_context(|| format!("Failed to read directory: {}", dir_path))?
  22. {
  23. let entry = entry.with_context(|| "Failed to read directory entry")?;
  24. let path = entry.path();
  25. if path.is_file()
  26. && path
  27. .file_name()
  28. .and_then(|name| name.to_str())
  29. .map(|name| name.ends_with(suffix))
  30. .unwrap_or(false)
  31. {
  32. matching_files.push(path);
  33. }
  34. }
  35. match matching_files.len() {
  36. 0 => Err(anyhow::anyhow!("No file found ending with '{}'", suffix))
  37. .with_context(|| format!("In directory: {}", dir_path)),
  38. 1 => Ok(matching_files[0].to_string_lossy().into_owned()),
  39. _ => {
  40. matching_files.sort();
  41. let matches = matching_files
  42. .iter()
  43. .map(|p| p.display().to_string())
  44. .collect::<Vec<_>>()
  45. .join(", ");
  46. Err(anyhow::anyhow!(
  47. "Multiple files found ending with '{}': {}",
  48. suffix,
  49. matches
  50. ))
  51. .with_context(|| format!("In directory: {}", dir_path))
  52. }
  53. }
  54. }
  55. /// Return the output prefix by removing all suffixes from the first dot in the filename.
  56. ///
  57. /// This is intentionally stricter than [`Path::file_stem`]: filenames such as
  58. /// `sample.v1.vcf.gz` become `sample`, not `sample.v1.vcf`. This matches tools
  59. /// that expect a prefix rather than a single-extension stem.
  60. ///
  61. /// # Examples
  62. ///
  63. /// ```text
  64. /// /tmp/sample.vcf.gz -> /tmp/sample
  65. /// /tmp/sample.v1.vcf.gz -> /tmp/sample
  66. /// ```
  67. pub fn path_prefix(out: &str) -> anyhow::Result<String> {
  68. let out_path = Path::new(out);
  69. let out_dir = out_path
  70. .parent()
  71. .ok_or_else(|| anyhow::anyhow!("Can't parse the dir of {}", out_path.display()))?;
  72. let name = out_path
  73. .file_name()
  74. .and_then(|name| name.to_str())
  75. .ok_or_else(|| anyhow::anyhow!("Can't parse the file name of {}", out_path.display()))?;
  76. let stem = name
  77. .split_once('.')
  78. .map(|(stem, _)| stem)
  79. .ok_or_else(|| anyhow::anyhow!("Can't parse the file stem of {}", name))?;
  80. Ok(format!("{}/{stem}", out_dir.display()))
  81. }
  82. pub fn force_or_not(_path: &str, _force: bool) -> anyhow::Result<()> {
  83. // let path = Path::new(path);
  84. // let mut output_exists = path.exists();
  85. // let dir = path
  86. // .parent()
  87. // .context(format!("Can't parse the parent dir of {}", path.display()))?;
  88. //
  89. // if force && output_exists {
  90. // fs::remove_dir_all(dir)?;
  91. // fs::create_dir_all(dir)?;
  92. // output_exists = false;
  93. // }
  94. //
  95. // if output_exists {
  96. // info!("{} already exists.", path.display())
  97. // }
  98. Ok(())
  99. }
  100. /// Builds Singularity bind flags from input/output paths.
  101. ///
  102. /// - Converts file paths to their parent directory.
  103. /// - Skips non-existing paths to avoid Singularity errors.
  104. /// - Canonicalizes and deduplicates directories.
  105. pub fn singularity_bind_flags<I, P>(paths: I) -> String
  106. where
  107. I: IntoIterator<Item = P>,
  108. P: AsRef<Path>,
  109. {
  110. let mut seen = HashSet::new();
  111. let mut binds = Vec::new();
  112. let mut push_dir = |p: &Path| {
  113. if p.as_os_str().is_empty() || !p.exists() {
  114. return;
  115. }
  116. let dir = p.canonicalize().unwrap_or_else(|_| p.to_path_buf());
  117. if seen.insert(dir.clone()) {
  118. binds.push(format!(
  119. "--bind {src}:{dst}",
  120. src = dir.display(),
  121. dst = dir.display()
  122. ));
  123. }
  124. };
  125. for path_str in paths {
  126. let path = path_str.as_ref();
  127. if path.is_dir() {
  128. push_dir(path);
  129. } else if let Some(parent) = path.parent() {
  130. push_dir(parent);
  131. }
  132. }
  133. binds.join(" ")
  134. }
  135. use std::cmp::Ord;
  136. pub struct VectorIntersection<T> {
  137. pub common: Vec<T>,
  138. pub only_in_first: Vec<T>,
  139. pub only_in_second: Vec<T>,
  140. }
  141. impl<T> fmt::Display for VectorIntersection<T> {
  142. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  143. let total_items = self.common.len() + self.only_in_first.len() + self.only_in_second.len();
  144. writeln!(f, "Total items: {}", total_items)?;
  145. writeln!(
  146. f,
  147. "Common: {} ({:.2}%)",
  148. self.common.len(),
  149. percentage(self.common.len(), total_items)
  150. )?;
  151. writeln!(
  152. f,
  153. "Only in first: {} ({:.2}%)",
  154. self.only_in_first.len(),
  155. percentage(self.only_in_first.len(), total_items)
  156. )?;
  157. writeln!(
  158. f,
  159. "Only in second: {} ({:.2}%)",
  160. self.only_in_second.len(),
  161. percentage(self.only_in_second.len(), total_items)
  162. )
  163. }
  164. }
  165. fn percentage(part: usize, total: usize) -> f64 {
  166. if total == 0 {
  167. 0.0
  168. } else {
  169. (part as f64 / total as f64) * 100.0
  170. }
  171. }
  172. impl<T> Default for VectorIntersection<T> {
  173. fn default() -> Self {
  174. Self {
  175. common: Vec::new(),
  176. only_in_first: Vec::new(),
  177. only_in_second: Vec::new(),
  178. }
  179. }
  180. }
  181. impl<T: Ord + Clone> VectorIntersection<T> {
  182. fn merge(&mut self, other: &mut Self) {
  183. self.common.append(&mut other.common);
  184. self.only_in_first.append(&mut other.only_in_first);
  185. self.only_in_second.append(&mut other.only_in_second);
  186. }
  187. }
  188. fn intersection<T: Ord + Clone>(vec1: &[T], vec2: &[T]) -> VectorIntersection<T> {
  189. let mut result = VectorIntersection::default();
  190. let mut i = 0;
  191. let mut j = 0;
  192. while i < vec1.len() && j < vec2.len() {
  193. match vec1[i].cmp(&vec2[j]) {
  194. Ordering::Less => {
  195. result.only_in_first.push(vec1[i].clone());
  196. i += 1;
  197. }
  198. Ordering::Greater => {
  199. result.only_in_second.push(vec2[j].clone());
  200. j += 1;
  201. }
  202. Ordering::Equal => {
  203. let val = &vec1[i];
  204. let mut count1 = 1;
  205. let mut count2 = 1;
  206. // Count occurrences in vec1
  207. while i + 1 < vec1.len() && &vec1[i + 1] == val {
  208. i += 1;
  209. count1 += 1;
  210. }
  211. // Count occurrences in vec2
  212. while j + 1 < vec2.len() && &vec2[j + 1] == val {
  213. j += 1;
  214. count2 += 1;
  215. }
  216. // Add to common
  217. result
  218. .common
  219. .extend(std::iter::repeat_n(val.clone(), count1.min(count2)));
  220. // Add excess to only_in_first or only_in_second
  221. match count1.cmp(&count2) {
  222. Ordering::Greater => {
  223. result
  224. .only_in_first
  225. .extend(std::iter::repeat_n(val.clone(), count1 - count2));
  226. }
  227. Ordering::Less => {
  228. result
  229. .only_in_second
  230. .extend(std::iter::repeat_n(val.clone(), count2 - count1));
  231. }
  232. Ordering::Equal => {
  233. // No excess elements, do nothing
  234. }
  235. }
  236. i += 1;
  237. j += 1;
  238. }
  239. }
  240. }
  241. result.only_in_first.extend(vec1[i..].iter().cloned());
  242. result.only_in_second.extend(vec2[j..].iter().cloned());
  243. result
  244. }
  245. pub fn par_intersection<T: Ord + Send + Sync + Clone>(
  246. vec1: &[T],
  247. vec2: &[T],
  248. ) -> VectorIntersection<T> {
  249. par_intersection_by_value(vec1, vec2)
  250. }
  251. fn par_intersection_by_value<T: Ord + Send + Sync + Clone>(
  252. vec1: &[T],
  253. vec2: &[T],
  254. ) -> VectorIntersection<T> {
  255. const SEQUENTIAL_THRESHOLD: usize = 4096;
  256. if vec1.len() + vec2.len() <= SEQUENTIAL_THRESHOLD {
  257. return intersection(vec1, vec2);
  258. }
  259. if vec1.is_empty() {
  260. return VectorIntersection {
  261. common: Vec::new(),
  262. only_in_first: Vec::new(),
  263. only_in_second: vec2.to_vec(),
  264. };
  265. }
  266. if vec2.is_empty() {
  267. return VectorIntersection {
  268. common: Vec::new(),
  269. only_in_first: vec1.to_vec(),
  270. only_in_second: Vec::new(),
  271. };
  272. }
  273. let pivot = &vec1[vec1.len() / 2];
  274. let v1_left = vec1.partition_point(|x| x < pivot);
  275. let v1_right = vec1.partition_point(|x| x <= pivot);
  276. let v2_left = vec2.partition_point(|x| x < pivot);
  277. let v2_right = vec2.partition_point(|x| x <= pivot);
  278. let (mut left, mut right) = rayon::join(
  279. || par_intersection_by_value(&vec1[..v1_left], &vec2[..v2_left]),
  280. || par_intersection_by_value(&vec1[v1_right..], &vec2[v2_right..]),
  281. );
  282. let count1 = v1_right - v1_left;
  283. let count2 = v2_right - v2_left;
  284. let mut middle = VectorIntersection::default();
  285. middle
  286. .common
  287. .extend(std::iter::repeat_n(pivot.clone(), count1.min(count2)));
  288. match count1.cmp(&count2) {
  289. Ordering::Greater => middle
  290. .only_in_first
  291. .extend(std::iter::repeat_n(pivot.clone(), count1 - count2)),
  292. Ordering::Less => middle
  293. .only_in_second
  294. .extend(std::iter::repeat_n(pivot.clone(), count2 - count1)),
  295. Ordering::Equal => {}
  296. }
  297. left.merge(&mut middle);
  298. left.merge(&mut right);
  299. left
  300. }
  301. pub fn estimate_shannon_entropy(dna_sequence: &str) -> f64 {
  302. let m = dna_sequence.len() as f64;
  303. // Early return for empty sequences
  304. if m == 0.0 {
  305. return 0.0;
  306. }
  307. // Count occurrences of each base
  308. let mut bases = HashMap::<char, usize>::new();
  309. for base in dna_sequence.chars() {
  310. *bases.entry(base).or_insert(0) += 1;
  311. }
  312. // Calculate Shannon entropy
  313. let shannon_entropy_value: f64 = bases
  314. .values()
  315. .map(|&n_i| {
  316. let p_i = n_i as f64 / m;
  317. if p_i > 0.0 {
  318. -p_i * p_i.log2()
  319. } else {
  320. 0.0 // Avoid log2(0)
  321. }
  322. })
  323. .sum();
  324. shannon_entropy_value
  325. }
  326. pub fn mean<T>(values: &[T]) -> f64
  327. where
  328. T: Copy + Add<Output = T> + Div<Output = T> + Sum + Into<f64>,
  329. {
  330. let count = values.len();
  331. if count == 0 {
  332. return 0.0;
  333. }
  334. let sum: T = values.iter().copied().sum();
  335. sum.into() / count as f64
  336. }
  337. pub fn bin_data<V>(data: Vec<V>, bin_size: V) -> Vec<(V, usize)>
  338. where
  339. V: Copy + Default + PartialOrd + std::ops::AddAssign + std::ops::Add<Output = V>,
  340. {
  341. if data.is_empty() || bin_size.partial_cmp(&V::default()) != Some(Ordering::Greater) {
  342. return Vec::new();
  343. }
  344. // Sort comparable data points. For floats, this drops NaN values.
  345. let mut sorted_data: Vec<V> = data
  346. .into_iter()
  347. .filter(|v| v.partial_cmp(v) == Some(Ordering::Equal))
  348. .collect();
  349. if sorted_data.is_empty() {
  350. return Vec::new();
  351. }
  352. sorted_data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
  353. // Initialize bins
  354. let mut bins: Vec<(V, usize)> = Vec::new();
  355. let mut current_bin_start = sorted_data[0];
  356. let mut count = 0;
  357. for &value in &sorted_data {
  358. if value < current_bin_start + bin_size {
  359. count += 1;
  360. } else {
  361. bins.push((current_bin_start, count));
  362. current_bin_start += bin_size;
  363. while value >= current_bin_start + bin_size {
  364. current_bin_start += bin_size;
  365. }
  366. count = 1;
  367. }
  368. }
  369. // Push the last bin
  370. bins.push((current_bin_start, count));
  371. bins
  372. }
  373. // fn aggregate_data(data: &[(u128, u32)], num_bins: usize) -> Vec<(u32, u32)> {
  374. // if data.is_empty() || num_bins == 0 {
  375. // return vec![];
  376. // }
  377. //
  378. // let bin_size = ((data.len() as f64) / (num_bins as f64)).ceil() as usize;
  379. //
  380. // (0..num_bins)
  381. // .map(|i| {
  382. // let start = i * bin_size;
  383. // let end = (start + bin_size).min(data.len()); // Ensure `end` does not exceed `data.len()`
  384. //
  385. // // If `start` is out of bounds, return (0, 0)
  386. // if start >= data.len() {
  387. // return (0, 0);
  388. // }
  389. //
  390. // let bin = &data[start..end];
  391. //
  392. // let sum_x: u128 = bin.iter().map(|&(x, _)| x).sum();
  393. // let count = bin.len() as u128;
  394. // let mean_x = (sum_x / count) as u32; // Rounded down to nearest u32
  395. //
  396. // let sum_n: u32 = bin.iter().map(|&(_, n)| n).sum();
  397. //
  398. // (mean_x, sum_n)
  399. // })
  400. // .collect()
  401. // }
  402. //
  403. pub fn app_storage_dir() -> anyhow::Result<PathBuf> {
  404. let app_name = env!("CARGO_PKG_NAME");
  405. let app_dir = dirs::data_dir()
  406. .context("Failed to get data directory")?
  407. .join(app_name);
  408. if !app_dir.exists() {
  409. fs::create_dir_all(&app_dir).context("Failed to create application directory")?;
  410. }
  411. Ok(app_dir)
  412. }
  413. use blake3::Hasher as Blake3Hasher;
  414. use std::hash::{BuildHasher, Hasher};
  415. pub struct Blake3Hash(Blake3Hasher);
  416. impl Hasher for Blake3Hash {
  417. fn finish(&self) -> u64 {
  418. let hash = self.0.finalize();
  419. u64::from_le_bytes(hash.as_bytes()[..8].try_into().unwrap())
  420. }
  421. fn write(&mut self, bytes: &[u8]) {
  422. self.0.update(bytes);
  423. }
  424. }
  425. /// Default Hasher
  426. #[derive(Default, Clone)]
  427. pub struct Blake3BuildHasher;
  428. impl BuildHasher for Blake3BuildHasher {
  429. type Hasher = Blake3Hash;
  430. fn build_hasher(&self) -> Self::Hasher {
  431. Blake3Hash(Blake3Hasher::new())
  432. }
  433. }
  434. // Custom 128-bit hash type
  435. #[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Debug, Encode, Decode)]
  436. pub struct Hash128([u8; 16]);
  437. impl std::hash::Hash for Hash128 {
  438. fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
  439. state.write(&self.0);
  440. }
  441. }
  442. impl Hash128 {
  443. pub fn new(bytes: [u8; 16]) -> Self {
  444. Hash128(bytes)
  445. }
  446. pub fn to_bytes(&self) -> [u8; 16] {
  447. self.0
  448. }
  449. }
  450. pub fn get_dir_size(path: &Path) -> std::io::Result<u64> {
  451. let mut total_size = 0;
  452. if path.is_dir() {
  453. for entry in fs::read_dir(path)? {
  454. let entry = entry?;
  455. let path = entry.path();
  456. if path.is_file() {
  457. total_size += path.metadata()?.len();
  458. } else if path.is_dir() {
  459. total_size += get_dir_size(&path)?;
  460. }
  461. }
  462. }
  463. Ok(total_size)
  464. }
  465. /// Finds all files matching the given glob pattern.
  466. ///
  467. /// # Arguments
  468. ///
  469. /// * `pattern` - A glob pattern string (e.g., `"data/**/*.txt"`).
  470. ///
  471. /// # Returns
  472. ///
  473. /// * `Ok(Vec<PathBuf>)` with all successfully matched file paths.
  474. /// * `Err` if the glob pattern is invalid or any matched path fails to resolve.
  475. ///
  476. /// # Errors
  477. ///
  478. /// Returns an error if:
  479. /// - The glob pattern is invalid.
  480. /// - A file path matching the pattern cannot be resolved.
  481. ///
  482. /// # Examples
  483. ///
  484. /// ```rust
  485. /// let files = find_files("src/**/*.rs")?;
  486. /// for file in files {
  487. /// println!("{:?}", file);
  488. /// }
  489. /// ```
  490. pub fn find_files(pattern: &str) -> anyhow::Result<Vec<PathBuf>> {
  491. let mut result = Vec::new();
  492. let entries = glob(pattern).with_context(|| format!("Invalid glob pattern: '{}'", pattern))?;
  493. for entry in entries {
  494. let path =
  495. entry.with_context(|| format!("Failed to resolve path for pattern '{}'", pattern))?;
  496. result.push(path);
  497. }
  498. result.sort();
  499. Ok(result)
  500. }
  501. // fn system_time_to_utc(system_time: SystemTime) -> Option<DateTime<Utc>> {
  502. // system_time
  503. // .duration_since(UNIX_EPOCH)
  504. // .ok()
  505. // .map(|duration| Utc.timestamp(duration.as_secs() as i64, duration.subsec_nanos()))
  506. // }
  507. /// List all files in a directory that have the given suffix (file extension).
  508. ///
  509. /// # Arguments
  510. /// * `dir` – Directory to scan.
  511. /// * `suffix` – File extension to match (without the dot).
  512. ///
  513. /// # Returns
  514. /// A vector of `PathBuf` entries whose extension matches `suffix`.
  515. ///
  516. /// # Errors
  517. /// Returns any I/O error from `read_dir` or directory traversal.
  518. pub fn list_files_with_ext(dir: impl AsRef<Path>, ext: &str) -> std::io::Result<Vec<PathBuf>> {
  519. let mut out = Vec::new();
  520. for entry in fs::read_dir(dir)? {
  521. let entry = entry?;
  522. let path = entry.path();
  523. if path.is_file() {
  524. if let Some(file_ext) = path.extension().and_then(|e| e.to_str()) {
  525. if file_ext == ext {
  526. out.push(path);
  527. }
  528. }
  529. }
  530. }
  531. out.sort();
  532. Ok(out)
  533. }
  534. pub fn list_directories(dir_path: PathBuf) -> std::io::Result<Vec<String>> {
  535. let mut directories = Vec::new();
  536. for entry in fs::read_dir(dir_path)? {
  537. let entry = entry?;
  538. let path = entry.path();
  539. // Check if the path is a directory
  540. if path.is_dir() {
  541. if let Some(dir_name) = path.file_name().and_then(|name| name.to_str()) {
  542. directories.push(dir_name.to_string());
  543. }
  544. }
  545. }
  546. directories.sort();
  547. Ok(directories)
  548. }
  549. pub fn list_files_recursive(root: impl AsRef<Path>) -> Vec<PathBuf> {
  550. fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
  551. let entries = match fs::read_dir(dir) {
  552. Ok(entries) => entries,
  553. Err(e) => {
  554. warn!("Cannot read directory {}: {}", dir.display(), e);
  555. return;
  556. }
  557. };
  558. for entry in entries {
  559. let entry = match entry {
  560. Ok(entry) => entry,
  561. Err(e) => {
  562. warn!("Cannot read directory entry in {}: {}", dir.display(), e);
  563. continue;
  564. }
  565. };
  566. let path = entry.path();
  567. if path.is_dir() {
  568. walk(&path, out);
  569. } else if path.is_file() {
  570. out.push(path);
  571. }
  572. }
  573. }
  574. let mut files = Vec::new();
  575. walk(root.as_ref(), &mut files);
  576. files.sort();
  577. files
  578. }
  579. /// Checks whether the modification time of `file1` is older than `file2`.
  580. ///
  581. /// If `rm` is `true` and `file1` is older, attempts to remove the directory containing `file1`.
  582. ///
  583. /// # Arguments
  584. ///
  585. /// * `file1` - Path to the first file.
  586. /// * `file2` - Path to the second file.
  587. /// * `rm` - If true, and `file1` is older, attempts to remove its parent directory.
  588. ///
  589. /// # Returns
  590. ///
  591. /// * `Ok(true)` if `file1` is older than `file2`.
  592. /// * `Ok(false)` otherwise.
  593. ///
  594. /// # Errors
  595. ///
  596. /// Returns an [`anyhow::Error`] if:
  597. /// - Either `file1` or `file2` does not exist.
  598. /// - File metadata cannot be read.
  599. /// - File modification times cannot be retrieved.
  600. /// - Directory removal fails when `rm == true` and `file1` is older.
  601. ///
  602. pub fn is_file_older(file1: &str, file2: &str, rm: bool) -> anyhow::Result<bool> {
  603. debug!("is_file_older {file1} {file2}, {rm:?}");
  604. let mtime1 = fs::metadata(file1)
  605. .with_context(|| format!("Failed to read metadata for '{}'", file1))?
  606. .modified()
  607. .with_context(|| format!("Failed to get modified time for '{}'", file1))?;
  608. let mtime2 = fs::metadata(file2)
  609. .with_context(|| format!("Failed to read metadata for '{}'", file2))?
  610. .modified()
  611. .with_context(|| format!("Failed to get modified time for '{}'", file2))?;
  612. if mtime1 < mtime2 && rm {
  613. if let Some(file1_dir) = Path::new(file1).parent() {
  614. warn!("Removing old directory: {}", file1_dir.display());
  615. fs::remove_dir_all(file1_dir).map_err(|e| {
  616. warn!("Failed to remove {}: {}", file1_dir.display(), e);
  617. e
  618. })?;
  619. }
  620. }
  621. Ok(mtime1 < mtime2)
  622. }
  623. pub fn remove_dir_if_exists(dir: &str) -> anyhow::Result<()> {
  624. debug!("Trying to remove: {dir}");
  625. match fs::remove_dir_all(dir) {
  626. Ok(_) => {}
  627. Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
  628. Err(e) => anyhow::bail!("Failed to remove directory '{}': {}", dir, e),
  629. }
  630. Ok(())
  631. }
  632. /// Searches a directory for the first file matching a given name pattern.
  633. ///
  634. /// This function looks for a file in the given directory whose filename
  635. /// starts with the provided `starts_with` prefix and ends with the provided
  636. /// `ends_with` suffix. It returns the full path to the first match found.
  637. ///
  638. /// # Arguments
  639. ///
  640. /// * `dir` - A reference to the directory path where the search will occur.
  641. /// * `starts_with` - The required prefix of the filename (e.g., `"throughput_"`).
  642. /// * `ends_with` - The required suffix of the filename (e.g., `".csv"`).
  643. ///
  644. /// # Returns
  645. ///
  646. /// * `Some(PathBuf)` - Path to the first file matching the pattern.
  647. /// * `None` - If no matching file is found or the directory can't be read.
  648. ///
  649. /// # Example
  650. ///
  651. /// ```rust
  652. /// let dir = std::path::Path::new("/path/to/data");
  653. /// if let Some(path) = find_matching_file(dir, "throughput_", ".csv") {
  654. /// println!("Found file: {}", path.display());
  655. /// } else {
  656. /// eprintln!("No matching file found.");
  657. /// }
  658. /// ```
  659. pub fn find_matching_file(dir: &Path, starts_with: &str, ends_with: &str) -> Option<PathBuf> {
  660. let mut matches: Vec<_> = fs::read_dir(dir)
  661. .ok()?
  662. .filter_map(Result::ok)
  663. .map(|entry| entry.path())
  664. .filter(|path| {
  665. path.is_file()
  666. && path
  667. .file_name()
  668. .and_then(|name| name.to_str())
  669. .map(|name| name.starts_with(starts_with) && name.ends_with(ends_with))
  670. .unwrap_or(false)
  671. })
  672. .collect();
  673. matches.sort();
  674. matches.into_iter().next()
  675. }
  676. /// Searches for the first file in the given directory whose file name
  677. /// satisfies the provided condition.
  678. ///
  679. /// # Arguments
  680. ///
  681. /// * `dir` - Path to the directory to search.
  682. /// * `condition` - A closure that takes a file name (`&str`) and returns `true`
  683. /// if the file matches the desired condition.
  684. ///
  685. /// # Returns
  686. ///
  687. /// An `Option<PathBuf>` containing the path to the first matching file,
  688. /// or `None` if no file matches or the directory can't be read.
  689. ///
  690. /// # Examples
  691. ///
  692. /// ```
  693. /// use std::path::Path;
  694. /// let result = find_file(Path::new("."), |name| name.ends_with(".rs"));
  695. /// ```
  696. pub fn find_file<F>(dir: &Path, condition: F) -> Option<PathBuf>
  697. where
  698. F: Fn(&str) -> bool,
  699. {
  700. let mut matches: Vec<_> = fs::read_dir(dir)
  701. .ok()?
  702. .filter_map(Result::ok)
  703. .map(|entry| entry.path())
  704. .filter(|path| {
  705. path.is_file()
  706. && path
  707. .file_name()
  708. .and_then(|name| name.to_str())
  709. .map(&condition)
  710. .unwrap_or(false)
  711. })
  712. .collect();
  713. matches.sort();
  714. matches.into_iter().next()
  715. }
  716. /// Represents a string made of either:
  717. /// - one character repeated N times,
  718. /// - a two-character block repeated N times,
  719. /// - or no valid repetition pattern.
  720. #[derive(Debug, PartialEq)]
  721. pub enum Repeat {
  722. None,
  723. RepOne(char, usize),
  724. RepTwo(String, usize),
  725. }
  726. pub fn detect_repetition(s: &str) -> Repeat {
  727. let len = s.chars().count();
  728. if len == 0 {
  729. return Repeat::None;
  730. }
  731. // Check for single-char repetition
  732. let mut chars = s.chars();
  733. let first = chars.next().unwrap();
  734. if s.chars().all(|c| c == first) {
  735. return Repeat::RepOne(first, len);
  736. }
  737. // Check for two-char block repetition
  738. if len.is_multiple_of(2) {
  739. let mut iter = s.chars();
  740. let a = iter.next().unwrap();
  741. let b = iter.next().unwrap();
  742. let mut count = 1;
  743. while let (Some(c), Some(d)) = (iter.next(), iter.next()) {
  744. if c != a || d != b {
  745. return Repeat::None;
  746. }
  747. count += 1;
  748. }
  749. return Repeat::RepTwo(format!("{}{}", a, b), count);
  750. }
  751. Repeat::None
  752. }
  753. pub fn test_init() {
  754. let _ = env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug"))
  755. .try_init();
  756. }
  757. use regex::Regex;
  758. /// Extracts the numeric barcode suffix from a filename.
  759. ///
  760. /// # Examples
  761. ///
  762. /// ```
  763. /// let f = "sample_SQK-NBD114-24_barcode07.bam";
  764. /// assert_eq!(extract_barcode(f), Some(7));
  765. /// ```
  766. ///
  767. /// Returns `None` if no `barcodeNN` pattern is found
  768. /// or if the number cannot be parsed.
  769. pub fn extract_barcode(name: &str) -> Option<u32> {
  770. static BARCODE_RE: OnceLock<Regex> = OnceLock::new();
  771. let re = BARCODE_RE.get_or_init(|| Regex::new(r"barcode(\d+)").unwrap());
  772. re.captures(name)
  773. .and_then(|c| c.get(1))
  774. .and_then(|m| m.as_str().parse::<u32>().ok())
  775. }
  776. pub fn human_size(bytes: u64) -> String {
  777. const UNITS: [&str; 5] = ["B", "KB", "MB", "GB", "TB"];
  778. let mut size = bytes as f64;
  779. let mut unit = 0;
  780. while size >= 1024.0 && unit < UNITS.len() - 1 {
  781. size /= 1024.0;
  782. unit += 1;
  783. }
  784. if unit == 0 {
  785. format!("{} {}", bytes, UNITS[unit])
  786. } else {
  787. format!("{:.2} {}", size, UNITS[unit])
  788. }
  789. }
  790. /// RAII guard that removes a temporary directory when dropped.
  791. pub struct TempDirGuard {
  792. path: PathBuf,
  793. }
  794. impl TempDirGuard {
  795. pub fn new(path: PathBuf) -> Self {
  796. Self { path }
  797. }
  798. }
  799. impl Drop for TempDirGuard {
  800. fn drop(&mut self) {
  801. if self.path.exists() {
  802. // Log the attempt. Using eprintln/warn! ensures the error is noted,
  803. // but the program does not panic.
  804. if let Err(e) = fs::remove_dir_all(&self.path) {
  805. error!(
  806. "Failed to remove temporary directory '{}': {}",
  807. self.path.display(),
  808. e
  809. );
  810. }
  811. }
  812. }
  813. }
  814. /// Removes a BAM file and its associated index file (`.bam.bai` or `.bai`).
  815. pub fn remove_bam_with_index(bam: &Path) -> anyhow::Result<()> {
  816. if bam.exists() {
  817. fs::remove_file(bam).with_context(|| format!("Failed to remove BAM: {}", bam.display()))?;
  818. }
  819. let bai = bam.with_extension("bam.bai");
  820. if bai.exists() {
  821. fs::remove_file(&bai).ok();
  822. }
  823. let bai_alt = bam.with_extension("bai");
  824. if bai_alt.exists() {
  825. fs::remove_file(&bai_alt).ok();
  826. }
  827. Ok(())
  828. }
  829. /// Guard that tracks temporary filesystem paths and cleans them up on drop
  830. /// unless explicitly disarmed.
  831. ///
  832. /// This is intended for pipeline-style code where intermediate outputs
  833. /// are produced by downstream tools. The guard only tracks paths; it does
  834. /// **not** create files itself.
  835. ///
  836. /// If the pipeline completes successfully, call [`disarm`] to prevent
  837. /// cleanup. Otherwise, all tracked paths are removed on drop on a
  838. /// best-effort basis.
  839. ///
  840. /// # Example
  841. ///
  842. /// ```no_run
  843. /// use std::path::PathBuf;
  844. ///
  845. /// let tmp_dir = PathBuf::from("/tmp");
  846. /// let mut guard = TempFileGuard::new();
  847. ///
  848. /// let path1 = guard.temp_path(".tmp", &tmp_dir);
  849. /// let path2 = guard.temp_path(".tmp", &tmp_dir);
  850. ///
  851. /// // Produce outputs at `path1` and `path2`
  852. ///
  853. /// // Mark success
  854. /// guard.disarm();
  855. /// ```
  856. ///
  857. /// If `disarm()` is not called, both paths are cleaned up automatically
  858. /// when the guard is dropped.
  859. pub struct TempFileGuard {
  860. files: Vec<PathBuf>,
  861. disarmed: AtomicBool,
  862. }
  863. impl Default for TempFileGuard {
  864. fn default() -> Self {
  865. Self::new()
  866. }
  867. }
  868. impl TempFileGuard {
  869. pub fn new() -> Self {
  870. Self {
  871. files: Vec::new(),
  872. disarmed: AtomicBool::new(false),
  873. }
  874. }
  875. /// Registers a temporary file for cleanup.
  876. pub fn track(&mut self, path: PathBuf) {
  877. self.files.push(path);
  878. }
  879. /// Registers multiple temporary files for cleanup.
  880. pub fn track_many(&mut self, paths: impl IntoIterator<Item = PathBuf>) {
  881. self.files.extend(paths);
  882. }
  883. /// Disarms the guard, preventing cleanup on drop.
  884. /// Call this when the pipeline succeeds.
  885. pub fn disarm(&self) {
  886. self.disarmed
  887. .store(true, std::sync::atomic::Ordering::SeqCst);
  888. }
  889. /// Manually clean up all tracked files.
  890. pub fn cleanup(&mut self) {
  891. for file in &self.files {
  892. if let Err(e) = remove_tracked_path(file) {
  893. warn!("Failed to clean up temp file {}: {}", file.display(), e);
  894. }
  895. }
  896. self.files.clear();
  897. }
  898. pub fn clear(&mut self) {
  899. self.files.clear();
  900. }
  901. /// Generates a unique temporary path inside `tmp_dir` and registers it
  902. /// for cleanup.
  903. ///
  904. /// The returned path is derived from a UUIDv4 and **no file is created**.
  905. /// The caller is responsible for creating or writing to the path.
  906. ///
  907. /// # Example
  908. ///
  909. /// ```no_run
  910. /// use std::path::PathBuf;
  911. ///
  912. /// let tmp_dir = PathBuf::from("/tmp");
  913. /// let mut guard = TempFileGuard::new();
  914. ///
  915. /// let path = guard.temp_path(".tmp", &tmp_dir);
  916. /// // create or write to `path`
  917. /// ```
  918. pub fn tmp_path(&mut self, suffix: &str, tmp_dir: impl AsRef<Path>) -> PathBuf {
  919. let name = format!("pandora-tmp-{}{}", Uuid::new_v4(), suffix);
  920. let tmp_dir = tmp_dir.as_ref();
  921. let path = tmp_dir.join(name);
  922. self.track(path.clone());
  923. path
  924. }
  925. }
  926. fn remove_tracked_path(path: &Path) -> anyhow::Result<()> {
  927. if path.is_dir() {
  928. fs::remove_dir_all(path)
  929. .with_context(|| format!("Failed to remove temp directory: {}", path.display()))?;
  930. } else if path.exists() {
  931. fs::remove_file(path)
  932. .with_context(|| format!("Failed to remove temp file: {}", path.display()))?;
  933. }
  934. if path.extension().and_then(|e| e.to_str()) == Some("bam") {
  935. remove_existing_file(path.with_extension("bam.bai"));
  936. remove_existing_file(path.with_extension(".tbi"));
  937. remove_existing_file(path.with_extension("bai"));
  938. }
  939. Ok(())
  940. }
  941. fn remove_existing_file(path: PathBuf) {
  942. if path.exists() {
  943. let _ = fs::remove_file(path);
  944. }
  945. }
  946. impl Drop for TempFileGuard {
  947. fn drop(&mut self) {
  948. if !self.disarmed.load(std::sync::atomic::Ordering::SeqCst) {
  949. warn!(
  950. "Cleaning up {} temporary files :{}",
  951. self.files.len(),
  952. self.files
  953. .iter()
  954. .map(|f| format!("\n\t- {}", f.display()))
  955. .collect::<Vec<String>>()
  956. .join("")
  957. );
  958. self.cleanup();
  959. }
  960. }
  961. }
  962. // pub fn temp_file_path(suffix: &str, config: &Config) -> std::io::Result<PathBuf> {
  963. // let p = config.tmp_dir;
  964. // let temp_path = tempfile::Builder::new()
  965. // .prefix("pandora-temp-")
  966. // .suffix(suffix)
  967. // .rand_bytes(5)
  968. // .tempfile()?
  969. // .into_temp_path();
  970. //
  971. // Ok(temp_path.to_path_buf())
  972. // }
  973. /// Extracts genome sizes from BAM header.
  974. /// Extract contig names and lengths from a BAM header as a hash map.
  975. ///
  976. /// This delegates parsing to [`crate::io::bam::get_genome_sizes`]. Because the
  977. /// return type is a hash map, iteration order is not BAM header order; use
  978. /// [`get_genome_sizes_in_header_order`] when order matters.
  979. pub fn get_genome_sizes(
  980. header: &rust_htslib::bam::Header,
  981. ) -> anyhow::Result<FxHashMap<String, u64>> {
  982. Ok(crate::io::bam::get_genome_sizes(header)?
  983. .into_iter()
  984. .collect())
  985. }
  986. /// Extract contig names and lengths from a BAM header in header order.
  987. ///
  988. /// Use this when downstream region generation must follow the BAM/reference
  989. /// contig order. [`get_genome_sizes`] returns a hash map and does not preserve
  990. /// that order.
  991. pub fn get_genome_sizes_in_header_order(
  992. header: &rust_htslib::bam::HeaderView,
  993. ) -> anyhow::Result<Vec<(String, u64)>> {
  994. (0..header.target_count())
  995. .map(|tid| {
  996. let name = String::from_utf8_lossy(header.tid2name(tid)).into_owned();
  997. let len = header
  998. .target_len(tid)
  999. .with_context(|| format!("BAM header target {name} is missing length"))?;
  1000. Ok((name, len))
  1001. })
  1002. .collect()
  1003. }
  1004. pub fn bam_contigs<P: AsRef<std::path::Path>>(bam: P) -> anyhow::Result<Vec<String>> {
  1005. let reader = rust_htslib::bam::Reader::from_path(&bam)?;
  1006. Ok(reader
  1007. .header()
  1008. .target_names()
  1009. .iter()
  1010. .map(|name| String::from_utf8_lossy(name).into_owned())
  1011. .collect())
  1012. }
  1013. /// Split genome into ~n_parts equal-sized chunks (by number of bases),
  1014. /// returning for each chunk a list of regions in `ctg:start-end` form.
  1015. /// Split genome into ~n_parts equal-sized chunks (by bases).
  1016. /// Each chunk is a single region `ctg:start-end` (no cross-contig regions).
  1017. ///
  1018. /// Note: the number of regions returned will be ≈ n_parts (may differ by 1–2).
  1019. pub fn split_genome_into_n_regions(
  1020. genome_sizes: &FxHashMap<String, u64>,
  1021. n_parts: usize,
  1022. ) -> Vec<String> {
  1023. if n_parts == 0 || genome_sizes.is_empty() {
  1024. return Vec::new();
  1025. }
  1026. // Deterministic contig order
  1027. let mut contigs: Vec<(String, u64)> = genome_sizes
  1028. .iter()
  1029. .map(|(ctg, len)| (ctg.clone(), *len))
  1030. .collect();
  1031. contigs.sort_by(|a, b| a.0.cmp(&b.0));
  1032. let total_bases: u64 = contigs.iter().map(|(_, len)| *len).sum();
  1033. if total_bases == 0 {
  1034. return Vec::new();
  1035. }
  1036. let target_chunk_size: u64 = total_bases.div_ceil(n_parts as u64); // ceil
  1037. let mut regions = Vec::new();
  1038. for (ctg, len) in contigs {
  1039. let mut remaining = len;
  1040. let mut start: u64 = 1;
  1041. while remaining > 0 {
  1042. let take = remaining.min(target_chunk_size);
  1043. let end = start + take - 1;
  1044. regions.push(format!("{ctg}:{start}-{end}"));
  1045. remaining -= take;
  1046. start = end + 1;
  1047. }
  1048. }
  1049. regions
  1050. }
  1051. /// Split genome into exactly `n_parts` chunks with (almost) equal total size.
  1052. /// Each chunk is a *list* of regions `ctg:start-end`. A chunk may span several
  1053. /// contigs, but each region is within a single contig.
  1054. ///
  1055. /// The sizes of the parts will differ by at most 1 base.
  1056. pub fn split_genome_into_n_regions_exact(
  1057. genome_sizes: &FxHashMap<String, u64>,
  1058. n_parts: usize,
  1059. ) -> Vec<Vec<String>> {
  1060. let mut contigs: Vec<(String, u64)> = genome_sizes
  1061. .iter()
  1062. .map(|(ctg, len)| (ctg.clone(), *len))
  1063. .collect();
  1064. contigs.sort_by(|a, b| a.0.cmp(&b.0));
  1065. split_ordered_genome_into_n_regions_exact(&contigs, n_parts)
  1066. }
  1067. pub fn split_ordered_genome_into_n_regions_exact(
  1068. genome_sizes: &[(String, u64)],
  1069. n_parts: usize,
  1070. ) -> Vec<Vec<String>> {
  1071. if n_parts == 0 || genome_sizes.is_empty() {
  1072. return Vec::new();
  1073. }
  1074. let total_bases: u64 = genome_sizes.iter().map(|(_, len)| *len).sum();
  1075. if total_bases == 0 {
  1076. return Vec::new();
  1077. }
  1078. // We cannot have more non-empty parts than bases if we use 1-based inclusive coordinates.
  1079. let parts = n_parts.min(total_bases as usize);
  1080. // Distribute bases as evenly as possible:
  1081. // first `remainder` parts get (base_per_part + 1), the rest get base_per_part.
  1082. let base_per_part = total_bases / parts as u64;
  1083. let remainder = total_bases % parts as u64;
  1084. let target_sizes: Vec<u64> = (0..parts)
  1085. .map(|i| {
  1086. if i < remainder as usize {
  1087. base_per_part + 1
  1088. } else {
  1089. base_per_part
  1090. }
  1091. })
  1092. .collect();
  1093. let mut chunks: Vec<Vec<String>> = Vec::with_capacity(parts);
  1094. let mut part_idx = 0;
  1095. let mut remaining_in_part = target_sizes[0];
  1096. for (ctg, mut len) in genome_sizes.iter().cloned() {
  1097. let mut start: u64 = 1;
  1098. while len > 0 && part_idx < parts {
  1099. let take = len.min(remaining_in_part);
  1100. let end = start + take - 1;
  1101. if chunks.len() <= part_idx {
  1102. chunks.push(Vec::new());
  1103. }
  1104. chunks[part_idx].push(format!("{ctg}:{start}-{end}"));
  1105. len -= take;
  1106. start = end + 1;
  1107. remaining_in_part -= take;
  1108. if remaining_in_part == 0 {
  1109. part_idx += 1;
  1110. if part_idx == parts {
  1111. break;
  1112. }
  1113. remaining_in_part = target_sizes[part_idx];
  1114. }
  1115. }
  1116. }
  1117. // Sanity: we should have exactly `parts` chunks, all non-empty
  1118. debug_assert_eq!(chunks.len(), parts);
  1119. chunks
  1120. }
  1121. /// Convert a numeric value to an RGB triplet using a diverging palette.
  1122. ///
  1123. /// Values are clipped to `[-clip, +clip]` and mapped as:
  1124. /// - negative → blue
  1125. /// - zero → white
  1126. /// - positive → red
  1127. ///
  1128. /// # Arguments
  1129. /// * `v` – value to color (e.g. delta methylation)
  1130. /// * `clip` – symmetric clipping bound (must be > 0)
  1131. ///
  1132. /// # Returns
  1133. /// `(r, g, b)` as 8-bit RGB components.
  1134. ///
  1135. /// # Panics
  1136. /// Panics if `clip <= 0.0`.
  1137. pub fn diverging_rgb(v: f64, clip: f64) -> (u8, u8, u8) {
  1138. assert!(clip > 0.0, "clip must be > 0");
  1139. let x = v.clamp(-clip, clip);
  1140. let t = (x + clip) / (2.0 * clip); // [-clip,+clip] → [0,1]
  1141. if t < 0.5 {
  1142. // blue → white
  1143. let u = t / 0.5;
  1144. let r = (255.0 * u).round() as u8;
  1145. let g = (255.0 * u).round() as u8;
  1146. (r, g, 255)
  1147. } else {
  1148. // white → red
  1149. let u = (t - 0.5) / 0.5;
  1150. let g = (255.0 * (1.0 - u)).round() as u8;
  1151. let b = (255.0 * (1.0 - u)).round() as u8;
  1152. (255, g, b)
  1153. }
  1154. }
  1155. pub fn revcomp(s: &str) -> String {
  1156. fn comp(b: u8) -> u8 {
  1157. match b {
  1158. b'A' => b'T',
  1159. b'C' => b'G',
  1160. b'G' => b'C',
  1161. b'T' => b'A',
  1162. b'N' => b'N',
  1163. _ => b'N',
  1164. }
  1165. }
  1166. let v: Vec<u8> = s
  1167. .as_bytes()
  1168. .iter()
  1169. .rev()
  1170. .map(|&b| comp(b.to_ascii_uppercase()))
  1171. .collect();
  1172. String::from_utf8(v).unwrap()
  1173. }
  1174. pub fn format_count(n: u64) -> String {
  1175. let s = n.to_string();
  1176. let mut result = String::new();
  1177. for (i, c) in s.chars().rev().enumerate() {
  1178. if i > 0 && i % 3 == 0 {
  1179. result.push('_');
  1180. }
  1181. result.push(c);
  1182. }
  1183. result.chars().rev().collect()
  1184. }
  1185. #[cfg(test)]
  1186. mod tests {
  1187. use hashbrown::HashSet;
  1188. use itertools::Itertools;
  1189. use super::*;
  1190. #[test]
  1191. fn par_intersection_keeps_values_only_in_second_outside_first_range() {
  1192. let res = par_intersection(&[10], &[1]);
  1193. assert_eq!(res.common, Vec::<i32>::new());
  1194. assert_eq!(res.only_in_first, vec![10]);
  1195. assert_eq!(res.only_in_second, vec![1]);
  1196. }
  1197. #[test]
  1198. fn par_intersection_handles_duplicate_counts() {
  1199. let res = par_intersection(&[1, 2, 2, 3], &[0, 2, 2, 2, 4]);
  1200. assert_eq!(res.common, vec![2, 2]);
  1201. assert_eq!(res.only_in_first, vec![1, 3]);
  1202. assert_eq!(res.only_in_second, vec![0, 2, 4]);
  1203. }
  1204. #[test]
  1205. fn bin_data_handles_empty_and_invalid_bin_size() {
  1206. assert_eq!(bin_data::<f64>(Vec::new(), 0.1), Vec::<(f64, usize)>::new());
  1207. assert_eq!(bin_data(vec![1.0, 2.0], 0.0), Vec::<(f64, usize)>::new());
  1208. assert_eq!(
  1209. bin_data(vec![1.0, 2.0], f64::NAN),
  1210. Vec::<(f64, usize)>::new()
  1211. );
  1212. assert_eq!(bin_data(vec![f64::NAN], 0.1), Vec::<(f64, usize)>::new());
  1213. }
  1214. #[test]
  1215. fn bin_data_keeps_sparse_values_in_aligned_bins() {
  1216. assert_eq!(bin_data(vec![0.0, 1.0], 0.25), vec![(0.0, 1), (1.0, 1)]);
  1217. }
  1218. #[test]
  1219. fn split_ordered_genome_regions_preserves_input_contig_order() {
  1220. let genome = vec![("chr2".to_string(), 2), ("chr1".to_string(), 2)];
  1221. assert_eq!(
  1222. split_ordered_genome_into_n_regions_exact(&genome, 2),
  1223. vec![vec!["chr2:1-2".to_string()], vec!["chr1:1-2".to_string()]]
  1224. );
  1225. }
  1226. /// Parse `"ctg:start-end"` → `(contig, start, end)` (1-based inclusive).
  1227. fn parse_region(s: &str) -> (&str, u64, u64) {
  1228. let (ctg, range) = s.split_once(':').expect("region missing ':'");
  1229. let (start, end) = range.split_once('-').expect("region missing '-'");
  1230. (
  1231. ctg,
  1232. start.parse().expect("bad start"),
  1233. end.parse().expect("bad end"),
  1234. )
  1235. }
  1236. /// Assert that `parts` cover every base of `genome` exactly once:
  1237. /// - All contigs are present with no extra ones.
  1238. /// - Regions within each contig are sorted, contiguous, start at 1, end at
  1239. /// the declared length.
  1240. /// - No region exceeds its contig's declared length.
  1241. fn assert_full_coverage(parts: &[Vec<String>], genome: &[(String, u64)], label: &str) {
  1242. use std::collections::HashMap;
  1243. let mut by_contig: HashMap<&str, Vec<(u64, u64)>> = HashMap::new();
  1244. for part in parts {
  1245. for region in part {
  1246. let (ctg, start, end) = parse_region(region);
  1247. by_contig.entry(ctg).or_default().push((start, end));
  1248. }
  1249. }
  1250. assert_eq!(
  1251. by_contig.len(),
  1252. genome.len(),
  1253. "[{label}] got {} contigs, expected {}",
  1254. by_contig.len(),
  1255. genome.len()
  1256. );
  1257. for (ctg, expected_len) in genome {
  1258. let ranges = by_contig
  1259. .get(ctg.as_str())
  1260. .unwrap_or_else(|| panic!("[{label}] contig {ctg} missing from output"));
  1261. let mut sorted = ranges.clone();
  1262. sorted.sort_unstable();
  1263. assert_eq!(
  1264. sorted[0].0, 1,
  1265. "[{label}] {ctg}: first region starts at {} (expected 1)",
  1266. sorted[0].0
  1267. );
  1268. assert_eq!(
  1269. sorted.last().unwrap().1,
  1270. *expected_len,
  1271. "[{label}] {ctg}: last region ends at {} (expected {expected_len})",
  1272. sorted.last().unwrap().1
  1273. );
  1274. for i in 1..sorted.len() {
  1275. assert_eq!(
  1276. sorted[i].0,
  1277. sorted[i - 1].1 + 1,
  1278. "[{label}] {ctg}: gap between {:?} and {:?}",
  1279. sorted[i - 1],
  1280. sorted[i]
  1281. );
  1282. }
  1283. for &(start, end) in &sorted {
  1284. assert!(
  1285. end <= *expected_len,
  1286. "[{label}] {ctg}: region {start}-{end} exceeds contig length {expected_len}"
  1287. );
  1288. }
  1289. }
  1290. }
  1291. #[test]
  1292. fn split_g_covers_full_genome() {
  1293. let max_rec = 3;
  1294. // Synthetic genome with varied contig sizes; no file I/O needed.
  1295. let genome: Vec<(String, u64)> = vec![
  1296. ("chr1".to_string(), 248_956_422),
  1297. ("chr2".to_string(), 242_193_529),
  1298. ("chr3".to_string(), 198_295_559),
  1299. ("chr4".to_string(), 190_214_555),
  1300. ("chrX".to_string(), 156_040_895),
  1301. ("chrY".to_string(), 57_227_415),
  1302. ("chrM".to_string(), 16_569),
  1303. ];
  1304. let total_bases: u64 = genome.iter().map(|(_, l)| l).sum();
  1305. let mut rec = 0;
  1306. let mut regions_hash = HashSet::new();
  1307. while rec < max_rec {
  1308. for n_parts in [1, 2, 3, 4, 7, 10, 60, 100] {
  1309. let parts = split_ordered_genome_into_n_regions_exact(&genome, n_parts);
  1310. let r = regions_hash.insert(parts.iter().flatten().join(", "));
  1311. if rec > 0 && r {
  1312. panic!("Not deterministic !");
  1313. }
  1314. let label = format!("n_parts={n_parts}");
  1315. // Correct number of parts (capped at total_bases when n_parts is huge)
  1316. let expected_parts = n_parts.min(total_bases as usize);
  1317. assert_eq!(
  1318. parts.len(),
  1319. expected_parts,
  1320. "[{label}] wrong number of parts"
  1321. );
  1322. // All parts non-empty
  1323. assert!(
  1324. parts.iter().all(|p| !p.is_empty()),
  1325. "[{label}] at least one empty part"
  1326. );
  1327. // Full coverage with no gaps or overlaps
  1328. assert_full_coverage(&parts, &genome, &label);
  1329. // Even distribution: sizes differ by at most 1 base
  1330. let sizes: Vec<u64> = parts
  1331. .iter()
  1332. .map(|p| {
  1333. p.iter()
  1334. .map(|r| {
  1335. let (_, s, e) = parse_region(r);
  1336. e - s + 1
  1337. })
  1338. .sum()
  1339. })
  1340. .collect();
  1341. let max = *sizes.iter().max().unwrap();
  1342. let min = *sizes.iter().min().unwrap();
  1343. assert!(
  1344. max - min <= 1,
  1345. "[{label}] uneven distribution: max={max}, min={min}"
  1346. );
  1347. }
  1348. rec += 1;
  1349. }
  1350. }
  1351. }