Skip to content

Commit

Permalink
modify expand-class command to avoid conditional includes
Browse files Browse the repository at this point in the history
  • Loading branch information
mirpedrol committed Nov 6, 2024
1 parent f9a98ab commit 0444d4b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
6 changes: 1 addition & 5 deletions nf_class/subworkflows-template/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ workflow {{ component_name_underscore|upper }} {
{{ input_channels }}

main:

ch_versions = Channel.empty()

{{ run_module }}
ch_versions = ch_versions.mix({{ classname|upper }}.out.versions)
{{ run_module }}

emit:
{{ output_channels }}
Expand Down
35 changes: 21 additions & 14 deletions nf_class/subworkflows/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,8 @@ def _get_info_for_expanding(self) -> None:

# Generated code for include statements
self.include_statements = ""
first = True
for component in self.components:
if first:
start = "if"
first = False
else:
start = "else if"
self.include_statements += f"""{start} ( params.{self.classname} == "{component}" ) {{\n include {{ {component.replace("/", "_").upper()} as {self.classname.upper()} }} from '../../../modules/{self.org}/{component}/main'\n}} """
self.include_statements += f"""include {{ {component.replace("/", "_").upper()} }} from '../../../modules/{self.org}/{component}/main'\n"""

# Naming input channels
input_channels = []
Expand Down Expand Up @@ -150,19 +144,13 @@ def _get_info_for_expanding(self) -> None:
)
self.inputs_yml_swf: str = yaml.dump(inputs_yml_swf)

# Code for running the included module
self.run_module = f"{self.classname.upper()} ( {", ".join(input_channels)} )"

# Generate code for output channels
out_channel_names = []
for channel in self.outputs_yml:
out_channel_names.append(list(channel.keys())[0])
self.output_channels = ""
for out_channel in out_channel_names:
out = f"{self.classname.upper()}.out.{out_channel}"
if out_channel == "versions":
out = "ch_versions"
self.output_channels += f"\t{out_channel} = {out}\n"
self.output_channels += f"\t{out_channel} = ch_out_{out_channel}\n"

# Yml output channels
outputs_yml_swf: dict = {"output": []}
Expand All @@ -177,6 +165,25 @@ def _get_info_for_expanding(self) -> None:
)
self.outputs_yml_swf: str = yaml.dump(outputs_yml_swf)

# Code for running the included module
self.run_module = ""
for out_channel in out_channel_names:
self.run_module += f"\tdef ch_out_{out_channel} = Channel.empty()\n"
first = True
for component in self.components:
if first:
start = "if"
first = False
else:
start = "else if"
module_name = component.replace("/", "_").upper()
self.run_module += f"""\t{start} ( params.{self.classname} == "{component}" ) {{\n\t\t{module_name}( {", ".join(input_channels)} )\n"""
for out_channel in out_channel_names:
self.run_module += (
f"\t\tch_out_{out_channel} = ch_out_{out_channel}.mix({module_name}.out.{out_channel})\n"
)
self.run_module += "\t}\n"

# nf-tests
self._generate_nftest_code()

Expand Down

0 comments on commit 0444d4b

Please sign in to comment.