#! /usr/bin/env python

import sys
import argparse
from Bio import SeqIO

parser = argparse.ArgumentParser(
    description="slice a region of a collection of sequences in fasta format"
)
parser.add_argument(
    "-1",
    "--one-based",
    action="store_true",
    default=False,
    help="use 1-based coordinates rather than 0-based.",
)
parser.add_argument("start", type=int, help="region start, 0-based.")
parser.add_argument("end", type=int, help="region end, 0-based.")
parser.add_argument(
    "infile", nargs="?", default=sys.stdin, help="filename (default: stdin)"
)
args = parser.parse_args()

if args.one_based:
    args.start -= 1

for r in SeqIO.parse(args.infile, "fasta"):
    #print(SeqIO.write(r[args.start : args.end], sys.stdout, "fasta"))
    s = r[args.start : args.end]
    print(f">{s.description}\n{s.seq}")
