def fasta_wrap(text):
    """Line wrap FASTA output at 80 characters."""
    new = []
    lines = text.split("\n")
    while lines:
        line = lines.pop(0)
        if line.startswith(">"):
            while len(line) > 80 and " " in line:
                cut = line
                while len(cut) > 80 and " " in cut:
                    cut, rest = cut.rsplit(" ", 1)
                new.append(cut)
                line = line[len(cut) + 1 :].lstrip()
                del cut
            new.append(line)
            line = lines.pop(0)
            while len(line) > 80:
                new.append(line[:80])
                line = line[80:]
        new.append(line)
    return "\n".join(new)