Hi all,

A very simple question: is it possible to change the identifier of a given molecule using the Python API? If so, how? Thanks in advance for the help!



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).

Hi Anthony,

Thanks for the help! Do you mean editing it manually? Because it wouldn't be this useful to generate a quick script. But I guess I can find alternative solutions (like generating sub-directories or so). 



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).


import ccdc.io
import os

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[0]
return crystal

csd_reader = ccdc.io.EntryReader('CSD')
hxacan_entry = csd_reader.entry('HXACAN')
hxacan_crystal = hxacan_entry.crystal
print hxacan_crystal.identifier
hxacan_crystal = change_identifier_for_crystal(hxacan_crystal, 'new_id')
print hxacan_crystal.identifier

You must be signed in to post in this forum.