Example #1
0
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
Example #2
0
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("--convergence", dest="convergence", type="int", default=0, help="Analyze convergence over specified 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=315.0, 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 > 0:
        # 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
        shift = options.convergence
        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 = {}
        while True:
            outfile = item['outfile']
            #sys.stderr.write("Processing WHAM outfile: %s\n" % outfile)
            pmf = process_pmf(outfile, shift=options.autoshift)
            # 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=150, imax=250)-correction
            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])
        
    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=25, 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
        (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
        wham_dicts = []
        for config_file in args:
            sys.stderr.write("Processing config file: %s\n" % config_file)
            wham_dict = process_config(config_file, options, start_index=options.start_index)
            wham_dict.update(wham_defaults)
            wham_dicts.append(wham_dict)
        
        combined_dict = combine_metadatas(wham_dicts)
        q.put(combined_dict)
            
        # Wait for wham to finish
        sys.stderr.write("Waiting for WHAM to complete\n")
        q.join() 
    
        try:
            outfile = outfile_q.get_nowait()
            while True:
                print outfile
                outfile_q.task_done()
                outfile = outfile_q.get_nowait()
        except:
            # queue empty
            pass