def load_snakemake_params(): global is_snakemake_child if PARAMETER_PASSING_ENVVAR not in os.environ: return is_snakemake_child = lambda: True import json, collections from snakemake.io import Namedlist import builtins options = json.load(open(os.environ[PARAMETER_PASSING_ENVVAR])) # Unset the parameter file var not to pass to children. del os.environ[PARAMETER_PASSING_ENVVAR] for varname, value in options.items(): if not isinstance(value, int): nl = Namedlist() for k, v in value: nl.append(v) if k is not None: nl.add_name(k) value = nl setattr(builtins, varname, value)
def recursive_format(ruleinfo): """Expand wildcards within ruleinfo This is not fully implemented! At this time, only `input` and `output` are expanded within `params`. """ args = {} for name in ['input', 'output']: attr = getattr(ruleinfo, name) if attr is None: continue nlist = Namedlist() for item in flatten(attr[0]): nlist.append(item) for key, item in attr[1].items(): nlist.append(item) nlist.add_name(key) args[name] = nlist for key, value in ruleinfo.params[1].items(): if not isinstance(value, str): continue value = partial_format(value, **args) ruleinfo.params[1][key] = value
def make_namedlist(self, item, first_lvl=True): if isinstance(item, list): return Namedlist(item) elif isinstance(item, dict): return Namedlist(fromdict={ k: self.make_namedlist(v, False) for k, v in item.items() }) elif isinstance(item, (str, int, float)): if first_lvl: return Namedlist([item]) else: return item return Namedlist([])
def listfiles(pattern, restriction=None, omit_value=None): """ Yield a tuple of existing filepaths for the given pattern. Wildcard values are yielded as the second tuple item. Arguments pattern -- a filepattern. Wildcards are specified in snakemake syntax, e.g. "{id}.txt" """ pattern = os.path.normpath(pattern) first_wildcard = re.search("{[^{]", pattern) if first_wildcard: dirname = os.path.dirname(pattern[:first_wildcard.start()]) if not dirname: dirname = "." else: dirname = os.path.dirname(pattern) pattern = re.compile(regex(pattern)) for dirpath, dirnames, filenames in os.walk(dirname): for f in chain(filenames, dirnames): if dirpath != ".": f = os.path.join(dirpath, f) match = re.match(pattern, f) if match and len(match.group()) == len(f): wildcards = Namedlist(fromdict=match.groupdict()) if restriction is not None: invalid = any(omit_value not in v and v != wildcards[k] for k, v in restriction.items()) if not invalid: yield f, wildcards else: yield f, wildcards
def _to_iofile(self, files): def cleanup(f): prefix = self.rule.workflow.default_remote_prefix # remove constraints and turn this into a plain string cleaned = strip_wildcard_constraints(f) modified_by = get_flag_value(f, PATH_MODIFIER_FLAG) if ( self.rule.workflow.default_remote_provider is not None and f.startswith(prefix) and not is_flagged(f, "local") ): cleaned = f[len(prefix) + 1 :] cleaned = IOFile(cleaned, rule=self.rule) else: cleaned = IOFile(AnnotatedString(cleaned), rule=self.rule) cleaned.clone_remote_object(f) if modified_by is not None: cleaned.flags[PATH_MODIFIER_FLAG] = modified_by return cleaned files = Namedlist(files, custom_map=cleanup) return files
def listfiles(pattern, restriction=None, omit_value=None): """Yield a tuple of existing filepaths for the given pattern. Wildcard values are yielded as the second tuple item. Args: pattern (str): a filepattern. Wildcards are specified in snakemake syntax, e.g. "{id}.txt" restriction (dict): restrict to wildcard values given in this dictionary omit_value (str): wildcard value to omit Yields: tuple: The next file matching the pattern, and the corresponding wildcards object """ pattern = os.path.normpath(pattern) first_wildcard = re.search("{[^{]", pattern) if first_wildcard: dirname = os.path.dirname(pattern[: first_wildcard.start()]) if not dirname: dirname = "." else: dirname = os.path.dirname(pattern) pattern = re.compile(regex(pattern)) for dirpath, dirnames, filenames in os.walk(dirname): for f in chain(filenames, dirnames): if dirpath != ".": f = os.path.normpath(os.path.join(dirpath, f)) match = re.match(pattern, f) if match: wildcards = Namedlist(fromdict=match.groupdict()) if restriction is not None: invalid = any( omit_value not in v and v != wildcards[k] for k, v in restriction.items() ) if not invalid: yield f, wildcards else: yield f, wildcards