File size: 23,378 Bytes
fcbea81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#!/usr/bin/env python3
"""Convert raw STRING downloads into Hugging Face Data Viewer-friendly Parquet.

The converter streams the raw files, assigns rows to deterministic
train/validation/test splits, and writes sharded Parquet files under:

    data/<config>/<split>-00000.parquet

It is designed for very large STRING files: chunks are parsed in a bounded
process pool and written incrementally, so the complete dataset is never loaded
into memory.
"""

import argparse
import gzip
import io
import json
import logging
import shutil
import subprocess
import sys
import time
import zlib
from concurrent.futures import FIRST_COMPLETED, ProcessPoolExecutor, wait
from contextlib import contextmanager
from pathlib import Path
from typing import Dict, Iterator, List, Mapping, Optional, Sequence, Set, Tuple


TABLE_ORDER = (
    "species",
    "protein_info",
    "protein_aliases",
    "protein_sequences",
    "protein_links",
)

RAW_FILES = {
    "species": "species.v12.0.txt",
    "protein_info": "protein.info.v12.0.txt.gz",
    "protein_aliases": "protein.aliases.v12.0.txt.gz",
    "protein_sequences": "protein.sequences.v12.0.fa.gz",
    "protein_links": "protein.links.full.v12.0.txt.gz",
}

COLUMNS = {
    "species": (
        "taxon_id",
        "string_type",
        "string_name_compact",
        "official_name_ncbi",
        "domain",
    ),
    "protein_info": (
        "string_protein_id",
        "taxon_id",
        "preferred_name",
        "protein_size",
        "annotation",
    ),
    "protein_aliases": (
        "string_protein_id",
        "taxon_id",
        "alias",
        "source",
    ),
    "protein_sequences": (
        "string_protein_id",
        "taxon_id",
        "sequence",
        "sequence_length",
    ),
    "protein_links": (
        "protein1",
        "protein2",
        "taxon_id",
        "neighborhood",
        "neighborhood_transferred",
        "fusion",
        "cooccurence",
        "homology",
        "coexpression",
        "coexpression_transferred",
        "experiments",
        "experiments_transferred",
        "database",
        "database_transferred",
        "textmining",
        "textmining_transferred",
        "combined_score",
    ),
}

TYPE_NAMES = {
    "species": (
        "int32",
        "string",
        "string",
        "string",
        "string",
    ),
    "protein_info": (
        "string",
        "int32",
        "string",
        "int32",
        "string",
    ),
    "protein_aliases": (
        "string",
        "int32",
        "string",
        "string",
    ),
    "protein_sequences": (
        "string",
        "int32",
        "string",
        "int32",
    ),
    "protein_links": (
        "string",
        "string",
        "int32",
        "int16",
        "int16",
        "int16",
        "int16",
        "int16",
        "int16",
        "int16",
        "int16",
        "int16",
        "int16",
        "int16",
        "int16",
        "int16",
        "int16",
    ),
}


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Post-process STRING v12.0 raw files into sharded Parquet configs."
    )
    parser.add_argument("--raw-dir", type=Path, default=Path("v12.0"))
    parser.add_argument("--output-dir", type=Path, default=Path("data"))
    parser.add_argument(
        "--tables",
        default="all",
        help="Comma-separated table list, or 'all'. Valid tables: %s" % ", ".join(TABLE_ORDER),
    )
    parser.add_argument("--num-proc", type=int, default=32)
    parser.add_argument(
        "--max-in-flight",
        type=int,
        default=None,
        help="Maximum parser chunks queued at once. Defaults to --num-proc.",
    )
    parser.add_argument(
        "--rows-per-chunk",
        type=int,
        default=100_000,
        help="Rows sent to one worker at a time. Lower this if RAM is tight.",
    )
    parser.add_argument("--train-ratio", type=float, default=0.98)
    parser.add_argument("--validation-ratio", type=float, default=0.01)
    parser.add_argument("--test-ratio", type=float, default=0.01)
    parser.add_argument(
        "--split-seed",
        default="string-v12.0",
        help="Stable salt used by the row hash split assignment.",
    )
    parser.add_argument(
        "--link-min-combined-score",
        type=int,
        default=None,
        help="Optional filter for protein_links. Keeps rows with combined_score >= this value.",
    )
    parser.add_argument(
        "--max-rows-per-table",
        type=int,
        default=None,
        help="Optional raw-row cap per table, useful for creating a small preview dataset.",
    )
    parser.add_argument(
        "--compression",
        default="zstd",
        choices=("zstd", "snappy", "gzip", "brotli", "none"),
        help="Parquet compression codec.",
    )
    parser.add_argument(
        "--decompressor",
        default="auto",
        choices=("auto", "python", "gzip", "pigz"),
        help="How to stream .gz files. auto uses pigz when installed, otherwise Python gzip.",
    )
    parser.add_argument(
        "--decompressor-proc",
        type=int,
        default=4,
        help="Threads passed to pigz when --decompressor is pigz/auto and pigz is available.",
    )
    parser.add_argument(
        "--overwrite",
        action="store_true",
        help="Delete existing output subdirectories for selected tables before writing.",
    )
    parser.add_argument("--log-every", type=int, default=25)
    return parser.parse_args()


