Skip to content

Commit

Permalink
add data table filter
Browse files Browse the repository at this point in the history
  • Loading branch information
bernt-matthias committed Nov 17, 2021
1 parent c89bcad commit f99c729
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/galaxy/tool_util/xsd/galaxy.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -4287,7 +4287,7 @@ only used if ``multiple`` is set to ``true``.]]></xs:documentation>
<xs:annotation>
<xs:documentation xml:lang="en">If ``true``, keep columns matching the
value, if ``false`` discard columns matching the value. Used when ``type`` is
either ``static_value``, ``regexp`` or ``param_value``.</xs:documentation>
either ``static_value``, ``regexp``, ``param_value`` or ``data_table``.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="value" type="xs:string">
Expand Down Expand Up @@ -4319,6 +4319,18 @@ added to the end of the list.</xs:documentation>
from the list.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="table_name" type="xs:string">
<xs:annotation>
<xs:documentation xml:lang="en">Only used when ``type`` is
``data_table``. The name of the data table to use.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="data_table_column" type="xs:string">
<xs:annotation>
<xs:documentation xml:lang="en">Only used when ``type`` is
``data_table``. The column of the data table to use (0 based index or column name).</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="Outputs">
<xs:annotation>
Expand Down Expand Up @@ -6192,6 +6204,7 @@ and ``bibtex`` are the only supported options.</xs:documentation>
<xs:enumeration value="add_value"/>
<xs:enumeration value="remove_value"/>
<xs:enumeration value="sort_by"/>
<xs:enumeration value="data_table"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ActionsConditionalFilterType">
Expand Down
57 changes: 56 additions & 1 deletion lib/galaxy/tools/parameters/dynamic_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,58 @@ def filter_options(self, options, trans, other_values):
return sorted(options, key=lambda x: x[self.column])


class DataTableFilter(Filter):
"""
Filters a list of options by entries present in a data table, i.e.
option[column] needs to be in the specified data table column
Type: data_table
Required Attributes:
- column: column in options to compare with
- table_name: data table to use
- data_table_column: data table column to use
Optional Attributes:
- keep: Keep options where option[column] is in the data table column (True)
Discard columns matching value (False)
"""
def __init__(self, d_option, elem):
Filter.__init__(self, d_option, elem)
self.table_name = elem.get("table_name", None)
assert self.table_name is not None, "Required 'table_name' attribute missing from filter"
column = elem.get("column", None)
assert column is not None, "Required 'column' attribute missing from filter"
self.column = d_option.column_spec_to_index(column)
self.data_table_column = elem.get("data_table_column", None)
assert self.data_table_column is not None, "Required 'data_table_column' attribute missing from filter"
self.keep = string_as_bool(elem.get("keep", 'True'))

def filter_options(self, options, trans, other_values):
# get column from data table, by index or column name
entries = None
try:
entries = set([f[int(self.data_table_column)] for f in trans.app.tool_data_tables[self.table_name].get_fields()])
except TypeError:
pass
try:
entries = set([f[self.data_table_column] for f in trans.app.tool_data_tables[self.table_name].get_named_fields_list()])
except KeyError:
pass
if entries is None:
log.error(f"could not get data from column {self.data_table_column} from data_table {self.table_name}")
return options

rval = []
for o in options:
log.error(f"o {o}")
if self.keep == (o[self.column] in entries):
rval.append(o)
return rval

filter_types = dict(data_meta=DataMetaFilter,
param_value=ParamValueFilter,
static_value=StaticValueFilter,
Expand All @@ -508,7 +560,8 @@ def filter_options(self, options, trans, other_values):
attribute_value_splitter=AttributeValueSplitterFilter,
add_value=AdditionalValueFilter,
remove_value=RemoveValueFilter,
sort_by=SortByColumnFilter)
sort_by=SortByColumnFilter,
data_table=DataTableFilter)


class DynamicOptions:
Expand Down Expand Up @@ -557,10 +610,12 @@ def load_from_parameter(from_parameter, transform_lines=None):
data_file = data_file.strip()
if not os.path.isabs(data_file):
full_path = os.path.join(self.tool_param.tool.app.config.tool_data_path, data_file)
log.error(f"full_path {full_path}")
if os.path.exists(full_path):
self.index_file = data_file
with open(full_path) as fh:
self.file_fields = self.parse_file_fields(fh)
log.error(f"exists file_fields {self.file_fields}")
else:
self.missing_index_file = data_file
elif dataset_file is not None:
Expand Down
2 changes: 2 additions & 0 deletions test/functional/tool-data/test_file.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hg19_value hg19 hg19_name hg19_path
absent_value absent absent_name absent_path
45 changes: 45 additions & 0 deletions test/functional/tools/filter_data_table.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<tool id="filter_data_table" name="filter data table" version="0.1.0">
<description>Filter on datatable entries</description>
<command><![CDATA[
echo $dynamic_select > '$output'
]]></command>
<inputs>
<param name="dynamic_select" type="select">
<!-- initialize options from file (contains all entries that could be added to the data table) -->
<options from_file="../test/functional/tool-data/test_file.tsv">
<column name="name" index="0"/>
<column name="value" index="0"/>
<!-- filter options that are already in the data table -->
<filter type="data_table" column="0" table_name="test_fasta_indexes" data_table_column="0" keep="false"/>
<validator type="no_options" message="No options available" />
</options>
</param>
</inputs>

<outputs>
<data name="output" format="txt" />
</outputs>

<tests>
<!-- select the value that is absent from the data table -->
<test expect_failure="false">
<param name="dynamic_select" value="absent_value" />
<output name="output">
<assert_contents>
<has_text text="absent_value"/>
</assert_contents>
</output>
</test>
<!-- selecting the value that is already in the data table (and therefor filtered) fails -->
<test expect_failure="true">
<param name="dynamic_select" value="hg19_value" />
<output name="output">
<assert_contents>
<has_text text="hg19_value"/>
</assert_contents>
</output>
</test>
</tests>
<help>
</help>
</tool>
1 change: 1 addition & 0 deletions test/functional/tools/samples_tool_conf.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<tool file="dbkey_filter_input.xml" />
<tool file="dbkey_filter_multi_input.xml" />
<tool file="dbkey_filter_collection.xml" />
<tool file="filter_data_table.xml" />
<tool file="dbkey_filter_paired_collection.xml" />
<tool file="dbkey_output_action.xml" />
<tool file="composite_output.xml" />
Expand Down

0 comments on commit f99c729

Please sign in to comment.