diff --git a/shareloc_utils/VERSION b/shareloc_utils/VERSION index dcab1c7..439e743 100644 --- a/shareloc_utils/VERSION +++ b/shareloc_utils/VERSION @@ -1,3 +1,3 @@ { - "version": "0.1.8" + "version": "0.1.9" } diff --git a/shareloc_utils/batch_download.py b/shareloc_utils/batch_download.py index 1399b8e..77d39f8 100644 --- a/shareloc_utils/batch_download.py +++ b/shareloc_utils/batch_download.py @@ -12,7 +12,7 @@ from tqdm import tqdm from shareloc_utils.smlm_file import read_smlm_file - +from shareloc_utils.formats import supported_text_formats class DownloadProgressBar(tqdm): def update_to(self, b=1, bsize=1, tsize=None): @@ -32,10 +32,15 @@ def resolve_url(rdf_url, path): return urljoin(os.path.dirname(rdf_url) + "/", path) -def convert_smlm(file_path, delimiter=",", extension=".csv"): +def convert_smlm(file_path, delimiter=",", extension=".csv", format="ThunderSTORM (csv)"): smlm_info = read_smlm_file(file_path) converted_files = [] table_count = len(smlm_info["files"]) + format_config = supported_text_formats[format]; + header_transform = {} + for k, v in format_config["header_transform"].items(): + header_transform[v]=k + for tbi, file_info in enumerate(smlm_info["files"]): cols = file_info["cols"] rows = file_info["rows"] @@ -46,7 +51,7 @@ def convert_smlm(file_path, delimiter=",", extension=".csv"): ) with open(fp, "w") as f: for i in range(cols): - f.write(headers[i] + (delimiter if i < cols - 1 else "\n")) + f.write((header_transform.get(headers[i], headers[i])) + (delimiter if i < cols - 1 else "\n")) for i in tqdm(range(rows), total=rows): for j in range(cols): f.write( diff --git a/shareloc_utils/formats.py b/shareloc_utils/formats.py new file mode 100644 index 0000000..1d76ab5 --- /dev/null +++ b/shareloc_utils/formats.py @@ -0,0 +1,141 @@ + +supported_text_formats = { + "ThunderSTORM (csv)": { + # Required + "type": "table", + "extension": ".csv", + "mode": "text", + "dtype": "float32", + "delimiter": ",", + "comments": "", + # specify which row is the header, set to -1 if there is no header + "header_row": 0, + # specify how to transform the headers to the standard header defined in smlm format, + "header_transform": { "x [nm]": "x", "y [nm]": "y", "z [nm]": "z", "uncertainty_xy [nm]": "uncertainty_xy", "uncertainty_z [nm]": "uncertainty_z" }, + + # Optional + "name": "ThunderSTORM (csv)", + "description": "a csv format used in thunderSTORM" + }, + "ThunderSTORM (xls)": { + # Required + "type": "table", + "extension": ".xls", + "mode": "text", + "dtype": "float32", + "delimiter": "\t", + "comments": "", + # specify which row is the header, set to -1 if there is no header + "header_row": 0, + # specify how to transform the headers to the standard header defined in smlm format, + "header_transform": { "x [nm]": "x", "y [nm]": "y", "z [nm]": "z", "uncertainty_xy [nm]": "uncertainty_xy", "uncertainty_z [nm]": "uncertainty_z" }, + + # Optional + "name": "ThunderSTORM (xls)", + "description": "a xls format used in thunderSTORM" + }, + "ZEISS (txt)": { + # Required + "type": "table", + "extension": ".txt", + "mode": "text", + "dtype": "float32", + "delimiter": "\t", + "comments": "", + # specify which row is the header, set to -1 if there is no header + "header_row": 0, + # specify how to transform the headers to the standard header defined in smlm format, + "header_transform": { "Position X [nm]": "x", "Position Y [nm]": "y", "Position Z [nm]": "z", "First Frame": "frame" }, + + # Optional + "name": "ZEISS (txt)", + "description": "a txt format used in ZEISS microscope" + }, + "ZEISSv1 (csv)": { + # Required + "type": "table", + "extension": ".csv", + "mode": "text", + "dtype": "float32", + "delimiter": ";", + "comments": "", + # specify which row is the header, set to -1 if there is no header + "header_row": 0, + # specify how to transform the headers to the standard header defined in smlm format, + "header_transform": { "Position X [nm]": "x", "Position Y [nm]": "y", "Position Z [nm]": "z", "First Frame": "frame" }, + + # Optional + "name": "ZEISSv1 (csv)", + "description": "a csv format used in ZEISS microscope" + }, + "ZEISSv2 (csv)": { + # Required + "type": "table", + "extension": ".csv", + "mode": "text", + "dtype": "float32", + "delimiter": ";", + "comments": "", + # specify which row is the header, set to -1 if there is no header + "header_row": 0, + # specify how to transform the headers to the standard header defined in smlm format, + "header_transform": { "x [nm]": "x", "y [nm]": "y", "z [nm]": "z", "First Frame": "frame" }, + + # Optional + "name": "ZEISSv2 (csv)", + "description": "a csv format used for ZEISS microscope" + }, + "RapidSTORM (txt)": { + # Required + "type": "table", + "extension": ".txt", + "mode": "text", + "dtype": "float32", + "delimiter": " ", + "comments": "", + # specify which row is the header, set to -1 if there is no header + "header_row": 0, + + # specify how to transform the headers to the standard header defined in smlm format, + "header_transform": { "*all*": 'identifier="([\\w\\d\\-_]+)"', "Position-0-0": "x", "Position-1-0": "y", "Position-2-0": "z", "ImageNumber-0-0": "frame" }, + + # Optional + "name": "RapidSTORM (txt)", + "description": "a text format used in RapidSTORM" + }, + "Space Seperated List (txt)": { + # Required + "type": "table", + "extension": ".txt", + "mode": "text", + "dtype": "float32", + "delimiter": " ", + "comments": "", + # specify which row is the header, set to -1 if there is no header + "header_row": 0, + + # specify how to transform the headers to the standard header defined in smlm format, + "header_transform": {}, + + # Optional + "name": "Space Seperated List (txt)", + "description": "A list of coordinates seperated with space." + }, + "Nikon NSTORM (txt)": { + # Required + "type": "table", + "extension": ".txt", + "mode": "text", + "dtype": "float32", + "delimiter": "\t", + "comments": "", + # specify which row is the header, set to -1 if there is no header + "header_row": 0, + # specify how to transform the headers to the standard header defined in smlm format, + "header_transform": { "X": "x", "Y": "y", "Z": "z", "Frame": "frame" }, + + # Optional + "name": "Nikon NSTORM (txt)", + "description": "a txt format exported from Nikon NSTORM software" + }, +} \ No newline at end of file