# Path: bipy/utils.py
# def flatten(l):
#     for el in l:
#         if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
#             for sub in flatten(el):
#                 yield sub
#         else:
#             yield el
# 
# def remove_suffix(filename):
#     filename, extension = os.path.splitext(filename)
#     return filename
# 
# def is_pair(arg):
#     """
#     check if 'arg' is a two-item sequence
# 
#     """
#     return is_sequence(arg) and len(arg) == 2
# 
# Path: bipy/toolbox/reporting.py
# class LatexReport(object):
#     """Abstract class for generating Latex reports. Inherit from this
#     and implement template and generate_report"""
#     __metaclass__ = abc.ABCMeta
# 
#     @abc.abstractproperty
#     def template(self):
#         return
# 
#     @abc.abstractmethod
#     def generate_report(self, *args, **kwargs):
#         """Generate the Latex output using the template"""
#         return
# 
# def safe_latex(to_fix):
#     """Escape characters that make LaTeX unhappy.
#     Lifted from Brad Chapman (bcbio)
#     """
#     chars = ["%", "_", "&", "#"]
#     for char in chars:
#         to_fix = to_fix.replace(char, "\\%s" % char)
#     return to_fix
# 
# Path: bipy/pipeline/stages.py
# class AbstractStage(object):
#     """
#     Wrapper around a stage for use with the pipeline manager. Subclasses should
#     implement their own __init__ in addition to calling the super
#     __init__. __init__ should parse the appropriate information from the config
#     dict and save it in the objects state. __call__ should be implemented as a
#     pure function with no closures.
# 
#     """
# 
#     stage = "abstract"
# 
#     def __init__(self, config):
#         self.config = config
#         self._validate_config()
# 
#     def _start_message(self, in_file, **kwargs):
#         if kwargs:
#             logger.info("Starting %s on %s with arguments %s." % (self.stage,
#                                                                   in_file,
#                                                                   kwargs))
#         else:
#             logger.info("Starting %s on %s." % (self.stage, in_file))
# 
#     def _end_message(self, in_file):
#         logger.info("%s complete on %s." % (self.stage, in_file))
# 
#     def _check_run(self, in_file):
#         if not file_exists(in_file):
#             raise IOError("%s not found." % (in_file))
# 
#     def __call__(self, in_file):
#         pass
# 
#     def _validate_config(self):
#         if "stage" not in self.config:
#             raise ValueError('Could not find "stage" in the config file.')


import subprocess
import os
import logging
import abc
import sh
import zipfile
from bipy.utils import flatten, remove_suffix, is_pair
from bcbio.utils import safe_makedir, file_exists
from mako.template import Template
from bipy.toolbox.reporting import LatexReport, safe_latex
from bipy.pipeline.stages import AbstractStage
from bcbio.log import logger, setup_local_logging
from bcbio.provenance import do
        \centering
        \begin{tabular}{|p{8cm}rrp{4cm}|}
        \hline
        Sequence & Count & Percent & Match \\\
        \hline
        % for seq, count, percent, match in overrep:
            \texttt{${seq}} & ${count} & ${"%.2f" % float(percent)} & ${match} \\\
        % endfor
        \hline
        \end{tabular}
        \caption{Overrepresented read sequences}
        \end{table}
    % endif
% endif
"""


class RNASeqFastQCReport(FastQCReport):
    """FastQCreport class for outputting information from RNASeq experiments"""

    CAPTIONS = {"per_base_quality.png": "",
                "per_base_sequence_content.png": "",
                "per_sequence_gc_content.png": "",
                "kmer_profiles.png": "",
                "duplication_levels.png": "",
                "per_bases_n_content.png": "",
                "per_sequence_quality.png": "",
                "sequence_length_distribution.png": ""}



# Path: bipy/utils.py
# def flatten(l):
#     for el in l:
#         if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
#             for sub in flatten(el):
#                 yield sub
#         else:
#             yield el
# 
# def remove_suffix(filename):
#     filename, extension = os.path.splitext(filename)
#     return filename
# 
# def is_pair(arg):
#     """
#     check if 'arg' is a two-item sequence
# 
#     """
#     return is_sequence(arg) and len(arg) == 2
# 
# Path: bipy/toolbox/reporting.py
# class LatexReport(object):
#     """Abstract class for generating Latex reports. Inherit from this
#     and implement template and generate_report"""
#     __metaclass__ = abc.ABCMeta
# 
#     @abc.abstractproperty
#     def template(self):
#         return
# 
#     @abc.abstractmethod
#     def generate_report(self, *args, **kwargs):
#         """Generate the Latex output using the template"""
#         return
# 
# def safe_latex(to_fix):
#     """Escape characters that make LaTeX unhappy.
#     Lifted from Brad Chapman (bcbio)
#     """
#     chars = ["%", "_", "&", "#"]
#     for char in chars:
#         to_fix = to_fix.replace(char, "\\%s" % char)
#     return to_fix
# 
# Path: bipy/pipeline/stages.py
# class AbstractStage(object):
#     """
#     Wrapper around a stage for use with the pipeline manager. Subclasses should
#     implement their own __init__ in addition to calling the super
#     __init__. __init__ should parse the appropriate information from the config
#     dict and save it in the objects state. __call__ should be implemented as a
#     pure function with no closures.
# 
#     """
# 
#     stage = "abstract"
# 
#     def __init__(self, config):
#         self.config = config
#         self._validate_config()
# 
#     def _start_message(self, in_file, **kwargs):
#         if kwargs:
#             logger.info("Starting %s on %s with arguments %s." % (self.stage,
#                                                                   in_file,
#                                                                   kwargs))
#         else:
#             logger.info("Starting %s on %s." % (self.stage, in_file))
# 
#     def _end_message(self, in_file):
#         logger.info("%s complete on %s." % (self.stage, in_file))
# 
#     def _check_run(self, in_file):
#         if not file_exists(in_file):
#             raise IOError("%s not found." % (in_file))
# 
#     def __call__(self, in_file):
#         pass
# 
#     def _validate_config(self):
#         if "stage" not in self.config:
#             raise ValueError('Could not find "stage" in the config file.')


# Path: bipy/toolbox/fastqc.py
import subprocess
import os
import logging
import abc
import sh
import zipfile
from bipy.utils import flatten, remove_suffix, is_pair
from bcbio.utils import safe_makedir, file_exists
from mako.template import Template
from bipy.toolbox.reporting import LatexReport, safe_latex
from bipy.pipeline.stages import AbstractStage
from bcbio.log import logger, setup_local_logging
from bcbio.provenance import do

        \centering
        \begin{tabular}{|p{8cm}rrp{4cm}|}
        \hline
        Sequence & Count & Percent & Match \\\
        \hline
        % for seq, count, percent, match in overrep:
            \texttt{${seq}} & ${count} & ${"%.2f" % float(percent)} & ${match} \\\
        % endfor
        \hline
        \end{tabular}
        \caption{Overrepresented read sequences}
        \end{table}
    % endif
% endif
"""


class RNASeqFastQCReport(FastQCReport):
    """FastQCreport class for outputting information from RNASeq experiments"""

    CAPTIONS = {"per_base_quality.png": "",
                "per_base_sequence_content.png": "",
                "per_sequence_gc_content.png": "",
                "kmer_profiles.png": "",
                "duplication_levels.png": "",
                "per_bases_n_content.png": "",
                "per_sequence_quality.png": "",
                "sequence_length_distribution.png": ""}



class FastQC(AbstractStage):