Example #1
0
def topology(struct, protein="protein", dirname="top", ff="charmm27", water="spc", ignh=True, **top_args):
    """
    Generate a topology for a given structure.

    @return a dict with the following keys: {"top", "struct", "dirname"}, where
    the values are the file names of the resulting topology, structure, and position restraint files.
    """

    logging.info("autogenerating topology using pdb2gmx...")
    pdb2gmx_args = {"ff":ff, "water":water, "ignh":ignh}
    pdb2gmx_args.update(top_args)
    struct = data_tofile(struct, "src.gro", dirname=dirname)
    result = gromacs.setup.topology(struct, protein, "system.top", dirname, **pdb2gmx_args)
    result["dirname"] = dirname

    return MDManager(result)
Example #2
0
def topology(struct, protein="protein", dirname="top", ff="charmm27", water="spc", ignh=True, **top_args):
    """
    Generate a topology for a given structure.

    @return a dict with the following keys: {"top", "struct", "dirname"}, where
    the values are the file names of the resulting topology, structure, and position restraint files.
    """

    logging.info("autogenerating topology using pdb2gmx...")
    pdb2gmx_args = {"ff":ff, "water":water, "ignh":ignh}
    pdb2gmx_args.update(top_args)
    struct = data_tofile(struct, "src.pdb", dirname=dirname)
    result = gromacs.setup.topology(struct, protein, "system.top", dirname, **pdb2gmx_args)
    result["dirname"] = dirname

    print("result: {}".format(result))

    return MDManager(result)
Example #3
0
def solvate(struct, top, top_includes=None, box = None,
            concentration=0, cation='NA', anion='CL',
            water='spc', solvent_name='SOL', with_membrane=False,
            ndx = 'main.ndx', mainselection = '"Protein"',
            dirname=None, deffnm='sol',
            **kwargs):
    """Put protein into box, add water, add counter-ions.

    Currently this really only supports solutes in water. If you need
    to embedd a protein in a membrane then you will require more
    sophisticated approaches.

    However, you *can* supply a protein already inserted in a
    bilayer. In this case you will probably want to set *distance* =
    ``None`` and also enable *with_membrane* = ``True`` (using extra
    big vdw radii for typical lipids).

    .. Note:: The defaults are suitable for solvating a globular
       protein in a fairly tight (increase *distance*!) dodecahedral
       box.

    :Arguments:
      *struct* : MDAnalysis universe, AtomGroup or data buffer. This is the starting
          dry, unsolvated structure.

      *top* : filename
          Gromacs topology
      *distance* : float
          When solvating with water, make the box big enough so that
          at least *distance* nm water are between the solute *struct*
          and the box boundary.
          Set *boxtype*  to ``None`` in order to use a box size in the input
          file (gro or pdb).
      *boxtype* : string
          Any of the box types supported by :class:`~gromacs.tools.Editconf`
          (triclinic, cubic, dodecahedron, octahedron). Set the box dimensions
          either with *distance* or the *box* and *angle* keywords.

          If set to ``None`` it will ignore *distance* and use the box
          inside the *struct* file.
      *box* : May be either an numpy array, or a list.
          If the struct is an MDAnalysis obj, the box is obtained from the
          MDAnalysis trajectory.
          .
      *angles*
          List of three angles (only necessary for triclinic boxes).
      *concentration* : float
          Concentration of the free ions in mol/l. Note that counter
          ions are added in excess of this concentration.
      *cation* and *anion* : string
          Molecule names of the ions. This depends on the chosen force field.
      *water* : string
          Name of the water model; one of "spc", "spce", "tip3p",
          "tip4p". This should be appropriate for the chosen force
          field. If an alternative solvent is required, simply supply the path to a box
          with solvent molecules (used by :func:`~gromacs.genbox`'s  *cs* argument)
          and also supply the molecule name via *solvent_name*.
      *solvent_name*
          Name of the molecules that make up the solvent (as set in the itp/top).
          Typically needs to be changed when using non-standard/non-water solvents.
          ["SOL"]
      *with_membrane* : bool
           ``True``: use special ``vdwradii.dat`` with 0.1 nm-increased radii on
           lipids. Default is ``False``.
      *ndx* : filename
          How to name the index file that is produced by this function.
      *mainselection* : string
          A string that is fed to :class:`~gromacs.tools.Make_ndx` and
          which should select the solute.
      *dirname* : directory name
          Name of the directory in which all files for the solvation stage are stored.
      *includes*
          List of additional directories to add to the mdp include path
      *kwargs*
          Additional arguments are passed on to
          :class:`~gromacs.tools.Editconf` or are interpreted as parameters to be
          changed in the mdp file.

    """

    if dirname is None:
        dirname = tempfile.mkdtemp(prefix="tmp." + deffnm + ".")
        logging.debug("created solvation dir {}".format(dirname))

    # The box, a list of three box lengths [A,B,C] that are used by :class:`~gromacs.tools.Editconf`
    # in combination with *boxtype* (``bt`` in :program:`editconf`) and *angles*.
    if box is None and isinstance(struct, MDAnalysis.core.AtomGroup.Universe):
        # convert to nm
        box = struct.trajectory.ts.dimensions[:3] / 10.0
    else:
        if not (isinstance(box, numpy.ndarray) or isinstance(box, list)) or len(box) != 3:
            raise ValueError("box must be either a length 3 numpy array or list")

    # build the substitution index.
    # TODO: Verify that solvate only adds atoms after the dry structure
    # current logic, to be verified, is that the sub index (the original atom indices)
    # are the first n atoms in the resulting n + nsol solvated structure.
    # The sub indices are used to pick out the original strucure out of
    # the n + nsol atom trr trajectory files.
    sub = None

    if isinstance(struct, MDAnalysis.core.AtomGroup.Universe):
        sub = numpy.arange(len(struct.atoms))

    struct = data_tofile(struct, "src.pdb", dirname=dirname)

    if sub is None:
        u = MDAnalysis.Universe(struct)
        sub = numpy.arange(len(u.atoms))

    top = data_tofile(top, "src.top", dirname=dirname)

    # dump the included itp files to the dir where solvation occurs.
    logging.debug("top_includes: {}".format(top_includes))
    for i in top_includes:
        logging.debug("copying file {} to {}".format(i,dirname))
        data_tofile(i,dirname=dirname)

    result = gromacs.setup.solvate(struct, top,
            1.0, "triclinic",
            concentration, cation, anion,
            water, solvent_name, with_membrane,
            ndx, mainselection,
            dirname, box=list(box))
    result["dirname"] = dirname
    result["top"] = top
    result["sub"] = sub

    return MDManager(result)
