vdjdb_structure_models / tools /verify_current_records.py
mikessh's picture
Link every structure to current VDJdb record dispositions
0c86dc6
Raw History Blame Contribute Delete
3.13 kB
"""Verify delivered metadata, current IDs, source-chunk sequences, and complete asset coverage."""
from pathlib import Path
import argparse
import hashlib
import json
import polars as pl
from vdjdb.io.chunks import read_chunk
p=argparse.ArgumentParser(description=__doc__)
p.add_argument('--out',type=Path,required=True)
p.add_argument('--structures',type=Path,required=True)
p.add_argument('--database',type=Path,required=True)
a=p.parse_args()
def read(name):
return pl.read_csv(a.out/name,separator='\t',infer_schema_length=0).fill_null('')
m=read('vdjdb_structures_metadata_current.tsv.gz')
s=pl.read_csv(a.structures/'vdjdb_structures_metadata.tsv.gz',separator='\t',infer_schema_length=0).fill_null('').with_row_index('source_row',offset=1).with_columns(pl.col('source_row').cast(pl.String))
assert m.select(s.columns).unique().sort(pl.col('source_row').cast(pl.UInt32)).equals(s), 'Original metadata changed or was lost'
l=read('record_links.tsv.gz')
assert l.select('source_row','record_id').n_unique()==l.height
assert m.filter(pl.col('record_id')!='').height==l.height
for state in ['original','repaired']:
expected=(pl.col('cdr3.alpha')==pl.col('current_'+state+'_alpha')) & (pl.col('cdr3.beta')==pl.col('current_'+state+'_beta')) & (pl.col('antigen.epitope')==pl.col('current_antigen_epitope')) & (pl.col('record_id')!='')
assert m.filter(expected != (pl.col('matches_'+state)=='true')).is_empty()
original=l.select('record_id','current_chunk_file','current_chunk_row','current_original_alpha','current_original_beta').unique()
parts=[]
for filename in original['current_chunk_file'].unique().sort():
frame=read_chunk(a.database/'chunks'/filename).select(pl.col('chunk.file').alias('current_chunk_file'),pl.col('chunk.row').cast(pl.String).alias('current_chunk_row'),pl.col('cdr3.alpha').alias('raw_alpha'),pl.col('cdr3.beta').alias('raw_beta'))
parts.append(frame)
raw=original.join(pl.concat(parts),on=['current_chunk_file','current_chunk_row'],how='left',validate='m:1')
assert raw.filter((pl.col('current_original_alpha')!=pl.col('raw_alpha')) | (pl.col('current_original_beta')!=pl.col('raw_beta')) | pl.col('raw_alpha').is_null()).is_empty(), 'Submitted sequence does not match current chunk'
d=read('source_disposition.tsv.gz');assets=read('asset_inventory.tsv.gz');mapping=read('asset_sources.tsv.gz')
assert d.height==s.height and d['source_row'].n_unique()==s.height
assert set(mapping['asset_row'])==set(assets['asset_row'])
assert set(mapping['source_row'])<=set(s['source_row'])
assert assets.select('archive','member').n_unique()==assets.height
report=json.loads((a.out/'verification.json').read_text())
for item in report['inputs']:
path=Path(item['path'])
with path.open('rb') as stream:
assert hashlib.file_digest(stream,'sha256').hexdigest()==item['sha256'], path
print(json.dumps({'source_rows_preserved':s.height,'record_links_verified':l.height,'distinct_record_ids':l['record_id'].n_unique(),'original_chunk_pairs_verified':original.height,'assets_accounted_for':assets.height,'input_digests_verified':len(report['inputs']),'status':'passed'},indent=2))