-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsample.py
More file actions
257 lines (214 loc) · 10.6 KB
/
Copy pathsample.py
File metadata and controls
257 lines (214 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from typing import Set
from django.conf import settings
from django.db import models
from django.utils import timezone
from data_refinery_common.models.computed_file import ComputedFile
from data_refinery_common.models.managers import ProcessedObjectsManager, PublicObjectsManager
class Sample(models.Model):
"""
An individual sample.
"""
class Meta:
db_table = "samples"
base_manager_name = "public_objects"
get_latest_by = "created_at"
indexes = [
models.Index(fields=["accession_code"]),
]
def __str__(self):
return self.accession_code
# Managers
objects = models.Manager()
public_objects = PublicObjectsManager()
processed_objects = ProcessedObjectsManager()
# Identifiers
accession_code = models.CharField(max_length=255, unique=True)
title = models.CharField(max_length=255, unique=False, blank=True)
# Relations
organism = models.ForeignKey("Organism", blank=True, null=True, on_delete=models.SET_NULL)
results = models.ManyToManyField("ComputationalResult", through="SampleResultAssociation")
original_files = models.ManyToManyField("OriginalFile", through="OriginalFileSampleAssociation")
computed_files = models.ManyToManyField("ComputedFile", through="SampleComputedFileAssociation")
experiments = models.ManyToManyField("Experiment", through="ExperimentSampleAssociation")
# Historical Properties
source_database = models.CharField(max_length=255, blank=False)
source_archive_url = models.CharField(max_length=255)
source_filename = models.CharField(max_length=255, blank=False)
source_absolute_file_path = models.CharField(max_length=255)
has_raw = models.BooleanField(default=True) # Did this sample have a raw data source?
# Technological Properties
platform_accession_code = models.CharField(max_length=256, blank=True)
platform_name = models.CharField(max_length=256, blank=True)
technology = models.CharField(max_length=256, blank=True) # MICROARRAY, RNA-SEQ
manufacturer = models.CharField(max_length=256, blank=True)
protocol_info = models.JSONField(default=dict)
# Scientific Properties
sex = models.CharField(max_length=255, blank=True)
age = models.DecimalField(max_length=255, blank=True, max_digits=8, decimal_places=3, null=True)
specimen_part = models.CharField(max_length=255, blank=True)
genetic_information = models.CharField(max_length=255, blank=True)
developmental_stage = models.CharField(max_length=255, blank=True)
disease = models.CharField(max_length=255, blank=True)
disease_stage = models.CharField(max_length=255, blank=True)
cell_line = models.CharField(max_length=255, blank=True)
treatment = models.CharField(max_length=255, blank=True)
race = models.CharField(max_length=255, blank=True)
subject = models.CharField(max_length=255, blank=True)
compound = models.CharField(max_length=255, blank=True)
time = models.CharField(max_length=255, blank=True)
# Crunch Properties
is_processed = models.BooleanField(default=False)
is_unable_to_be_processed = models.BooleanField(default=False)
last_processor_job = models.ForeignKey("ProcessorJob", null=True, on_delete=models.SET_NULL)
last_downloader_job = models.ForeignKey("DownloaderJob", null=True, on_delete=models.SET_NULL)
# Set related_name to "+" to prevent the backwards relation, since
# it should be a duplicate of the relation already established by
# the computed_files field.
most_recent_smashable_file = models.ForeignKey(
"ComputedFile", null=True, on_delete=models.SET_NULL, related_name="+"
)
most_recent_quant_file = models.ForeignKey(
"ComputedFile", null=True, on_delete=models.SET_NULL, related_name="+"
)
is_unable_to_be_processed = models.BooleanField(default=False)
# Blacklisting
is_blacklisted = models.BooleanField(default=False)
# Common Properties
is_public = models.BooleanField(default=True)
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)
def save(self, *args, **kwargs):
"""On save, update timestamps"""
current_time = timezone.now()
if not self.id:
self.created_at = current_time
self.last_modified = current_time
return super(Sample, self).save(*args, **kwargs)
def to_metadata_dict(self, computed_file=None):
"""Render this Sample as a dict."""
metadata = {}
metadata["refinebio_title"] = self.title
metadata["refinebio_accession_code"] = self.accession_code
metadata["refinebio_organism"] = self.organism.name if self.organism else None
metadata["refinebio_source_database"] = self.source_database
metadata["refinebio_source_archive_url"] = self.source_archive_url
metadata["refinebio_sex"] = self.sex
metadata["refinebio_age"] = self.age or ""
metadata["refinebio_specimen_part"] = self.specimen_part
metadata["refinebio_genetic_information"] = self.genetic_information
metadata["refinebio_disease"] = self.disease
metadata["refinebio_disease_stage"] = self.disease_stage
metadata["refinebio_cell_line"] = self.cell_line
metadata["refinebio_treatment"] = self.treatment
metadata["refinebio_race"] = self.race
metadata["refinebio_subject"] = self.subject
metadata["refinebio_compound"] = self.compound
metadata["refinebio_time"] = self.time
metadata["refinebio_platform"] = self.pretty_platform
metadata["refinebio_processed"] = self.has_raw
metadata["refinebio_annotations"] = [
data for data in self.sampleannotation_set.all().values_list("data", flat=True)
]
metadata["refinebio_developmental_stage"] = self.developmental_stage
if computed_file and computed_file.result and computed_file.result.processor:
metadata["refinebio_processor_id"] = computed_file.result.processor.id
metadata["refinebio_processor_name"] = computed_file.result.processor.name
metadata["refinebio_processor_version"] = computed_file.result.processor.version
return metadata
# Returns a set of ProcessorJob objects but we cannot specify
# that in type hints because it hasn't been declared yet.
def get_processor_jobs(self) -> Set:
processor_jobs = set()
for original_file in self.original_files.prefetch_related("processor_jobs").all():
for processor_job in original_file.processor_jobs.all():
processor_jobs.add(processor_job)
return processor_jobs
def get_most_recent_processor_job(self):
processor_jobs = self.get_processor_jobs()
if processor_jobs:
return min(processor_jobs, key=lambda job: job.created_at)
# Returns a set of DownloaderJob objects but we cannot specify
# that in type hints because it hasn't been declared yet.
def get_downloader_jobs(self) -> Set:
downloader_jobs = set()
for original_file in self.original_files.prefetch_related("downloader_jobs").all():
for downloader_job in original_file.downloader_jobs.all():
downloader_jobs.add(downloader_job)
return downloader_jobs
def get_most_recent_downloader_job(self):
downloader_jobs = self.get_downloader_jobs()
if downloader_jobs:
return min(downloader_jobs, key=lambda job: job.created_at)
def get_result_files(self):
"""Get all of the ComputedFile objects associated with this Sample"""
return self.computed_files.all()
def get_most_recent_smashable_result_file(self):
"""Get the most recent of the ComputedFile objects associated with this Sample"""
try:
if settings.RUNNING_IN_CLOUD:
latest_computed_file = self.computed_files.filter(
is_public=True, is_smashable=True, s3_bucket__isnull=False, s3_key__isnull=False
).latest()
else:
latest_computed_file = self.computed_files.filter(
is_public=True, is_smashable=True
).latest()
return latest_computed_file
except ComputedFile.DoesNotExist as e:
# This sample has no smashable files yet.
return None
def get_most_recent_quant_sf_file(self):
"""Returns the latest quant.sf file that was generated for this sample.
Note: We don't associate that file to the computed_files of this sample, that's
why we have to go through the computational results."""
return (
ComputedFile.objects.filter(
result__in=self.results.all(),
filename="quant.sf",
s3_key__isnull=False,
s3_bucket__isnull=False,
)
.order_by("-created_at")
.first()
)
@property
def pretty_platform(self):
"""Turns
[HT_HG-U133_Plus_PM] Affymetrix HT HG-U133+ PM Array Plate
into
Affymetrix HT HG-U133+ PM Array Plate (hthgu133pluspm)
"""
if "]" in self.platform_name:
platform_base = self.platform_name.split("]")[1].strip()
else:
platform_base = self.platform_name
return platform_base + " (" + self.platform_accession_code + ")"
@property
def experiment_accession_codes(self):
return [e.accession_code for e in self.experiments.all()]
@property
def contributed_metadata(self):
out = {}
for attrib in self.attributes.all():
val = {"value": attrib.get_value()}
if attrib.unit is not None:
val["unit"] = attrib.unit.human_readable_name
if attrib.probability is not None:
val["confidence"] = attrib.probability
try:
out[attrib.source.source_name][attrib.name.human_readable_name] = val
except KeyError:
out[attrib.source.source_name] = {attrib.name.human_readable_name: val}
return out
@property
def contributed_keywords(self):
out = {}
for kw in self.keywords.all():
try:
out[kw.source.source_name].append(kw.name.human_readable_name)
except KeyError:
out[kw.source.source_name] = [kw.name.human_readable_name]
return out