示例#1
0
def create(algor='LM', dir=None, force=False):
    """Create the Dasha script file 'dasha_script' for controlling the program.

    @keyword algor: The optimisation algorithm to use.  This can be the Levenberg-Marquardt algorithm 'LM' or the Newton-Raphson algorithm 'NR'.
    @type algor:    str
    @keyword dir:   The optional directory to place the script into.
    @type dir:      str or None
    @keyword force: A flag which if True will cause any pre-existing file to be overwritten.
    @type force:    bool
    """

    # Test if the current pipe exists.
    check_pipe()

    # Test if sequence data is loaded.
    if not exists_mol_res_spin_data():
        raise RelaxNoSequenceError

    # Determine the parameter set.
    model_type = determine_model_type()

    # Test if diffusion tensor data for the data_pipe exists.
    if model_type != 'local_tm' and not hasattr(cdp, 'diff_tensor'):
        raise RelaxNoTensorError('diffusion')

    # Test if the PDB file has been loaded (for the spheroid and ellipsoid).
    if model_type != 'local_tm' and cdp.diff_tensor.type != 'sphere' and not hasattr(
            cdp, 'structure'):
        raise RelaxNoPdbError

    # Test the optimisation algorithm.
    if algor not in ['LM', 'NR']:
        raise RelaxError(
            "The Dasha optimisation algorithm '%s' is unknown, it should either be 'LM' or 'NR'."
            % algor)

    # Deselect certain spins.
    __deselect_spins()

    # Directory creation.
    if dir == None:
        dir = pipes.cdp_name()
    mkdir_nofail(dir, verbosity=0)

    # Calculate the angle alpha of the XH vector in the spheroid diffusion frame.
    if cdp.diff_tensor.type == 'spheroid':
        angles.spheroid_frame()

    # Calculate the angles theta and phi of the XH vector in the ellipsoid diffusion frame.
    elif cdp.diff_tensor.type == 'ellipsoid':
        angles.ellipsoid_frame()

    # The 'dasha_script' file.
    script = open_write_file(file_name='dasha_script', dir=dir, force=force)
    create_script(script, model_type, algor)
    script.close()
示例#2
0
def create(algor='LM', dir=None, force=False):
    """Create the Dasha script file 'dasha_script' for controlling the program.

    @keyword algor: The optimisation algorithm to use.  This can be the Levenberg-Marquardt algorithm 'LM' or the Newton-Raphson algorithm 'NR'.
    @type algor:    str
    @keyword dir:   The optional directory to place the script into.
    @type dir:      str or None
    @keyword force: A flag which if True will cause any pre-existing file to be overwritten.
    @type force:    bool
    """

    # Test if the current pipe exists.
    check_pipe()

    # Test if sequence data is loaded.
    if not exists_mol_res_spin_data():
        raise RelaxNoSequenceError

    # Determine the parameter set.
    model_type = determine_model_type()

    # Test if diffusion tensor data for the data_pipe exists.
    if model_type != 'local_tm' and not hasattr(cdp, 'diff_tensor'):
        raise RelaxNoTensorError('diffusion')

    # Test if the PDB file has been loaded (for the spheroid and ellipsoid).
    if model_type != 'local_tm' and cdp.diff_tensor.type != 'sphere' and not hasattr(cdp, 'structure'):
        raise RelaxNoPdbError

    # Test the optimisation algorithm.
    if algor not in ['LM', 'NR']:
        raise RelaxError("The Dasha optimisation algorithm '%s' is unknown, it should either be 'LM' or 'NR'." % algor)

    # Deselect certain spins.
    __deselect_spins()

    # Directory creation.
    if dir == None:
        dir = pipes.cdp_name()
    mkdir_nofail(dir, verbosity=0)

    # Calculate the angle alpha of the XH vector in the spheroid diffusion frame.
    if cdp.diff_tensor.type == 'spheroid':
        angles.spheroid_frame()

    # Calculate the angles theta and phi of the XH vector in the ellipsoid diffusion frame.
    elif cdp.diff_tensor.type == 'ellipsoid':
        angles.ellipsoid_frame()

    # The 'dasha_script' file.
    script = open_write_file(file_name='dasha_script', dir=dir, force=force)
    create_script(script, model_type, algor)
    script.close()
