# Path: ratatosk/job.py
# class BaseJobTask(luigi.Task):
# class JobTask(BaseJobTask):
# class InputJobTask(JobTask):
# class JobWrapperTask(JobTask):
# class NullJobTask(JobTask):
# class GenericWrapperTask(JobWrapperTask):
# class InputPath(InputJobTask):
# class PipedTask(JobTask):
# class PipelineTask(JobWrapperTask):
# class PrintConfig(JobTask):
#     def __init__(self, *args, **kwargs):
#     def _register_parent_task(self, parents):
#     def _update_config(self, config, param_values_dict, disable_parent_task_update=False, *args, **kwargs):
#     def path(self):
#     def exe(self):
#     def main(self):
#     def opts(self):
#     def args(self):
#     def threads(self):
#     def sfx(self, index=0):
#     def adl(self, index=0):
#     def dil(self, index=0):
#     def lab(self):
#     def max_memory(self):
#     def add_suffix(self):
#     def init_local(self):
#     def run(self):
#     def parent(self):
#     def output(self):
#     def requires(self):
#     def complete(self):
#     def get_param_default(self, k):
#     def target_iterator(self):
#     def source(self):
#     def _make_source_file_name(self, parent_cls, diff_label=None, add_label=None):
#     def job_runner(self):
#     def args(self):
#     def requires(self):
#     def run(self):
#     def complete(self):
#     def run(self):
#     def run(self):
#     def requires(self):
#     def output(self):
#     def job_runner(self):
#     def args(self):
#     def requires(self):
#     def output(self):
#     def run(self):
# def name_prefix():
# def generic_target_generator(target_generator_infile=None, **kwargs):
# 
# Path: ratatosk/lib/tools/samtools.py
# class SamToBam(SamtoolsJobTask):
#     sub_executable = "view"
#     options = luigi.Parameter(default=("-bSh",), is_list=True)
#     parent_task = luigi.Parameter(default=("ratatosk.lib.tools.samtools.InputSamFile", ), is_list=True)
#     suffix = luigi.Parameter(default=".bam")
# 
#     def args(self):
#         retval = [self.input()[0], ">", self.output()]
#         if self.pipe:
#             return retval + ["-"]
#         return retval
# 
# Path: ratatosk/utils.py
# def rreplace(s, old, new, occurrence):
#     li = s.rsplit(old, occurrence)
#     return new.join(li)
# 
# def fullclassname(o):
#     return o.__module__ + "." + o.__name__
# 
# 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 re
import luigi
import ratatosk.lib.files.input
from itertools import izip
from ratatosk.job import JobTask, JobWrapperTask, DefaultShellJobRunner, PipedTask
from ratatosk.lib.tools.samtools import SamToBam
from ratatosk.utils import rreplace, fullclassname, determine_read_type
            from ratatosk import backend
    def job_runner(self):
        return BwaJobRunner()

    def output(self):
        return luigi.LocalTarget(self.target)


class Aln(BwaJobTask):
    sub_executable = "aln"
    parent_task = luigi.Parameter(default=("ratatosk.lib.align.bwa.InputFastqFile",))
    suffix = luigi.Parameter(default=".sai")
    read1_suffix = luigi.Parameter(default="_R1_001")
    read2_suffix = luigi.Parameter(default="_R2_001")
    is_read1 = True
    can_multi_thread = True

    def opts(self):
        # Threads is an option so handle it here
        retval = list(self.options)
        return retval + ['-t {}'.format(str(self.threads()))]

    def requires(self):
        cls = self.parent()[0]
        source = self.source()[0]
        # Ugly hack for 1 -> 2 dependency: works but should be dealt with otherwise
        if str(fullclassname(cls)) in ["ratatosk.lib.utils.misc.ResyncMates"]:
            rt = determine_read_type(source, self.read1_suffix, self.read2_suffix)
            if rt == 1:
                self.is_read1 = True
                fq1 = source

