# 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 panda_to_latex(table, caption=""):
#     header = make_latex_table_header(len(table.keys()))
# 
#     def _panda_row_to_latex_row(row):
#         vals = map(str, [e for e in row[1]])
#         return " & ".join(vals) + " \\\\"
# 
#     rows = [_panda_row_to_latex_row(x) for x in table.iterrows()]
# 
#     _table_template = r"""
# \begin{table}[h]
#     \centering
#     ${header}
# 
#     % for row in rows:
#         ${row}
#     % endfor
#     \hline
#     \end{tabular}
#     \caption{${caption}}
# \end{table}
# """
# 
#     template = Template(_table_template)
#     latex_table = template.render(header=header, rows=rows, caption=caption)
#     return latex_table


import os
import rpy2.robjects as robjects
import rpy2.robjects.vectors as vectors
import pandas as pd
from bcbio.utils import safe_makedir, file_exists
from bipy.toolbox.reporting import LatexReport, panda_to_latex
from mako.template import Template

    def get_top_genes(self):
        final_info = {}
        for info, suffix in self.INFO_SUFFIXES.items():
            final_f = os.path.join(self._dir, self._comparison + suffix)
            if file_exists(final_f):
                final_info[info] = final_f

        # prefer to use the annotated file
        if "annotated" in final_info:
            top_table = self._get_top_by(final_info["annotated"])
        else:
            top_table = self._get_top_by(final_info["deseq"])

        return (top_table, "")

    def _get_top_by(self, in_file, by="padj"):
        if "annotated" in in_file:
            cols = ["id", "log2FoldChange", "padj", "symbol"]
        else:
            cols = ["id", "log2FoldChange", "padj"]
        if by not in cols:
            by = "padj"
        table = pd.read_table(in_file, header=0)
        # grab the top genes up to top_max genes
        sub_table = table[cols].sort(columns=by)[0:self._top_max]

        return sub_table



# 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 panda_to_latex(table, caption=""):
#     header = make_latex_table_header(len(table.keys()))
# 
#     def _panda_row_to_latex_row(row):
#         vals = map(str, [e for e in row[1]])
#         return " & ".join(vals) + " \\\\"
# 
#     rows = [_panda_row_to_latex_row(x) for x in table.iterrows()]
# 
#     _table_template = r"""
# \begin{table}[h]
#     \centering
#     ${header}
# 
#     % for row in rows:
#         ${row}
#     % endfor
#     \hline
#     \end{tabular}
#     \caption{${caption}}
# \end{table}
# """
# 
#     template = Template(_table_template)
#     latex_table = template.render(header=header, rows=rows, caption=caption)
#     return latex_table


# Path: bipy/toolbox/deseq.py
import os
import rpy2.robjects as robjects
import rpy2.robjects.vectors as vectors
import pandas as pd
from bcbio.utils import safe_makedir, file_exists
from bipy.toolbox.reporting import LatexReport, panda_to_latex
from mako.template import Template


    def get_top_genes(self):
        final_info = {}
        for info, suffix in self.INFO_SUFFIXES.items():
            final_f = os.path.join(self._dir, self._comparison + suffix)
            if file_exists(final_f):
                final_info[info] = final_f

        # prefer to use the annotated file
        if "annotated" in final_info:
            top_table = self._get_top_by(final_info["annotated"])
        else:
            top_table = self._get_top_by(final_info["deseq"])

        return (top_table, "")

    def _get_top_by(self, in_file, by="padj"):
        if "annotated" in in_file:
            cols = ["id", "log2FoldChange", "padj", "symbol"]
        else:
            cols = ["id", "log2FoldChange", "padj"]
        if by not in cols:
            by = "padj"
        table = pd.read_table(in_file, header=0)
        # grab the top genes up to top_max genes
        sub_table = table[cols].sort(columns=by)[0:self._top_max]

        return sub_table



class DeseqReport(LatexReport):