Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a prototype of Sample::developmental_stage backfill script #3461

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.18 on 2023-12-08 00:45

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("data_refinery_common", "0074_sample_developmental_stage"),
]

operations = [
migrations.AddField(
model_name="sample",
name="last_refreshed",
field=models.DateTimeField(auto_now=True, null=True),
),
]
4 changes: 4 additions & 0 deletions common/data_refinery_common/models/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def __str__(self):
created_at = models.DateTimeField(editable=False, default=timezone.now)
last_modified = models.DateTimeField(default=timezone.now)

# Auxiliary field for tracking latest metadata update time.
# Originally added to support Sample::developmental_stage values backfilling.
last_refreshed = models.DateTimeField(auto_now=True, null=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

  • We will probably want last_refreshed on Experiment as well, since a sample could belong to more than one experiment.
  • We probably want to add last_refresh_failure as a timestamp on both as well to help with re-running


def save(self, *args, **kwargs):
"""On save, update timestamps"""
current_time = timezone.now()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import time

from django.core.management.base import BaseCommand

from data_refinery_common.logging import get_and_configure_logger
from data_refinery_common.models import Sample
from data_refinery_foreman.surveyor.sra import SraSurveyor

logger = get_and_configure_logger(__name__)


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"--limit",
default=1000,
type=int,
help="Number of samples to refresh",
)
parser.add_argument(
"--source",
choices=("SRA",),
required=True,
type=str,
help="Source name (ARRAY_EXPRESS, GEO, SRA)",
)

def handle(self, *args, **options):
for sample in Sample.objects.filter(
developmental_stage__isnull=True,
last_refreshed__isnull=True,
source_database=options["source"],
).order_by("id")[: options["limit"]]:
logger.info(f"Refreshing metadata for a sample {sample.accession_code}")
try:
_, sample_metadata = SraSurveyor.gather_all_metadata(sample.accession_code)
SraSurveyor._apply_harmonized_metadata_to_sample(sample_metadata)
Copy link
Contributor

Choose a reason for hiding this comment

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

This takes sample as the first argument

Copy link
Contributor

Choose a reason for hiding this comment

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

One more consideration here is that after updating the sample we will want to update the cached values on experiment.

ie:

# Update our cached values
experiment.update_num_samples()
experiment.update_sample_metadata_fields()
experiment.update_platform_names()
experiment.save()

except Exception as e:
logger.exception(e)
finally:
sample.save()

time.sleep(1)