#! /usr/bin/env python

import sys
import argparse
import textwrap
from Bio import SeqIO

parser = argparse.ArgumentParser()
parser.add_argument(
    "infile", nargs="?", default=sys.stdin, help="filename (default: stdin)"
)
parser.add_argument(
    "-w",
    "--width",
    type=int,
    action="store",
    dest="width",
    default=80,
    help="wrap at width WIDTH (default: 80)",
)
args = parser.parse_args()

for r in SeqIO.parse(args.infile, "fasta"):
    stext = "".join(str(r.seq).split())
    desc = r.description
    slen = len(stext)
    # sid = str(r.id)
    len_str = f"length={slen}"
    if len_str in desc:  # see if length=xxxxx is already in the description
        print(f">{desc}")
    else:
        print(f">{desc} length={slen}")
    if args.width < 1:
        print(stext)
    else:
        print(textwrap.fill(stext, width=args.width))
