bcftools.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. use anyhow::Context;
  2. use std::fs;
  3. use uuid::Uuid;
  4. use crate::runners::{run_wait, CommandRun, RunReport};
  5. #[derive(Debug)]
  6. pub struct BcftoolsConfig {
  7. pub bin: String,
  8. pub threads: u8,
  9. }
  10. impl Default for BcftoolsConfig {
  11. fn default() -> Self {
  12. Self {
  13. bin: "/data/tools/bcftools-1.21/bcftools".to_string(),
  14. threads: 20,
  15. }
  16. }
  17. }
  18. pub fn bcftools_keep_pass(
  19. input: &str,
  20. output: &str,
  21. config: BcftoolsConfig,
  22. ) -> anyhow::Result<RunReport> {
  23. let tmp_file = format!("/tmp/{}", Uuid::new_v4());
  24. // First sort
  25. let mut cmd_run = CommandRun::new(&config.bin, &["sort", input, "-o", &tmp_file]);
  26. let _ = run_wait(&mut cmd_run)?;
  27. // Then filter
  28. let mut cmd_run = CommandRun::new(
  29. &config.bin,
  30. &[
  31. "view",
  32. "--write-index",
  33. "--threads",
  34. &config.threads.to_string(),
  35. "-i",
  36. "FILTER='PASS'",
  37. &tmp_file,
  38. "-o",
  39. output,
  40. ],
  41. );
  42. let res = run_wait(&mut cmd_run)?;
  43. fs::remove_file(tmp_file)?;
  44. Ok(res)
  45. }
  46. pub fn bcftools_concat(
  47. inputs: Vec<String>,
  48. output: &str,
  49. config: BcftoolsConfig,
  50. ) -> anyhow::Result<RunReport> {
  51. let tmp_file = format!("/tmp/{}", Uuid::new_v4());
  52. fs::write(&tmp_file, inputs.join("\n"))?;
  53. let args = [
  54. "concat",
  55. "--write-index",
  56. "--threads",
  57. &config.threads.to_string(),
  58. "-a",
  59. "-D",
  60. "-f",
  61. &tmp_file,
  62. "-o",
  63. output,
  64. ];
  65. // Then filter
  66. let mut cmd_run = CommandRun::new(&config.bin, &args);
  67. let res = run_wait(&mut cmd_run)?;
  68. fs::remove_file(tmp_file)?;
  69. Ok(res)
  70. }
  71. pub fn bcftools_keep_only_in_a(a: &str, b: &str, out: &str, config: &BcftoolsConfig) -> anyhow::Result<()> {
  72. let args = ["isec", "-C", "-w", "1", a, b, "-o", out];
  73. let mut cmd_run = CommandRun::new(&config.bin, &args);
  74. let _ = run_wait(&mut cmd_run).context(format!(
  75. "Error while running bcftools isec {}",
  76. args.join(" ")
  77. ))?;
  78. Ok(())
  79. }