示例#1
0
文件: dgbind.py 项目: davecap/labwork
def main():    
    usage = """
        usage: %prog [options] <PMF>

        Calculate dG_bind
    """
    
    parser = optparse.OptionParser(usage)
    parser.add_option("--bmin", dest="bound_min", type="float", default=-35, help="Lower bound for binding [default: %default]")
    parser.add_option("--bmax", dest="bound_max", type="float", default=-25, help="Upper bound for binding [default: %default]")
    parser.add_option("--umin", dest="unbound_min", type="float", default=-45, help="Lower bound for standard state [default: %default]")
    parser.add_option("--umax", dest="unbound_max", type="float", default=-35, help="Upper bound for standard state [default: %default]")
    parser.add_option("--autoshift", dest="autoshift", default=True, action="store_false", help="Auto-shift PMF [default: %default]")
    parser.add_option("-s", "--std", dest="standard", default=True, action="store_false", help="Do standard state correction [default: %default]")
    
    (options, args) = parser.parse_args()
    
    pmf = process_pmf(args[0], shift=options.autoshift)
    
    correction = 0.0
    if options.standard:
        correction = dG_bind(pmf, imin=options.unbound_min, imax=options.unbound_max)
        print "Correction: %0.2f" % correction

    print dG_bind(pmf, imin=options.bound_min, imax=options.bound_max)-correction
