Example #1
0
    def prettyPrint(self, include_header=False, transpose_data=False, column_limit=None, col_sep="\t"):
        """Returns a string method of the data and character order.

        include_header: include charcter order or not
        transpose_data: data as is (rows are positions) or transposed
            (rows are characters) to line it up with an alignment
        column_limit = int, maximum number of columns displayed
        col_sep = string, column separator
        """
        h = self.CharOrder
        d = self.Data
        if column_limit is None:
            max_col_idx = d.shape[1]
        else:
            max_col_idx = column_limit
        if include_header and not transpose_data:
            r = [h] + d.tolist()
        elif include_header and transpose_data:
            r = [[x] + y for x, y in zip(h, transpose(d).tolist())]
        elif transpose_data:
            r = transpose(d).tolist()
        else:
            r = d.tolist()
        # resize the result based on the column limit
        if column_limit is not None:
            r = [row[:column_limit] for row in r]
        # nicely format the table content, discard the header (already included)
        if r:
            new_header, formatted_res = formattedCells(r)
        else:
            formatted_res = r
        return "\n".join([col_sep.join(map(str, i)) for i in formatted_res])
Example #2
0
 def toRichHtmlTable(self,
                     row_cell_func=None,
                     header_cell_func=None,
                     element_formatters={},
                     merge_identical=True,
                     compact=True):
     """returns just the table html code.
     Arguments:
         - row_cell_func: callback function that formats the row values. Must
           take the row value and coordinates (row index, column index).
         - header_cell_func: callback function that formats the column headings
           must take the header label value and coordinate
         - element_formatters: a dictionary of specific callback funcs for
           formatting individual html table elements.
           e.g. {'table': lambda x: '<table border="1" class="docutils">'}
         - merge_identical: cells within a row are merged to one span.
     """
     formatted_table = self.array.tolist()
     header, formatted_table = table_format.formattedCells(
         formatted_table,
         self.Header,
         digits=self._digits,
         column_templates=self._column_templates,
         missing_data=self._missing_data)
     # but we strip the cell spacing
     header = [v.strip() for v in header]
     rows = [[c.strip() for c in r] for r in formatted_table]
     return table_format.rich_html(rows,
                                   row_cell_func=row_cell_func,
                                   header=header,
                                   header_cell_func=header_cell_func,
                                   element_formatters=element_formatters,
                                   compact=compact)
Example #3
0
 def toRichHtmlTable(self, row_cell_func=None, header_cell_func=None,
             element_formatters={}, merge_identical=True, compact=True):
     """returns just the table html code.
     Arguments:
         - row_cell_func: callback function that formats the row values. Must
           take the row value and coordinates (row index, column index).
         - header_cell_func: callback function that formats the column headings
           must take the header label value and coordinate
         - element_formatters: a dictionary of specific callback funcs for
           formatting individual html table elements.
           e.g. {'table': lambda x: '<table border="1" class="docutils">'}
         - merge_identical: cells within a row are merged to one span.
     """
     formatted_table = self.array.tolist()
     header, formatted_table = table_format.formattedCells(formatted_table,
                                 self.Header,
                                 digits = self._digits,
                                 column_templates = self._column_templates,
                                 missing_data = self._missing_data)
     # but we strip the cell spacing
     header = [v.strip() for v in header]
     rows = [[c.strip() for c in r] for r in formatted_table]
     return table_format.rich_html(rows, row_cell_func=row_cell_func,
                                   header=header,
                                   header_cell_func=header_cell_func,
                                   element_formatters=element_formatters,
                                   compact=compact)
Example #4
0
    def prettyPrint(self, include_header=False, transpose_data=False,\
        column_limit=None, col_sep='\t'):
        """Returns a string method of the data and character order.

        include_header: include charcter order or not
        transpose_data: data as is (rows are positions) or transposed
            (rows are characters) to line it up with an alignment
        column_limit = int, maximum number of columns displayed
        col_sep = string, column separator
        """
        h = self.CharOrder
        d = self.Data
        if column_limit is None:
            max_col_idx = d.shape[1]
        else:
            max_col_idx = column_limit
        if include_header and not transpose_data:
            r = [h]+d.tolist()
        elif include_header and transpose_data:
            r =[[x] + y for x,y in zip(h,transpose(d).tolist())]
        elif transpose_data:
            r = transpose(d).tolist()
        else:
            r = d.tolist()
        # resize the result based on the column limit
        if column_limit is not None:
            r = [row[:column_limit] for row in r]
        # nicely format the table content, discard the header (already included)
        if r:
            new_header, formatted_res = formattedCells(r)
        else:
            formatted_res = r
        return '\n'.join([col_sep.join(map(str,i)) for i in formatted_res])
