File size: 18,014 Bytes
84a242a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Validate packaged data only. Never train, dock, download, or unpickle objects.

Default exit status: 0 when available required-core data pass, 1 on corruption.
With --require-complete, exit 2 when acquisition inputs remain incomplete.
"""
import argparse
import array
import ast
import csv
import hashlib
import json
import math
from pathlib import Path
import pickletools
import struct
import sys
import zipfile

ALGORITHMS = ['surfdock', 'unimol', 'diffdock', 'diffdock_l', 'gnina', 'autodock', 'smina']


def table(path):
    with path.open(newline='', encoding='utf-8-sig') as handle:
        reader = csv.reader(handle)
        header = next(reader)
        rows = list(reader)
    assert len(header) == len(set(header)), f'Duplicate columns: {path}'
    assert all(len(row) == len(header) for row in rows), f'Ragged rows: {path}'
    return header, rows


def finite(value):
    number = float(value)
    assert math.isfinite(number), f'Nonfinite numeric value: {value}'
    return number


def score(rmsd, pb):
    z = 3.0 * (rmsd - 2.0)
    return ((1.0 + math.exp(-6.0)) / (1.0 + math.exp(z)) if z < 700 else 0.0) * (pb == 1.0)


def matching_id_column(header, rows, expected):
    candidates = [i for i in range(len(header))
                  if len({row[i] for row in rows}) == len(rows)
                  and {row[i] for row in rows} == set(expected)]
    assert len(candidates) == 1, f'Expected exactly one ID column; found {candidates}'
    return candidates[0]


def npy_metadata(path):
    with path.open('rb') as handle:
        assert handle.read(6) == b'\x93NUMPY', 'Not an NPY file'
        major, minor = handle.read(2)
        assert major in (1, 2, 3), f'Unsupported NPY version: {major}.{minor}'
        size = struct.unpack('<H' if major == 1 else '<I', handle.read(2 if major == 1 else 4))[0]
        header = ast.literal_eval(handle.read(size).decode('utf-8' if major == 3 else 'latin1'))
        dtype = header['descr']
        assert dtype in ('<f4', '<f8', '>f4', '>f8'), f'Unsupported numeric dtype: {dtype}'
        values = array.array('f' if dtype.endswith('4') else 'd')
        values.frombytes(handle.read())
        if (dtype[0] == '<') != (sys.byteorder == 'little'):
            values.byteswap()
        assert len(values) == math.prod(header['shape']), 'NPY payload size mismatch'
        assert all(math.isfinite(x) for x in values), 'NPY contains nonfinite values'
        return header


def validate(root):
    report = {'scope': 'file integrity and existing-data consistency; no scientific experiments',
              'checks': {}, 'errors': [], 'warnings': [], 'missing_acquisition_inputs': []}
    checks = report['checks']
    try:
        matrices = {}
        ids = None
        for stem, suffix in [('rmsd', '_rmsd'), ('pb_ratio', '_pb_ratio'), ('runtime', '_runtime')]:
            path = root / 'data/raw' / f'moad_{stem}_trimmed.csv'
            h, rows = table(path)
            assert h == ['PDB_CCD_ID'] + [a + suffix for a in ALGORITHMS], f'Unexpected schema: {path}'
            current = [row[0] for row in rows]
            assert len(current) == 3179 and len(set(current)) == 3179, f'Invalid ID coverage: {path}'
            if ids is not None:
                assert ids == current, f'Row order differs: {path}'
            ids = current
            vals = [[finite(x) for x in row[1:]] for row in rows]
            assert all(x >= 0 for row in vals for x in row), f'Negative value: {path}'
            if stem == 'runtime':
                assert all(x > 0 for row in vals for x in row), 'Runtime must be positive'
            if stem == 'pb_ratio':
                assert all(x <= 1 for row in vals for x in row), 'PB ratio exceeds 1'
            matrices[stem] = vals
            checks[stem] = {'rows': len(rows), 'algorithm_columns': 7, 'finite_values': 22253,
                            'minimum': min(map(min, vals)), 'maximum': max(map(max, vals))}
            if stem == 'rmsd' and max(map(max, vals)) > 1000:
                extreme_count = sum(x > 1000 for row in vals for x in row)
                checks['rmsd']['values_above_1000_angstrom'] = extreme_count
                report['warnings'].append(f'Raw RMSD contains {extreme_count} values above 1000 Angstrom; preserved unchanged. Failure/sentinel semantics are not established.')
        h, rows = table(root / 'data/ordered_complex_ids.csv')
        assert h == ['PDB_CCD_ID'] and [r[0] for r in rows] == ids, 'Raw ID index mismatch'
        checks['raw_matrix_id_order'] = 'identical across all three matrices and raw ID index'
        target = root / 'data/targets/strict_score_matrix.csv'
        h, rows = table(target)
        assert h == ['PDB_CCD_ID'] + ALGORITHMS and [r[0] for r in rows] == ids, 'Target alignment mismatch'
        max_error = 0.0
        for i, row in enumerate(rows):
            for j, value in enumerate(row[1:]):
                observed = finite(value)
                assert 0 <= observed <= 1, 'Strict score outside [0,1]'
                max_error = max(max_error, abs(observed - score(matrices['rmsd'][i][j], matrices['pb_ratio'][i][j])))
        assert max_error <= 1e-12, f'Strict-score formula mismatch: {max_error}'
        checks['strict_score'] = {'maximum_absolute_formula_error': max_error, 'tolerance': 1e-12,
                                 'lambda': 3.0, 'pb_valid': 'pb_ratio == 1.0'}
        original_target = root / 'data/targets/full_strict_score_matrix.csv'
        if original_target.exists():
            h, rows = table(original_target)
            assert h == ['PDB_CCD_ID'] + [a + '_strict_score' for a in ALGORITHMS], 'Original target schema mismatch'
            assert [r[0] for r in rows] == ids, 'Original target ID order mismatch'
            max_error = max(abs(finite(row[j + 1]) - score(matrices['rmsd'][i][j], matrices['pb_ratio'][i][j]))
                            for i, row in enumerate(rows) for j in range(7))
            assert max_error <= 1e-12, f'Original target formula mismatch: {max_error}'
            checks['original_strict_score'] = {'maximum_absolute_formula_error': max_error, 'tolerance': 1e-12}
        native_target = root / 'data/targets/moad_score_native_reconstructed.csv'
        if native_target.exists():
            h, rows = table(native_target)
            assert h == ['PDB_CCD_ID'] + ALGORITHMS and [r[0] for r in rows] == ids, 'Native evaluation target alignment mismatch'
            max_error = 0.0
            for i, row in enumerate(rows):
                for j in range(7):
                    rmsd = matrices['rmsd'][i][j]
                    expected = 0.0 if rmsd > 5 or rmsd < 0 else score(rmsd, matrices['pb_ratio'][i][j])
                    max_error = max(max_error, abs(finite(row[j + 1]) - expected))
            assert max_error <= 1e-12, f'Native evaluation formula mismatch: {max_error}'
            native_provenance = json.loads((root / 'data/provenance/native_evaluation_target.json').read_text())
            assert hashlib.sha256(native_target.read_bytes()).hexdigest() == native_provenance['sha256'], 'Native target provenance hash mismatch'
            validation = native_provenance['saved_downstream_validation']
            assert validation['complete_unique_complexes'] == 3179 and validation['engine_score_values_checked'] == 22253 and validation['mismatches'] == 0
            checks['native_evaluation_target'] = {'status': 'reconstructed, not recovered original CSV',
                                                  'maximum_absolute_formula_error': max_error,
                                                  'saved_engine_values_independently_checked': 22253,
                                                  'maximum_saved_engine_value_difference': validation['maximum_absolute_difference'],
                                                  'distinct_from_acquisition_target': True}

        original_split = root / 'data/splits/moad_5fold_split_seed42.csv'
        recovered_split = root / 'data/splits/recovered_heldout_folds_from_downstream.csv'
        memberships = {}
        for path in [original_split, recovered_split]:
            if not path.exists():
                continue
            h, rows = table(path)
            id_col = matching_id_column(h, rows, ids)
            fold_cols = [i for i, name in enumerate(h) if 'fold' in name.lower() and i != id_col]
            assert len(fold_cols) == 1, f'Ambiguous fold column: {path}'
            col = fold_cols[0]
            raw_folds = [finite(row[col]) for row in rows]
            assert all(v == int(v) for v in raw_folds), 'Noninteger fold label'
            offset = 1 if set(raw_folds) == {1, 2, 3, 4, 5} else 0
            assignment = {row[id_col]: int(v) - offset for row, v in zip(rows, raw_folds)}
            assert set(assignment.values()) == set(range(5)), 'Expected five folds'
            counts = {str(k): sum(v == k for v in assignment.values()) for k in range(5)}
            assert sorted(counts.values()) == [635, 636, 636, 636, 636], 'Unexpected fold sizes'
            memberships[path.name] = assignment
            checks[path.name] = {'id_column': h[id_col], 'fold_column': h[col], 'counts': counts,
                                 'normalization_offset': offset}
        if len(memberships) == 2:
            assert memberships[original_split.name] == memberships[recovered_split.name], 'Original/recovered fold mismatch'
            checks['original_recovered_folds_agree'] = True
        if not original_split.exists():
            report['warnings'].append('Original seed42 split file unavailable; recovered file records existing downstream membership, not a new random split.')
        if not memberships:
            report['missing_acquisition_inputs'].append('original or validated recovered outer-fold assignments')

        descriptor = root / 'data/descriptors/moad_physical_descriptors_filtered.csv'
        if descriptor.exists():
            h, rows = table(descriptor)
            id_col = matching_id_column(h, rows, ids)
            numeric = []
            for j in range(len(h)):
                if j == id_col:
                    continue
                try:
                    [finite(row[j]) for row in rows]
                    numeric.append(h[j])
                except (ValueError, AssertionError):
                    pass
            assert len(numeric) == 4, f'Expected four finite numeric descriptors, found {numeric}'
            checks['physical_descriptors'] = {'id_column': h[id_col], 'numeric_columns': numeric, 'rows': len(rows)}
        else:
            report['missing_acquisition_inputs'].append(str(descriptor.relative_to(root)))

        feature = root / 'data/features/complex_features_ligand_protein_d256_seed0.npy'
        feature_ids = root / 'data/features/complex_feature_ids.csv'
        if feature.exists():
            metadata = npy_metadata(feature)
            assert tuple(metadata['shape']) == (3179, 256), 'Combined feature shape mismatch'
            checks['combined_feature_numeric_payload'] = metadata
        else:
            report['missing_acquisition_inputs'].append(str(feature.relative_to(root)))
        if feature_ids.exists():
            h, rows = table(feature_ids)
            assert len(h) == 1 and [x[0] for x in rows] == ids, 'Combined feature ID order mismatch'
            provenance = root / 'data/provenance/combined_feature_alignment.json'
            assert provenance.exists(), 'Producer-verified combined-feature alignment evidence is required'
            evidence = json.loads(provenance.read_text())
            assert evidence.get('producer_verified') is True and evidence.get('evidence'), 'Incomplete feature alignment evidence'
            assert hashlib.sha256(feature.read_bytes()).hexdigest() == evidence['cache_sha256'], 'Feature alignment evidence refers to a different cache'
            assert hashlib.sha256(feature_ids.read_bytes()).hexdigest() == evidence['feature_ids_sha256'], 'Feature alignment evidence refers to different IDs'
            comparison = json.loads((root / evidence['evidence']).read_text())
            assert comparison['passed'] and comparison['matching_rows'] == 3179 and comparison['matching_values'] == 813824, 'Incomplete cache-value comparison'
            checks['combined_feature_ids'] = {'ordered_ids': 3179, 'independently_compared_cache_values': 813824,
                                             'maximum_absolute_difference': comparison['maximum_absolute_difference'],
                                             'absolute_tolerance': comparison['absolute_tolerance'],
                                             'relative_tolerance': comparison['relative_tolerance']}
        else:
            report['missing_acquisition_inputs'].append('producer-verified data/features/complex_feature_ids.csv and alignment evidence')

        ligand = root / 'data/features/ligand_chemberta_moad3179.pt'
        if ligand.exists():
            with zipfile.ZipFile(ligand) as archive:
                prefix = archive.namelist()[0].split('/')[0]
                ops = pickletools.genops(archive.read(prefix + '/data.pkl'))
                strings = [arg for op, arg, _ in ops if op.name in ('BINUNICODE', 'SHORT_BINUNICODE', 'UNICODE')]
                id_set = set(ids)
                embedded_ids = [s for s in strings if s in id_set]
                assert embedded_ids == ids, 'Embedded ligand IDs do not match raw ID order'
                values = array.array('f')
                values.frombytes(archive.read(prefix + '/data/0'))
                if archive.read(prefix + '/byteorder').decode().strip() != sys.byteorder:
                    values.byteswap()
                assert len(values) == 3179 * 384 and all(math.isfinite(x) for x in values), 'Ligand numeric payload invalid'
            h, rows = table(root / 'data/features/ligand_feature_ids.csv')
            assert h == ['PDB_CCD_ID'] and [x[0] for x in rows] == embedded_ids, 'Ligand index mismatch'
            checks['ligand_embeddings'] = {'embedded_ordered_ids': 3179, 'finite_float_values': len(values),
                                          'inspection': 'ZIP/pickle opcodes only; no object deserialization'}

        graph_dir = root / 'data/external/protein_graphs_esmc_600m'
        if graph_dir.exists():
            files = list(graph_dir.glob('pyg_graph_*_esmc_600m.pt'))
            graph_ids = [p.name.removeprefix('pyg_graph_').removesuffix('_esmc_600m.pt') for p in files]
            assert len(files) == 3179 and set(graph_ids) == set(ids), 'External protein graph ID coverage mismatch'
            stats = [p.stat() for p in files]
            placeholders = sum(bool(getattr(s, 'st_flags', 0) & 0x40000000) for s in stats)
            assert placeholders == 0, 'External protein graph directory contains cloud placeholders'
            checks['external_protein_graphs'] = {'files': len(files), 'logical_bytes': sum(s.st_size for s in stats),
                                               'cloud_placeholders': placeholders,
                                               'check_scope': 'filenames and filesystem metadata, not payload deserialization'}

        historical_audit = root / 'data/provenance/original_input_audit.json'
        if historical_audit.exists():
            audit = json.loads(historical_audit.read_text())
            current_files = {p.name: p for p in [*(root / 'data/raw').glob('*.csv'), original_split,
                                                descriptor, feature] if p.exists()}
            matched = 0
            for original_path, expected_hash in audit['sha256'].items():
                filename = Path(original_path).name
                assert filename in current_files, f'Historical audited input missing: {filename}'
                assert hashlib.sha256(current_files[filename].read_bytes()).hexdigest() == expected_hash, f'Historical input hash mismatch: {filename}'
                matched += 1
            checks['original_input_audit_hashes'] = {'matching_inputs': matched,
                                                    'note': 'Comparison to preserved historical hashes; historical experiments were not rerun.'}

        checksum_file = root / 'data/CHECKSUMS.sha256'
        if checksum_file.exists():
            count = 0
            for line in checksum_file.read_text().splitlines():
                digest, rel = line.split('  ', 1)
                path = root / rel
                assert path.is_relative_to(root) and path.is_file(), f'Missing checksum target: {rel}'
                assert hashlib.sha256(path.read_bytes()).hexdigest() == digest, f'Checksum mismatch: {rel}'
                count += 1
            checks['checksums'] = {'verified_files': count}
    except (AssertionError, ValueError, KeyError, OSError, csv.Error, zipfile.BadZipFile) as error:
        report['errors'].append(str(error))
    report['available_data_integrity_passed'] = not report['errors']
    report['acquisition_inputs_complete'] = not report['errors'] and not report['missing_acquisition_inputs']
    report['full_downstream_reproduction_verified'] = False
    report['license_clearance_verified'] = False
    return report


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--root', type=Path, default=Path(__file__).resolve().parents[1])
    parser.add_argument('--report', type=Path, help='Optional path for a JSON integrity report')
    parser.add_argument('--require-complete', action='store_true')
    args = parser.parse_args()
    result = validate(args.root.resolve())
    text = json.dumps(result, indent=2, allow_nan=False) + '\n'
    print(text, end='')
    if args.report:
        args.report.parent.mkdir(parents=True, exist_ok=True)
        args.report.write_text(text)
    if result['errors']:
        return 1
    if args.require_complete and not result['acquisition_inputs_complete']:
        return 2
    return 0


if __name__ == '__main__':
    raise SystemExit(main())