示例#1
0
def grompp_mdruns(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.grompp)
        return

    pers = cpc.dataflow.Persistence(
        os.path.join(inp.getPersistentDir(), "persistent.dat"))

    grompp_inputs = ['mdp', 'top', 'conf', 'ndx', 'settings', 'include']
    mdrun_inputs = ['priority', 'cmdline_options', 'resources']
    inputs = grompp_inputs + mdrun_inputs
    grompp_outputs = ['tpr']
    mdrun_outputs = ['conf', 'xtc', 'trr', 'edr', 'log']
    outputs = grompp_outputs + mdrun_outputs
    running = 0
    if (pers.get("running")):
        running = pers.get("running")
    it = iterate.iterations(inp, inputs, outputs, pers)
    out = inp.getFunctionOutput()
    for i in xrange(running, it.getN()):
        gromppInstName = "grompp_%d" % i
        mdrunInstName = "mdrun_%d" % i
        out.addInstance(gromppInstName, "grompp")
        out.addInstance(mdrunInstName, "mdrun")
        out.addConnection('%s:out.tpr' % gromppInstName,
                          '%s:in.tpr' % mdrunInstName)
        it.connectOnly(grompp_inputs, grompp_outputs, out, i, gromppInstName)
        it.connectOnly(mdrun_inputs, mdrun_outputs, out, i, mdrunInstName)
        running += 1
    pers.set("running", running)
    pers.write()
    return out
