Hello

I'm quite new to python (no experience whatsoever) and I need some help to convert a folder of mol2 files to cif files

With part of the  " send_mol2_to_notepad+.py   "  script (copied from elsewhere on this forum, see below) I can do this from within mercury, but only one file at a time

 

I would like to either select a bunch of structure files in mercury and apply the script to all of them

or use the command line to run a script that takes a supplied folder as input

 

any help is much appreceated

Regards

Koen

 

#
# This script can be used for any purpose without limitation subject to the
# conditions at http://www.ccdc.cam.ac.uk/Community/Pages/Licences/v2.aspx
#
# This permission notice and the following statement of attribution must be
# included in all copies or substantial portions of this script.
#
# 2014-06-05: created by P. A. Wood, the Cambridge Crystallographic Data Centre
# 2015-07-01: updated by M Towler, the Cambridge Crystallographic Data Centre
# 2015-07-17: made available by the Cambridge Crystallographic Data Centre

'''
    send_mol2_to_notepad+.py - Example script to illustrate how to output a structure file to a 3rd party program
    Currently notepad++, this could also be used for a 3rd party chemical editing application.  It can also be
    trivially changed to write CIF or other formats instead of Mol2
'''

import os
import subprocess
import ccdc.io
from mercury_interface import MercuryInterface

if __name__ == '__main__':
    # Extract information from system arguments
    helper = MercuryInterface()
    entry_id = helper.identifier
    html_file = helper.output_html_file

    # Extract output directory from input html_file string
    output_dir = os.path.dirname(html_file)

    # Read crystal relating to entry_id from any supported file format
    crystal = helper.current_entry.crystal

    # Write out crystal to structure file in output directory
    # N.B. You can change the format of output file with the extension!
    # crystal_file = os.path.join(output_dir, '%s.mol2' % entry_id)
    crystal_file = os.path.join(output_dir, '%s.cif' % entry_id)
    with ccdc.io.CrystalWriter(crystal_file) as filehandle:
        filehandle.write(crystal)

 

Hi Koen,

Give this script a try from the command line. You can give it either a folder of .mol2 files or a single .mol2 file, and it will convert the files into .cif format. If you're having trouble getting the correct folder, type the full path into the command line.

 

for example:

python mol2_to_cif.py C:\andy\my_mol2s

would convert all of the .mol2 files in the folder "my_mol2s" to .cif files.

 

If you want to delete the .mol2 files as you go, you can uncomment the two lines that read # os.remove(m) (you do this by deleting the "# " at the start). 

 

Please let me know how you get on.

Cheers,

Andy

Hi Andy

 

The script works as a charm,

There might be a small annoyance in the conversion from mol2 to cif though,

I'm currently processing the converted cif files with platon and several fail to process as some "zero" coordinates

(zero in both the original cif, as exported from the csd AND zero in the mol2 file, as exported from the csd)

are converted into numbers in scientific E notation eg. refcode DAVKAO gets the following coordinates

loop_
_atom_site_label
_atom_site_type_symbol
_atom_site_fract_x
_atom_site_fract_y
_atom_site_fract_z
Zn1 Zn -1.06457e-16 0.954369 0.635158
N1 N 0.133599 0.908702 0.555098
N2 N 0.250696 0.858901 0.398095
O1 O -1.1556e-16 0.949398 0.860605
O2 O -1.25329e-16 0.930802 1.1288
O3 O 0.0937993 0.841899 0.9702
S1 S -1.15442e-16 0.88749 0.980199
C1 C 0.215498 0.878998 0.640503

I guess the cif format accepts scientific E notation, but it would be more elegant to round the numbers to zero

 

 

(not sure neither if this is the proper place to post this comment)

 

Anyway, many thanks

Koen

 

You must be signed in to post in this forum.