Example #1
0
def load_state(state=None, dir=None, verbosity=1, force=False):
    """Function for loading a saved program state.

    @keyword state:     The saved state file.
    @type state:        str
    @keyword dir:       The path of the state file.
    @type dir:          str
    @keyword verbosity: The verbosity level.
    @type verbosity:    int
    @keyword force:     If True, the relax data store will be reset prior to state loading.
    @type force:        bool
    """

    # Open the file for reading.
    file = open_read_file(file_name=state, dir=dir, verbosity=verbosity)

    # Reset.
    if force:
        reset()

    # Make sure that the data store is empty.
    if not ds.is_empty():
        raise RelaxError("The relax data store is not empty.")

    # Restore from the XML.
    ds.from_xml(file)

    # Signal a change in the current data pipe.
    status.observers.pipe_alteration.notify()

    # Signal the state loading
    status.observers.state_load.notify()
Example #2
0
def macro_run(file=None, dir=None):
    """Execute the Molmol macro from the given text file.

    @keyword file:          The name of the macro file to execute.
    @type file:             str
    @keyword dir:           The name of the directory where the macro file is located.
    @type dir:              str
    """

    # Open the file for reading.
    file_path = get_file_path(file, dir)
    file = open_read_file(file, dir)

    # Loop over the commands and apply them.
    for command in file.readlines():
        molmol_obj.exec_cmd(command)
Example #3
0
def macro_run(file=None, dir=None):
    """Execute the PyMOL macro from the given text file.

    @keyword file:          The name of the macro file to execute.
    @type file:             str
    @keyword dir:           The name of the directory where the macro file is located.
    @type dir:              str
    """

    # Open the file for reading.
    file_path = get_file_path(file, dir)
    file = open_read_file(file, dir)

    # Loop over the commands and apply them.
    for command in file.readlines():
        pymol_obj.exec_cmd(command)
Example #4
0
def read(file='results', dir=None):
    """Function for reading the data out of a file."""

    # Test if the current data pipe exists.
    check_pipe()

    # Make sure that the data pipe is empty.
    if not cdp.is_empty():
        raise RelaxError("The current data pipe is not empty.")

    # Get the full file path, for later use.
    file_path = get_file_path(file_name=file, dir=dir)

    # Open the file.
    file = open_read_file(file_name=file_path)

    # Determine the format of the file.
    format = determine_format(file)

    # XML results.
    if format == 'xml':
        ds.from_xml(file, dir=dirname(file_path), pipe_to=pipes.cdp_name())

    # Columnar results (for backwards compatibility with ancient relax results model-free files).
    elif format == 'columnar':
        # Extract the data from the file.
        file_data = extract_data(file=file)

        # Strip data.
        file_data = strip(file_data)

        # Do nothing if the file does not exist.
        if not file_data:
            raise RelaxFileEmptyError

        # Read the results.
        read_columnar_results(file_data)

    # Unknown results file.
    else:
        raise RelaxError("The format of the results file " + repr(file_path) + " cannot be determined.")

    # Update all of the required metadata structures.
    mol_res_spin.metadata_update()
    interatomic.metadata_update()
Example #5
0
def read(file='results', dir=None):
    """Function for reading the data out of a file."""

    # Test if the current data pipe exists.
    check_pipe()

    # Make sure that the data pipe is empty.
    if not cdp.is_empty():
        raise RelaxError("The current data pipe is not empty.")

    # Get the full file path, for later use.
    file_path = get_file_path(file_name=file, dir=dir)

    # Open the file.
    file = open_read_file(file_name=file_path)

    # Determine the format of the file.
    format = determine_format(file)

    # XML results.
    if format == 'xml':
        ds.from_xml(file, dir=dirname(file_path), pipe_to=pipes.cdp_name())

    # Columnar results (for backwards compatibility with ancient relax results model-free files).
    elif format == 'columnar':
        # Extract the data from the file.
        file_data = extract_data(file=file)

        # Strip data.
        file_data = strip(file_data)

        # Do nothing if the file does not exist.
        if not file_data:
            raise RelaxFileEmptyError

        # Read the results.
        read_columnar_results(file_data)

    # Unknown results file.
    else:
        raise RelaxError("The format of the results file " + repr(file_path) + " cannot be determined.")
