Exemplo n.º 1
0
def energy_termnames(edrfile):
    """ Get a list of energy term names from the .edr file by parsing a system call to g_energy. """
    if not os.path.exists(edrfile):
        logger.error("Cannot determine energy term names without an .edr file\n")
        raise RuntimeError
    ## Figure out which energy terms need to be printed.
    o = callgmx("energy -f %s -xvg no" % (edrfile), stdin="Total-Energy\n", copy_stdout=False, copy_stderr=True)
    parsemode = 0
    energyterms = OrderedDict()
    for line in o:
        s = line.split()
        if "Select the terms you want from the following list" in line:
            parsemode = 1
        if parsemode == 1:
            if len(s) > 0 and all([isint(i) for i in s[::2]]):
                parsemode = 2
        if parsemode == 2:
            if len(s) > 0:
                try:
                    if all([isint(i) for i in s[::2]]):
                        for j in range(len(s))[::2]:
                            num = int(s[j])
                            name = s[j + 1]
                            energyterms[name] = num
                except:
                    pass
    return energyterms
Exemplo n.º 2
0
def edit_mdp(fin=None, fout=None, options={}, defaults={}, verbose=False):
    """
    Read, create or edit a Gromacs MDP file.  The MDP file contains GROMACS run parameters.
    If the input file exists, it is parsed and options are replaced where "options" overrides them.
    If the "options" dictionary contains more options, they are added at the end.
    If the "defaults" dictionary contains more options, they are added at the end.
    Keys are standardized to lower-case strings where all dashes are replaced by underscores.
    The output file contains the same comments and "dressing" as the input.
    Also returns a dictionary with the final key/value pairs.

    Parameters
    ----------
    fin : str, optional
        Input .mdp file name containing options that are more important than "defaults", but less important than "options"
    fout : str, optional
        Output .mdp file name.
    options : dict, optional
        Dictionary containing mdp options. Existing options are replaced, new options are added at the end, None values are deleted from output mdp.
    defaults : dict, optional
        defaults Dictionary containing "default" mdp options, added only if they don't already exist.
    verbose : bool, optional
        Print out additional information        

    Returns
    -------
    OrderedDict
        Key-value pairs combined from the input .mdp and the supplied options/defaults and equivalent to what's printed in the output mdp.
    """
    clashes = ["pbc"]
    # Make sure that the keys are lowercase, and the values are all strings.
    options = OrderedDict(
        [(key.lower().replace("-", "_"), str(val) if val is not None else None) for key, val in options.items()]
    )
    # List of lines in the output file.
    out = []
    # List of options in the output file.
    haveopts = []
    # List of all options in dictionary form, to be returned.
    all_options = OrderedDict()
    if fin is not None and os.path.isfile(fin):
        for line in open(fin).readlines():
            line = line.strip().expandtabs()
            # The line structure should look something like this:
            # key   = value  ; comments
            # First split off the comments.
            if len(line) == 0:
                out.append("")
                continue
            s = line.split(";", 1)
            data = s[0]
            comms = s[1] if len(s) > 1 else None
            # Pure comment lines or empty lines get appended to the output.
            if set(data).issubset([" "]):
                out.append(line)
                continue
            # Now split off the key and value fields at the equals sign.
            keyf, valf = data.split("=", 1)
            key = keyf.strip().lower().replace("-", "_")
            haveopts.append(key)
            if key in options:
                val = options[key]
                val0 = valf.strip()
                if key in clashes and val != val0:
                    logger.error(
                        "edit_mdp tried to set %s = %s but its original value was %s = %s\n" % (key, val, key, val0)
                    )
                    raise RuntimeError
                # Passing None as the value causes the option to be deleted
                if val is None:
                    continue
                if len(val) < len(valf):
                    valf = " " + val + " " * (len(valf) - len(val) - 1)
                else:
                    valf = " " + val + " "
                lout = [keyf, "=", valf]
                if comms is not None:
                    lout += [";", comms]
                out.append("".join(lout))
            else:
                out.append(line)
                val = valf.strip()
            all_options[key] = val
    for key, val in options.items():
        key = key.lower().replace("-", "_")
        if key not in haveopts:
            haveopts.append(key)
            out.append("%-20s = %s" % (key, val))
            all_options[key] = val
    # Fill in some default options.
    for key, val in defaults.items():
        key = key.lower().replace("-", "_")
        options[key] = val
        if key not in haveopts:
            out.append("%-20s = %s" % (key, val))
            all_options[key] = val
    if fout != None:
        file_out = wopen(fout)
        for line in out:
            print >> file_out, line
        file_out.close()
    if verbose:
        printcool_dictionary(options, title="%s -> %s with options:" % (fin, fout))
    return all_options
Exemplo n.º 3
0
elif which("mdrun" + gmxsuffix) != "":
    logger.info("Using double precision GROMACS version 4\n")
    gmxpath = which("mdrun" + gmxsuffix)
    GMXVERSION = 4
else:
    gmxsuffix = ""
    if which("gmx" + gmxsuffix) != "":
        logger.info("Using single precision GROMACS version 5\n")
        gmxpath = which("gmx" + gmxsuffix)
        GMXVERSION = 5
    elif which("mdrun" + gmxsuffix) != "":
        logger.info("Using single precision GROMACS version 4\n")
        gmxpath = which("mdrun" + gmxsuffix)
        GMXVERSION = 4
    else:
        logger.error("Cannot find the GROMACS executables!\n")
        raise RuntimeError
os.environ["GMX_MAXBACKUP"] = "-1"
os.environ["GMX_NO_SOLV_OPT"] = "TRUE"
os.environ["GMX_NO_ALLVSALL"] = "TRUE"

gmxprogs = [
    "anadock",
    "anaeig",
    "analyze",
    "angle",
    "bar",
    "bond",
    "bundle",
    "chi",
    "cluster",