File size: 1,660 Bytes
1734776
 
 
 
 
 
61efa61
1734776
 
 
 
f24cbed
1734776
f24cbed
 
1734776
 
 
 
 
 
 
 
c451443
f24cbed
c451443
61efa61
c451443
1734776
c451443
f24cbed
c451443
1734776
c451443
1734776
c451443
1734776
c451443
 
f24cbed
1734776
 
 
 
 
 
 
 
 
 
 
 
c451443
 
 
 
 
 
1734776
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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())