<s> import math <EOL> from skbio . util . _misc import chunk_str <EOL> from skbio . metadata . _repr import _MetadataReprBuilder <EOL> class _SequenceReprBuilder ( _MetadataReprBuilder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , seq , width , indent , chunk_size ) : <EOL> super ( _SequenceReprBuilder , self ) . __init__ ( seq , width , indent ) <EOL> self . _chunk_size = chunk_size <EOL> def _process_header ( self ) : <EOL> cls_name = self . _obj . __class__ . __name__ <EOL> self . _lines . add_line ( cls_name ) <EOL> self . _lines . add_separator ( ) <EOL> def _process_data ( self ) : <EOL> num_lines , num_chars , column_width = self . _find_optimal_seq_chunking ( ) <EOL> if num_lines <= <NUM_LIT:5> : <EOL> self . _lines . add_lines ( self . _format_chunked_seq ( <EOL> range ( num_lines ) , num_chars , column_width ) ) <EOL> else : <EOL> self . _lines . add_lines ( self . _format_chunked_seq ( <EOL> range ( <NUM_LIT:2> ) , num_chars , column_width ) ) <EOL> self . _lines . add_line ( '<STR_LIT>' ) <EOL> self . _lines . add_lines ( self . _format_chunked_seq ( <EOL> range ( num_lines - <NUM_LIT:2> , num_lines ) , num_chars , column_width ) ) <EOL> def _find_optimal_seq_chunking ( self ) : <EOL> """<STR_LIT>""" <EOL> num_lines = <NUM_LIT:0> <EOL> num_chars = <NUM_LIT:0> <EOL> column_width = <NUM_LIT:0> <EOL> num_chunks = <NUM_LIT:1> <EOL> not_exceeded = True <EOL> while not_exceeded : <EOL> line_len , new_chunk_info = self . _compute_chunked_seq_line_len ( <EOL> num_chunks ) <EOL> not_exceeded = line_len <= self . _width <EOL> if not_exceeded : <EOL> num_lines , num_chars , column_width = new_chunk_info <EOL> num_chunks += <NUM_LIT:1> <EOL> return num_lines , num_chars , column_width <EOL> def _compute_chunked_seq_line_len ( self , num_chunks ) : <EOL> """<STR_LIT>""" <EOL> num_chars = num_chunks * self . _chunk_size <EOL> num_lines = int ( math . ceil ( len ( self . _obj ) / num_chars ) ) <EOL> column_width = len ( '<STR_LIT>' % ( ( num_lines - <NUM_LIT:1> ) * num_chars ) ) <EOL> line_len = column_width + num_chars + ( num_chunks - <NUM_LIT:1> ) <EOL> return line_len , ( num_lines , num_chars , column_width ) <EOL> def _format_chunked_seq ( self , line_idxs , num_chars , column_width ) : <EOL> """<STR_LIT>""" <EOL> lines = [ ] <EOL> for line_idx in line_idxs : <EOL> seq_idx = line_idx * num_chars <EOL> chars = str ( self . _obj [ seq_idx : seq_idx + num_chars ] ) <EOL> chunked_chars = chunk_str ( chars , self . _chunk_size , '<STR_LIT:U+0020>' ) <EOL> lines . append ( ( '<STR_LIT>' % seq_idx ) . ljust ( column_width ) + chunked_chars ) <EOL> return lines </s>