Example #6
0
def create_object(file=None, dir=None):
    """Parse the DC data file and create and return an object representation of the data.

    @keyword file:  The name of the file to open.
    @type file:     str
    @keyword dir:   The directory containing the file (defaults to the current directory if None).
    @type dir:      str or None
    @return:        The object representation of the Bruker DC file.
    @rtype:         DCObject instance
    """

    # Extract the data from the file.
    file_handle = open_read_file(file, dir)
    lines = file_handle.readlines()
    file_handle.close()

    # Create the object.
    obj = DCObject()
    obj.populate(lines)
    obj.process()

    # Return the object.
    return obj
Example #7
0
def load_state(state=None, dir=None, verbosity=1, force=False):
    """Function for loading a saved program state.

    @keyword state:     The saved state file.
    @type state:        str
    @keyword dir:       The path of the state file.
    @type dir:          str
    @keyword verbosity: The verbosity level.
    @type verbosity:    int
    @keyword force:     If True, the relax data store will be reset prior to state loading.
    @type force:        bool
    """

    # Open the file for reading.
    file = open_read_file(file_name=state, dir=dir, verbosity=verbosity)

    # Reset.
    if force:
        reset()

    # Make sure that the data store is empty.
    if not ds.is_empty():
        raise RelaxError("The relax data store is not empty.")

    # Restore from the XML.
    ds.from_xml(file)

    # Update all of the required metadata structures.
    for pipe, pipe_name in pipes.pipe_loop(name=True):
        mol_res_spin.metadata_update(pipe=pipe_name)
        interatomic.metadata_update(pipe=pipe_name)

    # Signal a change in the current data pipe.
    status.observers.pipe_alteration.notify()

    # Signal the state loading
    status.observers.state_load.notify()
Example #8
0
def read_restraints(file=None, dir=None, proton1_col=None, proton2_col=None, lower_col=None, upper_col=None, sep=None):
    """Load NOESY or ROESY constraint information from file.

    If the input file is a pre-formatted Xplor file, the column number and separator arguments will
    be ignored.


    @keyword file:          The name of the file containing the relaxation data.
    @type file:             str
    @keyword dir:           The directory where the file is located.
    @type dir:              None or str
    @keyword proton1_col:   The column containing the first proton of the NOE or ROE cross peak.
    @type proton1_col:      None or int
    @keyword proton2_col:   The column containing the second proton of the NOE or ROE cross peak.
    @type proton2_col:      None or int
    @keyword lower_col:     The column containing the lower NOE bound.
    @type lower_col:        None or int
    @keyword upper_col:     The column containing the upper NOE bound.
    @type upper_col:        None or int
    @keyword sep:           The column separator (the default is white space).
    @type sep:              None or str
    """

    # Test if the current data pipe exists.
    check_pipe()

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

    # Open the file.
    file = open_read_file(file_name=file, dir=dir)
    lines = file.readlines()
    file.close()

    # Determine the file type.
    format = __file_format(lines)

    # Parse and extract the NOE restraints.
    if format == 'xplor':
        noe_restraints = xplor.parse_noe_restraints(lines)
    elif format == 'generic':
        noe_restraints = parse_noe_restraints(lines, proton1_col=proton1_col, proton2_col=proton2_col, lower_col=lower_col, upper_col=upper_col, sep=sep)

    # Pseudoatom conversion.
    for i in range(len(noe_restraints)):
        # Loop over atom IDs.
        for j in range(2):
            # Skip normal atoms.
            if isinstance(noe_restraints[i][j], str):
                continue

            # Loop over the pseudoatoms.
            pseudo_name = None
            for k in range(len(noe_restraints[i][j])):
                # Get the spin.
                spin = return_spin(spin_id=noe_restraints[i][j][k])

                # Check the pseudoatom consistency.
                if pseudo_name and pseudo_name != spin.pseudo_name:
                    raise RelaxError("The pseudoatom names '%s' and '%s' do not match." % (pseudo_name, spin.pseudo_name))

                # Set the name.
                pseudo_name = spin.pseudo_name

            # No pseudoatom.
            if not pseudo_name:
                raise RelaxError("Cannot find the pseudoatom corresponding to the atoms in %s." % noe_restraints[i][j])

            # Otherwise, place the pseudoatom name into the NOE restraint list.
            noe_restraints[i][j] = pseudo_name

    # Place the restraints into the current data pipe.
    cdp.noe_restraints = noe_restraints

    # Check for the presence of the spin containers corresponding to the atom ids.
    for restraint in cdp.noe_restraints:
        if not return_spin(spin_id=restraint[0]):
            raise RelaxError("The spin container corresponding to '%s' cannot be found." % restraint[0])
        if not return_spin(spin_id=restraint[1]):
            raise RelaxError("The spin container corresponding to '%s' cannot be found." % restraint[1])