def resolve_tables(value: str) -> List[str]:
    if value == "all":
        return list(TABLE_ORDER)
    selected = [item.strip() for item in value.split(",") if item.strip()]
    unknown = sorted(set(selected) - set(TABLE_ORDER))
    if unknown:
        raise ValueError("Unknown table(s): %s" % ", ".join(unknown))
    return selected


def split_cutoffs(train_ratio: float, validation_ratio: float, test_ratio: float) -> Tuple[int, int]:
    total = train_ratio + validation_ratio + test_ratio
    if abs(total - 1.0) > 1e-9:
        raise ValueError("Split ratios must sum to 1.0, got %.8f" % total)
    train_cutoff = int(train_ratio * (2**32))
    validation_cutoff = int((train_ratio + validation_ratio) * (2**32))
    return train_cutoff, validation_cutoff


def seed_crc(seed: str) -> int:
    return zlib.crc32(seed.encode("utf-8")) & 0xFFFFFFFF


def assign_split(key: str, seed_value: int, cutoffs: Tuple[int, int]) -> str:
    value = zlib.crc32(key.encode("utf-8"), seed_value) & 0xFFFFFFFF
    if value < cutoffs[0]:
        return "train"
    if value < cutoffs[1]:
        return "validation"
    return "test"


def taxon_from_protein_id(protein_id: str) -> int:
    return int(protein_id.split(".", 1)[0])


@contextmanager
def open_text(path: Path, decompressor: str, decompressor_proc: int) -> Iterator[io.TextIOBase]:
    if path.suffix != ".gz":
        with path.open("rt", encoding="utf-8", newline="") as handle:
            yield handle
        return

    if decompressor == "pigz" and not shutil.which("pigz"):
        raise RuntimeError("Requested --decompressor pigz, but pigz is not installed")

    use_pigz = decompressor == "pigz" or (decompressor == "auto" and shutil.which("pigz"))
    use_gzip_cmd = decompressor == "gzip"

    if use_pigz:
        cmd = ["pigz", "-dc", "-p", str(max(1, decompressor_proc)), str(path)]
    elif use_gzip_cmd:
        cmd = ["gzip", "-cd", str(path)]
    else:
        with gzip.open(path, "rt", encoding="utf-8", newline="") as handle:
            yield handle
        return

    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    assert proc.stdout is not None
    handle = io.TextIOWrapper(proc.stdout, encoding="utf-8", newline="")
    try:
        yield handle
    finally:
        handle.close()
        return_code = proc.wait()
        if return_code != 0:
            raise RuntimeError("Decompressor failed with exit code %s: %s" % (return_code, " ".join(cmd)))


