File size: 11,970 Bytes
89078a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
import os
from typing import Dict, Iterable, Iterator, List, Tuple, Optional
from tqdm.auto import tqdm

from datasets import Dataset, DatasetDict, Features, Sequence, Value, ClassLabel, disable_progress_bars

disable_progress_bars()


# --------------------------------------------------------------------
# Defaults: your file names (searched under base_dir if data_files unset)
# --------------------------------------------------------------------
DEFAULT_ELUT_FILES = [
    "CB660_neural_stem_cell_IEX_Wan_2015.elut",
    "G166_glioma_stem_cell_IEX_Wan_2015_Hs_HCW_2.elut",
    "G166_glioma_stem_cell_IEX_Wan_2015_Hs_HCW_3.elut",
    "IEX_Wan_2015_Hs_IEX_1.elut",
    "IEX_Wan_2015_Hs_IEX_2.elut",
    "IEX_Wan_2015_Hs_HCW_4.elut",
    "IEX_Wan_2015_Hs_HCW_5.elut",
    "IEX_Wan_2015_Hs_HCW_6.elut",
    "IEX_Wan_2015_Hs_HCW_7.elut",
    "IEX_Wan_2015_Hs_HCW_8.elut",
    "IEX_Wan_2015_Hs_HCW_9.elut",
    "HEK_293_T_cells_SEC_Mallam_2019_C1.elut",
    "HEK_293_T_cells_SEC_Mallam_2019_C2.elut",
    "NTera2_embryonal_carcinoma_stem_cells_IEX_Moutaoufik_2019_2_R1.elut",
    "NTera2_embryonal_carcinoma_stem_cells_IEX_Moutaoufik_2019_2_R2.elut",
    "NTera2_embryonal_carcinoma_stem_cells_IEX_Moutaoufik_2019_R1.elut",
    "NTera2_embryonal_carcinoma_stem_cells_IEX_Moutaoufik_2019_R2.elut",
    "NTera2_embryonal_carcinoma_stem_cells_SEC_Moutaoufik_2019_2_R1.elut",
    "NTera2_embryonal_carcinoma_stem_cells_SEC_Moutaoufik_2019_2_R2.elut",
    "NTera2_embryonal_carcinoma_stem_cells_SEC_Moutaoufik_2019_R1.elut",
    "NTera2_embryonal_carcinoma_stem_cells_SEC_Moutaoufik_2019_R2.elut",
    "T98G_glioblastoma_multiforme_cells_SEC_Conelly_2018_Bio1.elut",
    "T98G_glioblastoma_multiforme_cells_SEC_Conelly_2018_Bio2.elut",
    "U2OS_cells_SEC_Kirkwood_2013_rep1.elut",
    "U2OS_cells_SEC_Kirkwood_2013_rep2.elut",
    "U2OS_cells_SEC_Kirkwood_2013_rep3.elut",
    "U2OS_cells_SEC_Larance_2016_PT3281S1.elut",
    "U2OS_cells_SEC_Larance_2016_PT3441S1.elut",
    "U2OS_cells_SEC_Larance_2016_PT3442S1.elut",
    "U2OS_cells_SEC_Larance_2016_PT3701S1.elut",
    "U2OS_cells_SEC_Larance_2016_PTSS3801.elut",
    "U2OS_cells_SEC_Larance_2016_PTSS3802.elut",
]


DEFAULT_PAIR_FILES = {
    # split -> (pos, neg)
    "train": (
        "intact_complex_merge_20230309.train_ppis.txt",
        "intact_complex_merge_20230309.neg_train_ppis.txt",
    ),
    "test": (
        "intact_complex_merge_20230309.test_ppis.txt",
        "intact_complex_merge_20230309.neg_test_ppis.txt",
    ),
}


def read_elut_table(path: str) -> Tuple[str, List[str], Dict[str, List[int]]]:
    experiment_id = os.path.splitext(os.path.basename(path))[0]
    table: Dict[str, List[int]] = {}

    with open(path, "r", encoding="utf-8", errors="ignore") as f:
        first = f.readline().rstrip("\n\r")
        if not first:
            return experiment_id, [], {}
        cols = first.split("\t")
        fraction_names = cols[1:]

        for line in f:
            line = line.rstrip("\n\r")
            if not line:
                continue
            toks = line.split("\t")
            if not toks:
                continue
            uniprot_id = toks[0]
            trace: Li8st[int] = []
            for x in toks[1:]:
                if x == "" or x is None:
                    trace.append(0)
                else:
                    try:
                        trace.append(int(float(x)))
                    except ValueError:
                        trace.append(0)
            table[uniprot_id] = trace

    return experiment_id, fraction_names, table


