annotate tabular_to_ZeissLMDtext.py @ 0:f17bc16fefcf draft default tip

planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
author galaxyp
date Mon, 01 Jul 2024 14:07:46 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
1 import argparse
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
2
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
3 from shapely.geometry import Polygon
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
4
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
5
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
6 def tabular_to_text(input_file, text_file):
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
7 """
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
8 Converts tabular coordinate data from Galaxy into a formatted text file which is readable for the LMD.
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
9
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
10 This function reads tabular data from an input file, processes it to form a closed polygon, calculates the area
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
11 of the polygon, and writes the formatted information to an output text file.
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
12
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
13 Parameters:
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
14 input_file (str): Path to the input file containing tabular coordinate data.
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
15 The file should have a header and each line should contain x and y coordinates separated by a tab.
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
16 text_file (str): Path to the output text file where the formatted information will be written.
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
17
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
18 The output text file will contain:
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
19 - Header information
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
20 - A section with metadata including version, date, and time
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
21 - A section with details of the polygon including type, color, thickness, number, cutshot, area, comment,
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
22 and coordinates.
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
23 """
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
24 coordinates = []
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
25 with open(input_file, 'r') as f:
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
26 next(f) # Skip the header
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
27 for line in f:
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
28 x, y = map(float, line.strip().split('\t'))
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
29 coordinates.append([x, y])
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
30
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
31 coordinates.append(coordinates[0]) # Close the polygon by repeating the first point as the last point
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
32 area = Polygon(coordinates).area
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
33
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
34 with open(text_file, 'w') as f:
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
35 f.write("PALMRobo Elements\n")
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
36 f.write("Version:\tV 4.6.0.4\n")
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
37 f.write("Date, Time:\t13.02.2024\t16:06:32\n")
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
38 f.write("\nMICROMETER\nElements :\n\nType\tColor\tThickness\tNo\tCutShot\tArea\tComment\tCoordinates\n\n")
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
39 f.write(f"Freehand\tgreen\t0\t7\t0,0\t{area}\tROI imported from tabular data\n")
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
40
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
41 for i in range(0, len(coordinates), 5):
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
42 for j in range(5):
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
43 if i + j < len(coordinates):
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
44 x, y = coordinates[i + j]
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
45 f.write(f"\t{x},{y}")
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
46 f.write("\n.")
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
47
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
48
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
49 if __name__ == '__main__':
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
50 parser = argparse.ArgumentParser(description="Convert tabular coordinate data into a formatted text file")
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
51 parser.add_argument('--input', type=str, required=True, help='Path to the input tabular file')
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
52 parser.add_argument('--output', type=str, required=True, help='Path to the output text file')
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
53 args = parser.parse_args()
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
54
f17bc16fefcf planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/zeiss_lmd_converter commit 17fc7ee7911e0b875aaed5be4a129cfea208c76f
galaxyp
parents:
diff changeset
55 tabular_to_text(args.input, args.output)