Hi,
is this for a particular data table? Or one that you created yourself?
The following code should change your NmrResidue residueTypes from 1 letter to 3 letter code
def convertResidueCode(residueName, inputCodeType='oneLetter', outputCodeType='threeLetter', molType ='protein'):
"""
:param inputCodeType: oneLetter, threeLetter, synonym, molFormula
:type inputCodeType: str
:param molType: 'protein', 'DNA', 'RNA'
:type molType: str
:return: the same residue with the new letter code/name
:rtype: str
"""
from ccpnmodel.ccpncore.lib.chemComp.ChemCompOverview import chemCompStdDict
modes = ['oneLetter', 'threeLetter', 'synonym', 'molFormula'] # order as they come from ChemCom dictionary
if inputCodeType not in modes or outputCodeType not in modes:
print('Code type not recognised. It has to be one of: ', modes)
return
for k, v in chemCompStdDict.get(molType).items():
dd = {i:j for i,j in zip(modes,v)}
if residueName == dd.get(inputCodeType):
return dd.get(outputCodeType)
def convertNmrChain1to3LetterCode(nmrChain, molType='protein'):
"""
converts NmrResidues from 1 to 3 LetterCode.
:param nmrChain: Ccpn object NmrChain
:param molType: 'protein', 'DNA', 'RNA'
:return:
"""
for nmrResidue in nmrChain.nmrResidues:
if len(nmrResidue.residueType) == 1:
newNmrResidueName = convertResidueCode(nmrResidue.residueType, inputCodeType='oneLetter', outputCodeType='threeLetter', molType=molType)
try:
nmrResidue.rename(nmrResidue.sequenceCode, newNmrResidueName)
except Exception as err:
print('Error renaming NmrResidue %s.' %nmrResidue.pid, err)
else:
if not nmrResidue.residueType:
print('Skipping... ResidueType not found for nmrResidue %s' %nmrResidue.pid)
else:
print('Skipping... Could not rename to 1 Letter code for nmrResidue %s' % nmrResidue.pid)
def convertNmrChain3to1LetterCode(nmrChain, molType='protein'):
"""
converts NmrResidues from 3 to 1 LetterCode.
:param nmrChain: Ccpn object NmrChain
:param molType: 'protein', 'DNA', 'RNA'
:return:
"""
for nmrResidue in nmrChain.nmrResidues:
if len(nmrResidue.residueType) == 3:
newNmrResidueName = convertResidueCode(nmrResidue.residueType, inputCodeType='threeLetter', outputCodeType='oneLetter', molType=molType)
try:
nmrResidue.rename(nmrResidue.sequenceCode, newNmrResidueName)
except Exception as err:
print('Error renaming NmrResidue %s.' %nmrResidue.pid, err)
else:
if not nmrResidue.residueType:
print('Skipping... ResidueType not found for nmrResidue %s' %nmrResidue.pid)
else:
print('Skipping... Could not rename to 3 Letter code for nmrResidue %s' % nmrResidue.pid)
# Do this on every NmrChain in the project
with undoBlock():
for nmrChain in project.nmrChains:
convertNmrChain3to1LetterCode(nmrChain)
Easiest thing is to open the Macro Editor (NM) and copy and paste this code in, then use the green play button to run it.
At the moment ILE residues seem to be causing an issue (they won’t convert). There seems to be some degeneracy in the ChemComp dictionary. Looks like a mistake to me, but I’ll need to check with colleagues before I make changes. Once we sort that I’ll add this to the CCPN Macros in the Macro menu.
There is already a CCPN macro which does the opposite (i.e. 1-letter to 3-letter conversion).
So depending on exactly what you are trying to do, these bits of code might help you.
To convert the Isoleucines form ILE to I you can use this code:
with undoBlock():
for nmrChain in project.nmrChains:
for nmrResidue in nmrChain.nmrResidues:
if nmrResidue.residueType == 'ILE':
try:
nmrResidue.rename(nmrResidue.sequenceCode, '')
except Exception as err:
print('Error renaming NmrResidue %s.' %nmrResidue.pid, err)
try:
nmrResidue.rename(nmrResidue.sequenceCode, 'I')
except Exception as err:
print('Error renaming NmrResidue %s.' %nmrResidue.pid, err)
Vicky