Renumber unconnected nmrChain

A macro to renumber an unconnected nmrChain.

import numpy as np
import re

nmrChain = get('NC:A') ## replace with your nmrChain name
offset = 100           ## residue 1 will become 101

newValues = []
for nmrResidue in nmrChain.nmrResidues:
    sequenceCode = nmrResidue.sequenceCode
    ints = re.findall(r'\d+', sequenceCode)
    if len(ints)==1:
        code,sep, resOffs = int(ints[0]), '',''
    if len(ints)==2:
        code, sep, resOffs = int(ints[0]), '-', int(ints[1])

    newCode = f'{int(code)+offset}{sep}{resOffs}'
    newValues.append([str(newCode), nmrResidue.residueType])

newValues = np.array(newValues)[::-1]
nmrResidues = np.array(nmrChain.nmrResidues)[::-1]

with undoBlock():
    for nmrResidue, newValue in zip(nmrResidues, newValues):
        print(f'Renaming {nmrResidue} to --> {newValue}')
        nmrResidue.rename(*newValue)
        

So far the macro is not running but I will play around! Thanks a lot Luca!

ok I tested on 3.1 with few nmrChains and various residue offsets. like i-1. Let me know any issues, traceback, or upload your version…

For the sake of anyone searching the archive…

This macro works for positive offsets or negative offsets into non-overlapping sequence space but fails for negative offsets into overlapping sequence space (i.e. where oldFirstResidue < newLastResidue < oldLast Residue). It also risks mangling negative NmrResidue ids.

It is superceded by the GUI menu item offered via right clicking on an NmrChain in the sidebar.

Yes exactly ! I found a way to overcome it by doing a two-step offset, first a large positive value (such as 500 or 1000), and then a negative value smaller, to get the final offset I wanted.
E.g here: +1000 then -1004 to get my final -4 offset without any overlapping issue.
At the end I do have of course one or two negative residues in my Final NMRChain, but in that case it is not an issue.
Thanks for answering !