Mercurial > repos > imgteam > imagej2_find_maxima
annotate imagej2_analyze_particles_binary_jython_script.py @ 2:6a07f71806bb draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
author | imgteam |
---|---|
date | Sun, 05 Nov 2023 14:19:21 +0000 |
parents | e2622ebb5ce4 |
children | 4409f9eb9433 |
rev | line source |
---|---|
0
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
1 import sys |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
2 |
0
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
3 from ij import IJ |
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
4 from ij.plugin.filter import Analyzer |
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
5 |
2
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
6 OPTIONS = ["edm=Overwrite", "iterations=1", "count=1"] |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
7 |
0
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
8 # Fiji Jython interpreter implements Python 2.5 which does not |
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
9 # provide support for argparse. |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
10 error_log = sys.argv[-14] |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
11 input_file = sys.argv[-13] |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
12 black_background = sys.argv[-12] == "yes" |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
13 size = sys.argv[-11] |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
14 circularity_min = float(sys.argv[-10]) |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
15 circularity_max = float(sys.argv[-9]) |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
16 show = sys.argv[-8] |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
17 display_results = sys.argv[-7] == "yes" |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
18 all_results = sys.argv[-6] == "yes" |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
19 exclude_edges = sys.argv[-5] == "yes" |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
20 include_holes = sys.argv[-4] == "yes" |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
21 output_filename = sys.argv[-3] |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
22 output_datatype = sys.argv[-2] |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
23 results_path = sys.argv[-1] |
0
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
24 |
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
25 # Open the input image file. |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
26 input_image_plus = IJ.openImage(input_file) |
0
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
27 |
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
28 # Create a copy of the image. |
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
29 input_image_plus_copy = input_image_plus.duplicate() |
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
30 image_processor_copy = input_image_plus_copy.getProcessor() |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
31 analyzer = Analyzer(input_image_plus_copy) |
0
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
32 |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
33 # Set binary options. |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
34 options_list = OPTIONS |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
35 if black_background: |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
36 options_list.append("black") |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
37 options = " ".join(options_list) |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
38 IJ.run(input_image_plus_copy, "Options...", options) |
0
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
39 |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
40 if not image_processor_copy.isBinary(): |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
41 # Convert the image to binary grayscale. |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
42 IJ.run(input_image_plus_copy, "Make Binary", "") |
0
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
43 |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
44 # Set the options. |
2
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
45 options = ["size=%s" % size] |
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
46 circularity_str = "%.3f-%.3f" % (circularity_min, circularity_max) |
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
47 options.append("circularity=%s" % circularity_str) |
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
48 if show.find("_") >= 0: |
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
49 show_str = "[%s]" % show.replace("_", " ") |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
50 else: |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
51 show_str = show |
2
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
52 options.append("show=%s" % show_str) |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
53 if display_results: |
2
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
54 options.append("display") |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
55 if not all_results: |
2
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
56 options.append("summarize") |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
57 if exclude_edges: |
2
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
58 options.append("exclude") |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
59 if include_holes: |
2
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
60 options.append("include") |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
61 # Always run "in_situ". |
2
6a07f71806bb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
imgteam
parents:
1
diff
changeset
|
62 options.append("in_situ") |
0
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
63 |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
64 # Run the command. |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
65 IJ.run(input_image_plus_copy, "Analyze Particles...", " ".join(options)) |
0
29b39f5b0e69
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff
changeset
|
66 |
1
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
67 # Save outputs. |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
68 if len(output_filename) > 0: |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
69 # Save the ImagePlus object as a new image. |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
70 IJ.saveAs(input_image_plus_copy, output_datatype, output_filename) |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
71 if display_results and len(results_path) > 0: |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
72 results_table = analyzer.getResultsTable() |
e2622ebb5ce4
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5"
imgteam
parents:
0
diff
changeset
|
73 results_table.saveAs(results_path) |