Author: bugman Date: Sun Mar 10 14:50:01 2013 New Revision: 18735 URL: http://svn.gna.org/viewcvs/relax?rev=18735&view=rev Log: Created the generic_fns.mol_res_spin.return_molecule_by_name() function. This will be used in the future as it is much faster than generic_fns.mol_res_spin.return_molecule() if the molecule name is already known. Modified: trunk/generic_fns/mol_res_spin.py Modified: trunk/generic_fns/mol_res_spin.py URL: http://svn.gna.org/viewcvs/relax/trunk/generic_fns/mol_res_spin.py?rev=18735&r1=18734&r2=18735&view=diff ============================================================================== --- trunk/generic_fns/mol_res_spin.py (original) +++ trunk/generic_fns/mol_res_spin.py Sun Mar 10 14:50:01 2013 @@ -2643,6 +2643,39 @@ # Return the molecule container. return mol_container + + +def return_molecule_by_name(name=None, pipe_cont=None, pipe_name=None): + """Return the molecule container matching the given name. + + @keyword name: The molecule name. If not supplied and only a single molecule container exists, then that container will be returned. + @type name: str + @keyword pipe_cont: The data pipe object. + @type pipe_cont: PipeContainer instance + @keyword pipe_name: The data pipe name. + @type pipe_name: str + @return: The molecule container object. + @rtype: MoleculeContainer instance + """ + + # The data pipe. + if pipe_cont == None: + pipe_cont = pipes.get_pipe(pipe) + + # No molecule name specified, so assume a single molecule. + if name == None: + # More than one molecule. + if len(pipe_cont.mol) > 1: + raise RelaxError("Cannot return the molecule with no name as more than one molecule exists.") + + # Return the molecule. + return pipe_cont.mol[0] + + # Loop over the molecules. + for mol in pipe_cont.mol: + # Return the matching molecule. + if mol.name == name: + return mol def return_residue(selection=None, pipe=None, indices=False):