def mask_sequence(seq, maskchar, fpos, tpos):
    """Given a sequence, mask it with maskchar starting at fpos (including) and
    ending at tpos (excluding)
    """
    if len(maskchar) > 1:
        raise RuntimeError("Internal error: more than one character given to mask_sequence")
    if fpos < 0:
        fpos = 0
    if tpos > len(seq):
        tpos = len(seq)

    newseq = "".join((seq[:fpos], maskchar * (tpos - fpos), seq[tpos:]))

    return newseq