Example #4
0
def setup_md_debug(struct, top, top_includes, deffnm="md", dirname=None, mdp="md_CHARMM27.mdp",
             mainselection=None, **kwargs):
    """Set up Gromacs MD run..

    Additional itp files should be in the same directory as the top file.

    Many of the keyword arguments below already have sensible values. Note that
    setting *mainselection* = ``None`` will disable many of the automated
    choices and is often recommended when using your own mdp file.

    :Keywords:
       *dirname*
          set up under directory dirname [MD_POSRES]
       *struct*
          input structure (gro, pdb, ...) [em/em.pdb]
       *top*
          topology file [top/system.top]
       *mdp*
          mdp file (or use the template) [templates/md.mdp]
       *ndx*
          index file (supply when using a custom mdp)
       *includes*
          additional directories to search for itp files
       *mainselection*
          :program:`make_ndx` selection to select main group ["Protein"]
          (If ``None`` then no canonical index file is generated and
          it is the user's responsibility to set *tc_grps*,
          *tau_t*, and *ref_t* as keyword arguments, or provide the mdp template
          with all parameter pre-set in *mdp* and probably also your own *ndx*
          index file.)
       *deffnm*
          default filename for Gromacs run [md]
       *runtime*
          total length of the simulation in ps [1000]
       *dt*
          integration time step in ps [0.002]
       *qscript*
          script to submit to the queuing system; by default
          uses the template :data:`gromacs.config.qscript_template`, which can
          be manually set to another template from :data:`gromacs.config.templates`;
          can also be a list of template names.
       *qname*
          name to be used for the job in the queuing system [PR_GMX]
       *mdrun_opts*
          option flags for the :program:`mdrun` command in the queuing system
          scripts such as "-stepout 100". [""]
       *kwargs*
          remaining key/value pairs that should be changed in the template mdp
          file, eg ``nstxtcout=250, nstfout=250`` or command line options for
          ``grompp` such as ``maxwarn=1``.

          In particular one can also set **define** and activate
          whichever position restraints have been coded into the itp
          and top file. For instance one could have

             *define* = "-DPOSRES_MainChain -DPOSRES_LIGAND"

          if these preprocessor constructs exist. Note that there
          **must not be any space between "-D" and the value.**

          By default *define* is set to "-DPOSRES".

    :Returns: a dict that can be fed into :func:`gromacs.setup.MD`
              (but check, just in case, especially if you want to
              change the ``define`` parameter in the mdp file)

    .. Note:: The output frequency is drastically reduced for position
              restraint runs by default. Set the corresponding ``nst*``
              variables if you require more output.
    """
    logging.info("[%(dirname)s] Setting up MD with position restraints..." % vars())

    if dirname is None:
        dirname = tempfile.mkdtemp(prefix="tmp." + deffnm + ".")
        logging.debug("created md dir {}".format(dirname))

    struct = data_tofile(struct, "src.pdb", dirname=dirname)
    top = data_tofile(top, "src.top", dirname=dirname)

    logging.debug("top_includes: {}".format(top_includes))
    for i in top_includes:
        logging.debug("copying file {} to {}".format(i,dirname))
        data_tofile(i,dirname=dirname)

    # required from gromacswrapper, if this is not set, it will fail as it expects
    # a queing systme.
    kwargs.setdefault('qname', None)

    # this stop grompp from failing if there are warning.
    # TODO, is this the best place to put this, should this be in in
    # config.py ????
    kwargs.setdefault('maxwarn', -1)

    logging.info("using mdp template {} from key {}".format(config.templates[mdp], mdp))
    mdp = config.templates[mdp]

    logging.debug("calling _setup_MD with kwargs: {}".format(kwargs))

    setup_MD = gromacs.setup._setup_MD(dirname, struct=struct, top=top, deffnm=deffnm, mdp=mdp,
                                       mainselection=mainselection, **kwargs)

    setup_MD["dirname"] = dirname

    logging.debug("finished _setup_MD, recieved: {}".format(setup_MD))

    return MDManager(setup_MD)
