Example #1
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 #2
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 #3
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 #4
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))