Skip to content

Commit

Permalink
Merge pull request #905 from AlexsLemonade/davidsmejia/sum-estimated-…
Browse files Browse the repository at this point in the history
…demux-sample-counts

Sum estimated demux sample counts
  • Loading branch information
davidsmejia authored Sep 24, 2024
2 parents 9278ebd + 4f93509 commit 0520493
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 13 additions & 6 deletions api/scpca_portal/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,19 @@ def update_sample_aggregate_properties(self):
sample.seq_units = ", ".join(sorted(sample_seq_units, key=str.lower))
sample.technologies = ", ".join(sorted(sample_technologies, key=str.lower))

if multiplexed_library := sample.libraries.filter(is_multiplexed=True).first():
multiplexed_ids = set(s.scpca_id for s in multiplexed_library.samples.all())
sample.multiplexed_with = sorted(multiplexed_ids.difference({sample.scpca_id}))
sample.demux_cell_count_estimate = multiplexed_library.metadata[
"sample_cell_estimates"
].get(sample.scpca_id)
if multiplexed_libraries := sample.libraries.filter(is_multiplexed=True):
# Cache all sample ID's related through the multiplexed libraries.
sample.multiplexed_with = list(
sample.multiplexed_with_samples.order_by("scpca_id").values_list(
"scpca_id", flat=True
)
)
# Sum demux_cell_count_estimate from all related library's
# sample_cell_estimates for that sample.
sample.demux_cell_count_estimate = sum(
library.metadata["sample_cell_estimates"].get(sample.scpca_id, 0)
for library in multiplexed_libraries
)
else:
sample.sample_cell_count_estimate = sample_cell_count_estimate

Expand Down
8 changes: 8 additions & 0 deletions api/scpca_portal/models/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ def modalities(self):
def computed_files(self):
return self.sample_computed_files.order_by("created_at")

@property
def multiplexed_with_samples(self):
return (
Sample.objects.filter(libraries__in=self.libraries.filter(is_multiplexed=True))
.distinct()
.exclude(scpca_id=self.scpca_id)
)

@property
def multiplexed_ids(self):
multiplexed_sample_ids = [self.scpca_id]
Expand Down

0 comments on commit 0520493

Please sign in to comment.