Mercurial > repos > galaxyp > qupath_roi_splitter
annotate qupath_roi_splitter.py @ 5:17c54a716a5b draft
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
author | galaxyp |
---|---|
date | Tue, 30 Jul 2024 12:59:11 +0000 |
parents | 9f136ebf73ac |
children | 6a8cf86fd3b7 |
rev | line source |
---|---|
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
1 import argparse |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
2 |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
3 import cv2 |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
4 import geojson |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
5 import numpy as np |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
6 import pandas as pd |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
7 |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
8 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
9 def collect_coords(input_coords, feature_index, coord_index=0): |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
10 coords_with_index = [] |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
11 for coord in input_coords: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
12 coords_with_index.append((coord[0], coord[1], feature_index, coord_index)) |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
13 coord_index += 1 |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
14 return coords_with_index |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
15 |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
16 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
17 def collect_roi_coords(input_roi, feature_index): |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
18 all_coords = [] |
2
7bee859bbd11
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 7391296e0c7c8d48b42a129d154b50b29fd41737
galaxyp
parents:
1
diff
changeset
|
19 if len(input_roi["geometry"]["coordinates"]) == 1: |
7bee859bbd11
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 7391296e0c7c8d48b42a129d154b50b29fd41737
galaxyp
parents:
1
diff
changeset
|
20 # Polygon w/o holes |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
21 all_coords.extend(collect_coords(input_roi["geometry"]["coordinates"][0], feature_index)) |
2
7bee859bbd11
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 7391296e0c7c8d48b42a129d154b50b29fd41737
galaxyp
parents:
1
diff
changeset
|
22 else: |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
23 coord_index = 0 |
2
7bee859bbd11
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 7391296e0c7c8d48b42a129d154b50b29fd41737
galaxyp
parents:
1
diff
changeset
|
24 for sub_roi in input_roi["geometry"]["coordinates"]: |
5
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
25 if len(sub_roi) == 2: |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
26 # Special case: LMD data |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
27 all_coords.extend(collect_coords([sub_roi], feature_index, coord_index)) |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
28 coord_index += 1 |
2
7bee859bbd11
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 7391296e0c7c8d48b42a129d154b50b29fd41737
galaxyp
parents:
1
diff
changeset
|
29 else: |
5
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
30 # Polygon with holes or MultiPolygon |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
31 if not isinstance(sub_roi[0][0], list): |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
32 all_coords.extend(collect_coords(sub_roi, feature_index, coord_index)) |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
33 coord_index += len(sub_roi) |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
34 else: |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
35 # MultiPolygon with holes |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
36 for sub_coord in sub_roi: |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
37 all_coords.extend(collect_coords(sub_coord, feature_index, coord_index)) |
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
38 coord_index += len(sub_coord) |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
39 return all_coords |
2
7bee859bbd11
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 7391296e0c7c8d48b42a129d154b50b29fd41737
galaxyp
parents:
1
diff
changeset
|
40 |
7bee859bbd11
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 7391296e0c7c8d48b42a129d154b50b29fd41737
galaxyp
parents:
1
diff
changeset
|
41 |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
42 def split_qupath_roi(in_roi): |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
43 with open(in_roi) as file: |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
44 qupath_roi = geojson.load(file) |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
45 |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
46 # HE dimensions |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
47 dim_plt = [int(qupath_roi["dim"]["width"]), int(qupath_roi["dim"]["height"])] |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
48 |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
49 tma_name = qupath_roi["name"] |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
50 cell_types = [ct.rsplit(" - ", 1)[-1] for ct in qupath_roi["featureNames"]] |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
51 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
52 coords_by_cell_type = {ct: [] for ct in cell_types} |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
53 coords_by_cell_type['all'] = [] # For storing all coordinates if args.all is True |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
54 |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
55 for feature_index, roi in enumerate(qupath_roi["features"]): |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
56 feature_coords = collect_roi_coords(roi, feature_index) |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
57 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
58 if args.all: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
59 coords_by_cell_type['all'].extend(feature_coords) |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
60 elif "classification" in roi["properties"]: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
61 cell_type = roi["properties"]["classification"]["name"] |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
62 if cell_type in cell_types: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
63 coords_by_cell_type[cell_type].extend(feature_coords) |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
64 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
65 for cell_type, coords in coords_by_cell_type.items(): |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
66 if coords: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
67 # Generate image (white background) |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
68 img = np.ones((dim_plt[1], dim_plt[0]), dtype="uint8") * 255 |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
69 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
70 # Convert to numpy array and ensure integer coordinates |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
71 coords_arr = np.array(coords).astype(int) |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
72 |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
73 # Sort by feature_index first, then by coord_index |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
74 coords_arr = coords_arr[np.lexsort((coords_arr[:, 3], coords_arr[:, 2]))] |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
75 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
76 # Get filled pixel coordinates |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
77 if args.fill: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
78 filled_coords = np.column_stack(np.where(img == 0)) |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
79 all_coords = np.unique(np.vstack((coords_arr[:, :2], filled_coords[:, ::-1])), axis=0) |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
80 else: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
81 all_coords = coords_arr[:, :2] |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
82 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
83 # Save all coordinates to CSV |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
84 coords_df = pd.DataFrame(all_coords, columns=['x', 'y'], dtype=int) |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
85 coords_df.to_csv("{}_{}.txt".format(tma_name, cell_type), sep='\t', index=False) |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
86 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
87 # Generate image for visualization if --img is specified |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
88 if args.img: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
89 # Group coordinates by feature_index |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
90 features = {} |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
91 for x, y, feature_index, coord_index in coords_arr: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
92 if feature_index not in features: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
93 features[feature_index] = [] |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
94 features[feature_index].append((x, y)) |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
95 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
96 # Draw each feature separately |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
97 for feature_coords in features.values(): |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
98 pts = np.array(feature_coords, dtype=np.int32) |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
99 if args.fill: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
100 cv2.fillPoly(img, [pts], color=0) # Black fill |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
101 else: |
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
102 cv2.polylines(img, [pts], isClosed=True, color=0, thickness=1) # Black outline |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
103 |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
104 cv2.imwrite("{}_{}.png".format(tma_name, cell_type), img) |
3
24ccdcfbabac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 00029e8a3ee400f69a6dbe9e556ec9c27c6979cb
galaxyp
parents:
2
diff
changeset
|
105 |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
106 |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
107 if __name__ == "__main__": |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
108 parser = argparse.ArgumentParser(description="Split ROI coordinates of QuPath TMA annotation by cell type (classification)") |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
109 parser.add_argument("--qupath_roi", default=False, help="Input QuPath annotation (GeoJSON file)") |
4
9f136ebf73ac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 918ae25f84e7042ed36461219ff068633c1c2427
galaxyp
parents:
3
diff
changeset
|
110 parser.add_argument("--fill", action="store_true", required=False, help="Fill pixels in ROIs (order of coordinates will be lost)") |
5
17c54a716a5b
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e3d24f3dc7a9812f8fa4447c9391e5bd3b5d4dc6
galaxyp
parents:
4
diff
changeset
|
111 parser.add_argument('--version', action='version', version='%(prog)s 0.3.1') |
2
7bee859bbd11
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 7391296e0c7c8d48b42a129d154b50b29fd41737
galaxyp
parents:
1
diff
changeset
|
112 parser.add_argument("--all", action="store_true", required=False, help="Extracts all ROIs") |
3
24ccdcfbabac
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit 00029e8a3ee400f69a6dbe9e556ec9c27c6979cb
galaxyp
parents:
2
diff
changeset
|
113 parser.add_argument("--img", action="store_true", required=False, help="Generates image of ROIs") |
0
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
114 args = parser.parse_args() |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
115 |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
116 if args.qupath_roi: |
b5e9cebb27e3
planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff
changeset
|
117 split_qupath_roi(args.qupath_roi) |