lib.rs 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. //! # 🧬 Long-read Somatic Variant Calling and Analysis Framework
  2. //!
  3. //! This Rust library provides a modular, parallelizable framework for somatic variant calling, annotation, and interpretation from long-read sequencing data. It is designed to support full pipelines for research and clinical workflows across multiple variant callers and analysis stages.
  4. //!
  5. //! The library also serves as an extensible platform that developers can leverage to add custom features, integrate new tools, and tailor workflows to specific use cases.
  6. //!
  7. //! ## 🧩 Key Features
  8. //!
  9. //! - **POD5 Demultiplexing and Alignment**: End-to-end support for processing ONT POD5 files:
  10. //! - Barcode-aware demultiplexing using metadata CSVs
  11. //! - POD5 subsetting and organization by case
  12. //! - Integration with basecallers (e.g., Dorado) for read alignment
  13. //! - **Pipeline Management**: Full orchestration of Dockerized execution pipelines for tools such as ClairS, Nanomonsv, DeepVariant, Savana, Modkit, and Severus.
  14. //! - **Flexible Configuration**: Centralized configuration system (`Config`, `CollectionsConfig`) for all modules and pipelines.
  15. //! - **Input Abstraction**: Unified handling of BAM, POD5, and VCF file collections across cohorts and directories.
  16. //! - **Variant Processing**: Modular loading, filtering, statistical analysis, and annotation of somatic and germline variants.
  17. //! - **Haplotype Phasing and Methylation**: Support for LongPhase-based phasing and Modkit methylation pileups with support for multi-threaded pileup and aggregation.
  18. //! - **Parallel Execution**: Uses `rayon` for efficient multicore parallelization over large cohorts and tasks.
  19. //!
  20. //! ## 📚 Module Highlights
  21. //!
  22. //! - `callers`: Interfaces to variant calling tools (ClairS, DeepVariant, Nanomonsv, Savana, etc...)
  23. //! - `runners`: Pipeline runners (e.g. `Somatic`, `SeverusSolo`, `LongphasePhase`) that manage end-to-end execution.
  24. //! - `collection`: Organizes input data across BAMs, VCFs, and POD5 files with auto-detection of completed runs.
  25. //! - `annotation`: VEP line parsing and high-level annotation aggregation.
  26. //! - `pipes`: Composition modules for executing pipelines across callers and post-processing steps.
  27. //! - `functions`: Custom logic for genome assembly, entropy estimation, and internal tooling.
  28. //! - `positions`, `variant`, `helpers`: Utilities for SV modeling, variant filtering, position overlap logic, and helper methods.
  29. //!
  30. //! ## ⚡ Workflow Overview
  31. //!
  32. //! ### 1. 📦 From POD5 to BAM Alignment
  33. //!
  34. //! - **Demultiplexing**: POD5 files are subset and demuxed using barcodes (via CSV metadata).
  35. //! - **Flowcell Case Management**: Each sample is identified by a [`collection::pod5::FlowCellCase`] containing its ID, time point, and POD5 directory.
  36. //! - **Alignment**: The [`commands::dorado::Dorado`] module handles alignment of POD5 reads to reference genome, producing BAMs.
  37. //!
  38. //! ```rust
  39. //! let case = FlowCellCase { id: "PATIENT1", time_point: "diag", barcode: "01", pod_dir: "...".into() };
  40. //! Dorado::init(case, Config::default())?.run_pipe()?;
  41. //! ```
  42. //!
  43. //! ### 2. 🧬 Variant Calling (BAM ➝ VCF)
  44. //!
  45. //! Using the aligned BAMs, multiple variant callers can be run in parallel. The [`callers`] and [`runners`] modules support:
  46. //!
  47. //! - **ClairS** – somatic small variant calling with LongPhase haplotagging
  48. //! - **Nanomonsv** – structural variants (SV)
  49. //! - **DeepVariant** – germline small variants
  50. //! - **Savana** – SVs and copy number variations (CNV)
  51. //! - **Modkit** – methylation pileups
  52. //! - **LongPhase** – phasing and modcalling
  53. //!
  54. //! All workflows can be triggered per-case or per-cohort using `Collections` or `Somatic` runners.
  55. //!
  56. //! ```rust
  57. //! ClairS::initialize("PATIENT1", Config::default())?.run()?;
  58. //! NanomonSV::initialize("PATIENT1", Config::default())?.run()?;
  59. //! ```
  60. //!
  61. //! ### 3. 📈 Aggregation & Statistics (VCF ➝ JSON / Stats)
  62. //!
  63. //! After variant calling:
  64. //!
  65. //! - Annotate with VEP ([`annotation`] module)
  66. //! - Load and filter with [`variant::variant_collection`]
  67. //! - Compute variant and region-level stats (e.g., mutation rates, alteration categories, coding overlaps)
  68. //!
  69. //! ```rust
  70. //! let variants = Variants::load_from_json("/path/to/somatic_variants.json.gz")?;
  71. //! let stats = VariantsStats::new(&variants, "PATIENT1", &config)?;
  72. //! stats.save_to_json("/output/path/stats.json.gz")?;
  73. //! ```
  74. //!
  75. //! ### 4. 🧠 Intelligent Task Management (`collection` module)
  76. //!
  77. //! - Auto-discovers available samples, POD5s, BAMs, and VCFs
  78. //! - Detects missing outputs and creates task lists
  79. //! - Tasks are parallelizable using Rayon and can be run on-demand
  80. //!
  81. //! ```rust
  82. //! let mut collections = Collections::new(CollectionsConfig::default())?;
  83. //! collections.todo()?; // Identify missing steps
  84. //! collections.run()?; // Run them automatically
  85. //! ```
  86. //!
  87. //! ## 🔬 Testing
  88. //!
  89. //! Integration tests demonstrate the entire pipeline. Run with logging enabled:
  90. //!
  91. //! ```bash
  92. //! export RUST_LOG=debug
  93. //! cargo test -- --nocapture
  94. //! ```
  95. //!
  96. //! ## 🧪 Example Use Cases
  97. //!
  98. //! - Full somatic variant calling pipeline on matched tumor/normal samples
  99. //! - POD5-based pipeline from raw signal to variants
  100. //! - Aggregation and annotation of SVs across a clinical cohort
  101. //! - Methylation analysis using nanopore-specific tools
  102. //! - Variant calling and analysis in large-scale longitudinal studies
  103. //!
  104. //! ## 🚀 Getting Started
  105. //!
  106. //! All workflows are initialized from `Config` and driven by the `Collections` structure:
  107. //!
  108. //! ```rust
  109. //! let collections = Collections::new(CollectionsConfig::default())?;
  110. //! collections.todo()?;
  111. //! collections.run()?;
  112. //! ```
  113. //!
  114. //! ## 🔗 References
  115. //!
  116. //! **Basecalling and alignment**
  117. //! - Dorado: <https://github.com/nanoporetech/dorado>
  118. //!
  119. //! **Variants Callers**
  120. //! - ClairS: <https://github.com/HKU-BAL/ClairS>
  121. //! - Nanomonsv: <https://github.com/friend1ws/nanomonsv>
  122. //! - Savana: <https://github.com/cortes-ciriano-lab/savana>
  123. //! - DeepVariant: <https://github.com/google/deepvariant>
  124. //! - DeepSomatic: <https://github.com/google/deepsomatic>
  125. //! - LongPhase: <https://github.com/PorubskyResearch/LongPhase>
  126. //! - Modkit: <https://github.com/nanoporetech/modkit>
  127. //!
  128. //! **Variants annotation**
  129. //! - VEP: <https://www.ensembl.org/info/docs/tools/vep/index.html>
  130. //!
  131. //! ---
  132. use std::sync::{Arc, Mutex};
  133. pub mod commands;
  134. pub mod config;
  135. pub mod modkit;
  136. pub mod callers;
  137. pub mod runners;
  138. pub mod collection;
  139. pub mod functions;
  140. pub mod helpers;
  141. pub mod variant;
  142. pub mod io;
  143. pub mod pipes;
  144. pub mod positions;
  145. pub mod annotation;
  146. pub mod cases;
  147. pub mod scan;
  148. pub mod math;
  149. #[macro_use]
  150. extern crate lazy_static;
  151. // Define DOCKER_ID lock for handling Docker kill when ctrlc is pressed
  152. lazy_static! {
  153. static ref DOCKER_ID: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
  154. }
  155. #[cfg(test)]
  156. mod tests {
  157. use std::{collections::HashMap, fs, path::Path};
  158. use annotation::{vep::{VepLine, VEP}, Annotations};
  159. use callers::{nanomonsv::nanomonsv_create_pon, savana::{Savana, SavanaReadCounts}, severus::{Severus, SeverusSolo}};
  160. use collection::{bam::{counts_at, counts_ins_at, nt_pileup, WGSBam, WGSBamStats}, Initialize, InitializeSolo, Version};
  161. use commands::{longphase::{LongphaseConfig, LongphaseHap, LongphaseModcallSolo, LongphasePhase}, modkit::{bed_methyl, ModkitConfig}};
  162. use functions::assembler::{Assembler, AssemblerConfig};
  163. use helpers::estimate_shannon_entropy;
  164. use io::bed::read_bed;
  165. use itertools::Itertools;
  166. use log::{debug, error, info, warn};
  167. use pandora_lib_variants::variants::VariantCategory;
  168. use pipes::somatic::SomaticPipe;
  169. use positions::{overlaps_par, GenomePosition, GenomeRange};
  170. use rayon::prelude::*;
  171. use runners::Run;
  172. use variant::{variant::{Variants, VcfVariant}, variant_collection};
  173. use self::{collection::pod5::{FlowCellCase, Pod5Collection}, commands::dorado, config::Config};
  174. use super::*;
  175. use crate::{annotation::Annotation, callers::{clairs::ClairS, deep_variant::DeepVariant, nanomonsv::{NanomonSV, NanomonSVSolo}, savana::SavanaCN}, collection::{bam::{self, nt_pileup_new}, flowcells::{scan_archive, FlowCells}, run_tasks, vcf::VcfCollection, Collections, CollectionsConfig, ShouldRun}, commands::dorado::Dorado, helpers::find_files, io::{bed::bedrow_overlaps_par, dict::read_dict, gff::features_ranges}, pipes::somatic::const_stats, positions::{merge_overlapping_genome_ranges, range_intersection_par, sort_ranges}, scan::scan::somatic_scan, variant::{variant::{AlterationCategory, BNDDesc, BNDGraph, GroupByThreshold, ToBNDGraph}, variant_collection::{group_variants_by_bnd_desc, group_variants_by_bnd_rc, Variant, VariantCollection}, variants_stats::{self, somatic_depth_quality_ranges, VariantsStats}}};
  176. // export RUST_LOG="debug"
  177. fn init() {
  178. let _ = env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
  179. .is_test(true)
  180. .try_init();
  181. }
  182. #[test]
  183. fn it_works() {
  184. let bam_path = "/data/longreads_basic_pipe/PARACHINI/diag/PARACHINI_diag_hs1.bam";
  185. modkit::modkit(bam_path);
  186. }
  187. #[test]
  188. fn run_dorado() -> anyhow::Result<()> {
  189. let case = FlowCellCase {
  190. id: "CONSIGNY".to_string(),
  191. time_point: "mrd".to_string(), barcode: "07".to_string(), pod_dir: "/data/run_data/20240326-CL/CONSIGNY-MRD-NB07_RICCO-DIAG-NB08/20240326_1355_1E_PAU78333_bc25da25/pod5_pass/barcode07".into()
  192. };
  193. dorado::Dorado::init(case, Config::default())?.run_pipe()
  194. }
  195. #[test]
  196. fn pod5() -> anyhow::Result<()> {
  197. let _ = env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
  198. .build();
  199. let coll = Pod5Collection::new(
  200. "/data/run_data",
  201. "/data/flow_cells.tsv",
  202. "/data/longreads_basic_pipe",
  203. )?;
  204. println!("{coll:#?}");
  205. // let runs = Runs::import_dir("/home/prom/store/banana-pool/run_data", "/data/flow_cells.tsv")?;
  206. Ok(())
  207. }
  208. #[test]
  209. fn bam() -> anyhow::Result<()> {
  210. init();
  211. let bam_collection = bam::load_bam_collection("/data/longreads_basic_pipe");
  212. bam_collection
  213. .bams
  214. .iter()
  215. // .filter(|b| matches!(b.bam_type, BamType::Panel(_)))
  216. .for_each(|b| println!("{b:#?}"));
  217. let u = bam_collection.get("PARACHINI", "mrd");
  218. println!("{u:#?}");
  219. Ok(())
  220. }
  221. #[test]
  222. fn vcf() -> anyhow::Result<()> {
  223. init();
  224. let mut vcf_collection = VcfCollection::new("/data/longreads_basic_pipe");
  225. vcf_collection.sort_by_id();
  226. vcf_collection
  227. .vcfs
  228. .iter()
  229. .for_each(|v| v.println().unwrap());
  230. Ok(())
  231. }
  232. // pod5 view -I /data/run_data/20240903-CL/ARMEM-DG-N02_ASSJU-DG-N03/20240903_1428_1B_PAW47629_fc24c3cf/pod5/PAW47629_fc24c3cf_77b07847_0.pod5 | head -5000 | awk '{if(NR==1){print "target,"$0}else{print "subset_1.pod5,"$0}}' > /tmp/subset_ids.csv
  233. // pod5 subset /data/run_data/20240903-CL/ARMEM-DG-N02_ASSJU-DG-N03/20240903_1428_1B_PAW47629_fc24c3cf/pod5/PAW47629_fc24c3cf_77b07847_0.pod5 --csv /tmp/subset_ids.csv -o /data/test_suite/pod5/muxed/
  234. #[test]
  235. fn mux() -> anyhow::Result<()> {
  236. init();
  237. let result_dir = "/data/test_suite/results".to_string();
  238. let cases = vec![
  239. FlowCellCase { id: "test_02".to_string(), time_point: "diag".to_string(), barcode: "02".to_string(), pod_dir: "/data/test_suite/pod5/muxed".into() },
  240. FlowCellCase { id: "test_03".to_string(), time_point: "diag".to_string(), barcode: "03".to_string(), pod_dir: "/data/test_suite/pod5/muxed".into() },
  241. ];
  242. cases.iter().for_each(|c| {
  243. let dir = format!("{result_dir}/{}", c.id);
  244. if Path::new(&dir).exists() {
  245. fs::remove_dir_all(dir).unwrap();
  246. }
  247. });
  248. let config = Config { result_dir, ..Default::default() };
  249. Dorado::from_mux(cases, config)
  250. }
  251. // #[test_log::test]
  252. // fn clairs() -> anyhow::Result<()> {
  253. // let config = ClairSConfig {
  254. // result_dir: "/data/test".to_string(),
  255. // ..ClairSConfig::default()
  256. // };
  257. // ClairS::new("test_a", "/data/test_data/subset.bam", "/data/test_data/subset_mrd.bam", config).run()
  258. // }
  259. #[test]
  260. fn nanomonsv() -> anyhow::Result<()> {
  261. init();
  262. let id = "HAMROUNE";
  263. NanomonSV::initialize(id, Config::default())?.run()
  264. }
  265. #[test]
  266. fn nanomddonsv_solo() -> anyhow::Result<()> {
  267. init();
  268. NanomonSVSolo::initialize("BRETON", "diag", Config::default())?.run()
  269. }
  270. // cargo test run -- --nocapture; ~/run_scripts/notify_finish.sh &
  271. #[test]
  272. fn todo_all() -> anyhow::Result<()> {
  273. init();
  274. // let config = CollectionsConfig::default();
  275. let config = CollectionsConfig { pod_dir: "/data/run_data".to_string(), ..Default::default() };
  276. info!("Runing todo with config: {:#?}", config);
  277. let mut collections = Collections::new(config)?;
  278. collections.todo()?;
  279. collections.tasks.iter().for_each(|t| println!("{t}"));
  280. println!("{}", collections.tasks.len());
  281. Ok(())
  282. }
  283. // #[test]
  284. // fn todo_agg() -> anyhow::Result<()> {
  285. // init();
  286. // let config = CollectionsConfig::default();
  287. // info!("Runing todo with config: {:#?}", config);
  288. // let collections = Collections::new(config)?;
  289. // let agg_tasks = collections.todo_variants_agg()?;
  290. // println!("{:#?}", agg_tasks);
  291. // println!("{}", agg_tasks.len());
  292. // Ok(())
  293. // }
  294. // #[test]
  295. // fn run_agg() -> anyhow::Result<()> {
  296. // init();
  297. // let config = CollectionsConfig {
  298. // id_black_list: vec!["MANCUSO".to_string(),"HAMROUNE".to_string()],
  299. // ..Default::default()
  300. // };
  301. // info!("Runing todo with config: {:#?}", config);
  302. // let mut collections = Collections::new(config)?;
  303. // collections.tasks = collections.todo_variants_agg()?;
  304. // collections.run()?;
  305. //
  306. // Ok(())
  307. // }
  308. // export RUST_LOG="debug"
  309. #[test]
  310. fn run_t() -> anyhow::Result<()> {
  311. init();
  312. // let config = CollectionsConfig::default();
  313. let config = CollectionsConfig { pod_dir: "/data/run_data".to_string(), ..Default::default() };
  314. run_tasks(config)
  315. }
  316. // #[test_log::test]
  317. // fn bcftools_pass() {
  318. // let config = BcftoolsConfig::default();
  319. // let id = "RICCO";
  320. // let time = "diag";
  321. // let caller = "DeepVariant";
  322. //
  323. // Config::default();
  324. //
  325. // // let (i, o) =
  326. // // let i = format!("/data/longreads_basic_pipe/{id}/{time}/nanomonsv/{id}_diag.nanomonsv.result.vcf");
  327. // // let o = format!("/data/longreads_basic_pipe/{id}/{time}/nanomonsv/{id}_diag_nanomonsv_PASSED.vcf.gz");
  328. // bcftools_keep_pass(&i, &o, config).unwrap();
  329. // }
  330. #[test]
  331. fn bam_ok() -> anyhow::Result<()> {
  332. init();
  333. let collections = Collections::new(
  334. CollectionsConfig::default()
  335. )?;
  336. let mut res: Vec<_> = collections.bam.by_id_completed(15.0, 10.0).iter().map(|b| {
  337. (b.id.to_string(), b.time_point.to_string(), b.path.to_str().unwrap().to_string())
  338. }).collect();
  339. res.sort_by_key(|b| b.1.clone());
  340. res.sort_by_key(|b| b.0.clone());
  341. res.iter().for_each(|(id, tp, path)| println!("{id}\t{tp}\t{path}"));
  342. Ok(())
  343. }
  344. #[test]
  345. fn todo_assembler() -> anyhow::Result<()> {
  346. init();
  347. let collections = Collections::new(
  348. CollectionsConfig::default()
  349. )?;
  350. collections.todo_assembler()?;
  351. Ok(())
  352. }
  353. #[test]
  354. fn sv_pon() -> anyhow::Result<()> {
  355. init();
  356. nanomonsv_create_pon(&Config::default(), "/data/ref/hs1/nanomonsv_pon.vcf.gz")
  357. }
  358. #[test]
  359. fn todo_mod() -> anyhow::Result<()> {
  360. init();
  361. let collections = Collections::new(
  362. CollectionsConfig::default()
  363. )?;
  364. collections.todo_mod_pileup();
  365. Ok(())
  366. }
  367. #[test]
  368. fn todo_deepv() -> anyhow::Result<()> {
  369. init();
  370. let collections = Collections::new(
  371. CollectionsConfig::default()
  372. )?;
  373. let tasks = collections.todo_deepvariants();
  374. tasks.iter().for_each(|t| info!("{t}"));
  375. info!("n tasks {}", tasks.len());
  376. Ok(())
  377. }
  378. #[test]
  379. fn todo_clairs() -> anyhow::Result<()> {
  380. init();
  381. let collections = Collections::new(
  382. CollectionsConfig::default()
  383. )?;
  384. collections.todo_clairs().iter().for_each(|t| info!("{t}"));
  385. Ok(())
  386. }
  387. #[test]
  388. fn run_assemblers() -> anyhow::Result<()> {
  389. Assembler::new("CAMEL".to_string(), "diag".to_string(), AssemblerConfig::default()).run()
  390. }
  391. // #[test]
  392. // fn run_dmr_par() -> anyhow::Result<()> {
  393. // init();
  394. // let collections = Collections::new(
  395. // CollectionsConfig::default()
  396. // )?;
  397. // let tasks = collections.todo_dmr_c_diag_mrd();
  398. // tasks.iter().for_each(|t| info!("{t}"));
  399. // let len = tasks.len();
  400. // // let pool = ThreadPoolBuilder::new().num_threads(10).build().unwrap();
  401. // // pool.install(|| {
  402. // // tasks.par_iter().enumerate().for_each(|(i, t)| {
  403. // // let config = ModkitConfig {threads: 2, ..Default::default() };
  404. // // if let collection::CollectionsTasks::DMRCDiagMrd { id, .. } = t { let _ = dmr_c_mrd_diag(id, &config); }
  405. // // println!("⚡ {i}/{len}");
  406. // // });
  407. // // });
  408. // Ok(())
  409. // }
  410. #[test]
  411. fn run_mod_par() -> anyhow::Result<()> {
  412. init();
  413. let collections = Collections::new(
  414. CollectionsConfig::default()
  415. )?;
  416. let tasks = collections.todo_mod_pileup();
  417. let len = tasks.len();
  418. tasks.par_iter().enumerate().for_each(|(i, t)| {
  419. let config = ModkitConfig {threads: 2, ..Default::default() };
  420. if let collection::CollectionsTasks::ModPileup { bam, .. } = t { let _ = bed_methyl(bam.to_owned(), &config); }
  421. println!("⚡ {i}/{len}");
  422. });
  423. Ok(())
  424. }
  425. #[test]
  426. fn run_severus() -> anyhow::Result<()> {
  427. init();
  428. Severus::initialize("CAMEL", Config::default())?.run()
  429. }
  430. #[test]
  431. fn run_severus_solo() -> anyhow::Result<()> {
  432. init();
  433. SeverusSolo::initialize("CAMEL","diag", Config::default())?.run()
  434. }
  435. #[test]
  436. fn run_savana() -> anyhow::Result<()> {
  437. init();
  438. let collections = Collections::new(
  439. CollectionsConfig::default()
  440. )?;
  441. for bam in collections.bam.by_id_completed(15.0, 10.0).iter() {
  442. let id = &bam.id;
  443. match ClairS::initialize(id, Config::default())?.run() {
  444. Ok(_) => match Savana::initialize(id, Config::default())?.run() {
  445. Ok(_) => (),
  446. Err(e) => error!("{e}"),
  447. },
  448. Err(e) => error!("{e}"),
  449. }
  450. ;
  451. }
  452. Ok(())
  453. }
  454. #[test]
  455. fn check_versions() -> anyhow::Result<()> {
  456. init();
  457. let config = Config::default();
  458. let v = Savana::version(&config)?;
  459. info!("Savanna version {v}");
  460. let v = Severus::version(&config)?;
  461. info!("Severus version {v}");
  462. Ok(())
  463. }
  464. #[test]
  465. fn run_multi_deepvariant() -> anyhow::Result<()> {
  466. init();
  467. let mut collections = Collections::new(
  468. CollectionsConfig::default()
  469. )?;
  470. collections.run_deepvariant()
  471. }
  472. #[test]
  473. fn run_deepvariant() -> anyhow::Result<()> {
  474. init();
  475. DeepVariant::initialize("HAMROUNE", "diag", Config::default())?.run()
  476. }
  477. #[test]
  478. fn run_clairs() -> anyhow::Result<()> {
  479. init();
  480. ClairS::initialize("ADJAGBA", Config::default())?.run()
  481. }
  482. #[test]
  483. fn run_longphase() -> anyhow::Result<()> {
  484. init();
  485. let id = "BECERRA";
  486. let diag_bam = format!("/data/longreads_basic_pipe/{id}/diag/{id}_diag_hs1.bam");
  487. let vcf = format!("/data/longreads_basic_pipe/{id}/diag/ClairS/clair3_normal_tumoral_germline_output.vcf.gz");
  488. let mrd_bam = format!("/data/longreads_basic_pipe/{id}/mrd/{id}_mrd_hs1.bam");
  489. LongphaseHap::new(id, &diag_bam, &vcf, LongphaseConfig::default()).run()?;
  490. LongphaseHap::new(id, &mrd_bam, &vcf, LongphaseConfig::default()).run()
  491. }
  492. #[test]
  493. fn run_longphase_modcall() -> anyhow::Result<()> {
  494. init();
  495. let id = "ADJAGBA";
  496. let time = "diag";
  497. LongphaseModcallSolo::initialize(id, time, Config::default())?.run()
  498. }
  499. #[test]
  500. fn run_longphase_phase() -> anyhow::Result<()> {
  501. init();
  502. let id = "ADJAGBA";
  503. LongphasePhase::initialize(id, Config::default())?.run()
  504. }
  505. #[test]
  506. fn snv_parse() -> anyhow::Result<()> {
  507. init();
  508. // ClairS
  509. let row = "chr1\t10407\t.\tA\tG\t10.1\tPASS\tF\tGT:GQ:DP:AD:AF\t0/1:10:31:10,20:0.6452";
  510. let variant: VcfVariant = row.parse()?;
  511. let var_string = variant.into_vcf_row();
  512. assert_eq!(row, &var_string);
  513. let mut variant_col = VariantCollection {
  514. variants: vec![variant],
  515. vcf: collection::vcf::Vcf::new("/data/longreads_basic_pipe/ACHITE/diag/ClairS/ACHITE_diag_clairs_PASSED.vcf.gz".into())?,
  516. caller: Annotation::Callers(annotation::Caller::ClairS, annotation::Sample::Somatic)
  517. };
  518. let annotations = Annotations::default();
  519. variant_col.annotate_with_constit_bam(&annotations, "/data/longreads_basic_pipe/ACHITE/mrd/ACHITE_mrd_hs1.bam", 1)?;
  520. // DeepVariant
  521. let row = "chr1\t10407\t.\tA\tG\t7.4\tPASS\t.\tGT:GQ:DP:AD:VAF:PL\t1/1:5:9:1,8:0.888889:5,5,0";
  522. variant_col.variants.push(row.parse()?);
  523. let anns: Vec<Vec<Annotation>> = variant_col.variants.iter()
  524. .filter_map(|e| {
  525. annotations.store.get(&e.hash()).map(|v| v.value().to_owned())
  526. })
  527. .collect();
  528. assert_eq!(anns[0], anns[1]);
  529. Ok(())
  530. }
  531. #[test]
  532. fn deletion_parse() -> anyhow::Result<()> {
  533. init();
  534. // Clairs
  535. let row = "chr1\t16760\t.\tAaag\tA\t8.48\tPASS\tF\tGT:GQ:DP:AD:AF\t0/1:8:39:22,17:0.4359";
  536. let variant: VcfVariant = row.parse()?;
  537. let var_string = variant.into_vcf_row();
  538. // case are not keeped
  539. assert_eq!(&var_string, "chr1\t16760\t.\tAAAG\tA\t8.48\tPASS\tF\tGT:GQ:DP:AD:AF\t0/1:8:39:22,17:0.4359");
  540. assert_eq!(AlterationCategory::DEL, variant.alteration_category());
  541. let mut variant_col = VariantCollection {
  542. variants: vec![variant],
  543. vcf: collection::vcf::Vcf::new("/data/longreads_basic_pipe/ACHITE/diag/ClairS/ACHITE_diag_clairs_PASSED.vcf.gz".into())?,
  544. caller: Annotation::Callers(annotation::Caller::ClairS, annotation::Sample::Somatic)
  545. };
  546. let annotations = Annotations::default();
  547. // DeepVariant, VAF is not an f32 at it should be, sometimes too long removed the last number for eq
  548. let row = "chr12\t326911\t.\tGTGTA\tG\t4.7\tPASS\t.\tGT:GQ:DP:AD:VAF:PL\t0/1:5:8:5,3:0.37500:2,0,21";
  549. let variant: VcfVariant = row.parse()?;
  550. let var_string = variant.into_vcf_row();
  551. assert_eq!(row, &var_string);
  552. variant_col.variants.push(row.parse()?);
  553. assert_eq!(AlterationCategory::DEL, variant.alteration_category());
  554. // Severus, 0000 added to VAF and hVAF
  555. let row = "chr10\t108974982\tseverus_DEL885\tN\t<DEL>\t60\tPASS\tPRECISE;SVTYPE=DEL;SVLEN=474;END=108975456;STRANDS=+-;INSIDE_VNTR=TRUE;MAPQ=60\tGT:GQ:VAF:hVAF:DR:DV\t0/1:61:0.30000:0.30000,0.00000,0.00000:7:3";
  556. let variant: VcfVariant = row.parse()?;
  557. let var_string = variant.into_vcf_row();
  558. assert_eq!(row, &var_string);
  559. assert_eq!(AlterationCategory::DEL, variant.alteration_category());
  560. variant_col.variants.push(row.parse()?);
  561. // Nanomonsv dont parse last format remove: \t22:0 and nt putted in uppercase
  562. let row = "chr14\t5247080\td_106\tG\t<DEL>\t.\tPASS\tEND=5247255;SVTYPE=DEL;SVLEN=-175;SVINSLEN=39;SVINSSEQ=ATAACCCAGGTGATATAACACTTCTTTAGGCTCTGCCTA\tTR:VR\t18:3";
  563. let variant: VcfVariant = row.parse()?;
  564. let var_string = variant.into_vcf_row();
  565. assert_eq!(row, &var_string);
  566. assert_eq!(AlterationCategory::DEL, variant.alteration_category());
  567. variant_col.variants.push(row.parse()?);
  568. let row = "chr1\t20654667\t.\tTG\tT\t3.5\tPASS\t.\tGT:GQ:DP:AD:VAF:PL\t0/1:3:23:20,3:0.13044:0,0,16";
  569. let variant: VcfVariant = row.parse()?;
  570. let var_string = variant.into_vcf_row();
  571. assert_eq!(row, &var_string);
  572. assert_eq!(AlterationCategory::DEL, variant.alteration_category());
  573. variant_col.variants.push(row.parse()?);
  574. let row = "chr1\t2160094\t.\tTCTGACAGCCTGGAACAGCACCCACAACCGCAGGTGAGCATCTGACAGCCCGCAGCAGCACCCACACGCACAGGTGAGAATCTGACAGCCCGGAGCAGCACCCACACAGGCAGGGGAGCATCTGACATCCTGGAGCAGCACCGACAACCCCAGGTGAGCAACTGAGAGCCTGGAACAGCACCCACACCCCCAGGTGAGAATCTGACAGCCTGGAAGAGCACCCCACATCCCCGGGTGAGCATCTGACAGCCTGGAACAGCATCAACACCCCCAGGTGAGCATCCGATAGCCTGGAGCAGCACCCACACCCTCAGGTGAGCATCTGACAGCCTGGAACAGCAACCACACCCCCAGGTGAACATCTGACAGCCCGGAGCAGCACCCACACCCCCAGGTGAGCATCTGACAGCCTGGAACAGCACCCACACCCCCAGGAGAGCATCCGGTAGCCTGGAGCAGAACCCACACCCACAGGCGAGCATCTGACAGCCTGGGTTGGCACCCACACCCCCAGGTGAGCATCTGATGGTCTGGAGCAGCACCCACACCTACAGGAGAGCATCTGACAACCTGCAACAGAACCCAAACCCCCAGGTGAGCATCTGACAGACTGGAACAGCACCCTGCACCCCCAGGTGAGTATCTGACGGCCTGGAACAGAACACACAAGCCCAGGTGAGCATCCGACAGCCTGGAGCAGCACCCACACCCCCAGGTGAGCATCAGACAGCCTGGAGTAGCACCCCACACCCCCAGGTGAGCATCCGACAGCCTGGAGCAGCACCCACACTCACCAGGTGAGCATGTGACATGCTGGAACAGCACCCACACCCCCAGGCGAGCATCTGACAGCCTGGAGCGGCACCCCACACCCCCAGGTGAGCATCGGACAGCCTGGAGCAGTACCCACACCCCCAGGTGAGCATCCGACAGCCTGGAACAGCACCCACACCCCCAGGTGAGCATCTGACAGACAGGAACAGCACCCACATGTCCAGGTGAGCCTCTGACAGACTGGAACAGCACGCGCACCCCCAGGTGAGCATCTGACAGGCTGGAACAGCACCCACACCCCCAGGAGAGCATCTTACAGCATGTAACAGCACCCACACACCCACGTGAGCATCTGACAGCCTGGAACAGCACCCTGCACCCCCAGGTGCGCACGTGACAGCCTGGAACAGCACCCACACCCCCAGGCGAGCATCGGACAGCCTGGAGCAGCACCCCACACCCGCAGGTGAGCATCCGACAGCCTGGAGCAGCACCCACACCCCCAGGTGAGCATGTGACAGCCTGGAACAGCACCCACACCCCCAGGCGAGCATCTGACAGCCTGGAGCAACACCCCACACCCCCAGGTGAGCATCGAACTGTCTGGAGCAGCACCCACAACCACAGGTGGGCATCGGAGAGAGTGGAGCAGCGCCCAGACCACCAGGCAAGCATCTGACAGCCTGGAGCAGTGCCCACACCCCCATGTGAGCATCTGACAGTATGGAGCAGCACCCACAGCCCAAGGTGAGCATCTGACAACCTGGAGCAGCACCCACACCCCCAGGCGAGCATCTGAACGCACAGAGCAGCACCCACACCCCCAGGCGAGCATCCGACAGCGTGGAGCAGCACCCACACTCCCAGGTGCGCGTGTGATGGTCTGGGGCAGCACCCACACACACAGGTGAGCCTGCGACAGCCTGGAGCAGCACCCACAGCCCCAGGTGAGCATCTGATGGTCTGGAGCAGCACCCACAACCACAGGTGAGCATCGGAGAGACTGGAGCAGCGCCGAAACCCCCAGGCGAACATTTGAGAGCCTGGAGCAGTGCCGACACCCCCAGGTGAGCATCTGACACCGTGGAGCAGCACCCACAGCCCAAGGTGAGCATCTGACAACCTGGAGCAGCACCCACAGCCCCAGGCGAGCATTTGAACGCACGGAGCAGGACCTACAGCCCCAGGCGAGCATCCGACAGCCTGGAGCAGCACCCACACACCCAGGTGAGCATCTGACAGCCTGGAGCAGCACCCACAACCCCAGGGGAGCATCTGACCGCATGGAATGGCATCCTCACCCGTAGGTGAACGTCCGACAGCCTGGAGCAGCACCCACACCCCCAGGTGAGCATCTGACAGCCTGGAACAGCACCTGCACCCCCGGGTGAGGATCAGATAGCCTGGAGCAGCACCCACACTCCAGGTGAGCATCTGACAGCCTGAAGCAGCACCCACACCAACAGGTGAGCATCTGACAGCCTGGAAGAGCACCCACAACCCCACGTGAGCATCTGACAGCCTGGAACAGGACCCTGCACCCCAAAGTGAGCATCTGACAACCTGGAGCAGGAACCACAACCCCAGGTGAGCATCTGATAGCCTGGAATAGCACCCACACACCCAGGTGAGCATCTGAGAGCCTGGAGCAGCACCCACACCCCCAGGTGAGCATCCGACAGCCTGGAACAGTACCCACACACCCAGGCGAGCATCTGACAGCCTGAAACAGCACACACACTCCCAGGTGAGCATCTCATATGCTGGAACAGCACCCACACCCCCAGGTGAGCATCTGACTGCCCGGAGCAGCACGCACACCCCCGGGTGAGCATCTGATAGCCTGGAACAGCACCCACACCCCCAGATGAGCATCCGACAGCCTGGAGCGGAGCCCACAGCCCCAGGCGAGCATCTGACAGCCTGGAACAGCACCCTGCATCCCCGGGTGAGGATCAGACAGCCTGGAGCAGCACCCACACTCCAGGTGAGCATCTGACAGCCTGAAGCAGCACCCACACCAACAGGTGAGCCTCTGACAGCCTGCAACAGCACCCACACCCCAAGGTGAGCATCTGACAGCCTGGAAGAGGACCCTGCAGCCCCAAGTGAGCATCTGACAACCTGAAGCAGCAACCACACTCCAAGGTGAGCATCCAACAGCCTGGAACAGCACCCACACACCCAGGTGAGCATCTGACAGCCTGGAGCAGCACCACACCCCCAGGTGAGCATCCGACAGCCTGGAACAGCACCTACAAACCCAGGAGAGCATCCGACAGCCTGGAGCAGCACCCACACCCCCAGGCGAGCATCTGACAGCCCGGAGCAGCACGCAAACCCCCAGGTGAGCATCTGATACCCTGGAACAGCACCCACACACCCAGGTGAGCATCCGACAGCCTAGAGCTGAACCCACACCAACAGGAGAGCATCTGACAGCCTGGGTCGGCACCCACACCCCCAGGTGAGCATCTGACAGCCTGGAACAGCACCCTGCACCCTCAGGTGCGCACGTGACAGCCTGGAACAGCACCCACACACCCAGGCGAGCATCTGACGGCCTGGAACGGCACCCACACCCCGAGGCGAGCATGGGACAGCCTGGAGAGCAGCCACACCCTCAGATGAGCATCTGACAGCCTGGAACAGCACCCTGCACCCCCAGGTGAGCATCTGACAGCCTGGAACAGGACCCACGTCCCCAGGCGAGCAA\tT\t3.1\tPASS\t.\tGT:GQ:DP:AD:VAF:PL\t0/1:3:15:11,4:0.26667:0,0,22";
  575. let variant: VcfVariant = row.parse()?;
  576. let var_string = variant.into_vcf_row();
  577. assert_eq!(row, &var_string);
  578. assert_eq!(AlterationCategory::DEL, variant.alteration_category());
  579. variant_col.variants.push(row.parse()?);
  580. let row = "chr1\t27073397\t.\tTGGGG\tT\t28.9\tPASS\t.\tGT:GQ:DP:AD:VAF:PL\t./.:2:13:1,3:0.23077:24,2,26";
  581. let variant: VcfVariant = row.parse()?;
  582. let var_string = variant.into_vcf_row();
  583. assert_eq!(row, &var_string);
  584. assert_eq!(AlterationCategory::DEL, variant.alteration_category());
  585. variant_col.variants.push(row.parse()?);
  586. let row = "chrY\t639648\t.\tCTTTTTT\tC\t10.4\tPASS\t.\tGT:GQ:DP:AD:VAF:PL\t0/1:1:7:0,2:0.28571:4,0,2";
  587. let variant: VcfVariant = row.parse()?;
  588. let var_string = variant.into_vcf_row();
  589. assert_eq!(row, &var_string);
  590. assert_eq!(AlterationCategory::DEL, variant.alteration_category());
  591. variant_col.variants.push(row.parse()?);
  592. let row = "chr11\t31260259\tseverus_BND1077_1\tN\t]chr11:36214171]N\t60\tPASS\tPRECISE;SVTYPE=BND;SVLEN=4953912;MATE_ID=severus_BND1077_2;STRANDS=+-;MAPQ=60\tGT:GQ:VAF:hVAF:DR:DV\t0/1:68:0.67000:0.67000,0.00000,0.00000:4:8";
  593. let variant: VcfVariant = row.parse()?;
  594. let var_string = variant.into_vcf_row();
  595. assert_eq!(row, &var_string);
  596. assert_eq!(AlterationCategory::DEL, variant.alteration_category());
  597. println!("{:#?}", variant.deletion_desc());
  598. variant_col.variants.push(row.parse()?);
  599. variant_col.annotate_with_constit_bam(
  600. &annotations,
  601. "/data/longreads_basic_pipe/ACHITE/mrd/ACHITE_mrd_hs1.bam",
  602. 1
  603. )?;
  604. Ok(())
  605. }
  606. #[test]
  607. fn insertion_parse() -> anyhow::Result<()> {
  608. init();
  609. // Clairs
  610. let row = "chr1\t23005\t.\tT\tTCAC\t3.1\tPASS\tF\tGT:GQ:DP:AD:A\t0/1:3:13:9,4:0.3077";
  611. let variant: VcfVariant = row.parse()?;
  612. let var_string = variant.into_vcf_row();
  613. assert_eq!(&var_string, row);
  614. assert_eq!(AlterationCategory::INS, variant.alteration_category());
  615. let mut variant_col = VariantCollection {
  616. variants: vec![variant],
  617. vcf: collection::vcf::Vcf::new("/data/longreads_basic_pipe/ACHITE/diag/ClairS/ACHITE_diag_clairs_PASSED.vcf.gz".into())?,
  618. caller: Annotation::Callers(annotation::Caller::ClairS, annotation::Sample::Somatic)
  619. };
  620. let annotations = Annotations::default();
  621. // DeepVariant, VAF is not an f32 at it should be, sometimes too long removed the last number for eq
  622. let row = "chrX\t152732993\t.\tG\tGTTTTTTTTT\t18.9\tPASS\t.\tGT:GQ:DP:AD:VAF:PL\t1/1:3:6:0,5:0.50000:15,7,3";
  623. let variant: VcfVariant = row.parse()?;
  624. let var_string = variant.into_vcf_row();
  625. assert_eq!(row, &var_string);
  626. variant_col.variants.push(row.parse()?);
  627. assert_eq!(AlterationCategory::INS, variant.alteration_category());
  628. // Severus, 0000 added to VAF and hVAF
  629. let row = "chr11\t26669932\tseverus_INS9403\tN\tT\t60\tPASS\tPRECISE;SVTYPE=INS;SVLEN=460;INSIDE_VNTR=TRUE;MAPQ=60\tGT:GQ:VAF:hVAF:DR:DV\t0/1:71:0.27000:0.27000,0.00000,0.00000:8:3";
  630. let variant: VcfVariant = row.parse()?;
  631. let var_string = variant.into_vcf_row();
  632. assert_eq!(row, &var_string);
  633. assert_eq!(AlterationCategory::INS, variant.alteration_category());
  634. variant_col.variants.push(row.parse()?);
  635. // Nanomonsv dont parse last format remove: \t22:0 and nt putted in uppercase
  636. let row = "chr8\t87940084\td_333\tT\t<INS>\t.\tPASS\tEND=87940201;SVTYPE=INS;SVINSLEN=172;SVINSSEQ=TA\tTR:VR\t9:5";
  637. let variant: VcfVariant = row.parse()?;
  638. let var_string = variant.into_vcf_row();
  639. assert_eq!(row, &var_string);
  640. assert_eq!(AlterationCategory::INS, variant.alteration_category());
  641. variant_col.variants.push(row.parse()?);
  642. variant_col.annotate_with_constit_bam(
  643. &annotations,
  644. "/data/longreads_basic_pipe/ACHITE/mrd/ACHITE_mrd_hs1.bam",
  645. 1
  646. )?;
  647. Ok(())
  648. }
  649. #[test]
  650. fn trl_parse() -> anyhow::Result<()> {
  651. init();
  652. let row = "chr2\t207968575\tID_16420_1\ta\ta]chr11:41497080]\t.\tPASS\tSVTYPE=BND;MATEID=ID_16420_2;TUMOUR_READ_SUPPORT=4;TUMOUR_ALN_SUPPORT=4;NORMAL_READ_SUPPORT=0;NORMAL_ALN_SUPPORT=0;SVLEN=0;BP_NOTATION=++;SOURCE=SUPPLEMENTARY;CLUSTERED_READS_TUMOUR=4;CLUSTERED_READS_NORMAL=0;ORIGIN_STARTS_STD_DEV=0.433;ORIGIN_MAPQ_MEAN=56.25;ORIGIN_EVENT_SIZE_STD_DEV=0;ORIGIN_EVENT_SIZE_MEDIAN=0;ORIGIN_EVENT_SIZE_MEAN=0;END_STARTS_STD_DEV=17.754;END_MAPQ_MEAN=56.25;END_EVENT_SIZE_STD_DEV=0;END_EVENT_SIZE_MEDIAN=0;END_EVENT_SIZE_MEAN=0;TUMOUR_DP_BEFORE=8,11;TUMOUR_DP_AT=4,11;TUMOUR_DP_AFTER=4,11;NORMAL_DP_BEFORE=7,16;NORMAL_DP_AT=7,16;NORMAL_DP_AFTER=7,16;TUMOUR_AF=1,0.364;NORMAL_AF=0,0;TUMOUR_TOTAL_HP_AT=1,3,0;NORMAL_TOTAL_HP_AT=4,3,0;TUMOUR_ALT_HP=2,1,1;TUMOUR_PS=207946665;NORMAL_ALT_HP=0,0,0;CLASS=PREDICTED_SOMATIC\tGT\t0/1";
  653. let variant: VcfVariant = row.parse()?;
  654. let var_string = variant.into_vcf_row();
  655. let u = variant.n_alt_depth();
  656. println!("{u:?}");
  657. Ok(())
  658. }
  659. #[test]
  660. fn variant_parse() -> anyhow::Result<()> {
  661. let row = "chr1\t1366\t.\tC\tCCCT\t8.2\tPASS\t.\tGT:GQ:DP:AD:VAF:PL\t1/1:4:6:1,4:0.66667:6,4,0";
  662. let variant: VcfVariant = row.parse()?;
  663. let var_string = variant.into_vcf_row();
  664. assert_eq!(row, &var_string);
  665. let row = "chr1\t1366\t.\tC\tCCCT\t8.2\tPASS\t.";
  666. let variant: VcfVariant = row.parse()?;
  667. let var_string = variant.into_vcf_row();
  668. assert_eq!(row, &var_string);
  669. let row = "chr1\t2628434\t.\tC\tT\t17.973\tPASS\tH;FAU=0;FCU=7;FGU=0;FTU=7;RAU=0;RCU=2;RGU=0;RTU=2\tGT:GQ:DP:AF:AD:NAF:NDP:NAD:AU:CU:GU:TU:NAU:NCU:NGU:NTU\t0/1:17:18:0.5:0,9:0:11:0,0:0:9:0:9:0:11:0:0";
  670. let variant: VcfVariant = row.parse()?;
  671. let var_string = variant.into_vcf_row();
  672. assert_eq!(row, &var_string);
  673. let row = "chr1\t52232\t.\tC\tCT\t18\t.\t.\tGT:GQ:DP:AD:AF\t1/.:1:24:3,5:0.208333";
  674. let variant: VcfVariant = row.parse()?;
  675. let var_string = variant.into_vcf_row();
  676. assert_eq!(row, &var_string);
  677. let row = "chr1\t52232\t.\tC\tCT\t18\t.\t.\tGT:GQ:DP:AD:AF\t1/1:1:24:3,5:0.208333";
  678. let variant_b: VcfVariant = row.parse()?;
  679. assert_eq!(variant, variant_b);
  680. let row = "chr1\t475157\t.\tA\tG\t12.301\tPASS\tH;FAU=2;FCU=0;FGU=2;FTU=0;RAU=3;RCU=0;RGU=3;RTU=0\tGT:GQ:DP:AF:AD:NAF:NDP:NAD:AU:CU:GU:TU:NAU:NCU:NGU:NTU\t0/1:12:10:0.5:0,5:0.0769:13:0,1:5:0:5:0:12:0:1:0";
  681. let variant: VcfVariant = row.parse()?;
  682. let var_string = variant.into_vcf_row();
  683. assert_eq!(row, &var_string);
  684. let row = "chr1\t161417408\tr_10_0\tT\t[chr1:161417447[TTGGCAGGTTCC\t.\tPASS\tSVTYPE=BND;MATEID=r_10_1;SVINSLEN=11;SVINSSEQ=TTGGCAGGTTC\tTR:VR\t22:3\t12:0";
  685. let variant: VcfVariant = row.parse()?;
  686. println!("{variant:#?}");
  687. let u = variant.bnd_desc();
  688. println!("{u:#?}");
  689. // Severus mates are not in RC
  690. let vcf = "chr7\t27304522\tseverus_BND6747_1\tN\t[chr6:32688062[N\t60\tPASS\tPRECISE;SVTYPE=BND;MATE_ID=severus_BND6747_2;STRANDS=--;MAPQ=60;CLUSTERID=severus_2\tGT:VAF:hVAF:DR:DV\t0/1:0.29:0.29,0,0:12:5";
  691. let variant: VcfVariant = vcf.parse()?;
  692. let bnd_a = variant.bnd_desc()?;
  693. let vcf = "chr6\t32688062\tseverus_BND6747_2\tN\t[chr7:27304522[N\t60\tPASS\tPRECISE;SVTYPE=BND;MATE_ID=severus_BND6747_1;STRANDS=--;MAPQ=60;CLUSTERID=severus_2 GT:VAF:hVAF:DR:DV\t0/1:0.29:0.29,0,0:12:5";
  694. let variant: VcfVariant = vcf.parse()?;
  695. let bnd_b = variant.bnd_desc()?;
  696. assert_eq!(bnd_a, bnd_b.rc());
  697. println!("{bnd_a}\n{bnd_b}");
  698. // Savana here each mate are in RC
  699. let vcf = "chr10\t102039096\tID_35957_2\tG\t]chr10:101973386]G\t.\tPASS\tSVTYPE=BND;MATEID=ID_35957_1;TUMOUR_READ_SUPPORT=7;TUMOUR_ALN_SUPPORT=7;NORMAL_READ_SUPPORT=0;NORMAL_ALN_SUPPORT=0;SVLEN=65710;BP_NOTATION=+-;SOURCE=SUPPLEMENTARY;CLUSTERED_READS_TUMOUR=7;CLUSTERED_READS_NORMAL=0;ORIGIN_STARTS_STD_DEV=0.35;ORIGIN_MAPQ_MEAN=60;ORIGIN_EVENT_SIZE_STD_DEV=7.248;ORIGIN_EVENT_SIZE_MEDIAN=65710;ORIGIN_EVENT_SIZE_MEAN=65705.4;END_STARTS_STD_DEV=7.007;END_MAPQ_MEAN=60;END_EVENT_SIZE_STD_DEV=7.248;END_EVENT_SIZE_MEDIAN=65710;END_EVENT_SIZE_MEAN=65705.4;TUMOUR_DP_BEFORE=38,29;TUMOUR_DP_AT=44,21;TUMOUR_DP_AFTER=44,21;NORMAL_DP_BEFORE=13,15;NORMAL_DP_AT=13,15;NORMAL_DP_AFTER=13,15;TUMOUR_AF=0.159,0.333;NORMAL_AF=0,0;TUMOUR_TOTAL_HP_AT=20,16,8;NORMAL_TOTAL_HP_AT=6,7,0;TUMOUR_ALT_HP=0,1,6;TUMOUR_PS=101917152;NORMAL_ALT_HP=0,0,0;CLASS=PREDICTED_SOMATIC\tGT\t0/1";
  700. let variant: VcfVariant = vcf.parse()?;
  701. let bnd_a = variant.bnd_desc()?;
  702. let vcf = "chr10\t101973386\tID_35957_1\tA\tA[chr10:102039096[\t.\tPASS\tSVTYPE=BND;MATEID=ID_35957_2;TUMOUR_READ_SUPPORT=7;TUMOUR_ALN_SUPPORT=7;NORMAL_READ_SUPPORT=0;NORMAL_ALN_SUPPORT=0;SVLEN=65710;BP_NOTATION=+-;SOURCE=SUPPLEMENTARY;CLUSTERED_READS_TUMOUR=7;CLUSTERED_READS_NORMAL=0;ORIGIN_STARTS_STD_DEV=0.35;ORIGIN_MAPQ_MEAN=60;ORIGIN_EVENT_SIZE_STD_DEV=7.248;ORIGIN_EVENT_SIZE_MEDIAN=65710;ORIGIN_EVENT_SIZE_MEAN=65705.4;END_STARTS_STD_DEV=7.007;END_MAPQ_MEAN=60;END_EVENT_SIZE_STD_DEV=7.248;END_EVENT_SIZE_MEDIAN=65710;END_EVENT_SIZE_MEAN=65705.4;TUMOUR_DP_BEFORE=29,38;TUMOUR_DP_AT=21,44;TUMOUR_DP_AFTER=21,44;NORMAL_DP_BEFORE=15,13;NORMAL_DP_AT=15,13;NORMAL_DP_AFTER=15,13;TUMOUR_AF=0.333,0.159;NORMAL_AF=0,0;TUMOUR_TOTAL_HP_AT=17,0,4;NORMAL_TOTAL_HP_AT=5,7,3;TUMOUR_ALT_HP=0,6,1;TUMOUR_PS=101917152;NORMAL_ALT_HP=0,0,0;CLASS=PREDICTED_SOMATIC\tGT\t0/1";
  703. let variant: VcfVariant = vcf.parse()?;
  704. let bnd_b = variant.bnd_desc()?;
  705. assert_eq!(bnd_a, bnd_b);
  706. println!("{bnd_a}\n{bnd_b}");
  707. // Deletions
  708. // Severus
  709. let vcf = "chr7\t143674704\tseverus_DEL7318\tN\t<DEL>\t60\tPASS≥\tPRECISE;SVTYPE=DEL;SVLEN=3642;END=143678346;STRANDS=+-;MAPQ=60\tGT:GQ:VAF:hVAF:DR:DV\t0/1:114:0.39:0.39,0,0:14:9";
  710. let variant: VcfVariant = vcf.parse()?;
  711. println!("{:?}", variant.infos);
  712. println!("{:?}", variant.formats);
  713. let del = variant.deletion_desc().unwrap();
  714. println!("{:?}", del);
  715. println!("{:?} {:?}", del.len(), variant.formats.n_alt_depth());
  716. assert_eq!("chr7:143674704_143678346_del", variant.deletion_desc().unwrap().to_string());
  717. println!("--\n");
  718. let vcf="chr7\t144003249\tr_106\tC\t<DEL>\t.\tPASS\tEND=144142733;SVTYPE=DEL;SVLEN=-139484;SVINSLEN=4;SVINSSEQ=GCCA\tTR:VR\t12:10\t51:0";
  719. let variant: VcfVariant = vcf.parse()?;
  720. println!("{:?}", variant.infos);
  721. println!("{:?}", variant.formats);
  722. let del = variant.deletion_desc().unwrap();
  723. println!("{:?}", del);
  724. println!("{:?} {:?}", del.len(), variant.formats.n_alt_depth());
  725. let path = "/data/ref/hs1/chm13v2.0_RefSeq_Liftoff_v5.1_Genes.bed";
  726. let r = read_bed(path)?;
  727. let deleted_genes = bedrow_overlaps_par(&r, &vec![&GenomeRange { contig: variant.position.contig, range: del.start..del.end }]).into_iter().filter_map(|e| {
  728. e.name
  729. }).collect::<Vec<String>>().join(", ");
  730. println!("{deleted_genes}");
  731. Ok(())
  732. }
  733. #[test]
  734. fn variant_load_deepvariant() -> anyhow::Result<()> {
  735. init();
  736. let id = "ADJAGBA";
  737. let time = "diag";
  738. let dv = DeepVariant::initialize(id, time, Config::default())?;
  739. let annotations = Annotations::default();
  740. let variant_collection = dv.variants(&annotations)?;
  741. println!("Deepvariant for {id} {time}: variants {} {}", variant_collection.variants.len(), variant_collection.vcf.n_variants);
  742. Ok(())
  743. }
  744. #[test]
  745. fn variant_load_clairs() -> anyhow::Result<()> {
  746. init();
  747. let id = "ADJAGBA";
  748. let clairs = ClairS::initialize(id, Config::default())?;
  749. let annotations = Annotations::default();
  750. let variant_collection = clairs.variants(&annotations)?;
  751. println!("ClairS for {id}: variants {} {}", variant_collection.variants.len(), variant_collection.vcf.n_variants);
  752. Ok(())
  753. }
  754. #[test]
  755. fn variant_load_nanomonsv() -> anyhow::Result<()> {
  756. init();
  757. let id = "ADJAGBA";
  758. let nanomonsv = NanomonSV::initialize(id, Config::default())?;
  759. let annotations = Annotations::default();
  760. let variant_collection = nanomonsv.variants(&annotations)?;
  761. println!("NanomonSV for {id}: variants {} {}", variant_collection.variants.len(), variant_collection.vcf.n_variants);
  762. println!("{:?}", variant_collection.variants.first());
  763. Ok(())
  764. }
  765. #[test]
  766. fn variant_load_clairs_germline() -> anyhow::Result<()> {
  767. init();
  768. let id = "ADJAGBA";
  769. let clairs = ClairS::initialize(id, Config::default())?;
  770. let annotations = Annotations::default();
  771. let germline_variant_collection = clairs.germline(&annotations)?;
  772. println!("ClairS for {id}: variants {} {}", germline_variant_collection.variants.len(), germline_variant_collection.vcf.n_variants);
  773. Ok(())
  774. }
  775. #[test]
  776. fn pipe_somatic() -> anyhow::Result<()> {
  777. init();
  778. let collections = Collections::new(
  779. CollectionsConfig::default()
  780. )?;
  781. let c = Config {
  782. somatic_pipe_force: true,
  783. ..Default::default()
  784. };
  785. for (a, _) in collections.bam_pairs().iter() {
  786. if ["AUBERT", "BAFFREAU", "BAILLEUL"].contains(&a.id.as_str()) {
  787. continue;
  788. }
  789. if let Err(e) = SomaticPipe::initialize(&a.id, c.clone()).map(|mut p| if p.should_run() {
  790. if let Err(e) = p.run() {
  791. error!("{e}");
  792. }
  793. }) {
  794. error!("{e}");
  795. }
  796. }
  797. Ok(())
  798. // let id = "VILI";
  799. // SomaticPipe::initialize(id, Config::default())?.run()
  800. }
  801. #[test]
  802. fn overlaps() {
  803. init();
  804. let positions = vec![
  805. &GenomePosition { contig: 1, position: 100 },
  806. &GenomePosition { contig: 1, position: 150 },
  807. &GenomePosition { contig: 1, position: 200 },
  808. &GenomePosition { contig: 2, position: 150 },
  809. ];
  810. let ranges = vec![
  811. &GenomeRange { contig: 1, range: 50..150 },
  812. &GenomeRange { contig: 2, range: 100..200 },
  813. ];
  814. let parallel_overlapping_indices = overlaps_par(&positions, &ranges);
  815. assert_eq!(parallel_overlapping_indices, vec![0, 3])
  816. }
  817. #[test]
  818. fn bed_read() -> anyhow::Result<()> {
  819. init();
  820. let path = &Config::default().mask_bed("ADJAGBA");
  821. let r = read_bed(path)?;
  822. println!("{}", r.len());
  823. Ok(())
  824. }
  825. #[test]
  826. fn test_read_dict() -> anyhow::Result<()> {
  827. init();
  828. let genome = read_dict(&Config::default().dict_file)?;
  829. let genome_length: usize = genome.into_iter().map(|(_, len)| len as usize).sum();
  830. println!("{genome_length}");
  831. Ok(())
  832. }
  833. #[test]
  834. fn bases_at() -> anyhow::Result<()> {
  835. init();
  836. let id = "ADJAGBA";
  837. let c = Config::default();
  838. let chr = "chr3";
  839. let position = 62416039; // 1-based
  840. let mut bam = rust_htslib::bam::IndexedReader::from_path(c.solo_bam(id, "diag"))?;
  841. let p = nt_pileup(&mut bam, chr, position - 1, false)?.iter().map(|e| String::from_utf8(vec![*e]).unwrap()).collect::<Vec<_>>();
  842. let mut counts = HashMap::new();
  843. for item in p.iter() {
  844. *counts.entry(item.as_str()).or_insert(0) += 1;
  845. }
  846. for (key, value) in &counts {
  847. println!("{}: {}", key, value);
  848. }
  849. assert_eq!(8, *counts.get("C").unwrap());
  850. assert_eq!(13, *counts.get("G").unwrap());
  851. assert_eq!(6, *counts.get("D").unwrap());
  852. let chr = "chr1";
  853. let position = 3220; // 1-based
  854. let mut bam = rust_htslib::bam::IndexedReader::from_path(c.solo_bam(id, "mrd"))?;
  855. let p = counts_at(&mut bam, chr, position - 1)?;
  856. println!("{p:#?}");
  857. Ok(())
  858. }
  859. #[test]
  860. fn seq_at() -> anyhow::Result<()> {
  861. init();
  862. let c = Config::default();
  863. let chr = "chr1";
  864. let position = 16761;
  865. let mut fasta_reader = noodles_fasta::indexed_reader::Builder::default().build_from_path(c.reference)?;
  866. let r = io::fasta::sequence_at(&mut fasta_reader, chr, position, 3)?;
  867. println!("{r} ({} {:.2})", r.len(), estimate_shannon_entropy(r.as_str()));
  868. Ok(())
  869. }
  870. #[test]
  871. fn ins_at() -> anyhow::Result<()> {
  872. init();
  873. let id = "ADJAGBA";
  874. let c = Config::default();
  875. let chr = "chr1";
  876. let position = 52232; // 1-based like in vcf
  877. let mut bam = rust_htslib::bam::IndexedReader::from_path(c.solo_bam(id, "mrd"))?;
  878. // let p = ins_pileup(&mut bam, chr, position - 1, true)?.iter().map(|e| String::from_utf8(vec![*e]).unwrap()).collect::<Vec<_>>();
  879. let counts = counts_ins_at(&mut bam, chr, position -1)?;
  880. for (key, value) in &counts {
  881. println!("{}: {}", key, value);
  882. }
  883. Ok(())
  884. }
  885. #[test]
  886. fn vep_line() -> anyhow::Result<()> {
  887. init();
  888. let line = "chr2_1922358_-/T\tchr2:1922357-1922358\tT\tMYT1L\tNM_001303052.2\tTranscript\tintron_variant\t-\t-\t-\t-\t-\t-\tIMPACT=MODIFIER;STRAND=-1;SYMBOL=MYT1L;SOURCE=chm13v2.0_RefSeq_Liftoff_v5.1_sorted.gff3.gz;HGVSc=NM_001303052.2:c.1619-613dup";
  889. let vep_line: VepLine = line.parse()?;
  890. println!("{vep_line:#?}");
  891. let vep: VEP = VEP::try_from(&vep_line)?;
  892. println!("{vep:#?}");
  893. Ok(())
  894. }
  895. #[test]
  896. fn savana_cn() -> anyhow::Result<()> {
  897. init();
  898. let id = "CAMARA";
  899. // let s = SavanaCopyNumber::load_id(id, Config::default())?;
  900. let s = SavanaReadCounts::load_id(id, Config::default())?;
  901. println!("tumoral reads: {}", s.n_tumoral_reads());
  902. println!("normal reads: {}", s.n_normal_reads());
  903. println!("tumoral:\n{:#?}", s.norm_chr_counts());
  904. Ok(())
  905. }
  906. #[test]
  907. fn coverage() -> anyhow::Result<()> {
  908. init();
  909. let id = "CAMARA";
  910. let time = "diag";
  911. WGSBamStats::new(id, time, Config::default())?.print();
  912. Ok(())
  913. }
  914. #[test]
  915. fn load_bam() -> anyhow::Result<()> {
  916. init();
  917. let id = "ADJAGBA";
  918. let time = "diag";
  919. let bam_path = Config::default().solo_bam(id, time);
  920. WGSBam::new(Path::new(&bam_path).to_path_buf())?;
  921. Ok(())
  922. }
  923. #[test]
  924. fn tar() -> anyhow::Result<()> {
  925. init();
  926. scan_archive("/data/lto/20241030_CUNVI-DG-N20_SEBZA-DG-N21.tar")?;
  927. Ok(())
  928. }
  929. #[test]
  930. fn run_somatic() -> anyhow::Result<()> {
  931. init();
  932. let collections = Collections::new(
  933. CollectionsConfig::default()
  934. )?;
  935. let bams = collections.bam.by_id_completed(15.0, 10.0);
  936. let n = bams.len();
  937. let mut config = Config::default();
  938. // config.somatic_scan_force = true;
  939. warn!("{n} cases");
  940. for (i, bam) in bams.iter().enumerate() {
  941. let id = &bam.id;
  942. warn!("{i}/{n} {id}");
  943. if id == "BANGA" {
  944. continue;
  945. }
  946. if id == "ARM" {
  947. continue;
  948. }
  949. match SomaticPipe::initialize(id, config.clone())?.run() {
  950. Ok(_) => (),
  951. Err(e) => error!("{id} {e}"),
  952. };
  953. }
  954. Ok(())
  955. }
  956. #[test]
  957. fn somatic_cases() -> anyhow::Result<()> {
  958. init();
  959. let id = "ACHITE";
  960. let config = Config { somatic_pipe_force: true, ..Default::default() };
  961. match SomaticPipe::initialize(id, config)?.run() {
  962. Ok(_) => (),
  963. Err(e) => error!("{id} {e}"),
  964. };
  965. Ok(())
  966. }
  967. #[test]
  968. fn load_variants() -> anyhow::Result<()> {
  969. init();
  970. let id = "ACHITE";
  971. let config = Config::default();
  972. let path = format!("{}/{id}/diag/{id}_somatic_variants.bit", config.result_dir);
  973. let variants = variant_collection::Variants::load_from_file(&path)?;
  974. println!("n variants {}", variants.data.len());
  975. let n_vep: usize = variants.data.iter().map(|v| v.vep().len()).sum();
  976. println!("VEP: {n_vep}");
  977. let translocations = variants.get_alteration_cat(AlterationCategory::TRL);
  978. println!("{} translocations", translocations.len());
  979. let threshold = 5;
  980. let res = group_variants_by_bnd_desc(&translocations, 5);
  981. let rres = group_variants_by_bnd_rc(&res, threshold);
  982. rres.iter().for_each(|group| {
  983. println!("{} {}", group.0.len(), group.1.len());
  984. });
  985. Ok(())
  986. }
  987. #[test]
  988. fn load_fc() -> anyhow::Result<()> {
  989. init();
  990. // FlowCells::load_archive_from_scan("/data/lto", "/data/archives.json")?;
  991. let r = FlowCells::load("/home/prom/mnt/store",
  992. "/data/pandora_id_inputs.json", "/data/archives.json.gz")?;
  993. println!("{r:#?}");
  994. Ok(())
  995. }
  996. #[test]
  997. fn alt_cat() -> anyhow::Result<()> {
  998. let id = "ADJAGBA";
  999. let config = Config::default();
  1000. let path = format!("{}/{id}/diag/somatic_variants.json.gz", config.result_dir);
  1001. let variants = variant_collection::Variants::load_from_json(&path)?;
  1002. println!("n variants {}", variants.data.len());
  1003. variants.data.iter()
  1004. .filter(|v| v.alteration_category().contains(&AlterationCategory::TRL))
  1005. .for_each(|v| {
  1006. println!("{:?} {}",
  1007. v.vcf_variants.iter().map(|v| v.bnd_desc()).collect::<Vec<_>>(),
  1008. v.annotations.iter().filter(|a| matches!(a, Annotation::Callers(..))).map(|a| a.to_string()).collect::<Vec<String>>().join(";")
  1009. )
  1010. });
  1011. Ok(())
  1012. }
  1013. #[test]
  1014. fn variants_stats() -> anyhow::Result<()> {
  1015. init();
  1016. let config = Config::default();
  1017. let all_variants_bit = find_files(&format!("{}/*/diag/*_somatic_variants.bit", config.result_dir))?;
  1018. for v in all_variants_bit.into_iter() {
  1019. let id = v.file_name().unwrap().to_str().unwrap().split("_somatic").next().unwrap();
  1020. println!("{id}");
  1021. let config = Config::default();
  1022. let path = format!("{}/{id}/diag/{id}_somatic_variants.bit", config.result_dir);
  1023. match variant_collection::Variants::load_from_file(&path) {
  1024. Ok(mut variants) => {
  1025. let (mut high_depth_ranges, _) =
  1026. somatic_depth_quality_ranges(&id, &config)?;
  1027. high_depth_ranges.par_sort_by_key(|r| (r.contig, r.range.start));
  1028. let res = VariantsStats::new(&mut variants, id, &config, &high_depth_ranges)?.save_to_json(&format!(
  1029. "{}/{id}/diag/{id}_somatic_variants_stats.json.gz", config.result_dir));
  1030. if res.is_err() {
  1031. info!("{:#?}", res);
  1032. }
  1033. },
  1034. Err(e) => error!("{e}"),
  1035. }
  1036. }
  1037. Ok(())
  1038. }
  1039. #[test]
  1040. fn constit_stats() {
  1041. init();
  1042. let id = "ADJAGBA";
  1043. let config = Config::default();
  1044. let _ = const_stats(id.to_string(), config);
  1045. }
  1046. #[test]
  1047. fn test_bnd() -> anyhow::Result<()> {
  1048. init();
  1049. let id = "COIFFET";
  1050. let config = Config::default();
  1051. let annotations = Annotations::default();
  1052. let s = Savana::initialize(id, config)?.variants(&annotations)?;
  1053. s.variants.iter().for_each(|e| {
  1054. if let Ok(bnd) = e.bnd_desc() {
  1055. println!("{}\t{}\t{}", e.position , e.reference, e.alternative);
  1056. println!("{:#?}", bnd);
  1057. }
  1058. });
  1059. Ok(())
  1060. }
  1061. #[test]
  1062. fn parse_savana_seg() {
  1063. init();
  1064. let r = SavanaCN::parse_file("ADJAGBA", &config::Config::default()).unwrap().segments;
  1065. println!("{} lines", r.len());
  1066. println!("{:#?}", r.first().unwrap());
  1067. }
  1068. #[test]
  1069. fn whole_scan() -> anyhow::Result<()> {
  1070. init();
  1071. let id = "CHENU";
  1072. let mut config = Config::default();
  1073. let u = config.solo_bam(id, "mrd");
  1074. println!("{u}");
  1075. config.somatic_scan_force = true;
  1076. somatic_scan(id, &config)?;
  1077. Ok(())
  1078. }
  1079. #[test]
  1080. fn parse_gff() -> anyhow::Result<()> {
  1081. init();
  1082. let id = "ADJAGBA";
  1083. let config = Config::default();
  1084. let path = format!("{}/{id}/diag/somatic_variants.bit", config.result_dir);
  1085. let exon_ranges = features_ranges("exon", &config)?;
  1086. let exon_ranges = merge_overlapping_genome_ranges(&exon_ranges);
  1087. let variants = variant_collection::Variants::load_from_file(&path)?;
  1088. let full = variants_stats::somatic_rates(&variants.data, &exon_ranges, &config);
  1089. info!("{full:#?}");
  1090. // let restrained: Vec<variant_collection::Variant> = variants.data.iter().filter(|v| v.vcf_variants.len() >= 2)
  1091. // .cloned().collect();
  1092. // let min_2 = variants_stats::somatic_rates(&restrained, &exon_ranges, &config);
  1093. // info!("{min_2:#?}");
  1094. //
  1095. // let restrained: Vec<variant_collection::Variant> = restrained.iter().filter(|v| v.vcf_variants.len() >= 3)
  1096. // .cloned().collect();
  1097. // let min_3 = variants_stats::somatic_rates(&restrained, &exon_ranges, &config);
  1098. // info!("{min_3:#?}");
  1099. // let mut high_depth_ranges = variants_stats::high_depth_somatic(id, &config)?;
  1100. // high_depth_ranges.par_sort_by_key(|r| ( r.contig, r.range.start ));
  1101. //
  1102. // let exon_ranges_ref: Vec<&GenomeRange> = exon_ranges.iter().collect();
  1103. // let exons_high_depth = range_intersection_par(&high_depth_ranges.iter().collect::<Vec<&GenomeRange>>(), &exon_ranges_ref);
  1104. //
  1105. // let full = variants_stats::somatic_rates(&variants.data, &exons_high_depth, &config);
  1106. // info!("{full:#?}");
  1107. //
  1108. // info!("n variants loaded: {}", variants.data.len());
  1109. //
  1110. // let r = features_ranges("exon", &config::Config::default())?;
  1111. // info!("n exon: {}", r.len());
  1112. //
  1113. // let merged = merge_overlapping_genome_ranges(&r);
  1114. // info!("n merged exon: {}", merged.len());
  1115. //
  1116. // let ol = par_overlaps(&variants.data, &r);
  1117. // info!("variants in exon {}", ol.len());
  1118. //
  1119. // let n_coding = ol.iter().filter_map(|i| variants.data[*i].best_vep().ok() ).filter_map(|bv| bv.impact()).filter(|impact| *impact <= VepImpact::MODERATE).count();
  1120. // info!("coding variants {n_coding}");
  1121. //
  1122. // let n_bases_m = merged.par_iter().map(|gr| gr.length()).sum::<u32>();
  1123. // info!("{n_bases_m}nt");
  1124. //
  1125. // let mega_base_m = n_bases_m as f64 / 10.0e6;
  1126. //
  1127. // let wgs_len = read_dict(&config.dict_file)?.iter().map(|(_, l)| *l).sum::<u32>();
  1128. // info!("wgs len {wgs_len}");
  1129. // let rate_wgs = variants.data.len() as f64 / (wgs_len as f64 / 10.0e6);
  1130. // info!("somatic mutation rate {rate_wgs:.2}/mb");
  1131. //
  1132. // let n_exons_mb = ol.len() as f64 / mega_base_m;
  1133. // info!("somatic mutation rate in the coding region {n_exons_mb:.2}/mb");
  1134. //
  1135. // let n_exons_mb = n_coding as f64 / mega_base_m;
  1136. // info!("somatic non synonymous mutation rate in the coding region {n_exons_mb:.2}/mb");
  1137. Ok(())
  1138. }
  1139. fn gr(contig: u8, start: u32, end: u32) -> GenomeRange {
  1140. GenomeRange {
  1141. contig,
  1142. range: start..end,
  1143. }
  1144. }
  1145. #[test]
  1146. fn test_both_empty() {
  1147. let a: Vec<&GenomeRange> = vec![];
  1148. let b: Vec<&GenomeRange> = vec![];
  1149. let result = range_intersection_par(&a, &b);
  1150. assert!(result.is_empty());
  1151. }
  1152. #[test]
  1153. fn test_one_empty() {
  1154. let a = [gr(1, 0, 100)];
  1155. let b: Vec<&GenomeRange> = vec![];
  1156. let a_refs: Vec<&GenomeRange> = a.iter().collect();
  1157. let mut result = range_intersection_par(&a_refs, &b);
  1158. sort_ranges(&mut result);
  1159. assert!(result.is_empty());
  1160. }
  1161. #[test]
  1162. fn test_single_range_no_overlap() {
  1163. let a = [gr(1, 0, 100)];
  1164. let b = [gr(1, 100, 200)];
  1165. let a_refs: Vec<&GenomeRange> = a.iter().collect();
  1166. let b_refs: Vec<&GenomeRange> = b.iter().collect();
  1167. let mut result = range_intersection_par(&a_refs, &b_refs);
  1168. sort_ranges(&mut result);
  1169. assert!(result.is_empty());
  1170. }
  1171. #[test]
  1172. fn test_single_range_full_overlap() {
  1173. let a = [gr(1, 0, 100)];
  1174. let b = [gr(1, 0, 100)];
  1175. let a_refs: Vec<&GenomeRange> = a.iter().collect();
  1176. let b_refs: Vec<&GenomeRange> = b.iter().collect();
  1177. let mut result = range_intersection_par(&a_refs, &b_refs);
  1178. let mut expected = [gr(1, 0, 100)];
  1179. sort_ranges(&mut result);
  1180. sort_ranges(&mut expected);
  1181. assert_eq!(result, expected);
  1182. }
  1183. #[test]
  1184. fn test_different_contigs() {
  1185. let a = [gr(1, 0, 100)];
  1186. let b = [gr(2, 0, 100)];
  1187. let a_refs: Vec<&GenomeRange> = a.iter().collect();
  1188. let b_refs: Vec<&GenomeRange> = b.iter().collect();
  1189. let mut result = range_intersection_par(&a_refs, &b_refs);
  1190. sort_ranges(&mut result);
  1191. assert!(result.is_empty());
  1192. }
  1193. #[test]
  1194. fn test_touching_ranges() {
  1195. let a = [gr(1, 0, 100)];
  1196. let b = [gr(1, 100, 200)];
  1197. let a_refs: Vec<&GenomeRange> = a.iter().collect();
  1198. let b_refs: Vec<&GenomeRange> = b.iter().collect();
  1199. let mut result = range_intersection_par(&a_refs, &b_refs);
  1200. sort_ranges(&mut result);
  1201. assert!(result.is_empty());
  1202. }
  1203. #[test]
  1204. fn test_complete_subrange() {
  1205. let a = [gr(1, 0, 200)];
  1206. let b = [gr(1, 50, 150)];
  1207. let a_refs: Vec<&GenomeRange> = a.iter().collect();
  1208. let b_refs: Vec<&GenomeRange> = b.iter().collect();
  1209. let mut result = range_intersection_par(&a_refs, &b_refs);
  1210. let mut expected = [gr(1, 50, 150)];
  1211. sort_ranges(&mut result);
  1212. sort_ranges(&mut expected);
  1213. assert_eq!(result, expected);
  1214. }
  1215. #[test]
  1216. fn test_multiple_overlaps_same_contig() {
  1217. let a = [gr(1, 0, 50), gr(1, 75, 125)];
  1218. let b = [gr(1, 25, 100), gr(1, 150, 200)];
  1219. let a_refs: Vec<&GenomeRange> = a.iter().collect();
  1220. let b_refs: Vec<&GenomeRange> = b.iter().collect();
  1221. let mut result = range_intersection_par(&a_refs, &b_refs);
  1222. let mut expected = [gr(1, 25, 50), gr(1, 75, 100)];
  1223. sort_ranges(&mut result);
  1224. sort_ranges(&mut expected);
  1225. assert_eq!(result, expected);
  1226. }
  1227. #[test]
  1228. fn test_multiple_contigs() {
  1229. let a = [gr(1, 0, 100), gr(2, 50, 150), gr(3, 200, 300)];
  1230. let b = [gr(1, 50, 150), gr(2, 0, 100), gr(4, 0, 100)];
  1231. let a_refs: Vec<&GenomeRange> = a.iter().collect();
  1232. let b_refs: Vec<&GenomeRange> = b.iter().collect();
  1233. let mut result = range_intersection_par(&a_refs, &b_refs);
  1234. let mut expected = [gr(1, 50, 100), gr(2, 50, 100)];
  1235. sort_ranges(&mut result);
  1236. sort_ranges(&mut expected);
  1237. assert_eq!(result, expected);
  1238. }
  1239. #[test]
  1240. fn test_adjacent_ranges() {
  1241. let a = [gr(1, 0, 50), gr(1, 50, 100)];
  1242. let b = [gr(1, 25, 75)];
  1243. let a_refs: Vec<&GenomeRange> = a.iter().collect();
  1244. let b_refs: Vec<&GenomeRange> = b.iter().collect();
  1245. let mut result = range_intersection_par(&a_refs, &b_refs);
  1246. let mut expected = [gr(1, 25, 50), gr(1, 50, 75)];
  1247. sort_ranges(&mut result);
  1248. sort_ranges(&mut expected);
  1249. assert_eq!(result, expected);
  1250. }
  1251. #[test]
  1252. fn test_minimal_overlap() {
  1253. let a = [gr(1, 0, 100)];
  1254. let b = [gr(1, 99, 200)];
  1255. let a_refs: Vec<&GenomeRange> = a.iter().collect();
  1256. let b_refs: Vec<&GenomeRange> = b.iter().collect();
  1257. let mut result = range_intersection_par(&a_refs, &b_refs);
  1258. let mut expected = [gr(1, 99, 100)];
  1259. sort_ranges(&mut result);
  1260. sort_ranges(&mut expected);
  1261. assert_eq!(result, expected);
  1262. }
  1263. #[test]
  1264. fn todo_scan() -> anyhow::Result<()> {
  1265. init();
  1266. let mut collections = Collections::new(
  1267. CollectionsConfig::default()
  1268. )?;
  1269. collections.todo_bam_count(&Config::default())?;
  1270. collections.tasks.iter().for_each(|t| info!("{t}"));
  1271. let pool = rayon::ThreadPoolBuilder::new()
  1272. .num_threads(100)
  1273. .build()
  1274. .unwrap();
  1275. pool.install(move || {
  1276. collections.tasks.into_iter().for_each(|t| {
  1277. // info!("{t}");
  1278. if let Err(e) = t.run() {
  1279. error!("{e}");
  1280. }
  1281. });
  1282. });
  1283. Ok(())
  1284. }
  1285. /// helper to build a forward‑strand BND (same contig) where
  1286. /// A = pos, B = pos + 5
  1287. fn fwd(contig: &str, pos: u32) -> BNDDesc {
  1288. BNDDesc {
  1289. a_contig: contig.into(),
  1290. a_position: pos,
  1291. a_sens: true,
  1292. b_contig: contig.into(),
  1293. b_position: pos + 5,
  1294. b_sens: true,
  1295. added_nt: String::new(),
  1296. }
  1297. }
  1298. /// Build a six‑node *forward* chain relying **only** on `auto_connect()`
  1299. /// (no manual edges) and assert the Hamiltonian path spans all nodes.
  1300. #[test]
  1301. fn hamiltonian_chain_auto() {
  1302. // positions 10,15 20,25 … 60,65 satisfy B(u) ≤ A(v)
  1303. let bnds: Vec<BNDDesc> = (1..=6).map(|i| fwd("chr1", i * 10)).collect();
  1304. let g: BNDGraph<()> = bnds.to_bnd_graph(); // trait uses auto_connect()
  1305. // ensure auto_connect produced 5 edges in a line
  1306. assert_eq!(g.inner().edge_count(), 5);
  1307. let path = g.hamiltonian_path().expect("chain should be Hamiltonian");
  1308. assert_eq!(path.len(), 6);
  1309. }
  1310. /// Two disconnected "V" shapes -> auto_connect creates 2 edges in each
  1311. /// component, no Hamiltonian path, components sorted by size.
  1312. #[test]
  1313. fn components_after_auto_connect() {
  1314. // comp1: a->b<-c on reverse strand of chrX
  1315. let a = BNDDesc {
  1316. a_contig: "chrX".into(), a_position: 300, a_sens: false,
  1317. b_contig: "chrX".into(), b_position: 250, b_sens: false,
  1318. added_nt: String::new()
  1319. };
  1320. let b = BNDDesc {
  1321. a_contig: "chrX".into(), a_position: 200, a_sens: false,
  1322. b_contig: "chrX".into(), b_position: 150, b_sens: false,
  1323. added_nt: String::new()
  1324. };
  1325. let c = BNDDesc {
  1326. a_contig: "chrX".into(), a_position: 100, a_sens: false,
  1327. b_contig: "chrX".into(), b_position: 50, b_sens: false,
  1328. added_nt: String::new()
  1329. };
  1330. // comp2: three‑node forward chain on chrY
  1331. let d = fwd("chrY", 10);
  1332. let e = fwd("chrY", 20);
  1333. let f = fwd("chrY", 30);
  1334. let g: BNDGraph<()> = vec![a,b,c,d,e,f].to_bnd_graph();
  1335. assert!(g.hamiltonian_path().is_none());
  1336. let comps = g.components_by_size();
  1337. comps.iter().for_each(|a| println!("{}", g.fmt_path(a) ));
  1338. assert_eq!(comps.len(), 2);
  1339. assert_eq!(comps[0].len(), 3);
  1340. assert_eq!(comps[1].len(), 3);
  1341. }
  1342. #[test]
  1343. fn bnd_connections() {
  1344. // comp1: a->b<-c on reverse strand of chrX
  1345. let a = BNDDesc {
  1346. a_contig: "chrX".into(), a_position: 300, a_sens: true,
  1347. b_contig: "chr14".into(), b_position: 250, b_sens: false,
  1348. added_nt: String::new()
  1349. };
  1350. let b = BNDDesc {
  1351. a_contig: "chr14".into(), a_position: 200, a_sens: false,
  1352. b_contig: "chrX".into(), b_position: 301, b_sens: true,
  1353. added_nt: String::new()
  1354. };
  1355. let c = BNDDesc {
  1356. a_contig: "chrX".into(), a_position: 302, a_sens: true,
  1357. b_contig: "chrZ".into(), b_position: 50, b_sens: false,
  1358. added_nt: String::new()
  1359. };
  1360. // comp2: three‑node forward chain on chrY
  1361. let d = fwd("chrY", 10);
  1362. let e = fwd("chrY", 20);
  1363. let f = fwd("chrY", 30);
  1364. let g: BNDGraph<()> = vec![a,b,c,d,e,f].to_bnd_graph();
  1365. assert!(g.hamiltonian_path().is_none());
  1366. let comps = g.components_by_size();
  1367. comps.iter().for_each(|a| println!("{}", g.fmt_path(a) ));
  1368. assert_eq!(comps.len(), 2);
  1369. assert_eq!(comps[0].len(), 3);
  1370. assert_eq!(comps[1].len(), 3);
  1371. }
  1372. }