コード例 #1
0
def analyze_directory(experiment_dir):
    """Return free energy and error of a single experiment.
    Parameters
    ----------
    experiment_dir : str
        The path to the directory storing the nc files.
    Return
    ------
    DeltaF : simtk.unit.Quantity
        Difference in free energy between the end states in kcal/mol.
    """
    import yaml
    from simtk import unit
    from yank.analyze import get_analyzer

    analysis_script_filepath = os.path.join(experiment_dir, 'analysis.yaml')

    # Load sign of alchemical phases.
    with open(analysis_script_filepath, 'r') as f:
        analysis_script = yaml.load(f)

    # Generate analysis object.
    analysis = {}
    for phase_name, sign in analysis_script:
        phase_path = os.path.join(experiment_dir, phase_name + '.nc')
        analyzer = get_analyzer(phase_path)
        analysis[phase_name] = analyzer.analyze_phase()
        kT = analyzer.kT
        #print("kT", kT)
        #print("k/T", float(kT/298.15))
        unit_conversion = kT / unit.kilocalories_per_mole
        #print("analysis[phase_name]",analysis[phase_name] )
        #analysis[str(phase_name)+'_dG_kcal/mol'] = float(analysis[phase_name]['free_energy_diff']*unit_conversion)
        analysis['unit_conversion'] = float(unit_conversion)
    return analysis
コード例 #2
0
ファイル: utils.py プロジェクト: zeromtmu/openmm_orion
def analyze_directory(source_directory):
    """
    This Function has been copied and adapted from the Yank ver 0.17.0 source code
    (yank.analyse.analyze_directory)

    Analyze contents of store files to compute free energy differences.

    This function is needed to preserve the old auto-analysis style of YANK. What it exactly does can be refined
    when more analyzers and simulations are made available. For now this function exposes the API.

    Parameters
    ----------
    source_directory : string
        The location of the simulation storage files.

    """
    analysis_script_path = os.path.join(source_directory, 'analysis.yaml')
    if not os.path.isfile(analysis_script_path):
        err_msg = 'Cannot find analysis.yaml script in {}'.format(
            source_directory)
        raise RuntimeError(err_msg)
    with open(analysis_script_path, 'r') as f:
        analysis = yaml.load(f)

    data = dict()
    for phase_name, sign in analysis:
        phase_path = os.path.join(source_directory, phase_name + '.nc')
        phase = get_analyzer(phase_path)
        data[phase_name] = phase.analyze_phase()
        kT = phase.kT

    # Compute free energy and enthalpy
    DeltaF = 0.0
    dDeltaF = 0.0
    DeltaH = 0.0
    dDeltaH = 0.0
    for phase_name, sign in analysis:
        DeltaF -= sign * (data[phase_name]['DeltaF'] +
                          data[phase_name]['DeltaF_standard_state_correction'])
        dDeltaF += data[phase_name]['dDeltaF']**2
        DeltaH -= sign * (data[phase_name]['DeltaH'] +
                          data[phase_name]['DeltaF_standard_state_correction'])
        dDeltaH += data[phase_name]['dDeltaH']**2
    dDeltaF = np.sqrt(dDeltaF)
    dDeltaH = np.sqrt(dDeltaH)

    DeltaF = DeltaF * kT / unit.kilocalories_per_mole
    dDeltaF = dDeltaF * kT / unit.kilocalories_per_mole
    DeltaH = DeltaH * kT / unit.kilocalories_per_mole
    dDeltaH = dDeltaH * kT / unit.kilocalories_per_mole

    return DeltaF, dDeltaF, DeltaH, dDeltaH
コード例 #3
0
def analyze_directory(experiment_dir):
    """Return free energy and error of a single experiment.

    Parameters
    ----------
    experiment_dir : str
        The path to the directory storing the nc files.

    Return
    ------
    DeltaF : simtk.unit.Quantity
        Difference in free energy between the end states in kcal/mol.
    dDeltaF: simtk.unit.Quantity
        Statistical error of the free energy estimate.

    """
    import yaml
    from simtk import unit
    from yank.analyze import get_analyzer

    analysis_script_filepath = os.path.join(experiment_dir, 'analysis.yaml')

    # Load sign of alchemical phases.
    with open(analysis_script_filepath, 'r') as f:
        analysis_script = yaml.load(f)

    # Generate analysis object.
    analysis = {}
    for phase_name, sign in analysis_script:
        phase_path = os.path.join(experiment_dir, phase_name + '.nc')
        analyzer = get_analyzer(phase_path)
        analysis[phase_name] = analyzer.analyze_phase()
        kT = analyzer.kT
        n_samples = len(analyzer.reporter._storage_dict['analysis'].
                        dimensions['iteration']) - 1
        assert n_samples == 5000, '{}'.format(n_samples)

    # Compute free energy.
    DeltaF = 0.0
    dDeltaF = 0.0
    for phase_name, sign in analysis_script:
        DeltaF -= sign * (
            analysis[phase_name]['DeltaF'] +
            analysis[phase_name]['DeltaF_standard_state_correction'])
        dDeltaF += analysis[phase_name]['dDeltaF']**2
    dDeltaF = np.sqrt(dDeltaF)

    # Convert from kT units to kcal/mol
    unit_conversion = kT / unit.kilocalories_per_mole
    return DeltaF * unit_conversion, dDeltaF * unit_conversion
コード例 #4
0
index = sys.argv[2]
solvent = sys.argv[1]
source_directory = "../{1}licit-all/experiments/cmethingesinglehinge{0}/".format(
    index, solvent)

analysis_script_path = os.path.join(source_directory, 'analysis.yaml')
with open(analysis_script_path, 'r') as f:
    analysis = yaml.load(f)
phases = dict()
max_analysis_iteration = np.inf
phase_names = [phase_name for phase_name, _ in analysis]
# Populate placeholders
for phase_name, sign in analysis:
    phase_path = os.path.join(source_directory, phase_name + '.nc')
    print("Loading {} analyzer".format(phase_name))
    analyzer = analyze.get_analyzer(phase_path)
    phases[phase_name] = {}
    phases[phase_name]['sign'] = sign
    phases[phase_name]['analyzer'] = analyzer
    # phases[phase_name]['kT'] = analyzer.kT  # This is slow
    phases[phase_name]['kT'] = kT
    print("Loading {} energy".format(phase_name))
    kln, un_kln = analyzer.get_states_energies()
    if kln.shape[-1] < max_analysis_iteration:
        max_analysis_iteration = kln.shape[-1]
    phases[phase_name]['energy'] = kln
    phases[phase_name]['unsampled'] = un_kln
    phases[phase_name][
        'standard_state'] = analyzer.get_standard_state_correction()
    phases[phase_name]['f_k'] = np.zeros(kln.shape[1] + un_kln.shape[1])
# Now do free energy