[docs]deffinalize_matrix(args_json:str):"""Finalize the matrix after all array jobs complete."""args=json.loads(args_json)# Verify all tasks completedcompleted_dir=Path(args["temp_dir"])/"completed"expected_tasks=len(args["files"])completed_tasks=len(list(completed_dir.glob("task_*.json")))ifcompleted_tasks!=expected_tasks:raiseRuntimeError(f"Expected {expected_tasks} tasks but only {completed_tasks} completed")# Optimize the TileDB arraymatrix_uri=f"{args['output_dir']}/assays/{args['matrix_options']['matrix_name']}"optimize_tiledb_array(matrix_uri)# Save completion metadatawithopen(f"{args['temp_dir']}/matrix_metadata.json","w")asf:json.dump({"matrix_name":args["matrix_options"]["matrix_name"],"files_processed":len(args["files"]),"uri":matrix_uri,},f,)