1
1
import datetime
2
2
import json
3
3
import logging
4
- from typing import Union , List , NoReturn
4
+ from typing import Union , List , NoReturn , Mapping
5
5
6
6
import peppy
7
7
from sqlalchemy import and_ , delete , select
@@ -90,11 +90,16 @@ def get(
90
90
subsample_list = list (subsample_dict .values ())
91
91
else :
92
92
subsample_list = []
93
+
94
+ # samples
95
+ samples_dict = {
96
+ sample_sa .row_number : sample_sa .sample
97
+ for sample_sa in found_prj .samples_mapping
98
+ }
99
+
93
100
project_value = {
94
101
CONFIG_KEY : found_prj .config ,
95
- SAMPLE_RAW_DICT_KEY : [
96
- sample_sa .sample for sample_sa in found_prj .samples_mapping
97
- ],
102
+ SAMPLE_RAW_DICT_KEY : [samples_dict [key ] for key in sorted (samples_dict )],
98
103
SUBSAMPLE_RAW_LIST_KEY : subsample_list ,
99
104
}
100
105
# project_value = found_prj.project_value
@@ -466,16 +471,25 @@ def update(
466
471
found_prj .name = found_prj .config [NAME_KEY ]
467
472
468
473
if "samples" in update_dict :
469
- if found_prj .samples_mapping :
470
- for sample in found_prj .samples_mapping :
471
- _LOGGER .debug (f"deleting samples: { str (sample )} " )
472
- session .delete (sample )
473
-
474
- self ._add_samples_to_project (
475
- found_prj ,
476
- update_dict ["samples" ],
477
- sample_table_index = update_dict ["config" ].get (SAMPLE_TABLE_INDEX_KEY ),
474
+ self ._update_samples (
475
+ namespace = namespace ,
476
+ name = name ,
477
+ tag = tag ,
478
+ samples_list = update_dict ["samples" ],
479
+ sample_name_key = update_dict ["config" ].get (
480
+ SAMPLE_TABLE_INDEX_KEY , "sample_name"
481
+ ),
478
482
)
483
+ # if found_prj.samples_mapping:
484
+ # for sample in found_prj.samples_mapping:
485
+ # _LOGGER.debug(f"deleting samples: {str(sample)}")
486
+ # session.delete(sample)
487
+ #
488
+ # self._add_samples_to_project(
489
+ # found_prj,
490
+ # update_dict["samples"],
491
+ # sample_table_index=update_dict["config"].get(SAMPLE_TABLE_INDEX_KEY),
492
+ # )
479
493
480
494
if "subsamples" in update_dict :
481
495
if found_prj .subsamples_mapping :
@@ -496,6 +510,67 @@ def update(
496
510
else :
497
511
raise ProjectNotFoundError ("No items will be updated!" )
498
512
513
+ def _update_samples (
514
+ self ,
515
+ namespace : str ,
516
+ name : str ,
517
+ tag : str ,
518
+ samples_list : List [Mapping ],
519
+ sample_name_key : str = "sample_name" ,
520
+ ) -> None :
521
+ """
522
+ Update samples in the project
523
+ This is a new method that instead of deleting all samples and adding new ones,
524
+ updates samples and adds new ones if they don't exist
525
+
526
+ :param samples_list: list of samples to be updated
527
+ :param sample_name_key: key of the sample name
528
+ :return: None
529
+ """
530
+ new_sample_names = [sample [sample_name_key ] for sample in samples_list ]
531
+ with Session (self ._sa_engine ) as session :
532
+ project = session .scalar (
533
+ select (Projects ).where (
534
+ and_ (
535
+ Projects .namespace == namespace , Projects .name == name , Projects .tag == tag
536
+ )
537
+ )
538
+ )
539
+ old_sample_names = [sample .sample_name for sample in project .samples_mapping ]
540
+ for old_sample in old_sample_names :
541
+ if old_sample not in new_sample_names :
542
+ session .execute (
543
+ delete (Samples ).where (
544
+ and_ (
545
+ Samples .sample_name == old_sample , Samples .project_id == project .id
546
+ )
547
+ )
548
+ )
549
+
550
+ order_number = 0
551
+ for new_sample in samples_list :
552
+ order_number += 1
553
+ if new_sample [sample_name_key ] not in old_sample_names :
554
+ project .samples_mapping .append (
555
+ Samples (
556
+ sample = new_sample ,
557
+ sample_name = new_sample [sample_name_key ],
558
+ row_number = order_number ,
559
+ )
560
+ )
561
+ else :
562
+ sample_mapping = session .scalar (
563
+ select (Samples ).where (
564
+ and_ (
565
+ Samples .sample_name == new_sample [sample_name_key ],
566
+ Samples .project_id == project .id ,
567
+ )
568
+ )
569
+ )
570
+ sample_mapping .sample = new_sample
571
+ sample_mapping .row_number = order_number
572
+ session .commit ()
573
+
499
574
@staticmethod
500
575
def __create_update_dict (update_values : UpdateItems ) -> dict :
501
576
"""
0 commit comments