-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_report_file.py
executable file
·225 lines (195 loc) · 5.95 KB
/
parse_report_file.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
#!/usr/bin/env python3
## parse_report_file.py
## Cdiff related QC analysis
## Parses a report file to generate a .csv file and a .json file.g
import argparse
import os
import re
import json
parser = argparse.ArgumentParser(description="parse_report_file.py")
parser.add_argument("-r", "--report_file", help="report_file.txt")
parser.add_argument("-w", "--wgsnumber", help="wgsnumber", default="NA")
parser.add_argument("-s", "--stbit", help="ST string", default="ST;NA:NA")
parser.add_argument("-o", "--output_dir", default="output")
args = parser.parse_args()
## Functions
def print_header_to_output(csv_outfile):
"""Print header to a file
"""
header = "Name;cdtA/B;tcdA;tcdB;tcdClength;117del;A117T;TRST;TR6;TR10;ST;STalleles;WGS;tcdA:tcdB:tcdC:cdtA:cdtB"
csv_outfile=open(csv_outfile, "w")
print(header, file=csv_outfile)
return header
def indellength(info):
"""Get indel length
"""
try:
# Split the info string into elements
elements = info.split("),")
results = []
# Regex pattern to match the nucleotide sequences
pattern = re.compile(r"\{?\s?\d+:\s\(\"([ATCG]+)\"\,\s\"([ATCG]+)")
for case in elements:
match = pattern.search(case)
if match:
seq1, seq2 = match.groups()
length_diff = len(seq1) - len(seq2)
results.append(str(length_diff))
if not results:
results = ["0"]
return "_".join(results)
except Exception as e:
# Log the exception for debugging purposes
print(f"An error occurred: {e}")
return "0"
def get_sampleid(line, csv_data):
"""Line
Strain: cdiff1
"""
sampleid = "-"
if line.startswith("Strain"):
sampleid = line.split(":")[1].strip()
csv_data["Name"] = sampleid
return csv_data
def process_tcdAB_genes(line, csv_data):
"""Line
Found in tcdA:Gene is not present;64/8133;{}"""
genes = ["tcdA", "tcdB"]
for gene in genes:
if line.startswith(f"Found in {gene}:"):
answer = line.strip().split(":")[1].split(";")
if answer[0] == "Gene is present":
csv_data[f"{gene}"] = "+"
csv_data["cov_info"][f"{gene}"] = answer[1]
return csv_data
def process_tcdC_gene(line, csv_data):
"""Line
Found in tcdC:Gene is not present;0/700;{}
"""
if line.startswith("Found in tcdC:"):
csv_data["cov_info"][f"tcdC"] = line.split(":", 1)[1].split(";")[1]
csv_data["tcdClength"] = indellength(line.split(":", 1)[1].split(";")[2])
if "18499: ('CT', 'C')" in line:
csv_data["117del"] = "+"
remaining = csv_data["tcdClength"].split("_")
first = remaining.pop(0)
if first in {"4", "18", "54"}:
csv_data["tcdClength"] = first
elif remaining:
csv_data["tcdClength"] = str(remaining)
else:
csv_data["tcdClength"] = "0"
else:
csv_data["117del"] = "-"
return csv_data
def process_A117T(line, csv_data):
"""Line
gi|126697566|ref|NC_009089.1|
"""
if line.startswith("gi|"):
print(line)
answer = line.split()
A117T = answer[4]
if A117T == "A" and not line[6] == "LowQual":
header = answer[8].split(":")
index = header.index("DP")
depth = answer[9].split(":")[index]
if int(depth) > 20:
csv_data["A117T"] = "+"
return csv_data
def process_cdtAB_genes(line, csv_data):
"""Line
Found in ..
"""
genes = ["cdtA", "cdtB"]
for gene in genes:
if line.startswith(f"Found in {gene}:"):
answer = line.split(":", 1)[1].split(";")
if answer[0] == "Gene is present":
csv_data[f"{gene}"] = "+"
csv_data["cov_info"][f"{gene}"] = answer[1]
return csv_data
def process_trst_results(line, csv_data, report):
"""Line
[]
"""
if line.startswith("TRST results"):
next_line = next(report).strip()
TR6 = TR10 = ""
if next_line.startswith("["):
TR6=next_line[1:-1].split(', ')
TR6=','.join([element.strip("'") for element in TR6])
csv_data["TR6"] = TR6
TR10 = next(report).strip()[1:-1].strip("'")
csv_data["TR10"] = TR10
csv_data["TRST"] = next(report).split()[0].strip("'")
if not TR6:
csv_data["TR6"] = "Unknown"
if not TR10:
csv_data["TR10"] = "Unknown"
if csv_data["TRST"] == "trunknown":
csv_data["TRST"] = "Unknown"
return csv_data
def parse_report(report_file, stbit, wgsnumber):
"""Parse a text file
"""
with open(report_file, "r") as report:
csv_data = {
"Name": "-",
"cdtA": "-",
"cdtB": "-",
"tcdA": "-",
"tcdB": "-",
"tcdClength": "0",
"117del": "-",
"A117T": "-",
"TRST": "-",
"TR6": "-",
"TR10": "-",
"ST": f"{stbit}",
"WGS": f"{wgsnumber}",
"cov_info": {"tcdA": "-", "tcdB": "-", "tcdC": "-", "cdtA": "-", "cdtB": "-"}
}
for line in report:
csv_data = get_sampleid(line, csv_data)
csv_data = process_trst_results(line, csv_data, report)
csv_data = process_A117T(line, csv_data)
csv_data = process_tcdAB_genes(line, csv_data)
csv_data = process_tcdC_gene(line, csv_data)
csv_data = process_cdtAB_genes(line, csv_data)
return csv_data
def write_to_csv(csv_data, csv_outfile):
"""Write to csv file
"""
with open(csv_outfile, "a") as csvout:
if csv_data:
fields = [
# header: "Name;cdtA/B;tcdA;tcdB;tcdClength;117del;A117T;TRST;TR6;TR10;ST;STalleles;WGS;tcdA:tcdB:tcdC:cdtA:cdtB"
csv_data["Name"],
f"{csv_data["cdtA"]}/{csv_data["cdtB"]}",
csv_data["tcdA"],
csv_data["tcdB"],
csv_data["tcdClength"],
csv_data["117del"],
csv_data["A117T"],
csv_data["TRST"],
csv_data["TR6"],
csv_data["TR10"],
csv_data["ST"],
csv_data["WGS"],
f"{csv_data["cov_info"]["tcdA"]}:{csv_data["cov_info"]["tcdB"]}:{csv_data["cov_info"]["tcdC"]}:{csv_data["cov_info"]["cdtA"]}:{csv_data["cov_info"]["cdtB"]}"
]
print(";".join(fields), file=csvout)
def write_to_json(csv_data, json_outfile):
"""Write to json file
"""
with open(json_outfile, "w") as jsonout:
json.dump(csv_data, jsonout)
## Main
csv_data = parse_report(args.report_file, args.stbit, args.wgsnumber)
sampleid = csv_data["Name"]
csv_outfile=os.path.join(args.output_dir, f"{sampleid}.csv")
header = print_header_to_output(csv_outfile)
write_to_csv(csv_data, csv_outfile)
json_outfile=os.path.join(args.output_dir, f"{sampleid}.json")
write_to_json(csv_data, json_outfile)