annotate qupath_roi_splitter.py @ 1:064b53fd3131 draft

planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e08a178a7c35c0673f6bfc163614a8fc730ffc6c
author galaxyp
date Wed, 14 Jun 2023 16:37:01 +0000
parents b5e9cebb27e3
children 7bee859bbd11
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
9 def draw_poly(input_df, input_img, col=(0, 0, 0)):
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
10 s = np.array(input_df)
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
11 output_img = cv2.fillPoly(input_img, pts=np.int32([s]), color=col)
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
12 return output_img
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
13
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
14
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
15 def split_qupath_roi(in_roi):
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
16 with open(in_roi) as file:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
17 qupath_roi = geojson.load(file)
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
18
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
19 # HE dimensions
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
20 dim_plt = [qupath_roi["dim"]["width"], qupath_roi["dim"]["height"]]
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
21
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
22 tma_name = qupath_roi["name"]
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
23 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
24
1
064b53fd3131 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e08a178a7c35c0673f6bfc163614a8fc730ffc6c
galaxyp
parents: 0
diff changeset
25 for cell_type in cell_types:
064b53fd3131 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e08a178a7c35c0673f6bfc163614a8fc730ffc6c
galaxyp
parents: 0
diff changeset
26 # create numpy array with white background
064b53fd3131 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e08a178a7c35c0673f6bfc163614a8fc730ffc6c
galaxyp
parents: 0
diff changeset
27 img = np.zeros((dim_plt[1], dim_plt[0], 3), dtype="uint8")
064b53fd3131 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit e08a178a7c35c0673f6bfc163614a8fc730ffc6c
galaxyp
parents: 0
diff changeset
28 img.fill(255)
0
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
29
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
30 for i, roi in enumerate(qupath_roi["features"]):
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
31 if roi["properties"]["classification"]["name"] == cell_type:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
32 if len(roi["geometry"]["coordinates"]) == 1:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
33 # Polygon w/o holes
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
34 img = draw_poly(roi["geometry"]["coordinates"][0], img)
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
35 else:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
36 first_roi = True
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
37 for sub_roi in roi["geometry"]["coordinates"]:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
38 # Polygon with holes
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
39 if not isinstance(sub_roi[0][0], list):
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
40 if first_roi:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
41 img = draw_poly(sub_roi, img)
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
42 first_roi = False
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
43 else:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
44 # holes in ROI
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
45 img = draw_poly(sub_roi, img, col=(255, 255, 255))
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
46 else:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
47 # MultiPolygon with holes
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
48 for sub_coord in sub_roi:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
49 if first_roi:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
50 img = draw_poly(sub_coord, img)
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
51 first_roi = False
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
52 else:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
53 # holes in ROI
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
54 img = draw_poly(sub_coord, img, col=(255, 255, 255))
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
55
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
56 # get all black pixel
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
57 coords_arr = np.column_stack(np.where(img == (0, 0, 0)))
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
58
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
59 # remove duplicated rows
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
60 coords_arr_xy = coords_arr[coords_arr[:, 2] == 0]
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
61
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
62 # remove last column
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
63 coords_arr_xy = np.delete(coords_arr_xy, 2, axis=1)
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
64
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
65 # to pandas and rename columns to x and y
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
66 coords_df = pd.DataFrame(coords_arr_xy, columns=['x', 'y'])
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
67
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
68 # drop duplicates
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
69 coords_df = coords_df.drop_duplicates(
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
70 subset=['x', 'y'],
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
71 keep='last').reset_index(drop=True)
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
72
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
73 coords_df.to_csv("{}_{}.txt".format(tma_name, cell_type), sep='\t', index=False)
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
74
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
75
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
76 if __name__ == "__main__":
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
77 parser = argparse.ArgumentParser(description="Split ROI coordinates of QuPath TMA annotation by cell type (classfication)")
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
78 parser.add_argument("--qupath_roi", default=False, help="Input QuPath annotation (GeoJSON file)")
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
79 parser.add_argument('--version', action='version', version='%(prog)s 0.1.0')
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
80 args = parser.parse_args()
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
81
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
82 if args.qupath_roi:
b5e9cebb27e3 planemo upload for repository hhttps://github.com/npinter/ROIsplitter commit cdf3e9652b10c7a0b179202129a797e32fd95909
galaxyp
parents:
diff changeset
83 split_qupath_roi(args.qupath_roi)