<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -7,20 +7,43 @@
 &quot;&quot;&quot;
 __docformat__ = &quot;epytext en&quot;
 
+from itertools import chain
+
 from Bio.Nexus import Nexus
 
+# Structure of a Nexus tree-only file
+NEX_TEMPLATE = &quot;&quot;&quot;\
+#NEXUS
+Begin Taxa;
+ Dimensions NTax=%(count)d;
+ TaxLabels %(labels)s;
+End;
+Begin Trees;
+ %(trees)s
+End;
+&quot;&quot;&quot;
+
+# 'index' starts from 1; 'tree' is the Newick tree string
+TREE_TEMPLATE = &quot;Tree tree%(index)d=[&amp;U]%(tree)s;&quot;
+
 
 def parse(file):
     nex = Nexus.Nexus(file)
     return iter(nex.trees)
 
 
+# XXX Nexus/Newick-specific tree attributes: to_string, get_taxa
 def write(obj, file, **kwargs):
-    raise NotImplementedError(&quot;This function doesn't work yet.&quot;)
-    nex = Nexus.Nexus()
-    if isinstance(obj, list):
-        nex.trees = obj 
-    else:
-        nex.trees = list(obj)
-    Nexus.Nexus.write_nexus_data(nex, file, **kwargs)
-    return len(nex.trees)
+    trees = list(obj)
+    nexus_trees = [TREE_TEMPLATE % {
+            'index': idx+1,
+            'tree': tree.to_string(plain_newick=True, plain=False, **kwargs)
+            } for idx, tree in enumerate(trees)]
+    tax_labels = list(chain(*(t.get_taxa() for t in trees)))
+    text = NEX_TEMPLATE % {
+            'count':    len(tax_labels),
+            'labels':   ' '.join(tax_labels),
+            'trees':    '\n'.join(nexus_trees),
+            }
+    file.write(text)
+    return len(nexus_trees)</diff>
      <filename>Bio/TreeIO/NexusIO.py</filename>
    </modified>
    <modified>
      <diff>@@ -73,7 +73,7 @@ def write(trees, file, format, **kwargs):
         trees = [trees]
     do_close = False
     if isinstance(file, basestring):
-        file = open(file, 'r')
+        file = open(file, 'w+')
         do_close = True
     try:
         n = getattr(supported_formats[format], 'write')(trees, file, **kwargs)</diff>
      <filename>Bio/TreeIO/__init__.py</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>ac2facc6c70b521a9655d996bfd741ef45f6945a</id>
    </parent>
  </parents>
  <author>
    <name>Eric Talevich</name>
    <email>eric.talevich@gmail.com</email>
  </author>
  <url>http://github.com/etal/biopython/commit/e0c560f017f9971e8e57fd5e76bbabfbef1a5baf</url>
  <id>e0c560f017f9971e8e57fd5e76bbabfbef1a5baf</id>
  <committed-date>2009-10-13T12:54:24-07:00</committed-date>
  <authored-date>2009-10-13T12:54:24-07:00</authored-date>
  <message>TreeIO: Fixed NexusIO.write support.

It doesn't actually use any Nexus methods, just fills in a template based on the
output of Forester with an example file.</message>
  <tree>2fda30e0d87328158a7b634dedfe7d2afcc84bed</tree>
  <committer>
    <name>Eric Talevich</name>
    <email>eric.talevich@gmail.com</email>
  </committer>
</commit>