Example #5
0
def minimize(struct, top, top_includes, dirname=None,
             minimize_output="em.pdb", deffnm="em", mdrunner=MDrunner,
             mdp="em.mdp", **kwargs):
    """
    Energy minimize a system.

    Creates the directory minimize_dir, and all operations are performed there.

    @param struct: name of structure file
    @param top: name of top file

    @return: a dictionary with the following values:
        'struct': final_struct,
        'top': topology,
        'mainselection': mainselection,
        'dirname': dir where minimization took place.

    This sets up the system (creates run input files) and also runs
    ``mdrun_d``. Thus it can take a while.

    Additional itp files should be in the same directory as the top file.

    Many of the keyword arguments below already have sensible values.

    :Keywords:
       *dirname*
          set up under directory dirname [em]
       *struct*
          input structure (gro, pdb, ...) [solvate/ionized.gro]
       *output*
          output structure (will be put under dirname) [em.pdb]
       *deffnm*
          default name for mdrun-related files [em]
       *top*
          topology file [top/system.top]
       *mdp*
          mdp file (or use the template) [templates/em.mdp]
       *includes*
          additional directories to search for itp files
       *mdrunner*
          :class:`gromacs.run.MDrunner` class; by defauly we
          just try :func:`gromacs.mdrun_d` and :func:`gromacs.mdrun` but a
          MDrunner class gives the user the ability to run mpi jobs
          etc. [None]
       *kwargs*
          remaining key/value pairs that should be changed in the
          template mdp file, eg ``nstxtcout=250, nstfout=250``.

    .. note:: If :func:`~gromacs.mdrun_d` is not found, the function
              falls back to :func:`~gromacs.mdrun` instead.
    """
    if dirname is None:
        dirname = tempfile.mkdtemp(prefix="tmp." + deffnm + ".")
        logging.debug("created energy minimization dir {}".format(dirname))

    struct = data_tofile(struct, "src.pdb", dirname=dirname)
    top = data_tofile(top, "src.top", dirname=dirname)

#    import pdb; pdb.set_trace()
    logging.debug("top_includes: {}".format(top_includes))
    for i in top_includes:
        data_tofile(i,dirname=dirname)

    logging.info("using mdp template {} from key {}".format(config.templates[mdp], mdp))
    mdp = config.templates[mdp]

    # this stop grompp from failing if there are warning.
    # TODO, is this the best place to put this, should this be in in
    # config.py ????
    kwargs.setdefault('maxwarn', -1)

    # gromacs.setup.energy_minimize returns
    # { 'struct': final_struct,
    #   'top': topology,
    #   'mainselection': mainselection,
    # }
    result = gromacs.setup.energy_minimize(dirname=dirname, struct=struct,
                                         top=top, output=minimize_output, deffnm=deffnm,
                                         mdp=mdp, mdrunner=mdrunner, **kwargs)
    result["dirname"] = dirname
    return MDManager(result)