示例#2
0
def runGmxCheckGap(fileTypeFlag, checkFile):
    cmdnames = cmds.GromacsCommands()
    cmd = cmdnames.gmxcheck.split() + [fileTypeFlag, checkFile]
    proc = subprocess.Popen(cmd,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
    #noMatchLine=re.compile(r'Timesteps at.*don\'t match.*\(([0-9.]+),\s([0-9.]+)\)')

    stdExpected = None

    for line in proc.stdout:
        #m = noMatchLine.match(line)
        #if m:
        if 'Timesteps at t=' in line:
            log.debug("%s, %s", checkFile, line)
            parts = line.split()
            thisExpected = float(parts[5][1:-1])
            thisActual = float(parts[6][:-1])
            if stdExpected == None:
                stdExpected = thisExpected
            if thisActual > stdExpected:
                log.debug("Discarding %s due to a gap in the file" % checkFile)
                return False

    return True
示例#3
0
 def __init__(self, persDir):
     self.persDir = persDir
     lastfound = True
     self.trajlist = []
     self.lastcpt = None
     lastrundir = None
     i = 0
     while lastfound:
         i += 1
         currundir = os.path.join(persDir, "run_%03d" % i)
         lastfound = False
         try:
             st = os.stat(currundir)
             if st.st_mode & stat.S_IFDIR:
                 lastfound = True
                 lastrundir = currundir
                 trajl = self.checkResults(lastrundir)
                 self.trajlist.append(trajl)
         except OSError:
             pass
     self.lastDirNr = i
     self.lastDir = lastrundir
     self.newRunDir = currundir
     self.lastTrajNr = self._extractLastTrajNr()
     self.cmdnames = cmds.GromacsCommands()
示例#4
0
def mdruns(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.trjcat)
        cpc.util.plugin.testCommand("%s -version" % cmdnames.eneconv)
        cpc.util.plugin.testCommand("%s -version" % cmdnames.gmxdump)
        return

    pers = cpc.dataflow.Persistence(
        os.path.join(inp.getPersistentDir(), "persistent.dat"))

    inputs = ['tpr', 'priority', 'cmdline_options', 'resources']
    outputs = ['conf', 'xtc', 'trr', 'edr', 'log']
    running = 0
    if (pers.get("running")):
        running = pers.get("running")
    it = iterate.iterations(inp, inputs, outputs, pers)
    out = inp.getFunctionOutput()
    for i in xrange(running, it.getN()):
        instName = "mdrun_%d" % i
        out.addInstance(instName, "mdrun")
        it.connect(out, i, instName)
        running += 1
    pers.set("running", running)
    pers.write()
    return out
示例#5
0
def grompps(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.grompp)
        return

    pers = cpc.dataflow.Persistence(
        os.path.join(inp.getPersistentDir(), "persistent.dat"))

    inputs = ['mdp', 'top', 'conf', 'ndx', 'settings', 'include']
    outputs = ['tpr']
    running = 0
    if (pers.get("running")):
        running = pers.get("running")
    it = iterate.iterations(inp, inputs, outputs, pers)
    out = inp.getFunctionOutput()
    for i in xrange(running, it.getN()):
        instName = "grompp_%d" % i
        out.addInstance(instName, "grompp")
        it.connect(out, i, instName)
        out.addConnection("%s:out.tpr" % instName, "self:ext_out.tpr[%d]" % i)
        running += 1
    pers.set("running", running)
    pers.write()
    return out
示例#6
0
def tune_fn(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        #cpc.util.plugin.testCommand("grompp -version")
        #cpc.util.plugin.testCommand("mdrun -version")
        return
    fo = inp.getFunctionOutput()
    persDir = inp.getPersistentDir()
    mdpfile = procSettings(inp, inp.getOutputDir())
    # copy the topology and include files
    topfile = os.path.join(inp.getOutputDir(), 'topol.top')
    shutil.copy(inp.getInput('top'), topfile)
    incl = inp.getInput('include')
    if incl is not None and len(incl) > 0:
        for i in range(len(incl)):
            filename = inp.getInput('include[%d]' % i)
            if filename is not None:
                # same name, but in one directory.
                nname = os.path.join(inp.getOutputDir(),
                                     os.path.split(filename)[1])
                shutil.copy(filename, nname)
    # and execute grompp
    cmdlist = cmdnames.grompp.split()
    cmdlist += [
        "-f",
        mdpfile,
        "-quiet",
        "-c",
        inp.getInput('conf'),
        "-p",
        'topol.top',  # we made sure it's there
        "-o",
        "topol.tpr"
    ]
    if inp.hasInput('ndx'):
        cmdlist.append('-n')
        cmdlist.append(inp.getInput('ndx'))
    proc = subprocess.Popen(cmdlist,
                            stdin=None,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT,
                            cwd=inp.getOutputDir())
    (stdo, stde) = proc.communicate(None)
    if proc.returncode != 0:
        #raise GromacsError("Error running grompp: %s"%
        #                   (open(stdoutfn,'r').read()))
        fo.setError("Error running grompp: %s, %s" % (stdo, stde))
        return fo
    rsrc = Resources()
    tune.tune(rsrc, inp.getInput('conf'),
              os.path.join(inp.getOutputDir(), 'topol.tpr'), persDir)
    fo.setOut('mdp', FileValue(mdpfile))
    fo.setOut('resources', rsrc.setOutputValue())
    return fo
示例#7
0
def extractConf(tprFile, confFile):
    """Extract a configuration to confFile from tprFile."""
    cmdnames = cmds.GromacsCommands()
    cmdlist = cmdnames.editconf.split() + ['-f', tprFile, '-o', confFile]
    proc = subprocess.Popen(cmdlist,
                            stdin=None,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT,
                            cwd=os.path.split(confFile)[0])
    proc.communicate(None)
    if proc.returncode != 0:
        raise GromacsError("Error running editconf: %s" % proc.stdout)
示例#8
0
def g_energy(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.g_energy)
        return
    edrfile=inp.getInput('edr')
    item=inp.getInput('item')
    outDir=inp.getOutputDir()
    xvgoutname=os.path.join(outDir, "energy.xvg")
    cmdlist = cmdnames.g_energy.split() + ["-f", edrfile, "-o", xvgoutname]
    proc=subprocess.Popen(cmdlist,
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT,
                          cwd=inp.getOutputDir(),
                          close_fds=True)
    (stdout, stderr)=proc.communicate(item)
    if proc.returncode != 0:
        raise GromacsError("ERROR: g_energy returned %s"%(stdout))
    regitem=re.compile(r'^%s'%(item))
    regsplit=re.compile(r'---------------')
    splitmatch=False
    for line in iter(stdout.splitlines()):
        if not splitmatch:
            if regsplit.match(line):
                splitmatch=True
        else:
            if regitem.match(line):
                foundmatch=True
                sp=line.split()
                av=float(sp[1])
                err=float(sp[2])
                rmsd=float(sp[3])
                drift=float(sp[4])
                unit=sp[5]
                break
    if not foundmatch:
        raise GromacsError("ERROR: couldn't find match for energy item %s in output."%
                           item)
    fo=inp.getFunctionOutput()
    fo.setOut('xvg', FileValue(xvgoutname))
    fo.setOut('average', FloatValue(av))
    fo.setOut('error', FloatValue(err))
    fo.setOut('rmsd', FloatValue(rmsd))
    fo.setOut('drift', FloatValue(drift))
    fo.setOut('unit', StringValue(unit))
    return fo
示例#9
0
def tryRun(tprFile, runDir, Ncores):
    """Try to run mdrun with Ncores cores."""
    cmdnames = cmds.GromacsCommands()
    cmdlist = cmdnames.mdrun.split()
    cmdlist += [
        "-nt",
        "%d" % Ncores, "-s", tprFile, "-rcon", "0.7", "-maxh", "0.0005"
    ]
    proc = subprocess.Popen(cmdlist,
                            stdin=None,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT,
                            cwd=runDir)
    (stdo, stde) = proc.communicate(None)
    if proc.returncode != 0:
        return (False, stdo)
    return (True, stdo)
示例#10
0
def pdb2gmx(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.pdb2gmx)
        return
    input_choices=inp.getInput('input_choices')
    if input_choices is None:
        input_choices=''
    pdbfile=inp.getInput('conf')
    #pdbfile=os.path.join(inp.getOutputDir(),inp.getInput('conf'))
    #shutil.copy(inp.getInput('conf'),pdbfile)
    forcefield=inp.getInput('ff')
    watermodel=inp.getInput('water')
    skip_hydrogens=True #default to ignh
    if inp.getInput('cmdline_options') is not None:
        cmdlineOpts=shlex.split(inp.getInput('cmdline_options'))
    else:
        cmdlineOpts=[]
    cmdline = cmdnames.pdb2gmx
    cmdline += ["-f", pdbfile, "-ff", forcefield, "-water", watermodel]
    if skip_hydrogens:
        cmdline.extend(["-ignh"])
    cmdline.extend(cmdlineOpts)

    proc=subprocess.Popen(cmdline,
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT,
                          cwd=inp.getOutputDir(),
                          close_fds=True)
    (stdout, stderr)=proc.communicate(input_choices)
    if proc.returncode != 0:
        raise GromacsError("ERROR: pdb2gmx returned %s"%(stdout))
    fo=inp.getFunctionOutput()
    fo.setOut('conf', FileValue(os.path.join(inp.getOutputDir(),'conf.gro')))
    fo.setOut('top', FileValue(os.path.join(inp.getOutputDir(),'topol.top')))
    #how do we handle itp output files?
    itpfiles=glob.glob(os.path.join(inp.getOutputDir(),'*.itp'))
    fo.setOut('include',itpfiles)
    for i in xrange(len(itpfiles)):
        fo.setOut('include[%d]'%(i),itpfiles[i])
    return fo
示例#11
0
def _eneconv(inp, fo):
    """Internal implementation of eneconv"""
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.eneconv)
        return
    pers=cpc.dataflow.Persistence(os.path.join(inp.getPersistentDir(),
                                               "persistent.dat"))
    if pers.get('init') is None:
        init=True
        pers.set('init', 1)
    else:
        inpItems=[ 'edr_files', 'scalefac', 'dt', 'offset', 'cmdline_options' ]
        if not checkUpdated(inp, inpItems):
            return
        init=False
    writeStdin=StringIO()

    edrFilesList=inp.getInput('edr_files')

    outDir=inp.getOutputDir()
    edrOutname=os.path.join(outDir, "fixed.edr")
    #cmdline=["eneconv", '-f', edrFiles, '-o', edrOutname]
    cmdline = cmdnames.eneconv.split() + ['-f']
    for i in xrange(len(edrFilesList)):
        cmdline.append(inp.getInput('edr_files[%d]' % i))

    cmdline.extend(['-o', edrOutname])

    first_frame_ps=inp.getInput('first_frame_ps')
    if first_frame_ps is not None:
        cmdline.extend(['-b', "%g"%first_frame_ps] )
    last_frame_ps=inp.getInput('last_frame_ps')
    if last_frame_ps is not None:
        cmdline.extend(['-e', "%g"%last_frame_ps] )
    dt=inp.getInput('dt')
    if dt is not None:
        cmdline.extend(['-dt', "%g"%dt] )
    offset=inp.getInput('offset')
    if offset is not None:
        cmdline.extend(['-offset', "%g"%offset] )
    scaleF=inp.getInput('scalefac')
    if scaleF is not None:
        cmdline.extend(['-scalefac', "%g"%scaleF] )
    if inp.getInput('cmdline_options') is not None:
        cmdlineOpts=shlex.split(inp.getInput('cmdline_options'))
    else:
        cmdlineOpts=[]
    cmdline.extend(cmdlineOpts)
    log.debug(cmdline)

    proc=subprocess.Popen(cmdline,
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT,
                          cwd=inp.getOutputDir(),
                          close_fds=True)
    (stdout, stderr)=proc.communicate(writeStdin.getvalue())
    if proc.returncode != 0:
        raise GromacsError("ERROR: eneconv returned %s"%(stdout))
    fo.setOut('edr', FileValue(edrOutname))

    pers.write()
示例#12
0
def _trjconv(inp, fo, split):
    """Internal implementation of trjconv and trjconv_split"""
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.trjconv)
        return
    pers=cpc.dataflow.Persistence(os.path.join(inp.getPersistentDir(),
                                               "persistent.dat"))
    if pers.get('init') is None:
        init=True
        pers.set('init', 1)
    else:
        inpItems=[ 'traj', 'tpr', 'ndx', 'dt', 'skip', 'dump', 'pbc', 'ur',
                   'center', 'fit', 'fit_type', 'cmdline_options' ]
        if not checkUpdated(inp, inpItems):
            return
        init=False
    writeStdin=StringIO()
    trajfile=inp.getInput('traj')
    tprfile=inp.getInput('tpr')
    #item=inp.getInput('item')
    outDir=inp.getOutputDir()
    xtcoutname=os.path.join(outDir, "trajout.xtc")
    grooutname=os.path.join(outDir, "out.gro")
    cmdline = cmdnames.trjconv.split() + ['-s', tprfile, '-f', trajfile]
    if not split:
        cmdline.extend(['-o', xtcoutname])
    else:
        cmdline.extend(['-sep', '-o', grooutname])
    ndxfile=inp.getInput('ndx')
    if ndxfile is not None:
        cmdline.extend(['-n', ndxfile] )
    first_frame_ps=inp.getInput('first_frame_ps')
    if first_frame_ps is not None:
        cmdline.extend(['-b', "%g"%first_frame_ps] )
    last_frame_ps=inp.getInput('last_frame_ps')
    if last_frame_ps is not None:
        cmdline.extend(['-b', "%g"%last_frame_ps] )
    dt=inp.getInput('dt')
    if dt is not None:
        cmdline.extend(['-dt', "%g"%dt] )
    skip=inp.getInput('skip')
    if skip is not None:
        cmdline.extend(['-skip', "%d"%skip] )
    dump=inp.getInput('dump')
    if dump is not None:
        cmdline.extend(['-dump', "%g"%dump] )
    pbc=inp.getInput('pbc')
    if pbc is not None:
        cmdline.extend(['-pbc', pbc] )
    ur=inp.getInput('ur')
    if ur is not None:
        cmdline.extend(['-ur', ur] )
    center=inp.getInput('center')
    if center is not None:
        cmdline.extend(['-center'])
        writeStdin.write("%s\n"%center)
    fit=inp.getInput('fit')
    fit_type=inp.getInput('fit_type')
    if fit is not None:
        if center is not None:
            raise GromacsError('Both fit and center set')
        if fit_type is None:
            fit_type='rot+trans'
        cmdline.extend(['-fit', fit_type])
        writeStdin.write("%s\n"%fit)
    if inp.getInput('cmdline_options') is not None:
        cmdlineOpts=shlex.split(inp.getInput('cmdline_options'))
    else:
        cmdlineOpts=[]
    cmdline.extend(cmdlineOpts)
    log.debug(cmdline)
    outputGroup=inp.getInput('output_group')
    if outputGroup is not None:
        writeStdin.write("%s\n"%outputGroup)
    else:
        writeStdin.write("System\n")

    proc=subprocess.Popen(cmdline,
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT,
                          cwd=inp.getOutputDir(),
                          close_fds=True)
    (stdout, stderr)=proc.communicate(writeStdin.getvalue())
    if proc.returncode != 0:
        raise GromacsError("ERROR: trjconv returned %s"%(stdout))
    if not split:
        fo.setOut('xtc', FileValue(xtcoutname))
    else:
        i=0
        # iterate as long as there are files with anme 'out%d.gro' for
        # increasing i
        while True:
            filename=os.path.join(outDir, 'out%d.gro'%i)
            if not os.path.exists(filename):
                break
            fo.setOut('confs[%d]'%i, FileValue(filename))
            i+=1
    pers.write()
