From b842063c9332a347d7a6d17acc01e8238fff1c65 Mon Sep 17 00:00:00 2001 From: Brandon Fuller Date: Thu, 14 Nov 2024 13:54:04 -0500 Subject: [PATCH] Fix relative path issue with paths in config Make it so that all of the paths in config are absolute paths, which fixes the issue of relative paths breaking when running from a different directory, i.e. when running unit tests from the test/ directory --- lib/common.py | 8 ++++---- lib/patterns_targets.py | 6 +++--- workflows/chipseq/Snakefile | 4 +++- workflows/rnaseq/Snakefile | 3 ++- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/common.py b/lib/common.py index 829cc129..13286298 100644 --- a/lib/common.py +++ b/lib/common.py @@ -64,13 +64,13 @@ def resolve_config(config, workdir=None): if isinstance(config, str): config = yaml.load(open(config), Loader=yaml.FullLoader) - def rel(pth): - if workdir is None or os.path.isabs(pth): + def abs_path(pth): + if os.path.isabs(pth): return pth return os.path.join(workdir, pth) for key in PATH_KEYS: - if key in config: - config[key] = rel(config[key]) + if key in config and workdir: + config[key] = abs_path(config[key]) return config diff --git a/lib/patterns_targets.py b/lib/patterns_targets.py index 542d4116..7cca01f7 100644 --- a/lib/patterns_targets.py +++ b/lib/patterns_targets.py @@ -46,17 +46,17 @@ def __init__(self, config, patterns, workdir=None): as relative to `workdir` """ self.path = None - self.workdir = '.' + self.workdir = None if workdir is not None: - config = os.path.join(workdir, config) patterns = os.path.join(workdir, patterns) self.workdir = workdir if isinstance(config, str): self.path = config + config = os.path.join(workdir, config) self.config = common.load_config( - common.resolve_config(config, workdir)) + common.resolve_config(config, self.workdir)) stranded = self.config.get('stranded', None) self.stranded = None diff --git a/workflows/chipseq/Snakefile b/workflows/chipseq/Snakefile index 90c84d28..a182fe2b 100644 --- a/workflows/chipseq/Snakefile +++ b/workflows/chipseq/Snakefile @@ -10,6 +10,7 @@ import pybedtools from lib import common, utils, helpers, aligners, chipseq from lib.patterns_targets import ChIPSeqConfig from lib.utils import autobump, gb, hours +from pathlib import Path # ---------------------------------------------------------------------------- # @@ -30,7 +31,8 @@ helpers.preflight(config) c = ChIPSeqConfig( config, - config.get('patterns', 'config/chipseq_patterns.yaml') + config.get('patterns', 'config/chipseq_patterns.yaml'), + Path('.').resolve() ) SAMPLES = c.sampletable.iloc[:, 0].values diff --git a/workflows/rnaseq/Snakefile b/workflows/rnaseq/Snakefile index 1cde537a..f324b30b 100644 --- a/workflows/rnaseq/Snakefile +++ b/workflows/rnaseq/Snakefile @@ -9,6 +9,7 @@ import pandas as pd from lib import common, utils, helpers, aligners from lib.utils import autobump, gb, hours from lib.patterns_targets import RNASeqConfig +from pathlib import Path # ---------------------------------------------------------------------------- # @@ -27,7 +28,7 @@ include: '../references/Snakefile' # Verify configuration of config and sampletable files helpers.preflight(config) -c = RNASeqConfig(config, config.get('patterns', 'config/rnaseq_patterns.yaml')) +c = RNASeqConfig(config, config.get('patterns', 'config/rnaseq_patterns.yaml'), Path('.').resolve()) SAMPLES = c.sampletable.iloc[:, 0].values wildcard_constraints: