Пример #1
0
 def __str__(self):
     out = StringIO()
     alignment = self.alignment
     tree = self.tree
     # write the format identifier
     print >> out, '# STOCKHOLM 1.0'
     # write the comments
     for comment in self.comments:
         print >> out, '#=GF CC', comment
     # write the tree
     print >> out, '#=GF NH', tree.get_newick_string()
     # determine the max header length
     augmented_column_annotation_names = ['#=GC '+name for name, value in self.column_annotations]
     headers = alignment.headers + augmented_column_annotation_names
     max_header_length = max(len(header) for header in headers)
     # write the alignment
     for header, sequence in zip(alignment.headers, alignment.sequences):
         left_justified_header = Monospace.left_justify(header, max_header_length, ' ')
         print >> out, '%s %s' % (left_justified_header, sequence)
     # write the column annotations
     column_annotation_values = [value for name, value in self.column_annotations]
     for name, value in zip(augmented_column_annotation_names, column_annotation_values):
         justified_name = Monospace.left_justify(name, max_header_length, ' ')
         print >> out, justified_name, value
     # write the format terminator
     print >> out, '//'
     return out.getvalue()
Пример #2
0
def do_analysis_helper(labels, element_lists, w):
    """
    Chop up the rows of data.
    Yield lines of text to be displayed in an html pre tag.
    @param labels: row labels to be left justified
    @param element_lists: data rows where each element is a letter or a span
    @param w: the width; the number of elements allowed per page row
    """
    if len(set(len(element_list) for element_list in element_lists)) != 1:
        msg = 'each element list should have the same nonzero length'
        raise ValueError(msg)
    label_width = max(len(label) for label in labels) + 1
    chopped_element_lists = [list(iterutils.chopped(element_list, w))
            for element_list in element_lists]
    page_rows = zip(*chopped_element_lists)
    for i, page_row in enumerate(page_rows):
        header = ''
        header += ' ' * label_width
        header += Monospace.get_ruler_line(i*w + 1, i*w + len(page_row[0]))
        yield header
        for label, element_list in zip(labels, page_row):
            justified_label = label.ljust(label_width)
            yield ''.join([justified_label] + list(element_list))
        if i < len(page_rows) - 1:
            yield ''
Пример #3
0
def do_analysis_helper(labels, element_lists, w):
    """
    Chop up the rows of data.
    Yield lines of text to be displayed in an html pre tag.
    @param labels: row labels to be left justified
    @param element_lists: data rows where each element is a letter or a span
    @param w: the width; the number of elements allowed per page row
    """
    if len(set(len(element_list) for element_list in element_lists)) != 1:
        msg = 'each element list should have the same nonzero length'
        raise ValueError(msg)
    label_width = max(len(label) for label in labels) + 1
    chopped_element_lists = [
        list(iterutils.chopped(element_list, w))
        for element_list in element_lists
    ]
    page_rows = zip(*chopped_element_lists)
    for i, page_row in enumerate(page_rows):
        header = ''
        header += ' ' * label_width
        header += Monospace.get_ruler_line(i * w + 1, i * w + len(page_row[0]))
        yield header
        for label, element_list in zip(labels, page_row):
            justified_label = label.ljust(label_width)
            yield ''.join([justified_label] + list(element_list))
        if i < len(page_rows) - 1:
            yield ''
Пример #4
0
 def __str__(self):
     out = StringIO()
     alignment = self.alignment
     tree = self.tree
     # write the taxa block
     print >> out, '#NEXUS'
     print >> out, ''
     for comment in self.comments:
         print >> out, '[%s]' % comment
     print >> out, ''
     print >> out, 'BEGIN TAXA;'
     print >> out, '  DIMENSIONS ntax = %d;' % len(alignment.headers)
     print >> out, '  TAXLABELS %s;' % ' '.join(alignment.headers)
     print >> out, 'END;'
     # write the tree block
     print >> out, ''
     print >> out, 'BEGIN TREES;'
     print >> out, '  TREE primates = %s' % tree.get_newick_string()
     print >> out, 'END;'
     # write the alignment block
     print >> out, ''
     print >> out, 'BEGIN CHARACTERS;'
     print >> out, '  DIMENSIONS nchar = %d;' % len(alignment.columns)
     print >> out, '  FORMAT datatype = DNA;'
     print >> out, '  MATRIX'
     max_header_length = max(len(header) for header in alignment.headers)
     for header, sequence in zip(alignment.headers, alignment.sequences):
         print >> out, '    %s %s' % (Monospace.left_justify(
             header, max_header_length, ' '), sequence)
     print >> out, '  ;'
     print >> out, 'END;'
     return out.getvalue()
Пример #5
0
 def __str__(self):
     out = StringIO()
     alignment = self.alignment
     tree = self.tree
     # write the taxa block
     print >> out, '#NEXUS'
     print >> out, ''
     for comment in self.comments:
         print >> out, '[%s]' % comment
     print >> out, ''
     print >> out, 'BEGIN TAXA;'
     print >> out, '  DIMENSIONS ntax = %d;' % len(alignment.headers)
     print >> out, '  TAXLABELS %s;' % ' '.join(alignment.headers)
     print >> out, 'END;'
     # write the tree block
     print >> out, ''
     print >> out, 'BEGIN TREES;'
     print >> out, '  TREE primates = %s' % tree.get_newick_string()
     print >> out, 'END;'
     # write the alignment block
     print >> out, ''
     print >> out, 'BEGIN CHARACTERS;'
     print >> out, '  DIMENSIONS nchar = %d;' % len(alignment.columns)
     print >> out, '  FORMAT datatype = DNA;'
     print >> out, '  MATRIX'
     max_header_length = max(len(header) for header in alignment.headers)
     for header, sequence in zip(alignment.headers, alignment.sequences):
         print >> out, '    %s %s' % (Monospace.left_justify(header, max_header_length, ' '), sequence)
     print >> out, '  ;'
     print >> out, 'END;'
     return out.getvalue()
Пример #6
0
 def _get_padded_column_labels(self):
     """
     Return a list of padded column labels.
     The number of columns returned is the number of original column labels
     plus the maximum row label length.
     The column labels are padded to equal lengths.
     @return: column labels padded vertically and horizontally
     """
     rmax = max(len(s) for s in self.row_labels)
     cmax = max(len(s) for s in self.column_labels)
     column_labels = [''] * rmax + self.column_labels
     return [Monospace.left_justify(x, cmax, '.') for x in column_labels]
Пример #7
0
 def _get_padded_column_labels(self):
     """
     Return a list of padded column labels.
     The number of columns returned is the number of original column labels
     plus the maximum row label length.
     The column labels are padded to equal lengths.
     @return: column labels padded vertically and horizontally
     """
     rmax = max(len(s) for s in self.row_labels)
     cmax = max(len(s) for s in self.column_labels)
     column_labels = [''] * rmax + self.column_labels
     return [Monospace.left_justify(x, cmax, '.') for x in column_labels]
Пример #8
0
 def _get_padded_row_labels(self):
     """
     @return: row labels padded to the length of the longest row label
     """
     rmax = max(len(s) for s in self.row_labels)
     return [Monospace.left_justify(x, rmax, '.') for x in self.row_labels]
Пример #9
0
 def _get_padded_row_labels(self):
     """
     @return: row labels padded to the length of the longest row label
     """
     rmax = max(len(s) for s in self.row_labels)
     return [Monospace.left_justify(x, rmax, '.') for x in self.row_labels]