Example #9
0
def parse_file(file=None, dir=None):
    """Parse the DC data file and return the extracted data.

    @keyword file:  The name of the file to open.
    @type file:     str
    @keyword dir:   The directory containing the file (defaults to the current directory if None).
    @type dir:      str or None
    @return:        The data from the Bruker Dynamics Centre file.  This includes the values, the errors, the residue numbers, the integration type, the field strength frequency, the relaxation data type, the isotope, the spin name, and the BDC version number.
    @rtype:         list of float, list of float, list of int, str, float, str, str, str, str
    """

    # Extract the data from the file.
    file_handle = open_read_file(file, dir)
    lines = file_handle.readlines()
    file_handle.close()

    # Init.
    values = []
    errors = []
    res_nums = []
    int_type = None
    isotope = None
    spin_name = None

    # Loop over the data.
    in_ri_data = False
    for line in lines:
        # Split the line.
        row = split("\t", line)

        # Strip the rubbish.
        for j in range(len(row)):
            row[j] = row[j].strip()

        # Empty line.
        if len(row) == 0:
            continue

        # The DC version.
        if row[0] == 'generated by:':
            version = row[1]

        # Check for bad errors.
        if row[0] == 'Systematic error estimation of data:':
            # Badness.
            if row[1] == 'worst case per peak scenario':
                raise RelaxError("The errors estimation method \"worst case per peak scenario\" is not suitable for model-free analysis.  Please go back to the DC and switch to \"average variance calculation\".")

        # The data type.
        if row[0] == 'Project:':
            if search('T1', row[1]):
                ri_type = 'R1'
            elif search('T2', row[1]):
                ri_type = 'R2'
            elif search('NOE', row[1]):
                ri_type = 'NOE'

        # Get the frequency, converting to Hz.
        elif row[0] == 'Proton frequency[MHz]:':
            frq = float(row[1]) * 1e6

        # Inside the relaxation data section.
        elif row[0] == 'SECTION:' and row[1] == 'results':
            in_ri_data = True

        # The relaxation data.
        elif in_ri_data:
            # Skip the header.
            if row[0] == 'Peak name':
                # Catch old PDC files (to fix https://gna.org/bugs/?20152).
                pdc_file = False
                if ri_type == 'R1' and not search('R1', line):
                    pdc_file = True
                elif ri_type == 'R2' and not search('R2', line):
                    pdc_file = True
                if pdc_file:
                    raise RelaxError("The old Protein Dynamics Center (PDC) files are not supported")

                # Skip.
                continue

            # The residue info.
            res_nums.append(get_res_num(row[0]))

            # Get the relaxation data.
            if ri_type != 'NOE':
                #rx, rx_err = convert_relax_data(row[3:])
                rx = float(row[-2])
                rx_err = float(row[-1])
            else:
                rx = float(row[-3])
                rx_err = float(row[-2])

            # Append the data.
            values.append(rx)
            errors.append(rx_err)

        # The temperature.
        elif row[0] == 'Temperature (K):':
            # Set the value (not implemented yet).
            pass

        # The labelling.
        elif row[0] == 'Labelling:':
            # Store the isotope for later use.
            isotope = row[1]

            # Name the spins.
            spin_name = split('([A-Z]+)', row[1])[1]

        # The integration method.
        elif row[0] == 'Used integrals:':
            # Peak heights.
            if row[1] == 'peak intensities':
                int_type = 'height'

            # Peak volumes:
            if row[1] == 'area integral':
                int_type = 'volume'

    # Return the data.
    return values, errors, res_nums, int_type, frq, ri_type, spin_name, isotope, version
