forked from Helmholtz-UFZ/galaxy-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import argparse | ||
import csv | ||
import json | ||
|
||
import ezomero as ez | ||
|
||
|
||
def get_object_ezo(user, pws, host, port, obj_type, ids, tsv_file): | ||
# Function to write tabular file from the ezomero output | ||
def write_values_to_tsv(data, header): | ||
with open(tsv_file, 'a+', newline='') as f: | ||
f.seek(0) | ||
is_empty = f.tell() == 0 # Check if file is empty | ||
writer = csv.writer(f, delimiter='\t') | ||
if is_empty: | ||
writer.writerow([header]) # Write the header | ||
for item in data: | ||
writer.writerow([item]) # Write each value | ||
|
||
# Function to write tabular file from a dictionary ezomero output | ||
def write_dict_to_tsv(data, headers): | ||
with open(tsv_file, 'a+', newline='') as f: | ||
f.seek(0) | ||
is_empty = f.tell() == 0 # Check if file is empty | ||
writer = csv.writer(f, delimiter='\t') | ||
if is_empty: | ||
writer.writerow(headers) # Write the headers | ||
for key, value in data.items(): | ||
writer.writerow([key, value]) # Write each key-value pair | ||
|
||
# Function to write tabular file from list of list ezomero output | ||
def write_table_to_tsv(data): | ||
with open(tsv_file, 'w') as f: | ||
for row in data: | ||
f.write('\t'.join([str(val) for val in row]) + '\n') | ||
|
||
try: | ||
with ez.connect(user, pws, "", host, port, secure=True) as conn: | ||
if obj_type == "Annotation": | ||
ma_dict = {} | ||
for maid in ids: | ||
current_ma_dict = ez.get_map_annotation(conn, maid) | ||
ma_dict = {**ma_dict, **current_ma_dict} | ||
write_dict_to_tsv(ma_dict, ["Annotation ID", "Annotation Value"]) | ||
return ma_dict | ||
elif obj_type == "Tag": | ||
tags = [] | ||
for tag_id in ids: | ||
tags.append(ez.get_tag(conn, tag_id)) | ||
# Sort the tags for consistency: | ||
tags.sort | ||
write_ids_to_tsv(tags, "Tags") | ||
return tags | ||
elif obj_type == "Table": | ||
if len(ids) > 1: | ||
raise ValueError("Only one table can be exported at a time") | ||
table = ez.get_table(conn, ids[0]) | ||
write_table_to_tsv(table) | ||
return table | ||
else: | ||
raise ValueError(f"Unsupported object type: {obj_type}") | ||
|
||
except Exception as e: | ||
print(f"Connection error: {str(e)}") | ||
return None | ||
|
||
|
||
# Argument parsing | ||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Fetch and save data as TSV based on object type.") | ||
parser.add_argument("--credential-file", dest="credential_file", type=str, | ||
required=True, help="Credential file (JSON file with username and password for OMERO)") | ||
parser.add_argument('--host', required=True, | ||
help="Host server address.") | ||
parser.add_argument('--port', required=True, type=int, | ||
help='OMERO port') | ||
parser.add_argument('--obj_type', required=True, | ||
help="Type of object to fetch: Annotation, Table or Tag.") | ||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument('--ids', nargs = '+', type = int, | ||
help="IDs of the OMERO objects.") | ||
group.add_argument('--ids_path', | ||
help="File with IDs of the OMERO objects (one per line).") | ||
parser.add_argument('--tsv_file', default='id_list.tsv', required=True, | ||
help="Output TSV file path.") | ||
args = parser.parse_args() | ||
|
||
if args.ids_path: | ||
args.ids = [] | ||
with open(args.ids_path, 'r') as f: | ||
for line in f: | ||
try: | ||
args.ids.append(int(line)) | ||
except ValueError: | ||
print(f"{line.strip()} is not a valid ID.") | ||
if len(args.ids) == 0: | ||
raise ValueError("Cound not find a single ID in the file.") | ||
|
||
with open(args.credential_file, 'r') as f: | ||
crds = json.load(f) | ||
|
||
# Call the main function to get the object and save it as a TSV | ||
get_object_ezo(user=crds['username'], pws=crds['password'], host=args.host, | ||
port=args.port, | ||
obj_type=args.obj_type, | ||
ids=args.ids, | ||
tsv_file=args.tsv_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<tool id="omero_get_value" name="OMERO get Object" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" | ||
profile="20.01" license="MIT"> | ||
<description> with ezomero </description> | ||
<macros> | ||
<token name="@TOOL_VERSION@">5.18.0</token> | ||
<token name="@VERSION_SUFFIX@">0</token> | ||
</macros> | ||
<xrefs> | ||
<xref type="bio.tools">omero</xref> | ||
</xrefs> | ||
<requirements> | ||
<requirement type="package" version="3.0.1">ezomero</requirement> | ||
<!-- openjdk is needed: https://github.com/conda-forge/omero-py-feedstock/pull/16 --> | ||
<requirement type="package" version="21.0.2">openjdk</requirement> | ||
</requirements> | ||
<command detect_errors="exit_code"><![CDATA[ | ||
python $__tool_directory__/omero_get_value.py | ||
--credential-file '$credentials' | ||
--host $omero_host | ||
--port $omero_port | ||
--obj_type $obj_type | ||
#if $ids_input.ids_format == "values": | ||
--ids str($ids_input.ids).replace(',', ' ') | ||
#else: | ||
--ids_path $ids_input.ids_path | ||
#end if | ||
--tsv_file $tsv | ||
]]></command> | ||
<configfiles> | ||
<configfile name="credentials"><![CDATA[ | ||
{ | ||
"username": "$__user__.extra_preferences.get('omero_account|username', $test_username)", | ||
"password": "$__user__.extra_preferences.get('omero_account|password', $test_password)" | ||
} | ||
]]></configfile> | ||
</configfiles> | ||
<inputs> | ||
<param name="omero_host" type="text" label="OMERO host URL"> | ||
<validator type="regex" message="Enter a valid host location, for example, your.omero.server">^[a-zA-Z0-9._-]*$</validator> | ||
<validator type="expression" message="No two dots (..) allowed">'..' not in value</validator> | ||
</param> | ||
<param argument="omero_port" type="integer" optional="false" value="4064" label="OMERO port"/> | ||
<param argument="obj_type" type="select" optional="false" label="Type of object to fetch:"> | ||
<option value="Annotation">Annotation</option> | ||
<option value="Tag">Tag</option> | ||
<option value="Table">Table</option> | ||
</param> | ||
<conditional name="ids_input"> | ||
<param name="ids_format" type="select" label="How do you provide the ID(s) of the OMERO object?"> | ||
<option value="values">Comma separated values</option> | ||
<option value="file">From a dataset (one per line)</option> | ||
</param> | ||
<when value="values"> | ||
<param argument="--ids" type="text" value="" label="ID(s) of the object(s) to fetch on OMERO separated by comma"> | ||
<validator type="regex">[0-9]+(,[0-9]+)*</validator> | ||
</param> | ||
</when> | ||
<when value="file"> | ||
<param argument="--ids_path" type="data" format="txt,tabular" label="Dataset with ID(s) of the object(s) to fetch on OMERO (one per line)"/> | ||
</when> | ||
</conditional> | ||
<param name="test_username" type="hidden" value=""/> | ||
<param name="test_password" type="hidden" value=""/> | ||
</inputs> | ||
<outputs> | ||
<data name="tsv" format="tabular"/> | ||
</outputs> | ||
<tests> | ||
<!-- TODO --> | ||
</tests> | ||
<help> | ||
Description | ||
----------- | ||
|
||
Tool to fetch Annotation, Tag and Tables from IDs. | ||
|
||
The IDs can be obtained with the tool OMERO get IDs with ezomero | ||
|
||
</help> | ||
<citations> | ||
<citation type="doi">10.1038/nmeth.1896</citation> | ||
</citations> | ||
</tool> |