def read_pair_file(path: str) -> Iterable[Tuple[str, str]]:
    with open(path, "r", encoding="utf-8", errors="ignore") as f:
        for line in f:
            line = line.strip()
            if not line or line.startswith("#"):
                continue
            parts = line.split()
            if len(parts) != 2:
                continue
            yield parts[0], parts[1]


def build_proteins_dataset(elut_files: List[str]) -> Dataset:
    features = Features(
        {
            "experiment_id": Value("string"),
            "uniprot_id": Value("string"),
            "fraction_names": Sequence(Value("string")),
            "trace": Sequence(Value("int32")),
        }
    )

    def gen() -> Iterator[dict]:
        with tqdm(total=None, unit="ex",
                  desc="proteins: examples") as pbar:
            for elut_path in elut_files:
                exp_id, frac_names, table = read_elut_table(elut_path)
                for uid, trace in tqdm(table.items(),
                                       desc=f"{os.path.basename(elut_path)}",
                                       leave=False):
                    yield {
                        "experiment_id": exp_id,
                        "uniprot_id": uid,
                        "fraction_names": frac_names,
                        "trace": trace,
                    }
                    pbar.update(1)
    return Dataset.from_generator(gen, features=features)


def build_pairs_dataset(
        elut_files: List[str],
        split_posneg: Dict[str, Tuple[List[str], List[str]]]
) -> DatasetDict:
    features = Features(
        {
            "experiment_id": Value("string"),
            "uniprot_id1": Value("string"),
            "uniprot_id2": Value("string"),
            "elut_trace1": Sequence(Value("int32")),
            "elut_trace2": Sequence(Value("int32")),
            "label": ClassLabel(names=["neg", "pos"]),
        }
    )

    elut_tables: Dict[str, Tuple[List[str], Dict[str, List[int]]]] = {}
    for elut_path in tqdm(elut_files, desc="pairs: elution files"):
        try:
            exp_id, frac_names, table = read_elut_table(elut_path)
            elut_tables[exp_id] = (frac_names, table)
        except Exception as e:
            tqdm.write(f"WARNING: Failed to read {elut_path}: {e}")

    def make_split_gen(split: str, pos_files: List[str], neg_files: List[str]) -> Iterator[dict]:
        with tqdm(unit="ex", desc=f"{split}: examples") as pbar:
            def emit_for_pair(a: str, b: str, label: str) -> Iterator[dict]:
                for exp_id, (_fracs, table) in elut_tables.items():
                    ta = table.get(a)
                    tb = table.get(b)
                    if ta is None or tb is None:
                        continue
                    yield {
                        "experiment_id": exp_id,
                        "uniprot_id1": a,
                        "uniprot_id2": b,
                        "elut_trace1": ta,
                        "elut_trace2": tb,
                        "label": label,
                    }
                    pbar.update(1)

            #for pf in tqdm(pos_files, desc=f"{split}: positive files"):
            for pf in pos_files:
                #for a, b in tqdm(read_pair_file(pf), desc=os.path.basename(pf), leave=False):
                for a, b in read_pair_file(pf):
                    yield from emit_for_pair(a, b, "pos")

            #for nf in tqdm(neg_files, desc=f"{split}: negative files"):
            for nf in neg_files:
                #for a, b in tqdm(read_pair_file(nf), desc=os.path.basename(nf), leave=False):
                for a, b in read_pair_file(nf):
                    yield from emit_for_pair(a, b, "neg")

    dataset_dict = {}
    for split, (pos_paths, neg_paths) in split_posneg.items():
        dataset_dict[split] = Dataset.from_generator(
            make_split_gen,
            gen_kwargs={"split": split, "pos_files": pos_paths, "neg_files": neg_paths},
            features=features,
        )
    return DatasetDict(dataset_dict)


def existing(paths: List[str]) -> List[str]:
    return [p for p in paths if p and os.path.exists(p)]


