def read(filename):
    """Read in a fasta-formatted sequence file.

    Returns a dictionary of sequences, indexed by sequence title.

    Arguments:
    ---------
    filename: a string of the filename to be read or an open file handle

    """
    if isinstance(filename, str):
        handle = open(filename, 'r')
    else:
        handle = filename

    seq = ""
    title = ""
    seqData = {}
    copy = False

    for line in handle:
        if line[0] == '>':
            if copy is True:
                seqData[title] = seq
            title = line.strip(">\n\r ")
            copy = True
            seq = ""
        else:
            seq += line.strip("\n\r ")
    if title in seqData.keys():
        raise KeyError('Fasta file contains duplicate contig ids.')
    seqData[title] = seq

    if isinstance(filename, str):
        handle.close()
    return seqData