File size: 10,927 Bytes
8e574de
 
ae74a72
8e574de
 
 
 
 
 
ae74a72
 
 
8e574de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae74a72
 
 
 
 
8e574de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae74a72
8e574de
 
 
 
 
 
 
 
 
ae74a72
8e574de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae74a72
 
8e574de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Clean FireProtDB 2.0 CSV into ML-ready table with some reformatting.

Outputs:
- A canonical row-per-experiment table with parsed mutation fields and normalized columns.
- Optionally writes Parquet for speed.

Usage:
  python 01_process_csv.py \
    --input ../data/fireprotdb_20251015_16.csv \
    --output ../data/fireprotdb_clean.parquet

Notes:
- This script is conservative: it does NOT impute missing ddg/dtm.
- It standardizes a few categorical fields; extend mappings as needed.
"""

from __future__ import annotations

import argparse
import math
import re
from typing import Optional, Tuple, Dict

import pandas as pd
###PDB parsing
_PDB_SPLIT = re.compile(r"[;,| ]+")
_PDB_ID = re.compile(r"^[0-9][A-Za-z0-9]{3}$")  # 4-char PDB id, first char numeric

def parse_pdb_ids(x: object):
    """
    Returns (pdb_id, pdb_ids) where:
      - pdb_id: first valid 4-char PDB id (lowercase), or None
      - pdb_ids: sorted unique list of valid ids (lowercase)
    """
    if not isinstance(x, str):
        return None, []
    s = x.strip()
    if not s:
        return None, []

    parts = [p.strip() for p in _PDB_SPLIT.split(s) if p.strip()]
    ids = []
    for p in parts:
        p = p.strip()
        # sometimes entries include chain like "1ABC:A" or "1ABC_A"
        p = re.split(r"[:_]", p)[0].strip()
        if _PDB_ID.match(p):
            ids.append(p.lower())

    ids = sorted(set(ids))
    return (ids[0] if ids else None), ids

# --- Mutation parsing ---
# Accept common patterns:
#   A123V
#   p.Ala123Val (rare)
#   123A>V (rare)
_MUT_A123V = re.compile(r"^(?P<wt>[ACDEFGHIKLMNPQRSTVWY])(?P<pos>\d+)(?P<mut>[ACDEFGHIKLMNPQRSTVWY])$")
_MUT_123A_GT_V = re.compile(r"^(?P<pos>\d+)(?P<wt>[ACDEFGHIKLMNPQRSTVWY])>(?P<mut>[ACDEFGHIKLMNPQRSTVWY])$")


def parse_substitution(s: str) -> Tuple[Optional[str], Optional[int], Optional[str], Optional[str]]:
    """
    Returns (wt_residue, position, mut_residue, normalized_mutation_string)
    """
    if not isinstance(s, str) or not s.strip():
        return None, None, None, None
    s = s.strip()

    m = _MUT_A123V.match(s)
    if m:
        wt = m.group("wt")
        pos = int(m.group("pos"))
        mut = m.group("mut")
        return wt, pos, mut, f"{wt}{pos}{mut}"

    m = _MUT_123A_GT_V.match(s)
    if m:
        pos = int(m.group("pos"))
        wt = m.group("wt")
        mut = m.group("mut")
        return wt, pos, mut, f"{wt}{pos}{mut}"

    # If it's something else (multi-mutation, insertion/deletion notation, etc.),
    # keep it in "mutation_raw" but do not parse.
    return None, None, None, None


# --- Categorical normalization ---
def norm_str(x: object) -> Optional[str]:
    if not isinstance(x, str):
        return None
    x = x.strip()
    return x if x else None


BUFFER_MAP: Dict[str, str] = {
    "sodium tetraborate": "Sodium tetraborate",
    "tetra-borate": "Sodium tetraborate",
    "tetraborate": "Sodium tetraborate",
    "sodium phosphate": "Sodium phosphate",
}


METHOD_MAP: Dict[str, str] = {
    "dsc": "DSC",
    "cd": "CD",
}


MEASURE_MAP: Dict[str, str] = {
    "thermal": "Thermal",
}


def normalize_categoricals(df: pd.DataFrame) -> pd.DataFrame:
    def map_lower(series: pd.Series, mapping: Dict[str, str]) -> pd.Series:
        s = series.astype("string")
        s_lower = s.str.lower().str.strip()
        return s_lower.map(mapping).fillna(s.str.strip())

    if "BUFFER" in df.columns:
        df["buffer_norm"] = map_lower(df["BUFFER"], BUFFER_MAP)
    else:
        df["buffer_norm"] = pd.NA

    if "METHOD" in df.columns:
        df["method_norm"] = map_lower(df["METHOD"], METHOD_MAP)
    else:
        df["method_norm"] = pd.NA

    if "MEASURE" in df.columns:
        df["measure_norm"] = map_lower(df["MEASURE"], MEASURE_MAP)
    else:
        df["measure_norm"] = pd.NA

    return df


# --- Numeric cleanup ---
def to_float(x: object) -> Optional[float]:
    if x is None or (isinstance(x, float) and math.isnan(x)):
        return None
    if isinstance(x, (int, float)):
        return float(x)
    if isinstance(x, str):
        s = x.strip()
        if not s:
            return None
        # Handle "1mM" vs "1 mM" etc. for numeric fields by stripping units if present.
        # For now: attempt raw float parse.
        try:
            return float(s)
        except ValueError:
            # try to extract first float substring
            m = re.search(r"[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?", s)
            if m:
                try:
                    return float(m.group(0))
                except ValueError:
                    return None
    return None


def clean_numeric_columns(df: pd.DataFrame) -> pd.DataFrame:
    # ddg-like
    for col in ["DDG", "DOMAINOME_DDG", "DG", "DH", "DHVH"]:
        if col in df.columns:
            df[col.lower()] = df[col].map(to_float)
        else:
            df[col.lower()] = pd.NA

    # temperature-like
    for col in ["TM", "DTM", "EXP_TEMPERATURE"]:
        if col in df.columns:
            df[col.lower()] = df[col].map(to_float)
        else:
            df[col.lower()] = pd.NA
    # fitness
    if "DOMAINOME_FITNESS" in df.columns:
        df['fitness'] = df["DOMAINOME_FITNESS"].map(to_float)
    else:
        df['fitness'] = pd.NA

    # pH
    if "PH" in df.columns:
        df["ph"] = df["PH"].map(to_float)
    else:
        df["ph"] = pd.NA

    return df


def derive_labels(df: pd.DataFrame) -> pd.DataFrame:
    # Stabilizing classification: prefer explicit STABILIZING column if present,
    # else use ddg sign if ddg available.
    if "STABILIZING" in df.columns:
        s = df["STABILIZING"].astype("string").str.lower().str.strip()
        df["stabilizing_explicit"] = s.map({"yes": True, "no": False})
    else:
        df["stabilizing_explicit"] = pd.NA

    # ddg-based label (common convention: ddg < 0 stabilizing)
    df["stabilizing_ddg"] = df["ddg"].apply(lambda v: True if isinstance(v, float) and v < 0 else (False if isinstance(v, float) and v > 0 else pd.NA))

    # unified label: explicit if available else ddg-based
    df["stabilizing"] = df["stabilizing_explicit"]
    df.loc[df["stabilizing"].isna(), "stabilizing"] = df.loc[df["stabilizing"].isna(), "stabilizing_ddg"]

    return df


def select_and_rename(df: pd.DataFrame) -> pd.DataFrame:
    # canonical columns (keep more if you want)
    keep = {
        "EXPERIMENT_ID": "experiment_id",
        "SEQUENCE_ID": "sequence_id",
        "MUTANT_ID": "mutant_id",
        "SOURCE_SEQUENCE_ID": "source_sequence_id",
        "TARGET_SEQUENCE_ID": "target_sequence_id",
        "SEQUENCE_LENGTH": "sequence_length",
        "SUBSTITUTION": "substitution_raw",
        "INSERTION": "insertion_raw",
        "DELETION": "deletion_raw",
        "PROTEIN": "protein_name",
        "ORGANISM": "organism",
        "UNIPROTKB": "uniprotkb",
        "EC_NUMBER": "ec_number",
        "INTERPRO": "interpro",
        "PUBLICATION_PMID": "pmid",
        "PUBLICATION_DOI": "doi",
        "PUBLICATION_YEAR": "publication_year",
        "SOURCE_DATASET": "source_dataset",
        "REFERENCING_DATASET": "referencing_dataset",
        "WWPDB": "wwpdb_raw",

    }

    out = pd.DataFrame()
    for src, dst in keep.items():
        out[dst] = df[src] if src in df.columns else pd.NA

    # numeric & normalized categorical fields added earlier
    extra_cols = [
        "ddg", "domainome_ddg", "dg", "dh", "dhvh",
        "tm", "dtm", "exp_temperature", "fitness",
        "ph",
        "buffer_norm", "method_norm", "measure_norm",
        "stabilizing",
    ]
    for c in extra_cols:
        out[c] = df[c] if c in df.columns else pd.NA

    # keep raw text fields that matter for conditions (optional)
    for src, dst in [("BUFFER", "buffer_raw"), ("BUFFER_CONC", "buffer_conc_raw"), ("ION", "ion_raw"), ("ION_CONC", "ion_conc_raw"), ("STATE", "state")]:
        out[dst] = df[src] if src in df.columns else pd.NA
    out["pdb_id"] = df["pdb_id"] if "pdb_id" in df.columns else pd.NA
    out["pdb_ids"] = df["pdb_ids"] if "pdb_ids" in df.columns else [[] for _ in range(len(df))]
    return out


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--input", help="Path to raw FireProtDB 2.0 CSV", default='../data/fireprotdb_20251015-164116.csv')
    ap.add_argument("--output", help="Path to output .parquet or .csv", default='../data/fireprotdb_cleaned.parquet')
    ap.add_argument("--min_seq_len", type=int, default=1, help="Drop sequences shorter than this")
    ap.add_argument("--drop_no_label", action="store_true", help="Drop rows with neither ddg nor dtm")
    args = ap.parse_args()

    # Load as strings to avoid pandas guessing mixed types
    df = pd.read_csv(args.input, dtype="string", keep_default_na=False, na_values=["", "NA", "NaN", "nan"])
    df = df.replace({"": pd.NA})

    # Basic trimming
    for c in df.columns:
        if pd.api.types.is_string_dtype(df[c]):
            df[c] = df[c].astype("string").str.strip()

    # Normalize & parse
    df = normalize_categoricals(df)
    df = clean_numeric_columns(df)

    # Parse substitution into structured columns
    parsed = df["SUBSTITUTION"].apply(lambda x: parse_substitution(x) if "SUBSTITUTION" in df.columns else (None, None, None, None))
    df["wt_residue"] = parsed.map(lambda t: t[0])
    df["position"] = parsed.map(lambda t: t[1]).astype("Int64")
    df["mut_residue"] = parsed.map(lambda t: t[2])
    df["mutation"] = parsed.map(lambda t: t[3])

    df = derive_labels(df)

    if "WWPDB" in df.columns:
        parsed_pdb = df["WWPDB"].astype("string").fillna("").apply(lambda v: parse_pdb_ids(str(v)))
        df["pdb_id"] = parsed_pdb.map(lambda t: t[0])
        df["pdb_ids"] = parsed_pdb.map(lambda t: t[1])
    else:
        df["pdb_id"] = pd.NA
        df["pdb_ids"] = [[] for _ in range(len(df))]

    # Filter
    if "SEQUENCE_LENGTH" in df.columns:
        seq_len = df["SEQUENCE_LENGTH"].map(to_float)
        df["sequence_length_num"] = seq_len
        df = df[df["sequence_length_num"].fillna(0) >= args.min_seq_len]

    if args.drop_no_label:
        df = df[~(df["ddg"].isna() & df["dtm"].isna())]

    # Select final schema
    out = select_and_rename(df)

    # Add parsed mutation columns
    out["wt_residue"] = df["wt_residue"]
    out["position"] = df["position"]
    out["mut_residue"] = df["mut_residue"]
    out["mutation"] = df["mutation"]

    # De-dupe obvious duplicates (same experiment id)
    if "experiment_id" in out.columns:
        out = out.drop_duplicates(subset=["experiment_id"])

    # Write
    if args.output.lower().endswith(".parquet"):
        out.to_parquet(args.output, index=False)
    elif args.output.lower().endswith(".csv"):
        out.to_csv(args.output, index=False)
    else:
        raise ValueError("Output must end with .parquet or .csv")

    print(f"Wrote {len(out):,} rows to {args.output}")


if __name__ == "__main__":
    main()