Skip to content

Commit

Permalink
Merge pull request #6914 from nsoranzo/remove_value
Browse files Browse the repository at this point in the history
Allow ``remove_value`` filter on data tables
  • Loading branch information
mvdbeek committed Oct 24, 2018
2 parents 51ca8dc + 19c98b2 commit 014b9d7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/galaxy/tools/parameters/dynamic_options.py
Expand Up @@ -388,7 +388,7 @@ def __init__(self, d_option, elem):
self.ref_name = elem.get("ref", None)
self.meta_ref = elem.get("meta_ref", None)
self.metadata_key = elem.get("key", None)
assert self.value is not None or ((self.ref_name is not None or self.meta_ref is not None)and self.metadata_key is not None), ValueError("Required 'value' or 'ref' and 'key' attributes missing from filter")
assert self.value is not None or self.ref_name is not None or (self.meta_ref is not None and self.metadata_key is not None), ValueError("Required 'value', or 'ref', or 'meta_ref' and 'key' attributes missing from filter")
self.multiple = string_as_bool(elem.get("multiple", "False"))
self.separator = elem.get("separator", ",")

Expand All @@ -401,13 +401,14 @@ def compare_value(option_value, filter_value):
if self.multiple:
option_value = option_value.split(self.separator)
for value in filter_value:
if value not in filter_value:
if value not in option_value:
return False
return True
return option_value in filter_value
if self.multiple:
return filter_value in option_value.split(self.separator)
return option_value == filter_value

value = self.value
if value is None:
if self.ref_name is not None:
Expand All @@ -419,7 +420,9 @@ def compare_value(option_value, filter_value):
if not isinstance(data_ref, HistoryDatasetAssociation) and not isinstance(data_ref, galaxy.tools.wrappers.DatasetFilenameWrapper):
return options # cannot modify options
value = data_ref.metadata.get(self.metadata_key, None)
return [(disp_name, optval, selected) for disp_name, optval, selected in options if not compare_value(optval, value)]
# Default to the second column (i.e. 1) since this used to work only on options produced by the data_meta filter
value_col = self.dynamic_option.columns.get('value', 1)
return [option for option in options if not compare_value(option[value_col], value)]


class SortByColumnFilter(Filter):
Expand Down Expand Up @@ -615,11 +618,13 @@ def get_fields(self, trans, other_values):
# Ensure parsing dynamic options does not consume more than a megabyte worth memory.
path = dataset.file_name
if os.path.getsize(path) < 1048576:
options = self.parse_file_fields(open(path))
with open(path) as fh:
options = self.parse_file_fields(fh)
else:
# Pass just the first megabyte to parse_file_fields.
log.warning("Attempting to load options from large file, reading just first megabyte")
contents = open(path, 'r').read(1048576)
with open(path, 'r') as fh:
contents = fh.read(1048576)
options = self.parse_file_fields(StringIO(contents))
elif self.tool_data_table:
options = self.tool_data_table.get_fields()
Expand Down
32 changes: 32 additions & 0 deletions test/functional/tools/remove_value.xml
@@ -0,0 +1,32 @@
<tool id="remove_value" name="Remove values from dynamic options" version="0.1.0">
<command detect_errors="exit_code"><![CDATA[
echo $choose_value > '$out_file'
]]></command>
<inputs>
<param name="choose_value" type="select">
<options from_data_table="test_fasta_indexes">
<!-- contains hg18 and hg19 -->
<filter type="remove_value" value="hg18"/>
<filter type="remove_value" value="hg20"/>
<!-- This doesn't work as we don't have the remaining values like name and path
<filter type="add_value" value="hg20"/>
-->
</options>
</param>
</inputs>
<outputs>
<data name="out_file"/>
</outputs>
<tests>
<test>
<output name="out_file">
<assert_contents>
<has_line line="hg19" />
</assert_contents>
</output>
</test>
<test expect_failure="true">
<param name="choose_value" value="hg18"/>
</test>
</tests>
</tool>
1 change: 1 addition & 0 deletions test/functional/tools/samples_tool_conf.xml
Expand Up @@ -12,6 +12,7 @@
<tool file="disambiguate_cond.xml" />
<tool file="multi_repeats.xml"/>
<tool file="library_data.xml"/>
<tool file="remove_value.xml"/>
<tool file="bibtex.xml"/>
<tool file="multi_select.xml" />
<tool file="multi_output.xml" />
Expand Down

0 comments on commit 014b9d7

Please sign in to comment.