"""Build the example PubChem CID 702 SDF package for local installation.""" from argparse import ArgumentParser from base64 import b64encode from configparser import ConfigParser from datetime import datetime, timezone from hashlib import sha256 from io import StringIO from pathlib import Path import sys from zipfile import ZipFile, ZIP_DEFLATED cli = ArgumentParser(description="Prepare a managed exemplar archive from the genuine PubChem 3D ethanol SDF") cli.add_argument("--framework", type=Path, required=True, help="GX-TXT installation root") cli.add_argument("sdf", type=Path, help="Unmodified PubChem 3D SDF for CID 702") cli.add_argument("output", type=Path, help="New scientific Library ZIP") args = cli.parse_args() framework = args.framework.resolve(strict=True) sys.path.insert(0,str(framework/"core/python")) from scientific_library_graph import validate_entry from scientific_library_records import canonical, entry_hash def write_zip(identity, kind, provider, label, source, license, payload): ini=ConfigParser(interpolation=None) ini["library"]=dict(library_id=identity,library_type=kind,version="1.0", provider_id=provider,format_version="1",display_name=label, description="Local verification example",license=license,origin=source) stream=StringIO() ini.write(stream) output=args.output with ZipFile(output,"w",ZIP_DEFLATED) as z: z.writestr("library.ini",stream.getvalue()) z.writestr("payload/library.gxsrl",payload) z.writestr("SHA256SUMS.txt",sha256(payload).hexdigest()+" payload/library.gxsrl\n") print(output) def molecule(): path=args.sdf.resolve(strict=True) with path.open("rb") as stream: title = stream.readline().strip() if path.suffix.lower() != ".sdf" or title != b"702": raise ValueError("This example requires the unmodified PubChem SDF for CID 702") raw=path.read_bytes() kind="molecular.structures" source="https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/702/SDF?record_type=3d" entry=dict(entry_id="etanolo-pug-702",record_type="chemistry.structure",revision="1", label="Etanolo CID 702 — SDF locale NIH PubChem",content_contract="molecular.structure", fields=dict(identifiers={"cid":"702"},geometry_origin="computed_3d", coordinate_unit="angstrom",dimensionality="3d",validated_atoms=9, validated_bonds=8), provenance=dict(source_id="pubchem.pug_rest",source_url=source, retrieved_at=datetime.now(timezone.utc).isoformat()), resources=[dict(role="molecular_structure",availability="local",original_filename=path.name, media_type="chemical/x-mdl-sdfile",sha256=sha256(raw).hexdigest(), data_base64=b64encode(raw).decode("ascii"))], relations=[]) declaration=validate_entry(entry,kind) entry["domain_schema"]=dict(provider_id=declaration["provider_id"], provider_version=declaration["provider_version"],sha256=declaration["schema_sha256"]) entry["content_sha256"]=entry_hash(entry) payload=canonical(dict(schema="gxtxt.library_records.v1",library_type=kind, entries={entry["entry_id"]:entry},history=[])) write_zip("guida-local-sdf-cid702","molecular.structures","library.molecular.structures", "Etanolo — conformero SDF locale",source,"As stated by original source",payload) if __name__ == "__main__": molecule()