Ejemplo n.º 1
0
def rex_map(log_fname, out_dir, dcd_fnames, log_ftype='auto'):
    # validate inputs
    if not isinstance(log_fname, basestring):
        raise TypeError("invalid log_fname")
    if not isinstance(out_dir, basestring):
        raise TypeError("invalid out_dir")
    if not dcd_fnames:
        raise ValueError("must specify input dcd files")
    # parse rexlog
    rexlog = ExchangeLog(fname=log_fname, validate=True, ftype=log_ftype)
    # parse nsavc value
    nsavc = get_nsavc(*dcd_fnames)
    # make nsavc is compatible with step values from log
    for entry in rexlog:
        if entry.step % nsavc != 0:
            raise ValueError("bad value for nsavc, is not multiple of step %d in exchangelog entry %d" % (entry.step, entry.exchange))
    # create output fnames
    out_dcd_fnames = []
    for i, temp in enumerate(rexlog.temp_array):
        fname = _myexpandpath(out_dir) + os.path.sep + 'rex_map_%02d_%6.2f.dcd' % (i, temp)
        out_dcd_fnames.append(fname)
    # write headers
    with open_dcd(dcd_fnames[0], mode='r') as fp:
        base_header = fp.export_header()
    for fname in out_dcd_fnames:
        with open_dcd(fname, mode='wb') as fp:
            tmp_header = {}
            tmp_header.update(base_header)
            tmp = ["\x02\x00\x00\x00"]
            tmp.append("%-80s" % "* This DCD was generated by rex_map")
            tmp.append("%-80s" % "* This DCD index is %d, and temp is %d" % (i,rexlog.temp_array[i]))
            tmp_header['title'] = ''.join(tmp)
            fp.import_header(tmp_header)
            fp.write_header()
    # write frames
    inp_dcd = [ open_dcd(fname, mode='rb') for fname in dcd_fnames ]
    out_dcd = [ open_dcd(fname, mode='ab') for fname in out_dcd_fnames ]
    ## ignore entries where repeat != 1
    iterator = ( entry for entry in rexlog if entry.repeat == 1 )
    current_step = 0 # how many steps have we written?
    for entry in iterator:
#        print "exchange number: %d" % entry.exchange
        while current_step < entry.step:
#            print "  step number: %d" % (current_step + nsavc)
            ###################
            # do work #########
            ###################
            for i, out in enumerate(out_dcd):
#                print "    reading from: %s" % dcd_fnames[entry.index_array[i]-1]
#                print "    writing to: %s" % out.name
                out.write(inp_dcd[entry.index_array[i]-1].read_frame())
            ##################
            # done with work #
            ##################
            current_step += nsavc
Ejemplo n.º 2
0
def get_nsavc(*fnames):
    if not fnames:
        raise ValueError("must specify input dcd files")
    nsavc_array = []
    for fname in fnames:
        with open_dcd(fname, mode='r') as fp:
            nsavc_array.append(fp.nsavc)
    nsavc0 = nsavc_array[0]
    for nsavc in nsavc_array:
        if nsavc != nsavc0:
            raise ValueError('bad value for nsavc, first dcd has nsavc = %d but dcd file "%s" has value of %d' % (nsavc, dcd_fnames[i], nsav))
    return nsavc0