annotate index_to_name.py @ 3:52b6f2ac38c7 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:52 +0000
parents 3c5d82bf6e8a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
1 #!/usr/bin/env python
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
2
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
3 import sys
3
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
4 from pathlib import Path
2
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
5
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
6 import pandas as pd
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
7
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
8
3
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
9 def get_column_names(file_path, indices):
2
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
10 """
3
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
11 Get column names based on multiple indices from a tabular file.
2
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
12 """
3
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
13 try:
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
14 file_ext = Path(file_path).suffix.lower()
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
15 sep = ',' if file_ext == '.csv' else '\t'
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
16
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
17 if file_ext in ['.csv', '.tsv', '.txt', '.tab', '.tabular']:
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
18 data = pd.read_csv(file_path, sep=sep)
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
19 else:
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
20 raise ValueError(f"Unsupported file extension: {file_ext}")
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
21
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
22 except Exception as e:
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
23 raise ValueError(f"Error loading data from {file_path}: {e}") from e
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
24
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
25 column_names = []
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
26
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
27 for index in indices:
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
28 zero_based_index = index - 1 # Convert to zero-based index
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
29 if zero_based_index < 0 or zero_based_index >= len(data.columns):
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
30 print(f"Error: Index {index} is out of range. File has {len(data.columns)} columns (1-{len(data.columns)}).")
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
31 return None
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
32 column_names.append(data.columns[zero_based_index].strip())
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
33
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
34 return column_names
2
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
35
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
36
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
37 if __name__ == "__main__":
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
38
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
39 file_path = sys.argv[1]
3
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
40 indices_str = sys.argv[2]
2
3c5d82bf6e8a planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 1afbaf45449e25238935e222f983da62392c067a
bgruening
parents:
diff changeset
41
3
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
42 # Parse comma-separated indices
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
43 try:
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
44 indices = [int(i.strip()) for i in indices_str.split(',')]
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
45 except ValueError:
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
46 print("Error: All indices must be integers.")
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
47 sys.exit(1)
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
48
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
49 result = get_column_names(file_path, indices)
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
50 if result is not None:
52b6f2ac38c7 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flexynesis commit 6b520305ec30e6dc37eba92c67a5368cea0fc5ad
bgruening
parents: 2
diff changeset
51 print(','.join(result))