-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfms_run_processing2json.py
executable file
·373 lines (343 loc) · 16.3 KB
/
fms_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
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
#!/usr/bin/env python3
import argparse
import json
import re
import glob
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 parse_arguments():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
prog='run_processing2json.py',
description="Creates json file for project tracking database from a Run generated by GenPipes Run Processing and Freezeman."
)
parser.add_argument(
'-i',
'--input',
required=True,
help="Folder of the Run to be converted. To be formatted like this /lb/robot/research/freezeman-processing/<sequencer>/<year>/<run_folder>."
)
parser.add_argument(
'-o',
'--output',
default=None,
help="Output json filename (Default: .json)."
)
parser.add_argument(
'-n',
'--nucleic_acid_type',
choices=['DNA', 'RNA'],
default='ALL',
help="Nucleic Acid type, either DNA or RNA (Default: ALL)."
)
parser.add_argument(
'-l',
'--lane',
nargs='+',
default=["1", "2", "3", "4", "5", "6", "7", "8"],
help="Only considers lane(s) provided for json creation."
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
'-s',
'--sample',
nargs='+',
help="Only considers sample(s) provided for json creation."
)
group.add_argument(
'-x',
'--xsample',
nargs='+',
help="Ignores sample(s) provided for json creation."
)
return parser.parse_args()
def load_json_file(file_path):
"""Load a JSON file."""
try:
with open(file_path, mode="r") as file:
return json.load(file)
except FileNotFoundError:
logger.error(f"JSON file not found: {file_path}")
except json.JSONDecodeError:
logger.error(f"Error decoding JSON file: {file_path}")
return None
def parse_sample_name(sample_name):
"""Parse the sample name to extract specimen, cohort, and institution."""
result = re.search(r"^((MoHQ-(JG|CM|GC|MU|MR|XX|HM|CQ)-\w+)-\w+)-\w+-\w+(D|R)(T|N)", sample_name)
return result.group(1), result.group(2), result.group(3)
def jsonify_run_processing(input_run_folder, fms_json, lanes_json, output, lanes, samples, nucleic_acid_type):
"""
Converts the Run Processing csv file to a json file for project tracking database.
Args:
input_run_folder (str): Path to the Run Processing csv file.
fms_json (json): JSON file from Freezeman.
output (str): Path to the output json file.
lanes (list): List of lanes to consider.
samples (list): List of samples to consider.
nucleic_acid_type (str): Nucleic Acid type to consider.
"""
readset_dict = {}
sample_dict = {}
json_output = {
"operation_platform": "abacus",
"project_ext_id": None,
"project_ext_src": None,
"project_name": "MOH-Q",
"run_ext_id": fms_json["run_obj_id"],
"run_ext_src": "FREEZEMAN",
"run_name": input_run_folder.split("/")[-1],
"run_instrument": "novaseq",
"run_date": f"{datetime.strptime(fms_json['run_start_date'], '%Y-%m-%d')}",
"specimen": []
}
for _, lane_json in lanes_json.items():
for readset_key, readset in lane_json["readsets"].items():
sample_name = readset["sample_name"]
if sample_name.startswith("MoHQ") and lane_json['lane'] in lanes and sample_name in samples:
specimen, cohort, institution = parse_sample_name(sample_name)
# 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)
# Check if the sample is already in specimen_json["sample"]
sample_names = [spec["sample_name"] for spec in specimen_json["sample"]]
if sample_name in sample_names:
# sample is present, find its position
position = sample_names.index(sample_name)
sample_json = specimen_json["sample"][position]
else:
# sample is not present, add it to specimen_json["sample"]
sample_json = {
"sample_ext_id": int(readset["derived_sample_obj_id"]),
"sample_ext_src": "FREEZEMAN",
"sample_name": sample_name,
"sample_tumour": sample_name.endswith("T"),
"readset": []
}
specimen_json["sample"].append(sample_json)
if sample_name.endswith("RT"):
fastq1 = readset["fastq_1"]["final_path"]
fastq2 = readset["fastq_2"]["final_path"]
file_json = [
{
"location_uri": f"abacus://{fastq1}",
"file_name": f"{os.path.basename(fastq1)}",
"file_extra_metadata": {"read_type": "R1"},
"file_deliverable": True
},
{
"location_uri": f"abacus://{fastq2}",
"file_name": f"{os.path.basename(fastq2)}",
"file_extra_metadata": {"read_type": "R2"},
"file_deliverable": True
}
]
else:
bam = readset["bam"]["final_path"]
bai = readset["bai"]["final_path"]
file_json = [
{
"location_uri": f"abacus://{bam}",
"file_name": f"{os.path.basename(bam)}",
"file_deliverable": True
},
{
"location_uri": f"abacus://{bai}",
"file_name": f"{os.path.basename(bai)}",
"file_deliverable": True
}
]
for run_v in lane_json["run_validation"]:
if run_v.get("sample") == readset_key:
raw_reads_count = run_v.get("qc", {}).get("nb_reads")
raw_reads_count_flag = get_flag(raw_reads_count)
raw_reads_count, raw_reads_count_flag = check_na(raw_reads_count, raw_reads_count_flag)
if raw_reads_count == 0:
raw_reads_count_flag = "FAILED"
if readset["library_type"] == "RNASeq":
raw_reads_count_flag = rna_raw_reads_count_check(sample_name, raw_reads_count)
raw_duplication_rate = run_v.get("qc", {}).get("duplication_rate")
raw_duplication_rate_flag = get_flag(raw_duplication_rate)
if readset["library_type"] != "RNASeq":
raw_duplication_rate_flag = dna_raw_duplication_rate_check(sample_name, raw_duplication_rate)
raw_duplication_rate, raw_duplication_rate_flag = check_na(raw_duplication_rate, raw_duplication_rate_flag)
raw_median_insert_size = run_v.get("alignment", {}).get("median_aligned_insert_size")
raw_median_insert_size_flag = median_insert_size_check(sample_name, raw_median_insert_size) if raw_median_insert_size else "MISSING"
raw_median_insert_size, raw_median_insert_size_flag = check_na(raw_median_insert_size, raw_median_insert_size_flag)
raw_mean_insert_size = run_v.get("alignment", {}).get("average_aligned_insert_size")
raw_mean_insert_size_flag = get_flag(raw_mean_insert_size)
raw_mean_insert_size, raw_mean_insert_size_flag = check_na(raw_mean_insert_size, raw_mean_insert_size_flag)
raw_mean_coverage = run_v.get("alignment", {}).get("mean_coverage")
raw_mean_coverage_flag = get_flag(raw_mean_coverage)
raw_mean_coverage, raw_mean_coverage_flag = check_na(raw_mean_coverage, raw_mean_coverage_flag)
if raw_mean_coverage_flag == "PASS" and readset["library_type"] != "RNASeq":
raw_mean_coverage_flag = dna_raw_mean_coverage_check(sample_name, raw_mean_coverage, sample_name.endswith("T"))
metric_json = [
{
"metric_name": "raw_reads_count",
"metric_value": raw_reads_count,
"metric_flag": raw_reads_count_flag,
"metric_deliverable": True
},
{
"metric_name": "raw_duplication_rate",
"metric_value": raw_duplication_rate,
"metric_flag": raw_duplication_rate_flag
},
{
"metric_name": "raw_median_insert_size",
"metric_value": raw_median_insert_size,
"metric_flag": raw_median_insert_size_flag
},
{
"metric_name": "raw_mean_insert_size",
"metric_value": raw_mean_insert_size,
"metric_flag": raw_mean_insert_size_flag
},
{
"metric_name": "raw_mean_coverage",
"metric_value": raw_mean_coverage,
"metric_flag": raw_mean_coverage_flag
}
]
readset_name = f"{sample_name}.{lane_json['run']}_{lane_json['lane']}"
readset_dict[readset_name] = (specimen, sample_name)
# 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"]
for fms_s in fms_json["samples"]:
if fms_s.get("sample_name") == sample_name:
library_kit = fms_s["library_kit"]
readset_json = {
"experiment_sequencing_technology": None,
"experiment_type": f"{readset['library_type']}",
"experiment_nucleic_acid_type": "RNA" if readset["library_type"] == "RNASeq" else "DNA",
"experiment_library_kit": library_kit,
"experiment_kit_expiration_date": None,
"readset_name": readset_name,
"readset_lane": f"{lane_json['lane']}",
"readset_adapter1": f"{readset['barcodes'][0]['ADAPTERi7']}",
"readset_adapter2": f"{readset['barcodes'][0]['ADAPTERi5']}",
"readset_sequencing_type": f"{lane_json['sequencing_method']}",
"readset_quality_offset": "33",
"file": file_json,
"metric": metric_json
}
sample_json["readset"].append(readset_json)
specimens_to_remove = []
for specimen in json_output.get("specimen", []):
samples_to_remove = []
for sample in specimen.get("sample", []):
readsets_to_remove = []
for readset in sample.get("readset", []):
if readset.get("experiment_nucleic_acid_type") != nucleic_acid_type and nucleic_acid_type != 'ALL':
readsets_to_remove.append(readset)
for readset in readsets_to_remove:
sample["readset"].remove(readset)
if not sample["readset"]:
samples_to_remove.append(sample)
for sample in samples_to_remove:
specimen["sample"].remove(sample)
if not specimen["sample"]:
specimens_to_remove.append(specimen)
for specimen in specimens_to_remove:
json_output["specimen"].remove(specimen)
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 check_na(value, flag):
""" Check if value is 'NA' and set flag to 'NOT_APPLICABLE' """
if value == "N/A":
value = None
flag = "NOT_APPLICABLE"
return value, flag
def get_flag(value, default="PASS", missing="MISSING"):
""" Get flag for metric """
return default if value is not None else missing
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} from json.")
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 'nb_reads' value for {sample} from json.")
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 'duplication_rate' value for {sample} from json.")
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_aligned_insert_size' value for {sample} from json.")
if float(value)<300:
ret = "WARNING"
elif float(value)<150:
ret = "FAILED"
else:
ret = "PASS"
return ret
def main():
""" Main """
args = parse_arguments()
output = args.output or f"{os.path.basename(args.input)}.json"
lanes = args.lane
fms_json = load_json_file(glob.glob(os.path.join(args.input, "*.json"))[0])
if not fms_json:
return
lanes_json_files = glob.glob(os.path.join(args.input, "report", "*.run_validation_report.json"))
lanes_json = {os.path.basename(file): load_json_file(file) for file in lanes_json_files}
if args.sample:
samples = list(args.sample)
elif args.xsample:
all_samples = [sample["sample_name"] for sample in fms_json["samples"]]
samples = list(set(all_samples).difference(set(args.xsample)))
else:
samples = [sample["sample_name"] for sample in fms_json["samples"]]
jsonify_run_processing(args.input, fms_json, lanes_json, output, lanes, samples, args.nucleic_acid_type)
if __name__ == '__main__':
main()