def format_seq(rec, p5_len, p3_len):
    """Trims a FASTA sequence to just the region of interest and its
       accompanying primers, and reports the length."""
    target_range = [i for (i, n) in enumerate(rec.seq) if n.isupper()]
    start = 0 if target_range[0]-p5_len < 0 else target_range[0]-p5_len
    end = target_range[-1]+p3_len+1
    target_seq = rec.seq[start:end]
    return target_seq, len(target_range)