# Path: ratatosk/config.py
# def get_config():
#     return RatatoskConfigParser.instance()
# 
# Path: ratatosk/utils.py
# def make_fastq_links(targets, indir, outdir, fastq_suffix="001.fastq.gz", ssheet="SampleSheet.csv"):
#     """Given a set of targets and an output directory, create links
#     from targets (source raw data) to an output directory.
# 
#     :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(), 
#                            project_prefix=outdir, sample_prefix=os.path.join(outdir, os.path.relpath(tgt.prefix("sample"), indir)),
#                            sample_run_prefix=os.path.join(outdir, os.path.relpath(tgt.prefix("sample_run"), indir)))
#         newtargets.append(newsample)
#         # newtargets.append((tgt.sample_id(), 
#         #                    os.path.join(outdir, os.path.relpath(tgt.prefix("sample"), indir)),
#         #                    os.path.join(outdir, os.path.relpath(tgt.prefix("sample_run"), indir))))
#     return newtargets
# 
# def rreplace(s, old, new, occurrence):
#     li = s.rsplit(old, occurrence)
#     return new.join(li)
# 
# def determine_read_type(fn, read1_suffix, read2_suffix=None, suffix="(.fastq.gz$|.fastq$|.fq.gz$|.fq$|.sai$)"):
#     """Deduce if fn is first or second in read pair.
# 
#     :param fn: file name
#     :param read1_suffix: read1 suffix
#     :param read2_suffix: read2 suffix
#     :param suffix: suffix to use for paired files
#     """
#     parts = os.path.basename(fn).split(".")
# 
#     if parts[0].endswith(read1_suffix):
#         return 1
#     if read2_suffix:
#         if parts[0].endswith(str(read2_suffix)):
#             return 2
#     raise Exception("file name {} doesn't appear to have any read pair information in it's name".format(fn))


import os
import shutil
import unittest
import luigi
import logging
import yaml
import ratatosk.lib.align.bwa as BWA
import ratatosk.lib.tools.samtools as SAM
import ratatosk.lib.files.fastq as FASTQ
import ratatosk.lib.tools.picard as PICARD
import ratatosk.lib.tools.gatk as GATK
import ratatosk.lib.utils.cutadapt as CUTADAPT
import ratatosk.lib.tools.fastqc as FASTQC
import ratatosk.lib.files.external
from itertools import izip
from ratatosk.config import get_config
from ratatosk.utils import make_fastq_links, rreplace, determine_read_type

logging.basicConfig(level=logging.DEBUG)
sample = "P001_101_index3_TGACCA_L001"
bam = os.path.join(sample + ".bam")
localconf = "mock.yaml"
ratatosk_conf = os.path.join(os.path.dirname(__file__), os.pardir, "config", "ratatosk.yaml")

def setUpModule():
    global cnf

# Path: ratatosk/config.py
# def get_config():
#     return RatatoskConfigParser.instance()
# 
# Path: ratatosk/utils.py
# def make_fastq_links(targets, indir, outdir, fastq_suffix="001.fastq.gz", ssheet="SampleSheet.csv"):
#     """Given a set of targets and an output directory, create links
#     from targets (source raw data) to an output directory.
# 
#     :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(), 
#                            project_prefix=outdir, sample_prefix=os.path.join(outdir, os.path.relpath(tgt.prefix("sample"), indir)),
#                            sample_run_prefix=os.path.join(outdir, os.path.relpath(tgt.prefix("sample_run"), indir)))
#         newtargets.append(newsample)
#         # newtargets.append((tgt.sample_id(), 
#         #                    os.path.join(outdir, os.path.relpath(tgt.prefix("sample"), indir)),
#         #                    os.path.join(outdir, os.path.relpath(tgt.prefix("sample_run"), indir))))
#     return newtargets
# 
# def rreplace(s, old, new, occurrence):
#     li = s.rsplit(old, occurrence)
#     return new.join(li)
# 
# def determine_read_type(fn, read1_suffix, read2_suffix=None, suffix="(.fastq.gz$|.fastq$|.fq.gz$|.fq$|.sai$)"):
#     """Deduce if fn is first or second in read pair.
# 
#     :param fn: file name
#     :param read1_suffix: read1 suffix
#     :param read2_suffix: read2 suffix
#     :param suffix: suffix to use for paired files
#     """
#     parts = os.path.basename(fn).split(".")
# 
#     if parts[0].endswith(read1_suffix):
#         return 1
#     if read2_suffix:
#         if parts[0].endswith(str(read2_suffix)):
#             return 2
#     raise Exception("file name {} doesn't appear to have any read pair information in it's name".format(fn))


# Path: test/test_functions.py
import os
import shutil
import unittest
import luigi
import logging
import yaml
import ratatosk.lib.align.bwa as BWA
import ratatosk.lib.tools.samtools as SAM
import ratatosk.lib.files.fastq as FASTQ
import ratatosk.lib.tools.picard as PICARD
import ratatosk.lib.tools.gatk as GATK
import ratatosk.lib.utils.cutadapt as CUTADAPT
import ratatosk.lib.tools.fastqc as FASTQC
import ratatosk.lib.files.external
from itertools import izip
from ratatosk.config import get_config
from ratatosk.utils import make_fastq_links, rreplace, determine_read_type


logging.basicConfig(level=logging.DEBUG)
sample = "P001_101_index3_TGACCA_L001"
bam = os.path.join(sample + ".bam")
localconf = "mock.yaml"
ratatosk_conf = os.path.join(os.path.dirname(__file__), os.pardir, "config", "ratatosk.yaml")

def setUpModule():
    global cnf

    cnf = get_config()