Mercurial > repos > maciek > spamr_vet_tools
comparison spamr_vet_tools_v2/mlst_amrfinder_staramr.py @ 2:d7b099fbb003 draft default tip
Corrected file names and updated tool wrappers for consistency.
author | maciek |
---|---|
date | Tue, 25 Mar 2025 13:35:00 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:e57a908b9d3d | 2:d7b099fbb003 |
---|---|
1 import json | |
2 import csv | |
3 import sys | |
4 | |
5 def generate_csv_from_json(json_data): | |
6 """ | |
7 Parse the JSON and generate CSV files based on the analysis_software_name Abricate, AMRfinder plus and STARamr. | |
8 Additionally, extract and process the 'mlst_file' content into its own CSV. | |
9 """ | |
10 for entry in json_data: | |
11 analysis_software = entry.get("analysis_software_name", "unknown") | |
12 results = entry.get("results", []) | |
13 | |
14 if results: | |
15 csv_file = f"{analysis_software}_output.csv" | |
16 extracted_data = [] | |
17 headers = [] | |
18 | |
19 for result in results: | |
20 if result.get("name") == "mlst_file": | |
21 mlst_file_path = "mlst.csv" | |
22 mlst_content = result.get("content", []) | |
23 mlst_headers = ["Isolate ID", "Scheme", "Sequence Type", "Locus"] | |
24 | |
25 # Write the MLST CSV file | |
26 if mlst_content: | |
27 with open(mlst_file_path, "w", newline="", encoding="utf-8") as f: | |
28 writer = csv.DictWriter(f, fieldnames=mlst_headers) | |
29 writer.writeheader() | |
30 for row in mlst_content: | |
31 writer.writerow({ | |
32 "Isolate ID": row.get("Isolate ID", ""), | |
33 "Scheme": row.get("Scheme", ""), | |
34 "Sequence Type": row.get("Sequence Type", ""), | |
35 "Locus": "; ".join(row.get("Locus", [])) | |
36 }) | |
37 | |
38 print(f"MLST CSV file successfully generated: {mlst_file_path}") | |
39 | |
40 if "content" in result and isinstance(result["content"], list): | |
41 for content_item in result["content"]: | |
42 extracted_data.append(content_item) | |
43 for key in content_item.keys(): | |
44 if key not in headers: | |
45 headers.append(key) # Maintain the original order of the JSON keys | |
46 | |
47 # Write the CSV file if there is data | |
48 if extracted_data: | |
49 with open(csv_file, "w", newline="", encoding="utf-8") as f: | |
50 writer = csv.DictWriter(f, fieldnames=headers) | |
51 writer.writeheader() | |
52 for row in extracted_data: | |
53 writer.writerow({key: row.get(key, "") for key in headers}) | |
54 | |
55 print(f"CSV file successfully generated: {csv_file}") | |
56 else: | |
57 print(f"No content found for {analysis_software}.") | |
58 | |
59 if __name__ == "__main__": | |
60 if len(sys.argv) != 2: | |
61 print("Usage: python script.py input.json") | |
62 sys.exit(1) | |
63 | |
64 input_json_file = sys.argv[1] | |
65 | |
66 try: | |
67 with open(input_json_file, "r", encoding="utf-8") as file: | |
68 json_data = json.load(file) | |
69 generate_csv_from_json(json_data) | |
70 sys.exit(0) | |
71 except Exception as e: | |
72 print(f"Error processing file: {e}") | |
73 sys.exit(1) |