def readPrefs(prefsfile, minpref=<NUM_LIT:0>, avgprefs=False, randprefs=False,<EOL>seed=<NUM_LIT:1>, sites_as_strings=False):
assert minpref >= <NUM_LIT:0>, '<STR_LIT>'<EOL>aas = set(phydmslib.constants.AA_TO_INDEX.keys())<EOL>try:<EOL><INDENT>df = pandas.read_csv(prefsfile, sep=None, engine='<STR_LIT>')<EOL>pandasformat = True<EOL><DEDENT>except ValueError:<EOL><INDENT>pandasformat = False<EOL><DEDENT>if pandasformat and (set(df.columns) == aas.union(set(['<STR_LIT>'])) or<EOL>set(df.columns) == aas.union(set(['<STR_LIT>', '<STR_LIT:*>']))):<EOL><INDENT>sites = df['<STR_LIT>'].tolist()<EOL>prefs = {}<EOL>for r in sites:<EOL><INDENT>rdf = df[df['<STR_LIT>'] == r]<EOL>prefs[r] = {}<EOL>for aa in df.columns:<EOL><INDENT>if aa != '<STR_LIT>':<EOL><INDENT>prefs[r][aa] = float(rdf[aa])<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>prefs = phydmslib.file_io.readPrefs_dms_tools_format(prefsfile)[<NUM_LIT:2>]<EOL>sites = list(prefs.keys())<EOL><DEDENT>if not sites_as_strings:<EOL><INDENT>try:<EOL><INDENT>sites = [int(r) for r in sites]<EOL><DEDENT>except ValueError:<EOL><INDENT>raise ValueError("<STR_LIT>".format(prefsfile))<EOL><DEDENT>assert (min(sites) == <NUM_LIT:1> and max(sites) - min(sites) == len(sites) - <NUM_LIT:1>),"<STR_LIT>"<EOL>prefs = dict([(int(r), rprefs) for (r, rprefs) in prefs.items()])<EOL><DEDENT>else:<EOL><INDENT>sites = [str(r) for r in sites]<EOL>prefs = dict([(str(r), rprefs) for (r, rprefs) in prefs.items()])<EOL><DEDENT>assert len(set(sites)) == len(sites), "<STR_LIT>"<EOL>assert all([all([pi >= <NUM_LIT:0> for pi in rprefs.values()]) for rprefs in<EOL>prefs.values()]), "<STR_LIT>".format(prefsfile)<EOL>for r in list(prefs.keys()):<EOL><INDENT>rprefs = prefs[r]<EOL>assert sum(rprefs.values()) - <NUM_LIT:1> <= <NUM_LIT>, (<EOL>"<STR_LIT>".format(prefsfile))<EOL>if '<STR_LIT:*>' in rprefs:<EOL><INDENT>del rprefs['<STR_LIT:*>']<EOL><DEDENT>assert aas == set(rprefs.keys()), ("<STR_LIT>"<EOL>"<STR_LIT>").format(prefsfile, r)<EOL>rsum = float(sum(rprefs.values()))<EOL>prefs[r] = dict([(aa, pi / rsum) for (aa, pi) in rprefs.items()])<EOL><DEDENT>assert set(sites) == set(prefs.keys())<EOL>for r in list(prefs.keys()):<EOL><INDENT>rprefs = prefs[r]<EOL>iterations = <NUM_LIT:0><EOL>while any([pi < minpref for pi in rprefs.values()]):<EOL><INDENT>rprefs = dict([(aa, max(<NUM_LIT> * minpref,<EOL>pi)) for (aa, pi) in rprefs.items()])<EOL>newsum = float(sum(rprefs.values()))<EOL>rprefs = dict([(aa, pi / newsum) for (aa, pi) in rprefs.items()])<EOL>iterations += <NUM_LIT:1><EOL>assert iterations <= <NUM_LIT:3>, "<STR_LIT>"<EOL><DEDENT>prefs[r] = rprefs<EOL><DEDENT>if randprefs:<EOL><INDENT>assert not avgprefs, "<STR_LIT>"<EOL>random.seed(seed)<EOL>sites = sorted([r for r in prefs.keys()])<EOL>prefs = [prefs[r] for r in sites]<EOL>random.shuffle(sites)<EOL>prefs = dict(zip(sites, prefs))<EOL><DEDENT>elif avgprefs:<EOL><INDENT>avg_prefs = dict([(aa, <NUM_LIT:0.0>) for aa in aas])<EOL>for rprefs in prefs.values():<EOL><INDENT>for aa in aas:<EOL><INDENT>avg_prefs[aa] += rprefs[aa]<EOL><DEDENT><DEDENT>for aa in aas:<EOL><INDENT>avg_prefs[aa] /= float(len(prefs))<EOL><DEDENT>for r in list(prefs.keys()):<EOL><INDENT>prefs[r] = avg_prefs<EOL><DEDENT><DEDENT>return prefs<EOL>
Read preferences from file with some error checking.

    Args:
        `prefsfile` (string or readable file-like object)
            File holding amino-acid preferences. Can be
            comma-, space-, or tab-separated file with column
            headers of `site` and then all one-letter amino-acid
            codes, or can be in the more complex format written
            `dms_tools v1 <http://jbloomlab.github.io/dms_tools/>`_.
            Must be prefs for consecutively numbered sites starting at 1.
            Stop codon prefs can be present (stop codons are indicated by
            ``*``); if so they are removed and prefs re-normalized to sum to 1.
        `minpref` (float >= 0)
            Adjust all preferences to be >= this number.
        `avgprefs`, `randprefs` (bool)
            Mutually exclusive options specifying to average or
            randomize prefs across sites.
        `seed` (int)
            Seed used to sort random number generator for `randprefs`.
        `sites_as_strings` (bool)
            By default, the site numers are coerced to integers.
            If this option is `True`, then they are kept as strings.

    Returns:
        `prefs` (dict)
            `prefs[r][a]` is the preference of site `r` for amino-acid `a`.
            `r` is an `int` unless `sites_as_strings=True`.