示例#3
0
def assemble_param_vector(spin=None,
                          spin_id=None,
                          sim_index=None,
                          model_type=None):
    """Assemble the model-free parameter vector (as numpy array).

    If the spin argument is supplied, then the spin_id argument will be ignored.

    @keyword spin:          The spin data container.
    @type spin:             SpinContainer instance
    @keyword spin_id:       The spin identification string.
    @type spin_id:          str
    @keyword sim_index:     The optional MC simulation index.
    @type sim_index:        int
    @keyword model_type:    The optional model type, one of 'all', 'diff', 'mf', or 'local_tm'.
    @type model_type:       str or None
    @return:                An array of the parameter values of the model-free model.
    @rtype:                 numpy array
    """

    # Initialise.
    param_vector = []

    # Determine the model type.
    if not model_type:
        model_type = determine_model_type()

    # Diffusion tensor parameters.
    if model_type == 'diff' or model_type == 'all':
        # Normal parameters.
        if sim_index == None:
            # Spherical diffusion.
            if cdp.diff_tensor.type == 'sphere':
                if hasattr(cdp.diff_tensor, 'tm'):
                    param_vector.append(cdp.diff_tensor.tm)
                else:
                    param_vector.append(None)

            # Spheroidal diffusion.
            elif cdp.diff_tensor.type == 'spheroid':
                if hasattr(cdp.diff_tensor, 'tm'):
                    param_vector.append(cdp.diff_tensor.tm)
                    param_vector.append(cdp.diff_tensor.Da)
                    param_vector.append(cdp.diff_tensor.theta)
                    param_vector.append(cdp.diff_tensor.phi)
                else:
                    param_vector += [None, None, None, None]

            # Ellipsoidal diffusion.
            elif cdp.diff_tensor.type == 'ellipsoid':
                if hasattr(cdp.diff_tensor, 'tm'):
                    param_vector.append(cdp.diff_tensor.tm)
                    param_vector.append(cdp.diff_tensor.Da)
                    param_vector.append(cdp.diff_tensor.Dr)
                    param_vector.append(cdp.diff_tensor.alpha)
                    param_vector.append(cdp.diff_tensor.beta)
                    param_vector.append(cdp.diff_tensor.gamma)
                else:
                    param_vector += [None, None, None, None, None, None]

        # Monte Carlo diffusion tensor parameters.
        else:
            # Spherical diffusion.
            if cdp.diff_tensor.type == 'sphere':
                param_vector.append(cdp.diff_tensor.tm_sim[sim_index])

            # Spheroidal diffusion.
            elif cdp.diff_tensor.type == 'spheroid':
                param_vector.append(cdp.diff_tensor.tm_sim[sim_index])
                param_vector.append(cdp.diff_tensor.Da_sim[sim_index])
                param_vector.append(cdp.diff_tensor.theta_sim[sim_index])
                param_vector.append(cdp.diff_tensor.phi_sim[sim_index])

            # Ellipsoidal diffusion.
            elif cdp.diff_tensor.type == 'ellipsoid':
                param_vector.append(cdp.diff_tensor.tm_sim[sim_index])
                param_vector.append(cdp.diff_tensor.Da_sim[sim_index])
                param_vector.append(cdp.diff_tensor.Dr_sim[sim_index])
                param_vector.append(cdp.diff_tensor.alpha_sim[sim_index])
                param_vector.append(cdp.diff_tensor.beta_sim[sim_index])
                param_vector.append(cdp.diff_tensor.gamma_sim[sim_index])

    # Model-free parameters (spin specific parameters).
    if model_type != 'diff':
        # The loop.
        if spin:
            loop = [spin]
        else:
            loop = spin_loop(spin_id)

        # Loop over the spins.
        for spin in loop:
            # Skip deselected spins.
            if not spin.select:
                continue

            # Skip spins with no parameters.
            if not hasattr(spin, 'params'):
                continue

            # Loop over the model-free parameters.
            for i in range(len(spin.params)):
                # local tm.
                if spin.params[i] == 'local_tm':
                    if sim_index == None:
                        param_vector.append(spin.local_tm)
                    else:
                        param_vector.append(spin.local_tm_sim[sim_index])

                # S2.
                elif spin.params[i] == 's2':
                    if sim_index == None:
                        param_vector.append(spin.s2)
                    else:
                        param_vector.append(spin.s2_sim[sim_index])

                # S2f.
                elif spin.params[i] == 's2f':
                    if sim_index == None:
                        param_vector.append(spin.s2f)
                    else:
                        param_vector.append(spin.s2f_sim[sim_index])

                # S2s.
                elif spin.params[i] == 's2s':
                    if sim_index == None:
                        param_vector.append(spin.s2s)
                    else:
                        param_vector.append(spin.s2s_sim[sim_index])

                # te.
                elif spin.params[i] == 'te':
                    if sim_index == None:
                        param_vector.append(spin.te)
                    else:
                        param_vector.append(spin.te_sim[sim_index])

                # tf.
                elif spin.params[i] == 'tf':
                    if sim_index == None:
                        param_vector.append(spin.tf)
                    else:
                        param_vector.append(spin.tf_sim[sim_index])

                # ts.
                elif spin.params[i] == 'ts':
                    if sim_index == None:
                        param_vector.append(spin.ts)
                    else:
                        param_vector.append(spin.ts_sim[sim_index])

                # Rex.
                elif spin.params[i] == 'rex':
                    if sim_index == None:
                        param_vector.append(spin.rex)
                    else:
                        param_vector.append(spin.rex_sim[sim_index])

                # r.
                elif spin.params[i] == 'r':
                    if sim_index == None:
                        param_vector.append(spin.r)
                    else:
                        param_vector.append(spin.r_sim[sim_index])

                # CSA.
                elif spin.params[i] == 'csa':
                    if sim_index == None:
                        param_vector.append(spin.csa)
                    else:
                        param_vector.append(spin.csa_sim[sim_index])

                # Unknown parameter.
                else:
                    raise RelaxError("Unknown parameter.")

    # Return a numpy array.
    return array(param_vector, float64)
