File size: 2,045 Bytes
5032722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Bridges a naming/shape mismatch between preprocess_data/ output and the
pair_alignment/ dataloader (FullLenDset), discovered while reproducing
AnnabelLarge/protein_evolution_icml_2026:

1. metadata.tsv uses 'num_dels' but FullLenDset._load_metadata expects 'num_del'.
2. FullLenDset expects a *_pair-times.tsv (pairID, branch length) which
   preprocess_data/clean_data.py never writes; we derive it from the
   TREEDIST_anc-to-desc column already present in metadata.tsv.
3. FullLenDset expects *_AAcounts.npy to be a single (alphabet_size,) vector
   of amino-acid emission counts summed over the whole split (used to seed
   the F81 equilibrium distribution). clean_data.py instead writes a
   per-pair (N, 3, 3) array (a different, unrelated count), which crashes
   the += accumulation in FullLenDset. We recompute the correct (20,) vector
   directly from the aligned_mats.npy amino-acid tokens (indices 3..22).
"""
import argparse
import numpy as np
import pandas as pd


def fix_split(data_dir: str, split: str) -> None:
    meta_path = f"{data_dir}/{split}_metadata.tsv"
    df = pd.read_csv(meta_path, sep="\t", index_col=0)
    df = df.rename(columns={"num_dels": "num_del"})
    df.to_csv(meta_path, sep="\t")

    times_path = f"{data_dir}/{split}_pair-times.tsv"
    df[["pairID", "TREEDIST_anc-to-desc"]].to_csv(
        times_path, sep="\t", header=False, index=False
    )

    aligned = np.load(f"{data_dir}/{split}_aligned_mats.npy")  # (N, L, 4)
    tokens = np.concatenate([aligned[:, :, 0].ravel(), aligned[:, :, 1].ravel()])
    counts = np.array([(tokens == (3 + i)).sum() for i in range(20)], dtype=np.uint32)
    np.save(f"{data_dir}/{split}_AAcounts.npy", counts)

    print(f"{split}: {len(df)} pairs, AAcounts sum={counts.sum()}")


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("-data_dir", required=True)
    ap.add_argument("-splits", nargs="+", required=True)
    args = ap.parse_args()
    for s in args.splits:
        fix_split(args.data_dir, s)