Пример #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))
Пример #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))