def resolve_elut_files(base_dir: str, user_elut: Optional[List[str]]):
    if user_elut:
        return existing([os.path.abspath(p) for p in user_elut])
    # Fall back to defaults under base_dir
    candidates = [os.path.join(base_dir, f) for f in DEFAULT_ELUT_FILES]
    files = existing(candidates)
    if not files:
        raise FileNotFoundError("No .elut files found. Provide --elut or place defaults under --base_dir")
    return files


def resolve_pair_files(
        base_dir: str,
        train_pos: Optional[List[str]],
        train_neg: Optional[List[str]],
        test_pos: Optional[List[str]],
        test_neg: Optional[List[str]]
) -> Dict[str, Tuple[List[str], List[str]]]:
    out = {}
    if any([train_pos, train_neg, test_pos, test_neg]):
        if train_pos or train_neg:
            out["train"] = (
                existing([os.path.abspath(p) for p in train_pos or []]),
                existing([os.path.abspath(p) for p in train_neg or []])
            )
        if test_pos or test_neg:
            out["test"] = (
                existing([os.path.abspath(p) for p in test_pos or []]),
                existing([os.path.abspath(p) for p in test_neg or []])
            )
    else:
        # Use defaults under base_dir
        for split, (pos_path, neg_path) in DEFAULT_PAIR_FILES.items():
            out[split] = (
                existing([os.path.join(base_dir, pos_path)]),
                existing([os.path.join(base_dir, neg_path)])
            )
    if not out:
        raise ValueError("No pair files specified for 'pairs'.")
    return out


def main():
    ap = argparse.ArgumentParser(
        description="Build CF-MS dataset from .elut tables and .txt files. Uses both 'pairs' and 'proteins' configurations."
    )
    ap.add_argument("--view", choices=["proteins", "pairs"], default="pairs",
                    help="Dataset configuration to build (default: pairs).")
    ap.add_argument("--base_dir", type=str, default=os.getcwd(),
                    help="Directory to search for default files if paths not provided via '--elut' or --{test/train}_{pos/neg}")
    ap.add_argument("--elut", type=str, nargs="*",
                    help="Path(s) to .elut files. If omitted, defaults under '--base_dir' are used.")
    ap.add_argument("--train_pos", type=str, nargs="*",
                    help="Path(s) to positive TRAINING pairs .txt")
    ap.add_argument("--train_neg", type=str, nargs="*",
                    help="Path(s) to negative TRAINING pairs .txt")
    ap.add_argument("--test_pos", type=str, nargs="*",
                    help="Path(s) to positive TEST pairs .txt")
    ap.add_argument("--test_neg", type=str, nargs="*",
                    help="Path(s) to positive TEST pairs .txt")
    ap.add_argument("--out_dir", type=str, default=os.getcwd(),
                    help="Output directory for dataset .parquet files")
    args = ap.parse_args()

    base_dir = os.path.abspath(args.base_dir)
    os.makedirs(args.out_dir, exist_ok=True)

    elut_files = resolve_elut_files(base_dir, args.elut)

    # Create individual protein elutions dataset
    if args.view == "proteins":
        ds = build_proteins_dataset(elut_files)
        # saved as single 'train' split for consistency with original behavior
        #DatasetDict({"train": ds}).to_parquet(args.out_dir)
        ds.to_parquet(os.path.join(args.out_dir, "proteins_indiv_elut.parquet"))
        print(f"[ok] proteins dataset saved to {args.out_dir}")
    else:
        split_posneg = resolve_pair_files(
            base_dir,
            args.train_pos, args.train_neg,
            args.test_pos, args.test_neg
        )
        dataset_dict = build_pairs_dataset(elut_files, split_posneg)
        ds_train = dataset_dict["train"]
        ds_test = dataset_dict["test"]
        ds_train.to_parquet(os.path.join(args.out_dir, "pairs_elut_TRAIN.parquet"))
        ds_test.to_parquet(os.path.join(args.out_dir, "pairs_elut_TEST.parquet"))
        # quick counts
        for split, ds in dataset_dict.items():
            print(f"[ok] {split}: {len(ds):,} examples")
        print(f"[ok] pairs dataset saved to {args.out_dir}")

if __name__ == "__main__":
    main()