Skip to content

Commit

Permalink
Merge pull request #5 from Aratz/interpolate_environment
Browse files Browse the repository at this point in the history
Interpolate environment variables
  • Loading branch information
Aratz authored Aug 30, 2023
2 parents b2e8cd8 + aa153e0 commit b2ad387
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
25 changes: 23 additions & 2 deletions sequencing_report_service/nextflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def _construct_nf_param_list(self, runfolder_path):
defaults = {'runfolder_path': str(runfolder_path),
'runfolder_name': runfolder_path.name,
'current_year': datetime.datetime.now().year}
conf = configparser.ConfigParser(defaults=defaults, interpolation=configparser.ExtendedInterpolation())
conf = configparser.ConfigParser(
defaults=defaults,
interpolation=configparser.ExtendedInterpolation())
params_as_conf_dict = {'nextflow_config': self._raw_params}
conf.read_dict(params_as_conf_dict)

Expand All @@ -71,6 +73,25 @@ def _construct_nf_param_list(self, runfolder_path):

return lst

def _construct_environment(self, runfolder_path):
"""
Interpolates default values in the environment config
"""
defaults = {'runfolder_path': str(runfolder_path),
'runfolder_name': runfolder_path.name,
'current_year': datetime.datetime.now().year}
conf = configparser.ConfigParser(
defaults=defaults,
interpolation=configparser.ExtendedInterpolation())
conf.optionxform = str
env_as_conf_dict = {'environment': self._config_dict.get('environment')}
conf.read_dict(env_as_conf_dict)

return {
key: conf['environment'][key]
for key in env_as_conf_dict['environment']
}

def command(self, runfolder):
"""
Return a list containing the command to run with the specified runfolder
Expand All @@ -80,7 +101,7 @@ def command(self, runfolder):
if not isinstance(runfolder, Path):
runfolder = Path(runfolder)
cmd = self._cmd + self._construct_nf_param_list(runfolder)
env_config = self._config_dict.get('environment')
env_config = self._construct_environment(runfolder)
nf_command = {'command': cmd, 'environment': env_config}
log.debug("Generated command: %s", nf_command)
return nf_command
12 changes: 10 additions & 2 deletions tests/test_nextflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def test_command():
'nf_profile': 'singularity,snpseq',
'environment':
{
'NXF_TEMP': '/tmp_foo'
'NXF_TEMP': '/tmp_foo',
'TEST_RUNFOLDER': '${DEFAULT:runfolder_name}',
'TEST_PATH': '${DEFAULT:runfolder_path}',
'TEST_YEAR': '${DEFAULT:current_year}',
},
'parameters':
{'hello': '${DEFAULT:runfolder_path}',
Expand All @@ -30,7 +33,12 @@ def test_command():
'--name', 'runfolder',
]

assert result['environment'] == {'NXF_TEMP': '/tmp_foo'}
assert result['environment'] == {
'NXF_TEMP': '/tmp_foo',
'TEST_RUNFOLDER': 'runfolder',
'TEST_PATH': '/path/to/runfolder',
'TEST_YEAR': str(datetime.datetime.now().year),
}


def test_raises_on_no_parameters_section():
Expand Down

0 comments on commit b2ad387

Please sign in to comment.