Update virus sequences when schema segment is removed
Deleting segments that are associated with sequences should result in unassignment.
diff --git a/virtool/api/otus.py b/virtool/api/otus.py
index 5201465be..661a948c2 100644
--- a/virtool/api/otus.py
+++ b/virtool/api/otus.py
@@ -176,6 +176,8 @@ async def edit(req):
         }
     })
 
+    await virtool.db.otus.update_sequence_segments(db, old, document)
+
     new = await virtool.db.otus.join(db, otu_id, document)
 
     issues = await virtool.db.otus.update_verification(db, new)
diff --git a/virtool/db/otus.py b/virtool/db/otus.py
index 1d6dc6127..81d702222 100644
--- a/virtool/db/otus.py
+++ b/virtool/db/otus.py
@@ -312,3 +312,22 @@ async def update_last_indexed_version(db, id_list, version):
     })
 
     return result
+
+
+async def update_sequence_segments(db, old, new):
+    if old is None or new is None:
+        return
+
+    old_names = {s["name"] for s in old["schema"]}
+    new_names = {s["name"] for s in new["schema"]}
+
+    if old_names == new_names:
+        return
+
+    to_unset = list(old_names - new_names)
+
+    await db.sequences.update_many({"otu_id": old["_id"], "segment": {"$in": to_unset}}, {
+        "$unset": {
+            "segment": ""
+        }
+    })
['virtool/api/otus.py', 'virtool/db/otus.py']