# Path: ratatosk/job.py
# class InputJobTask(JobTask):
#     """Input job task. Should have as a parent task one of the tasks
#     in ratatosk.lib.files.external"""
#     def requires(self):
#         cls = self.parent()[0]
#         return cls(target=self.target)
#     
#     def run(self):
#         """No run should be defined"""
#         pass
# 
# class JobTask(BaseJobTask):
#     def job_runner(self):
#         return DefaultShellJobRunner()
# 
#     def args(self):
#         return []
# 
# Path: ratatosk/utils.py
# def rreplace(s, old, new, occurrence):
#     li = s.rsplit(old, occurrence)
#     return new.join(li)
# 
# Path: ratatosk/jobrunner.py
# class DefaultShellJobRunner(JobRunner):
#     """Default job runner to use for shell jobs."""
#     def __init__(self):
#         pass
# 
#     @staticmethod
#     def _fix_paths(job):
#         """Modelled after hadoop_jar.HadoopJarJobRunner._fix_paths.
#         """
#         tmp_files = []
#         args = []
#         for x in job.args():
#             if isinstance(x, luigi.LocalTarget): # input/output
#                 if x.exists(): # input
#                     args.append(x.path)
#                 else: # output
#                     ypath = x.path + '-luigi-tmp-%09d' % random.randrange(0, 1e10)
#                     y = luigi.LocalTarget(ypath)
#                     logger.info("Using temp path: {0} for path {1}".format(y.path, x.path))
#                     args.append(y.path)
#                     if job.add_suffix():
#                         x = luigi.LocalTarget(x.path + job.add_suffix())
#                         y = luigi.LocalTarget(y.path + job.add_suffix())
#                     tmp_files.append((y, x))
#             else:
#                 args.append(str(x))
#         return (tmp_files, args)
# 
#     @staticmethod
#     def _get_main(job):
#         """Add main program to command. Override this for programs
#         where main is given as a parameter, e.g. for GATK"""
#         return job.main()
# 
#     def _make_arglist(self, job):
#         """Make and return the arglist"""
#         if job.path():
#             exe = os.path.join(job.path(), job.exe())
#         else:
#             exe = job.exe()
#         if not exe:# or not os.path.exists(exe):
#             logger.error("Can't find executable: {0}, full path {1}".format(exe,
#                                                                             os.path.abspath(exe)))
#             raise Exception("executable does not exist")
#         arglist = [exe]
#         if job.main():
#             arglist.append(self._get_main(job))
#         if job.opts():
#             arglist += job.opts()
#         # Need to call self.__class__ since fix_paths overridden in
#         # DefaultGzShellJobRunner
#         (tmp_files, job_args) = self.__class__._fix_paths(job)
#         if not job.pipe:
#             arglist += job_args
#         return (arglist, tmp_files)
#         
#     def run_job(self, job):
#         (arglist, tmp_files) = self._make_arglist(job)
#         if job.pipe:
#             return (arglist, tmp_files)
#         cmd = ' '.join(arglist)
#         logger.info("\nJob runner '{0}';\n\trunning command '{1}'\n".format(self.__class__, cmd))
#         (stdout, stderr, returncode) = shell.exec_cmd(cmd, shell=True)
#         if returncode == 0:
#             logger.info("Shell job completed")
#             for a, b in tmp_files:
#                 logger.info("renaming {0} to {1}".format(a.path, b.path))
#                 # This is weird; using the example in luigi
#                 # (a.move(b)) doesn't work, and using just b.path
#                 # fails unless it contains a directory (e.g. './file'
#                 # works, 'file' doesn't)
#                 a.move(os.path.join(os.curdir, b.path))
#         else:
#             raise Exception("Job '{}' failed: \n{}".format(' '.join(arglist), " ".join([stderr])))
# 
# Path: ratatosk/log.py
# def get_logger():
#     logger = logging.getLogger('ratatosk-interface')
#     if not logger.handlers:
#         setup_logging()
#     return logger


import os
import luigi
import ratatosk.lib.files.input
from ratatosk.job import InputJobTask, JobTask
from ratatosk.utils import rreplace
from ratatosk.jobrunner import DefaultShellJobRunner
from ratatosk.log import get_logger
# Copyright (c) 2013 Per Unneberg
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""
Provide wrappers for  `samtools <http://samtools.sourceforge.net/>`_


