-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetoff_workflows.py
executable file
·1202 lines (1111 loc) · 51.6 KB
/
setoff_workflows.py
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""setoff_workflows.py
Collect sequencing runs and initiate runfolder processing for those requiring
processing. See Readme and docstrings for further details. Contains the following classes:
- SequencingRuns
Collects sequencing runs and initiates runfolder processing for those sequencing runs requiring processing
- ProcessRunfolder
A new instance of this class is initiated for each runfolder being assessed. Calls methods to process and
upload a runfolder including creation of DNAnexus project, upload of data using upload_runfolder,
building and execution of dx run commands to set off sample workflows and apps, creation of decision
support tool upload scripts, and sending of pipeline emails
- DevPipeline
Collate DNAnexus commands for development runs. This runtype has no decision
support upload or postprocessing commands, or SQL queries
- ArcherDxPipeline
Collate DNAnexus commands for ArcherDX runs. This runtype has no decision
support upload or postprocessing commands
- SnpPipeline
Collate DNAnexus commands for SNP runs. This run type has no decision
support upload or post processing commands
- OncoDeepPipeline
Collate DNAnexus commands for OncoDEEP runs. This runtype has no post processing commands
- TsoPipeline
Collate commands for TSO workflow. This runtype has postprocessing commands, decision
support upload commands, and SQL queries
- WesPipeline
Collate commands for WES workflow. This runtype has no postprocesing commands
- CustomPanelsPipeline
Collate commands for Custom Panels workflow. This runtype has no postprocesing commands
"""
import sys
import os
import re
from itertools import chain
from typing import Optional, Union
from ad_logger.ad_logger import AdLogger, shutdown_logs
from config.ad_config import SWConfig
import logging
from upload_runfolder.upload_runfolder import UploadRunfolder
from toolbox.toolbox import (
return_scriptlog_config,
test_upload_software,
RunfolderObject,
RunfolderSamples,
read_lines,
get_num_processed_runfolders,
get_credential,
git_tag,
write_lines,
execute_subprocess_command,
get_samplename_dict,
)
from setoff_workflows.pipeline_emails import PipelineEmails
from setoff_workflows.build_dx_commands import (
BuildRunfolderDxCommands,
BuildSampleDxCommands,
)
from toolbox.toolbox import script_start_logmsg, script_end_logmsg
# Set up script logging
ad_logger_obj = AdLogger(__name__, "sw", return_scriptlog_config()["sw"])
script_logger = ad_logger_obj.get_logger()
class SequencingRuns(SWConfig):
"""
Collects sequencing runs and initiates runfolder processing for those
sequencing runs requiring processing
Methods:
setoff_processing()
Call methods to collect runfolders for processing
set_runfolders()
Update self.runfolders list with NGS runfolders in the runfolders directory
requires_processing(rf_obj)
Calls other methods to determine whether the runfolder requires processing (demultiplexing
has finished successfully and the runfolder has not already been uploaded)
has_demultiplexed(rf_obj)
Check if demultiplexing has already been performed and completed sucessfully
already_uploaded(rf_obj)
Checks for presence of DNAnexus upload flag file(denotes that the runfolder has
already been processed)
process_runfolder(rf_obj)
If software tests pass, set up logging and pass rf_obj to the ProcessRunfolder class for
processing, shutting down logs upon completion
"""
def __init__(self):
"""
Constructor for the SequencingRuns class
"""
def setoff_processing(self) -> None:
"""
Call methods to collect runfolders for processing. Called by __main__.py
:return None:
"""
processed_runfolders = []
script_start_logmsg(script_logger, __file__)
runs_to_process = self.set_runfolders()
if test_upload_software(script_logger):
for rf_obj in runs_to_process:
if not os.path.exists(rf_obj.upload_flagfile):
script_logger.info(
script_logger.log_msgs["start_runfolder_proc"],
rf_obj.runfolder_name,
)
if self.process_runfolder(rf_obj):
processed_runfolders.append(rf_obj.runfolder_name)
get_num_processed_runfolders(script_logger, processed_runfolders)
script_end_logmsg(script_logger, __file__)
def set_runfolders(self) -> list:
"""
Update self.runs_to_process list with NGS runfolders in the runfolders directory
that match the runfolder pattern, and require processing by the script
:return (list): List of runfolder objects that require processing
"""
runs_to_process = []
for folder in os.listdir(SWConfig.RUNFOLDERS):
if os.path.isdir(os.path.join(SWConfig.RUNFOLDERS, folder)) and re.compile(
SWConfig.RUNFOLDER_PATTERN
).match(folder):
script_logger.info(
script_logger.log_msgs["runfolder_identified"], folder
)
rf_obj = RunfolderObject(folder, SWConfig.TIMESTAMP)
if self.requires_processing(rf_obj):
runs_to_process.append(rf_obj)
return runs_to_process
def requires_processing(self, rf_obj: object) -> Optional[bool]:
"""
Calls other methods to determine whether the runfolder requires processing (demultiplexing
has finished successfully and the runfolder has not already been uploaded)
:param rf_obj (obj): RunfolderObject object (contains runfolder-specific attributes)
:return (Optional[bool]): Returns true if runfolder requires processing, else None
"""
if self.has_demultiplexed(rf_obj):
if self.already_uploaded(rf_obj):
script_logger.info(
script_logger.log_msgs["runfolder_prev_proc"],
rf_obj.runfolder_name,
)
else:
script_logger.info(
script_logger.log_msgs["runfolder_requires_proc"],
rf_obj.runfolder_name,
)
return True
def has_demultiplexed(self, rf_obj: object) -> Optional[bool]:
"""
Check if demultiplexing has already been performed and completed sucessfully. Checks the
demultiplex log file exists, and if present checks the expected success string is in the
last line of the log file.
:param rf_obj (obj): RunfolderObject object (contains runfolder-specific attributes)
:return (Optional[bool]): Return True if runfolder already demultiplexed, else None
"""
if os.path.isfile(rf_obj.bcl2fastqlog_file):
logfile_list = read_lines(rf_obj.bcl2fastqlog_file)
completed_strs = [
SWConfig.STRINGS["demultiplex_not_required_msg"].partition(" ")[-1],
SWConfig.STRINGS["demultiplex_success"],
]
if logfile_list:
if any(
re.search(success_str, logfile_list[-1])
for success_str in completed_strs
):
script_logger.info(script_logger.log_msgs["demux_complete"])
return True
else:
script_logger.info(script_logger.log_msgs["success_string_absent"])
else:
script_logger.info(script_logger.log_msgs["bcl2fastqlog_empty"])
else:
script_logger.info(script_logger.log_msgs["not_yet_demultiplexed"])
def already_uploaded(self, rf_obj: object) -> Optional[bool]:
"""
Checks for presence of DNAnexus upload flag file (denotes that the runfolder has already been processed)
:param rf_obj (obj): RunfolderObject object (contains runfolder-specific attributes)
:return (Optional[bool]): Returns True if runfolder already uploaded, else None
"""
if os.path.isfile(rf_obj.upload_flagfile):
script_logger.info(script_logger.log_msgs["ua_file_present"])
return True
else:
script_logger.info(script_logger.log_msgs["ua_file_absent"])
def process_runfolder(self, rf_obj: object) -> Optional[bool]:
"""
If software tests pass, set up logging and pass rf_obj to the ProcessRunfolder class for processing,
shutting down logs upon completion. Append to self.processed_runfolders
:param rf_obj (obj): RunfolderObject object (contains runfolder-specific attributes)
:return (str): True name if runfolder has been processed
"""
loggers = rf_obj.get_runfolder_loggers(__package__) # Get dictionary of loggers
loggers["sw"].info(
loggers["sw"].log_msgs["ad_version"],
git_tag(),
)
samplename_dict = get_samplename_dict(loggers["sw"], rf_obj.samplesheet_path)
if samplename_dict:
ProcessRunfolder(rf_obj, loggers)
for logger_name in loggers.keys():
shutdown_logs(loggers[logger_name]) # Shut down logging
script_logger.info(
script_logger.log_msgs["runfolder_processed"],
rf_obj.runfolder_name,
)
return True
class ProcessRunfolder(SWConfig):
"""
A new instance of this class is initiated for each runfolder being assessed. Calls methods to process and
upload a runfolder including creation of DNAnexus project, upload of data using upload_runfolder,
building and execution of dx run commands to set off sample workflows and apps, creation of decision
support tool upload scripts, and sending of pipeline emails
Attributes:
rf_obj (obj): RunfolderObject() object (contains runfolder-specific attributes)
loggers (dict): Dict of loggers
dnanexus_auth (str): DNAnexus auth token
rf_samples_obj (object): RunfolderSamples object
users_dict (dict): Dictionary of users and admins requiring access to the DNAnexus project
nexus_identifiers (dict): Dictionary containing project name and ID
upload_runfolder (obj): UploadRunfolder() object with methods that can be called to upload
files to the DNAnexus project
upload_cmds (dict): Dictionary of commands for uploading files to the DNAnexus project
pre_pipeline_upload_dict (dict): Dict of files to upload prior to pipeline setoff, and commands
pipeline_obj (object): Object with the workflow_cmds, dx_postprocessing_cmds,
decision_support_upload_cmds and sql_queries as attributes
pipeline_emails (obj): PipelineEmails object for sending the start of pipeline emails
Methods:
get_users_dict()
Create a dictionary of users and admins that require access to the DNAnexus project
write_project_creation_script()
Write the script that creates the DNAnexus project and shares it with the
required users with the required access level
run_project_creation_script()
Set off the project creation script using subprocess, return project ID
get_upload_cmds()
Build file upload commands
split_tso_samplesheet()
Split tso500 SampleSheet into parts with x samples per SampleSheet (no.
defined in TSO_BATCH_SIZE) and write to runfolder
read_tso_samplesheet()
Read required lines from the TSO SampleSheet
create_file_upload_dict()
Create dictionary of files to upload prior to setting off the pipeline, and
the upload commands required
build_dx_commands()
Calls other classes to generate the required commands for the runfolder
write_dx_run_cmds(pipeline_obj)
Write dx run commands to the dx run script, post dx run script, and decision
support upload script for the runfolder
pre_pipeline_upload()
Uploads the files in the rf_obj.pre_pipeline_upload_dict for the
runfolder. Calls the tso runfolder upload function if the runfolder is tso
upload_to_dnanexus(filetype, file_upload_dict)
Passes the command and file list in file_upload_dict to upload_runfolder.upload_files()
which writes log messages to the upload agent log within the runfolder
upload_rest_of_runfolder()
Backs up the rest of the runfolder, ignoring files dependent upon the type of run
run_dx_run_commands()
Execute the dx run bash script
post_pipeline_upload()
Uploads the rest of the runfolder if not a tso run, and uploads the
runfolder logfiles (upload_agent file is not uploaded because it is being
written to as the upload is taking place)
"""
def __init__(self, rf_obj: RunfolderObject, loggers: dict):
"""
Constructor for the RunfolderProcessor class. Calls the class methods
:param rf_obj (obj): RunfolderObject object (contains runfolder-specific
attributes)
:param loggers (dict): Dict of loggers
"""
self.rf_obj = rf_obj
self.loggers = loggers
self.dnanexus_auth = get_credential(SWConfig.CREDENTIALS["dnanexus_authtoken"])
open(
self.rf_obj.upload_flagfile, "w"
).close() # Create upload flag file (prevents processing by other script runs)
self.rf_samples_obj = RunfolderSamples(self.rf_obj, self.loggers["sw"])
self.users_dict = self.get_users_dict()
self.write_project_creation_script()
self.nexus_identifiers = {
"proj_name": self.rf_samples_obj.nexus_paths["proj_name"],
"proj_id": self.run_project_creation_script(),
}
self.upload_runfolder = UploadRunfolder(
self.loggers["backup"],
self.rf_obj.runfolder_name,
self.rf_obj.runfolderpath,
self.rf_obj.upload_flagfile,
self.nexus_identifiers,
)
self.upload_cmds = self.get_upload_cmds()
self.pre_pipeline_upload_dict = self.create_file_upload_dict()
self.pipeline_obj = self.build_dx_commands()
self.write_dx_run_cmds()
self.pre_pipeline_upload()
self.run_dx_run_commands()
self.pipeline_emails = PipelineEmails(
self.rf_obj,
self.rf_samples_obj,
self.pipeline_obj.sql_queries,
self.loggers["sw"],
)
if self.pipeline_obj.sql_queries:
self.pipeline_emails.send_sql_email()
self.pipeline_emails.send_samples_email()
self.post_pipeline_upload()
def get_users_dict(self) -> dict:
"""
Create a dictionary of users and admins that require access to the DNAnexus project. This also
includes dry lab DNAnexus IDs if applicable for the samples in the runfolder. These are taken
from the per-sample panel_settings in the samples_dict. This is required because some samples
are analysed at dry labs, with access to projects only given where there is a sample for that
dry lab on the run
:return (dict): Dictionary of users and admins requiring access to the DNAnexus project
"""
dry_lab_list = list(
set(
[
v["panel_settings"]["dry_lab"]
for k, v in self.rf_samples_obj.samples_dict.items()
if v["panel_settings"]["dry_lab"]
]
)
)
if True in dry_lab_list:
viewers = list(
chain([SWConfig.BSPS_ID], SWConfig.DNANEXUS_USERS["viewers"])
)
else:
viewers = SWConfig.DNANEXUS_USERS["viewers"]
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["view_users"],
viewers,
)
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["admin_users"],
SWConfig.DNANEXUS_USERS["admins"],
)
return {
"viewers": {
"user_list": viewers,
"permissions": "VIEW",
},
"admins": {
"user_list": SWConfig.DNANEXUS_USERS["admins"],
"permissions": "ADMINISTER",
},
}
def write_project_creation_script(self) -> None:
"""
Write the script that creates the DNAnexus project and shares it with the
required users with the required access levels. The project is created using
the project creation command which utilises the DNAnexus sdk. This command and
the project sharing commands are written to a bash script
:return None:
"""
lines_to_write = [
SWConfig.SDK_SOURCE,
f"AUTH={self.dnanexus_auth}",
SWConfig.DX_CMDS["create_proj"]
% (
SWConfig.PROD_ORGANISATION,
self.rf_samples_obj.nexus_paths["proj_name"],
),
]
# Give view and admin permissions for project
for permissions_level in self.users_dict.keys():
if self.users_dict[permissions_level]["user_list"]:
for user in self.users_dict[permissions_level]["user_list"]:
lines_to_write.append(
SWConfig.DX_CMDS["invite_user"]
% (
user,
self.users_dict[permissions_level]["permissions"],
)
)
else:
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["no_users"],
permissions_level,
)
lines_to_write.append("echo $PROJECT_ID")
write_lines(self.rf_obj.proj_creation_script, "w", lines_to_write)
def run_project_creation_script(self) -> str:
"""
Set off the project creation script using subprocess. The output of this command is
checked to ensure it meets the expected success pattern. If unsuccessful, exit script
:return projectid (str): Project ID of the created project
"""
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["creating_proj"],
self.rf_obj.proj_creation_script,
)
project_creation_cmd = f"bash {self.rf_obj.proj_creation_script}"
project_id, err, returncode = execute_subprocess_command(
project_creation_cmd, self.loggers["sw"], "exit_on_fail"
)
if returncode == 0:
return project_id
else:
self.loggers["sw"].error(
self.loggers["sw"].log_msgs["proj_creation_fail"],
self.rf_samples_obj["proj_name"],
err,
)
sys.exit(1)
def get_upload_cmds(self) -> dict:
"""
Build file upload commands
:return upload_cmds (dict): Dictionary of commands for uploading
files to the DNAnexus project
"""
upload_cmds = {
"cd": SWConfig.DX_CMDS["file_upload_cmd"]
% (
self.rf_obj.dnanexus_auth,
self.nexus_identifiers["proj_id"],
"/QC",
" ".join(
f"'{cd_file}'" for cd_file in self.rf_obj.cluster_density_files
),
),
"bcl2fastq_qc": SWConfig.DX_CMDS["file_upload_cmd"]
% (
self.rf_obj.dnanexus_auth,
self.nexus_identifiers["proj_id"],
f"{os.path.join(self.rf_samples_obj.nexus_paths['fastqs_dir'], 'Stats')}",
self.rf_obj.bcl2fastqstats_file,
),
"logfiles": SWConfig.DX_CMDS["file_upload_cmd"]
% (
self.rf_obj.dnanexus_auth,
self.nexus_identifiers["proj_id"],
self.rf_samples_obj.nexus_paths["logfiles_dir"],
" ".join(f"'{logfile}'" for logfile in self.rf_obj.logfiles_to_upload),
),
}
if self.rf_samples_obj.pipeline == "tso500":
self.rf_obj.tso_ss_list = self.split_tso_samplesheet()
samplesheet_paths = [
os.path.join(self.rf_obj.runfolderpath, ss)
for ss in self.rf_obj.tso_ss_list
]
upload_cmds["runfolder_samplesheet"] = SWConfig.DX_CMDS[
"file_upload_cmd"
] % (
self.rf_obj.dnanexus_auth,
self.nexus_identifiers["proj_id"],
f"/{self.rf_obj.runfolder_name}",
" ".join(f"'{samplesheet}'" for samplesheet in samplesheet_paths),
)
if self.rf_samples_obj.pipeline == "oncodeep":
upload_cmds["masterfile"] = SWConfig.DX_CMDS["file_upload_cmd"] % (
self.rf_obj.dnanexus_auth,
self.nexus_identifiers["proj_id"],
f"/",
self.rf_obj.runfolder_masterfile_path,
)
if self.rf_samples_obj.pipeline != "tso500":
# tso500 run is not demultiplexed locally so there are no fastqs
# All other runfolders have fastqs in the BaseCalls directory
upload_cmds["fastqs"] = SWConfig.DX_CMDS["file_upload_cmd"] % (
self.rf_obj.dnanexus_auth,
self.nexus_identifiers["proj_id"],
self.rf_samples_obj.nexus_paths["fastqs_dir"],
" ".join(
[
self.rf_samples_obj.fastqs_str,
self.rf_samples_obj.undetermined_fastqs_str,
]
),
)
upload_cmds["runfolder_samplesheet"] = SWConfig.DX_CMDS[
"file_upload_cmd"
] % (
self.rf_obj.dnanexus_auth,
self.nexus_identifiers["proj_id"],
"/",
self.rf_obj.runfolder_samplesheet_path,
)
return upload_cmds
def split_tso_samplesheet(self) -> list:
"""
Split tso500 SampleSheet into parts with x samples per SampleSheet (no.
defined in TSO_BATCH_SIZE), and write to runfolder
:return (list): SampleSheet names
"""
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["splitting_tso_samplesheet"],
SWConfig.TSO_BATCH_SIZE,
self.rf_obj.samplesheet_path,
)
samplesheet_list = []
samples, samplesheet_header = self.read_tso_samplesheet()
# Split samples into batches (size specified in config)
batches = [
samples[i : i + SWConfig.TSO_BATCH_SIZE]
for i in range(0, len(samples), SWConfig.TSO_BATCH_SIZE)
]
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["tso_batches_count"],
len(batches),
)
# Create new SampleSheets named "PartXofY", add SampleSheet to list
# Capture path for SampleSheet in runfolder
for samplesheet_count, batch in enumerate(batches, start=1):
# Capture SampleSheet file path to write SampleSheet paths to the runfolder
samplesheet_filepath = (
f'{self.rf_obj.runfolder_samplesheet_path.split(".csv")[0]}'
f"Part{samplesheet_count}of{len(batches)}.csv"
)
# Capture SampleSheet name to write to list- use runfolder name
samplesheet_name = (
f"{self.rf_obj.runfolder_name}_SampleSheetPart"
f"{samplesheet_count}of{len(batches)}.csv"
)
samplesheet_list.append(samplesheet_name)
write_lines(samplesheet_filepath, "w", samplesheet_header)
write_lines(samplesheet_filepath, "a", batch)
samplesheet_list.append(self.rf_obj.samplesheet_name)
return samplesheet_list
def read_tso_samplesheet(self) -> Union[list, list]:
"""
Read required lines from the TSO SampleSheet
:return samples (list): Samples read from SampleSheet
:return samplesheet_header (list): SampleSheet header lines
"""
samples, samplesheet_header = [], []
no_sample_lines = 0
expected_data_headers = ["Sample_ID", "Sample_Name", "index"]
header_identified = False
# Read all lines from the sample sheet
samplesheet = read_lines(self.rf_obj.runfolder_samplesheet_path)
for line in samplesheet:
line = line.strip("\n")
if any(header in line for header in expected_data_headers):
samplesheet_header.append(line) # Extract header and add to list
header_identified = True
elif (
not header_identified
): # Extract lines above the header and add to list
samplesheet_header.append(line)
# Skip empty lines (check first element of the line, after splitting on comma)
elif header_identified and len(line.split(",")[0]) > 2:
samples.append(line)
no_sample_lines += 1
elif len(line.split(",")[0]) < 2: # Skip empty lines
pass
return samples, samplesheet_header
def create_file_upload_dict(self) -> dict:
"""
Create dictionary of files to upload prior to setting off the pipeline,
and the upload commands required
:return pre_pipeline_upload_dict (dict): Dict of files to upload prior to
pipeline setoff, and commands
"""
pre_pipeline_upload_dict = {
"cluster density": {
"cmd": self.upload_cmds["cd"],
"files_list": self.rf_obj.cluster_density_files,
},
}
if self.rf_samples_obj.pipeline == "tso500": # Add SampleSheet entry
pre_pipeline_upload_dict["runfolder_samplesheet"] = {
"cmd": self.upload_cmds["runfolder_samplesheet"],
"files_list": [
os.path.join(self.rf_obj.runfolderpath, ss)
for ss in self.rf_obj.tso_ss_list
],
}
else:
pre_pipeline_upload_dict["runfolder_samplesheet"] = {
"cmd": self.upload_cmds["runfolder_samplesheet"],
"files_list": [self.rf_obj.runfolder_samplesheet_path],
}
pre_pipeline_upload_dict["fastqs"] = {
"cmd": self.upload_cmds["fastqs"],
"files_list": [
*self.rf_samples_obj.fastqs_list,
*self.rf_samples_obj.undetermined_fastqs_list,
],
}
pre_pipeline_upload_dict["bcl2fastq_qc"] = {
"cmd": self.upload_cmds["bcl2fastq_qc"],
"files_list": [self.rf_obj.bcl2fastqstats_file],
}
if self.rf_samples_obj.pipeline == "oncodeep": # Add MasterFile entry
pre_pipeline_upload_dict["masterfile"] = {
"cmd": self.upload_cmds["masterfile"],
"files_list": [self.rf_obj.runfolder_masterfile_path],
}
return pre_pipeline_upload_dict
def build_dx_commands(self) -> object:
"""
Build dx run commands (pipeline-dependent) by calling the relevant classes and
appending to the cmd lists
:return pipeline_obj (object): Object with the workflow_cmds, dx_postprocessing_cmds,
decision_support_upload_cmds and sql_queries as attributes
"""
if self.rf_samples_obj.pipeline == "tso500":
pipeline_obj = TsoPipeline(
self.rf_obj, self.rf_samples_obj, self.loggers["sw"]
)
if self.rf_samples_obj.pipeline == "archerdx":
pipeline_obj = ArcherDxPipeline(
self.rf_obj, self.rf_samples_obj, self.loggers["sw"]
)
if self.rf_samples_obj.pipeline == "wes":
pipeline_obj = WesPipeline(
self.rf_obj, self.rf_samples_obj, self.loggers["sw"]
)
if self.rf_samples_obj.pipeline == "oncodeep":
pipeline_obj = OncoDeepPipeline(
self.rf_obj, self.rf_samples_obj, self.loggers["sw"]
)
if self.rf_samples_obj.pipeline == "snp":
pipeline_obj = SnpPipeline(
self.rf_obj, self.rf_samples_obj, self.loggers["sw"]
)
if self.rf_samples_obj.pipeline == "pipe":
pipeline_obj = CustomPanelsPipeline(
self.rf_obj, self.rf_samples_obj, self.loggers["sw"]
)
if self.rf_samples_obj.pipeline == "dev":
pipeline_obj = DevPipeline(
self.rf_obj, self.rf_samples_obj, self.loggers["sw"]
)
self.loggers["sw"].info(self.loggers["sw"].log_msgs["cmds_built"])
return pipeline_obj
def write_dx_run_cmds(self) -> None:
"""
Write dx run commands to the dx run script, post dx run script, and decision support upload
script for the runfolder. Remove any None values
from the command list
:param pipeline_obj (object): Object with the workflow_cmds, dx_postprocessing_cmds,
decision_support_upload_cmds and sql_queries as attributes
:return None:
"""
base_variables = [
SWConfig.SDK_SOURCE,
f"AUTH={get_credential(SWConfig.CREDENTIALS['dnanexus_authtoken'])}",
f"PROJECT_ID={self.nexus_identifiers['proj_id']}",
f"PROJECT_NAME={self.nexus_identifiers['proj_name']}",
f"RUNFOLDER_NAME={self.rf_obj.runfolder_name}",
SWConfig.EMPTY_DEPENDS,
]
if self.pipeline_obj.workflow_cmds: # Write dx run commands
self.pipeline_obj.workflow_cmds[:0] = base_variables
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["writing_cmds"],
self.rf_obj.runfolder_dx_run_script,
)
write_lines( # Write commands to dx run script
self.rf_obj.runfolder_dx_run_script,
"w",
list(filter(None, self.pipeline_obj.workflow_cmds)),
)
if self.pipeline_obj.dx_postprocessing_cmds: # Write postprocessing commands
self.pipeline_obj.dx_postprocessing_cmds[:0] = base_variables
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["writing_cmds"],
self.rf_obj.runfolder_dx_run_script,
)
write_lines(
self.rf_obj.post_run_dx_run_script,
"w",
list(filter(None, self.pipeline_obj.dx_postprocessing_cmds)),
)
if self.pipeline_obj.decision_support_upload_cmds:
self.pipeline_obj.decision_support_upload_cmds[:0] = base_variables
write_lines(
self.rf_obj.decision_support_upload_script,
"w",
list(filter(None, self.pipeline_obj.decision_support_upload_cmds)),
)
def pre_pipeline_upload(self) -> None:
"""
Uploads the files in the pre_pipeline_upload_dict for the runfolder.
Calls the upload_rest_of_runfolder function if the runfolder is tso500
:return None:
"""
for filetype in self.pre_pipeline_upload_dict.keys():
self.upload_to_dnanexus(filetype, self.pre_pipeline_upload_dict)
if self.rf_samples_obj.pipeline == "tso500":
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["tso_backup"],
)
self.upload_rest_of_runfolder()
def upload_to_dnanexus(self, filetype: str, file_upload_dict: dict) -> None:
"""
Passes the command and file list in file_upload_dict to upload_runfolder.upload_files()
which writes log messages to the backup runfolder log file
:param filetype (str): Name of the file upload type
:param file_upload_dict (dict): Dictionary of files for upload
:return None:
"""
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["uploading_files"], filetype
)
result = self.upload_runfolder.upload_files(
file_upload_dict[filetype]["cmd"],
file_upload_dict[filetype]["files_list"],
)
if result == "success":
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["upload_success"], filetype
)
if result == "fail":
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["upload_fail"],
filetype,
self.rf_obj.upload_runfolder_logfile,
)
elif result is list:
self.loggers["sw"].error(
self.loggers["sw"].log_msgs["nonexistent_files"], result
)
def upload_rest_of_runfolder(self) -> None:
"""
Backs up the rest of the runfolder. Specifies which files to ignore (excludes BCL files for all
runs except tso500 runs for which they are needed for demultiplexing on DNAnexus). Calls
upload_runfolder.upload_rest_of_runfolder(ignore), passing a run-dependent ignore string, and
the this handles the runfolder upload. upload_runfolder writes log messages to the upload
runfolder log file. If unsuccessful, exit script
:return None:
"""
# Build upload_runfolder.py commands, ignoring some files
if self.rf_samples_obj.pipeline in ["tso500", "dev"]:
ignore = "" # Upload BCL files for tso500 and dev runs
else:
ignore = "/L00"
try:
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["uploading_rf"],
ignore,
self.rf_obj.upload_runfolder_logfile,
)
self.upload_runfolder.upload_rest_of_runfolder(ignore)
except Exception as exception:
self.loggers["sw"].error(
self.loggers["sw"].log_msgs["upload_rf_error"],
exception,
self.rf_obj.sw_runfolder_logfile,
self.rf_obj.upload_runfolder_logfile,
)
sys.exit(1)
def run_dx_run_commands(self) -> None:
"""
Execute the dx run bash script
:return None:
"""
dx_run_cmd = f"bash {self.rf_obj.runfolder_dx_run_script}"
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["running_cmds"],
)
out, err, returncode = execute_subprocess_command(
dx_run_cmd, self.loggers["sw"], "exit_on_fail"
)
if returncode != 0:
self.loggers["sw"].error(
self.loggers["sw"].log_msgs["dx_run_err"],
dx_run_cmd,
out,
err,
)
else:
self.loggers["sw"].info(
self.loggers["sw"].log_msgs["dx_run_success"],
self.rf_obj.runfolder_name,
)
def post_pipeline_upload(self) -> None:
"""
Uploads the rest of the runfolder if not a tso run
:return None:
"""
if self.rf_samples_obj.pipeline != "tso500":
self.upload_rest_of_runfolder()
logfiles_upload_dict = {
"logfiles": {
"cmd": self.upload_cmds["logfiles"],
"files_list": self.rf_obj.logfiles_to_upload,
},
}
for filetype in logfiles_upload_dict.keys(): # Upload logfiles for all runtypes
self.upload_to_dnanexus(filetype, logfiles_upload_dict)
class DevPipeline:
"""
Collate DNAnexus commands for development runs. This runtype has no decision
support upload or postprocessing commands, or SQL queries
"""
def __init__(self, rf_obj: object, rf_samples: object, logger: logging.Logger):
"""
Constructor for the DevPipeline class
"""
self.rf_obj = rf_obj
self.rf_samples_obj = rf_samples
self.logger = logger
self.workflow_cmds = []
self.sql_queries = False
self.decision_support_upload_cmds, self.dx_postprocessing_cmds = False, False
self.rf_cmds_obj = BuildRunfolderDxCommands(self.rf_obj, self.logger)
for sample_name in self.rf_samples_obj.samples_dict.keys():
sample_cmds_obj = BuildSampleDxCommands(
self.rf_obj.runfolder_name,
self.rf_samples_obj.samples_dict[sample_name],
self.logger,
)
self.workflow_cmds.append(sample_cmds_obj.create_fastqc_cmd())
self.workflow_cmds.append(SWConfig.UPLOAD_ARGS["depends_list"])
# Return downstream app commands
self.workflow_cmds.extend(
self.rf_cmds_obj.return_multiqc_cmds(self.rf_samples_obj.pipeline)
)
self.workflow_cmds.append(self.rf_cmds_obj.create_duty_csv_cmd())
class ArcherDxPipeline:
"""
Collate DNAnexus commands for ArcherDX runs. This runtype has no decision
support upload or postprocessing commands
"""
def __init__(self, rf_obj: object, rf_samples: object, logger: logging.Logger):
self.rf_obj = rf_obj
self.rf_samples_obj = rf_samples
self.logger = logger
self.workflow_cmds = []
self.sql_queries = []
self.dx_postprocessing_cmds = False
self.decision_support_upload_cmds = []
self.rf_cmds_obj = BuildRunfolderDxCommands(self.rf_obj, self.logger)
for sample_name in self.rf_samples_obj.samples_dict.keys():
sample_cmds_obj = BuildSampleDxCommands(
self.rf_obj.runfolder_name,
self.rf_samples_obj.samples_dict[sample_name],
self.logger,
)
self.workflow_cmds.append(sample_cmds_obj.create_fastqc_cmd())
self.workflow_cmds.append(SWConfig.UPLOAD_ARGS["depends_list"])
self.sql_queries.append(
sample_cmds_obj.return_oncology_query()
) # Get SQL queries
# Return downstream app commands
self.workflow_cmds.extend(
self.rf_cmds_obj.return_multiqc_cmds(self.rf_samples_obj.pipeline)
)
self.workflow_cmds.append(self.rf_cmds_obj.create_duty_csv_cmd())
class SnpPipeline: # TODO eventually remove this and associated pipeline-specific functions
"""
Collate DNAnexus commands for SNP runs. This run type has no decision
support upload or post processing commands
Attributes
workflow_cmd (str): Dx run command for the sample workflow
query (str): Sample-level SQL query
"""
def __init__(self, rf_obj: object, rf_samples: object, logger: logging.Logger):
self.rf_obj = rf_obj
self.rf_samples_obj = rf_samples
self.logger = logger
self.workflow_cmds = []
self.sql_queries = []
self.decision_support_upload_cmds, self.dx_postprocessing_cmds = False, False
self.rf_cmds_obj = BuildRunfolderDxCommands(self.rf_obj, self.logger)
for sample_name in self.rf_samples_obj.samples_dict.keys():
sample_cmds_obj = BuildSampleDxCommands(
self.rf_obj.runfolder_name,
self.rf_samples_obj.samples_dict[sample_name],
self.logger,
)
self.workflow_cmds.append(sample_cmds_obj.create_snp_cmd())
self.workflow_cmds.append(SWConfig.UPLOAD_ARGS["depends_list"])
self.sql_queries.append(
sample_cmds_obj.return_rd_query()
) # Get SQL queries
# Return downstream app commands
self.workflow_cmds.extend(
self.rf_cmds_obj.return_multiqc_cmds(self.rf_samples_obj.pipeline)
)
self.workflow_cmds.append(self.rf_cmds_obj.create_duty_csv_cmd())
class OncoDeepPipeline:
"""
Collate DNAnexus commands for OncoDEEP runs. This runtype has no post processing commands
or decision support upload script, as the decision support commands are run automatically
therefore reside in the dx run script
"""
def __init__(self, rf_obj: object, rf_samples: object, logger: logging.Logger):
self.rf_obj = rf_obj
self.rf_samples_obj = rf_samples
self.logger = logger
self.workflow_cmds = []
self.sql_queries = []
self.decision_support_upload_cmds, self.dx_postprocessing_cmds = False, False
self.rf_cmds_obj = BuildRunfolderDxCommands(self.rf_obj, self.logger)
for sample_name in self.rf_samples_obj.samples_dict.keys():
sample_cmds_obj = BuildSampleDxCommands(
self.rf_obj.runfolder_name,
self.rf_samples_obj.samples_dict[sample_name],
self.logger,
)
self.workflow_cmds.append(sample_cmds_obj.create_fastqc_cmd())
self.workflow_cmds.append(SWConfig.UPLOAD_ARGS["depends_list"])
self.sql_queries.append(sample_cmds_obj.return_oncology_query())
for read in ["R1", "R2"]: # Generate sample oncodeep upload commands
self.workflow_cmds.append(
sample_cmds_obj.build_oncodeep_upload_cmd(
f"{sample_name}-{read}",
self.rf_samples_obj.nexus_runfolder_suffix,
self.rf_samples_obj.samples_dict[sample_name]["fastqs"][read][
"nexus_path"
],
)
)
# Generate command for MasterFile upload
self.workflow_cmds.append(
sample_cmds_obj.build_oncodeep_upload_cmd(
self.rf_obj.masterfile_name,
self.rf_samples_obj.nexus_runfolder_suffix,
f"{self.rf_obj.DNANEXUS_PROJ_ID}:{self.rf_obj.masterfile_name}",
)
)
# Return downstream app commands
self.workflow_cmds.extend(
self.rf_cmds_obj.return_multiqc_cmds(self.rf_samples_obj.pipeline)
)
self.workflow_cmds.append(self.rf_cmds_obj.create_duty_csv_cmd())
class TsoPipeline:
"""
Collate commands for TSO workflow. This runtype has postprocessing commands and
decision support upload commands, and SQL queries
"""
def __init__(self, rf_obj: object, rf_samples: object, logger: logging.Logger):
self.rf_obj = rf_obj
self.rf_samples_obj = rf_samples
self.logger = logger
self.workflow_cmds = []
self.dx_postprocessing_cmds = []
self.decision_support_upload_cmds = []
self.sql_queries = []
self.rf_cmds_obj = BuildRunfolderDxCommands(self.rf_obj, self.logger)
# Create tso app commands