Example #10
0
def script(file=None, dir=None, analysis_type=None, model_selection=None, engine=None, model_elim=False, universal_solution=False):
    """Specify the scripts used in the analysis.

    @keyword file:                  The name of the file to open.
    @type file:                     str
    @keyword dir:                   The directory containing the file (defaults to the current directory if None).
    @type dir:                      None or str
    @keyword analysis_type:         The type of analysis performed.
    @type analysis_type:            str
    @keyword model_selection:       The model selection technique used, if relevant.
    @type model_selection:          None or str
    @keyword engine:                The software engine used in the analysis.
    @type engine:                   str
    @keyword model_elim:            A model-free specific flag specifying if model elimination was performed.
    @type model_elim:               bool
    @keyword universal_solution:    A model-free specific flag specifying if the universal solution was sought after.
    @type universal_solution:       bool
    """

    # Test if the current pipe exists.
    pipes.test()

    # Check.
    allowed = ['frame order',
               'jw',
               'mf',
               'N-state',
               'noe',
               'relax_fit'
    ]
    if analysis_type not in allowed:
        raise RelaxError("The analysis type '%s' should be one of %s." % (analysis_type, allowed))

    # Set up the experimental info data container, if needed.
    if not hasattr(cdp, 'exp_info'):
        cdp.exp_info = ExpInfo()

    # Extract the text.
    f = open_read_file(file, dir)
    text = f.read()
    f.close()

    # Init the citation structures.
    cite_id = []
    cite_key = []

    # Model selection.
    if model_selection in ['AIC', 'AICc', 'BIC', 'Bootstrap', 'CV', 'Expect', 'Overall']:
        cite_id.append('model-free model selection')
        cite_key.append('dAuvergneGooley03')

    # Model-free model elimination.
    if model_elim:
        cite_id.append('model-free model elimination')
        cite_key.append('dAuvergneGooley06')

    # Universal solution citation.
    if universal_solution:
        cite_id.append('model-free set theory')
        cite_key.append('dAuvergneGooley07')

    # Get the info box.
    info = Info_box()

    # Loop over all citations.
    for id, key in zip(cite_id, cite_key):
        # Alias the bib entry.
        bib = info.bib[key]

        # Add the citation.
        cdp.exp_info.add_citation(cite_id=id, authors=bib.author2, doi=bib.doi, pubmed_id=bib.pubmed_id, full_citation=bib.cite_short(doi=False, url=False), title=bib.title, status=bib.status, type=bib.type, journal_abbrev=bib.journal, journal_full=bib.journal_full, volume=bib.volume, page_first=bib.page_first, page_last=bib.page_last, year=bib.year)

    # Place the data in the container.
    cdp.exp_info.setup_script(file=file, dir=dir, text=text, cite_ids=cite_id, analysis_type=analysis_type, model_selection=model_selection, engine=engine, model_elim=model_elim, universal_solution=universal_solution)
