diff --git a/sequence.py b/sequence.py index 8f99963799cf54ddbdf1f6e026ced32628e7b360..5181180990103ed8cf388286f2ba58604f6316b3 100644 --- a/sequence.py +++ b/sequence.py @@ -643,13 +643,15 @@ class Alignment(): distmat[i, j] = distmat[j, i] = dist return distmat - def writeHTML(self, filename): + def writeHTML(self, filename=None): """ Generate HTML that displays the alignment in color. Requires that the alphabet is annotated with the label 'html-color' (see Sequence.annotateSym) and that each symbol maps to a text string naming the color, e.g. 'blue' """ - fh = open(filename, 'w') - fh.write('\nSequence Alignment\n
\n')
+        if filename == None:
+            htmlstr = '
\n'
+        else:
+            htmlstr = '\nSequence Alignment\n
\n'
         maxNameLength =  self.getnamelen()
         html = ''.ljust(maxNameLength) + ' '
         for i in range(self.alignlen - 1):
@@ -658,7 +660,7 @@ class Alignment():
             else:
                 html += ' '
         html += '%s\n' % (self.alignlen)
-        fh.write(html)
+        htmlstr += html
         if self.alignlen > 10:
             html = ''.ljust(maxNameLength) + ' '
             for i in range(self.alignlen - 1):
@@ -667,7 +669,7 @@ class Alignment():
                 else:
                     html += ' '
             html += '\n'
-            fh.write(html)
+            htmlstr += html
         for seq in self.seqs:
             html = seq.name.ljust(maxNameLength) + ' '
             for sym in seq:
@@ -676,9 +678,15 @@ class Alignment():
                     color = 'white'
                 html += '%s' % (color, sym)
             html += '\n'
-            fh.write(html)
-        fh.write('
\n') - fh.close() + htmlstr += html + htmlstr += '
'
+        if filename:
+            fh = open(filename, 'w')
+            fh.write(htmlstr)
+            fh.write('\n')
+            fh.close()
+        else:
+            return htmlstr
 
 def saveConsensus(aln, theta1 = 0.99, theta2 = 0.01, countgaps = False, consensus = True, filename = None):
     """ Display a table with rows for each alignment column, showing