Skip to content

Commit

Permalink
Fix relabel_from_file collection operation error handling if duplicat…
Browse files Browse the repository at this point in the history
…e identifiers.
  • Loading branch information
jmchilton committed Apr 17, 2017
1 parent 09bcdd9 commit 2288a88
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/galaxy/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,16 @@ def produce_outputs(self, trans, out_data, output_collections, incoming, history
new_labels_dataset_assoc = incoming["how"]["labels"]
strict = string_as_bool(incoming["how"]["strict"])
new_elements = odict()

def add_copied_value_to_new_elements(new_label, dce_object):
new_label = new_label.strip()
if new_label in new_elements:
raise Exception("New identifier [%s] appears twice in resulting collection, these values must be unique." % new_label)
copied_value = dce_object.copy()
if getattr(copied_value, "history_content_type", None) == "dataset":
history.add_dataset(copied_value, set_hid=False)
new_elements[new_label] = copied_value

new_labels_path = new_labels_dataset_assoc.file_name
new_labels = open(new_labels_path, "r").readlines(1024 * 1000000)
if strict and len(hdca.collection.elements) != len(new_labels):
Expand All @@ -2531,18 +2541,12 @@ def produce_outputs(self, trans, out_data, output_collections, incoming, history
new_label = new_labels_dict.get(element_identifier, default)
if not new_label:
raise Exception("Failed to find new label for identifier [%s]" % element_identifier)
copied_value = dce_object.copy()
if getattr(copied_value, "history_content_type", None) == "dataset":
history.add_dataset(copied_value, set_hid=False)
new_elements[new_label] = copied_value
add_copied_value_to_new_elements(new_label, dce_object)
else:
# If new_labels_dataset_assoc is not a two-column tabular dataset we label with the current line of the dataset
for i, dce in enumerate(hdca.collection.elements):
dce_object = dce.element_object
copied_value = dce_object.copy()
if getattr(copied_value, "history_content_type", None) == "dataset":
history.add_dataset(copied_value, set_hid=False)
new_elements[new_labels[i].strip()] = copied_value
add_copied_value_to_new_elements(new_labels[i], dce_object)
for key in new_elements.keys():
if not re.match("^[\w\-_]+$", key):
raise Exception("Invalid new colleciton identifier [%s]" % key)
Expand Down
11 changes: 11 additions & 0 deletions lib/galaxy/tools/relabel_from_file.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@
<param name="how_select" value="txt" />
<param name="labels" value="new_labels_bad_1.txt" ftype="txt" />
</test>
<!-- test label bad because of duplicates -->
<test expect_failure="true">
<param name="input">
<collection type="list">
<element name="i1" value="simple_line.txt" />
<element name="i2" value="simple_line.txt" />
</collection>
</param>
<param name="how_select" value="txt" />
<param name="labels" value="new_labels_bad_2.txt" ftype="txt" />
</test>
</tests>
<help>
This tool will take an input list and a text file with new identifiers
Expand Down
2 changes: 2 additions & 0 deletions test-data/new_labels_bad_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
newi1
newi1

0 comments on commit 2288a88

Please sign in to comment.