def iter_text_chunks(
    path: Path,
    rows_per_chunk: int,
    max_rows: Optional[int],
    decompressor: str,
    decompressor_proc: int,
) -> Iterator[Tuple[int, List[str]]]:
    with open_text(path, decompressor, decompressor_proc) as handle:
        header = next(handle, None)
        if header is None:
            return

        chunk: List[str] = []
        chunk_id = 0
        seen = 0
        for line in handle:
            if max_rows is not None and seen >= max_rows:
                break
            chunk.append(line)
            seen += 1
            if len(chunk) >= rows_per_chunk:
                yield chunk_id, chunk
                chunk_id += 1
                chunk = []

        if chunk:
            yield chunk_id, chunk


def iter_fasta_records(
    path: Path,
    max_rows: Optional[int],
    decompressor: str,
    decompressor_proc: int,
) -> Iterator[Tuple[str, str]]:
    yielded = 0
    protein_id: Optional[str] = None
    parts: List[str] = []

    with open_text(path, decompressor, decompressor_proc) as handle:
        for raw_line in handle:
            line = raw_line.strip()
            if not line:
                continue
            if line.startswith(">"):
                if protein_id is not None:
                    yield protein_id, "".join(parts)
                    yielded += 1
                    if max_rows is not None and yielded >= max_rows:
                        return
                protein_id = line[1:].split(None, 1)[0]
                parts = []
            else:
                parts.append(line)

        if protein_id is not None and (max_rows is None or yielded < max_rows):
            yield protein_id, "".join(parts)


def iter_sequence_chunks(
    path: Path,
    rows_per_chunk: int,
    max_rows: Optional[int],
    decompressor: str,
    decompressor_proc: int,
) -> Iterator[Tuple[int, List[Tuple[str, str]]]]:
    chunk: List[Tuple[str, str]] = []
    chunk_id = 0
    for record in iter_fasta_records(path, max_rows, decompressor, decompressor_proc):
        chunk.append(record)
        if len(chunk) >= rows_per_chunk:
            yield chunk_id, chunk
            chunk_id += 1
            chunk = []
    if chunk:
        yield chunk_id, chunk


def empty_buckets(table: str) -> Dict[str, List[List[object]]]:
    width = len(COLUMNS[table])
    return {
        "train": [[] for _ in range(width)],
        "validation": [[] for _ in range(width)],
        "test": [[] for _ in range(width)],
    }


def append_row(bucket: List[List[object]], values: Sequence[object]) -> None:
    for index, value in enumerate(values):
        bucket[index].append(value)


def parse_species_line(line: str) -> Tuple[str, Tuple[object, ...]]:
    fields = line.rstrip("\n").split("\t")
    if len(fields) != 5:
        raise ValueError("expected 5 fields")
    taxon_id = int(fields[0])
    return str(taxon_id), (taxon_id, fields[1], fields[2], fields[3], fields[4])


def parse_protein_info_line(line: str) -> Tuple[str, Tuple[object, ...]]:
    fields = line.rstrip("\n").split("\t", 3)
    if len(fields) != 4:
        raise ValueError("expected 4 fields")
    protein_id = fields[0]
    taxon_id = taxon_from_protein_id(protein_id)
    return protein_id, (protein_id, taxon_id, fields[1], int(fields[2]), fields[3])


def parse_alias_line(line: str) -> Tuple[str, Tuple[object, ...]]:
    fields = line.rstrip("\n").split("\t", 2)
    if len(fields) != 3:
        raise ValueError("expected 3 fields")
    protein_id = fields[0]
    taxon_id = taxon_from_protein_id(protein_id)
    key = "%s\t%s\t%s" % (protein_id, fields[1], fields[2])
    return key, (protein_id, taxon_id, fields[1], fields[2])


