File size: 3,111 Bytes
191759a | 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 | """Check every declared PDB aggregate accession against deposited assets and current IDs."""
from pathlib import Path
import argparse,gzip,hashlib,json,re,tarfile
import polars as pl
from vdjdb.io.chunks import read_chunk
p=argparse.ArgumentParser(description=__doc__);p.add_argument('--structures',type=Path,required=True);p.add_argument('--tables',type=Path,required=True);p.add_argument('--database',type=Path,required=True);p.add_argument('--out',type=Path,required=True);a=p.parse_args();a.out.mkdir(exist_ok=True,parents=True)
chunk=read_chunk(a.database/'chunks/PDB_Database.tsv');records=pl.read_parquet(a.tables/'records.parquet')
existing={}
with tarfile.open(a.structures/'data/pdb_files_native.tgz','r|gz') as archive:
for member in archive:
match=re.fullmatch(r'(?:aligned_)*([0-9a-zA-Z]{4})\.pdb',Path(member.name).name)
if member.isfile() and match:existing[match[1].lower()]=member.name
raw=pl.read_csv(a.structures/'data/pdb_sources/manifest.tsv.gz',separator='\t',infer_schema_length=0).fill_null('');downloaded={r['pdb_id']:r for r in raw.to_dicts()}
for source in downloaded.values():
path=a.structures/source['file']
with gzip.open(path,'rb') as stream:assert hashlib.file_digest(stream,'sha256').hexdigest()==source['source_sha256']
rows=[]
ids=sorted({id.lower() for text in chunk['meta.structure.id'] for id in re.findall(r'\b[0-9][A-Za-z0-9]{3}\b',text)})
for id in ids:
matches=records.filter(pl.col('meta_structure_id').str.to_lowercase()==id)
assert matches.height>0,(id,'No current record ID')
assert id in existing or id in downloaded,(id,'No native structure')
for record in matches.select('record_id','reference_id','chunk_file','chunk_row').to_dicts():
rows.append({'pdb_id':id,**record,'archive':'data/pdb_files_native.tgz' if id in existing else '',
'member':existing.get(id,''),'deposited_source_file':downloaded.get(id,{}).get('file',''),
'deposited_source_sha256':downloaded.get(id,{}).get('source_sha256',''),
'coordinate_frame':'canonical_and_deposited' if id in existing and id in downloaded else 'canonical' if id in existing else 'deposited',
'source_url':f'https://www.rcsb.org/structure/{id.upper()}'})
for name,frame in [('pdb_coverage_current.tsv.gz',pl.DataFrame(rows)),('pdb_rows_without_accession.tsv.gz',chunk.filter(pl.col('meta.structure.id')=='').select('chunk.file','chunk.row','reference.id','method.identification','method.verification'))]:
with (a.out/name).open('wb') as stream,gzip.GzipFile(filename='',mode='wb',fileobj=stream,mtime=0) as gz:gz.write(frame.write_csv(separator='\t').encode())
report={'source_chunk_rows':chunk.height,'declared_pdb_accessions':len(ids),'in_historical_native_archive':len(set(ids)&set(existing)),
'newly_available_from_pdb':len(set(ids)-set(existing)),'unavailable_declared_accessions':0,
'source_rows_without_declared_accession':chunk.filter(pl.col('meta.structure.id')=='').height,
'accession_record_links':len(rows),'complete_raw_source_files_verified':raw.height}
(a.out/'pdb_coverage_verification.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report,indent=2))
|