def cpmgfit_input(dir=None, binary='cpmgfit', spin_id=None, force=False): """Create the CPMGFit input files. @keyword dir: The optional directory to place the files into. If None, then the files will be placed into a directory named after the dispersion model. @type dir: str or None @keyword binary: The name of the CPMGFit binary file. This can include the path to the binary. @type binary: str @keyword spin_id: The spin ID string to restrict the file creation to. @type spin_id: str @keyword force: A flag which if True will cause all pre-existing files 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 # Test if the experiment type has been set. if not hasattr(cdp, 'exp_type'): raise RelaxError("The relaxation dispersion experiment type has not been specified.") # Test if the model has been set. if not hasattr(cdp, 'model_type'): raise RelaxError("The relaxation dispersion model has not been specified.") # Directory creation. if dir != None: mkdir_nofail(dir, verbosity=0) # The 'run.sh' script. batch = open_write_file('batch_run.sh', dir, force) batch.write("#! /bin/sh\n\n") # Generate the input files for each spin. for spin, spin_id in spin_loop(return_id=True, skip_desel=True): # Translate the model. function = translate_model(spin.model) # Create the input file. file_in = create_spin_input(function=function, spin=spin, spin_id=spin_id, dir=dir) # The output file name. file_out = spin_file_name(spin_id=spin_id, output=True) # Add the file to the batch script. batch.write("%s -grid -xmgr -f %s | tee %s\n" % (binary, file_in, file_out)) # Close the batch script, then make it executable. batch.close() if dir: chmod(dir + sep + 'batch_run.sh', S_IRWXU|S_IRGRP|S_IROTH) else: chmod('batch_run.sh', S_IRWXU|S_IRGRP|S_IROTH)
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()
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()
def nessy_input(file='save.NESSY', dir=None, spin_id=None, force=False): """Create the NESSY input files. @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory. @type dir: str or None @keyword binary: The name of the CPMGFit binary file. This can include the path to the binary. @type binary: str @keyword spin_id: The spin ID string to restrict the file creation to. @type spin_id: str @keyword force: A flag which if True will cause all pre-existing files 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 # Test if the experiment type has been set. if not hasattr(cdp, 'exp_type'): raise RelaxError( "The relaxation dispersion experiment type has not been specified." ) # Directory creation. if dir != None: mkdir_nofail(dir, verbosity=0) # The save file. save_file = open_write_file(file, dir, force) # Create the NESSY data object. data = Nessy_data(spin_id=spin_id) # Create the NESSY file. write_program_setup(file=save_file, dir=dir, data=data) # Loop over the experiments. for ei in range(data.num_exp): write_sequence(file=save_file, data=data, ei=ei) write_cpmg_datasets(file=save_file, data=data, ei=ei) write_spinlock_datasets(file=save_file, data=data, ei=ei) write_experiment_setup(file=save_file, data=data, ei=ei)
def sample(self): """Generate the ensembles by random sampling of the snapshots.""" # Create the directory for the ensembles, if needed. mkdir_nofail(dir=self.results_dir + sep + "ensembles") # Loop over the configurations. for conf_index in range(len(self.configs)): # Loop over each ensemble. for ens in range(self.num_ens): # Random sampling. rand = [] for j in range(self.num_models): rand.append( randint(self.snapshot_min[conf_index], self.snapshot_max[conf_index])) # Print out. print("Generating ensemble %s%s from structures %s." % (self.configs[conf_index], ens, rand)) # The file name. file_name = "ensembles" + sep + self.configs[ conf_index] + repr(ens) + ".pdb" # Open the output file. out = open(self.results_dir + sep + file_name, 'w') # Header. out.write("REM Structures: " + repr(rand) + "\n") # Concatenation the files. for j in range(self.num_models): # The random file. rand_name = self.snapshot_dir[ conf_index] + sep + self.configs[conf_index] + repr( rand[j]) + ".pdb" # Append the file. out.write(open(rand_name).read()) # Close the file. out.close()
def nessy_input(file='save.NESSY', dir=None, spin_id=None, force=False): """Create the NESSY input files. @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory. @type dir: str or None @keyword binary: The name of the CPMGFit binary file. This can include the path to the binary. @type binary: str @keyword spin_id: The spin ID string to restrict the file creation to. @type spin_id: str @keyword force: A flag which if True will cause all pre-existing files to be overwritten. @type force: bool """ # Test if the current pipe exists. pipes.test() # Test if sequence data is loaded. if not exists_mol_res_spin_data(): raise RelaxNoSequenceError # Test if the experiment type has been set. if not hasattr(cdp, 'exp_type'): raise RelaxError("The relaxation dispersion experiment type has not been specified.") # Directory creation. if dir != None: mkdir_nofail(dir, verbosity=0) # The save file. save_file = open_write_file(file, dir, force) # Create the NESSY data object. data = Nessy_data(spin_id=spin_id) # Create the NESSY file. write_program_setup(file=save_file, dir=dir, data=data) # Loop over the experiments. for ei in range(data.num_exp): write_sequence(file=save_file, data=data, ei=ei) write_cpmg_datasets(file=save_file, data=data, ei=ei) write_spinlock_datasets(file=save_file, data=data, ei=ei) write_experiment_setup(file=save_file, data=data, ei=ei)
def catia_input(file='Fit.catia', dir=None, output_dir='output', force=False): """Create the CATIA input files. @keyword file: The main CATIA execution file. @type file: str @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory. @type dir: str or None @keyword output_dir: The CATIA output directory, located within the directory specified by the dir argument. This directory will be created. @type output_dir: str @keyword force: A flag which if True will cause all pre-existing files to be overwritten. @type force: Bool """ # Data checks. check_pipe() check_mol_res_spin_data() check_spectra_id_setup() check_model_type() # Check that this is CPMG data. for id in cdp.spectrum_ids: if cdp.exp_type[id] != 'SQ CPMG': raise RelaxError("Only CPMG type data is supported.") # Directory creation. if dir != None: mkdir_nofail(dir, verbosity=0) # Create the R2eff files. write_r2eff_files(input_dir='input_r2eff', base_dir=dir, force=force) # Create the parameter files. write_param_files(global_file="ParamGlobal.inp", set_file="ParamSet1.inp", dir=dir, force=force) # Create the main execution file. write_main_file(file=file, dir=dir, output_dir=output_dir, force=force) # Create the output directory as needed by CATIA (it does not create it itself). mkdir_nofail(dir + sep + output_dir, verbosity=0)
def catia_input(file='Fit.catia', dir=None, output_dir='output', force=False): """Create the CATIA input files. @keyword file: The main CATIA execution file. @type file: str @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory. @type dir: str or None @keyword output_dir: The CATIA output directory, located within the directory specified by the dir argument. This directory will be created. @type output_dir: str @keyword force: A flag which if True will cause all pre-existing files to be overwritten. @type force: Bool """ # Data checks. pipes.test() check_mol_res_spin_data() check_spectra_id_setup() check_model_type() # Check that this is CPMG data. for id in cdp.spectrum_ids: if cdp.exp_type[id] != 'SQ CPMG': raise RelaxError("Only CPMG type data is supported.") # Directory creation. if dir != None: mkdir_nofail(dir, verbosity=0) # Create the R2eff files. write_r2eff_files(input_dir='input_r2eff', base_dir=dir, force=force) # Create the parameter files. write_param_files(global_file="ParamGlobal.inp", set_file="ParamSet1.inp", dir=dir, force=force) # Create the main execution file. write_main_file(file=file, dir=dir, output_dir=output_dir, force=force) # Create the output directory as needed by CATIA (it does not create it itself). mkdir_nofail(dir + sep + output_dir, verbosity=0)
def sample(self): """Generate the ensembles by random sampling of the snapshots.""" # Create the directory for the ensembles, if needed. mkdir_nofail(dir=self.results_dir + sep + "ensembles") # Loop over the configurations. for conf_index in range(len(self.configs)): # Loop over each ensemble. for ens in range(self.num_ens): # Random sampling. rand = [] for j in range(self.num_models): rand.append(randint(self.snapshot_min[conf_index], self.snapshot_max[conf_index])) # Print out. print("Generating ensemble %s%s from structures %s." % (self.configs[conf_index], ens, rand)) # The file name. file_name = "ensembles" + sep + self.configs[conf_index] + repr(ens) + ".pdb" # Open the output file. out = open(self.results_dir+sep+file_name, 'w') # Header. out.write("REM Structures: " + repr(rand) + "\n") # Concatenation the files. for j in range(self.num_models): # The random file. rand_name = self.snapshot_dir[conf_index] + sep + self.configs[conf_index] + repr(rand[j]) + ".pdb" # Append the file. out.write(open(rand_name).read()) # Close the file. out.close()
def sherekhan_input(spin_id=None, force=False, dir='ShereKhan'): """Create the ShereKhan input files. @keyword spin_id: The spin ID string to restrict the file creation to. @type spin_id: str @keyword force: A flag which if True will cause all pre-existing files to be overwritten. @type force: bool @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory. @type dir: str or None """ # Test if the current pipe exists. check_pipe() # Test if sequence data is loaded. if not exists_mol_res_spin_data(): raise RelaxNoSequenceError # Test if the experiment type has been set. if not hasattr(cdp, 'exp_type'): raise RelaxError("The relaxation dispersion experiment type has not been specified.") # Test if the model has been set. if not hasattr(cdp, 'model_type'): raise RelaxError("The relaxation dispersion model has not been specified.") # Directory creation. if dir != None: mkdir_nofail(dir, verbosity=0) # Loop over the spin blocks. cluster_index = 0 for spin_ids in loop_cluster(): # The spin containers. spins = spin_ids_to_containers(spin_ids) # Loop over the magnetic fields. for exp_type, frq, ei, mi in loop_exp_frq(return_indices=True): # Loop over the time, and count it. time_i = 0 for time, ti in loop_time(exp_type=exp_type, frq=frq, return_indices=True): time_i += 1 # Check that not more than one time point is returned. if time_i > 1: raise RelaxError("Number of returned time poins is %i. Only 1 time point is expected."%time_i) # The ShereKhan input file for the spin cluster. file_name = 'sherekhan_frq%s.in' % (mi+1) if dir != None: dir_name = dir + sep + 'cluster%s' % (cluster_index+1) else: dir_name = 'cluster%s' % (cluster_index+1) file = open_write_file(file_name=file_name, dir=dir_name, force=force) # The B0 field for the nuclei of interest in MHz (must be positive to be accepted by the server). file.write("%.10f\n" % abs(frq / periodic_table.gyromagnetic_ratio('1H') * periodic_table.gyromagnetic_ratio('15N') / 1e6)) # The constant relaxation time for the CPMG experiment in seconds. file.write("%s\n" % (time)) # The comment line. file.write("# %-18s %-20s %-20s\n" % ("nu_cpmg (Hz)", "R2eff (rad/s)", "Error")) # Loop over the spins of the cluster. for i in range(len(spins)): # Get the residue container. res = return_residue(spin_ids[i]) # Name the residue if needed. res_name = res.name if res_name == None: res_name = 'X' # Initialise the lines to output (to be able to catch missing data). lines = [] # The residue ID line. lines.append("# %s%s\n" % (res_name, res.num)) # Loop over the dispersion points. for offset, point in loop_offset_point(exp_type=exp_type, frq=frq, skip_ref=True): # The parameter key. param_key = return_param_key_from_data(exp_type=exp_type, frq=frq, offset=offset, point=point) # No data. if param_key not in spins[i].r2eff: continue # Store the data. lines.append("%20.15g %20.13g %20.13g\n" % (point, spins[i].r2eff[param_key], spins[i].r2eff_err[param_key])) # No data. if len(lines) == 1: continue # Write out the data. for line in lines: file.write(line) # Close the file. file.close() # Increment the cluster index. cluster_index += 1
def write(file=None, dir=None, version='3.1', force=False): """Create a BMRB NMR-STAR formatted file. @keyword file: The name of the file to create or a file object. @type file: str or file object @keyword dir: The optional directory to place the file into. If set to 'pipe_name', then it will be placed in a directory with the same name as the current data pipe. @type dir: str or None @keyword version: The NMR-STAR version to create. This can be either '2.1', '3.0', or '3.1'. @type version: str @keyword force: A flag which if True will allow a currently existing file to be overwritten. @type force: bool """ # Test if bmrblib is installed. if not dep_check.bmrblib_module: raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') # Test if the current data pipe exists. pipe_name = cdp_name() if not pipe_name: raise RelaxNoPipeError # Check the file name. if file == None: raise RelaxError("The file name must be specified.") # A file object. if isinstance(file, str): # The special data pipe name directory. if dir == 'pipe_name': dir = pipe_name # Get the full file path. file = get_file_path(file, dir) # Fail if the file already exists and the force flag is False. if access(file, F_OK) and not force: raise RelaxFileOverwriteError(file, 'force flag') # Print out. print("Opening the file '%s' for writing." % file) # Create the directories. mkdir_nofail(dir, verbosity=0) # Specific results writing function. write_function = specific_analyses.setup.get_specific_fn('bmrb_write', ds[pipe_name].pipe_type) # Get the info box. info = Info_box() # Add the relax citations. for id, key in zip(['relax_ref1', 'relax_ref2'], ['dAuvergneGooley08a', 'dAuvergneGooley08b']): # Alias the bib entry. bib = info.bib[key] # Add. exp_info.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, issue=bib.number, page_first=bib.page_first, page_last=bib.page_last, year=bib.year) # Add the relax software package. exp_info.software(name=exp_info.SOFTWARE['relax'].name, version=version_full(), vendor_name=exp_info.SOFTWARE['relax'].authors, url=exp_info.SOFTWARE['relax'].url, cite_ids=['relax_ref1', 'relax_ref2'], tasks=exp_info.SOFTWARE['relax'].tasks) # Execute the specific BMRB writing code. write_function(file, version=version) # Add the file to the results file list. if isinstance(file, str): add_result_file(type='text', label='BMRB', file=file)
def noe_viol(self): """NOE violation calculations.""" # Redirect STDOUT to a log file. if self.log: sys.stdout = open( self.results_dir + sep + "logs" + sep + "NOE_viol.log", 'w') # Create a directory for the save files. dir = self.results_dir + sep + "NOE_results" mkdir_nofail(dir=dir) # Loop over the configurations. for config in self.configs: # Print out. print("\n" * 10 + "# Set up for config " + config + " #" + "\n") # Open the results file. out = open(self.results_dir + sep + "NOE_viol_" + config, 'w') out_sorted = open( self.results_dir + sep + "NOE_viol_" + config + "_sorted", 'w') out.write("%-20s%20s\n" % ("# Ensemble", "NOE_volation")) out_sorted.write("%-20s%20s\n" % ("# Ensemble", "NOE_volation")) # Create the data pipe. self.interpreter.pipe.create("noe_viol_%s" % config, "N-state") # Read the first structure. self.interpreter.structure.read_pdb( "ensembles" + sep + config + "0.pdb", dir=self.results_dir, set_mol_name=config, set_model_num=list(range(1, self.num_models + 1))) # Load all protons as the sequence. self.interpreter.structure.load_spins("@H*", ave_pos=False) # Create the pseudo-atoms. for i in range(len(self.pseudo)): self.interpreter.spin.create_pseudo( spin_name=self.pseudo[i][0], members=self.pseudo[i][1], averaging="linear") self.interpreter.sequence.display() # Read the NOE list. self.interpreter.noe.read_restraints(file=self.noe_file) # Set up the N-state model. self.interpreter.n_state_model.select_model(model="fixed") # Print out. print("\n" * 2 + "# Set up complete #" + "\n" * 10) # Loop over each ensemble. noe_viol = [] for ens in range(self.num_ens): # Print out the ensemble to both the log and screen. if self.log: sys.stdout.write(config + repr(ens) + "\n") sys.stderr.write(config + repr(ens) + "\n") # Delete the old structures and rename the molecule. self.interpreter.structure.delete() # Read the ensemble. self.interpreter.structure.read_pdb( "ensembles" + sep + config + repr(ens) + ".pdb", dir=self.results_dir, set_mol_name=config, set_model_num=list(range(1, self.num_models + 1))) # Get the atomic positions. self.interpreter.structure.get_pos(ave_pos=False) # Calculate the average NOE potential. self.interpreter.minimise.calculate() # Sum the violations. cdp.sum_viol = 0.0 for i in range(len(cdp.ave_dist)): if cdp.quad_pot[i][2]: cdp.sum_viol = cdp.sum_viol + cdp.quad_pot[i][2] # Write out the NOE violation. noe_viol.append([cdp.sum_viol, ens]) out.write("%-20i%30.15f\n" % (ens, cdp.sum_viol)) # Save the state. self.interpreter.results.write(file="%s_results_%s" % (config, ens), dir=dir, force=True) # Sort the NOE violations. noe_viol.sort() # Write the data. for i in range(len(noe_viol)): out_sorted.write("%-20i%20.15f\n" % (noe_viol[i][1], noe_viol[i][0]))
def create(dir=None, binary=None, diff_search=None, sims=None, sim_type=None, trim=None, steps=None, heteronuc_type=None, atom1=None, atom2=None, spin_id=None, force=False, constraints=True): """Create the Modelfree4 input files. The following files are created: - dir/mfin - dir/mfdata - dir/mfpar - dir/mfmodel - dir/run.sh @keyword dir: The optional directory to place the files into. If None, then the files will be placed into a directory named after the current data pipe. @type dir: str or None @keyword binary: The name of the Modelfree4 binary file. This can include the path to the binary. @type binary: str @keyword diff_search: The diffusion tensor search algorithm (see the Modelfree4 manual for details). @type diff_search: str @keyword sims: The number of Monte Carlo simulations to perform. @type sims: int @keyword sim_type: The type of simulation to perform (see the Modelfree4 manual for details). @type sim_type: str @keyword trim: Trimming of the Monte Carlo simulations (see the Modelfree4 manual for details). @type trim: int @keyword steps: The grid search size (see the Modelfree4 manual for details). @type steps: int @keyword heteronuc_type: The Modelfree4 three letter code for the heteronucleus type, e.g. '15N', '13C', etc. @type heteronuc_type: str @keyword atom1: The name of the heteronucleus in the PDB file. @type atom1: str @keyword atom2: The name of the proton in the PDB file. @type atom2: str @keyword spin_id: The spin identification string. @type spin_id: str @keyword force: A flag which if True will cause all pre-existing files to be overwritten. @type force: bool @keyword constraints: A flag which if True will result in constrained optimisation. @type constraints: bool """ # Test if the current pipe exists. check_pipe() # Test if sequence data is loaded. if not exists_mol_res_spin_data(): raise RelaxNoSequenceError # Test if the PDB file is loaded (for the spheroid and ellipsoid). if hasattr(cdp, 'diff_tensor' ) and not cdp.diff_tensor.type == 'sphere' and not hasattr( cdp, 'structure'): raise RelaxNoPdbError # Deselect certain spins. __deselect_spins() # Directory creation. if dir == None: dir = pipes.cdp_name() mkdir_nofail(dir, verbosity=0) # Number of field strengths and values. frq = [] for ri_id in cdp.ri_ids: # New frequency. if cdp.spectrometer_frq[ri_id] not in frq: frq.append(cdp.spectrometer_frq[ri_id]) # The 'mfin' file. mfin = open_write_file('mfin', dir, force) create_mfin(mfin, diff_search=diff_search, sims=sims, sim_type=sim_type, trim=trim, num_frq=len(frq), frq=frq) mfin.close() # Open the 'mfdata', 'mfmodel', and 'mfpar' files. mfdata = open_write_file('mfdata', dir, force) mfmodel = open_write_file('mfmodel', dir, force) mfpar = open_write_file('mfpar', dir, force) # Loop over the sequence. for spin, mol_name, res_num, res_name, id in spin_loop(spin_id, full_info=True, return_id=True): # Skip deselected spins. if not spin.select: continue # The 'mfdata' file. if not create_mfdata( mfdata, spin=spin, spin_id=id, num_frq=len(frq), frq=frq): continue # The 'mfmodel' file. create_mfmodel(mfmodel, spin=spin, spin_id=id, steps=steps, constraints=constraints) # The 'mfpar' file. create_mfpar(mfpar, spin=spin, spin_id=id, res_num=res_num, atom1=atom1, atom2=atom2) # Close the 'mfdata', 'mfmodel', and 'mfpar' files. mfdata.close() mfmodel.close() mfpar.close() # The 'run.sh' script. run = open_write_file('run.sh', dir, force) create_run(run, binary=binary, dir=dir) run.close() chmod(dir + sep + 'run.sh', S_IRWXU | S_IRGRP | S_IROTH)
def __init__(self, stage=1, results_dir=None, num_ens=10000, num_models=10, configs=None, snapshot_dir='snapshots', snapshot_min=None, snapshot_max=None, pseudo=None, noe_file=None, noe_norm=None, rdc_name=None, rdc_file=None, rdc_spin_id1_col=None, rdc_spin_id2_col=None, rdc_data_col=None, rdc_error_col=None, bond_length=None, bond_length_file=None, log=None, bucket_num=200, lower_lim_noe=0.0, upper_lim_noe=600.0, lower_lim_rdc=0.0, upper_lim_rdc=1.0): """Set up for the stereochemistry analysis. @keyword stage: Stage of analysis (see the module docstring above for the options). @type stage: int @keyword results_dir: The optional directory to place all results files into. @type results_dir: None or str @keyword num_ens: Number of ensembles. @type num_ens: int @keyword num_models: Ensemble size. @type num_models: int @keyword configs: All the configurations. @type configs: list of str @keyword snapshot_dir: Snapshot directories (corresponding to the configurations). @type snapshot_dir: list of str @keyword snapshot_min: The number of the first snapshots (corresponding to the configurations). @type snapshot_min: list of int @keyword snapshot_max: The number of the last snapshots (corresponding to the configurations). @type snapshot_max: list of int @keyword pseudo: The list of pseudo-atoms. Each element is a list of the pseudo-atom name and a list of all those atoms forming the pseudo-atom. For example, pseudo = [["Q7", ["@H16", "@H17", "@H18"]], ["Q9", ["@H20", "@H21", "@H22"]]]. @type pseudo: list of list of str and list of str @keyword noe_file: The name of the NOE restraint file. @type noe_file: str @keyword noe_norm: The NOE normalisation factor (equal to the sum of all NOEs squared). @type noe_norm: float @keyword rdc_name: The label for this RDC data set. @type rdc_name: str @keyword rdc_file: The name of the RDC file. @type rdc_file: str @keyword rdc_spin_id1_col: The spin ID column of the first spin in the RDC file. @type rdc_spin_id1_col: None or int @keyword rdc_spin_id2_col: The spin ID column of the second spin in the RDC file. @type rdc_spin_id2_col: None or int @keyword rdc_data_col: The data column of the RDC file. @type rdc_data_col: int @keyword rdc_error_col: The error column of the RDC file. @type rdc_error_col: int @keyword bond_length: The bond length value in meters. This overrides the bond_length_file argument. @type bond_length: float or None @keyword bond_length_file: The file of bond lengths for each atom pair in meters. The first and second columns must be the spin ID strings and the third column must contain the data. @type bond_length_file: float or None @keyword log: Log file output flag (only for certain stages). @type log: bool @keyword bucket_num: Number of buckets for the distribution plots. @type bucket_num: int @keyword lower_lim_noe: Distribution plot limits. @type lower_lim_noe: int @keyword upper_lim_noe: Distribution plot limits. @type upper_lim_noe: int @keyword lower_lim_rdc: Distribution plot limits. @type lower_lim_rdc: int @keyword upper_lim_rdc: Distribution plot limits. @type upper_lim_rdc: int """ # Initial printout. title(file=sys.stdout, text="Stereochemistry auto-analysis") # Safely execute the full protocol. try: # Execution lock. status.exec_lock.acquire('auto stereochem analysis', mode='auto-analysis') # Set up the analysis status object. status.init_auto_analysis('stereochem', type='stereochem') status.current_analysis = 'auto stereochem analysis' # Store all the args. self.stage = stage self.results_dir = results_dir self.num_ens = num_ens self.num_models = num_models self.configs = configs self.snapshot_dir = snapshot_dir self.snapshot_min = snapshot_min self.snapshot_max = snapshot_max self.pseudo = pseudo self.noe_file = noe_file self.noe_norm = noe_norm self.rdc_name = rdc_name self.rdc_file = rdc_file self.rdc_spin_id1_col = rdc_spin_id1_col self.rdc_spin_id2_col = rdc_spin_id2_col self.rdc_data_col = rdc_data_col self.rdc_error_col = rdc_error_col self.bond_length = bond_length self.bond_length_file = bond_length_file self.log = log self.bucket_num = bucket_num self.lower_lim_noe = lower_lim_noe self.upper_lim_noe = upper_lim_noe self.lower_lim_rdc = lower_lim_rdc self.upper_lim_rdc = upper_lim_rdc # Load the interpreter. self.interpreter = Interpreter(show_script=False, raise_relax_error=True) self.interpreter.populate_self() self.interpreter.on(verbose=False) # Create the results directory. if self.results_dir: mkdir_nofail(self.results_dir) # Or use the current working directory. else: self.results_dir = getcwd() # Create a directory for log files. if self.log: mkdir_nofail(self.results_dir + sep + "logs") # Clean up. finally: # Final printout. title(file=sys.stdout, text="Completion of the stereochemistry auto-analysis") print_elapsed_time(time() - status.start_time) # Finish and unlock execution. status.auto_analysis['stereochem'].fin = True status.current_analysis = None status.exec_lock.release()
def __init__(self, stage=1, results_dir=None, num_ens=10000, num_models=10, configs=None, snapshot_dir='snapshots', snapshot_min=None, snapshot_max=None, pseudo=None, noe_file=None, noe_norm=None, rdc_name=None, rdc_file=None, rdc_spin_id1_col=None, rdc_spin_id2_col=None, rdc_data_col=None, rdc_error_col=None, bond_length=None, bond_length_file=None, log=None, bucket_num=200, lower_lim_noe=0.0, upper_lim_noe=600.0, lower_lim_rdc=0.0, upper_lim_rdc=1.0): """Set up for the stereochemistry analysis. @keyword stage: Stage of analysis (see the module docstring above for the options). @type stage: int @keyword results_dir: The optional directory to place all results files into. @type results_dir: None or str @keyword num_ens: Number of ensembles. @type num_ens: int @keyword num_models: Ensemble size. @type num_models: int @keyword configs: All the configurations. @type configs: list of str @keyword snapshot_dir: Snapshot directories (corresponding to the configurations). @type snapshot_dir: list of str @keyword snapshot_min: The number of the first snapshots (corresponding to the configurations). @type snapshot_min: list of int @keyword snapshot_max: The number of the last snapshots (corresponding to the configurations). @type snapshot_max: list of int @keyword pseudo: The list of pseudo-atoms. Each element is a list of the pseudo-atom name and a list of all those atoms forming the pseudo-atom. For example, pseudo = [["Q7", ["@H16", "@H17", "@H18"]], ["Q9", ["@H20", "@H21", "@H22"]]]. @type pseudo: list of list of str and list of str @keyword noe_file: The name of the NOE restraint file. @type noe_file: str @keyword noe_norm: The NOE normalisation factor (equal to the sum of all NOEs squared). @type noe_norm: float @keyword rdc_name: The label for this RDC data set. @type rdc_name: str @keyword rdc_file: The name of the RDC file. @type rdc_file: str @keyword rdc_spin_id1_col: The spin ID column of the first spin in the RDC file. @type rdc_spin_id1_col: None or int @keyword rdc_spin_id2_col: The spin ID column of the second spin in the RDC file. @type rdc_spin_id2_col: None or int @keyword rdc_data_col: The data column of the RDC file. @type rdc_data_col: int @keyword rdc_error_col: The error column of the RDC file. @type rdc_error_col: int @keyword bond_length: The bond length value in meters. This overrides the bond_length_file argument. @type bond_length: float or None @keyword bond_length_file: The file of bond lengths for each atom pair in meters. The first and second columns must be the spin ID strings and the third column must contain the data. @type bond_length_file: float or None @keyword log: Log file output flag (only for certain stages). @type log: bool @keyword bucket_num: Number of buckets for the distribution plots. @type bucket_num: int @keyword lower_lim_noe: Distribution plot limits. @type lower_lim_noe: int @keyword upper_lim_noe: Distribution plot limits. @type upper_lim_noe: int @keyword lower_lim_rdc: Distribution plot limits. @type lower_lim_rdc: int @keyword upper_lim_rdc: Distribution plot limits. @type upper_lim_rdc: int """ # Execution lock. status.exec_lock.acquire('auto stereochem analysis', mode='auto-analysis') # Set up the analysis status object. status.init_auto_analysis('stereochem', type='stereochem') status.current_analysis = 'auto stereochem analysis' # Store all the args. self.stage = stage self.results_dir = results_dir self.num_ens = num_ens self.num_models = num_models self.configs = configs self.snapshot_dir = snapshot_dir self.snapshot_min = snapshot_min self.snapshot_max = snapshot_max self.pseudo = pseudo self.noe_file = noe_file self.noe_norm = noe_norm self.rdc_name = rdc_name self.rdc_file = rdc_file self.rdc_spin_id1_col = rdc_spin_id1_col self.rdc_spin_id2_col = rdc_spin_id2_col self.rdc_data_col = rdc_data_col self.rdc_error_col = rdc_error_col self.bond_length = bond_length self.bond_length_file = bond_length_file self.log = log self.bucket_num = bucket_num self.lower_lim_noe = lower_lim_noe self.upper_lim_noe = upper_lim_noe self.lower_lim_rdc = lower_lim_rdc self.upper_lim_rdc = upper_lim_rdc # Load the interpreter. self.interpreter = Interpreter(show_script=False, quit=False, raise_relax_error=True) self.interpreter.populate_self() self.interpreter.on(verbose=False) # Create the results directory. if self.results_dir: mkdir_nofail(self.results_dir) # Or use the current working directory. else: self.results_dir = getcwd() # Create a directory for log files. if self.log: mkdir_nofail(self.results_dir + sep + "logs") # Finish and unlock execution. status.auto_analysis['stereochem'].fin = True status.current_analysis = None status.exec_lock.release()
def superimpose(self): """Superimpose the ensembles using fit to first in Molmol.""" # Create the output directory. mkdir_nofail("ensembles_superimposed") # Logging turned on. if self.log: log = open( self.results_dir + sep + "logs" + sep + "superimpose_molmol.stderr", 'w') sys.stdout = open( self.results_dir + sep + "logs" + sep + "superimpose.log", 'w') # Loop over S and R. for config in ["R", "S"]: # Loop over each ensemble. for ens in range(self.num_ens): # The file names. file_in = "ensembles" + sep + config + repr(ens) + ".pdb" file_out = "ensembles_superimposed" + sep + config + repr( ens) + ".pdb" # Print out. sys.stderr.write( "Superimposing %s with Molmol, output to %s.\n" % (file_in, file_out)) if self.log: log.write( "\n\n\nSuperimposing %s with Molmol, output to %s.\n" % (file_in, file_out)) # Failure handling (if a failure occurred and this is rerun, skip all existing files). if access(self.results_dir + sep + file_out, F_OK): continue # Open the Molmol pipe. pipe = Popen("molmol -t -f -", shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=False) # Init all. pipe.stdin.write("InitAll yes\n") # Read the PDB. pipe.stdin.write("ReadPdb " + self.results_dir + sep + file_in + "\n") # Fitting to mean. pipe.stdin.write("Fit to_first 'selected'\n") pipe.stdin.write("Fit to_mean 'selected'\n") # Write the result. pipe.stdin.write("WritePdb " + self.results_dir + sep + file_out + "\n") # End Molmol. pipe.stdin.close() # Get STDOUT and STDERR. sys.stdout.write(pipe.stdout.read()) if self.log: log.write(pipe.stderr.read()) # Close the pipe. pipe.stdout.close() pipe.stderr.close() # Open the superimposed file in relax. self.interpreter.reset() self.interpreter.pipe.create('out', 'N-state') self.interpreter.structure.read_pdb(file_out) # Fix the retarded MOLMOL proton naming. for model in cdp.structure.structural_data: # Alias. mol = model.mol[0] # Loop over all atoms. for i in range(len(mol.atom_name)): # A proton. if search('H', mol.atom_name[i]): mol.atom_name[ i] = mol.atom_name[i][1:] + mol.atom_name[i][0] # Replace the superimposed file. self.interpreter.structure.write_pdb( config + repr(ens) + ".pdb", dir=self.results_dir + sep + "ensembles_superimposed", force=True)
def rdc_analysis(self): """Perform the RDC part of the analysis.""" # Redirect STDOUT to a log file. if self.log: sys.stdout = open(self.results_dir+sep+"logs" + sep + "RDC_%s_analysis.log" % self.rdc_name, 'w') # The dipolar constant. d = 0.0 if self.bond_length != None: d = 3.0 / (2.0*pi) * dipolar_constant(g13C, g1H, self.bond_length) # Create a directory for the save files. dir = self.results_dir + sep + "RDC_%s_results" % self.rdc_name mkdir_nofail(dir=dir) # Loop over the configurations. for config in self.configs: # Print out. print("\n"*10 + "# Set up for config " + config + " #" + "\n") # Open the results files. out = open(self.results_dir+sep+"Q_factors_" + config, 'w') out_sorted = open(self.results_dir+sep+"Q_factors_" + config + "_sorted", 'w') out.write("%-20s%20s%20s\n" % ("# Ensemble", "RDC_Q_factor(pales)", "RDC_Q_factor(standard)")) out_sorted.write("%-20s%20s\n" % ("# Ensemble", "RDC_Q_factor(pales)")) # Create the data pipe. self.interpreter.pipe.create("rdc_analysis_%s" % config, "N-state") # Read the first structure. self.interpreter.structure.read_pdb("ensembles_superimposed" + sep + config + "0.pdb", dir=self.results_dir, set_mol_name=config, set_model_num=list(range(1, self.num_models+1))) # Load all spins as the sequence. self.interpreter.structure.load_spins(ave_pos=False) # Create the pseudo-atoms. for i in range(len(self.pseudo)): self.interpreter.spin.create_pseudo(spin_name=self.pseudo[i][0], members=self.pseudo[i][1], averaging="linear") self.interpreter.sequence.display() # Read the RDC data. self.interpreter.rdc.read(align_id=self.rdc_file, file=self.rdc_file, spin_id1_col=self.rdc_spin_id1_col, spin_id2_col=self.rdc_spin_id2_col, data_col=self.rdc_data_col, error_col=self.rdc_error_col) # Define the magnetic dipole-dipole relaxation interaction. if self.bond_length != None: self.interpreter.interatom.set_dist(spin_id1='@C*', spin_id2='@H*', ave_dist=self.bond_length) self.interpreter.interatom.set_dist(spin_id1='@C*', spin_id2='@Q*', ave_dist=self.bond_length) else: self.interpreter.interatom.read_dist(file=self.bond_length_file, spin_id1_col=1, spin_id2_col=2, data_col=3) # Set the nuclear isotope. self.interpreter.spin.isotope(isotope='13C', spin_id='@C*') self.interpreter.spin.isotope(isotope='1H', spin_id='@H*') self.interpreter.spin.isotope(isotope='1H', spin_id='@Q*') # Set up the model. self.interpreter.n_state_model.select_model(model="fixed") # Print out. print("\n"*2 + "# Set up complete #" + "\n"*10) # Loop over each ensemble. q_factors = [] for ens in range(self.num_ens): # Print out the ensemble to both the log and screen. if self.log: sys.stdout.write(config + repr(ens) + "\n") sys.stderr.write(config + repr(ens) + "\n") # Delete the old structures. self.interpreter.structure.delete() # Read the ensemble. self.interpreter.structure.read_pdb("ensembles_superimposed" + sep + config + repr(ens) + ".pdb", dir=self.results_dir, set_mol_name=config, set_model_num=list(range(1, self.num_models+1))) # Get the positional information, then load the CH vectors. self.interpreter.structure.get_pos(ave_pos=False) if self.bond_length != None: self.interpreter.interatom.set_dist(spin_id1='@C*', spin_id2='@H*', ave_dist=self.bond_length) else: self.interpreter.interatom.read_dist(file=self.bond_length_file, spin_id1_col=1, spin_id2_col=2, data_col=3) self.interpreter.interatom.unit_vectors(ave=False) # Minimisation. #grid_search(inc=4) self.interpreter.minimise("simplex", constraints=False) # Store and write out the Q-factors. q_factors.append([cdp.q_rdc, ens]) out.write("%-20i%20.15f%20.15f\n" % (ens, cdp.q_rdc, cdp.q_rdc_norm2)) # Calculate the alignment tensor in Hz, and store it for reference. cdp.align_tensor_Hz = d * cdp.align_tensors[0].A cdp.align_tensor_Hz_5D = d * cdp.align_tensors[0].A_5D # Save the state. self.interpreter.results.write(file="%s_results_%s" % (config, ens), dir=dir, force=True) # Sort the NOE violations. q_factors.sort() # Write the data. for i in range(len(q_factors)): out_sorted.write("%-20i%20.15f\n" % (q_factors[i][1], q_factors[i][0]))
def noe_viol(self): """NOE violation calculations.""" # Redirect STDOUT to a log file. if self.log: sys.stdout = open(self.results_dir+sep+"logs" + sep + "NOE_viol.log", 'w') # Create a directory for the save files. dir = self.results_dir + sep + "NOE_results" mkdir_nofail(dir=dir) # Loop over the configurations. for config in self.configs: # Print out. print("\n"*10 + "# Set up for config " + config + " #" + "\n") # Open the results file. out = open(self.results_dir+sep+"NOE_viol_" + config, 'w') out_sorted = open(self.results_dir+sep+"NOE_viol_" + config + "_sorted", 'w') out.write("%-20s%20s\n" % ("# Ensemble", "NOE_volation")) out_sorted.write("%-20s%20s\n" % ("# Ensemble", "NOE_volation")) # Create the data pipe. self.interpreter.pipe.create("noe_viol_%s" % config, "N-state") # Read the first structure. self.interpreter.structure.read_pdb("ensembles" + sep + config + "0.pdb", dir=self.results_dir, set_mol_name=config, set_model_num=list(range(1, self.num_models+1))) # Load all protons as the sequence. self.interpreter.structure.load_spins("@H*", ave_pos=False) # Create the pseudo-atoms. for i in range(len(self.pseudo)): self.interpreter.spin.create_pseudo(spin_name=self.pseudo[i][0], members=self.pseudo[i][1], averaging="linear") self.interpreter.sequence.display() # Read the NOE list. self.interpreter.noe.read_restraints(file=self.noe_file) # Set up the N-state model. self.interpreter.n_state_model.select_model(model="fixed") # Print out. print("\n"*2 + "# Set up complete #" + "\n"*10) # Loop over each ensemble. noe_viol = [] for ens in range(self.num_ens): # Print out the ensemble to both the log and screen. if self.log: sys.stdout.write(config + repr(ens) + "\n") sys.stderr.write(config + repr(ens) + "\n") # Delete the old structures and rename the molecule. self.interpreter.structure.delete() # Read the ensemble. self.interpreter.structure.read_pdb("ensembles" + sep + config + repr(ens) + ".pdb", dir=self.results_dir, set_mol_name=config, set_model_num=list(range(1, self.num_models+1))) # Get the atomic positions. self.interpreter.structure.get_pos(ave_pos=False) # Calculate the average NOE potential. self.interpreter.calc() # Sum the violations. cdp.sum_viol = 0.0 for i in range(len(cdp.ave_dist)): if cdp.quad_pot[i][2]: cdp.sum_viol = cdp.sum_viol + cdp.quad_pot[i][2] # Write out the NOE violation. noe_viol.append([cdp.sum_viol, ens]) out.write("%-20i%30.15f\n" % (ens, cdp.sum_viol)) # Save the state. self.interpreter.results.write(file="%s_results_%s" % (config, ens), dir=dir, force=True) # Sort the NOE violations. noe_viol.sort() # Write the data. for i in range(len(noe_viol)): out_sorted.write("%-20i%20.15f\n" % (noe_viol[i][1], noe_viol[i][0]))
def write_r2eff_files(input_dir=None, base_dir=None, force=False): """Create the CATIA R2eff input files. @keyword input_dir: The special directory for the R2eff input files. @type input_dir: str @keyword base_dir: The base directory to place the files into. @type base_dir: str @keyword force: A flag which if True will cause a pre-existing file to be overwritten. @type force: bool """ # Create the directory for the R2eff files for each field and spin. dir = base_dir + sep + input_dir mkdir_nofail(dir, verbosity=0) # Determine the isotope information. isotope = None for spin in spin_loop(skip_desel=True): if hasattr(spin, 'isotope'): if isotope == None: isotope = spin.isotope elif spin.isotope != isotope: raise RelaxError("CATIA only supports one spin type.") if isotope == None: raise RelaxError("The spin isotopes have not been specified.") # Isotope translation. if isotope == '1H': isotope = 'H1' elif isotope == '13C': isotope = 'C13' elif isotope == '15N': isotope = 'N15' # Loop over the frequencies. for frq, mi in loop_frq(return_indices=True): # The frequency string in MHz. frq_string = int(frq * 1e-6) # The set files. file_name = "data_set_%i.inp" % frq_string set_file = open_write_file(file_name=file_name, dir=base_dir, force=force) id = frq_string set_file.write("ID=%s\n" % id) set_file.write("Sfrq = %s\n" % frq_string) set_file.write("Temperature = %s\n" % 0.0) set_file.write("Nucleus = %s\n" % isotope) set_file.write("Couplednucleus = %s\n" % 'H1') set_file.write("Time_equil = %s\n" % 0.0) set_file.write("Pwx_cp = %s\n" % 0.0) set_file.write("Taub = %s\n" % 0.0) set_file.write("Time_T2 = %s\n" % cdp.relax_time_list[0]) set_file.write("Xcar = %s\n" % 0.0) set_file.write("Seqfil = %s\n" % 'CW_CPMG') set_file.write("Minerror = %s\n" % "(2.%;0.5/s)") set_file.write("Basis = (%s)\n" % "Iph_7") set_file.write("Format = (%i;%i;%i)\n" % (0, 1, 2)) set_file.write("DataDirectory = %s\n" % (dir + sep)) set_file.write("Data = (\n") # Loop over the spins. for spin, mol_name, res_num, res_name, spin_id in spin_loop( full_info=True, return_id=True, skip_desel=True): # The file. file_name = "spin%s_%i.cpmg" % (spin_id.replace('#', '_').replace( ':', '_').replace('@', '_'), frq_string) spin_file = open_write_file(file_name=file_name, dir=dir, force=force) # Write the header. spin_file.write("# %18s %20s %20s\n" % ("nu_cpmg(Hz)", "R2(1/s)", "Esd(R2)")) # Loop over the dispersion points. for offset, point, oi, di in loop_offset_point( exp_type=EXP_TYPE_CPMG_SQ, frq=frq, return_indices=True): # The key. key = return_param_key_from_data(exp_type=EXP_TYPE_CPMG_SQ, frq=frq, offset=offset, point=point) # No data. if key not in spin.r2eff: continue # Write out the data. spin_file.write("%20.15f %20.15f %20.15f\n" % (point, spin.r2eff[key], spin.r2eff_err[key])) # Close the file. spin_file.close() # Add the file name to the set. catia_spin_id = "%i%s" % (res_num, spin.name) set_file.write(" [%s;%s];\n" % (catia_spin_id, file_name)) # Terminate the set file. set_file.write(")\n") set_file.close()
def write_r2eff_files(input_dir=None, base_dir=None, force=False): """Create the CATIA R2eff input files. @keyword input_dir: The special directory for the R2eff input files. @type input_dir: str @keyword base_dir: The base directory to place the files into. @type base_dir: str @keyword force: A flag which if True will cause a pre-existing file to be overwritten. @type force: bool """ # Create the directory for the R2eff files for each field and spin. dir = base_dir + sep + input_dir mkdir_nofail(dir, verbosity=0) # Determine the isotope information. isotope = None for spin in spin_loop(skip_desel=True): if hasattr(spin, 'isotope'): if isotope == None: isotope = spin.isotope elif spin.isotope != isotope: raise RelaxError("CATIA only supports one spin type.") if isotope == None: raise RelaxError("The spin isotopes have not been specified.") # Isotope translation. if isotope == '1H': isotope = 'H1' elif isotope == '13C': isotope = 'C13' elif isotope == '15N': isotope = 'N15' # Loop over the frequencies. for frq, mi in loop_frq(return_indices=True): # The frequency string in MHz. frq_string = int(frq*1e-6) # The set files. file_name = "data_set_%i.inp" % frq_string set_file = open_write_file(file_name=file_name, dir=base_dir, force=force) id = frq_string set_file.write("ID=%s\n" % id) set_file.write("Sfrq = %s\n" % frq_string) set_file.write("Temperature = %s\n" % 0.0) set_file.write("Nucleus = %s\n" % isotope) set_file.write("Couplednucleus = %s\n" % 'H1') set_file.write("Time_equil = %s\n" % 0.0) set_file.write("Pwx_cp = %s\n" % 0.0) set_file.write("Taub = %s\n" % 0.0) set_file.write("Time_T2 = %s\n"% cdp.relax_time_list[0]) set_file.write("Xcar = %s\n" % 0.0) set_file.write("Seqfil = %s\n" % 'CW_CPMG') set_file.write("Minerror = %s\n" % "(2.%;0.5/s)") set_file.write("Basis = (%s)\n" % "Iph_7") set_file.write("Format = (%i;%i;%i)\n" % (0, 1, 2)) set_file.write("DataDirectory = %s\n" % (dir+sep)) set_file.write("Data = (\n") # Loop over the spins. for spin, mol_name, res_num, res_name, spin_id in spin_loop(full_info=True, return_id=True, skip_desel=True): # The file. file_name = "spin%s_%i.cpmg" % (spin_id.replace('#', '_').replace(':', '_').replace('@', '_'), frq_string) spin_file = open_write_file(file_name=file_name, dir=dir, force=force) # Write the header. spin_file.write("# %18s %20s %20s\n" % ("nu_cpmg(Hz)", "R2(1/s)", "Esd(R2)")) # Loop over the dispersion points. for offset, point, oi, di in loop_offset_point(exp_type=EXP_TYPE_CPMG_SQ, frq=frq, return_indices=True): # The key. key = return_param_key_from_data(exp_type=EXP_TYPE_CPMG_SQ, frq=frq, offset=offset, point=point) # No data. if key not in spin.r2eff: continue # Write out the data. spin_file.write("%20.15f %20.15f %20.15f\n" % (point, spin.r2eff[key], spin.r2eff_err[key])) # Close the file. spin_file.close() # Add the file name to the set. catia_spin_id = "%i%s" % (res_num, spin.name) set_file.write(" [%s;%s];\n" % (catia_spin_id, file_name)) # Terminate the set file. set_file.write(")\n") set_file.close()
def rdc_analysis(self): """Perform the RDC part of the analysis.""" # Redirect STDOUT to a log file. if self.log: sys.stdout = open( self.results_dir + sep + "logs" + sep + "RDC_%s_analysis.log" % self.rdc_name, 'w') # The dipolar constant. d = 0.0 if self.bond_length != None: d = 3.0 / (2.0 * pi) * dipolar_constant( periodic_table.gyromagnetic_ratio('13C'), periodic_table.gyromagnetic_ratio('1H'), self.bond_length) # Create a directory for the save files. dir = self.results_dir + sep + "RDC_%s_results" % self.rdc_name mkdir_nofail(dir=dir) # Loop over the configurations. for config in self.configs: # Print out. print("\n" * 10 + "# Set up for config " + config + " #" + "\n") # Open the results files. out = open(self.results_dir + sep + "Q_factors_" + config, 'w') out_sorted = open( self.results_dir + sep + "Q_factors_" + config + "_sorted", 'w') out.write("%-20s%20s%20s\n" % ("# Ensemble", "RDC_Q_factor(pales)", "RDC_Q_factor(standard)")) out_sorted.write("%-20s%20s\n" % ("# Ensemble", "RDC_Q_factor(pales)")) # Create the data pipe. self.interpreter.pipe.create("rdc_analysis_%s" % config, "N-state") # Read the first structure. self.interpreter.structure.read_pdb( "ensembles_superimposed" + sep + config + "0.pdb", dir=self.results_dir, set_mol_name=config, set_model_num=list(range(1, self.num_models + 1))) # Load all spins as the sequence. self.interpreter.structure.load_spins(ave_pos=False) # Create the pseudo-atoms. for i in range(len(self.pseudo)): self.interpreter.spin.create_pseudo( spin_name=self.pseudo[i][0], members=self.pseudo[i][1], averaging="linear") self.interpreter.sequence.display() # Read the RDC data. self.interpreter.rdc.read(align_id=self.rdc_file, file=self.rdc_file, spin_id1_col=self.rdc_spin_id1_col, spin_id2_col=self.rdc_spin_id2_col, data_col=self.rdc_data_col, error_col=self.rdc_error_col) # Define the magnetic dipole-dipole relaxation interaction. if self.bond_length != None: self.interpreter.interatom.set_dist(spin_id1='@C*', spin_id2='@H*', ave_dist=self.bond_length) self.interpreter.interatom.set_dist(spin_id1='@C*', spin_id2='@Q*', ave_dist=self.bond_length) else: self.interpreter.interatom.read_dist( file=self.bond_length_file, spin_id1_col=1, spin_id2_col=2, data_col=3) # Set the nuclear isotope. self.interpreter.spin.isotope(isotope='13C', spin_id='@C*') self.interpreter.spin.isotope(isotope='1H', spin_id='@H*') self.interpreter.spin.isotope(isotope='1H', spin_id='@Q*') # Set up the model. self.interpreter.n_state_model.select_model(model="fixed") # Print out. print("\n" * 2 + "# Set up complete #" + "\n" * 10) # Loop over each ensemble. q_factors = [] for ens in range(self.num_ens): # Print out the ensemble to both the log and screen. if self.log: sys.stdout.write(config + repr(ens) + "\n") sys.stderr.write(config + repr(ens) + "\n") # Delete the old structures. self.interpreter.structure.delete() # Read the ensemble. self.interpreter.structure.read_pdb( "ensembles_superimposed" + sep + config + repr(ens) + ".pdb", dir=self.results_dir, set_mol_name=config, set_model_num=list(range(1, self.num_models + 1))) # Get the positional information, then load the CH vectors. self.interpreter.structure.get_pos(ave_pos=False) if self.bond_length != None: self.interpreter.interatom.set_dist( spin_id1='@C*', spin_id2='@H*', ave_dist=self.bond_length) else: self.interpreter.interatom.read_dist( file=self.bond_length_file, spin_id1_col=1, spin_id2_col=2, data_col=3) self.interpreter.interatom.unit_vectors(ave=False) # Minimisation. #minimise.grid_search(inc=4) self.interpreter.minimise.execute("simplex", constraints=False) # Store and write out the Q factors. q_factors.append([cdp.q_rdc_norm_squared_sum, ens]) out.write("%-20i%20.15f%20.15f\n" % (ens, cdp.q_rdc_norm_squared_sum, cdp.q_rdc_norm_squared_sum)) # Calculate the alignment tensor in Hz, and store it for reference. cdp.align_tensor_Hz = d * cdp.align_tensors[0].A cdp.align_tensor_Hz_5D = d * cdp.align_tensors[0].A_5D # Save the state. self.interpreter.results.write(file="%s_results_%s" % (config, ens), dir=dir, force=True) # Sort the NOE violations. q_factors.sort() # Write the data. for i in range(len(q_factors)): out_sorted.write("%-20i%20.15f\n" % (q_factors[i][1], q_factors[i][0]))
def write(file=None, dir=None, version='3.1', force=False): """Create a BMRB NMR-STAR formatted file. @keyword file: The name of the file to create or a file object. @type file: str or file object @keyword dir: The optional directory to place the file into. If set to 'pipe_name', then it will be placed in a directory with the same name as the current data pipe. @type dir: str or None @keyword version: The NMR-STAR version to create. This can be either '2.1', '3.0', or '3.1'. @type version: str @keyword force: A flag which if True will allow a currently existing file to be overwritten. @type force: bool """ # Test if bmrblib is installed. if not dep_check.bmrblib_module: raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') # Test if the current data pipe exists. pipe_name = cdp_name() if not pipe_name: raise RelaxNoPipeError # Check the file name. if file == None: raise RelaxError("The file name must be specified.") # A file object. if isinstance(file, str): # The special data pipe name directory. if dir == 'pipe_name': dir = pipe_name # Get the full file path. file = get_file_path(file, dir) # Fail if the file already exists and the force flag is False. if access(file, F_OK) and not force: raise RelaxFileOverwriteError(file, 'force flag') # Print out. print("Opening the file '%s' for writing." % file) # Create the directories. mkdir_nofail(dir, verbosity=0) # Get the info box. info = Info_box() # Add the relax citations. for id, key in zip(['relax_ref1', 'relax_ref2'], ['dAuvergneGooley08a', 'dAuvergneGooley08b']): # Alias the bib entry. bib = info.bib[key] # Add. exp_info.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, issue=bib.number, page_first=bib.page_first, page_last=bib.page_last, year=bib.year) # Add the relax software package. exp_info.software(name=exp_info.SOFTWARE['relax'].name, version=version_full(), vendor_name=exp_info.SOFTWARE['relax'].authors, url=exp_info.SOFTWARE['relax'].url, cite_ids=['relax_ref1', 'relax_ref2'], tasks=exp_info.SOFTWARE['relax'].tasks) # Execute the specific BMRB writing code. api = return_api(pipe_name=pipe_name) api.bmrb_write(file, version=version) # Add the file to the results file list. if isinstance(file, str): add_result_file(type='text', label='BMRB', file=file)
def superimpose(self): """Superimpose the ensembles using fit to first in Molmol.""" # Create the output directory. mkdir_nofail("ensembles_superimposed") # Logging turned on. if self.log: log = open(self.results_dir+sep+"logs" + sep + "superimpose_molmol.stderr", 'w') sys.stdout = open(self.results_dir+sep+"logs" + sep + "superimpose.log", 'w') # Loop over S and R. for config in ["R", "S"]: # Loop over each ensemble. for ens in range(self.num_ens): # The file names. file_in = "ensembles" + sep + config + repr(ens) + ".pdb" file_out = "ensembles_superimposed" + sep + config + repr(ens) + ".pdb" # Print out. sys.stderr.write("Superimposing %s with Molmol, output to %s.\n" % (file_in, file_out)) if self.log: log.write("\n\n\nSuperimposing %s with Molmol, output to %s.\n" % (file_in, file_out)) # Failure handling (if a failure occurred and this is rerun, skip all existing files). if access(self.results_dir+sep+file_out, F_OK): continue # Open the Molmol pipe. pipe = Popen("molmol -t -f -", shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=False) # Init all. pipe.stdin.write("InitAll yes\n") # Read the PDB. pipe.stdin.write("ReadPdb " + self.results_dir+sep+file_in + "\n") # Fitting to mean. pipe.stdin.write("Fit to_first 'selected'\n") pipe.stdin.write("Fit to_mean 'selected'\n") # Write the result. pipe.stdin.write("WritePdb " + self.results_dir+sep+file_out + "\n") # End Molmol. pipe.stdin.close() # Get STDOUT and STDERR. sys.stdout.write(pipe.stdout.read()) if self.log: log.write(pipe.stderr.read()) # Close the pipe. pipe.stdout.close() pipe.stderr.close() # Open the superimposed file in relax. self.interpreter.reset() self.interpreter.pipe.create('out', 'N-state') self.interpreter.structure.read_pdb(file_out) # Fix the retarded MOLMOL proton naming. for model in cdp.structure.structural_data: # Alias. mol = model.mol[0] # Loop over all atoms. for i in range(len(mol.atom_name)): # A proton. if search('H', mol.atom_name[i]): mol.atom_name[i] = mol.atom_name[i][1:] + mol.atom_name[i][0] # Replace the superimposed file. self.interpreter.structure.write_pdb(config + repr(ens) + ".pdb", dir=self.results_dir+sep+"ensembles_superimposed", force=True)
def create(dir=None, binary=None, diff_search=None, sims=None, sim_type=None, trim=None, steps=None, heteronuc_type=None, atom1=None, atom2=None, spin_id=None, force=False, constraints=True): """Create the Modelfree4 input files. The following files are created: - dir/mfin - dir/mfdata - dir/mfpar - dir/mfmodel - dir/run.sh @keyword dir: The optional directory to place the files into. If None, then the files will be placed into a directory named after the current data pipe. @type dir: str or None @keyword binary: The name of the Modelfree4 binary file. This can include the path to the binary. @type binary: str @keyword diff_search: The diffusion tensor search algorithm (see the Modelfree4 manual for details). @type diff_search: str @keyword sims: The number of Monte Carlo simulations to perform. @type sims: int @keyword sim_type: The type of simulation to perform (see the Modelfree4 manual for details). @type sim_type: str @keyword trim: Trimming of the Monte Carlo simulations (see the Modelfree4 manual for details). @type trim: int @keyword steps: The grid search size (see the Modelfree4 manual for details). @type steps: int @keyword heteronuc_type: The Modelfree4 three letter code for the heteronucleus type, e.g. '15N', '13C', etc. @type heteronuc_type: str @keyword atom1: The name of the heteronucleus in the PDB file. @type atom1: str @keyword atom2: The name of the proton in the PDB file. @type atom2: str @keyword spin_id: The spin identification string. @type spin_id: str @keyword force: A flag which if True will cause all pre-existing files to be overwritten. @type force: bool @keyword constraints: A flag which if True will result in constrained optimisation. @type constraints: bool """ # Test if the current pipe exists. pipes.test() # Test if sequence data is loaded. if not exists_mol_res_spin_data(): raise RelaxNoSequenceError # Test if the PDB file is loaded (for the spheroid and ellipsoid). if hasattr(cdp, 'diff_tensor') and not cdp.diff_tensor.type == 'sphere' and not hasattr(cdp, 'structure'): raise RelaxNoPdbError # Deselect certain spins. __deselect_spins() # Directory creation. if dir == None: dir = pipes.cdp_name() mkdir_nofail(dir, verbosity=0) # Number of field strengths and values. frq = [] for ri_id in cdp.ri_ids: # New frequency. if cdp.spectrometer_frq[ri_id] not in frq: frq.append(cdp.spectrometer_frq[ri_id]) # The 'mfin' file. mfin = open_write_file('mfin', dir, force) create_mfin(mfin, diff_search=diff_search, sims=sims, sim_type=sim_type, trim=trim, num_frq=len(frq), frq=frq) mfin.close() # Open the 'mfdata', 'mfmodel', and 'mfpar' files. mfdata = open_write_file('mfdata', dir, force) mfmodel = open_write_file('mfmodel', dir, force) mfpar = open_write_file('mfpar', dir, force) # Loop over the sequence. for spin, mol_name, res_num, res_name, id in spin_loop(spin_id, full_info=True, return_id=True): # Skip deselected spins. if not spin.select: continue # The 'mfdata' file. if not create_mfdata(mfdata, spin=spin, spin_id=id, num_frq=len(frq), frq=frq): continue # The 'mfmodel' file. create_mfmodel(mfmodel, spin=spin, spin_id=id, steps=steps, constraints=constraints) # The 'mfpar' file. create_mfpar(mfpar, spin=spin, spin_id=id, res_num=res_num, atom1=atom1, atom2=atom2) # Close the 'mfdata', 'mfmodel', and 'mfpar' files. mfdata.close() mfmodel.close() mfpar.close() # The 'run.sh' script. run = open_write_file('run.sh', dir, force) create_run(run, binary=binary, dir=dir) run.close() chmod(dir + sep+'run.sh', S_IRWXU|S_IRGRP|S_IROTH)