def parse_link_line(line: str, min_combined_score: Optional[int]) -> Optional[Tuple[str, Tuple[object, ...]]]:
    fields = line.rstrip("\n").split()
    if len(fields) != 16:
        raise ValueError("expected 16 fields")
    combined_score = int(fields[15])
    if min_combined_score is not None and combined_score < min_combined_score:
        return None
    protein1 = fields[0]
    protein2 = fields[1]
    taxon_id = taxon_from_protein_id(protein1)
    scores = tuple(int(value) for value in fields[2:15])
    key = "%s\t%s" % (protein1, protein2)
    return key, (protein1, protein2, taxon_id) + scores + (combined_score,)


def process_text_chunk(
    table: str,
    chunk_id: int,
    lines: Sequence[str],
    seed_value: int,
    cutoffs: Tuple[int, int],
    min_combined_score: Optional[int],
) -> Dict[str, object]:
    buckets = empty_buckets(table)
    bad_rows = 0
    filtered_rows = 0
    parsed_rows = 0

    for line in lines:
        if not line.strip():
            continue
        try:
            if table == "species":
                parsed = parse_species_line(line)
            elif table == "protein_info":
                parsed = parse_protein_info_line(line)
            elif table == "protein_aliases":
                parsed = parse_alias_line(line)
            elif table == "protein_links":
                parsed = parse_link_line(line, min_combined_score)
            else:
                raise ValueError("unsupported text table %s" % table)
        except Exception:
            bad_rows += 1
            continue

        if parsed is None:
            filtered_rows += 1
            continue

        key, values = parsed
        split = assign_split(key, seed_value, cutoffs)
        append_row(buckets[split], values)
        parsed_rows += 1

    return {
        "table": table,
        "chunk_id": chunk_id,
        "buckets": buckets,
        "parsed_rows": parsed_rows,
        "bad_rows": bad_rows,
        "filtered_rows": filtered_rows,
    }


def process_sequence_chunk(
    chunk_id: int,
    records: Sequence[Tuple[str, str]],
    seed_value: int,
    cutoffs: Tuple[int, int],
) -> Dict[str, object]:
    table = "protein_sequences"
    buckets = empty_buckets(table)
    bad_rows = 0
    parsed_rows = 0

    for protein_id, sequence in records:
        try:
            taxon_id = taxon_from_protein_id(protein_id)
        except Exception:
            bad_rows += 1
            continue
        values = (protein_id, taxon_id, sequence, len(sequence))
        split = assign_split(protein_id, seed_value, cutoffs)
        append_row(buckets[split], values)
        parsed_rows += 1

    return {
        "table": table,
        "chunk_id": chunk_id,
        "buckets": buckets,
        "parsed_rows": parsed_rows,
        "bad_rows": bad_rows,
        "filtered_rows": 0,
    }


def import_arrow():
    try:
        import pyarrow as pa
        import pyarrow.parquet as pq
    except ImportError as exc:
        raise SystemExit(
            "pyarrow is required. Install dependencies with: python -m pip install -r requirements.txt"
        ) from exc
    return pa, pq


def arrow_type(pa, type_name: str):
    if type_name == "string":
        return pa.string()
    if type_name == "int16":
        return pa.int16()
    if type_name == "int32":
        return pa.int32()
    raise ValueError("unsupported type %s" % type_name)


def arrow_schema(pa, table: str):
    return pa.schema(
        [pa.field(name, arrow_type(pa, type_name)) for name, type_name in zip(COLUMNS[table], TYPE_NAMES[table])]
    )