示例#4
0
def extract(dir, spin_id=None):
    """Extract the Modelfree4 results out of the 'mfout' file.

    @param dir:         The directory containing the 'mfout' file.
    @type dir:          str or None
    @keyword spin_id:   The spin identification string.
    @type spin_id:      str or None
    """

    # Test if sequence data is loaded.
    if not exists_mol_res_spin_data():
        raise RelaxNoSequenceError

    # Check for the diffusion tensor.
    if not hasattr(cdp, 'diff_tensor'):
        raise RelaxNoTensorError('diffusion')

    # The directory.
    if dir == None:
        dir = pipes.cdp_name()
    if not access(dir, F_OK):
        raise RelaxDirError('Modelfree4', dir)

    # Test if the file exists.
    if not access(dir + sep + 'mfout', F_OK):
        raise RelaxFileError('Modelfree4', dir + sep + 'mfout')

    # Determine the parameter set.
    model_type = determine_model_type()

    # Open the file.
    mfout_file = open(dir + sep + 'mfout', 'r')
    mfout_lines = mfout_file.readlines()
    mfout_file.close()

    # Get the section line positions of the mfout file.
    global_chi2_pos, diff_pos, s2_pos, s2f_pos, s2s_pos, te_pos, rex_pos, chi2_pos = line_positions(
        mfout_lines)

    # Find out if simulations were carried out.
    sims = 0
    for i in range(len(mfout_lines)):
        if search('_iterations', mfout_lines[i]):
            row = mfout_lines[i].split()
            sims = int(row[1])

    # Global data.
    if model_type in ['all', 'diff']:
        # Global chi-squared.
        row = mfout_lines[global_chi2_pos].split()
        cdp.chi2 = float(row[1])

        # Spherical diffusion tensor.
        if cdp.diff_tensor.type == 'sphere':
            # Split the lines.
            tm_row = mfout_lines[diff_pos].split()

            # Set the params.
            cdp.diff_tensor.set(param='tm', value=float(tm_row[2]))

        # Spheroid diffusion tensor.
        else:
            # Split the lines.
            tm_row = mfout_lines[diff_pos].split()
            dratio_row = mfout_lines[diff_pos + 1].split()
            theta_row = mfout_lines[diff_pos + 2].split()
            phi_row = mfout_lines[diff_pos + 3].split()

            # Set the params.
            diffusion_tensor.set([
                float(tm_row[2]),
                float(dratio_row[2]),
                float(theta_row[2]) * 2.0 * pi / 360.0,
                float(phi_row[2]) * 2.0 * pi / 360.0
            ], ['tm', 'Dratio', 'theta', 'phi'])

    # Loop over the sequence.
    pos = 0
    for spin, mol_name, res_num, res_name in spin_loop(spin_id,
                                                       full_info=True):
        # Skip deselected residues.
        if not spin.select:
            continue

        # Get the residue number from the mfout file.
        mfout_res_num = int(mfout_lines[s2_pos + pos].split()[0])

        # Skip the spin if the residue doesn't match.
        if mfout_res_num != res_num:
            continue

        # Test that the model has been set (needed to differentiate between te and ts).
        if not hasattr(spin, 'model'):
            raise RelaxNoModelError

        # Get the S2 data.
        if 's2' in spin.params:
            spin.s2, spin.s2_err = get_mf_data(mfout_lines, s2_pos + pos)

        # Get the S2f data.
        if 's2f' in spin.params or 's2s' in spin.params:
            spin.s2f, spin.s2f_err = get_mf_data(mfout_lines, s2f_pos + pos)

        # Get the S2s data.
        if 's2f' in spin.params or 's2s' in spin.params:
            spin.s2s, spin.s2s_err = get_mf_data(mfout_lines, s2s_pos + pos)

        # Get the te data.
        if 'te' in spin.params:
            spin.te, spin.te_err = get_mf_data(mfout_lines, te_pos + pos)
            spin.te = spin.te / 1e12
            spin.te_err = spin.te_err / 1e12

        # Get the ts data.
        if 'ts' in spin.params:
            spin.ts, spin.ts_err = get_mf_data(mfout_lines, te_pos + pos)
            spin.ts = spin.ts / 1e12
            spin.ts_err = spin.ts_err / 1e12

        # Get the Rex data.
        if 'rex' in spin.params:
            spin.rex, spin.rex_err = get_mf_data(mfout_lines, rex_pos + pos)
            spin.rex = spin.rex / (2.0 * pi *
                                   cdp.spectrometer_frq[cdp.ri_ids[0]])**2
            spin.rex_err = spin.rex_err / (
                2.0 * pi * cdp.spectrometer_frq[cdp.ri_ids[0]])**2

        # Get the chi-squared data.
        if not sims:
            row = mfout_lines[chi2_pos + pos].split()
            spin.chi2 = float(row[1])
        else:
            # The mfout chi2 position (with no sims) plus 2 (for the extra XML) plus the residue position times 22 (because of the simulated SSE rows).
            row = mfout_lines[chi2_pos + 2 + 22 * pos].split()
            spin.chi2 = float(row[1])

        # Increment the residue position.
        pos = pos + 1
