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()
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()
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()
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]
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]