Here are some usage notes, explaining the above script. To reproduce the examples within them, download the supporting material from this paper: Cryst. Growth Des., 2009, 9, 1869–1888. Comments, feedback and improvements very welcome!
A number of molecules exhibit very complex solid-form landscapes with a range polymorphs, solvates, hydrates etc. The Packing Similarity Tool from CSD-Materials can be used to explore how the packing of these molecules varies between different solid forms. One way to view this information of packing similarity is to construct a dendrogram or tree diagram. This has previously been done by hand for carbamazepine (Cryst. Growth Des., 2009, 9, 1869–1888). The attached script performs the packing similarity and produces such a dendrogram using the CSD Python API and matplotlib.
You could edit the temporary file with python and then read it back in - giving you a molecule or crystal object with the correct identifier (note though depending on what object you use and the original data/attributes, you could lose information in writing it out). Below is an example script for changing the identifier of a crystal object. For a molecule you probably want to write to a mol2 file (in which case you want to edit the second line).
def change_identifier_for_crystal(crystal, new_id):
temp_res_file = "delete_me.res"
# Re-label/edit identifier manually using a temp file
with ccdc.io.CrystalWriter(temp_res_file) as temp_writer:
g = open(temp_res_file, "r")
lines = g.readlines()
g = open(temp_res_file, "w")
for line in lines:
if "TITL" in line:
# For a res file the identifier is read from whatever comes after TITL
g.write("TITL " + new_id + "\n")
crystal_reader = ccdc.io.CrystalReader(temp_res_file)
crystal = crystal_reader
csd_reader = ccdc.io.EntryReader('CSD')
hxacan_entry = csd_reader.entry('HXACAN')
hxacan_crystal = hxacan_entry.crystal
hxacan_crystal = change_identifier_for_crystal(hxacan_crystal, 'new_id')
Version 1.0 of the API doesn't allow you to edit the identifier attribute but this feature should be available in the next released version. In the meantime you could write the structure to a (temporary) file and edit it there and read it back in if necessary (e.g. for multiple structure you could edit individual files and then read them back in to output to a multi-cif etc. file).
The API has functionality for showing tick marks and will return the two theta value of the tick mark but, unfortunately, the code for labelling the ticks by hkl value isn't currently exposed in the API. I've just logged a feature request for this though, so hopefully it will be available in a future version.
If you are dealing with a molecule from a crystal structure then you can invert molecules in it using the symmetric_molecule method (API reference). Here is an example:
inverted_mol = my_crystal.symmetric_molecule('-x,-y,-z', (0, 0, 0), force=True)
By default the method only allows you to apply operations compatible with that crystal's space-group symmetry (as in the GUI tool) but you can force inversion with the force=True option.
If you only have a molecule, with no crystal information, this method can't be used. However, if comparing molecules using the overlay method you can allow for inversion. Alternatively, you can create an empty default crystal and put the molecule into it and then apply the operations as above.