def test_grid_table_format(self): """test the table grid_table_format method""" from cogent3.format.table import grid_table_format formatted_grid = grid_table_format( self.t6_header, self.t6_rows, title="Test", legend="Units" ) self.assertEqual(len(formatted_grid.split("\n")), len(self.t6_rows) * 2 + 7) formatted_grid = grid_table_format( self.t6_header, self.t6_rows, title="Really Long Title", legend="Extra Long Legend", ) self.assertEqual(len(formatted_grid.split("\n")), len(self.t6_rows) * 2 + 7 + 2)
def to_string(self, format="", borders=True, sep=None, center=False, **kwargs): """Return the table as a formatted string. Parameters ---------- format possible formats are 'rest'/'rst', 'markdown'/'md', 'latex', 'html', 'phylip', 'bedgraph', 'csv', 'tsv', or 'simple' (default). sep A string separator for delineating columns, e.g. ',' or '\t'. Overrides format. center content is centered in the column, default is right justified 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 if format.lower() in ("tsv", "csv"): sep = sep or {"tsv": "\t", "csv": ","}[format.lower()] format = "" # convert self to a 2D list formatted_table = self.array.tolist() if format != "bedgraph": header, formatted_table = table_format.formatted_cells( formatted_table, self.header, digits=self._digits, column_templates=self._column_templates, missing_data=missing_data, center=center, ) args = (header, formatted_table, self.title, self.legend) if sep and format != "bedgraph": return table_format.separator_format(*args + (sep, )) elif format in ("rest", "rst"): return table_format.grid_table_format(*args) elif format in ("markdown", "md"): return table_format.markdown(header, formatted_table, **kwargs) 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": return self.to_rich_html(**kwargs) 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.phylip_matrix(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.simple_format( *args + (self._max_width, self._row_ids, borders, self.space))