Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
"""
try:
import rpy2.robjects as robjects
from rpy2.rlike.container import TaggedList
from rpy2.rinterface import RNULLType
except ImportError:
raise ValueError(
"Python 3 package rpy2 needs to be installed to use"
"the R function.")
activate_R()
# translate Namedlists into rpy2's TaggedList to have named lists in R
for key in kwargs:
value = kwargs[key]
if isinstance(value, Namedlist):
kwargs[key] = TaggedList([y for x, y in value.allitems()],
[x for x, y in value.allitems()])
code = snake_format(textwrap.dedent(code), stepout=2)
# wrap code in function to preserve clean global env and execute
rval = robjects.r("function({}){{ {} }}"
"".format(",".join(kwargs), code))(**kwargs)
# Reduce vectors of length 1 to scalar, as implicit in R.
if isinstance(rval, RNULLType):
rval = None
if rval and len(rval) == 1:
return rval[0]
return rval
class InheritanceException(RuleException):
"""Exception raised for errors during rule inheritance"""
def __init__(self, msg, rule, parent,
include=None, lineno=None, snakefile=None):
message = "'{}' when deriving {} from {}".format(
msg, rule.name, parent)
super().__init__(message=message,
include=include,
lineno=lineno,
snakefile=snakefile,
rule=rule)
class NamedList(_Namedlist):
"""Extended version of Snakemake's :class:`~snakemake.io.Namedlist`
- Fixes array assignment operator:
Writing a field via ``[]`` operator updates the value accessed
via ``.`` operator.
- Adds ``fromtuple`` to constructor:
Builds from Snakemake's typial ``(args, kwargs)`` tuples as
present in ruleinfo structures.
- Adds `update_tuple` method:
Updates values in ``(args,kwargs)`` tuples as present in
:class:`ruleinfo` structures.
"""
def get_names(self, *args, **kwargs):
"""Export ``get_names`` as public func"""
return self._get_names(*args, *kwargs)