Browse Source

Update Pod5sRun

Thomas 2 weeks ago
parent
commit
0a1d0d4be3
1 changed files with 39 additions and 5 deletions
  1. 39 5
      src/collection/pod5.rs

+ 39 - 5
src/collection/pod5.rs

@@ -345,8 +345,25 @@ impl Pod5sRuns {
     ///   - New `Pod5` entries are only added if their file name (from `pod.path.file_name()`)
     ///     does **not** already exist in the run.
     ///   - Duplicate file names are silently skipped (no error).
-    pub fn add_from_dir<P: AsRef<Path>>(&mut self, dir: P) -> anyhow::Result<()> {
-        let mut new_run = Pod5sRun::load_from_dir(&dir)?;
+    /// Add a new `Pod5sRun` by scanning a directory of `.pod5` files.
+    ///
+    /// - Builds a `Pod5sRun` via [`Pod5sRun::load_from_dir`].
+    /// - Optionally attaches a `bams_pass` directory to the run.
+    /// - If **no** existing run has the same `(run_id, flow_cell_id, sequencing_kit)`,
+    ///   the new run is appended to `data`.
+    /// - If a run **already exists** with these three identifiers:
+    ///   - `pod5s` are merged by file name (duplicates skipped).
+    ///   - `bams_pass`:
+    ///       * if existing has `Some` and new is `None` → keep existing.
+    ///       * if existing is `None` and new is `Some` → set existing to new.
+    ///       * if both `Some` but different → error (conflicting BAM-pass roots).
+    pub fn add_from_dir<P, Q>(&mut self, pod_dir: P, bams_pass: Option<Q>) -> anyhow::Result<()>
+    where
+        P: AsRef<Path>,
+        Q: AsRef<Path>,
+    {
+        let mut new_run = Pod5sRun::load_from_dir(&pod_dir)?;
+        new_run.bams_pass = bams_pass.map(|p| p.as_ref().to_path_buf());
 
         // Try to find an existing run with same identifiers
         if let Some(existing) = self.data.iter_mut().find(|r| {
@@ -354,6 +371,26 @@ impl Pod5sRuns {
                 && r.flow_cell_id == new_run.flow_cell_id
                 && r.sequencing_kit == new_run.sequencing_kit
         }) {
+            // --- merge bams_pass ---
+            match (&existing.bams_pass, &new_run.bams_pass) {
+                (Some(old), Some(new)) if old != new => {
+                    anyhow::bail!(
+                        "Conflicting bam_pass for run {} (flowcell {}): \
+                         existing='{}', new='{}'",
+                        existing.run_id,
+                        existing.flow_cell_id,
+                        old.display(),
+                        new.display()
+                    );
+                }
+                (None, Some(new)) => {
+                    existing.bams_pass = Some(new.clone());
+                }
+                _ => {
+                    // (Some, None) or (None, None) or (Some, Some equal): nothing to do
+                }
+            }
+
             // Build a set of existing Pod5 file names
             let mut existing_names: HashSet<String> = existing
                 .pod5s
@@ -381,9 +418,6 @@ impl Pod5sRuns {
             // Merge the unique new Pod5s into the existing run
             existing.pod5s.extend(new_run.pod5s);
 
-            // Optionally merge other fields (e.g., cases) if needed
-            // existing.cases.extend(new_run.cases);
-
             Ok(())
         } else {
             // No matching run: add as a new entry