import subprocess
import tempfile

from Bio import Phylo
import pylab


def encode_for_clustal(name):
    return name.replace(' ', '_')


def unfuck_clustal_name(name):
    return name.replace('_', ' ')


def get_tree_bytes(pairs):
    with tempfile.NamedTemporaryFile(mode='w') as clustal_input, \
            tempfile.NamedTemporaryFile(mode='r') as phylogeny_input:
        for name, sequence in pairs:
            clustal_input.write('> %s\n%s\n\n' % (encode_for_clustal(name), sequence))
        clustal_input.flush()

        subprocess.check_call([
            '/clustalo', '-i', clustal_input.name, '--outfmt=clu', '-o', phylogeny_input.name
        ])

        subprocess.check_call([
            'cat', phylogeny_input.name
        ])


def draw_tree(input_path, output_path):
    tree = Phylo.read(input_path, 'newick')
    pylab.savefig(output_path)

@@ -1,12 +1,68 @@
+import contextlib%0Aimport os%0Aimport random%0Aimport string%0A
 import subpr
@@ -67,16 +67,16 @@
 process%0A
-
 import t
@@ -250,24 +250,247 @@
 '_', ' ')%0A%0A%0A
+@contextlib.contextmanager%0Adef tempname():%0A    name = '/tmp/' + ''.join(random.choise(string.ascii_letters) for _ in range(20))%0A%0A    yield name%0A%0A    try:%0A        os.remove(name)%0A    except FileNotFoundError:%0A        pass%0A%0A%0A
 def get_tree
@@ -591,40 +591,13 @@
 temp
-file.NamedTemporaryFile(mode='r'
+name(
 ) as
@@ -881,37 +881,32 @@
  phylogeny_input
-.name
 %0A        %5D)%0A%0A   
@@ -926,32 +926,32 @@
 ss.check_call(%5B%0A
+
             'cat
@@ -968,21 +968,16 @@
 ny_input
-.name
 %0A       

Update phylotree