You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow for dynamic module names
resolvessnakemake#1923
### QC
<!-- Make sure that you can tick the boxes below. -->
* [x] The PR contains a test case for the changes or the changes are
already covered by an existing test case.
* [x] The documentation (`docs/`) is updated to reflect the changes or
this is not necessary (e.g. if the change does neither modify the
language nor the behavior or functionalities of Snakemake).
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **New Features**
- Enhanced modular workflows enable dynamic and semi-dynamic module
configuration with flexible aliasing and output management.
- **Documentation**
- Updated guidelines provide clear instructions on configuring dynamic
module imports and rule aliasing, including a new section on "Dynamic
Modules."
- **Tests**
- Expanded test coverage validates diverse module configurations and
expected outcomes, ensuring robust and reliable workflows.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Jens Zentgraf <[email protected]>
Co-authored-by: Johanna <[email protected]>
Copy file name to clipboardExpand all lines: docs/snakefiles/modularization.rst
+41Lines changed: 41 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -201,6 +201,47 @@ Otherwise, you will have two versions of the same rule, which might be unintende
201
201
202
202
Of course, it is possible to combine the use of rules from multiple modules (see :ref:`use_with_modules`), and via modifying statements they can be rewired and reconfigured in an arbitrary way.
203
203
204
+
.. _snakefiles-dynamic-modules:
205
+
206
+
---------------
207
+
Dynamic Modules
208
+
---------------
209
+
210
+
With Snakemake 9.0 and later, it is possible to load modules dynamically by providing the ``name`` keyword inside the module definition.
211
+
For example, by reading the module name from a config file or by iterating over several modules in a loop.
212
+
For this, the module name is not specified directly after the ``module`` keyword, but by specifying the ``name`` parameter.
213
+
214
+
215
+
.. code-block:: python
216
+
217
+
for module_name in ['module1', 'module2']:
218
+
module:
219
+
name: module_name
220
+
snakefile: f"{module_name}/Snakefile"
221
+
config: config[module_name]
222
+
223
+
use rule *from module_name as module_name*
224
+
225
+
.. note::
226
+
It is not allowed to specify the module name both after the ``module`` keyword and inside the module definition after the ``name`` parameter.
227
+
228
+
In the ``use rule`` statement, it is first checked if the module name (here, ``'module_name'``) corresponds to a loaded module. If yes, the rules are imported from the loaded module and an arbitrary alias can be provided after the ``as`` keyword.
229
+
230
+
If ``module_name`` was not registered as a module (as in the example above), the module name is resolved dynamically by searching the name in the current python variable scope. In the example, it resolves to ``'module1'`` and ``'module2'``.
231
+
Note that this means that if ``use rule`` is used with the optional ``as`` keyword inside the loop, the alias after ``as`` must be specified using a variable to ensure a one-to-one mapping between module names and their aliases. This can either be the same name variable (as in the above example) or a second variable (as in the example below).
232
+
233
+
In particular, it is not possible to modify the alias name in the ``use rule`` statement (e.g., writing directly ``use rule * from module as module_*`` is not allowed for dynamic modules).
234
+
235
+
.. code-block:: python
236
+
237
+
for module_name, alias inzip(['module1', 'module2'], ['module1_', 'module2_']):
0 commit comments