Skip to content

fix: sync user annotation file with database on Reset Annotations - #80

Open
IvoLeist with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-annotations-incongruency
Open

fix: sync user annotation file with database on Reset Annotations#80
IvoLeist with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-annotations-incongruency

Conversation

Copilot AI commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

After "Update Database" replaces variants in the database, "Reset Annotations" would leave the user's annotation file incongruent — stale rows (removed from DB) persisted in the file, and new DB rows were never added.

Changes

R/admin_utils.Rreset_user_annotations()

Before clearing annotation columns, sync the user file's rows against the current database state:

  • Remove stale rows: any row whose coordinates|REF|ALT composite key no longer exists in the DB is dropped from the user file
  • Append new rows: any DB entry not yet in the user file is appended with all annotation columns blank
  • Vote-count decrement logic now operates only on the filtered (still-in-DB) rows
  • Re-randomise row order: after all sync operations, the full row order is shuffled using a fresh digest-based seed (same algorithm as the initial login flow), so the user gets a new voting sequence that places new rows at random positions
  • Update images_randomisation_seed: the new seed is written to images_randomisation_seed in the user's _info.json alongside the existing vote_input_methods reset
# Sync before clearing annotation columns
db_entries <- DBI::dbGetQuery(db_pool, "SELECT coordinates, REF, ALT FROM annotations")

user_keys <- paste(annotations_df$coordinates, annotations_df$REF, annotations_df$ALT, sep = "|")
db_keys   <- paste(db_entries$coordinates,      db_entries$REF,      db_entries$ALT,      sep = "|")

# Drop rows no longer in DB
annotations_df <- annotations_df[user_keys %in% db_keys, , drop = FALSE]
user_keys      <- user_keys[user_keys %in% db_keys]

# Track new DB rows to append after reset
new_db_mask <- !db_keys %in% user_keys

# ... after building reset_df and appending new rows ...

# Re-randomise
randomisation_seed <- strtoi(substr(digest::digest(paste0(annotation_file_path, Sys.time()), algo = "crc32"), 1, 7), base = 16)
set.seed(randomisation_seed)
reset_df <- reset_df[sample(seq_len(nrow(reset_df))), , drop = FALSE]

tests/testthat/test-admin-reset-annotations.R

Three new test cases:

  • Rows removed from DB → dropped from user file; vote counts decremented only for retained rows
  • Rows added to DB → appended to user file with blank annotations
  • Already-congruent file + DB → unchanged row count (existing behaviour preserved)

Updated existing test: images_randomisation_seed is now verified to be updated to a new numeric value on every reset (not preserved).

Copilot AI and others added 2 commits April 8, 2026 11:06
When 'Update Database' is used before 'Reset Annotations', the user
annotation file could be out of sync with the database (rows removed
from the DB still appearing in the file, or new DB rows missing from
the file).

In reset_user_annotations() (R/admin_utils.R):
- Query the current database entries before performing the reset
- Remove from the user file any rows whose coordinates|REF|ALT no longer
  exist in the database
- Append to the reset file any new database rows that are not yet in
  the user file

Vote-count decrement logic is unaffected: it operates on the already-
filtered set of rows (only those that still exist in the DB).

New tests in test-admin-reset-annotations.R cover all three scenarios:
rows removed from DB, rows added to DB, and already-congruent state.

Agent-Logs-Url: https://github.com/biomedicalGenomicsCNAG/ShinyImageVoteR/sessions/ff46e7d5-457a-4d42-95a6-7f5592fec459

Co-authored-by: IvoLeist <28399610+IvoLeist@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix incongruency of annotations in user data files fix: sync user annotation file with database on Reset Annotations Apr 8, 2026
Copilot AI requested a review from IvoLeist April 8, 2026 11:09

@IvoLeist IvoLeist left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After appending the new rows the row order should be randomised again
Do not forget to update the images_randomisation_seed in _info.json

@IvoLeist
IvoLeist marked this pull request as ready for review April 8, 2026 12:40
…eset Annotations

After all sync operations (remove stale rows, append new DB rows),
shuffle the full reset_df with a fresh digest-based seed so the user
gets a new voting sequence that includes new rows at random positions.

The new seed is written to images_randomisation_seed in the user's
_info.json alongside the existing vote_input_methods reset.

Updated test: images_randomisation_seed is now verified to change
(not remain 12345) on every reset.

Agent-Logs-Url: https://github.com/biomedicalGenomicsCNAG/ShinyImageVoteR/sessions/453b93ef-2b26-4a3b-97b1-a21790234708

Co-authored-by: IvoLeist <28399610+IvoLeist@users.noreply.github.com>
Copilot AI requested a review from IvoLeist April 8, 2026 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incongruency of the annotations in the users data files when "Reset Annotations" after "Update Database" in the admin view

2 participants