Mercurial > repos > recetox > table_scipy_interpolate
annotate utils.py @ 0:0112f08c95ed draft default tip
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
author | recetox |
---|---|
date | Wed, 29 Jan 2025 15:36:02 +0000 |
parents | |
children |
rev | line source |
---|---|
0
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
1 import argparse |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
2 from typing import Tuple |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
3 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
4 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
5 import pandas as pd |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
6 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
7 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
8 class KeyValuePairsAction(argparse.Action): |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
9 def __call__(self, parser, namespace, values, option_string=None): |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
10 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
11 Parse key=value pairs from the command line arguments. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
12 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
13 Parameters: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
14 parser (argparse.ArgumentParser): The argument parser instance. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
15 namespace (argparse.Namespace): The namespace to hold the parsed values. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
16 values (list): The list of key=value pairs. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
17 option_string (str): The option string. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
18 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
19 Sets: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
20 namespace.dest (dict): A dictionary with 1-based column index as key and new column name as value. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
21 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
22 key_value_pairs = {} |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
23 for item in values: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
24 try: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
25 key, value = item.split("=") |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
26 key_value_pairs[int(key)] = value # Convert key to integer |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
27 except ValueError: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
28 parser.error( |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
29 f"Invalid format for --rename: {item}. Expected format: key=value" |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
30 ) |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
31 setattr(namespace, self.dest, key_value_pairs) |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
32 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
33 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
34 class LoadDataAction(argparse.Action): |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
35 def __call__(self, parser, namespace, values, option_string=None): |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
36 file_path, file_extension = values |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
37 file_extension = file_extension.lower() |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
38 if file_extension == "csv": |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
39 df = pd.read_csv(file_path) |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
40 elif file_extension in ["tsv", "tabular"]: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
41 df = pd.read_csv(file_path, sep="\t") |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
42 elif file_extension == "parquet": |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
43 df = pd.read_parquet(file_path) |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
44 else: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
45 raise ValueError(f"Unsupported file format: {file_extension}") |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
46 setattr(namespace, self.dest, df) |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
47 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
48 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
49 def write_csv(df: pd.DataFrame, file_path: str) -> None: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
50 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
51 Write the dataframe to a CSV file. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
52 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
53 Parameters: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
54 df (pd.DataFrame): The dataframe to write. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
55 file_path (str): The path to the output CSV file. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
56 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
57 df.to_csv(file_path, index=False) |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
58 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
59 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
60 def write_tsv(df: pd.DataFrame, file_path: str) -> None: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
61 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
62 Write the dataframe to a TSV file. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
63 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
64 Parameters: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
65 df (pd.DataFrame): The dataframe to write. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
66 file_path (str): The path to the output TSV file. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
67 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
68 df.to_csv(file_path, sep="\t", index=False) |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
69 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
70 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
71 def write_parquet(df: pd.DataFrame, file_path: str) -> None: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
72 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
73 Write the dataframe to a Parquet file. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
74 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
75 Parameters: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
76 df (pd.DataFrame): The dataframe to write. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
77 file_path (str): The path to the output Parquet file. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
78 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
79 df.to_parquet(file_path, index=False) |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
80 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
81 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
82 class StoreOutputAction(argparse.Action): |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
83 def __call__( |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
84 self, |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
85 parser: argparse.ArgumentParser, |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
86 namespace: argparse.Namespace, |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
87 values: Tuple[str, str], |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
88 option_string: str = None, |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
89 ) -> None: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
90 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
91 Custom argparse action to store the output function and file path based on file extension. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
92 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
93 Parameters: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
94 parser (argparse.ArgumentParser): The argument parser instance. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
95 namespace (argparse.Namespace): The namespace to hold the parsed values. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
96 values (Tuple[str, str]): The file path and file extension. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
97 option_string (str): The option string. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
98 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
99 file_path, file_extension = values |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
100 file_extension = file_extension.lower() |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
101 if file_extension == "csv": |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
102 write_func = write_csv |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
103 elif file_extension in ["tsv", "tabular"]: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
104 write_func = write_tsv |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
105 elif file_extension == "parquet": |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
106 write_func = write_parquet |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
107 else: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
108 raise ValueError(f"Unsupported file format: {file_extension}") |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
109 setattr(namespace, self.dest, (write_func, file_path)) |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
110 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
111 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
112 class SplitColumnIndicesAction(argparse.Action): |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
113 def __call__( |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
114 self, |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
115 parser: argparse.ArgumentParser, |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
116 namespace: argparse.Namespace, |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
117 values: str, |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
118 option_string: str = None, |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
119 ) -> None: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
120 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
121 Custom argparse action to split a comma-separated list of column indices and convert to 0-based indices. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
122 |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
123 Parameters: |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
124 parser (argparse.ArgumentParser): The argument parser instance. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
125 namespace (argparse.Namespace): The namespace to hold the parsed values. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
126 values (str): The comma-separated list of 1-based column indices. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
127 option_string (str): The option string. |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
128 """ |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
129 indices = [int(x) - 1 for x in values.split(",")] # Convert to 0-based indices |
0112f08c95ed
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
recetox
parents:
diff
changeset
|
130 setattr(namespace, self.dest, indices) |