Example #5
0
    def tostring(self, borders=True, sep=None, format='', **kwargs):
        """Return the table as a formatted string.
        
        Arguments:
            - format: possible formats are 'rest', 'latex', 'html', 'phylip',
              'bedgraph', or simple text (default).
            - sep: A string separator for delineating columns, e.g. ',' or '\t'.
              Overrides format.
        
        NOTE: If format is bedgraph, assumes that column headers are chrom,
        start, end, value. In that order!
        """
        if format.lower() == 'phylip':
            missing_data = "%.4f" % 0.0
        else:
            missing_data = self._missing_data

        # convert self to a 2D list
        formatted_table = self.array.tolist()
        if format != 'bedgraph':
            header, formatted_table = table_format.formattedCells(
                formatted_table,
                self.Header,
                digits=self._digits,
                column_templates=self._column_templates,
                missing_data=missing_data)
            args = (header, formatted_table, self.Title, self.Legend)
        if sep and format != 'bedgraph':
            return table_format.separatorFormat(*args + (sep, ))
        elif format == 'rest':
            return table_format.gridTableFormat(*args)
        elif format.endswith('tex'):
            caption = None
            if self.Title or self.Legend:
                caption = " ".join([self.Title or "", self.Legend or ""])
            return table_format.latex(formatted_table,
                                      header,
                                      caption=caption,
                                      **kwargs)
        elif format == 'html':
            rest = table_format.gridTableFormat(*args)
            return table_format.html(rest)
        elif format == 'phylip':
            # need to eliminate row identifiers
            formatted_table = [row[self._row_ids:] for row in formatted_table]
            header = header[self._row_ids:]
            return table_format.phylipMatrix(formatted_table, header)
        elif format == 'bedgraph':
            assert self.Shape[1] == 4, 'bedgraph format is for 4 column tables'
            # assuming that header order is chrom, start, end, val
            formatted_table = bedgraph.bedgraph(self.sorted().array.tolist(),
                                                **kwargs)
            return formatted_table
        else:
            return table_format.simpleFormat(
                *args + (self._max_width, self._row_ids, borders, self.Space))
Example #6
0
 def tostring(self, borders=True, sep=None, format='', **kwargs):
     """Return the table as a formatted string.
     
     Arguments:
         - format: possible formats are 'rest', 'latex', 'html', 'phylip',
           'bedgraph', or simple text (default).
         - sep: A string separator for delineating columns, e.g. ',' or '\t'.
           Overrides format.
     
     NOTE: If format is bedgraph, assumes that column headers are chrom,
     start, end, value. In that order!
     """
     if format.lower() == 'phylip':
         missing_data = "%.4f" % 0.0
     else:
         missing_data = self._missing_data
     
     # convert self to a 2D list
     formatted_table = self.array.tolist()
     if format != 'bedgraph':
         header, formatted_table = table_format.formattedCells(formatted_table,
                                 self.Header,
                                 digits = self._digits,
                                 column_templates = self._column_templates,
                                 missing_data = missing_data)
         args = (header, formatted_table, self.Title, self.Legend)
     if sep and format != 'bedgraph':
         return table_format.separatorFormat(*args + (sep,))
     elif format == 'rest':
         return table_format.gridTableFormat(*args)
     elif format.endswith('tex'):
         caption = None
         if self.Title or self.Legend:
             caption = " ".join([self.Title or "", self.Legend or ""])
         return table_format.latex(formatted_table, header,
                             caption = caption, **kwargs)
     elif format == 'html':
         rest = table_format.gridTableFormat(*args)
         return table_format.html(rest)
     elif format == 'phylip':
         # need to eliminate row identifiers
         formatted_table = [row[self._row_ids:] for row in formatted_table]
         header = header[self._row_ids:]
         return table_format.phylipMatrix(formatted_table, header)
     elif format == 'bedgraph':
         assert self.Shape[1] == 4, 'bedgraph format is for 4 column tables'
         # assuming that header order is chrom, start, end, val
         formatted_table = bedgraph.bedgraph(self.sorted().array.tolist(),
             **kwargs)
         return formatted_table
     else:
         return table_format.simpleFormat(*args + (self._max_width,
                             self._row_ids, borders, self.Space))
Example #7
0
    def array_repr(self, a):
        if len(a.shape) == 1:
            heading = [str(n) for n in self.names[0]]
            a = a[numpy.newaxis, :]
        elif len(a.shape) == 2:
            heading = [''] + [str(n) for n in self.names[1]]
            a = [[str(name)] + list(row)
                 for (name, row) in zip(self.names[0], a)]
        else:
            return '%s dimensional %s' % (len(self.names), type(self).__name__)

        formatted = table.formattedCells(rows=a, header=heading)
        return str(table.simpleFormat(formatted[0], formatted[1], space=4))
Example #8
0
 def array_repr(self, a):
     if len(a.shape) == 1:
         heading = [str(n) for n in self.names[0]]
         a = a[numpy.newaxis, :]
     elif len(a.shape) == 2:
         heading = [''] + [str(n) for n in self.names[1]]
         a = [[str(name)] + list(row) for (name, row) in zip(self.names[0], a)]
     else:
         return '%s dimensional %s' % (
             len(self.names), type(self).__name__)
     
     formatted = table.formattedCells(rows=a, header=heading)
     return str(table.simpleFormat(formatted[0], formatted[1], space=4))