Révision 748a6036 modules/formats.py
b/modules/formats.py | ||
---|---|---|
95 | 95 |
@param spec_atms: List of tuples containing pairs of new/traditional |
96 | 96 |
chemical symbols. |
97 | 97 |
@return: an object the required library can work with. |
98 |
""" # TODO POSCAR/CONTCAR files
|
|
98 |
""" |
|
99 | 99 |
import ase.io |
100 | 100 |
from ase.io.formats import filetype |
101 | 101 |
|
102 |
from modules.utilities import try_command |
|
103 |
|
|
102 | 104 |
req_vals = ['rdkit', 'ase'] |
103 |
file_type_vals = ['xyz', 'mol'] |
|
104 | 105 |
lib_err = f"The conversion to the '{requirement}' library object type" \ |
105 | 106 |
f" has not yet been implemented" |
106 | 107 |
conv_info = f"Converted {coord_file} to {requirement} object type" |
107 | 108 |
|
108 |
fil_type_err = f'The {filetype(coord_file)} file formnat is not supported'
|
|
109 |
fil_type_err = f'The {filetype(coord_file)} file format is not supported' |
|
109 | 110 |
|
110 | 111 |
if requirement not in req_vals: |
111 | 112 |
logger.error(lib_err) |
112 | 113 |
raise NotImplementedError(lib_err) |
113 | 114 |
|
114 |
if filetype(coord_file) not in file_type_vals: |
|
115 |
logger.error(fil_type_err) |
|
116 |
raise NotImplementedError(fil_type_err) |
|
117 |
|
|
118 | 115 |
if requirement == 'rdkit': |
119 |
if filetype(coord_file) == 'xyz':
|
|
120 |
from modules.xyz2mol import xyz2mol
|
|
116 |
from modules.xyz2mol import xyz2mol
|
|
117 |
if filetype(coord_file) == 'xyz': # TODO Include detection of charges.
|
|
121 | 118 |
ase_atms = ase.io.read(coord_file) |
122 | 119 |
atomic_nums = ase_atms.get_atomic_numbers().tolist() |
123 | 120 |
xyz_coordinates = ase_atms.positions.tolist() |
124 |
rd_mol_obj = xyz2mol(atomic_nums, xyz_coordinates, charge=0)
|
|
121 |
rd_mol_obj = xyz2mol(atomic_nums, xyz_coordinates) |
|
125 | 122 |
logger.debug(conv_info) |
126 | 123 |
return Chem.AddHs(rd_mol_obj) |
127 | 124 |
elif filetype(coord_file) == 'mol': |
128 | 125 |
logger.debug(conv_info) |
129 | 126 |
return Chem.AddHs(Chem.MolFromMolFile(coord_file, removeHs=False)) |
127 |
else: |
|
128 |
ase_atms = try_command(ase.io.read, |
|
129 |
[(ase.io.formats.UnknownFileTypeError, |
|
130 |
fil_type_err)], |
|
131 |
coord_file) |
|
132 |
atomic_nums = ase_atms.get_atomic_numbers().tolist() |
|
133 |
xyz_coordinates = ase_atms.positions.tolist() |
|
134 |
return xyz2mol(atomic_nums, xyz_coordinates) |
|
130 | 135 |
|
131 | 136 |
if requirement == 'ase': |
132 | 137 |
add_special_atoms(spec_atms) |
... | ... | |
137 | 142 |
logger.debug(conv_info) |
138 | 143 |
rd_mol = Chem.AddHs(Chem.MolFromMolFile(coord_file, removeHs=False)) |
139 | 144 |
return rdkit_mol_to_ase_atoms(rd_mol) |
145 |
else: |
|
146 |
return try_command(ase.io.read, |
|
147 |
[(ase.io.formats.UnknownFileTypeError, |
|
148 |
fil_type_err)], |
|
149 |
coord_file) |
|
140 | 150 |
|
141 | 151 |
|
142 | 152 |
def read_coords_cp2k(file, spec_atoms=tuple()): |
Formats disponibles : Unified diff