示例#13
0
def mdrun(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.trjcat)
        cpc.util.plugin.testCommand("%s -version" % cmdnames.eneconv)
        cpc.util.plugin.testCommand("%s -version" % cmdnames.gmxdump)
        return
    persDir = inp.getPersistentDir()
    outDir = inp.getOutputDir()
    fo = inp.getFunctionOutput()
    rsrc = Resources(inp.getInputValue("resources"))
    rsrcFilename = os.path.join(persDir, 'rsrc.dat')
    # check whether we need to reinit
    pers = cpc.dataflow.Persistence(os.path.join(persDir, "persistent.dat"))
    init = False
    lasttpr = pers.get('lasttpr')
    newtpr = inp.getInput('tpr')
    #if inp.getInputValue('tpr').isUpdated():
    if newtpr != lasttpr:
        lasttpr = newtpr
        # there was no previous command.
        # purge the persistent directory, by moving the confout files to a
        # backup directory
        log.debug("(Re)initializing mdrun")
        confout = glob.glob(os.path.join(persDir, "run_???"))
        if len(confout) > 0:
            backupDir = os.path.join(persDir, "backup")
            try:
                os.mkdir(backupDir)
            except OSError:
                pass
            for conf in confout:
                try:
                    os.rename(conf,
                              os.path.join(backupDir,
                                           os.path.split(conf)[-1]))
                except OSError:
                    pass
        init = True
        pers.set('lasttpr', lasttpr)
    elif inp.cmd is None:
        return fo
    if init:
        if rsrc.max.get('cores') is None:
            confFile = os.path.join(persDir, 'conf.gro')
            extractConf(newtpr, confFile)
            tune.tune(rsrc, confFile, newtpr, persDir)
        if inp.cmd is not None:
            log.debug("Canceling commands")
            fo.cancelPrevCommands()
        pers.set('initialized', True)
    else:
        if rsrc.max.get('cores') is None:
            rsrc.load(rsrcFilename)
    if inp.cmd is not None:
        log.debug("Return code was %s" % str(inp.cmd.getReturncode()))
    # try to find out whether the run has already finished
    confout = glob.glob(os.path.join(persDir, "run_???", "confout.*gro"))
    if len(confout) > 0:
        confoutDir = os.path.dirname(confout[0])
        hasFinalData = checkConfoutDir(confoutDir)
        if hasFinalData:
            log.debug("Extracting data. ")
            # confout exists. we're finished. Concatenate all the runs if
            # we need to, but first create the output dict
            extractData(confout, outDir, persDir, fo)
            return fo

    tfc = TrajFileCollection(persDir)
    lastDir = tfc.getLastDir()
    # first check whether we got an error code back
    if (inp.cmd is not None) and inp.cmd.getReturncode() != 0:
        # there was a problem. Check the log
        if lastDir:
            stde = os.path.join(lastDir, "stderr")
            if checkErr(stde, rsrc, newtpr, persDir):
                if os.path.exists(stde):
                    stdef = open(stde, 'r')
                    errmsg = unicode(stdef.read(), errors='ignore')
                    stdef.close()
                    raise MdrunError("Error running mdrun: %s" % errmsg)
        else:
            log.debug("An error has occured, but no lastDir was found.")

        # now check whether any of the last 4 iterations produced
        # trajectories
        trajlist = tfc.getTrajList()
        if len(trajlist) > 4:
            ret = False
            for j in range(4):
                haveTraj = (len(trajlist[-j - 1]) > 0)
                ret = ret or haveTraj  #prevtraj[-j-1]
            if not ret:
                if lastDir:
                    stde = os.path.join(lastDir, "stderr")
                    if os.path.exists(stde):
                        stdef = open(stde, 'r')
                        errmsg = unicode(stdef.read(), errors='ignore')
                        stdef.close()
                    else:
                        errmsg = ""
                    raise MdrunError(
                        "Error running mdrun. No trajectories: %s" % errmsg)
                else:
                    raise MdrunError(
                        "Error running mdrun. No trajectories and no lastDir was found."
                    )
    # Make a new directory with the continuation of this run
    #newdirname=currundir #"run_%03d"%(i+1)
    newdirname = tfc.getNewRunDir()
    log.debug("Making a new directory for this run: %s" % newdirname)
    try:
        os.mkdir(newdirname)
    except OSError:
        log.debug("Directory already exists.")
        pass
    tpr = newtpr
    src = os.path.join(inp.getBaseDir(), tpr)
    dst = os.path.join(newdirname, "topol.tpr")
    shutil.copy(src, dst)
    # handle command line inputs
    if inp.getInput('cmdline_options') is not None:
        cmdlineOpts = shlex.split(inp.getInput('cmdline_options'))
    else:
        cmdlineOpts = []
    if inp.getInput('priority') is not None:
        prio = inp.getInput('priority')
    else:
        prio = 0
    lastcpt = tfc.getLastCpt()
    # copy the checkpoint to the new cmd dir
    if lastcpt is not None:
        shutil.copy(lastcpt, os.path.join(newdirname, "state.cpt"))
        log.debug("Continuing from checkpoint")
    # now add to the priority if this run has already been started
    completed = tfc.getFractionCompleted(tpr)
    if completed > 0:
        log.debug("Fraction completed: %s" % completed)
        # Already finished, but no confout.gro?
        if completed >= 1:
            log.debug(
                "Iteration finished, but the final coordinates were not written."
            )
            if tfc.trajlist[-1].get('edr') or tfc.trajlist[-1].get(
                    'xtc') or tfc.trajlist[-1].get('trr'):
                log.debug(
                    "Last run produced output files without gaps (but no confout.gro). Generating coordinates from checkpoint."
                )
                confout = tfc.checkpointToConfout()
                if confout:
                    log.debug("Extracting data.")
                    extractData([confout], outDir, persDir, fo)
                    return fo
            else:
                log.debug(
                    "Last run did not produce any output files. Cannot generate coordinates from checkpoint."
                )
        # now the priority ranges from 1 to 4, depending on how
        # far along the simulation is.
        prio += 1 + int(3 * (completed))
        log.debug("Setting new priority to %d because it's in progress" % prio)
    # we can always add state.cpt, even if it doesn't exist.
    args = [
        "-quiet", "-s", "topol.tpr", "-noappend", "-cpi", "state.cpt", "-rcon",
        "0.7"
    ]
    args.extend(cmdlineOpts)
    # for the new neighbor search scheme in Gromacs 4.6, set this env
    # variable

    # any expected output files.
    newFileNr = tfc.getLastTrajNr() + 1
    outputFiles = [
        "traj.part%04d.xtc" % newFileNr,
        "traj.part%04d.trr" % newFileNr,
        "confout.part%04d.gro" % newFileNr,
        "ener.part%04d.edr" % newFileNr,
        "dhdl.part%04d.xvg" % newFileNr,
        "pullx.part%04d.xvg" % newFileNr,
        "pullf.part%04d.xvg" % newFileNr,
        "md.part%04d.log" % newFileNr, "state.cpt", "state_prev.cpt"
    ]
    log.debug("Expected output files: %s" % outputFiles)
    cmd = cpc.command.Command(newdirname,
                              "gromacs/mdrun",
                              args,
                              minVersion=cpc.command.Version("4.5"),
                              addPriority=prio,
                              outputFiles=outputFiles)
    if inp.hasInput("resources") and inp.getInput("resources") is not None:
        #log.debug("resources is %s"%(inp.getInput("resources")))
        #rsrc=Resources(inp.getInputValue("resources"))
        rsrc.updateCmd(cmd)
    log.debug("Adding command")
    fo.addCommand(cmd)
    if inp.getInputValue('tpr').isUpdated() and inp.cmd is not None:
        log.debug("Canceling commands")
        fo.cancelPrevCommands()

    # and save for further invocations
    rsrc.save(rsrcFilename)
    pers.write()
    return fo
