-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd.count_file.py
More file actions
157 lines (113 loc) · 2.99 KB
/
Copy pathd.count_file.py
File metadata and controls
157 lines (113 loc) · 2.99 KB
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
import os
import re
import json
from collections import defaultdict
INPUT_DIR = "ncbi"
OUTPUT_FILE = "accession_to_files.json"
# Valid accession patterns:
# KP710246
# NC_011268
# AB123456
# etc.
ACCESSION_PATTERN = re.compile(
r'([A-Z]{1,4}_?\d{5,9})'
)
def clean_accession(raw_value):
"""
Clean accession strings.
Handles:
L:KP710246
M: KP710264
KP710246.1
LK928904 (nts 2253-10260)
(nts 2253-10260):LK928904
, NC_011268
"""
value = str(raw_value).strip()
# Remove segment labels
# L:KP710246 -> KP710246
if ":" in value:
value = value.split(":")[-1].strip()
# Remove parenthesis annotations
# LK928904 (nts 2253-10260)
if "(" in value:
value = value.split("(")[0].strip()
# Remove version number
# NC_011268.1 -> NC_011268
if "." in value:
left, right = value.rsplit(".", 1)
if right.isdigit():
value = left
# Remove leading/trailing junk
value = value.strip(" ,;")
# Extract only valid accession
match = ACCESSION_PATTERN.search(value)
if match:
return match.group(1)
return None
# accession -> set(files)
accession_map = defaultdict(set)
# =========================
# PROCESS FILES
# =========================
for filename in os.listdir(INPUT_DIR):
if not filename.endswith(".json"):
continue
filepath = os.path.join(INPUT_DIR, filename)
print(f"Processing {filename}")
try:
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
for row in data:
for key, value in row.items():
# Only accession columns
if "accession" not in key.lower():
continue
# Skip empty fields
if not value:
continue
text = str(value).strip()
if not text:
continue
# Split multiple accessions
# Example:
# L:KP710246; M:KP710264; S:KP710267
parts = text.split(";")
for part in parts:
accession = clean_accession(part)
if not accession:
continue
accession_map[accession].add(
filename
)
except Exception as e:
print(f"ERROR {filename}: {e}")
# =========================
# BUILD OUTPUT
# =========================
output = []
for accession in sorted(accession_map):
output.append({
"accession": accession,
"files": sorted(
accession_map[accession]
)
})
# =========================
# WRITE OUTPUT
# =========================
with open(
OUTPUT_FILE,
"w",
encoding="utf-8"
) as f:
json.dump(
output,
f,
indent=4
)
print(
f"\nDONE\n"
f"Unique accessions: {len(output)}\n"
f"Output written to: {OUTPUT_FILE}"
)