It is not aiming for code golf, but the following offers the conversion to intermediate .xyz
which may be processed further (cf. vide infra):
import fnmatch
import os
import sys
gjf_register = []
for file in os.listdir("."):
# collect all .gjf:
if fnmatch.fnmatch(file, "*.gjf"):
gjf_register.append(file)
gjf_register.sort()
for entry in gjf_register:
# recover only the atom block:
per_file_register = []
with open(entry, mode="r") as source:
for line in source:
per_file_register.append(line)
del per_file_register[:5] # clip the first lines
# the other mandatory entry in the .xyz file:
number_of_atoms = len(per_file_register) - 1
# write an intermediate .xyz file:
intermediate_xyz = str(entry)[:-4] + str(".xyz")
print("File {} written.".format(intermediate_xyz))
with open(intermediate_xyz, mode="w") as newfile:
newfile.write(str(number_of_atoms))
newfile.write(str("\n"))
newfile.write(str(intermediate_xyz))
newfile.write(str("\n"))
for entry in per_file_register[:-1]:
newfile.write(entry)
sys.exit(0)
The conversion into .smi
may then be delegated to obabel
, e.g. by
obabel *.xyz -osmi -m
if there are multiple files to consider.
Proof: The following .cif
data_EFAHAW
_cell_length_a 8.1819
_cell_length_b 10.4285
_cell_length_c 9.2965
_cell_angle_alpha 90
_cell_angle_beta 99.962
_cell_angle_gamma 90
_symmetry_space_group_name_H-M 'P 21/c'
loop_
_symmetry_equiv_pos_site_id
_symmetry_equiv_pos_as_xyz
1 x,y,z
2 -x,1/2+y,1/2-z
3 -x,-y,-z
4 x,1/2-y,1/2+z
loop_
_atom_site_label
_atom_site_type_symbol
_atom_site_fract_x
_atom_site_fract_y
_atom_site_fract_z
O3 O 0.19290 0.35524 0.46580
C9 C 0.16780 0.24532 0.42690
O2 O 0.05757 0.21719 0.30246
C7 C 0.05180 0.07991 0.27798
C1 C 0.16866 0.02605 0.40441
C6 C 0.23660 0.12584 0.49162
C3 C 0.35070 0.10279 0.61788
C5 C 0.39278 -0.02130 0.65283
C2 C 0.32245 -0.12294 0.56458
C4 C 0.20840 -0.10008 0.43826
was converted by
babel -icif EFAHAW.cif -ogjf test.gjf
into
#Put Keywords Here, check Charge and Multiplicity.
EFAHAW
0 15
O 0.82917 3.70462 4.26502
C 0.68636 2.55832 3.90884
O -0.01540 2.26497 2.76942
C -0.02324 0.83334 2.54528
C 0.72957 0.27166 3.70291
C 1.14519 1.31232 4.50144
C 1.87569 1.07195 5.65752
C 2.16377 10.20637 5.97753
C 1.73027 9.14642 5.16948
C 1.00028 9.38482 4.01285
The above script converted it into .xyz
10
test.xyz
O 0.82917 3.70462 4.26502
C 0.68636 2.55832 3.90884
O -0.01540 2.26497 2.76942
C -0.02324 0.83334 2.54528
C 0.72957 0.27166 3.70291
C 1.14519 1.31232 4.50144
C 1.87569 1.07195 5.65752
C 2.16377 10.20637 5.97753
C 1.73027 9.14642 5.16948
C 1.00028 9.38482 4.01285
and already mentioned babel -ixyz test.xyz -osmi test.smiles
offered the SMILES string
O=C1O[C][C]=C1[C].[C][C][C] test.xyz
Due to the definition of asymmetric unit and unit cell, other original input formats than .cif
may work even better (i.e., may yield molecules which are not fragmented as here), e.g. «native» .mol2
or .sdf
.