|
@@ -771,6 +771,22 @@ pub fn fb_inv_from_record(
|
|
|
// anyhow::bail!("No SM tag found in @RG header of {bam_path}")
|
|
// anyhow::bail!("No SM tag found in @RG header of {bam_path}")
|
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
|
|
+// fn ensure_bam_sm_tag(id: &str, config: &Config) -> anyhow::Result<()> {
|
|
|
|
|
+// for bam in [config.normal_bam(id), config.tumoral_bam(id)] {
|
|
|
|
|
+// if read_sm_tag(&bam).is_err() {
|
|
|
|
|
+// let sample = Path::new(&bam)
|
|
|
|
|
+// .file_stem()
|
|
|
|
|
+// .unwrap_or_default()
|
|
|
|
|
+// .to_string_lossy()
|
|
|
|
|
+// .to_string();
|
|
|
|
|
+// info!("Injecting missing @RG SM:{sample} into {bam}");
|
|
|
|
|
+// SamtoolsReheader::from_config(config, &bam, &sample).run()?;
|
|
|
|
|
+// SamtoolsIndex::from_config(config, &bam).run()?;
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+// Ok(())
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
pub fn read_sm_tag_or_inject(
|
|
pub fn read_sm_tag_or_inject(
|
|
|
bam_path: &str,
|
|
bam_path: &str,
|
|
|
fallback_sample: &str,
|
|
fallback_sample: &str,
|
|
@@ -780,7 +796,6 @@ pub fn read_sm_tag_or_inject(
|
|
|
.with_context(|| format!("Failed to open BAM: {bam_path}"))?;
|
|
.with_context(|| format!("Failed to open BAM: {bam_path}"))?;
|
|
|
let header = bam::Header::from_template(reader.header());
|
|
let header = bam::Header::from_template(reader.header());
|
|
|
let header_text = String::from_utf8_lossy(&header.to_bytes()).to_string();
|
|
let header_text = String::from_utf8_lossy(&header.to_bytes()).to_string();
|
|
|
-
|
|
|
|
|
for line in header_text.lines() {
|
|
for line in header_text.lines() {
|
|
|
if line.starts_with("@RG") {
|
|
if line.starts_with("@RG") {
|
|
|
for field in line.split('\t') {
|
|
for field in line.split('\t') {
|
|
@@ -791,9 +806,14 @@ pub fn read_sm_tag_or_inject(
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Preserve original mtime before modifying header
|
|
|
|
|
+ let original_mtime = filetime::FileTime::from_last_modification_time(
|
|
|
|
|
+ &std::fs::metadata(bam_path)
|
|
|
|
|
+ .with_context(|| format!("Failed to stat BAM: {bam_path}"))?,
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
// SM missing (dorado/MinKNOW unclassified barcode) — inject it
|
|
// SM missing (dorado/MinKNOW unclassified barcode) — inject it
|
|
|
info!("No @RG SM tag in {bam_path}, injecting SM:{fallback_sample}");
|
|
info!("No @RG SM tag in {bam_path}, injecting SM:{fallback_sample}");
|
|
|
-
|
|
|
|
|
let mut reheader = SamtoolsReheader::from_config(config, bam_path, fallback_sample);
|
|
let mut reheader = SamtoolsReheader::from_config(config, bam_path, fallback_sample);
|
|
|
reheader
|
|
reheader
|
|
|
.run()
|
|
.run()
|
|
@@ -804,5 +824,9 @@ pub fn read_sm_tag_or_inject(
|
|
|
.run()
|
|
.run()
|
|
|
.with_context(|| format!("Failed to re-index {bam_path}"))?;
|
|
.with_context(|| format!("Failed to re-index {bam_path}"))?;
|
|
|
|
|
|
|
|
|
|
+ // Restore original mtime so is_file_older() doesn't re-trigger downstream callers
|
|
|
|
|
+ filetime::set_file_mtime(bam_path, original_mtime)
|
|
|
|
|
+ .with_context(|| format!("Failed to restore mtime on {bam_path}"))?;
|
|
|
|
|
+
|
|
|
Ok(fallback_sample.to_string())
|
|
Ok(fallback_sample.to_string())
|
|
|
}
|
|
}
|