Mercurial > repos > bgruening > flexynesis
diff index_to_name.py @ 6:33816f44fc7d draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
author | bgruening |
---|---|
date | Wed, 23 Jul 2025 07:49:41 +0000 |
parents | 466b593fd87e |
children |
line wrap: on
line diff
--- a/index_to_name.py Fri Jul 04 14:57:40 2025 +0000 +++ b/index_to_name.py Wed Jul 23 07:49:41 2025 +0000 @@ -1,25 +1,51 @@ #!/usr/bin/env python import sys +from pathlib import Path import pandas as pd -def get_column_name(file_path, index): +def get_column_names(file_path, indices): """ - Get the column name based on an index from a tabular file. + Get column names based on multiple indices from a tabular file. """ - data = pd.read_csv(file_path, sep=",") - index = index - 1 # Convert to zero-based index - if index < 0 or index >= len(data.columns): - print(f"Error: Index {index+1} is out of range. File has {len(data.columns)} columns (1-{len(data.columns)}).") - return None - return data.columns[index].strip() + try: + file_ext = Path(file_path).suffix.lower() + sep = ',' if file_ext == '.csv' else '\t' + + if file_ext in ['.csv', '.tsv', '.txt', '.tab', '.tabular']: + data = pd.read_csv(file_path, sep=sep) + else: + raise ValueError(f"Unsupported file extension: {file_ext}") + + except Exception as e: + raise ValueError(f"Error loading data from {file_path}: {e}") from e + + column_names = [] + + for index in indices: + zero_based_index = index - 1 # Convert to zero-based index + if zero_based_index < 0 or zero_based_index >= len(data.columns): + print(f"Error: Index {index} is out of range. File has {len(data.columns)} columns (1-{len(data.columns)}).") + return None + column_names.append(data.columns[zero_based_index].strip()) + + return column_names if __name__ == "__main__": file_path = sys.argv[1] - index = int(sys.argv[2]) + indices_str = sys.argv[2] - print(get_column_name(file_path, index)) + # Parse comma-separated indices + try: + indices = [int(i.strip()) for i in indices_str.split(',')] + except ValueError: + print("Error: All indices must be integers.") + sys.exit(1) + + result = get_column_names(file_path, indices) + if result is not None: + print(','.join(result))