csuite 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
csuite/__init__.py ADDED
File without changes
@@ -0,0 +1,147 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
4
+ import argparse
5
+ import logging
6
+ from functools import partial
7
+
8
+ from csuite.validators import (validate_report_args,
9
+ validate_cblaster_search_args,
10
+ validate_cblaster_makedb_args,
11
+ validate_remote_extract_args,
12
+ )
13
+ from cfoldseeker.main import parse_and_validate_arguments as cfs_arg_validator
14
+ from cfoldseeker.build_cds_db import parse_and_validate_arguments as cfscds_arg_validator
15
+ from cfoldseeker.extract_sequences import parse_and_validate_arguments as cfsext_arg_validator
16
+ from cagecleaner.validators import parse_and_validate_arguments as ccl_arg_validator
17
+
18
+ from csuite.defaults import (cfoldseekerDefaultConfiguration,
19
+ cfoldseekerCDSDefaultConfiguration,
20
+ CAGEcleanerDefaultConfiguration,
21
+ mainDefaultConfiguration,
22
+ reportDefaultConfiguration,
23
+ cblasterSearchDefaultConfiguration,
24
+ cblasterMakedbDefaultConfiguration,
25
+ extractDefaultConfiguration,
26
+ )
27
+
28
+
29
+ LOG = logging.getLogger(__name__)
30
+
31
+ # List which modules/tools are part of which workflow
32
+ WORKFLOW_TOOLS = {'remote_struc': ['MAIN', 'CFS'],
33
+ 'local_struc': ['MAIN', 'CFSCDS', 'CFS'],
34
+ 'remote_struc_derep': ['MAIN', 'CFS', 'rCCL'],
35
+ 'local_struc_derep': ['MAIN', 'CFSCDS', 'CFS', 'lCCL'],
36
+ 'remote_seq': ['MAIN', 'CBL'],
37
+ 'local_seq': ['MAIN', 'CBLDB', 'CBL'],
38
+ 'remote_seq_derep': ['MAIN', 'CBL', 'rCCL'],
39
+ 'local_seq_derep': ['MAIN', 'CBLDB', 'CBL', 'lCCL'],
40
+ 'derep': ['MAIN', 'CCL'],
41
+ 'report': ['MAIN', 'OUT'],
42
+ 'remote_extract': ['MAIN', 'rEXT'],
43
+ 'local_extract': ['MAIN', 'lEXT']
44
+ }
45
+
46
+ # Define the default argument values for each tool
47
+ TOOL_DEFAULT_CONFS = {'MAIN': mainDefaultConfiguration(),
48
+ 'CFS': cfoldseekerDefaultConfiguration(),
49
+ 'CFSCDS': cfoldseekerCDSDefaultConfiguration(),
50
+ 'lCCL': CAGEcleanerDefaultConfiguration(),
51
+ 'rCCL': CAGEcleanerDefaultConfiguration(),
52
+ 'CCL': CAGEcleanerDefaultConfiguration(),
53
+ 'OUT': reportDefaultConfiguration(),
54
+ 'CBL': cblasterSearchDefaultConfiguration(),
55
+ 'CBLDB': cblasterMakedbDefaultConfiguration(),
56
+ 'rEXT': extractDefaultConfiguration(),
57
+ 'lEXT': extractDefaultConfiguration(),
58
+ }
59
+
60
+ # Define the validator function for each tool
61
+ TOOL_ARG_VALIDATORS = {'MAIN': vars, # No validation for main args, just parsing; validation is done by tools
62
+ 'CFS': partial(cfs_arg_validator, skip_context_table_check = True),
63
+ 'CFSCDS': cfscds_arg_validator,
64
+ 'CCL': ccl_arg_validator,
65
+ 'lCCL': partial(ccl_arg_validator, bypass_source = 'local'),
66
+ 'rCCL': partial(ccl_arg_validator, bypass_source = 'remote'),
67
+ 'OUT': validate_report_args,
68
+ 'CBL': validate_cblaster_search_args,
69
+ 'CBLDB': validate_cblaster_makedb_args,
70
+ 'rEXT': validate_remote_extract_args,
71
+ 'lEXT': cfsext_arg_validator,
72
+ }
73
+
74
+
75
+ def categorise_args(args: argparse.Namespace) -> dict[str:argparse.Namespace]:
76
+ """
77
+ Categorise the argument namespace by workflow.
78
+
79
+ The arguments collected through the CLI are grouped by the tool that requires them, as indicated through the CLI
80
+ argument labels. Missing argument values are imputed from the default tool configurations.
81
+
82
+ The argument labels are prefixed by the codename of the tool that requires them.
83
+ For example, an argument `par1` required by tools `tool1` and `tool2` will be encoded as `tool1_tool2$par1`.
84
+
85
+ Args:
86
+ args (argparse.Namespace): Unparsed CLI argument namespace
87
+
88
+ Returns:
89
+ categorised_args (dict[argparse.Namespace]): dictionary of unparsed argument namespaces per tool
90
+ involved in the workflow, supplemented with default argument values.
91
+
92
+ Note:
93
+ Categorising arguments is facilitated by toolname prefixes in the argument names (see CLI subparsers.)
94
+ """
95
+ # Temporary parsing of the argument values
96
+ all_args = vars(args)
97
+
98
+ # Determine selected tools/modules
99
+ selected_tools = WORKFLOW_TOOLS[all_args['command']]
100
+
101
+ # Categorise arguments by tool
102
+ categorised_args = {}
103
+ for tool in selected_tools:
104
+ # Extract the arguments relevant for this tool using the prefixed argument labels
105
+ tool_args = {k.split('$')[1] : v # Which argument-value pair?
106
+ for k,v in all_args.items()
107
+ if tool in k.split('$')[0].split('_') # Is it required by this tool?
108
+ }
109
+
110
+ # Parse default argument values for this tool
111
+ default_args = TOOL_DEFAULT_CONFS[tool]
112
+ tool_args_with_defaults = vars(default_args)
113
+
114
+ # Add or adjust freshly parsed arguments to the default configuration to end up with a fully populated namespace
115
+ for arg_key, arg_val in tool_args.items():
116
+ tool_args_with_defaults[arg_key] = arg_val
117
+
118
+ # Define the new namespace for this tool
119
+ categorised_args[tool] = argparse.Namespace(**tool_args_with_defaults)
120
+
121
+ return categorised_args
122
+
123
+
124
+ def parse_and_validate_args(categorised_args: dict[str:argparse.Namespace]) -> dict:
125
+ """
126
+ Parse and validate the argument namespace for each tool.
127
+
128
+ Parses the arguments for each tool, and validates them using their associated validator functions.
129
+
130
+ Args:
131
+ categorised_args (dict[argparse.Namespace]): dictionary of unparsed argument namespaces per tool
132
+ involved in the workflow.
133
+
134
+ Returns:
135
+ parsed_categorised_args (dict): dictionary of dictionaries of argument name-value pairs,
136
+ grouped by tool involved in a workflow.
137
+ """
138
+ # Determine which validator functions will be needed for each tool
139
+ selected_validators = {tool: TOOL_ARG_VALIDATORS[tool] for tool in categorised_args.keys()}
140
+
141
+ # Parse and validate each tool's argument namespace using the selected validator
142
+ parsed_categorised_args = {}
143
+ for tool, validator in selected_validators.items():
144
+ parsed_categorised_args[tool] = validator(categorised_args[tool])
145
+
146
+ return parsed_categorised_args
147
+