def test_residue_loop(self): """Test the proper operation of the residue loop with residue selection. The function tested is pipe_control.mol_res_spin.residue_loop(). """ # Loop over the residues. for res in mol_res_spin.residue_loop('#Ap4Aase:Glu'): # Test the selection. self.assertEqual(res.num, 2) # Test loop length. self.assertEqual(len(list(mol_res_spin.residue_loop('#Ap4Aase:Glu'))), 1)
def test_calc(self): """The spectral density calculation test.""" # Execute the script. self.script_exec(status.install_path + sep + 'test_suite' + sep + 'system_tests' + sep + 'scripts' + sep + 'jw_mapping_calc_test.py') # Correct jw values. j0 = [4.0703318681008998e-09, 3.7739393907014834e-09] jwx = [1.8456254300773903e-10, 1.6347516082378241e-10] jwh = [1.5598167512718012e-12, 2.9480536599037041e-12] # Loop over residues. index = 0 for res in residue_loop(): # Residues -2 and -1 have data. if res.num == -2 or res.num == -1: self.assert_(res.spin[0].select) self.assertAlmostEqual(res.spin[0].j0 * 1e9, j0[index] * 1e9) self.assertAlmostEqual(res.spin[0].jwh * 1e10, jwh[index] * 1e10) self.assertAlmostEqual(res.spin[0].jwx * 1e12, jwx[index] * 1e12) index = index + 1 # Other residues have insufficient data. else: self.assert_(not res.spin[0].select)
def test_calc(self): """The consistency testing calculation test.""" # Execute the script. self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'consistency_tests_calc_test.py') # Correct consistency functions values. j0 = [4.0703318681008998e-09, 3.7739393907014834e-09] f_eta = [0.20413244790407614, 0.18898977395296815] f_r2 = [2.0482909381655862e-09, 1.8998154021753067e-09] # Loop over residues. index = 0 for res in residue_loop(): # Residues -2 and -1 have data. if res.num == -2 or res.num == -1: self.assert_(res.spin[0].select) self.assertAlmostEqual(res.spin[0].j0, j0[index]) self.assertAlmostEqual(res.spin[0].f_eta, f_eta[index]) self.assertAlmostEqual(res.spin[0].f_r2, f_r2[index]) index = index + 1 # Other residues have insufficient data. else: self.assert_(not res.spin[0].select)
def test_calc(self): """The spectral density calculation test.""" # Execute the script. self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'jw_mapping_calc_test.py') # Correct jw values. j0 = [4.0703318681008998e-09, 3.7739393907014834e-09] jwx = [1.8456254300773903e-10, 1.6347516082378241e-10] jwh = [1.5598167512718012e-12, 2.9480536599037041e-12] # Loop over residues. index = 0 for res in residue_loop(): # Residues -2 and -1 have data. if res.num == -2 or res.num == -1: self.assert_(res.spin[0].select) self.assertAlmostEqual(res.spin[0].j0 * 1e9, j0[index] * 1e9) self.assertAlmostEqual(res.spin[0].jwh * 1e10, jwh[index] * 1e10) self.assertAlmostEqual(res.spin[0].jwx * 1e12, jwx[index] * 1e12) index = index + 1 # Other residues have insufficient data. else: self.assert_(not res.spin[0].select)
def test_set_value(self): """The user function value.set().""" # Execute the script. self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'jw_mapping_set_value.py') # Loop over residues. for res in residue_loop(): self.assertAlmostEqual(res.spin[0].csa, N15_CSA)
def test_set_value(self): """The user function value.set().""" # Execute the script. self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'consistency_tests_set_value.py') # Loop over residues. for res in residue_loop(): self.assertAlmostEqual(res.spin[0].csa, N15_CSA)
def fixme_test_boolean_parenthesis_selection(self): """Test complex boolean mol-res-spin selections with parenthesis.""" # The selection loop: sel = list(mol_res_spin.residue_loop("(#Ap4Aase & :Pro) | (#RNA & :-4)")) # Test: self.assertEqual(len(sel), 2) for res in sel: self.assert_(res.num in [-4, 4])
def test_boolean_and_selection(self): """Test boolean and in mol-res-spin selections.""" # The selection loop: sel = list(mol_res_spin.residue_loop("#Ap4Aase:4 & :Pro")) # Test: self.assertEqual(len(sel), 1) for res in sel: self.assert_(res.name == "Pro" and res.num == 4)
def fixme_test_boolean_parenthesis_selection(self): """Test complex boolean mol-res-spin selections with parenthesis.""" # The selection loop: sel = list( mol_res_spin.residue_loop("(#Ap4Aase & :Pro) | (#RNA & :-4)")) # Test: self.assertEqual(len(sel), 2) for res in sel: self.assert_(res.num in [-4, 4])
def test_boolean_complex_selection(self): """Test complex boolean mol-res-spin selections.""" # The residue selection loop. sel = list(mol_res_spin.residue_loop("#Ap4Aase:4 & :Pro | #RNA")) # Residue names and numbers. names = ['Pro', None, None] numbers = [4, -5, -4] # The residues. self.assertEqual(len(sel), 3) for i in range(3): self.assertEqual(sel[i].name, names[i]) self.assertEqual(sel[i].num, numbers[i])
def test_residue_loop_no_data(self): """Test the proper operation of the residue loop when no data is present. The function tested is pipe_control.mol_res_spin.residue_loop(). """ # Reset relax. reset() # Add a data pipe to the data store. ds.add(pipe_name='orig', pipe_type='mf') # Loop over the residues. i = 0 for residue in mol_res_spin.residue_loop(): i = i + 1 # Test loop length. self.assertEqual(i, 0)
def test_residue_loop_no_selection(self): """Test the proper operation of the residue loop when no selection is present. The function tested is pipe_control.mol_res_spin.residue_loop(). """ # Spin data. num = [1, 2, 4, -5, -4] name = [None, 'Glu', 'Pro', None, None] # Loop over the residues. i = 0 for res in mol_res_spin.residue_loop(): # Test the residue numbers. self.assertEqual(res.num, num[i]) # Test the residue names. self.assertEqual(res.name, name[i]) # Increment i. i = i + 1 # Test loop length. self.assertEqual(i, 5)
def fail_test(): for residue in mol_res_spin.residue_loop(): pass
def create_script(file, model_type, algor): """Create the Dasha script file. @param file: The opened file descriptor. @type file: file object @param model_type: The model-free model type. @type model_type: str @param algor: The optimisation algorithm to use. This can be the Levenberg-Marquardt algorithm 'LM' or the Newton-Raphson algorithm 'NR'. @type algor: str """ # Delete all data. file.write("# Delete all data.\n") file.write("del 1 10000\n") # Nucleus type. file.write("\n# Nucleus type.\n") nucleus = None for spin in spin_loop(): # Skip protons. if spin.isotope == '1H': continue # Can only handle one spin type. if nucleus and spin.isotope != nucleus: raise RelaxError( "The nuclei '%s' and '%s' do not match, relax can only handle one nucleus type in Dasha." % (nucleus, spin.isotope)) # Set the nucleus. if not nucleus: nucleus = spin.isotope # Convert the name and write it. if nucleus == '15N': nucleus = 'N15' elif nucleus == '13C': nucleus = 'C13' else: raise RelaxError("Cannot handle the nucleus type '%s' within Dasha." % nucleus) file.write("set nucl %s\n" % nucleus) # Number of frequencies. file.write("\n# Number of frequencies.\n") file.write("set n_freq %s\n" % cdp.spectrometer_frq_count) # Frequency values. file.write("\n# Frequency values.\n") count = 1 for frq in loop_frequencies(): file.write("set H1_freq %s %s\n" % (frq / 1e6, count)) count += 1 # Set the diffusion tensor. file.write("\n# Set the diffusion tensor.\n") if model_type != 'local_tm': # Sphere. if cdp.diff_tensor.type == 'sphere': file.write("set tr %s\n" % (cdp.diff_tensor.tm / 1e-9)) # Spheroid. elif cdp.diff_tensor.type == 'spheroid': file.write('set tr %s\n' % (cdp.diff_tensor.tm / 1e-9)) # Ellipsoid. elif cdp.diff_tensor.type == 'ellipsoid': # Get the eigenvales. Dx, Dy, Dz = diffusion_tensor.return_eigenvalues() # Geometric parameters. file.write("set tr %s\n" % (cdp.diff_tensor.tm / 1e-9)) file.write("set D1/D3 %s\n" % (Dx / Dz)) file.write("set D2/D3 %s\n" % (Dy / Dz)) # Orientational parameters. file.write("set alfa %s\n" % (cdp.diff_tensor.alpha / (2.0 * pi) * 360.0)) file.write("set betta %s\n" % (cdp.diff_tensor.beta / (2.0 * pi) * 360.0)) file.write("set gamma %s\n" % (cdp.diff_tensor.gamma / (2.0 * pi) * 360.0)) # Reading the relaxation data. file.write("\n# Reading the relaxation data.\n") file.write("echo Reading the relaxation data.\n") noe_index = 1 r1_index = 1 r2_index = 1 for ri_id in cdp.ri_ids: # NOE. if cdp.ri_type[ri_id] == 'NOE': # Data set number. number = noe_index # Data type. data_type = 'noe' # Increment the data set index. noe_index = noe_index + 1 # R1. elif cdp.ri_type[ri_id] == 'R1': # Data set number. number = r1_index # Data type. data_type = '1/T1' # Increment the data set index. r1_index = r1_index + 1 # R2. elif cdp.ri_type[ri_id] == 'R2': # Data set number. number = r2_index # Data type. data_type = '1/T2' # Increment the data set index. r2_index = r2_index + 1 # Set the data type. if number == 1: file.write("\nread < %s\n" % data_type) else: file.write("\nread < %s %s\n" % (data_type, number)) # The relaxation data. for residue in residue_loop(): # Alias the spin. spin = residue.spin[0] # Skip deselected spins. if not spin.select: continue # Skip and deselect spins for which relaxation data is missing. if len(spin.ri_data) != len( cdp.ri_ids) or spin.ri_data[ri_id] == None: spin.select = False continue # Data and errors. file.write( "%s %s %s\n" % (residue.num, spin.ri_data[ri_id], spin.ri_data_err[ri_id])) # Terminate the reading. file.write("exit\n") # Individual residue optimisation. if model_type == 'mf': # Loop over the residues. for residue in residue_loop(): # Alias the spin. spin = residue.spin[0] # Skip deselected spins. if not spin.select: continue # Get the interatomic data containers. interatoms = return_interatom_list(spin_hash=spin._hash) if len(interatoms) == 0: raise RelaxNoInteratomError elif len(interatoms) > 1: raise RelaxError( "Only one interatomic data container, hence dipole-dipole interaction, is supported per spin." ) # Comment. file.write("\n\n\n# Residue %s\n\n" % residue.num) # Echo. file.write("echo Optimisation of residue %s\n" % residue.num) # Select the spin. file.write("\n# Select the residue.\n") file.write("set cres %s\n" % residue.num) # The angle alpha of the XH vector in the spheroid diffusion frame. if cdp.diff_tensor.type == 'spheroid': file.write("set teta %s\n" % spin.alpha) # The angles theta and phi of the XH vector in the ellipsoid diffusion frame. elif cdp.diff_tensor.type == 'ellipsoid': file.write( "\n# Setting the spherical angles of the XH vector in the ellipsoid diffusion frame.\n" ) file.write("set teta %s\n" % spin.theta) file.write("set fi %s\n" % spin.phi) # The 'jmode'. if 'ts' in spin.params: jmode = 3 elif 'te' in spin.params: jmode = 2 elif 's2' in spin.params: jmode = 1 # Chemical exchange. if 'rex' in spin.params: exch = True else: exch = False # Anisotropic diffusion. if cdp.diff_tensor.type == 'sphere': anis = False else: anis = True # Axial symmetry. if cdp.diff_tensor.type == 'spheroid': sym = True else: sym = False # Set the jmode. file.write("\n# Set the jmode.\n") file.write("set def jmode %s" % jmode) if exch: file.write(" exch") if anis: file.write(" anis") if sym: file.write(" sym") file.write("\n") # Parameter default values. file.write("\n# Parameter default values.\n") file.write("reset jmode %s\n" % residue.num) # Bond length. file.write("\n# Bond length.\n") file.write("set r_hx %s\n" % (interatoms[0].r / 1e-10)) # CSA value. file.write("\n# CSA value.\n") file.write("set csa %s\n" % (spin.csa / 1e-6)) # Fix the tf parameter if it isn't in the model. if not 'tf' in spin.params and jmode == 3: file.write("\n# Fix the tf parameter.\n") file.write("fix tf 0\n") # Optimisation of all residues. file.write("\n\n\n# Optimisation of all residues.\n") if algor == 'LM': file.write("lmin %s %s" % (first_residue_num(), last_residue_num())) elif algor == 'NR': file.write("min %s %s" % (first_residue_num(), last_residue_num())) # Show the results. file.write("\n# Show the results.\n") file.write("echo\n") file.write("show all\n") # Write the results. file.write("\n# Write the results.\n") file.write("write s2.out S\n") file.write("write s2f.out Sf\n") file.write("write s2s.out Ss\n") file.write("write te.out te\n") file.write("write tf.out tf\n") file.write("write ts.out ts\n") file.write("write rex.out rex\n") file.write("write chi2.out F\n") else: raise RelaxError( "Optimisation of the parameter set '%s' currently not supported." % model_type)
def update_mol(self, mol, mol_id): """Update the given molecule in the tree. @param mol: The molecule container. @type mol: MoleculeContainer instance @param mol_id: The molecule identification string. @type mol_id: str """ # Find the molecule, if it already exists. new_mol = True for key in self.tree_ids: # Get the python data. data = self.tree.GetItemPyData(key) # No info. if data == None or 'id' not in data: continue # Check the mol_id for a match and, if so, terminate to speed things up. if mol_id == data['id']: new_mol = False mol_branch_id = key break # A new molecule. if new_mol: # Append a molecule with name to the tree. mol_branch_id = self.tree.AppendItem(self.root, "Molecule: %s" % mol.name) # The data to store. data = { 'type': 'mol', 'mol_name': mol.name, 'id': mol_id, 'select': is_mol_selected(mol_id) } self.tree.SetPyData(mol_branch_id, data) # Add the id to the tracking structure. self.tree_ids[mol_branch_id] = {} # Set the bitmap. self.set_bitmap_mol(mol_branch_id, select=data['select']) # An old molecule. else: # Check the selection state. select = is_mol_selected(data['id']) # Change of state. if select != data['select']: # Store the new state. data['select'] = select # Set the bitmap. self.set_bitmap_mol(mol_branch_id, select=data['select']) # Update the residues of this molecule. for res, res_id in residue_loop(mol_id, return_id=True): self.update_res(mol_branch_id, mol, res, res_id) # Start new molecules expanded. if new_mol and data['select']: self.tree.Expand(mol_branch_id) # Remove any deleted residues. self.prune_res(mol_branch_id, mol_id) # Expand the root. self.tree.Expand(self.root)
def create_script(file, model_type, algor): """Create the Dasha script file. @param file: The opened file descriptor. @type file: file object @param model_type: The model-free model type. @type model_type: str @param algor: The optimisation algorithm to use. This can be the Levenberg-Marquardt algorithm 'LM' or the Newton-Raphson algorithm 'NR'. @type algor: str """ # Delete all data. file.write("# Delete all data.\n") file.write("del 1 10000\n") # Nucleus type. file.write("\n# Nucleus type.\n") nucleus = None for spin in spin_loop(): # Skip protons. if spin.isotope == '1H': continue # Can only handle one spin type. if nucleus and spin.isotope != nucleus: raise RelaxError("The nuclei '%s' and '%s' do not match, relax can only handle one nucleus type in Dasha." % (nucleus, spin.isotope)) # Set the nucleus. if not nucleus: nucleus = spin.isotope # Convert the name and write it. if nucleus == '15N': nucleus = 'N15' elif nucleus == '13C': nucleus = 'C13' else: raise RelaxError("Cannot handle the nucleus type '%s' within Dasha." % nucleus) file.write("set nucl %s\n" % nucleus) # Number of frequencies. file.write("\n# Number of frequencies.\n") file.write("set n_freq %s\n" % cdp.spectrometer_frq_count) # Frequency values. file.write("\n# Frequency values.\n") count = 1 for frq in loop_frequencies(): file.write("set H1_freq %s %s\n" % (frq / 1e6, count)) count += 1 # Set the diffusion tensor. file.write("\n# Set the diffusion tensor.\n") if model_type != 'local_tm': # Sphere. if cdp.diff_tensor.type == 'sphere': file.write("set tr %s\n" % (cdp.diff_tensor.tm / 1e-9)) # Spheroid. elif cdp.diff_tensor.type == 'spheroid': file.write('set tr %s\n' % (cdp.diff_tensor.tm / 1e-9)) # Ellipsoid. elif cdp.diff_tensor.type == 'ellipsoid': # Get the eigenvales. Dx, Dy, Dz = diffusion_tensor.return_eigenvalues() # Geometric parameters. file.write("set tr %s\n" % (cdp.diff_tensor.tm / 1e-9)) file.write("set D1/D3 %s\n" % (Dx / Dz)) file.write("set D2/D3 %s\n" % (Dy / Dz)) # Orientational parameters. file.write("set alfa %s\n" % (cdp.diff_tensor.alpha / (2.0 * pi) * 360.0)) file.write("set betta %s\n" % (cdp.diff_tensor.beta / (2.0 * pi) * 360.0)) file.write("set gamma %s\n" % (cdp.diff_tensor.gamma / (2.0 * pi) * 360.0)) # Reading the relaxation data. file.write("\n# Reading the relaxation data.\n") file.write("echo Reading the relaxation data.\n") noe_index = 1 r1_index = 1 r2_index = 1 for ri_id in cdp.ri_ids: # NOE. if cdp.ri_type[ri_id] == 'NOE': # Data set number. number = noe_index # Data type. data_type = 'noe' # Increment the data set index. noe_index = noe_index + 1 # R1. elif cdp.ri_type[ri_id] == 'R1': # Data set number. number = r1_index # Data type. data_type = '1/T1' # Increment the data set index. r1_index = r1_index + 1 # R2. elif cdp.ri_type[ri_id] == 'R2': # Data set number. number = r2_index # Data type. data_type = '1/T2' # Increment the data set index. r2_index = r2_index + 1 # Set the data type. if number == 1: file.write("\nread < %s\n" % data_type) else: file.write("\nread < %s %s\n" % (data_type, number)) # The relaxation data. for residue in residue_loop(): # Alias the spin. spin = residue.spin[0] # Skip deselected spins. if not spin.select: continue # Skip and deselect spins for which relaxation data is missing. if len(spin.ri_data) != len(cdp.ri_ids) or spin.ri_data[ri_id] == None: spin.select = False continue # Data and errors. file.write("%s %s %s\n" % (residue.num, spin.ri_data[ri_id], spin.ri_data_err[ri_id])) # Terminate the reading. file.write("exit\n") # Individual residue optimisation. if model_type == 'mf': # Loop over the residues. for residue in residue_loop(): # Alias the spin. spin = residue.spin[0] # Skip deselected spins. if not spin.select: continue # Get the interatomic data containers. interatoms = return_interatom_list(spin._spin_ids[0]) if len(interatoms) == 0: raise RelaxNoInteratomError elif len(interatoms) > 1: raise RelaxError("Only one interatomic data container, hence dipole-dipole interaction, is supported per spin.") # Comment. file.write("\n\n\n# Residue %s\n\n" % residue.num) # Echo. file.write("echo Optimisation of residue %s\n" % residue.num) # Select the spin. file.write("\n# Select the residue.\n") file.write("set cres %s\n" % residue.num) # The angle alpha of the XH vector in the spheroid diffusion frame. if cdp.diff_tensor.type == 'spheroid': file.write("set teta %s\n" % spin.alpha) # The angles theta and phi of the XH vector in the ellipsoid diffusion frame. elif cdp.diff_tensor.type == 'ellipsoid': file.write("\n# Setting the spherical angles of the XH vector in the ellipsoid diffusion frame.\n") file.write("set teta %s\n" % spin.theta) file.write("set fi %s\n" % spin.phi) # The 'jmode'. if 'ts' in spin.params: jmode = 3 elif 'te' in spin.params: jmode = 2 elif 's2' in spin.params: jmode = 1 # Chemical exchange. if 'rex' in spin.params: exch = True else: exch = False # Anisotropic diffusion. if cdp.diff_tensor.type == 'sphere': anis = False else: anis = True # Axial symmetry. if cdp.diff_tensor.type == 'spheroid': sym = True else: sym = False # Set the jmode. file.write("\n# Set the jmode.\n") file.write("set def jmode %s" % jmode) if exch: file.write(" exch") if anis: file.write(" anis") if sym: file.write(" sym") file.write("\n") # Parameter default values. file.write("\n# Parameter default values.\n") file.write("reset jmode %s\n" % residue.num) # Bond length. file.write("\n# Bond length.\n") file.write("set r_hx %s\n" % (interatoms[0].r / 1e-10)) # CSA value. file.write("\n# CSA value.\n") file.write("set csa %s\n" % (spin.csa / 1e-6)) # Fix the tf parameter if it isn't in the model. if not 'tf' in spin.params and jmode == 3: file.write("\n# Fix the tf parameter.\n") file.write("fix tf 0\n") # Optimisation of all residues. file.write("\n\n\n# Optimisation of all residues.\n") if algor == 'LM': file.write("lmin %s %s" % (first_residue_num(), last_residue_num())) elif algor == 'NR': file.write("min %s %s" % (first_residue_num(), last_residue_num())) # Show the results. file.write("\n# Show the results.\n") file.write("echo\n") file.write("show all\n") # Write the results. file.write("\n# Write the results.\n") file.write("write s2.out S\n") file.write("write s2f.out Sf\n") file.write("write s2s.out Ss\n") file.write("write te.out te\n") file.write("write tf.out tf\n") file.write("write ts.out ts\n") file.write("write rex.out rex\n") file.write("write chi2.out F\n") else: raise RelaxError("Optimisation of the parameter set '%s' currently not supported." % model_type)
def update_mol(self, mol, mol_id): """Update the given molecule in the tree. @param mol: The molecule container. @type mol: MoleculeContainer instance @param mol_id: The molecule identification string. @type mol_id: str """ # Find the molecule, if it already exists. new_mol = True for key in self.tree_ids: # Get the python data. if dep_check.wx_classic: data = self.tree.GetItemPyData(key) else: data = self.tree.GetItemData(key) # No info. if data == None or 'id' not in data: continue # Check the mol_id for a match and, if so, terminate to speed things up. if mol_id == data['id']: new_mol = False mol_branch_id = key break # A new molecule. if new_mol: # Append a molecule with name to the tree. mol_branch_id = self.tree.AppendItem(self.root, "Molecule: %s" % mol.name) # The data to store. data = { 'type': 'mol', 'mol_name': mol.name, 'id': mol_id, 'select': is_mol_selected(mol_id) } if dep_check.wx_classic: self.tree.SetPyData(mol_branch_id, data) else: self.tree.SetItemData(mol_branch_id, data) # Add the id to the tracking structure. self.tree_ids[mol_branch_id] = {} # Set the bitmap. self.set_bitmap_mol(mol_branch_id, select=data['select']) # An old molecule. else: # Check the selection state. select = is_mol_selected(data['id']) # Change of state. if select != data['select']: # Store the new state. data['select'] = select # Set the bitmap. self.set_bitmap_mol(mol_branch_id, select=data['select']) # Update the residues of this molecule. for res, res_id in residue_loop(mol_id, return_id=True): self.update_res(mol_branch_id, mol, res, res_id) # Start new molecules expanded. if new_mol and data['select']: self.tree.Expand(mol_branch_id) # Remove any deleted residues. self.prune_res(mol_branch_id, mol_id) # Expand the root. self.tree.Expand(self.root)