def _write_pdb_to_stringio(self, cys_cys_atomidx_set=None, disulfide_conect=True, noter=False, **kwargs): stringio_file = StringIO() stringio_file_out = StringIO() self.parm.write_pdb(stringio_file, **kwargs) stringio_file.seek(0) lines = stringio_file.readlines() if noter: for line in lines: if line.startswith("TER"): lines.remove(line) # TODO: update ParmEd? if disulfide_conect: conect_record = [ 'CONECT%5d%5d\n' % (idx0 + 1, idx1 + 1) for (idx0, idx1) in cys_cys_atomidx_set ] conect_str = ''.join(conect_record) lines[-1] = conect_str + 'END\n' if noter: for line in lines: if line.startswith("TER"): lines.remove(line) stringio_file_out.writelines(lines) stringio_file_out.seek(0) return stringio_file_out
def _html_(self): r""" HTML representation of a table. Strings of html will be parsed for math inside dollar and double-dollar signs. 2D graphics will be displayed in the cells. Expressions will be latexed. The ``align`` option for tables is ignored in HTML output. Specifying ``header_column=True`` may not have any visible effect in the Sage notebook, depending on the version of the notebook. OUTPUT: A :class:`~sage.misc.html.HtmlFragment` instance. EXAMPLES:: sage: T = table([[r'$\sin(x)$', '$x$', 'text'], [1,34342,3], [identity_matrix(2),5,6]]) sage: T._html_() <div.../div> sage: print(T._html_()) <div class="notruncate"> <table class="table_form"> <tbody> <tr class ="row-a"> <td><script type="math/tex">\sin(x)</script></td> <td><script type="math/tex">x</script></td> <td>text</td> </tr> <tr class ="row-b"> <td><script type="math/tex">1</script></td> <td><script type="math/tex">34342</script></td> <td><script type="math/tex">3</script></td> </tr> <tr class ="row-a"> <td><script type="math/tex">\left(\begin{array}{rr} 1 & 0 \\ 0 & 1 \end{array}\right)</script></td> <td><script type="math/tex">5</script></td> <td><script type="math/tex">6</script></td> </tr> </tbody> </table> </div> Note that calling ``html(table(...))`` has the same effect as calling ``table(...)._html_()``:: sage: T = table([["$x$", r"$\sin(x)$"]] + [(x,n(sin(x), digits=2)) for x in [0..3]], header_row=True, frame=True) sage: T +-----+-----------+ | $x$ | $\sin(x)$ | +=====+===========+ | 0 | 0.00 | +-----+-----------+ | 1 | 0.84 | +-----+-----------+ | 2 | 0.91 | +-----+-----------+ | 3 | 0.14 | +-----+-----------+ sage: print(html(T)) <div class="notruncate"> <table border="1" class="table_form"> <tbody> <tr> <th><script type="math/tex">x</script></th> <th><script type="math/tex">\sin(x)</script></th> </tr> <tr class ="row-a"> <td><script type="math/tex">0</script></td> <td><script type="math/tex">0.00</script></td> </tr> <tr class ="row-b"> <td><script type="math/tex">1</script></td> <td><script type="math/tex">0.84</script></td> </tr> <tr class ="row-a"> <td><script type="math/tex">2</script></td> <td><script type="math/tex">0.91</script></td> </tr> <tr class ="row-b"> <td><script type="math/tex">3</script></td> <td><script type="math/tex">0.14</script></td> </tr> </tbody> </table> </div> """ from itertools import cycle rows = self._rows header_row = self._options['header_row'] if self._options['frame']: frame = 'border="1"' else: frame = '' s = StringIO() if rows: s.writelines([ # If the table has < 100 rows, don't truncate the output in the notebook '<div class="notruncate">\n' if len(rows) <= 100 else '<div class="truncate">', '<table {} class="table_form">\n'.format(frame), '<tbody>\n', ]) # First row: if header_row: s.write('<tr>\n') self._html_table_row(s, rows[0], header=header_row) s.write('</tr>\n') rows = rows[1:] # Other rows: for row_class, row in zip(cycle(["row-a", "row-b"]), rows): s.write('<tr class ="{}">\n'.format(row_class)) self._html_table_row(s, row, header=False) s.write('</tr>\n') s.write('</tbody>\n</table>\n</div>') from sage.misc.html import HtmlFragment return HtmlFragment(s.getvalue())
def _html_(self): r""" HTML representation of a table. Strings of html will be parsed for math inside dollar and double-dollar signs. 2D graphics will be displayed in the cells. Expressions will be latexed. The ``align`` option for tables is ignored in HTML output. Specifying ``header_column=True`` may not have any visible effect in the Sage notebook, depending on the version of the notebook. OUTPUT: A :class:`~sage.misc.html.HtmlFragment` instance. EXAMPLES:: sage: T = table([[r'$\sin(x)$', '$x$', 'text'], [1,34342,3], [identity_matrix(2),5,6]]) sage: T._html_() <div.../div> sage: print(T._html_()) <div class="notruncate"> <table class="table_form"> <tbody> <tr class ="row-a"> <td><script type="math/tex">\sin(x)</script></td> <td><script type="math/tex">x</script></td> <td>text</td> </tr> <tr class ="row-b"> <td><script type="math/tex">1</script></td> <td><script type="math/tex">34342</script></td> <td><script type="math/tex">3</script></td> </tr> <tr class ="row-a"> <td><script type="math/tex">\left(\begin{array}{rr} 1 & 0 \\ 0 & 1 \end{array}\right)</script></td> <td><script type="math/tex">5</script></td> <td><script type="math/tex">6</script></td> </tr> </tbody> </table> </div> Note that calling ``html(table(...))`` has the same effect as calling ``table(...)._html_()``:: sage: T = table([["$x$", "$\sin(x)$"]] + [(x,n(sin(x), digits=2)) for x in [0..3]], header_row=True, frame=True) sage: T +-----+-----------+ | $x$ | $\sin(x)$ | +=====+===========+ | 0 | 0.00 | +-----+-----------+ | 1 | 0.84 | +-----+-----------+ | 2 | 0.91 | +-----+-----------+ | 3 | 0.14 | +-----+-----------+ sage: print(html(T)) <div class="notruncate"> <table border="1" class="table_form"> <tbody> <tr> <th><script type="math/tex">x</script></th> <th><script type="math/tex">\sin(x)</script></th> </tr> <tr class ="row-a"> <td><script type="math/tex">0</script></td> <td><script type="math/tex">0.00</script></td> </tr> <tr class ="row-b"> <td><script type="math/tex">1</script></td> <td><script type="math/tex">0.84</script></td> </tr> <tr class ="row-a"> <td><script type="math/tex">2</script></td> <td><script type="math/tex">0.91</script></td> </tr> <tr class ="row-b"> <td><script type="math/tex">3</script></td> <td><script type="math/tex">0.14</script></td> </tr> </tbody> </table> </div> """ import types from itertools import cycle rows = self._rows header_row = self._options['header_row'] if self._options['frame']: frame = 'border="1"' else: frame = '' s = StringIO() if rows: s.writelines([ # If the table has < 100 rows, don't truncate the output in the notebook '<div class="notruncate">\n' if len(rows) <= 100 else '<div class="truncate">' , '<table {} class="table_form">\n'.format(frame), '<tbody>\n', ]) # First row: if header_row: s.write('<tr>\n') self._html_table_row(s, rows[0], header=header_row) s.write('</tr>\n') rows = rows[1:] # Other rows: for row_class, row in zip(cycle(["row-a", "row-b"]), rows): s.write('<tr class ="{}">\n'.format(row_class)) self._html_table_row(s, row, header=False) s.write('</tr>\n') s.write('</tbody>\n</table>\n</div>') from sage.misc.html import HtmlFragment return HtmlFragment(s.getvalue())