Skip to content

Commit

Permalink
issue #76 - SeqAuto API - make sure you pull out bam file that matche…
Browse files Browse the repository at this point in the history
…s VCF, assign gene list to all samples from sequencing sample
  • Loading branch information
davmlaw committed Oct 22, 2024
1 parent 37ffa6b commit c9247c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
12 changes: 4 additions & 8 deletions seqauto/models/models_seqauto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,14 +1069,10 @@ def load_from_file(self, seqauto_run, **kwargs):
self.link_samples_if_exist()

def link_samples_if_exist(self, force_active=False):

try:
# SampleFromSequencingSample is only done after VCF import, so this may not be linked yet.
if sfss := self.sequencing_sample.samplefromsequencingsample_set.first():
sample = sfss.sample
self.create_and_assign_sample_gene_list(sample, force_active=force_active) # Also saves
except SampleFromSequencingSample.DoesNotExist:
pass
# SampleFromSequencingSample is only done after VCF import, so this may not be linked yet.
for sfss in self.sequencing_sample.samplefromsequencingsample_set.all():
sample = sfss.sample
self.create_and_assign_sample_gene_list(sample, force_active=force_active) # Also saves

def create_and_assign_sample_gene_list(self, sample: Sample, force_active=False):
logging.info("QCGeneList: %d - create_and_assign_sample_gene_list for %s", self.pk, sample)
Expand Down
25 changes: 11 additions & 14 deletions seqauto/serializers/seqauto_qc_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,17 @@ def get_object(data):
sample_sheet = SampleSheet.objects.get(hash=sheet_data["hash"], sequencing_run=sequencing_run)
sample_name = sequencing_sample_data["sample_name"]
sequencing_sample = SequencingSample.objects.get(sample_sheet=sample_sheet, sample_name=sample_name)
# Occasionally we could have multiple bam and VCF files in there that match path
# we want to make sure we get the bam out that is linked to the VCF file we pull out
bam_file_data = data.pop("bam_file")
# Occasionally we could have multiples in there, we don't really care so take 1st
bam_file_kwargs = {
"path": bam_file_data["path"],
"sequencing_run": sequencing_run,
"unaligned_reads__sequencing_sample": sequencing_sample
}
bam_file = BamFile.objects.filter(**bam_file_kwargs).first()
if not bam_file:
raise BamFile.DoesNotExist(f"No bam file for {bam_file_kwargs=}")

vcf_file_data = data.pop("vcf_file")
vcf_file_kwargs = {
"path": vcf_file_data["path"],
"bam_file": bam_file,
# Make sure bam file also matches
"bam_file__path": bam_file_data["path"],
"bam_file__sequencing_run": sequencing_run,
"bam_file__unaligned_reads__sequencing_sample": sequencing_sample

}
vcf_file = VCFFile.objects.filter(**vcf_file_kwargs).first()
if not vcf_file:
Expand All @@ -71,7 +67,7 @@ def get_object(data):

qc, _ = QC.objects.get_or_create(
sequencing_run=sequencing_run,
bam_file=bam_file,
bam_file=vcf_file.bam_file,
vcf_file=vcf_file,
defaults=defaults
)
Expand Down Expand Up @@ -112,8 +108,9 @@ def create(self, validated_data):
gene_list_text = ",".join(gene_list_data)
custom_text_gene_list = QCGeneList.create_gene_list(gene_list_text,
sequencing_sample=qc.sequencing_sample)
instance = QCGeneList.objects.create(qc=qc,
custom_text_gene_list=custom_text_gene_list)
instance, _created = QCGeneList.objects.update_or_create(qc=qc,
custom_text_gene_list=custom_text_gene_list,
defaults={"data_state": DataState.COMPLETE})

# With API - whatever we sent is always the active one
instance.link_samples_if_exist(force_active=True)
Expand Down

0 comments on commit c9247c3

Please sign in to comment.