Mercurial > repos > bgruening > flexynesis
comparison 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 |
comparison
equal
deleted
inserted
replaced
5:466b593fd87e | 6:33816f44fc7d |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import sys | 3 import sys |
4 from pathlib import Path | |
4 | 5 |
5 import pandas as pd | 6 import pandas as pd |
6 | 7 |
7 | 8 |
8 def get_column_name(file_path, index): | 9 def get_column_names(file_path, indices): |
9 """ | 10 """ |
10 Get the column name based on an index from a tabular file. | 11 Get column names based on multiple indices from a tabular file. |
11 """ | 12 """ |
12 data = pd.read_csv(file_path, sep=",") | 13 try: |
13 index = index - 1 # Convert to zero-based index | 14 file_ext = Path(file_path).suffix.lower() |
14 if index < 0 or index >= len(data.columns): | 15 sep = ',' if file_ext == '.csv' else '\t' |
15 print(f"Error: Index {index+1} is out of range. File has {len(data.columns)} columns (1-{len(data.columns)}).") | 16 |
16 return None | 17 if file_ext in ['.csv', '.tsv', '.txt', '.tab', '.tabular']: |
17 return data.columns[index].strip() | 18 data = pd.read_csv(file_path, sep=sep) |
19 else: | |
20 raise ValueError(f"Unsupported file extension: {file_ext}") | |
21 | |
22 except Exception as e: | |
23 raise ValueError(f"Error loading data from {file_path}: {e}") from e | |
24 | |
25 column_names = [] | |
26 | |
27 for index in indices: | |
28 zero_based_index = index - 1 # Convert to zero-based index | |
29 if zero_based_index < 0 or zero_based_index >= len(data.columns): | |
30 print(f"Error: Index {index} is out of range. File has {len(data.columns)} columns (1-{len(data.columns)}).") | |
31 return None | |
32 column_names.append(data.columns[zero_based_index].strip()) | |
33 | |
34 return column_names | |
18 | 35 |
19 | 36 |
20 if __name__ == "__main__": | 37 if __name__ == "__main__": |
21 | 38 |
22 file_path = sys.argv[1] | 39 file_path = sys.argv[1] |
23 index = int(sys.argv[2]) | 40 indices_str = sys.argv[2] |
24 | 41 |
25 print(get_column_name(file_path, index)) | 42 # Parse comma-separated indices |
43 try: | |
44 indices = [int(i.strip()) for i in indices_str.split(',')] | |
45 except ValueError: | |
46 print("Error: All indices must be integers.") | |
47 sys.exit(1) | |
48 | |
49 result = get_column_names(file_path, indices) | |
50 if result is not None: | |
51 print(','.join(result)) |