Skip to content

Commit

Permalink
Merge pull request galaxyproject#18879 from wm75/fix-get-column-options
Browse files Browse the repository at this point in the history
data_column params: offer same columns with and without use_header_names
  • Loading branch information
mvdbeek committed Sep 25, 2024
2 parents 3c92414 + fb3b717 commit c9d6ca6
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions lib/galaxy/tools/parameters/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,9 @@ def get_options(self, trans, other_values):
Show column labels rather than c1..cn if use_header_names=True
"""
options: List[Tuple[str, Union[str, Tuple[str, str]], bool]] = []
column_list = self.get_column_list(trans, other_values)
if not column_list:
return options
# if available use column_names metadata for option names
# otherwise read first row - assume is a header with tab separated names
if self.usecolnames:
Expand All @@ -1516,29 +1519,23 @@ def get_options(self, trans, other_values):
and hasattr(dataset.metadata, "column_names")
and dataset.metadata.element_is_set("column_names")
):
column_list = [
("%d" % (i + 1), "c%d: %s" % (i + 1, x)) for i, x in enumerate(dataset.metadata.column_names)
]
try:
options = [(f"c{c}: {dataset.metadata.column_names[int(c) - 1]}", c, False) for c in column_list]
except IndexError:
# ignore and rely on fallback
pass
else:
try:
with open(dataset.get_file_name()) as f:
head = f.readline()
cnames = head.rstrip("\n\r ").split("\t")
column_list = [("%d" % (i + 1), "c%d: %s" % (i + 1, x)) for i, x in enumerate(cnames)]
options = [(f"c{c}: {cnames[int(c) - 1]}", c, False) for c in column_list]
except Exception:
column_list = self.get_column_list(trans, other_values)
if self.numerical: # If numerical was requested, filter columns based on metadata
if hasattr(dataset, "metadata") and getattr(dataset.metadata, "column_types", None) is not None:
if len(dataset.metadata.column_types) >= len(column_list):
numerics = [i for i, x in enumerate(dataset.metadata.column_types) if x in ["int", "float"]]
column_list = [column_list[i] for i in numerics]
else:
column_list = self.get_column_list(trans, other_values)
for col in column_list:
if isinstance(col, tuple) and len(col) == 2:
options.append((col[1], col[0], False))
else:
options.append((f"Column: {col}", col, False))
# ignore and rely on fallback
pass
if not options:
# fallback if no options list could be built so far
options = [(f"Column: {col}", col, False) for col in column_list]
return options

def get_initial_value(self, trans, other_values):
Expand Down

0 comments on commit c9d6ca6

Please sign in to comment.