Skip to content

Commit

Permalink
error if assembly cannot be manually revised
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-parker committed Feb 15, 2025
1 parent 7a87945 commit e740e7a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
57 changes: 56 additions & 1 deletion ena-submission/src/ena_deposition/create_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
AssemblyChromosomeListFile,
AssemblyChromosomeListFileObject,
AssemblyManifest,
AssemblyType,
ChromosomeType,
MoleculeType,
)
Expand All @@ -37,6 +36,8 @@
find_conditions_in_db,
find_errors_in_db,
find_waiting_in_db,
is_revision,
last_version,
update_db_where_conditions,
)

Expand Down Expand Up @@ -331,6 +332,57 @@ def submission_table_update(db_config: SimpleConnectionPool):
raise RuntimeError(error_msg)


def can_be_revised(
config: Config, db_config: SimpleConnectionPool, entry: dict[str, str], retry_number: 3
) -> bool:
if not is_revision(db_config, entry):
return False
version_to_revise = last_version(db_config, entry)
last_version_data = find_conditions_in_db(
db_config,
table_name="submission_table",
conditions={"accession": entry["accession"], "version": version_to_revise},
)
if len(last_version_data) == 0:
error_msg = f"Last version {version_to_revise} not found in submission_table"
raise RuntimeError(error_msg)
differing_fields = []
for value in config.manifest_fields_mapping.values():
for field in value.get("loculus_fields", []):
last_entry = last_version_data[0]["metadata"].get(field)
new_entry = entry["metadata"].get(field)
if last_entry != new_entry:
differing_fields.append(field)
if differing_fields:
update_values = {
"status": Status.HAS_ERRORS,
"errors": json.dumps(
[
"Assembly cannot be revised because metadata fields "
f"{', '.join(differing_fields)} in manifest differs from last version"
]
),
"started_at": datetime.now(tz=pytz.utc),
}
number_rows_updated = 0
tries = 0
while number_rows_updated != 1 and tries < retry_number:
if tries > 0:
# If state not correctly added retry
logger.warning(
f"Assembly revision failed and DB update failed - reentry DB update #{tries}."
)
number_rows_updated = update_db_where_conditions(
db_config,
table_name="assembly_table",
conditions={"accession": entry["accession"], "version": entry["version"]},
update_values=update_values,
)
tries += 1
return False
return True


def assembly_table_create(
db_config: SimpleConnectionPool, config: Config, retry_number: int = 3, test: bool = False
):
Expand Down Expand Up @@ -385,6 +437,9 @@ def assembly_table_create(
error_msg = f"Entry {row['accession']} not found in project_table"
raise RuntimeError(error_msg)

if is_revision(db_config, seq_key) and not can_be_revised():
continue

try:
manifest_object = create_manifest_object(
config,
Expand Down
14 changes: 1 addition & 13 deletions ena-submission/src/ena_deposition/create_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
db_init,
find_conditions_in_db,
find_errors_in_db,
is_revision,
update_db_where_conditions,
)

Expand Down Expand Up @@ -285,19 +286,6 @@ def submission_table_update(db_config: SimpleConnectionPool):
raise RuntimeError(error_msg)


def is_revision(db_config: SimpleConnectionPool, seq_key: dict[str, str]):
"""Check if the entry is a revision"""
version = seq_key["version"]
if version == "1":
return False
accession = {"accession": seq_key["accession"]}
sample_data_in_submission_table = find_conditions_in_db(
db_config, table_name="submission_table", conditions=accession
)
all_versions = sorted([int(entry["version"]) for entry in sample_data_in_submission_table])
return len(all_versions) > 1 and version == all_versions[-1]


def is_old_version(db_config: SimpleConnectionPool, seq_key: dict[str, str], retry_number: int = 3):
"""Check if entry is incorrectly added older version - error and do not submit"""
version = seq_key["version"]
Expand Down
24 changes: 24 additions & 0 deletions ena-submission/src/ena_deposition/submission_db_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,27 @@ def add_to_submission_table(
return False
finally:
db_conn_pool.putconn(con)


def is_revision(db_config: SimpleConnectionPool, seq_key: dict[str, str]):
"""Check if the entry is a revision"""
version = seq_key["version"]
if version == "1":
return False
accession = {"accession": seq_key["accession"]}
sample_data_in_submission_table = find_conditions_in_db(
db_config, table_name="submission_table", conditions=accession
)
all_versions = sorted([int(entry["version"]) for entry in sample_data_in_submission_table])
return len(all_versions) > 1 and version == all_versions[-1]


def last_version(db_config: SimpleConnectionPool, seq_key: dict[str, str]) -> int | None:
if not is_revision(db_config, seq_key):
return None
accession = {"accession": seq_key["accession"]}
sample_data_in_submission_table = find_conditions_in_db(
db_config, table_name="submission_table", conditions=accession
)
all_versions = sorted([int(entry["version"]) for entry in sample_data_in_submission_table])
return all_versions[-2]

0 comments on commit e740e7a

Please sign in to comment.