示例#14
0
def extractData(confout, outDir, persDir, fo):
    """Concatenate all output data from the partial runs into the end results"""
    cmdnames = cmds.GromacsCommands()
    #outputs=dict()
    # Concatenate stuff
    confoutPath = os.path.join(outDir, "confout.gro")
    shutil.copy(confout[0], confoutPath)
    #outputs['conf'] = Value(confoutPath,
    #                        inp.function.getOutput('conf').getType())
    fo.setOut('conf', FileValue(confoutPath))
    # fix the xtc files
    xtcso = sorted(glob.glob(os.path.join(persDir, "run_???", "traj.*xtc")))
    # cull empty files and duplicate trajectory names
    xtcs = []
    xtcbase = []
    try:
        for file in xtcso:
            st = os.stat(file)
            base = os.path.split(file)[1]
            if st.st_size > 0:
                if base not in xtcbase:
                    xtcs.append(file)
                    xtcbase.append(base)
                else:
                    # there already was a file with this name. Overwrite
                    # it because mdrun wasn't aware of it when writing.
                    ind = xtcbase.index(base)
                    xtcs[ind] = file
    except OSError:
        pass

    # concatenate them
    xtcoutname = os.path.join(outDir, "traj.xtc")
    if len(xtcs) > 0:
        cmd = cmdnames.trjcat.split() + ["-f"]
        cmd.extend(xtcs)
        cmd.extend(["-o", xtcoutname])
        stdo = open(os.path.join(persDir, "trjcat_xtc.out"), "w")
        sp = subprocess.Popen(cmd, stdout=stdo, stderr=subprocess.STDOUT)
        sp.communicate(None)
        stdo.close()
        fo.setOut('xtc', FileValue(xtcoutname))
    # do the trrs
    trrso = sorted(glob.glob(os.path.join(persDir, "run_???", "traj.*trr")))
    # cull empty files and duplicate trajectory names
    trrs = []
    trrbase = []
    try:
        for file in trrso:
            st = os.stat(file)
            base = os.path.split(file)[1]
            if st.st_size > 0:
                if base not in trrbase:
                    trrs.append(file)
                    trrbase.append(base)
                else:
                    # there already was a file with this name. Overwrite
                    # it because mdrun wasn't aware of it when writing.
                    ind = trrbase.index(base)
                    trrs[ind] = file
    except OSError:
        pass
    # concatenate them
    trroutname = os.path.join(outDir, "traj.trr")
    if len(trrs) > 0:
        cmd = cmdnames.trjcat.split() + ["-f"]
        cmd.extend(trrs)
        cmd.extend(["-o", trroutname])
        stdo = open(os.path.join(persDir, "trjcat_trr.out"), "w")
        sp = subprocess.Popen(cmd, stdout=stdo, stderr=subprocess.STDOUT)
        sp.communicate(None)
        stdo.close()
        fo.setOut('trr', FileValue(trroutname))
    # and the edrs
    edrso = glob.glob(os.path.join(persDir, "run_???", "ener.*edr"))
    # cull empty files and duplicate trajectory names
    edrs = []
    edrbase = []
    try:
        for file in edrso:
            st = os.stat(file)
            base = os.path.split(file)[1]
            if st.st_size > 0:
                if base not in edrbase:
                    edrs.append(file)
                    edrbase.append(base)
                else:
                    # there already was a file with this name. Overwrite
                    # it because mdrun wasn't aware of it when writing.
                    ind = edrbase.index(base)
                    log.debug("Overwriting existing edr file %s with %s" %
                              (edrs[ind], file))
                    edrs[ind] = file
    except OSError:
        pass
    edroutname = os.path.join(outDir, "ener.edr")
    if len(edrs) > 1:
        log.debug("Concatenating edr files: %s" % edrs)
    # concatenate them
    if len(edrs) > 0:
        cmd = cmdnames.eneconv.split() + ["-f"]
        cmd.extend(edrs)
        cmd.extend(["-o", edroutname])
        stdo = open(os.path.join(persDir, "eneconv.out"), "w")
        sp = subprocess.Popen(cmd, stdout=stdo, stderr=subprocess.STDOUT)
        sp.communicate(None)
        stdo.close()
        log.debug("Setting edr output to %s" % edroutname)
        fo.setOut('edr', FileValue(edroutname))
    # do the stdout
    stdouto = glob.glob(os.path.join(persDir, "run_???", "stdout"))
    stdoutname = os.path.join(outDir, "stdout")
    outf = open(stdoutname, "w")
    for infile in stdouto:
        inf = open(infile, "r")
        outf.write(inf.read())
        inf.close()
    outf.write("%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S"))
    outf.write("%f\n" % time.time())
    outf.close()
    fo.setOut('stdout', FileValue(stdoutname))
    # do the stderr
    stderro = glob.glob(os.path.join(persDir, "run_???", "stderr"))
    stderrname = os.path.join(outDir, "stderr")
    outf = open(stderrname, "w")
    for infile in stderro:
        inf = open(infile, "r")
        outf.write(inf.read())
        inf.close()
    outf.close()
    fo.setOut('stderr', FileValue(stderrname))
    # and do md.log
    logo = glob.glob(os.path.join(persDir, "run_???", "md.*log"))
    logname = os.path.join(outDir, "md.log")
    outf = open(logname, "w")
    for infile in logo:
        inf = open(infile, "r")
        outf.write(inf.read())
        inf.close()
    outf.close()
    fo.setOut('log', FileValue(logname))

    log.debug("Returning without command.")
    log.debug("fo.cmds=%s" % str(fo.cmds))
