| import argparse | |
| import shutil | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| from tqdm import tqdm | |
| def main(args): | |
| path = Path(args.path) | |
| root = Path(__file__).parent | |
| df_all = pd.read_csv(root / "metadata_all.csv", index_col=0) | |
| df = pd.read_csv(root / "metadata.csv", index_col=0) | |
| missing_files = np.array(list(set(df_all.index) - set(df.index))) | |
| exists = np.array([(path / f"{f}.h5ad").exists() for f in missing_files]) | |
| print(f"Found {exists.sum()} missing files out of {len(exists)}") | |
| existing_files = missing_files[exists] | |
| if args.dry_run: | |
| print(f"Files will be copied inside {root}/<species>/<tissue>") | |
| else: | |
| for name in tqdm(existing_files, desc="Copying files"): | |
| species, tissue = df_all.loc[name, ["species", "tissue"]].values | |
| src = path / f"{name}.h5ad" | |
| dst_dir: Path = root / species / tissue | |
| dst = dst_dir / f"{name}.h5ad" | |
| dst_dir.mkdir(parents=True, exist_ok=True) | |
| shutil.copyfile(src, dst) | |
| if not args.dry_run: | |
| print(f"Appending {len(existing_files)} files to metadata.csv") | |
| df_all.loc[existing_files].to_csv(root / "metadata.csv", mode="a", header=False) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| "-p", | |
| "--path", | |
| type=str, | |
| required=True, | |
| help="Path to the data directories containing h5ad files to add", | |
| ) | |
| parser.add_argument( | |
| "--dry-run", | |
| action="store_true", | |
| help="Perform a dry run without copying files", | |
| ) | |
| main(parser.parse_args()) | |