Skip to content
Merged
25 changes: 25 additions & 0 deletions nmdc_runtime/site/repair/database_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def __init__(
:param study_id: NMDC study ID for which the missing records need to be added.
:param gold_nmdc_instrument_map_df: A dataframe originally stored as a TSV mapping file in the
NMDC schema repo, which maps GOLD instrument IDs to IDs of NMDC instrument_set records.
:param include_field_site_info: Whether to include field research site information when
translating GOLD data. When True, the GoldStudyTranslator will create
`nmdc:FieldResearchSite` records and associate them with biosamples. Defaults to False.
:param enable_biosample_filtering: Whether to filter biosamples based on their
`sequencingStrategy` (only keeping "Metagenome" or "Metatranscriptome") and associated
project `projectStatus`. When False, all biosamples are included regardless of sequencing
strategy or project status. Defaults to True.
"""

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Unrelated to main logic being added in this PR. I just noticed that these params were missing documentation in the docstring so added them here.

self.runtime_api_user_client = runtime_api_user_client
self.runtime_api_site_client = runtime_api_site_client
Expand Down Expand Up @@ -84,7 +91,18 @@ class which is responsible for making data_generation_set records.

all_gold_biosamples = []
all_gold_projects = []
existing_gold_project_ids = set()
for biosample in biosample_set:
# Check for existing DataGeneration records linked to this biosample
# using the /nmdcschema/linked_instances endpoint, which is more
# efficient than fetching all DataGeneration records for the study.
linked_data_generations = self.runtime_api_user_client.get_linked_data_generation_records_for_biosample(
biosample["id"]
)
for dg in linked_data_generations:
for gold_id in dg.get("gold_sequencing_project_identifiers", []):
existing_gold_project_ids.add(gold_id.replace("gold:", ""))

gold_biosample_identifiers = biosample.get("gold_biosample_identifiers")
if gold_biosample_identifiers:
for gold_biosample_id in gold_biosample_identifiers:
Expand All @@ -108,6 +126,13 @@ class which is responsible for making data_generation_set records.
filtered_biosamples = gold_study_translator.biosamples
filtered_projects = gold_study_translator.projects

# Filter out GOLD projects that already have DataGeneration records in the database
filtered_projects = [
project
for project in filtered_projects
if project["projectGoldId"] not in existing_gold_project_ids
]

gold_project_ids = [project["projectGoldId"] for project in filtered_projects]
nmdc_nucleotide_sequencing_ids = self.runtime_api_site_client.mint_id(
"nmdc:NucleotideSequencing", len(gold_project_ids)
Expand Down
24 changes: 24 additions & 0 deletions nmdc_runtime/site/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,30 @@ def get_biosamples_for_study(self, study_id: str):
response.raise_for_status()
return response.json()["resources"]

def get_linked_data_generation_records_for_biosample(self, biosample_id: str):
"""Fetch DataGeneration records linked to a given biosample using the
/nmdcschema/linked_instances endpoint.

Note: This method was added for use by the DatabaseUpdater class
(nmdc_runtime.site.repair.database_updater) to check for existing
DataGeneration records before creating new ones.

:param biosample_id: NMDC biosample ID (e.g. "nmdc:bsm-11-abc123").
:return: List of hydrated DataGeneration records linked to the biosample.
"""
response = self.request(
"GET",
"/nmdcschema/linked_instances",
{
"ids": biosample_id,
"types": "nmdc:DataGeneration",
"hydrate": True,
"max_page_size": 9999,
},
)
response.raise_for_status()
return response.json()["resources"]

def get_data_generation_records_for_study(self, study_id: str):
# TODO: same as above, we are using a large max_page_size to avoid pagination.
response = self.request(
Expand Down
5 changes: 5 additions & 0 deletions tests/test_data/test_database_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def test_generate_data_generation_set_records_from_gold_api_for_study(
}
]

# No existing DataGeneration records linked to this biosample
mock_runtime_api_user_client.get_linked_data_generation_records_for_biosample.return_value = (
[]
)

mock_gold_api_client.fetch_biosample_by_biosample_id.return_value = [
{
"biosampleGoldId": "Gb0150488",
Expand Down
Loading