Example #11
0
def parse_file(file=None, dir=None):
    """Parse the DC data file and return the extracted data.

    @keyword file:  The name of the file to open.
    @type file:     str
    @keyword dir:   The directory containing the file (defaults to the current directory if None).
    @type dir:      str or None
    @return:        The data from the Bruker Dynamics Centre file.  This includes the values, the errors, the residue numbers, the integration type, the field strength frequency, the relaxation data type, the isotope, the spin name, and the BDC version number.
    @rtype:         list of float, list of float, list of int, str, float, str, str, str, str
    """

    # Extract the data from the file.
    file_handle = open_read_file(file, dir)
    lines = file_handle.readlines()
    file_handle.close()

    # Init.
    values = []
    errors = []
    res_nums = []
    int_type = None
    isotope = None
    spin_name = None

    # Loop over the data.
    in_ri_data = False
    for line in lines:
        # Split the line.
        row = split("\t", line)

        # Strip the rubbish.
        for j in range(len(row)):
            row[j] = row[j].strip()

        # Skip empty lines.
        if len(row) == 0 or row == ['']:
            continue

        # The DC version.
        if row[0] == 'generated by:':
            version = row[1]

        # Check for bad errors.
        if row[0] == 'Systematic error estimation of data:':
            # Badness.
            if row[1] == 'worst case per peak scenario':
                raise RelaxError(
                    "The errors estimation method \"worst case per peak scenario\" is not suitable for model-free analysis.  Please go back to the DC and switch to \"average variance calculation\"."
                )

        # The data type.
        if row[0] == 'Project:':
            if search('T1', row[1]):
                ri_type = 'R1'
            elif search('T2', row[1]):
                ri_type = 'R2'
            elif search('NOE', row[1]):
                ri_type = 'NOE'

        # Get the frequency, converting to Hz.
        elif row[0] == 'Proton frequency[MHz]:':
            frq = float(row[1]) * 1e6

        # Inside the relaxation data section.
        elif row[0] == 'SECTION:' and row[1] == 'results':
            in_ri_data = True

        # The relaxation data.
        elif in_ri_data:
            # Skip the header.
            if row[0] == 'Peak name':
                # Catch old PDC files (to fix https://web.archive.org/web/https://gna.org/bugs/?20152).
                pdc_file = False
                if ri_type == 'R1' and not search('R1', line):
                    pdc_file = True
                elif ri_type == 'R2' and not search('R2', line):
                    pdc_file = True
                if pdc_file:
                    raise RelaxError(
                        "The old Protein Dynamics Center (PDC) files are not supported"
                    )

                # Skip.
                continue

            # The residue info.
            res_nums.append(get_res_num(row[0]))

            # Get the relaxation data.
            if ri_type != 'NOE':
                #rx, rx_err = convert_relax_data(row[3:])
                rx = float(row[-2])
                rx_err = float(row[-1])
            else:
                rx = float(row[-3])
                rx_err = float(row[-2])

            # Append the data.
            values.append(rx)
            errors.append(rx_err)

        # The temperature.
        elif row[0] == 'Temperature (K):':
            # Set the value (not implemented yet).
            pass

        # The labelling.
        elif row[0] == 'Labelling:':
            # Store the isotope for later use.
            isotope = row[1]

            # Name the spins.
            spin_name = split('([A-Z]+)', row[1])[1]

        # The integration method.
        elif row[0] == 'Used integrals:':
            # Peak heights.
            if row[1] == 'peak intensities':
                int_type = 'height'

            # Peak volumes:
            if row[1] == 'area integral':
                int_type = 'volume'

    # Return the data.
    return values, errors, res_nums, int_type, frq, ri_type, spin_name, isotope, version
Example #12
0
def read_restraints(file=None, dir=None, proton1_col=None, proton2_col=None, lower_col=None, upper_col=None, sep=None):
    """Load NOESY or ROESY constraint information from file.

    If the input file is a pre-formatted Xplor file, the column number and separator arguments will
    be ignored.


    @keyword file:          The name of the file containing the relaxation data.
    @type file:             str
    @keyword dir:           The directory where the file is located.
    @type dir:              None or str
    @keyword proton1_col:   The column containing the first proton of the NOE or ROE cross peak.
    @type proton1_col:      None or int
    @keyword proton2_col:   The column containing the second proton of the NOE or ROE cross peak.
    @type proton2_col:      None or int
    @keyword lower_col:     The column containing the lower NOE bound.
    @type lower_col:        None or int
    @keyword upper_col:     The column containing the upper NOE bound.
    @type upper_col:        None or int
    @keyword sep:           The column separator (the default is white space).
    @type sep:              None or str
    """

    # Test if the current data pipe exists.
    pipes.test()

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

    # Open the file.
    file = open_read_file(file_name=file, dir=dir)
    lines = file.readlines()
    file.close()

    # Determine the file type.
    format = __file_format(lines)

    # Parse and extract the NOE restraints.
    if format == 'xplor':
        noe_restraints = xplor.parse_noe_restraints(lines)
    elif format == 'generic':
        noe_restraints = parse_noe_restraints(lines, proton1_col=proton1_col, proton2_col=proton2_col, lower_col=lower_col, upper_col=upper_col, sep=sep)

    # Pseudoatom conversion.
    for i in range(len(noe_restraints)):
        # Loop over atom IDs.
        for j in range(2):
            # Skip normal atoms.
            if isinstance(noe_restraints[i][j], str):
                continue

            # Loop over the pseudoatoms.
            pseudo_name = None
            for k in range(len(noe_restraints[i][j])):
                # Get the spin.
                spin = return_spin(noe_restraints[i][j][k])

                # Check the pseudoatom consistency.
                if pseudo_name and pseudo_name != spin.pseudo_name:
                    raise RelaxError("The pseudoatom names '%s' and '%s' do not match." % (pseudo_name, spin.pseudo_name))

                # Set the name.
                pseudo_name = spin.pseudo_name

            # No pseudoatom.
            if not pseudo_name:
                raise RelaxError("Cannot find the pseudoatom corresponding to the atoms in %s." % noe_restraints[i][j])

            # Otherwise, place the pseudoatom name into the NOE restraint list.
            noe_restraints[i][j] = pseudo_name

    # Place the restraints into the current data pipe.
    cdp.noe_restraints = noe_restraints

    # Check for the presence of the spin containers corresponding to the atom ids.
    for restraint in cdp.noe_restraints:
        if not return_spin(restraint[0]):
            raise RelaxError("The spin container corresponding to '%s' cannot be found." % restraint[0])
        if not return_spin(restraint[1]):
            raise RelaxError("The spin container corresponding to '%s' cannot be found." % restraint[1])
