|
1 | | -import os |
2 | 1 | import argparse |
3 | | -from spot2cell import __version__ |
4 | | -from spot2cell import Spot2Cell |
| 2 | +import os |
5 | 3 | import pathlib |
| 4 | +from math import log |
6 | 5 | from pathlib import Path |
7 | | -if os.name == 'nt': |
| 6 | +from re import L |
| 7 | +from typing import Dict, List, Optional, Sequence, Tuple |
| 8 | + |
| 9 | +from spot2cell import Spot2Cell, __version__, logger |
| 10 | + |
| 11 | +if os.name == "nt": |
8 | 12 | pathlib.PosixPath = pathlib.WindowsPath |
9 | 13 |
|
10 | 14 |
|
11 | | -def get_args(): |
| 15 | +def CLI() -> argparse.ArgumentParser: |
12 | 16 | # Create an argument parser |
13 | | - parser = argparse.ArgumentParser(description='Assign spots to cells.') |
14 | | - parser.add_argument('-s', '--spots', type=str, help='Path to the spot table csv file.') |
15 | | - parser.add_argument('-m', '--mask', type=str, help='Path to the cell mask tif file.') |
16 | | - parser.add_argument('-o', '--output', type=str, help='Path to the output csv file.') |
17 | | - parser.add_argument('--version', action='version', version=f'{__version__}') |
18 | | - args = parser.parse_args() |
| 17 | + parser = argparse.ArgumentParser( |
| 18 | + description="This scripts assigns a list of spots to the objects in an instance mask." |
| 19 | + ) |
| 20 | + parser.add_argument( |
| 21 | + "-s", |
| 22 | + "--spots", |
| 23 | + type=Path, |
| 24 | + help="Path to the spot table file.", |
| 25 | + ) |
| 26 | + parser.add_argument( |
| 27 | + "-m", |
| 28 | + "--mask", |
| 29 | + type=Path, |
| 30 | + help="Path to the isntance mask file.", |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + "-x", |
| 34 | + "--x-col", |
| 35 | + type=int, |
| 36 | + default=0, |
| 37 | + help="Column index of the x coordinate in the spots table [default: 0].", |
| 38 | + ) |
| 39 | + parser.add_argument( |
| 40 | + "-y", |
| 41 | + "--y-col", |
| 42 | + type=int, |
| 43 | + default=1, |
| 44 | + help="Column index of the y coordinate in the spots table [default: 1].", |
| 45 | + ) |
| 46 | + parser.add_argument( |
| 47 | + "-o", |
| 48 | + "--output", |
| 49 | + type=Path, |
| 50 | + help="Path to the output csv file.", |
| 51 | + ) |
| 52 | + parser.add_argument("--verbose", action="store_true", help="Verbose logging.") |
| 53 | + parser.add_argument("--version", action="version", version=f"{__version__}") |
| 54 | + return parser |
| 55 | + |
| 56 | + |
| 57 | +def main(argv: Optional[Sequence[str]] = None) -> int: |
| 58 | + # Get CLI arguments |
| 59 | + parser = CLI() |
| 60 | + args = parser.parse_args(argv) |
19 | 61 |
|
20 | | - # Standardize paths |
21 | | - # Convert WindowsPath to PosixPath |
22 | | - args.spots = Path(args.spots).resolve() |
23 | | - args.mask = Path(args.mask).resolve() |
24 | | - args.output = Path(args.output).resolve() |
| 62 | + # Resolves the paths to absolute paths |
| 63 | + args.spots = args.spots.resolve() |
| 64 | + args.mask = args.mask.resolve() |
| 65 | + args.output = args.output.resolve() |
25 | 66 |
|
26 | | - return args |
| 67 | + # Set logger |
| 68 | + LOGGER = logger.set_logger(log_level="debug" if args.verbose else "info") |
| 69 | + LOGGER.info(f"spot2cell version: {__version__}") |
| 70 | + LOGGER.info(f"Reading mask from: {args.mask}") |
| 71 | + LOGGER.info(f"Reading spots from: {args.spots}") |
27 | 72 |
|
| 73 | + try: |
| 74 | + # Create an instance of the Spot2Cell class. |
| 75 | + LOGGER.info(f"Creating Spot2Cell instance") |
| 76 | + Spots = Spot2Cell( |
| 77 | + args.spots, |
| 78 | + args.mask, |
| 79 | + x_col=args.x_col, |
| 80 | + y_col=args.y_col, |
| 81 | + logger=LOGGER, |
| 82 | + ) |
28 | 83 |
|
29 | | -def main(): |
30 | | - # Get the arguments |
31 | | - args = get_args() |
| 84 | + # Save the assigned spots to a csv file. |
| 85 | + LOGGER.info(f"Writing assigned spots to: {args.output}") |
| 86 | + Spots.save(args.output) |
32 | 87 |
|
33 | | - # Create an instance of the Spot2Cell class. |
34 | | - spots = Spot2Cell(args.spots, args.mask) |
| 88 | + return 0 |
35 | 89 |
|
36 | | - # Save the assigned spots to a csv file. |
37 | | - spots.save(args.output) |
| 90 | + except Exception as e: |
| 91 | + print(f"spot2cells exit with an error: {e}") |
| 92 | + return 1 |
38 | 93 |
|
39 | 94 |
|
40 | | -if __name__ == '__main__': |
| 95 | +if __name__ == "__main__": |
41 | 96 | main() |
0 commit comments