Author: bugman Date: Fri Apr 4 16:03:20 2008 New Revision: 5305 URL: http://svn.gna.org/viewcvs/relax?rev=5305&view=rev Log: Created the Scientific Python specific data object. The functions specific to Scientific Python have been converted into methods of the object. The new object inherits from api_base.Str_object. Modified: 1.3/generic_fns/structure/scientific.py Modified: 1.3/generic_fns/structure/scientific.py URL: http://svn.gna.org/viewcvs/relax/1.3/generic_fns/structure/scientific.py?rev=5305&r1=5304&r2=5305&view=diff ============================================================================== --- 1.3/generic_fns/structure/scientific.py (original) +++ 1.3/generic_fns/structure/scientific.py Fri Apr 4 16:03:20 2008 @@ -33,89 +33,59 @@ module_avail = False # relax module imports. +from api_base import Str_object from data import Data as relax_data_store from relax_errors import RelaxNoPdbChainError, RelaxNoResError, RelaxPdbLoadError from relax_warnings import RelaxNoAtomWarning, RelaxZeroVectorWarning - -def load_structures(file_path, model, verbosity=False): - """Function for loading the structures from the PDB file. - - @param file_path: The full path of the file. - @type file_path: str - @param model: The PDB model to use. - @type model: int - @param verbosity: A flag which if True will cause messages to be printed. - @type verbosity: bool - """ - - # Alias the current data pipe. - cdp = relax_data_store[relax_data_store.current_pipe] - - # Use pointers (references) if the PDB data exists in another run. - for data_pipe in relax_data_store: - if hasattr(data_pipe, 'structure') and hasattr(cdp.structure, 'structures') and data_pipe.structure.file_name == file_path and data_pipe.structure.model == model: - # Make a pointer to the data. - cdp.structure.structures = data_pipe.structure.structures - +class Scientific_data(Str_object): + """The Scientific Python specific data object.""" + + def load_structures(self, file_path, model, verbosity=False): + """Function for loading the structures from the PDB file. + + @param file_path: The full path of the file. + @type file_path: str + @param model: The PDB model to use. + @type model: int + @param verbosity: A flag which if True will cause messages to be printed. + @type verbosity: bool + """ + + # Alias the current data pipe. + cdp = relax_data_store[relax_data_store.current_pipe] + + # Use pointers (references) if the PDB data exists in another run. + for data_pipe in relax_data_store: + if hasattr(data_pipe, 'structure') and hasattr(cdp.structure, 'structures') and data_pipe.structure.file_name == file_path and data_pipe.structure.model == model: + # Make a pointer to the data. + cdp.structure.structures = data_pipe.structure.structures + + # Print out. + if verbosity: + print "Using the structures from the data pipe " + `data_pipe.pipe_name` + "." + for i in xrange(len(cdp.structure.structures)): + print cdp.structure.structures[i] + + # Exit this function. + return + + # Initialisation. + cdp.structure.structures = [] + + # Load the structure i from the PDB file. + if type(model) == int: # Print out. if verbosity: - print "Using the structures from the data pipe " + `data_pipe.pipe_name` + "." - for i in xrange(len(cdp.structure.structures)): - print cdp.structure.structures[i] - - # Exit this function. - return - - # Initialisation. - cdp.structure.structures = [] - - # Load the structure i from the PDB file. - if type(model) == int: - # Print out. - if verbosity: - print "Loading structure " + `model` + " from the PDB file." - - # Load the structure into 'str'. - str = Scientific.IO.PDB.Structure(file_path, model) - - # Test the structure. - if len(str) == 0: - raise RelaxPdbLoadError, file_path - - # Print the PDB info. - if verbosity: - print str - - # Place the structure in 'cdp.structure'. - cdp.structure.structures.append(str) - - - # Load all structures. - else: - # Print out. - if verbosity: - print "Loading all structures from the PDB file." - - # First model. - i = 1 - - # Loop over all the other structures. - while 1: - # Load the pdb files. - str = Scientific.IO.PDB.Structure(file_path, i) - - # No model 1. - if len(str) == 0 and i == 1: - str = Scientific.IO.PDB.Structure(file_path) - if len(str) == 0: - raise RelaxPdbLoadError, file_path - - # Test if the last structure has been reached. + print "Loading structure " + `model` + " from the PDB file." + + # Load the structure into 'str'. + str = Scientific.IO.PDB.Structure(file_path, model) + + # Test the structure. if len(str) == 0: - del str - break + raise RelaxPdbLoadError, file_path # Print the PDB info. if verbosity: @@ -124,109 +94,142 @@ # Place the structure in 'cdp.structure'. cdp.structure.structures.append(str) - # Increment i. - i = i + 1 - - -def xh_vector(data, structure=None, unit=1): - """Function for calculating/extracting the XH vector from the loaded structure. - - @param data: The spin system data container. - @type data: Residue instance - @param structure: The structure number to get the XH vector from. If set to None and - multiple structures exist, then the XH vector will be averaged across all structures. - @type structure: int - @param unit: A flag which if set will cause the function to return the unit XH vector - rather than the full vector. - @type unit: int - @return: The XH vector (or unit vector if the unit flag is set). - @rtype: list or None - """ - - # Initialise. - vector_array = [] - ave_vector = zeros(3, float64) - - # Alias the current data pipe. - cdp = relax_data_store[relax_data_store.current_pipe] - - # Number of structures. - num_str = len(cdp.structure.structures) - - # Loop over the structures. - for i in xrange(num_str): - # The vectors from a specific structure. - if structure != None and structure != i: - continue - - # Reassign the first peptide or nucleotide chain of the first structure. - if cdp.structure.structures[i].peptide_chains: - pdb_residues = cdp.structure.structures[i].peptide_chains[0].residues - elif cdp.structure.structures[i].nucleotide_chains: - pdb_residues = cdp.structure.structures[i].nucleotide_chains[0].residues + + # Load all structures. else: - raise RelaxNoPdbChainError - - # Find the corresponding residue in the PDB. - pdb_res = None - for k in xrange(len(pdb_residues)): - if data.num == pdb_residues[k].number: - pdb_res = pdb_residues[k] - break - if pdb_res == None: - raise RelaxNoResError, data.num - - # Test if the proton atom exists for residue i. - if not pdb_res.atoms.has_key(data.proton): - warn(RelaxNoAtomWarning(data.proton, data.num)) - - # Test if the heteronucleus atom exists for residue i. - elif not pdb_res.atoms.has_key(data.heteronuc): - warn(RelaxNoAtomWarning(data.heteronuc, data.num)) - - # Calculate the vector. - else: - # Get the proton position. - posH = pdb_res.atoms[data.proton].position.array - - # Get the heteronucleus position. - posX = pdb_res.atoms[data.heteronuc].position.array - - # Calculate the XH bond vector. - vector = posH - posX - - # Unit vector. - if unit: - # Normalisation factor. - norm_factor = sqrt(dot(vector, vector)) - - # Test for zero length. - if norm_factor == 0.0: - warn(RelaxZeroVectorWarning(data.num)) - - # Calculate the normalised vector. + # Print out. + if verbosity: + print "Loading all structures from the PDB file." + + # First model. + i = 1 + + # Loop over all the other structures. + while 1: + # Load the pdb files. + str = Scientific.IO.PDB.Structure(file_path, i) + + # No model 1. + if len(str) == 0 and i == 1: + str = Scientific.IO.PDB.Structure(file_path) + if len(str) == 0: + raise RelaxPdbLoadError, file_path + + # Test if the last structure has been reached. + if len(str) == 0: + del str + break + + # Print the PDB info. + if verbosity: + print str + + # Place the structure in 'cdp.structure'. + cdp.structure.structures.append(str) + + # Increment i. + i = i + 1 + + + def xh_vector(self, data, structure=None, unit=1): + """Function for calculating/extracting the XH vector from the loaded structure. + + @param data: The spin system data container. + @type data: Residue instance + @param structure: The structure number to get the XH vector from. If set to None and + multiple structures exist, then the XH vector will be averaged across all structures. + @type structure: int + @param unit: A flag which if set will cause the function to return the unit XH vector + rather than the full vector. + @type unit: int + @return: The XH vector (or unit vector if the unit flag is set). + @rtype: list or None + """ + + # Initialise. + vector_array = [] + ave_vector = zeros(3, float64) + + # Alias the current data pipe. + cdp = relax_data_store[relax_data_store.current_pipe] + + # Number of structures. + num_str = len(cdp.structure.structures) + + # Loop over the structures. + for i in xrange(num_str): + # The vectors from a specific structure. + if structure != None and structure != i: + continue + + # Reassign the first peptide or nucleotide chain of the first structure. + if cdp.structure.structures[i].peptide_chains: + pdb_residues = cdp.structure.structures[i].peptide_chains[0].residues + elif cdp.structure.structures[i].nucleotide_chains: + pdb_residues = cdp.structure.structures[i].nucleotide_chains[0].residues + else: + raise RelaxNoPdbChainError + + # Find the corresponding residue in the PDB. + pdb_res = None + for k in xrange(len(pdb_residues)): + if data.num == pdb_residues[k].number: + pdb_res = pdb_residues[k] + break + if pdb_res == None: + raise RelaxNoResError, data.num + + # Test if the proton atom exists for residue i. + if not pdb_res.atoms.has_key(data.proton): + warn(RelaxNoAtomWarning(data.proton, data.num)) + + # Test if the heteronucleus atom exists for residue i. + elif not pdb_res.atoms.has_key(data.heteronuc): + warn(RelaxNoAtomWarning(data.heteronuc, data.num)) + + # Calculate the vector. + else: + # Get the proton position. + posH = pdb_res.atoms[data.proton].position.array + + # Get the heteronucleus position. + posX = pdb_res.atoms[data.heteronuc].position.array + + # Calculate the XH bond vector. + vector = posH - posX + + # Unit vector. + if unit: + # Normalisation factor. + norm_factor = sqrt(dot(vector, vector)) + + # Test for zero length. + if norm_factor == 0.0: + warn(RelaxZeroVectorWarning(data.num)) + + # Calculate the normalised vector. + else: + vector_array.append(vector / norm_factor) + + # Normal XH vector. else: - vector_array.append(vector / norm_factor) - - # Normal XH vector. - else: - vector_array.append(vector) - - # Return None if there are no vectors. - if not len(vector_array): - return - - # Sum the vectors. - for vector in vector_array: - # Sum. - ave_vector = ave_vector + vector - - # Average the vector. - ave_vector = ave_vector / len(vector_array) - - # Unit vector. - if unit: - ave_vector = ave_vector / sqrt(dot(ave_vector, ave_vector)) - - # Return the vector. - return ave_vector + vector_array.append(vector) + + # Return None if there are no vectors. + if not len(vector_array): + return + + # Sum the vectors. + for vector in vector_array: + # Sum. + ave_vector = ave_vector + vector + + # Average the vector. + ave_vector = ave_vector / len(vector_array) + + # Unit vector. + if unit: + ave_vector = ave_vector / sqrt(dot(ave_vector, ave_vector)) + + # Return the vector. + return ave_vector