示例#2
0
文件: wham.py 项目: davecap/labwork
def main():    
    usage = """
        usage: %prog [options] <config.ini> <config.ini> ...

        Prepare and run WHAM.
    """
    
    parser = optparse.OptionParser(usage)
    parser.add_option("-o", "--output-dir", dest="output_dir", default=None, help="Output directory [default: temporary dir]")
    parser.add_option("-p", "--output-pmf", dest="output_pmf", default=None, help="Output PMF file [default: temporary file]")
    parser.add_option("--convergence", dest="convergence", default=False, help="Analyze convergence [default: %default]")
    parser.add_option("--conv-x", dest="convergence_x", tyle="float", default=25, help="Convergence of this reaction coordinate [default: %default]")    
    parser.add_option("--conv-block-size", dest="convergence_block_size", type="float", default=80, help="Convergence block size [default: %default]")    
    parser.add_option("--conv-shift", dest="convergence_shift", type="float", default=40, help="Convergence shift size [default: %default]")    
    parser.add_option("--percentage", dest="percentage", type="int", default=25, help="Percentage of data for block size [default: %default]")    
    parser.add_option("--error", dest="error", type="int", default=0, help="Analyze error by block averaging [default: %default]")
    parser.add_option("--autoshift", dest="autoshift", default=True, action="store_false", help="Auto-shift PMF [default: %default]")
    parser.add_option("-t", "--threads", dest="worker_threads", type="int", default=1, help="Number of WHAM threads to use [default: %default]")
    parser.add_option("--wham-min", dest="wham_min", type="float", default=-48, help="Minimum bin value for WHAM [default: %default]")
    parser.add_option("--wham-max", dest="wham_max", type="float", default=0, help="Maximum bin value for WHAM [default: %default]")
    parser.add_option("--wham-bins", dest="wham_bins", type="int", default=200, help="Number of bins for WHAM [default: %default]")
    parser.add_option("--wham-tol", dest="wham_tol", type="float", default=0.0001, help="Tolerance for WHAM [default: %default]")
    parser.add_option("--wham-temp", dest="wham_temp", type="float", default=323.15, help="Temperature for WHAM [default: %default]")
    parser.add_option("--start-index", dest="start_index", type="int", default=0, help="Start index for data selection [default: %default]")
    parser.add_option("-d", "--data-file", dest="data_file", default="distances", help="Replica data file name [default: %default]")
    
    (options, args) = parser.parse_args()
    
    for config_file in args:
        if not os.path.exists(config_file):
            raise Exception("Config file not found at %s\n" % config_file)
    
    # start the wham threads
    sys.stderr.write("Starting %d worker threads...\n" % options.worker_threads)
    for i in range(options.worker_threads):
        t = Thread(target=worker)
        t.daemon = True
        t.start()
            
    wham_defaults = {'min':options.wham_min, 'max':options.wham_max, 'bins':options.wham_bins, 'tol':options.wham_tol, 'temp':options.wham_temp}
    
    if options.convergence:
        # note: ALWAYS COMBINES INPUT CONFIG FILES
        
        # 1) calculate blocks of data in sequential order for each config file
        #       block size is 10% of the max n
        # 2) calculate the PMFs from each block
        # 3) calculate some value from each PMF (dG_bind)
        # 4) print <block>,<value> to plot
        
        block_size = options.convergence_block_size
        shift = options.convergence_shift
        start_index = 0
        done = False
        while not done:
            end_index = start_index+block_size
            sys.stderr.write("Extracting block from %d to %d...\n" % (start_index, end_index))
            
            datasets = []
            for config_file in args:
                dataset = process_config2(config_file, options, start_index=start_index, end_index=start_index+block_size)
                # TODO: this is bad
                if not dataset:
                    done = True
                    break
                else:
                    datasets.append(dataset)

            if not done:
                metadata_file = write_datasets(datasets)
                wham_dict = wham_defaults.copy()
                wham_dict.update({'metafilepath': metadata_file, 'start_index':start_index, 'end_index':end_index})
                q.put(wham_dict)
                start_index += shift
        
        sys.stderr.write("Waiting for WHAM to complete\n")
        q.join()
        
        # process the PMFs        
        item = outfile_q.get_nowait()
        results = {}
        pmfs = {}
        while True:
            outfile = item['outfile']
            #sys.stderr.write("Processing WHAM outfile: %s\n" % outfile)
            pmf = process_pmf(outfile, shift=options.autoshift)
            pmfs[item['start_index']] = pmf
            # correction = dG_bind(pmf, imin=-32, imax=-22)
            #correction = 0.0
            # print "%d,%0.5f" % (item['start_index'],item['end_index'],dG_bind(pmf, imin=-32, imax=-22)-correction)
            #results[item['start_index']] = dG_bind(pmf, imin=-32, imax=-19)-correction
            results[item['start_index']] = dG(pmf, options.convergence_x)
            outfile_q.task_done()
            try:
                item = outfile_q.get_nowait()
            except:
                sys.stderr.write("All outfiles processed...\n")
                break
        for k in sorted(results.keys()):
            print "%d,%0.5f" % (k,results[k])
        
        # write PMFs        
        if options.output_pmf:
            fpath = options.output_pmf
        else:
            (fd, fpath) = tempfile.mkstemp()
        
        outfile = open(fpath, 'w')
        sys.stderr.write("Writing PMFs to %s\n" % fpath)
        pmf_bins = {}
        pmf_titles = []
        for p in sorted(pmfs.keys()):
            pmf_titles.append(str(p))
            for x,y in pmfs[p]:
                if x not in pmf_bins:
                    pmf_bins[x] = [y]
                else:
                    pmf_bins[x].append(y)

        outfile.write('BIN,MEAN,SEM,STDEV,MIN,MAX,%s\n' % (','.join(pmf_titles)))
        for bin in sorted(pmf_bins.keys()):
            pmf_values = ','.join([ str(v) for v in pmf_bins[bin] ])
            d = numpy.array(pmf_bins[bin])
            sem = numpy.std(d, ddof=1)/numpy.sqrt(d.size)
            outfile.write('%f,%f,%f,%f,%f,%f,%s\n' % (bin, d.mean(), sem, numpy.std(d), d.min(), d.max(), pmf_values))
        outfile.close()

    elif options.error > 0:
        # note: ALWAYS COMBINES INPUT CONFIG FILES
        
        # 1) calculate random blocks of data for each config file
        # 2) calculate PMFs for each block
        # 3) plot each PMF, get max/min values per bin, stdev per bin
        
        i=0        
        while i < options.error:
            wham_dicts = []
            for config_file in args:
                sys.stderr.write("Processing config file: %s\n" % config_file)                
                md = process_config(config_file, options, percent=options.percentage, randomize=True, start_index=options.start_index)
                md.update(wham_defaults)
                wham_dicts.append(md)
            combined_dict = combine_metadatas(wham_dicts)
            q.put(combined_dict)
            i += 1
                
        # Wait for wham to finish
        sys.stderr.write("Waiting for WHAM to complete\n")
        q.join()
        
        # process the PMFs
        error_data = {}
        item = outfile_q.get_nowait()
        while True:
            outfile = item['outfile']
            
            sys.stderr.write("Processing WHAM outfile: %s\n" % outfile)
            pmf = process_pmf(outfile, shift=options.autoshift)            
            for x,y in pmf:
                if x not in error_data:
                    error_data[x] = [y]
                else:
                    error_data[x].append(y)
            outfile_q.task_done()
            try:
                item = outfile_q.get_nowait()
            except:
                sys.stderr.write("All outfiles processed...\n")
                break

        # now we have the combined PMF data
        if options.output_pmf:
            fpath = options.output_pmf
        else:
            (fd, fpath) = tempfile.mkstemp()

        outfile = open(fpath, 'w')
        sys.stderr.write("Writing errors\n")
        outfile.write('BIN,MEAN,SEM,STDEV,MIN,MAX\n')
        for key in sorted(error_data.keys()):
            d = numpy.array(error_data[key])
            sys.stderr.write("%0.2f,%d " % (key, d.size))
            sem = numpy.std(d, ddof=1)/numpy.sqrt(d.size)
            outfile.write('%f,%f,%f,%f,%f,%f\n' % (key,d.mean(),sem,numpy.std(d),d.min(),d.max()))
        outfile.close()
        sys.stderr.write("\n"+fpath+"\n")
        
    else:
        # standard procedure
        # does not combine files
        
        # for each config file, send it to wham
        for config_file in args:
            sys.stderr.write("Processing config file: %s\n" % config_file)                
            md = process_config(config_file, options, percent=options.percentage, randomize=True, start_index=options.start_index)
            md.update(wham_defaults)
            q.put(md)
                            
        # Wait for wham to finish
        sys.stderr.write("Waiting for WHAM to complete\n")
        q.join() 
    
    
        # process the PMFs
        error_data = {}
        item = outfile_q.get_nowait()
        while True:
            outfile = item['outfile']
            sys.stderr.write("Processing WHAM outfile: %s\n" % outfile)
            pmf = process_pmf(outfile, shift=options.autoshift)            
            for x,y in pmf:
                if x not in error_data:
                    error_data[x] = [y]
                else:
                    error_data[x].append(y)
            outfile_q.task_done()
            try:
                item = outfile_q.get_nowait()
            except:
                sys.stderr.write("All outfiles processed...\n")
                break
    
        # now we have the combined PMF data
        if options.output_pmf:
            fpath = options.output_pmf
        else:
            (fd, fpath) = tempfile.mkstemp()

        outfile = open(fpath, 'w')
        
        sys.stderr.write("Writing errors\n")
        outfile.write('BIN,MEAN,SEM,STDEV,MIN,MAX\n')
        for key in sorted(error_data.keys()):
            d = numpy.array(error_data[key])
            sys.stderr.write("%0.2f,%d " % (key, d.size))
            sem = numpy.std(d, ddof=1)/numpy.sqrt(d.size)
            outfile.write('%f,%f,%f,%f,%f,%f\n' % (key,d.mean(),sem,numpy.std(d),d.min(),d.max()))
        outfile.close()
        sys.stderr.write("\n"+fpath+"\n")