示例#15
0
def grompp(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing whether the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.grompp)
        return

    #log.debug("base dir=%s"%inp.getBaseDir())
    #log.debug("output dir=%s"%inp.getOutputDir())
    #log.debug("persistent dir=%s"%inp.getPersistentDir())
    pers = cpc.dataflow.Persistence(
        os.path.join(inp.getPersistentDir(), "persistent.dat"))
    fo = inp.getFunctionOutput()
    if not (inp.getInputValue('conf').isUpdated()
            or inp.getInputValue('top').isUpdated()
            or inp.getInputValue('include').isUpdated()
            or inp.getInputValue('settings').isUpdated()
            or inp.getInputValue('ndx').isUpdated()):
        if pers.get('init') is not None:
            return fo
    if pers.get('init') is not None:
        log.debug("conf: %s" % (inp.getInputValue('conf').isUpdated()))
        log.debug("top: %s" % (inp.getInputValue('top').isUpdated()))
        log.debug("include: %s" % (inp.getInputValue('include').isUpdated()))
        log.debug("settings: %s" % (inp.getInputValue('settings').isUpdated()))
        log.debug("ndx: %s" % (inp.getInputValue('ndx').isUpdated()))

    pers.set('init', 1)
    mdpfile = procSettings(inp, inp.getOutputDir())
    # copy the topology and include files
    topfile = os.path.join(inp.getOutputDir(), 'topol.top')
    shutil.copy(inp.getInput('top'), topfile)
    incl = inp.getInput('include')
    if incl is not None and len(incl) > 0:
        for i in range(len(incl)):
            filename = inp.getInput('include[%d]' % i)
            if filename is not None:
                # same name, but in one directory.
                nname = os.path.join(inp.getOutputDir(),
                                     os.path.split(filename)[1])
                shutil.copy(filename, nname)
    # and execute grompp
    cmdlist = cmdnames.grompp.split() + [
        "-f",
        mdpfile,
        "-quiet",
        "-c",
        inp.getInput('conf'),
        "-p",
        'topol.top',
        # we made sure it's there
        "-o",
        "topol.tpr"
    ]
    if inp.hasInput('ndx'):
        cmdlist.append('-n')
        cmdlist.append(inp.getInput('ndx'))
    # TODO: symlink all the auxiliary files into the run dir
    stdoutfn = os.path.join(inp.getOutputDir(), "stdout")
    stdoutf = open(stdoutfn, "w")
    stdoutf.write("%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S"))
    stdoutf.write("%f\n" % time.time())
    #stdoutf=open(os.path.join(inp.getOutputDir(), "stderr"),"w")
    proc = subprocess.Popen(cmdlist,
                            stdin=None,
                            stdout=stdoutf,
                            stderr=subprocess.STDOUT,
                            cwd=inp.getOutputDir())
    proc.communicate(None)
    stdoutf.close()
    if proc.returncode != 0:
        raise GromacsError("Error running grompp: %s" %
                           (open(stdoutfn, 'r').read()))
    fo.setOut('stdout', FileValue(stdoutfn))
    fo.setOut('tpr', FileValue(os.path.join(inp.getOutputDir(), "topol.tpr")))
    pers.write()
    return fo