-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoolbox.py
executable file
·1169 lines (1083 loc) · 50.3 KB
/
toolbox.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
"""
This script contains functions and classes shared across scripts / modules. Contains the following classes:
- RunfolderObject:
An object with runfolder-specific properties
- RunfolderSamples
An object with properties derived from the samples names in the samplesheet
- SampleObject
Collect sample-specific attributes for a sample
"""
import sys
import os
import re
import subprocess
import logging
import time
import seglh_naming
from pathlib import Path
from typing import Tuple
from distutils.spawn import find_executable
from typing import Union, Optional
from config.ad_config import ToolboxConfig
from ad_logger.ad_logger import RunfolderLoggers
def get_credential(file: str) -> None:
"""
File from which to read credential
:param file (str): Filepath
:return None:
"""
with open(file, "r") as to_read:
credential = to_read.readline().rstrip()
return credential
def write_lines(file: str, mode: str, lines: str) -> None:
"""
Write line to newline of file
:param file (str): Filepath
:param mode (str): Mode to open the file in
:param lines (str | list): Line (/s)
:return None:
"""
if isinstance(lines, str):
lines = [lines]
with open(file, mode) as open_file:
for line in lines:
open_file.write(f"{line}\n")
def read_lines(file: str) -> None:
"""
Read lines from file
:param file (str): Filepath
:return lines (list): List of lines
"""
with open(file, "r") as f:
return f.readlines()
def return_scriptlog_config() -> dict:
"""
Return script-level logfile configuration
:return (dict): Dictionary containing logger names and logfile paths
"""
return {
"demux": os.path.join( # Record demultiplex script logs
ToolboxConfig.AD_LOGDIR,
"demultiplexing_script_logfiles",
f"{ToolboxConfig.TIMESTAMP}_demultiplex_script.log",
),
"sw": os.path.join( # Record sw script logs
ToolboxConfig.AD_LOGDIR,
"sw_script_logfiles",
f"{ToolboxConfig.TIMESTAMP}_setoff_workflow.log",
),
"wscleaner": os.path.join( # Record wscleaner script logs
ToolboxConfig.AD_LOGDIR,
"wscleaner_logs",
f"{ToolboxConfig.TIMESTAMP}_wscleaner.log",
),
}
def script_start_logmsg(logger: logging.Logger, file: str) -> None:
"""
Adds the log message that denotes the start of the script
running to the logfile
:param logger (logging.Logger): Logger
:param file (str): Path to logfile
:return None:
"""
logger.info(
logger.log_msgs["script_start"],
git_tag(),
os.path.basename(os.path.dirname(file)),
)
def script_end_logmsg(logger: logging.Logger, file: str) -> None:
"""
Adds the log message that denotes the end of the script
runnign to the logfile
:param logger (logging.Logger): Logger
:param file (str): Path to logfile
:return None:
"""
logger.info(
logger.log_msgs["script_end"],
git_tag(),
os.path.basename(os.path.dirname(file)),
)
def git_tag() -> str:
"""
Obtain the git tag of the current commit
:return (str): Git tag
"""
filepath = os.path.dirname(os.path.realpath(__file__))
cmd = f"git -C {filepath} describe --tags"
proc = subprocess.Popen(
[cmd],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=True,
)
out, _ = proc.communicate()
# Return standard out, removing any new line characters
return out.rstrip().decode("utf-8")
def execute_subprocess_command(
command: str, logger: logging.Logger, exit_on_fail=False
) -> Union[str, str, int]:
"""
Execute a subprocess
:param command(str): Input command
:param logger(logging.Logger): Logger
:return (stdout(str),
stderr(str)) (tuple): Outputs from the command
"""
logger.info(logger.log_msgs["executing_command"], command)
proc = subprocess.Popen(
[command],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=True,
executable="/bin/bash",
)
out, err, returncode = check_returncode(proc, logger)
if exit_on_fail == "exit_on_fail":
exit_on_returncode(returncode)
return out, err, returncode
def exit_on_returncode(returncode: int) -> None:
"""
Exit the script if the returncode is not 0 (success)
"""
if returncode != 0:
sys.exit(1)
def check_returncode(
proc: subprocess.Popen, logger: logging.Logger
) -> Union[str, str, int]:
"""
Check for success returncode and write to log accordingly
:param proc (class): subprocess.Popen class
:param logger (logging.Logger): Logger
:return (stdout(str),
stderr(str),
returncode(int))(tuple): Stdout, stderr, returncode
"""
out, err = proc.communicate()
out = out.decode("utf-8").strip()
err = err.decode("utf-8").strip()
returncode = proc.returncode
if returncode == 0:
logger.info(logger.log_msgs["cmd_success"], returncode)
return out, err, returncode
else:
logger.error(logger.log_msgs["cmd_fail"], returncode, out, err)
return out, err, returncode
def get_runfolder_path(runfolder_name: str) -> str:
"""
Return the path of the runfolder based on the runfolder_name input
:param runfolder_name (str): Runfolder name string
:return (str): Runfolder path
"""
return os.path.join(ToolboxConfig.RUNFOLDERS, runfolder_name)
def test_upload_software(logger: logging.Logger) -> True:
"""
Test the required software is installed and performing. If not, exit the script
:return True | None: Return True if all software tests pass, else None
"""
if test_programs("dx_toolkit", logger) and test_programs("upload_agent", logger):
return True
else:
logger.error(logger.log_msgs["software_fail"])
sys.exit(1)
def test_processing_software(logger: logging.Logger) -> Optional[bool]:
"""
Test the software is installed and performing, by calling the test_upload_agent
and test_dx_toolkit functions
:return True|None: Return true if the tests all pass
"""
if test_programs("bcl2fastq2", logger) and test_programs(
"gatk_collect_lane_metrics", logger
):
return True
def test_programs(software_name: str, logger: logging.Logger) -> True:
"""
Check software exists in path, and that the test command executes successfully
(return code 0). If it does not, exit script
:param software_name (str): Name of the sofware being tested
:param logger (logging.Logger): Logger
:return True: Return True if test passes, else exit script
"""
software_dict = ToolboxConfig.TEST_PROGRAMS_DICT[software_name]
logger.info(logger.log_msgs["testing_software"], software_name)
if find_executable(software_dict["executable"]):
logger.info(logger.log_msgs["found_program"], software_dict["executable"])
out, err, returncode = execute_subprocess_command(
software_dict["test_cmd"], logger, "exit_on_fail"
)
if returncode == 0:
logger.info(logger.log_msgs["test_pass"], software_name)
return True
else:
logger.error(logger.log_msgs["test_fail"], software_name, out, err)
sys.exit(1)
else:
logger.error(logger.log_msgs["program_missing"], software_dict["executable"])
sys.exit(1)
def get_num_processed_runfolders(
logger: logging.Logger, processed_runfolders: list
) -> int:
"""
Set self.num_processed_runfolders
:param logger (logging.Logger): Logger
:param processed_runfolders (list): List of names of processed runfolders
:return num_processed_runfolders (int): Number of processed runfolders
"""
num_processed_runfolders = len(processed_runfolders)
logger.info(
logger.log_msgs["runfolders_processed"],
num_processed_runfolders,
", ".join(processed_runfolders),
)
return num_processed_runfolders
def get_samplename_dict(
logger: logging.Logger, samplesheet_path: str
) -> Optional[dict]:
"""
Read SampleSheet to create a dict of samples and their pan numbers for the
run. Reads file into list and loops through in reverse allowing us to access
sample names and stop at column headers, skipping the file header
:param logger (logging.Logger): Logger
:param samplesheet_path (str): Path to samplesheet
:return samplename_dict (dict): Dict of sample names identified from the
SampleSheet, and their pan numbers
"""
samplename_dict = {}
if os.path.exists(samplesheet_path):
reversed_samplesheet = reversed(read_lines(samplesheet_path))
for line in reversed_samplesheet:
if line.startswith("Sample_ID") or "[Data]" in line:
break
# Skip empty lines (check first element of the line, after splitting on comma)
elif len(line.split(",")[0]) < 2:
pass
else: # If it's a line detailing a sample, get sample name and pan num
panel_number = ""
sample_name = line.split(",")[0]
for pannum in ToolboxConfig.PANELS:
if pannum in line:
panel_number = pannum
samplename_dict[sample_name] = panel_number
if samplename_dict: # If samples identified
return samplename_dict
else:
logger.error(logger.log_msgs["ss_missing"])
def validate_fastqs(fastq_dir_path: str, logger: logging.Logger) -> Optional[bool]:
"""
Validate the created fastqs in the BaseCalls directory and log success
or failure error message accordingly. If any failure, remove bcl2fastq log
file to trigger re-demultiplex on next script run
:param fastq_dir_path (str): Runfolder fastq directory path (within runfolder)
:param logger (logging.Logger): Logger
:return Optional[bool]: Return True if fastqs are all determined to be valid
"""
fastqs = sorted([x for x in os.listdir(fastq_dir_path) if x.endswith("fastq.gz")])
returncodes = []
for fastq in fastqs:
out, err, returncode = execute_subprocess_command(
f"gzip --test {os.path.join(fastq_dir_path, fastq)}",
logger,
)
returncodes.append(returncode)
if returncode == 0:
logger.info(
logger.log_msgs["fastq_valid"],
fastq,
)
else:
logger.error(
logger.log_msgs["fastq_invalid"],
fastq,
out,
err,
)
if all(code == 0 for code in returncodes):
logger.info(logger.log_msgs["demux_success"])
return True
class RunfolderObject(ToolboxConfig):
"""
An object with runfolder-specific properties
Attributes
dnanexus_auth (str): DNAnexus auth token
timestamp (str): Timestamp in the format str(f"{datetime.datetime.now():
%Y%m%d_%H%M%S}")
runfolder_name (str): Runfolder name string
runfolderpath (str): Path of runfolder on workstation
samplesheet_name (str): Name of runfolder SampleSheet
rtacompletefile_path (str): Sequencing finished filepath (within runfolder)
samplesheet_path (str): Path to SampleSheet in SampleSheets dir
runfolder_samplesheet_path (str): Runfolder SampleSheets path (within runfolder)
checksumfile_path (str): md5 checksum (integrity check) file path (within runfolder)
sscheck_flagfile_path (str): Samplesheet check flag file path (within runfolder)
bcl2fastqlog_file (str): bcl2fastq2 logfile path (within runfolder)
fastq_dir_path (str): Runfolder fastq directory path (within runfolder)
upload_flagfile (str): Flag file denoting upload has begun (within runfolder)
bcl2fastqstats_file (str): Bcl2fastq stats file (within runfolder)
cluster_density_files (list): List containing runfolder lane metrics
and phasing metrics file paths
demultiplex_runfolder_logfile (str): Runfolder demultiplex logfile (within logfiles dir)
sw_runfolder_logfile (str): Records output of setoff workflow script
upload_runfolder_logfile (str): Records the logs from the upload_runfolder script
runfolder_dx_run_script (str): Workflow dx run commands for runfolder (within logfiles dir)
post_run_dx_run_script (str): Separate DX run script for downstream processing apps (TSO only)
decision_support_upload_script (str): Decision support upload commands for runfolder (within logfiles dir)
proj_creation_script (str): DNAnexus project creation bash script (within logfiles dir)
samplesheet_validator_logfile (str): SampleSheet validator script logfile (within logfiles dir)
logfiles_config (dict): Contains all runfolder log files
logfiles_to_upload (list): All logfiles that require upload to DNAnexus
Methods
get_runfolder_loggers(script)
Return dictionary of logger.Logging objects for the runfolder
"""
def __init__(self, runfolder_name: str, timestamp: str):
"""
Constructor for the RunfolderObject class
:param runfolder_name (str): Runfolder name
:param timestamp (str): Timestamp in the format str(f"{datetime.datetime.now():%Y%m%d_%H%M%S}")
"""
self.dnanexus_auth = get_credential(
ToolboxConfig.CREDENTIALS["dnanexus_authtoken"]
)
self.timestamp = timestamp
self.runfolder_name = runfolder_name
self.runfolderpath = get_runfolder_path(self.runfolder_name)
self.samplesheet_name = f"{self.runfolder_name}_SampleSheet.csv"
self.rtacompletefile_path = os.path.join(
self.runfolderpath, ToolboxConfig.FLAG_FILES["seq_complete"]
)
self.samplesheet_path = os.path.join(
ToolboxConfig.RUNFOLDERS, "samplesheets", self.samplesheet_name
)
self.runfolder_samplesheet_path = os.path.join(
self.runfolderpath, self.samplesheet_name
)
self.masterfile_name = f"{self.runfolder_name}_MasterDataFile.xlsx"
self.masterfile_path = os.path.join(
ToolboxConfig.RUNFOLDERS, "samplesheets", self.masterfile_name
)
self.runfolder_masterfile_path = os.path.join(
self.runfolderpath, self.masterfile_name
)
self.checksumfile_path = os.path.join(
self.runfolderpath, ToolboxConfig.FLAG_FILES["md5checksum"]
)
self.sscheck_flagfile_path = os.path.join(
self.runfolderpath, ToolboxConfig.FLAG_FILES["sscheck_flag"]
)
self.bcl2fastqlog_file = os.path.join(
self.runfolderpath, ToolboxConfig.FLAG_FILES["bcl2fastqlog"]
)
self.fastq_dir_path = os.path.join(
self.runfolderpath, ToolboxConfig.FASTQ_DIRS["fastqs"]
)
self.upload_flagfile = os.path.join(
self.runfolderpath, ToolboxConfig.FLAG_FILES["upload_started"]
)
self.bcl2fastqstats_file = os.path.join(
self.runfolderpath,
"Data/Intensities/BaseCalls/Stats/Stats.json",
)
self.cluster_density_files = [
os.path.join(
self.runfolderpath,
f"{self.runfolder_name}{ToolboxConfig.STRINGS['lane_metrics_suffix']}",
),
os.path.join(
self.runfolderpath,
(
f"{self.runfolder_name}"
f"{ToolboxConfig.STRINGS['phasing_metrics_suffix']}"
),
),
]
self.demultiplex_runfolder_logfile = (
os.path.join( # Record demultiplex script logs
ToolboxConfig.AD_LOGDIR,
"demultiplexing_script_logfiles",
f"{self.runfolder_name}_demultiplex_runfolder.log",
)
)
self.sw_runfolder_logfile = os.path.join(
ToolboxConfig.AD_LOGDIR,
"sw_script_logfiles",
f"{self.runfolder_name}_setoff_workflow.log",
)
self.upload_runfolder_logfile = os.path.join(
ToolboxConfig.AD_LOGDIR,
"upload_runfolder_script_logfiles",
f"{self.runfolder_name}_upload_runfolder.log",
)
self.runfolder_dx_run_script = os.path.join(
ToolboxConfig.AD_LOGDIR,
"dx_run_commands",
f"{self.runfolder_name}_dx_run_commands.sh",
)
self.post_run_dx_run_script = os.path.join(
ToolboxConfig.AD_LOGDIR,
"dx_run_commands",
f"{self.runfolder_name}_post_run_commands.sh",
)
self.decision_support_upload_script = os.path.join(
ToolboxConfig.AD_LOGDIR,
"dx_run_commands",
f"{self.runfolder_name}_decision_support.sh",
)
self.proj_creation_script = os.path.join(
ToolboxConfig.AD_LOGDIR,
"dx_run_commands",
f"{self.runfolder_name}_create_nexus_project.sh",
)
self.samplesheet_validator_logfile = os.path.join(
ToolboxConfig.AD_LOGDIR,
"samplesheet_validator_script_logfiles",
f"{self.runfolder_name}_samplesheet_validator.log",
)
self.logfiles_config = {
"sw": self.sw_runfolder_logfile,
"demux": self.demultiplex_runfolder_logfile,
"backup": self.upload_runfolder_logfile,
"bcl2fastq2": self.bcl2fastqlog_file,
"ss_validator": self.samplesheet_validator_logfile,
}
# Log files that sit outside the runfolder that require uploading
self.logfiles_to_upload = [
self.sw_runfolder_logfile,
self.demultiplex_runfolder_logfile,
self.proj_creation_script,
self.runfolder_dx_run_script,
self.samplesheet_validator_logfile,
self.upload_runfolder_logfile,
]
def age(self) -> int:
"""
Return runfolder age in days
:return age (int): Runfolder age in days
"""
return (time.time() - Path(self.runfolderpath).stat().st_mtime) // (24 * 3600)
def get_runfolder_loggers(self, script: str) -> dict:
"""
Return dictionary of logger.Logging objects for the runfolder
:param script (str): Script name the function has been called from
:return (dict): Dictionary of logger.Logging objects
"""
loggers_obj = RunfolderLoggers(
script, self.runfolder_name, self.logfiles_config
)
return loggers_obj.loggers
class RunfolderSamples(ToolboxConfig):
"""
An object with properties derived from the sample names in the samplesheet
Attributes
samplesheet_path (str): Path to SampleSheet in SampleSheets dir
runfolder_name (str): Runfolder name string
fastq_dir_path (str): Runfolder fastq directory path (within runfolder)
logger (logging.Logger): Logger
samplename_dict (dict): Dict of sample names identified from the
SampleSheet, and their pan numbers
pipeline (str): Pipeline name
runtype_str (str): Runtype name string
nexus_runfolder_suffix (str): String of '_' delimited unique library numbers,
and WES batch numbers if run is a WES run
nexus_paths (dict): Dictionary of paths within the DNAnexus project
that are required for building dx commands
unique_pannos (set): Set of unique panel numbers within the run
samples_dict (dict): Dictionary of SampleObject per sample,
containing sample-specific attributes
fastqs_list (list): List of all sample fastqs in the run
fastqs_str (str): Space separated string of sample fastqs with
each fastq encased in quotation marks
undetermined_fastqs_list (list): List of all undetermined fastqs in the run
undetermined_fastqs_str (str) Space separated string of all undetermined fastqs
in the run, with each fastq encased in quotation marks
Methods
get_pipeline()
Use samplename_dict and the ToolboxConfig.PANEL_DICT to get the pipeline name for
samples in the run
get_runtype()
Use self.samplename_dict and the ToolboxConfig.PANEL_DICT to get a list of runtype
names for samples in the run. Returns the most frequent runtype name in the set
get_nexus_runfolder_suffix()
Get the runfolder suffix for the DNAnexus project name
capture_library_numbers()
Parse the names in self.samplename_dict to identify the library prep numbers
capture_wes_batch_numbers()
Parse the names in self.samplename_dict to identify the WES batch numbers
get_nexus_paths()
Build nexus paths, using NGS run numbers (and batch numbers in the case of WES)
get_unique_pannos()
Return set of unique pan numbers for samples within the run
get_samples_dict()
Create a SampleObject per sample, containing sample-specific properties, and
add each SampleObject to a larger samples_dict
check_for_missing_fastqs()
Validate the fastqs in the BaseCalls directory by checking that all sample fastqs
match a sample name from the self.samplename_dict
fastq_not_undetermined(fastq_dir_file)
Determine whether the fastq is an undetermined fastq
get_fastqs_list()
Return a list of sample fastqs for the run
get_fastqs_str(fastqs_list)
Return a space separated string of fastqs with each fastq encased in quotation marks
get_undetermined_fastqs_list()
Return a list of undetermined fastqs for the run
"""
def __init__(self, rf_obj: object, logger: logging.Logger):
"""
Constructor for the RunfolderSamples class
:param rf_obj (object): RunfolderObject object (contains runfolder-specific attributes)
:logger (logging.Logger): Logger
"""
self.samplesheet_path = rf_obj.samplesheet_path
self.runfolder_name = rf_obj.runfolder_name
self.fastq_dir_path = rf_obj.fastq_dir_path
self.logger = logger
self.samplename_dict = get_samplename_dict(self.logger, self.samplesheet_path)
self.pipeline = self.get_pipeline()
self.runtype_str = self.get_runtype()
self.nexus_runfolder_suffix = self.get_nexus_runfolder_suffix()
self.nexus_paths = self.get_nexus_paths()
self.unique_pannos = self.get_unique_pannos()
self.samples_dict = self.get_samples_dict()
self.check_for_missing_fastqs()
self.fastqs_list = self.get_fastqs_list()
self.fastqs_str = self.get_fastqs_str(self.fastqs_list)
self.undetermined_fastqs_list = self.get_undetermined_fastqs_list()
self.undetermined_fastqs_str = self.get_fastqs_str(
self.undetermined_fastqs_list
)
def get_pipeline(self) -> Optional[str]:
"""
Use samplename_dict and the ToolboxConfig.PANEL_DICT to get a list of pipeline
names for samples in the run. Generates error mesage if there is more than one
pipeline name in the list. Returns the most frequent pipeline name in the set
:return pipeline_name (Optional[str]): Pipeline name if only one pipeline name in list
"""
if self.samplename_dict:
try:
pipelines_list = []
for sample, panno in self.samplename_dict.items():
pipelines_list.append(ToolboxConfig.PANEL_DICT[panno]["pipeline"])
pipelines_list = sorted(list(set(pipelines_list)))
if len(pipelines_list) > 1:
self.logger.error(
self.logger.log_msgs["multiple_pipeline_names"],
pipelines_list,
ToolboxConfig.PIPELINES,
)
else:
pipeline_name = pipelines_list[0] # Get pipeline from pipelines_list
self.logger.debug(
self.logger.log_msgs["pipeline_name"],
pipeline_name,
)
return pipeline_name
except Exception:
return None
def get_runtype(self) -> str:
"""
Use samplename_dict and the ToolboxConfig.PANEL_DICT to get the runtype for samples
in the run for Custom Panels and WES runs where sample types vary (VCP1/2/3/WES/WES EB)
:return runtype_str (str): Runtype name string
"""
if self.samplename_dict:
try:
runtype_list = []
for sample, panno in self.samplename_dict.items():
runtype_list.append(ToolboxConfig.PANEL_DICT[panno]["runtype"])
if ToolboxConfig.PANEL_DICT[panno]["sample_prefix"]:
if all(ToolboxConfig.PANEL_DICT[panno]["sample_prefix"] not in runtype for runtype in runtype_list):
runtype_list.append(ToolboxConfig.PANEL_DICT[panno]["sample_prefix"])
runtype_str = "_".join(sorted(list(set(runtype_list))))
self.logger.debug(
self.logger.log_msgs["runtype_str"],
runtype_str,
)
return runtype_str
except Exception:
return None
def get_nexus_runfolder_suffix(self) -> str:
"""
Get the runfolder suffix for the DNAnexus project name. This consists of the
library number (see capture_library_numbers docstring for explanation), followed by
the WES batch if the run is a WES run, followed by the runtype (e.g. VCP1 / VCP2)
:return suffix (str): String of '_' delimited unique library numbers, and WES
batch numbers if run is a WES run, followed by the runtype
"""
if self.samplename_dict:
library_numbers = self.capture_library_numbers()
if self.pipeline == "wes":
library_numbers.extend(self.capture_wes_batch_numbers())
if self.pipeline in ["pipe", "wes", "dev"]:
library_numbers.append(self.runtype_str)
suffix = f"{'_'.join(library_numbers)}" # Provides more detail on contents of runs in runfolder name
return suffix
def capture_library_numbers(self) -> list:
"""
Parse the names in self.samplename_dict to identify the library prep numbers.
These are the first elements in the sample names (before the first underscore).
These numbers are used as the suffix for the DNAnexus project name (along with
the WES batch number in the case of WES runs). If no library prep numbers are
found, exit the script
:return list:List of unique library numbers
"""
library_numbers = []
for samplename in self.samplename_dict.keys():
if "_" in str(samplename): # Check there are underscores present
# Split on underscores to capture library number e.g. ONC100 or NGS100
library_numbers.append(samplename.split("_")[0])
if library_numbers: # Should always be library numbers found
self.logger.debug(
self.logger.log_msgs["library_nos_identified"],
", ".join(sorted(list(set(library_numbers)))),
)
return sorted(list(set(library_numbers)))
else: # Prompt a slack alert
self.logger.error(self.logger.log_msgs["library_no_err"])
sys.exit(1)
def capture_wes_batch_numbers(self) -> list:
"""
Parse the names in self.samplename_dict to identify the WES batch numbers. This
along with the library prep number is used as the DNAnexus project name suffix.
If unsuccessful, exit the script
:return wes_batch_numbers_list (list): List of unique WES batch numbers
"""
wes_batch_numbers_list = []
for samplename in self.samplename_dict.keys():
if "WES" in str(samplename):
# Capture WES batch (WES followed by digits)
# Optional underscore ensures this will capture WES5 or WES_5
wesbatch = re.search(r"WES_?\d+", samplename).group()
wes_batch_numbers_list.append(wesbatch.replace("_", ""))
if wes_batch_numbers_list:
self.logger.debug(
self.logger.log_msgs["wes_batch_nos_identified"],
", ".join(wes_batch_numbers_list),
)
return sorted(list(set(wes_batch_numbers_list)))
else: # Prompt a slack alert
self.logger.error(self.logger.log_msgs["wes_batch_nos_missing"])
sys.exit(1)
def get_nexus_paths(self) -> dict:
"""
Build nexus paths, using NGS run numbers (and batch numbers in the case of WES).
Builds the DNAnexus project name using the config-defined project prefix (denoting
status of the DNAnexus project), followed by the runfolder name and the and
self.nexus_runfolder_suffix as the suffix (library prep / WES batch numbers). Uses
the DNAnexus project name to build additional paths required for later dx run commands
:return nexus_paths (dict): Dictionary of paths within the DNAnexus project
that are required for building dx commands
"""
nexus_paths = {}
if self.pipeline == "tso500":
fastq_type = "tso_fastqs"
else:
fastq_type = "fastqs"
nexus_paths["proj_name"] = (
f"{ToolboxConfig.DNANEXUS_PROJECT_PREFIX}{self.runfolder_name}_{self.nexus_runfolder_suffix}"
)
nexus_paths["fastqs_dir"] = os.path.join(
f"/{self.runfolder_name}", ToolboxConfig.FASTQ_DIRS[fastq_type]
)
nexus_paths["logfiles_dir"] = os.path.join(
f"/{self.runfolder_name}", "automated_scripts_logfiles"
)
return nexus_paths
def get_unique_pannos(self) -> Optional[list]:
"""
Return set of unique pan numbers for samples within the run
:return Optional[list]: List of unique pan numbers if samples identified, else None
"""
if self.samplename_dict:
return set(self.samplename_dict.values())
def get_samples_dict(self) -> dict:
"""
Create a SampleObject for each sample which returns a sample dictionary
containing the sample_name, pannum, panel_settings and fastqs paths for that
sample. Add each SampleObject to a larger samples_dict
:return samples_dict (dict): Dictionary of SampleObject per sample,
containing sample-specific attributes
"""
if self.samplename_dict:
samples_dict = {}
for sample_name in self.samplename_dict.keys():
self.sample_obj = SampleObject(
sample_name,
self.pipeline,
self.logger,
self.fastq_dir_path,
self.nexus_paths,
self.nexus_runfolder_suffix,
)
if self.sample_obj.fastqs_dict:
samples_dict[sample_name] = self.sample_obj.return_sample_dict()
else:
self.logger.warning(
self.logger.log_msgs["sample_excluded"],
sample_name,
)
return samples_dict
def check_for_missing_fastqs(self) -> None:
"""
Validate the fastqs in the BaseCalls directory by checking that all sample fastqs
match a sample name from the self.samplename_dict. If they do not, log an error
and add to a missing_samples list. Add all samples in the missing samples list to
the samples_dict so that they are processed
:return None:
"""
if self.samplename_dict:
missing_samples = []
for fastq_dir_file in os.listdir(self.fastq_dir_path):
if os.path.isfile(fastq_dir_file):
if fastq_dir_file.endswith("fastq.gz"):
self.logger.info(
self.logger.log_msgs["checking_fastq"],
fastq_dir_file,
)
if self.fastq_not_undetermined(
fastq_dir_file
): # Exclude undetermined
try:
seglh_naming.Sample.from_string(fastq_dir_file)
sample_name = [
sample_name
for sample_name in self.samplename_dict.keys()
if sample_name in fastq_dir_file
]
if sample_name:
self.logger.info(
self.logger.log_msgs["sample_match"],
fastq_dir_file,
sample_name,
)
else:
self.logger.error(
self.logger.log_msgs["sample_mismatch"],
fastq_dir_file,
)
sample_name = re.sub(
"R[0-9]_001.fastq.gz", "", fastq_dir_file
)
missing_samples.append(fastq_dir_file)
except ValueError as exception:
self.logger.error(
self.logger.log_msgs["fastq_wrong_naming"],
fastq_dir_file,
exception,
)
else:
self.logger.info(
self.logger.log_msgs["not_fastq"],
fastq_dir_file,
)
for sample_name in missing_samples: # Add the sample to the sample_obj
# Strip end off sample name
sample_name = re.sub(r"_S[0-9]+_R[1-2]{1}_001.fastq.gz", "", sample_name)
self.logger.info(self.logger.log_msgs["add_missing_sample"], sample_name)
self.sample_obj = SampleObject(
sample_name,
self.pipeline,
self.logger,
self.fastq_dir_path,
self.nexus_paths,
self.nexus_runfolder_suffix,
)
self.samples_dict[sample_name] = self.sample_obj.return_sample_dict()
def fastq_not_undetermined(self, fastq_dir_file: str) -> Optional[bool]:
"""
Determine whether the fastq is an undetermined fastq
:param fastq_dir_file (str):
:return (Optional[bool]): Return True if undetermined, else return None
"""
if not fastq_dir_file.startswith("Undetermined"):
return True
else:
self.logger.info(
self.logger.log_msgs["undetermined_identified"],
fastq_dir_file,
)
def get_fastqs_list(self) -> list:
"""
Return a list of sample fastqs for the run
:return fastqs_list (list): List of all sample fastqs in the run
"""
if self.samples_dict:
fastqs_list = []
for sample_name in self.samples_dict.keys():
if self.samples_dict[sample_name]["fastqs"]:
fastqs_list.extend(
[
self.samples_dict[sample_name]["fastqs"][read]["path"]
for read, path in self.samples_dict[sample_name][
"fastqs"
].items()
]
)
return fastqs_list
def get_fastqs_str(self, fastqs_list: list) -> str:
"""
Return a space-separated string of fastqs with each fastq encased in quotation marks.
This is used for runs / samples that are demultiplexed locally
:param fastqs_list (list): List of sample fastqs
:return (str): Space separated string of fastqs with
each fastq encased in quotation marks
"""
if fastqs_list:
quotation_marked_list = []
for fastq in fastqs_list:
quotation_marked = f"'{fastq}'"
quotation_marked_list.append(quotation_marked)
return " ".join(quotation_marked_list)
def get_undetermined_fastqs_list(self) -> list:
"""
Return a list and string of undetermined fastqs for the run
:return undetermined_fastqs_list (list): List of all undetermined fastqs in the run
"""
undetermined_fastqs_list = []
r1 = os.path.join(self.fastq_dir_path, "Undetermined_S0_R1_001.fastq.gz")
r2 = os.path.join(self.fastq_dir_path, "Undetermined_S0_R2_001.fastq.gz")
for fastq in [r1, r2]:
if os.path.exists(fastq):
undetermined_fastqs_list.append(fastq)
return undetermined_fastqs_list
# TODO eventually adapt this class to use the SamplesheetValidator package
class SampleObject(ToolboxConfig):
"""
Collect sample-specific attributes for a sample. Including sample-specific command strings
for calling the pipeline and decision support tools where relevant
Attributes
sample_name (str): Sample name
pipeline (str): Pipeline name
logger (logging.Logger): Logger
fastq_dir_path (str): Runfolder fastq directory path (within runfolder)
nexus_paths (dict): Dictionary of paths within the DNAnexus project that
are required for building dx commands
nexus_runfolder_suffix (str): String of '_' delimited unique library numbers,
and WES batch numbers if run is a WES run
neg_control (bool): True if sample is a negative control, else False
pos_control (bool): True if sample is a reference sample, else False
pannum (str): Panel number that matches a config-defined panel
number, or None if pannum not valid
panel_settings (dict): Config defined panel settings specific to the sample panel number
primary_identifier (str): Primary sample identifier
secondary_identifier (str): Secondary sample identifier
fastqs_dict (dict): Dictionary containing R1 and R2 fastqs and their
local and cloud paths
Methods
check_control(identifiers, control_type)
Determine whether sample contains the control identifier strings
find_pannum()
Extract panel number from sample name using regular expression
return_panel_settings()
Return panel settings for the specified pan number, if exists
validate_pannum(pannum)
Check whether pan number is valid
get_identifiers()
For WES and PIPE samples, extract DNA number from sample name. For oncology
samples, collect 3rd and 4th identifiers, setting secondary_identifier to
null if the sample is a negative or positive control (these only have one identifier)
get_fastqs_dict()
Collate R1 and R2 fastqs and their local and cloud paths into a dictionary.
get_fastq_paths(read)
Get fastqs in fastq directory that correspond to each sample name in the
sample dictionary. Build the fastq name, local path, and DNAnexus path
for each fastq file
return_sample_dict()
Return sample dictionary with all collected information about the sample
"""
def __init__(
self,
sample_name: str,
pipeline: str,
logger: logging.Logger,
fastq_dir_path: str,
nexus_paths: dict,
nexus_runfolder_suffix: str,
):
"""
Constructor for the SampleObject class. Calls the class methods
:param sample_name (str): Sample name
:param pipeline (str): Pipeline name
:param logger (logging.Logger): Logger
:param fastq_dir_path (str): Runfolder fastq directory path (within runfolder)
:param nexus_paths (dict): Dictionary of paths within the DNAnexus project that are
required for building dx commands
:param nexus_runfolder_suffix (str): String of '_' delimited unique library numbers,
and WES batch numbers if run is a WES run
"""
self.sample_name = sample_name
self.pipeline = pipeline
self.logger = logger
self.fastq_dir_path = fastq_dir_path
self.nexus_paths = nexus_paths
self.nexus_runfolder_suffix = nexus_runfolder_suffix
self.neg_control = self.check_control(ToolboxConfig.NTCON_IDS, "Negative")
self.pos_control = self.check_control(ToolboxConfig.PSCON_IDS, "Positive")
self.pannum = self.find_pannum()
self.panel_settings = self.return_panel_settings()
self.primary_identifier, self.secondary_identifier = self.get_identifiers()
self.fastqs_dict = self.get_fastqs_dict()
def check_control(self, identifiers: list, control_type: str) -> Optional[bool]:
"""
Determine whether sample contains the control identifier strings