Classes
-------
"""


# Path: ratatosk/job.py
# class InputJobTask(JobTask):
#     """Input job task. Should have as a parent task one of the tasks
#     in ratatosk.lib.files.external"""
#     def requires(self):
#         cls = self.parent()[0]
#         return cls(target=self.target)
#     
#     def run(self):
#         """No run should be defined"""
#         pass
# 
# class JobTask(BaseJobTask):
#     def job_runner(self):
#         return DefaultShellJobRunner()
# 
#     def args(self):
#         return []
# 
# Path: ratatosk/utils.py
# def rreplace(s, old, new, occurrence):
#     li = s.rsplit(old, occurrence)
#     return new.join(li)
# 
# Path: ratatosk/jobrunner.py
# class DefaultShellJobRunner(JobRunner):
#     """Default job runner to use for shell jobs."""
#     def __init__(self):
#         pass
# 
#     @staticmethod
#     def _fix_paths(job):
#         """Modelled after hadoop_jar.HadoopJarJobRunner._fix_paths.
#         """
#         tmp_files = []
#         args = []
#         for x in job.args():
#             if isinstance(x, luigi.LocalTarget): # input/output
#                 if x.exists(): # input
#                     args.append(x.path)
#                 else: # output
#                     ypath = x.path + '-luigi-tmp-%09d' % random.randrange(0, 1e10)
#                     y = luigi.LocalTarget(ypath)
#                     logger.info("Using temp path: {0} for path {1}".format(y.path, x.path))
#                     args.append(y.path)
#                     if job.add_suffix():
#                         x = luigi.LocalTarget(x.path + job.add_suffix())
#                         y = luigi.LocalTarget(y.path + job.add_suffix())
#                     tmp_files.append((y, x))
#             else:
#                 args.append(str(x))
#         return (tmp_files, args)
# 
#     @staticmethod
#     def _get_main(job):
#         """Add main program to command. Override this for programs
#         where main is given as a parameter, e.g. for GATK"""
#         return job.main()
# 
#     def _make_arglist(self, job):
#         """Make and return the arglist"""
#         if job.path():
#             exe = os.path.join(job.path(), job.exe())
#         else:
#             exe = job.exe()
#         if not exe:# or not os.path.exists(exe):
#             logger.error("Can't find executable: {0}, full path {1}".format(exe,
#                                                                             os.path.abspath(exe)))
#             raise Exception("executable does not exist")
#         arglist = [exe]
#         if job.main():
#             arglist.append(self._get_main(job))
#         if job.opts():
#             arglist += job.opts()
#         # Need to call self.__class__ since fix_paths overridden in
#         # DefaultGzShellJobRunner
#         (tmp_files, job_args) = self.__class__._fix_paths(job)
#         if not job.pipe:
#             arglist += job_args
#         return (arglist, tmp_files)
#         
#     def run_job(self, job):
#         (arglist, tmp_files) = self._make_arglist(job)
#         if job.pipe:
#             return (arglist, tmp_files)
#         cmd = ' '.join(arglist)
#         logger.info("\nJob runner '{0}';\n\trunning command '{1}'\n".format(self.__class__, cmd))
#         (stdout, stderr, returncode) = shell.exec_cmd(cmd, shell=True)
#         if returncode == 0:
#             logger.info("Shell job completed")
#             for a, b in tmp_files:
#                 logger.info("renaming {0} to {1}".format(a.path, b.path))
#                 # This is weird; using the example in luigi
#                 # (a.move(b)) doesn't work, and using just b.path
#                 # fails unless it contains a directory (e.g. './file'
#                 # works, 'file' doesn't)
#                 a.move(os.path.join(os.curdir, b.path))
#         else:
#             raise Exception("Job '{}' failed: \n{}".format(' '.join(arglist), " ".join([stderr])))
# 
# Path: ratatosk/log.py
# def get_logger():
#     logger = logging.getLogger('ratatosk-interface')
#     if not logger.handlers:
#         setup_logging()
#     return logger


# Path: ratatosk/lib/tools/samtools.py
import os
import luigi
import ratatosk.lib.files.input
from ratatosk.job import InputJobTask, JobTask
from ratatosk.utils import rreplace
from ratatosk.jobrunner import DefaultShellJobRunner
from ratatosk.log import get_logger

# Copyright (c) 2013 Per Unneberg
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""
Provide wrappers for  `samtools <http://samtools.sourceforge.net/>`_


Classes
-------
"""


logger = get_logger