Skip to content

Commit d499901

Browse files
committed
Add a prototype of Sample::developmental_stage backfill script
1 parent 07d3759 commit d499901

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.2.18 on 2023-12-08 00:45
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("data_refinery_common", "0074_sample_developmental_stage"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="sample",
15+
name="last_refreshed",
16+
field=models.DateTimeField(auto_now=True, null=True),
17+
),
18+
]

common/data_refinery_common/models/sample.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def __str__(self):
9494
created_at = models.DateTimeField(editable=False, default=timezone.now)
9595
last_modified = models.DateTimeField(default=timezone.now)
9696

97+
# Auxiliary field for tracking latest metadata update time.
98+
# Originally added to support Sample::developmental_stage values backfilling.
99+
last_refreshed = models.DateTimeField(auto_now=True, null=True)
100+
97101
def save(self, *args, **kwargs):
98102
"""On save, update timestamps"""
99103
current_time = timezone.now()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import time
2+
3+
from django.core.management.base import BaseCommand
4+
5+
from data_refinery_common.logging import get_and_configure_logger
6+
from data_refinery_common.models import Sample
7+
from data_refinery_foreman.surveyor.sra import SraSurveyor
8+
9+
logger = get_and_configure_logger(__name__)
10+
11+
12+
class Command(BaseCommand):
13+
def add_arguments(self, parser):
14+
parser.add_argument(
15+
"--limit",
16+
default=1000,
17+
type=int,
18+
help="Number of samples to refresh",
19+
)
20+
parser.add_argument(
21+
"--source",
22+
choices=("SRA",),
23+
required=True,
24+
type=str,
25+
help="Source name (ARRAY_EXPRESS, GEO, SRA)",
26+
)
27+
28+
def handle(self, *args, **options):
29+
for sample in Sample.objects.filter(
30+
developmental_stage__isnull=True,
31+
last_refreshed__isnull=True,
32+
source_database=options["source"],
33+
).order_by("id")[: options["limit"]]:
34+
logger.info(f"Refreshing metadata for a sample {sample.accession_code}")
35+
try:
36+
_, sample_metadata = SraSurveyor.gather_all_metadata(sample.accession_code)
37+
SraSurveyor._apply_harmonized_metadata_to_sample(sample_metadata)
38+
except Exception as e:
39+
logger.exception(e)
40+
finally:
41+
sample.save()
42+
43+
time.sleep(1)

0 commit comments

Comments
 (0)