class ParquetSink:
    def __init__(self, output_dir: Path, compression: str):
        self.output_dir = output_dir
        self.compression = None if compression == "none" else compression
        self.pa, self.pq = import_arrow()
        self.schemas = {table: arrow_schema(self.pa, table) for table in TABLE_ORDER}
        self.shard_counts: Dict[str, Dict[str, int]] = {
            table: {"train": 0, "validation": 0, "test": 0} for table in TABLE_ORDER
        }
        self.row_counts: Dict[str, Dict[str, int]] = {
            table: {"train": 0, "validation": 0, "test": 0} for table in TABLE_ORDER
        }

    def write_buckets(self, table: str, buckets: Mapping[str, Sequence[Sequence[object]]]) -> None:
        table_dir = self.output_dir / table
        table_dir.mkdir(parents=True, exist_ok=True)

        for split in ("train", "validation", "test"):
            column_lists = buckets[split]
            row_count = len(column_lists[0]) if column_lists else 0
            if row_count == 0:
                continue

            data = {name: column_lists[index] for index, name in enumerate(COLUMNS[table])}
            arrow_table = self.pa.Table.from_pydict(data, schema=self.schemas[table])
            shard_id = self.shard_counts[table][split]
            output_path = table_dir / ("%s-%05d.parquet" % (split, shard_id))
            self.pq.write_table(
                arrow_table,
                output_path,
                compression=self.compression,
                use_dictionary=True,
                write_statistics=True,
            )
            self.shard_counts[table][split] += 1
            self.row_counts[table][split] += row_count


def prepare_output_dirs(output_dir: Path, tables: Sequence[str], overwrite: bool) -> None:
    output_dir.mkdir(parents=True, exist_ok=True)
    for table in tables:
        table_dir = output_dir / table
        if not table_dir.exists():
            continue
        if not overwrite:
            raise SystemExit(
                "Output directory already exists: %s. Re-run with --overwrite or choose another --output-dir."
                % table_dir
            )
        shutil.rmtree(table_dir)


def handle_result(result: Mapping[str, object], sink: ParquetSink, stats: Dict[str, Dict[str, int]]) -> None:
    table = str(result["table"])
    sink.write_buckets(table, result["buckets"])  # type: ignore[arg-type]
    stats[table]["parsed_rows"] += int(result["parsed_rows"])
    stats[table]["bad_rows"] += int(result["bad_rows"])
    stats[table]["filtered_rows"] += int(result["filtered_rows"])
    stats[table]["chunks"] += 1


def drain_one(
    pending: Set[object],
    sink: ParquetSink,
    stats: Dict[str, Dict[str, int]],
) -> Set[object]:
    done, still_pending = wait(pending, return_when=FIRST_COMPLETED)
    for future in done:
        handle_result(future.result(), sink, stats)
    return set(still_pending)


def drain_all(
    pending: Set[object],
    sink: ParquetSink,
    stats: Dict[str, Dict[str, int]],
) -> None:
    while pending:
        pending = drain_one(pending, sink, stats)


def chunk_iterator(
    table: str,
    path: Path,
    rows_per_chunk: int,
    max_rows: Optional[int],
    decompressor: str,
    decompressor_proc: int,
) -> Iterator[Tuple[int, object]]:
    if max_rows is not None and decompressor == "auto":
        decompressor = "python"
    if table == "protein_sequences":
        yield from iter_sequence_chunks(path, rows_per_chunk, max_rows, decompressor, decompressor_proc)
    else:
        yield from iter_text_chunks(path, rows_per_chunk, max_rows, decompressor, decompressor_proc)