示例#5
0
def assemble_param_vector(spin=None, spin_id=None, sim_index=None, model_type=None):
    """Assemble the model-free parameter vector (as numpy array).

    If the spin argument is supplied, then the spin_id argument will be ignored.

    @keyword spin:          The spin data container.
    @type spin:             SpinContainer instance
    @keyword spin_id:       The spin identification string.
    @type spin_id:          str
    @keyword sim_index:     The optional MC simulation index.
    @type sim_index:        int
    @keyword model_type:    The optional model type, one of 'all', 'diff', 'mf', or 'local_tm'.
    @type model_type:       str or None
    @return:                An array of the parameter values of the model-free model.
    @rtype:                 numpy array
    """

    # Initialise.
    param_vector = []

    # Determine the model type.
    if not model_type:
        model_type = determine_model_type()

    # Diffusion tensor parameters.
    if model_type == "diff" or model_type == "all":
        # Normal parameters.
        if sim_index == None:
            # Spherical diffusion.
            if cdp.diff_tensor.type == "sphere":
                if hasattr(cdp.diff_tensor, "tm"):
                    param_vector.append(cdp.diff_tensor.tm)
                else:
                    param_vector.append(None)

            # Spheroidal diffusion.
            elif cdp.diff_tensor.type == "spheroid":
                if hasattr(cdp.diff_tensor, "tm"):
                    param_vector.append(cdp.diff_tensor.tm)
                    param_vector.append(cdp.diff_tensor.Da)
                    param_vector.append(cdp.diff_tensor.theta)
                    param_vector.append(cdp.diff_tensor.phi)
                else:
                    param_vector += [None, None, None, None]

            # Ellipsoidal diffusion.
            elif cdp.diff_tensor.type == "ellipsoid":
                if hasattr(cdp.diff_tensor, "tm"):
                    param_vector.append(cdp.diff_tensor.tm)
                    param_vector.append(cdp.diff_tensor.Da)
                    param_vector.append(cdp.diff_tensor.Dr)
                    param_vector.append(cdp.diff_tensor.alpha)
                    param_vector.append(cdp.diff_tensor.beta)
                    param_vector.append(cdp.diff_tensor.gamma)
                else:
                    param_vector += [None, None, None, None, None, None]

        # Monte Carlo diffusion tensor parameters.
        else:
            # Spherical diffusion.
            if cdp.diff_tensor.type == "sphere":
                param_vector.append(cdp.diff_tensor.tm_sim[sim_index])

            # Spheroidal diffusion.
            elif cdp.diff_tensor.type == "spheroid":
                param_vector.append(cdp.diff_tensor.tm_sim[sim_index])
                param_vector.append(cdp.diff_tensor.Da_sim[sim_index])
                param_vector.append(cdp.diff_tensor.theta_sim[sim_index])
                param_vector.append(cdp.diff_tensor.phi_sim[sim_index])

            # Ellipsoidal diffusion.
            elif cdp.diff_tensor.type == "ellipsoid":
                param_vector.append(cdp.diff_tensor.tm_sim[sim_index])
                param_vector.append(cdp.diff_tensor.Da_sim[sim_index])
                param_vector.append(cdp.diff_tensor.Dr_sim[sim_index])
                param_vector.append(cdp.diff_tensor.alpha_sim[sim_index])
                param_vector.append(cdp.diff_tensor.beta_sim[sim_index])
                param_vector.append(cdp.diff_tensor.gamma_sim[sim_index])

    # Model-free parameters (spin specific parameters).
    if model_type != "diff":
        # The loop.
        if spin:
            loop = [spin]
        else:
            loop = spin_loop(spin_id)

        # Loop over the spins.
        for spin in loop:
            # Skip deselected spins.
            if not spin.select:
                continue

            # Skip spins with no parameters.
            if not hasattr(spin, "params"):
                continue

            # Loop over the model-free parameters.
            for i in range(len(spin.params)):
                # local tm.
                if spin.params[i] == "local_tm":
                    if sim_index == None:
                        param_vector.append(spin.local_tm)
                    else:
                        param_vector.append(spin.local_tm_sim[sim_index])

                # S2.
                elif spin.params[i] == "s2":
                    if sim_index == None:
                        param_vector.append(spin.s2)
                    else:
                        param_vector.append(spin.s2_sim[sim_index])

                # S2f.
                elif spin.params[i] == "s2f":
                    if sim_index == None:
                        param_vector.append(spin.s2f)
                    else:
                        param_vector.append(spin.s2f_sim[sim_index])

                # S2s.
                elif spin.params[i] == "s2s":
                    if sim_index == None:
                        param_vector.append(spin.s2s)
                    else:
                        param_vector.append(spin.s2s_sim[sim_index])

                # te.
                elif spin.params[i] == "te":
                    if sim_index == None:
                        param_vector.append(spin.te)
                    else:
                        param_vector.append(spin.te_sim[sim_index])

                # tf.
                elif spin.params[i] == "tf":
                    if sim_index == None:
                        param_vector.append(spin.tf)
                    else:
                        param_vector.append(spin.tf_sim[sim_index])

                # ts.
                elif spin.params[i] == "ts":
                    if sim_index == None:
                        param_vector.append(spin.ts)
                    else:
                        param_vector.append(spin.ts_sim[sim_index])

                # Rex.
                elif spin.params[i] == "rex":
                    if sim_index == None:
                        param_vector.append(spin.rex)
                    else:
                        param_vector.append(spin.rex_sim[sim_index])

                # r.
                elif spin.params[i] == "r":
                    if sim_index == None:
                        param_vector.append(spin.r)
                    else:
                        param_vector.append(spin.r_sim[sim_index])

                # CSA.
                elif spin.params[i] == "csa":
                    if sim_index == None:
                        param_vector.append(spin.csa)
                    else:
                        param_vector.append(spin.csa_sim[sim_index])

                # Unknown parameter.
                else:
                    raise RelaxError("Unknown parameter.")

    # Return a numpy array.
    return array(param_vector, float64)