# Path: ratatosk/log.py
# def get_logger():
#     logger = logging.getLogger('ratatosk-interface')
#     if not logger.handlers:
#         setup_logging()
#     return logger
# 
# Path: ratatosk/experiment.py
# class Sample(ISample):
#     """A class describing a sample. Provides placeholders for
#     project_id, sample_id, project_prefix, sample_prefix, and
#     sample_run_prefix.
# 
#     """
#     _levels = {}
#     _sample_id = None
#     _project_id = None
#     _project_prefix = None
#     _sample_prefix = None
#     _sample_run_prefix = None
# 
#     def __init__(self, project_id=None, sample_id=None, project_prefix=None, sample_prefix=None, sample_run_prefix=None, **kwargs):
#         self._project_id = project_id
#         self._sample_id = sample_id
#         self._prefix = {'project' : project_prefix,
#                         'sample' : sample_prefix,
#                         'sample_run' : sample_run_prefix
#                         }
#     def __repr__(self):
#         return "{} (".format(self.__class__) + ",".join([str(self.project_id()), str(self.sample_id()), str(self.prefix("project")), str(self.prefix("sample")), str(self.prefix("sample_run"))]) + ")"
#     def project_id(self):
#         return self._project_id
#     def sample_id(self):
#         return self._sample_id
#     def prefix(self, group="sample"):
#         if not group in ["project", "sample", "sample_run"]:
#             raise ValueError, "No such prefix level '{}'".format(group)
#         return self._prefix[group]
#     def path(self, group="sample"):
#         if not group in ["project", "sample", "sample_run"]:
#             raise ValueError, "No such prefix level '{}'".format(group)
#         d = os.path.dirname(self._prefix[group])
#         if not d:
#             d = os.curdir
#         return d
#     def levels(self):
#         return
#     def add_level(self, level):
#         return
#     def get_level(self, level):
#         return


import os
import re
import collections
import time
import luigi
import glob
import itertools
import logging
from datetime import datetime
from ratatosk.log import get_logger
from ratatosk.experiment import Sample

    :param targets: list of :class:`ratatosk.experiment.ISample` objects
    :param outdir: (top) output directory
    :param fastq_suffix: fastq suffix
    :param ssheet: sample sheet name

    :returns: new targets list with updated output directory
    """
    newtargets = []
    for tgt in targets:
        fastq = glob.glob("{}*{}".format(tgt.prefix("sample_run"), fastq_suffix))
        if len(fastq) == 0:
            logger.warn("No fastq files for prefix {} in {}".format(tgt.prefix("sample_run"), "make_fastq_links"))
        for f in fastq:
            newpath = os.path.join(outdir, os.path.relpath(f, indir))
            if not os.path.exists(os.path.dirname(newpath)):
                logger.info("Making directories to {}".format(os.path.dirname(newpath)))
                os.makedirs(os.path.dirname(newpath))
                if not os.path.exists(os.path.join(os.path.dirname(newpath), ssheet)):
                    try:
                        os.symlink(os.path.abspath(os.path.join(os.path.dirname(f), ssheet)), 
                                   os.path.join(os.path.dirname(newpath), ssheet))
                    except:
                        logger.warn("No sample sheet found for {}".format())
                        
            if not os.path.exists(newpath):
                logger.info("Linking {} -> {}".format(newpath, os.path.abspath(f)))
                os.symlink(os.path.abspath(f), newpath)
            if not os.path.lexists(os.path.join(os.path.dirname(newpath), ssheet)) and os.path.exists(os.path.abspath(os.path.join(os.path.dirname(f), ssheet))):
                os.symlink(os.path.abspath(os.path.join(os.path.dirname(f), ssheet)), os.path.join(os.path.dirname(newpath), ssheet))

# Path: ratatosk/log.py
# def get_logger():
#     logger = logging.getLogger('ratatosk-interface')
#     if not logger.handlers:
#         setup_logging()
#     return logger
# 
# Path: ratatosk/experiment.py
# class Sample(ISample):
#     """A class describing a sample. Provides placeholders for
#     project_id, sample_id, project_prefix, sample_prefix, and
#     sample_run_prefix.
# 
#     """
#     _levels = {}
#     _sample_id = None
#     _project_id = None
#     _project_prefix = None
#     _sample_prefix = None
#     _sample_run_prefix = None
# 
#     def __init__(self, project_id=None, sample_id=None, project_prefix=None, sample_prefix=None, sample_run_prefix=None, **kwargs):
#         self._project_id = project_id
#         self._sample_id = sample_id
#         self._prefix = {'project' : project_prefix,
#                         'sample' : sample_prefix,
#                         'sample_run' : sample_run_prefix
#                         }
#     def __repr__(self):
#         return "{} (".format(self.__class__) + ",".join([str(self.project_id()), str(self.sample_id()), str(self.prefix("project")), str(self.prefix("sample")), str(self.prefix("sample_run"))]) + ")"
#     def project_id(self):
#         return self._project_id
#     def sample_id(self):
#         return self._sample_id
#     def prefix(self, group="sample"):
#         if not group in ["project", "sample", "sample_run"]:
#             raise ValueError, "No such prefix level '{}'".format(group)
#         return self._prefix[group]
#     def path(self, group="sample"):
#         if not group in ["project", "sample", "sample_run"]:
#             raise ValueError, "No such prefix level '{}'".format(group)
#         d = os.path.dirname(self._prefix[group])
#         if not d:
#             d = os.curdir
#         return d
#     def levels(self):
#         return
#     def add_level(self, level):
#         return
#     def get_level(self, level):
#         return


# Path: ratatosk/utils.py
import os
import re
import collections
import time
import luigi
import glob
import itertools
import logging
from datetime import datetime
from ratatosk.log import get_logger
from ratatosk.experiment import Sample


    :param targets: list of :class:`ratatosk.experiment.ISample` objects
    :param outdir: (top) output directory
    :param fastq_suffix: fastq suffix
    :param ssheet: sample sheet name

    :returns: new targets list with updated output directory
    """
    newtargets = []
    for tgt in targets:
        fastq = glob.glob("{}*{}".format(tgt.prefix("sample_run"), fastq_suffix))
        if len(fastq) == 0:
            logger.warn("No fastq files for prefix {} in {}".format(tgt.prefix("sample_run"), "make_fastq_links"))
        for f in fastq:
            newpath = os.path.join(outdir, os.path.relpath(f, indir))
            if not os.path.exists(os.path.dirname(newpath)):
                logger.info("Making directories to {}".format(os.path.dirname(newpath)))
                os.makedirs(os.path.dirname(newpath))
                if not os.path.exists(os.path.join(os.path.dirname(newpath), ssheet)):
                    try:
                        os.symlink(os.path.abspath(os.path.join(os.path.dirname(f), ssheet)), 
                                   os.path.join(os.path.dirname(newpath), ssheet))
                    except:
                        logger.warn("No sample sheet found for {}".format())
                        
            if not os.path.exists(newpath):
                logger.info("Linking {} -> {}".format(newpath, os.path.abspath(f)))
                os.symlink(os.path.abspath(f), newpath)
            if not os.path.lexists(os.path.join(os.path.dirname(newpath), ssheet)) and os.path.exists(os.path.abspath(os.path.join(os.path.dirname(f), ssheet))):
                os.symlink(os.path.abspath(os.path.join(os.path.dirname(f), ssheet)), os.path.join(os.path.dirname(newpath), ssheet))

        newsample = Sample(project_id=tgt.project_id(), sample_id=tgt.sample_id(), 