# Path: snakemakelib/utils.py
# def isoformat(s=None):
#     """Return isoformat date from string"""
#     if s is None:
#         return
#     # Assume YYMMDD format
#     if len(s) == 6:
#         (YY, MM, DD) = (s[0:2], s[2:4], s[4:6])
#         return date(int("20{YY}".format(YY=YY)), int(MM.lstrip("0")), int(DD)).isoformat()
# 
# Path: snakemakelib/log.py
# class LoggerManager(object):
#     __metaclass__ = Singleton
# 
#     _loggers = {}
#     _fmt = "%(asctime)s (%(levelname)s) %(name)s :  %(message)s"
#     _ch = logging.StreamHandler()
#     _formatter = logging.Formatter(_fmt)
#     _ch.setFormatter(_formatter)
#     _has_loaded_config = False
# 
#     def __init__(self, *args, **kwargs):
#         if not LoggerManager._has_loaded_config:
#             # Add snakemakelib root handler
#             LoggerManager._loggers['snakemakelib'] = logging.getLogger('snakemakelib')
#             LoggerManager._loggers['snakemakelib'].setLevel(logging.WARNING)
#             LoggerManager._loggers['snakemakelib'].addHandler(LoggerManager._ch)
#             self._load_config()
#             LoggerManager._has_loaded_config = True
# 
#     def _load_config(self):
#         conf = {}
#         if os.path.exists("logconf.yaml"):
#             with open ("logconf.yaml", "r") as fh:
#                 conf = yaml.load(fh)
#         else:
#             conf = self._load_sml_config()
#         if conf:
#             logging.config.dictConfig(conf)
#      
#     def _load_sml_config(self):
#         cfg = {}
#         for fn in [os.path.join(os.getenv("HOME"), ".smlconf.yaml"),
#                    os.path.join(os.curdir, "smlconf.yaml")]:
#             if (fn is None):
#                 continue
#             if not os.path.exists(fn):
#                 continue
#             with open(fn, "r") as fh:
#                 cfg_tmp = yaml.load(fh)
#             if cfg_tmp:
#                 cfg.update(cfg_tmp)
#         return cfg.get("logging", {})
# 
#     @staticmethod
#     def getLogger(name=None):
#         if not name:
#             smllogger = logging.getLogger()
#             return logging.getLogger()
#         elif name not in LoggerManager._loggers.keys():
#             LoggerManager._loggers[name] = logging.getLogger(str(name))
#             LoggerManager._loggers[name].addHandler(LoggerManager._ch)
#             return LoggerManager._loggers[name]
#         else:
#             logging.warn("Trying to get already existing logger")


import re
import os
from itertools import groupby
from snakemakelib.utils import isoformat
from snakemakelib.log import LoggerManager
    def basename_pattern(self):
        """Return the basename pattern

        Return the basename pattern, replacing ?P=
        expressions with the corresponding group expression

        """
        invmap = {v: k for k, v in self.re.groupindex.items()}
        remap = {k: "" for k, v in self.re.groupindex.items()}
        i = 1
        for m in re.finditer(r"\(?P<[A-Za-z0-9]+>([^\)]+)\)", self.pattern):
            remap[invmap[i]] = m.group(1)
            i += 1
        fmt = re.sub(r"\(?P=([A-Za-z0-9]+)\)", "P<\\1>{\\1})", self.pattern)
        return os.path.basename(fmt.format(**remap))

    @property
    def fmt(self):
        return self._fmt

    @property
    def allowed_keys(self):
        return list(set(self._required_keys + self._group_keys
                        + self._extra_keys))

    def _init_format(self):
        """Initialize formatting string. Find groups defined by (?P<GROUP>) as
        well as constant expressions and concatenate"""
        m = re.findall("(\(\?P[<=](\w+)>?|({sep})|(?:[\[\]A-Za-z0-9\-\+\_]+\))|([A-Za-z0-9]+))".format(sep=os.sep), self.pattern)
        fmtlist = []

