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