# Path: ratatosk/job.py
# class BaseJobTask(luigi.Task):
# class JobTask(BaseJobTask):
# class InputJobTask(JobTask):
# class JobWrapperTask(JobTask):
# class NullJobTask(JobTask):
# class GenericWrapperTask(JobWrapperTask):
# class InputPath(InputJobTask):
# class PipedTask(JobTask):
# class PipelineTask(JobWrapperTask):
# class PrintConfig(JobTask):
#     def __init__(self, *args, **kwargs):
#     def _register_parent_task(self, parents):
#     def _update_config(self, config, param_values_dict, disable_parent_task_update=False, *args, **kwargs):
#     def path(self):
#     def exe(self):
#     def main(self):
#     def opts(self):
#     def args(self):
#     def threads(self):
#     def sfx(self, index=0):
#     def adl(self, index=0):
#     def dil(self, index=0):
#     def lab(self):
#     def max_memory(self):
#     def add_suffix(self):
#     def init_local(self):
#     def run(self):
#     def parent(self):
#     def output(self):
#     def requires(self):
#     def complete(self):
#     def get_param_default(self, k):
#     def target_iterator(self):
#     def source(self):
#     def _make_source_file_name(self, parent_cls, diff_label=None, add_label=None):
#     def job_runner(self):
#     def args(self):
#     def requires(self):
#     def run(self):
#     def complete(self):
#     def run(self):
#     def run(self):
#     def requires(self):
#     def output(self):
#     def job_runner(self):
#     def args(self):
#     def requires(self):
#     def output(self):
#     def run(self):
# def name_prefix():
# def generic_target_generator(target_generator_infile=None, **kwargs):
# 
# Path: ratatosk/lib/tools/samtools.py
# class SamToBam(SamtoolsJobTask):
#     sub_executable = "view"
#     options = luigi.Parameter(default=("-bSh",), is_list=True)
#     parent_task = luigi.Parameter(default=("ratatosk.lib.tools.samtools.InputSamFile", ), is_list=True)
#     suffix = luigi.Parameter(default=".bam")
# 
#     def args(self):
#         retval = [self.input()[0], ">", self.output()]
#         if self.pipe:
#             return retval + ["-"]
#         return retval
# 
# Path: ratatosk/utils.py
# def rreplace(s, old, new, occurrence):
#     li = s.rsplit(old, occurrence)
#     return new.join(li)
# 
# def fullclassname(o):
#     return o.__module__ + "." + o.__name__
# 
# 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: ratatosk/lib/align/bwa.py
import re
import luigi
import ratatosk.lib.files.input
from itertools import izip
from ratatosk.job import JobTask, JobWrapperTask, DefaultShellJobRunner, PipedTask
from ratatosk.lib.tools.samtools import SamToBam
from ratatosk.utils import rreplace, fullclassname, determine_read_type
            from ratatosk import backend

    def job_runner(self):
        return BwaJobRunner()

    def output(self):
        return luigi.LocalTarget(self.target)


class Aln(BwaJobTask):
    sub_executable = "aln"
    parent_task = luigi.Parameter(default=("ratatosk.lib.align.bwa.InputFastqFile",))
    suffix = luigi.Parameter(default=".sai")
    read1_suffix = luigi.Parameter(default="_R1_001")
    read2_suffix = luigi.Parameter(default="_R2_001")
    is_read1 = True
    can_multi_thread = True

    def opts(self):
        # Threads is an option so handle it here
        retval = list(self.options)
        return retval + ['-t {}'.format(str(self.threads()))]

    def requires(self):
        cls = self.parent()[0]
        source = self.source()[0]
        # Ugly hack for 1 -> 2 dependency: works but should be dealt with otherwise
        if str(fullclassname(cls)) in ["ratatosk.lib.utils.misc.ResyncMates"]:
            rt = determine_read_type(source, self.read1_suffix, self.read2_suffix)
            if rt == 1:
                self.is_read1 = True
                fq1 = source

                fq2 = rreplace(source, self.read1_suffix, self.read2_suffix, 1)