# Path: snakemakelib/utils.py
# def isoformat(s=None):
#     """Return isoformat date from string"""
#     if s is None:
#         return
#     # Assume YYMMDD format
#     if len(s) == 6:
#         (YY, MM, DD) = (s[0:2], s[2:4], s[4:6])
#         return date(int("20{YY}".format(YY=YY)), int(MM.lstrip("0")), int(DD)).isoformat()
# 
# Path: snakemakelib/log.py
# class LoggerManager(object):
#     __metaclass__ = Singleton
# 
#     _loggers = {}
#     _fmt = "%(asctime)s (%(levelname)s) %(name)s :  %(message)s"
#     _ch = logging.StreamHandler()
#     _formatter = logging.Formatter(_fmt)
#     _ch.setFormatter(_formatter)
#     _has_loaded_config = False
# 
#     def __init__(self, *args, **kwargs):
#         if not LoggerManager._has_loaded_config:
#             # Add snakemakelib root handler
#             LoggerManager._loggers['snakemakelib'] = logging.getLogger('snakemakelib')
#             LoggerManager._loggers['snakemakelib'].setLevel(logging.WARNING)
#             LoggerManager._loggers['snakemakelib'].addHandler(LoggerManager._ch)
#             self._load_config()
#             LoggerManager._has_loaded_config = True
# 
#     def _load_config(self):
#         conf = {}
#         if os.path.exists("logconf.yaml"):
#             with open ("logconf.yaml", "r") as fh:
#                 conf = yaml.load(fh)
#         else:
#             conf = self._load_sml_config()
#         if conf:
#             logging.config.dictConfig(conf)
#      
#     def _load_sml_config(self):
#         cfg = {}
#         for fn in [os.path.join(os.getenv("HOME"), ".smlconf.yaml"),
#                    os.path.join(os.curdir, "smlconf.yaml")]:
#             if (fn is None):
#                 continue
#             if not os.path.exists(fn):
#                 continue
#             with open(fn, "r") as fh:
#                 cfg_tmp = yaml.load(fh)
#             if cfg_tmp:
#                 cfg.update(cfg_tmp)
#         return cfg.get("logging", {})
# 
#     @staticmethod
#     def getLogger(name=None):
#         if not name:
#             smllogger = logging.getLogger()
#             return logging.getLogger()
#         elif name not in LoggerManager._loggers.keys():
#             LoggerManager._loggers[name] = logging.getLogger(str(name))
#             LoggerManager._loggers[name].addHandler(LoggerManager._ch)
#             return LoggerManager._loggers[name]
#         else:
#             logging.warn("Trying to get already existing logger")


# Path: snakemakelib/bio/ngs/regexp.py
import re
import os
from itertools import groupby
from snakemakelib.utils import isoformat
from snakemakelib.log import LoggerManager

    def basename_pattern(self):
        """Return the basename pattern

        Return the basename pattern, replacing ?P=
        expressions with the corresponding group expression

        """
        invmap = {v: k for k, v in self.re.groupindex.items()}
        remap = {k: "" for k, v in self.re.groupindex.items()}
        i = 1
        for m in re.finditer(r"\(?P<[A-Za-z0-9]+>([^\)]+)\)", self.pattern):
            remap[invmap[i]] = m.group(1)
            i += 1
        fmt = re.sub(r"\(?P=([A-Za-z0-9]+)\)", "P<\\1>{\\1})", self.pattern)
        return os.path.basename(fmt.format(**remap))

    @property
    def fmt(self):
        return self._fmt

    @property
    def allowed_keys(self):
        return list(set(self._required_keys + self._group_keys
                        + self._extra_keys))

    def _init_format(self):
        """Initialize formatting string. Find groups defined by (?P<GROUP>) as
        well as constant expressions and concatenate"""
        m = re.findall("(\(\?P[<=](\w+)>?|({sep})|(?:[\[\]A-Za-z0-9\-\+\_]+\))|([A-Za-z0-9]+))".format(sep=os.sep), self.pattern)
        fmtlist = []

        for x in m: