-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_processing2json.py
executable file
·295 lines (276 loc) · 12.9 KB
/
run_processing2json.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
#!/usr/bin/env python3
import argparse
import json
import re
import csv
import os
import logging
from datetime import datetime
logging.basicConfig(format='%(levelname)s: %(asctime)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def main():
""" Main """
parser = argparse.ArgumentParser(prog='run_processing2json.py', description="Creates json file for project tracking database for a given run processing.")
parser.add_argument('-i', '--input', required=True, help="Input align_bwa_mem.csv file from Run Processing.")
parser.add_argument('-o', '--output', required=False, help="Output json filename (Default: <input_filename>.json).")
parser.add_argument('-l', '--lane', required=False, help="Only considers lane(s) provided for json creation.", nargs='+')
group = parser.add_mutually_exclusive_group()
group.add_argument('-s', '--sample', required=False, help="Only considers sample(s) provided for json creation.", nargs='+')
group.add_argument('-x', '--xsample', required=False, help="Ignores sample(s) provided for json creation.", nargs='+')
args = parser.parse_args()
if not args.output:
output = f"{os.path.basename(args.input).split('.')[0]}.json"
else:
output = args.output
if args.lane:
lanes = list(args.lane)
else:
lanes = ["1", "2", "3", "4", "5", "6", "7", "8"]
samples = []
input_csv = args.input
run_list = []
with open(input_csv, 'rt') as run_file_in:
reader = csv.DictReader(run_file_in)
for row in reader:
run_list.append(row)
samples.append(row['Sample Name'])
if args.sample:
samples = list(args.sample)
elif args.xsample:
samples = list(set(samples).difference(list(args.xsample)))
jsonify_run_processing(input_csv, run_list, output, lanes, samples)
def jsonify_run_processing(input_csv, run_list, output, lanes, samples):
""" Writing RUn Processing json based on csv"""
readset_dict = {}
sample_dict = {}
json_output = {
"operation_platform": "abacus",
"project_ext_id": None,
"project_ext_src": None,
"project_name": "MOH-Q",
"run_ext_id": None,
"run_ext_src": None,
"run_name": f"{run_list[0]['Processing Folder Name']}",
"run_instrument": "novaseq",
"run_date": f"{datetime.strptime(run_list[0]['Processing Folder Name'][0:6], '%y%m%d')}",
"specimen": []
}
for run_row in run_list:
sample = run_row['Sample Name']
if sample.startswith("MoHQ") and run_row['Lane'] in lanes and sample in samples:
result = re.search(r"^((MoHQ-(JG|CM|GC|MU|MR|XX|HM|CQ)-\w+)-\w+)-\w+-\w+(D|R)(T|N)", sample)
specimen = result.group(1)
cohort = result.group(2)
institution = result.group(3)
# Check if the specimen is already in json_output["specimen"]
specimen_names = [spec["specimen_name"] for spec in json_output["specimen"]]
if specimen in specimen_names:
# Specimen is present, find its position
position = specimen_names.index(specimen)
specimen_json = json_output["specimen"][position]
else:
# Specimen is not present, add it to json_output["specimen"]
specimen_json = {
"specimen_ext_id": None,
"specimen_ext_src": None,
"specimen_name": specimen,
"specimen_cohort": cohort,
"specimen_institution": institution,
"sample": []
}
json_output["specimen"].append(specimen_json)
sample_tumour = sample.endswith("T")
# Check if the sample is already in specimen_json["sample"]
sample_names = [spec["sample_name"] for spec in specimen_json["sample"]]
if sample in sample_names:
# sample is present, find its position
position = sample_names.index(sample)
sample_json = specimen_json["sample"][position]
else:
# sample is not present, add it to specimen_json["sample"]
sample_json = {
"sample_ext_id": None,
"sample_ext_src": None,
"sample_name": sample,
"sample_tumour": sample_tumour,
"readset": []
}
specimen_json["sample"].append(sample_json)
copylist = os.path.join(os.path.dirname(input_csv), f"{os.path.basename(input_csv).split('.')[0]}.copylist.txt")
if not os.path.isfile(copylist):
raise Exception(f"File {copylist} not found; required to find raw data (bams/bais/fastqs) location")
fastq1 = fastq2 = bam = bai = ""
with open(copylist, 'r') as file:
for line in file:
if re.search(fr"{sample}/run{run_row['Run ID']}_{run_row['Lane']}.*\.ba(m|i)$", line):
fields = line.split(",")
file_path = fields[3].strip()
if file_path.endswith(".bam"):
bam = os.path.basename(file_path)
bam_location_uri = file_path
elif file_path.endswith(".bai"):
bai = os.path.basename(file_path)
bai_location_uri = file_path
if bam and bai:
file_json = [
{
"location_uri": f"abacus://{bam_location_uri}",
"file_name": f"{bam}",
"file_deliverable": True
},
{
"location_uri": f"abacus://{bai_location_uri}",
"file_name": f"{bai}",
"file_deliverable": True
}
]
break
elif re.search(fr"Unaligned\.{run_row['Lane']}/.*/Sample_{sample}.*\.fastq\.gz$", line):
fields = line.split(",")
file_path = fields[3].strip()
if "_R1_" in file_path:
fastq1 = os.path.basename(file_path)
fastq1_location_uri = file_path
elif "_R2_" in file_path:
fastq2 = os.path.basename(file_path)
fastq2_location_uri = file_path
if fastq1 and fastq2:
file_json = [
{
"location_uri": f"abacus://{fastq1_location_uri}",
"file_name": f"{fastq1}",
"file_extra_metadata": {"read_type": "R1"},
"file_deliverable": True
},
{
"location_uri": f"abacus://{fastq2_location_uri}",
"file_name": f"{fastq2}",
"file_extra_metadata": {"read_type": "R2"},
"file_deliverable": True
}
]
break
if not run_row['Clusters']:
raw_reads_count_flag = "MISSING"
if run_row['Clusters'] =='0':
raw_reads_count_flag = "FAILED"
else:
raw_reads_count_flag = "PASS"
if run_row['Library Type'] == "RNASeq":
raw_reads_count_flag = rna_raw_reads_count_check(sample, run_row['Clusters'])
raw_duplication_rate_flag = "PASS"
if run_row['Library Type'] != "RNASeq":
raw_duplication_rate_flag = dna_raw_duplication_rate_check(sample, run_row['Dup. Rate (%)'])
raw_median_insert_size_flag = median_insert_size_check(sample, run_row['Mapped Insert Size (median)'])
raw_mean_insert_size_flag = "PASS"
raw_mean_coverage_flag = "PASS"
if run_row['Library Type'] != "RNASeq":
raw_mean_coverage_flag = dna_raw_mean_coverage_check(sample, run_row['Mean Coverage'], sample_tumour)
metric_json = [
{
"metric_name": "raw_reads_count",
"metric_value": f"{run_row['Clusters']}",
"metric_flag": raw_reads_count_flag,
"metric_deliverable": True
},
{
"metric_name": "raw_duplication_rate",
"metric_value": f"{run_row['Dup. Rate (%)']}",
"metric_flag": raw_duplication_rate_flag
},
{
"metric_name": "raw_median_insert_size",
"metric_value": f"{run_row['Mapped Insert Size (median)']}",
"metric_flag": raw_median_insert_size_flag
},
{
"metric_name": "raw_mean_insert_size",
"metric_value": f"{run_row['Mapped Insert Size (mean)']}",
"metric_flag": raw_mean_insert_size_flag
},
{
"metric_name": "raw_mean_coverage",
"metric_value": f"{run_row['Mean Coverage']}",
"metric_flag": raw_mean_coverage_flag
}
]
readset_name = f"{sample}.{run_row['Run ID']}_{run_row['Lane']}"
readset_dict[readset_name] = (specimen, sample)
# Check if the readset is already in sample_json["readset"]
readset_names = [spec["readset_name"] for spec in sample_json["readset"]]
if readset_name in readset_names:
print(f"Duplicate readset: {readset_name}")
else:
# readset is not present, add it to specimen_json["readset"]
readset_json = {
"experiment_sequencing_technology": None,
"experiment_type": f"{run_row['Library Type']}",
"experiment_nucleic_acid_type": "RNA" if run_row['Library Type'] == "RNASeq" else "DNA",
"experiment_library_kit": None,
"experiment_kit_expiration_date": None,
"readset_name": readset_name,
"readset_lane": f"{run_row['Lane']}",
"readset_adapter1": f"{run_row['i7 Adapter Sequence']}",
"readset_adapter2": f"{run_row['i5 Adapter Sequence']}",
"readset_sequencing_type": f"{run_row['Run Type']}",
"readset_quality_offset": "33",
"file": file_json,
"metric": metric_json
}
sample_json["readset"].append(readset_json)
# sample_json["readset"].append(readset_json)
# specimen_json["sample"].append(sample_json)
# json_output["specimen"].append(specimen_json)
with open(output, 'w', encoding='utf-8') as file:
json.dump(json_output, file, ensure_ascii=False, indent=4)
return readset_dict, sample_dict
def dna_raw_mean_coverage_check(sample, value, tumour):
""" Mean Coverage DNA metric check """
if not value:
ret = "MISSING"
logger.warning(f"Missing 'Mean Coverage' value for {sample}")
if float(value)<30 and not tumour:
ret = "FAILED"
elif float(value)<80 and tumour:
ret = "FAILED"
else:
ret = "PASS"
return ret
def rna_raw_reads_count_check(sample, value):
""" Clusters RNA metric check """
if not value:
ret = "MISSING"
logger.warning(f"Missing 'RNA Cluster' value for {sample}")
if int(value)<80000000:
ret = "FAILED"
elif int(value)<100000000:
ret = "WARNING"
else:
ret = "PASS"
return ret
def dna_raw_duplication_rate_check(sample, value):
""" Dup. Rate (%) DNA metric check """
if not value:
ret = "MISSING"
logger.warning(f"Missing 'Dup. Rate (%)' value for {sample}")
elif float(value)>50:
ret = "FAILED"
elif float(value)>20:
ret = "WARNING"
else:
ret = "PASS"
return ret
def median_insert_size_check(sample, value):
""" Mapped Insert Size (median) metric check """
if not value:
ret = "MISSING"
logger.warning(f"Missing 'Median Insert Size' value for {sample}")
if float(value)<300:
ret = "WARNING"
elif float(value)<150:
ret = "FAILED"
else:
ret = "PASS"
return ret
if __name__ == '__main__':
main()