-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
executable file
·90 lines (77 loc) · 2.82 KB
/
config.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Creates an updated config file for the pipeline
"""
import json
import argparse
def make_config(data):
"""
Prints JSON config to stdout
"""
print(json.dumps(data, indent=4))
def update_config(updateFile, data):
"""
Update the values of a JSON config file for non-null values in the provided data
"""
f = open(updateFile)
old_data = json.load(f)
f.close()
for key, new_value in data.items():
if key in old_data:
# only update non-None values
if new_value is not None:
# if the old data value was a list, append new items
if isinstance(old_data[key], (list,)):
# start a new list from the old items
new_list = [ item for item in old_data[key] ]
# check if new item is single value or list of values
if isinstance(new_value, (list,)):
# add each new value
for item in new_value:
new_list.append(item)
else:
# add the new value
new_list.append(new_value)
old_data[key] = list(set(new_list))
# otherwise, overwrite old value
else:
old_data[key] = new_value
f = open(updateFile, "w")
json.dump(old_data, f, indent = 4)
f.close()
def main(**kwargs):
"""
Main control function for the script
"""
runID = kwargs.pop('runID', None)
fastqDirs = kwargs.pop('fastqDirs', [])
samplesheet = kwargs.pop('samplesheet', None)
updateFile = kwargs.pop('updateFile', False)
demux_samplesheet = kwargs.pop('demux_samplesheet', None)
if fastqDirs is None:
fastqDirs = []
data = {
'runID': runID,
'fastqDirs': list(set(fastqDirs)),
'samplesheet': samplesheet,
'demux_samplesheet': demux_samplesheet
}
if updateFile is False:
make_config(data)
else:
update_config(updateFile, data)
def parse():
"""
Parses script args
"""
parser = argparse.ArgumentParser(description='Creates an updated config file for the pipeline')
parser.add_argument("--runID", default = None, dest = 'runID', help="Run ID")
parser.add_argument("--fastqDirs", nargs = '+', dest = 'fastqDirs', help="Run directory")
parser.add_argument("--samplesheet", default = None, dest = 'samplesheet', help="Samplesheet file")
parser.add_argument("--demux-samplesheet", default = None, dest = 'demux_samplesheet', help="Demultiplexing Samplesheet file")
parser.add_argument("-u", "--update", default = False, dest = 'updateFile', help="JSON file to update")
args = parser.parse_args()
main(**vars(args))
if __name__ == '__main__':
parse()