def process_table(
    executor: ProcessPoolExecutor,
    sink: ParquetSink,
    table: str,
    raw_path: Path,
    args: argparse.Namespace,
    seed_value: int,
    cutoffs: Tuple[int, int],
    stats: Dict[str, Dict[str, int]],
) -> None:
    logging.info("Processing %s from %s", table, raw_path)
    started = time.time()
    pending: Set[object] = set()
    max_in_flight = args.max_in_flight or args.num_proc

    for chunk_id, chunk in chunk_iterator(
        table,
        raw_path,
        args.rows_per_chunk,
        args.max_rows_per_table,
        args.decompressor,
        args.decompressor_proc,
    ):
        if table == "protein_sequences":
            future = executor.submit(process_sequence_chunk, chunk_id, chunk, seed_value, cutoffs)
        else:
            future = executor.submit(
                process_text_chunk,
                table,
                chunk_id,
                chunk,
                seed_value,
                cutoffs,
                args.link_min_combined_score,
            )
        pending.add(future)

        if len(pending) >= max_in_flight:
            pending = drain_one(pending, sink, stats)

        submitted = chunk_id + 1
        if args.log_every and submitted % args.log_every == 0:
            logging.info("%s: submitted %d chunks", table, submitted)

    drain_all(pending, sink, stats)
    elapsed = time.time() - started
    logging.info(
        "Finished %s in %.1fs: %s rows, %s bad rows, %s filtered rows",
        table,
        elapsed,
        stats[table]["parsed_rows"],
        stats[table]["bad_rows"],
        stats[table]["filtered_rows"],
    )


def write_summary(
    output_dir: Path,
    tables: Sequence[str],
    args: argparse.Namespace,
    sink: ParquetSink,
    stats: Mapping[str, Mapping[str, int]],
) -> None:
    summary = {
        "raw_dir": str(args.raw_dir),
        "output_dir": str(args.output_dir),
        "tables": list(tables),
        "num_proc": args.num_proc,
        "rows_per_chunk": args.rows_per_chunk,
        "split_ratios": {
            "train": args.train_ratio,
            "validation": args.validation_ratio,
            "test": args.test_ratio,
        },
        "split_seed": args.split_seed,
        "link_min_combined_score": args.link_min_combined_score,
        "max_rows_per_table": args.max_rows_per_table,
        "compression": args.compression,
        "stats": {
            table: {
                "parser": dict(stats[table]),
                "rows": dict(sink.row_counts[table]),
                "shards": dict(sink.shard_counts[table]),
            }
            for table in tables
        },
    }
    path = output_dir / "processing_summary.json"
    path.write_text(json.dumps(summary, indent=2, sort_keys=True) + "\n", encoding="utf-8")
    logging.info("Wrote %s", path)


def main() -> int:
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s %(levelname)s %(message)s",
        datefmt="%H:%M:%S",
    )
    args = parse_args()
    tables = resolve_tables(args.tables)

    if args.num_proc < 1:
        raise SystemExit("--num-proc must be >= 1")
    if args.rows_per_chunk < 1:
        raise SystemExit("--rows-per-chunk must be >= 1")
    if args.max_in_flight is not None and args.max_in_flight < 1:
        raise SystemExit("--max-in-flight must be >= 1")

    cutoffs = split_cutoffs(args.train_ratio, args.validation_ratio, args.test_ratio)
    seed_value = seed_crc(args.split_seed)

    for table in tables:
        raw_path = args.raw_dir / RAW_FILES[table]
        if not raw_path.exists():
            raise SystemExit("Missing raw file for %s: %s" % (table, raw_path))

    prepare_output_dirs(args.output_dir, tables, args.overwrite)
    sink = ParquetSink(args.output_dir, args.compression)

    try:
        sink.pa.set_cpu_count(args.num_proc)
    except AttributeError:
        pass

    stats: Dict[str, Dict[str, int]] = {
        table: {"chunks": 0, "parsed_rows": 0, "bad_rows": 0, "filtered_rows": 0} for table in TABLE_ORDER
    }

    logging.info("Using %d worker processes", args.num_proc)
    with ProcessPoolExecutor(max_workers=args.num_proc) as executor:
        for table in tables:
            raw_path = args.raw_dir / RAW_FILES[table]
            process_table(executor, sink, table, raw_path, args, seed_value, cutoffs, stats)

    write_summary(args.output_dir, tables, args, sink, stats)
    return 0


if __name__ == "__main__":
    sys.exit(main())