Module deeporigin.src.utilities.conversions
Functions
def convert_block(source_type, source, destination_type, add_hydrogens=False)
-
Expand source code
def convert_block(source_type, source, destination_type, add_hydrogens=False): """ Convert molecular structure formats between different file types. This function takes a molecular structure in one format and converts it to another format using temporary files for the conversion process. Args: source_type (str): The file format of the input structure (e.g., 'pdb', 'mol2', 'sdf') source (str): The molecular structure content as a string in the source format destination_type (str): The desired output file format add_hydrogens (bool, optional): Whether to add hydrogen atoms during conversion. Defaults to False Returns: str: The converted molecular structure as a string in the destination format Raises: Any exceptions from the underlying convert_file function may be propagated Note: This function creates temporary files during the conversion process. The temporary files are automatically cleaned up by the system. """ source_file = tempfile.mktemp() with open(source_file, 'w') as f: f.write(source) destination_file = convert_file(source_type, source_file, destination_type, add_hydrogens=add_hydrogens) with open(destination_file, 'r') as f: return f.read()
Convert molecular structure formats between different file types.
This function takes a molecular structure in one format and converts it to another format using temporary files for the conversion process.
Args
source_type
:str
- The file format of the input structure (e.g., 'pdb', 'mol2', 'sdf')
source
:str
- The molecular structure content as a string in the source format
destination_type
:str
- The desired output file format
add_hydrogens
:bool
, optional- Whether to add hydrogen atoms during conversion. Defaults to False
Returns
str
- The converted molecular structure as a string in the destination format
Raises
Any exceptions from the underlying convert_file function may be propagated
Note
This function creates temporary files during the conversion process. The temporary files are automatically cleaned up by the system.
def convert_file(source_type, source, destination_type, destination=None, add_hydrogens=False)
-
Expand source code
def convert_file(source_type, source, destination_type, destination=None, add_hydrogens=False): """ Converts a chemical structure file from one format to another. This function uses OpenBabel via Pybel to convert between different molecular file formats. If no destination path is specified, it creates a temporary file. Args: source_type (str): Input file format (e.g., 'pdb', 'mol2', 'sdf') source (str): Path to the input file destination_type (str): Output file format (e.g., 'pdb', 'mol2', 'sdf') destination (str, optional): Path for the output file. If None, creates a temp file. Defaults to None. add_hydrogens (bool, optional): Whether to add hydrogen atoms to the molecule. Defaults to False. Returns: str: Path to the converted file Raises: IOError: If the source file cannot be read or destination cannot be written ValueError: If the file formats are not supported by OpenBabel """ mol_pb_gen = pybel.readfile(str(source_type), source) if not destination: destination = tempfile.mktemp() out_file = pybel.Outputfile(str(destination_type), destination, overwrite=True) for mol_pb in mol_pb_gen: if add_hydrogens: mol_pb.addh() out_file.write(mol_pb) out_file.close() return destination
Converts a chemical structure file from one format to another.
This function uses OpenBabel via Pybel to convert between different molecular file formats. If no destination path is specified, it creates a temporary file.
Args
source_type
:str
- Input file format (e.g., 'pdb', 'mol2', 'sdf')
source
:str
- Path to the input file
destination_type
:str
- Output file format (e.g., 'pdb', 'mol2', 'sdf')
destination
:str
, optional- Path for the output file. If None, creates a temp file. Defaults to None.
add_hydrogens
:bool
, optional- Whether to add hydrogen atoms to the molecule. Defaults to False.
Returns
str
- Path to the converted file
Raises
IOError
- If the source file cannot be read or destination cannot be written
ValueError
- If the file formats are not supported by OpenBabel