4
4
from pathlib import Path
5
5
6
6
from housekeeper .store .models import File , Version
7
- from pydantic .dataclasses import dataclass
8
7
9
8
from cg .apps .housekeeper .hk import HousekeeperAPI
10
9
from cg .apps .lims import LimsAPI
26
25
from cg .meta .workflow .analysis import AnalysisAPI
27
26
from cg .meta .workflow .utils .genome_build_helpers import genome_to_scout_format , get_genome_build
28
27
from cg .models .scout .scout_load_config import ScoutLoadConfig
28
+ from cg .store .api .data_classes import RNADNACollection
29
29
from cg .store .models import Analysis , Case , Customer , Sample
30
30
from cg .store .store import Store
31
31
32
32
LOG = logging .getLogger (__name__ )
33
33
34
34
35
- @dataclass
36
- class RNADNACollection :
37
- """Contains the id for an RNA sample, the name of its connected DNA sample,
38
- and a list of connected, uploaded DNA cases."""
39
-
40
- rna_sample_internal_id : str
41
- dna_sample_name : str
42
- dna_case_ids : list [str ]
43
-
44
-
45
35
class UploadScoutAPI :
46
36
"""Class that handles everything that has to do with uploading to Scout."""
47
37
@@ -197,9 +187,11 @@ def get_rna_alignment_cram(self, case_id: str, sample_id: str) -> File | None:
197
187
def upload_rna_alignment_file (self , case_id : str , dry_run : bool ) -> None :
198
188
"""Upload RNA alignment file to Scout."""
199
189
rna_case : Case = self .status_db .get_case_by_internal_id (case_id )
200
- rna_dna_collections : list [RNADNACollection ] = self .create_rna_dna_collections (rna_case )
190
+ rna_dna_collections : list [RNADNACollection ] = (
191
+ self .status_db .get_related_dna_cases_with_samples (rna_case )
192
+ )
201
193
for rna_dna_collection in rna_dna_collections :
202
- rna_sample_internal_id : str = rna_dna_collection .rna_sample_internal_id
194
+ rna_sample_internal_id : str = rna_dna_collection .rna_sample_id
203
195
dna_sample_name : str = rna_dna_collection .dna_sample_name
204
196
rna_alignment_cram : File | None = self .get_rna_alignment_cram (
205
197
case_id = case_id , sample_id = rna_sample_internal_id
@@ -337,9 +329,11 @@ def upload_rna_coverage_bigwig_to_scout(self, case_id: str, dry_run: bool) -> No
337
329
338
330
status_db : Store = self .status_db
339
331
rna_case = status_db .get_case_by_internal_id (case_id )
340
- rna_dna_collections : list [RNADNACollection ] = self .create_rna_dna_collections (rna_case )
332
+ rna_dna_collections : list [RNADNACollection ] = (
333
+ self .status_db .get_related_dna_cases_with_samples (rna_case )
334
+ )
341
335
for rna_dna_collection in rna_dna_collections :
342
- rna_sample_internal_id : str = rna_dna_collection .rna_sample_internal_id
336
+ rna_sample_internal_id : str = rna_dna_collection .rna_sample_id
343
337
dna_sample_name : str = rna_dna_collection .dna_sample_name
344
338
rna_coverage_bigwig : File | None = self .get_rna_coverage_bigwig (
345
339
case_id = case_id , sample_id = rna_sample_internal_id
@@ -373,7 +367,7 @@ def upload_omics_sample_id_to_scout(
373
367
self , dry_run : bool , rna_dna_collections : list [RNADNACollection ]
374
368
) -> None :
375
369
for rna_dna_collection in rna_dna_collections :
376
- rna_sample_internal_id : str = rna_dna_collection .rna_sample_internal_id
370
+ rna_sample_internal_id : str = rna_dna_collection .rna_sample_id
377
371
dna_sample_name : str = rna_dna_collection .dna_sample_name
378
372
for dna_case_id in rna_dna_collection .dna_case_ids :
379
373
LOG .info (
@@ -397,7 +391,7 @@ def upload_rna_fraser_outrider_to_scout(
397
391
"""Upload omics fraser and outrider file for a case to Scout."""
398
392
status_db : Store = self .status_db
399
393
for rna_dna_collection in rna_dna_collections :
400
- rna_sample_internal_id : str = rna_dna_collection .rna_sample_internal_id
394
+ rna_sample_internal_id : str = rna_dna_collection .rna_sample_id
401
395
dna_sample_name : str = rna_dna_collection .dna_sample_name
402
396
rna_fraser : File | None = self .get_rna_omics_fraser (case_id = case_id )
403
397
rna_outrider : File | None = self .get_rna_omics_outrider (case_id = case_id )
@@ -433,7 +427,7 @@ def upload_rna_fraser_outrider_to_scout(
433
427
def upload_rna_genome_build_to_scout (
434
428
self ,
435
429
dry_run : bool ,
436
- rna_case : str ,
430
+ rna_case : Case ,
437
431
rna_dna_collections : list [RNADNACollection ],
438
432
) -> None :
439
433
"""Upload RNA genome built for a RNA/DNA case to Scout."""
@@ -493,9 +487,11 @@ def upload_splice_junctions_bed_to_scout(self, dry_run: bool, case_id: str) -> N
493
487
status_db : Store = self .status_db
494
488
rna_case : Case = status_db .get_case_by_internal_id (case_id )
495
489
496
- rna_dna_collections : list [RNADNACollection ] = self .create_rna_dna_collections (rna_case )
490
+ rna_dna_collections : list [RNADNACollection ] = (
491
+ self .status_db .get_related_dna_cases_with_samples (rna_case )
492
+ )
497
493
for rna_dna_collection in rna_dna_collections :
498
- rna_sample_internal_id : str = rna_dna_collection .rna_sample_internal_id
494
+ rna_sample_internal_id : str = rna_dna_collection .rna_sample_id
499
495
dna_sample_name : str = rna_dna_collection .dna_sample_name
500
496
splice_junctions_bed : File | None = self .get_splice_junctions_bed (
501
497
case_id = case_id , sample_id = rna_sample_internal_id
@@ -606,7 +602,9 @@ def upload_rna_omics_to_scout(self, dry_run: bool, case_id: str) -> None:
606
602
"""Upload RNA omics files to Scout."""
607
603
status_db : Store = self .status_db
608
604
rna_case = status_db .get_case_by_internal_id (case_id )
609
- rna_dna_collections : list [RNADNACollection ] = self .create_rna_dna_collections (rna_case )
605
+ rna_dna_collections : list [RNADNACollection ] = (
606
+ self .status_db .get_related_dna_cases_with_samples (rna_case )
607
+ )
610
608
self .upload_omics_sample_id_to_scout (
611
609
dry_run = dry_run , rna_dna_collections = rna_dna_collections
612
610
)
@@ -666,48 +664,6 @@ def get_config_builder(self, analysis, hk_version) -> ScoutConfigBuilder:
666
664
667
665
return config_builders [analysis .workflow ]
668
666
669
- def create_rna_dna_collections (self , rna_case : Case ) -> list [RNADNACollection ]:
670
- return [
671
- self .create_rna_dna_collection (rna_sample = link .sample , customer = rna_case .customer )
672
- for link in rna_case .links
673
- ]
674
-
675
- def create_rna_dna_collection (self , rna_sample : Sample , customer : Customer ) -> RNADNACollection :
676
- """Creates a collection containing the given RNA sample id, its related DNA sample name, and
677
- a list of ids for the DNA cases connected to the DNA sample."""
678
- if not rna_sample .subject_id :
679
- raise CgDataError (
680
- f"Failed to link RNA sample { rna_sample .internal_id } to DNA samples - subject_id field is empty."
681
- )
682
-
683
- collaborators : set [Customer ] = customer .collaborators
684
- subject_id_samples : list [Sample ] = (
685
- self .status_db .get_samples_by_customer_ids_and_subject_id_and_is_tumour (
686
- customer_ids = [customer .id for customer in collaborators ],
687
- subject_id = rna_sample .subject_id ,
688
- is_tumour = rna_sample .is_tumour ,
689
- )
690
- )
691
-
692
- subject_id_dna_samples : list [Sample ] = self ._get_application_prep_category (
693
- subject_id_samples
694
- )
695
-
696
- if len (subject_id_dna_samples ) != 1 :
697
- raise CgDataError (
698
- f"Failed to upload files for RNA case: unexpected number of DNA sample matches for subject_id: "
699
- f"{ rna_sample .subject_id } . Number of matches: { len (subject_id_dna_samples )} "
700
- )
701
- dna_sample : Sample = subject_id_dna_samples [0 ]
702
- dna_cases : list [str ] = self ._dna_cases_related_to_dna_sample (
703
- dna_sample = dna_sample , collaborators = collaborators
704
- )
705
- return RNADNACollection (
706
- rna_sample_internal_id = rna_sample .internal_id ,
707
- dna_sample_name = dna_sample .name ,
708
- dna_case_ids = dna_cases ,
709
- )
710
-
711
667
def _dna_cases_related_to_dna_sample (
712
668
self , dna_sample : Sample , collaborators : set [Customer ]
713
669
) -> list [str ]:
0 commit comments