Example #13
0
def script(file=None, dir=None, analysis_type=None, model_selection=None, engine=None, model_elim=False, universal_solution=False):
    """Specify the scripts used in the analysis.

    @keyword file:                  The name of the file to open.
    @type file:                     str
    @keyword dir:                   The directory containing the file (defaults to the current directory if None).
    @type dir:                      None or str
    @keyword analysis_type:         The type of analysis performed.
    @type analysis_type:            str
    @keyword model_selection:       The model selection technique used, if relevant.
    @type model_selection:          None or str
    @keyword engine:                The software engine used in the analysis.
    @type engine:                   str
    @keyword model_elim:            A model-free specific flag specifying if model elimination was performed.
    @type model_elim:               bool
    @keyword universal_solution:    A model-free specific flag specifying if the universal solution was sought after.
    @type universal_solution:       bool
    """

    # Test if the current pipe exists.
    check_pipe()

    # Check.
    allowed = ['frame order',
               'jw',
               'mf',
               'N-state',
               'noe',
               'relax_fit'
    ]
    if analysis_type not in allowed:
        raise RelaxError("The analysis type '%s' should be one of %s." % (analysis_type, allowed))

    # Set up the experimental info data container, if needed.
    if not hasattr(cdp, 'exp_info'):
        cdp.exp_info = ExpInfo()

    # Extract the text.
    f = open_read_file(file, dir)
    text = f.read()
    f.close()

    # Init the citation structures.
    cite_id = []
    cite_key = []

    # Model selection.
    if model_selection in ['AIC', 'AICc', 'BIC', 'Bootstrap', 'CV', 'Expect', 'Overall']:
        cite_id.append('model-free model selection')
        cite_key.append('dAuvergneGooley03')

    # Model-free model elimination.
    if model_elim:
        cite_id.append('model-free model elimination')
        cite_key.append('dAuvergneGooley06')

    # Universal solution citation.
    if universal_solution:
        cite_id.append('model-free set theory')
        cite_key.append('dAuvergneGooley07')

    # Get the info box.
    info = Info_box()

    # Loop over all citations.
    for id, key in zip(cite_id, cite_key):
        # Alias the bib entry.
        bib = info.bib[key]

        # Add the citation.
        cdp.exp_info.add_citation(cite_id=id, authors=bib.author2, doi=bib.doi, pubmed_id=bib.pubmed_id, full_citation=bib.cite_short(doi=False, url=False), title=bib.title, status=bib.status, type=bib.type, journal_abbrev=bib.journal, journal_full=bib.journal_full, volume=bib.volume, page_first=bib.page_first, page_last=bib.page_last, year=bib.year)

    # Place the data in the container.
    cdp.exp_info.setup_script(file=file, dir=dir, text=text, cite_ids=cite_id, analysis_type=analysis_type, model_selection=model_selection, engine=engine, model_elim=model_elim, universal_solution=universal_solution)