Skip to content

Commit

Permalink
Check consistency between axis_code and peak assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
yokochi47 committed Dec 6, 2024
1 parent 094f982 commit 48c1132
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 320 deletions.
4 changes: 3 additions & 1 deletion wwpdb/utils/nmr/mr/ParserListenerUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2086,7 +2086,9 @@
'encoded_reduced_dimension_id': None
}

SPECTRAL_DIM_TRANSFER_TEMPLATE = {'indirect': None,
SPECTRAL_DIM_TRANSFER_TEMPLATE = {'spectral_dim_id_1': None,
'spectral_dim_id_2': None,
'indirect': None,
'type': None}


Expand Down
158 changes: 55 additions & 103 deletions wwpdb/utils/nmr/pk/AriaPKParserListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class AriaPKParserListener(ParseTreeListener, BasePKParserListener):
__volume = None
__volume_error = None

__proton1_active = False
__proton2_active = False
__hetero1_active = False
__hetero2_active = False

def __init__(self, verbose=True, log=sys.stdout,
representativeModelId=REPRESENTATIVE_MODEL_ID,
representativeAltId=REPRESENTATIVE_ALT_ID,
Expand Down Expand Up @@ -253,62 +258,68 @@ def exitElement(self, ctx: XMLParser.ElementContext): # pylint: disable=unused-

if self.__cur_path == '/spectrum':
self.num_of_dim = -1
self.__proton1_active, self.__proton2_active, self.__hetero1_active, self.__hetero2_active =\
False, False, False, False

elif self.__cur_path == '/spectrum/peak':
if self.num_of_dim == -1:
self.num_of_dim = 0
if self.__proton1_ppm is not None:
self.num_of_dim += 1
self.__proton1_active = True
if self.__proton2_ppm is not None:
self.num_of_dim += 1
self.__proton2_active = True
if self.__hetero1_ppm is not None:
self.num_of_dim += 1
self.__hetero1_active = True
if self.__hetero2_ppm is not None:
self.num_of_dim += 1
self.fillCurrentSpectralDim()
self.__hetero2_active = True
self.initSpectralDim()

index = self.__index

ppm = [None] * self.num_of_dim
ppm_error = [None] * self.num_of_dim
ass = [None] * self.num_of_dim

idx = 0
if self.__proton1_active:
ppm[idx] = self.__proton1_ppm
ppm_error[idx] = self.__proton1_ppm_error
if self.__proton1_atoms is not None and len(self.__proton1_atoms) == 1:
ass[idx] = self.__proton1_atoms
idx += 1
if self.__proton2_active:
ppm[idx] = self.__proton2_ppm
ppm_error[idx] = self.__proton2_ppm_error
if self.__proton2_atoms is not None and len(self.__proton2_atoms) == 1:
ass[idx] = self.__proton2_atoms
idx += 1
if self.__hetero1_active:
ppm[idx] = self.__hetero1_ppm
ppm_error[idx] = self.__hetero1_ppm_error
if self.__hetero1_atoms is not None and len(self.__hetero1_atoms) == 1:
ass[idx] = self.__hetero1_atoms
idx += 1
if self.__hetero2_active:
ppm[idx] = self.__hetero2_ppm
ppm_error[idx] = self.__hetero2_ppm_error
if self.__hetero2_atoms is not None and len(self.__hetero2_atoms) == 1:
ass[idx] = self.__hetero2_atoms

if not all(a is not None and len(a) == 1 and 'seq_id' in a[0] and 'atom_id' in a[0] for a in ass):
ass = [None] * self.num_of_dim

if self.num_of_dim == 2:
self.peaks2D += 1

ppm = [None] * self.num_of_dim
ppm_error = [None] * self.num_of_dim
ass = [None] * self.num_of_dim

idx = 0
if self.__proton1_ppm is not None:
ppm[idx] = self.__proton1_ppm
ppm_error[idx] = self.__proton1_ppm_error
if self.__proton1_atoms is not None and len(self.__proton1_atoms) == 1:
ass[idx] = self.__proton1_atoms
idx += 1
if self.__proton2_ppm is not None:
ppm[idx] = self.__proton2_ppm
ppm_error[idx] = self.__proton2_ppm_error
if self.__proton2_atoms is not None and len(self.__proton2_atoms) == 1:
ass[idx] = self.__proton2_atoms
idx += 1
if self.__hetero1_ppm is not None:
ppm[idx] = self.__hetero1_ppm
ppm_error[idx] = self.__hetero1_ppm_error
if self.__hetero1_atoms is not None and len(self.__hetero1_atoms) == 1:
ass[idx] = self.__hetero1_atoms
idx += 1
if self.__hetero2_ppm is not None:
ppm[idx] = self.__hetero2_ppm
ppm_error[idx] = self.__hetero2_ppm_error
if self.__hetero2_atoms is not None and len(self.__hetero2_atoms) == 1:
ass[idx] = self.__hetero2_atoms

if not all(a is not None and len(a) == 1 and 'seq_id' in a[0] and 'atom_id' in a[0] for a in ass):
ass = [None] * self.num_of_dim

if None in (ppm[0], ppm[1])\
or (self.__intensity is None and self.__volume is None):
self.peaks2D -= 1
return

index = self.__index

dstFunc = self.validatePeak2D(index, ppm[0], ppm[1],
ppm_error[0], ppm_error[1],
None, None, None, None, None, None,
Expand Down Expand Up @@ -374,6 +385,8 @@ def exitElement(self, ctx: XMLParser.ElementContext): # pylint: disable=unused-

if len(self.atomSelectionSet) == self.num_of_dim:
has_assignments = True
has_assignments &= self.fillAtomTypeInCase(1, a1['atom_id'][0])
has_assignments &= self.fillAtomTypeInCase(2, a2['atom_id'][0])

if self.createSfDict__:
sf = self.getSf()
Expand Down Expand Up @@ -409,45 +422,11 @@ def exitElement(self, ctx: XMLParser.ElementContext): # pylint: disable=unused-
elif self.num_of_dim == 3:
self.peaks3D += 1

ppm = [None] * self.num_of_dim
ppm_error = [None] * self.num_of_dim
ass = [None] * self.num_of_dim

idx = 0
if self.__proton1_ppm is not None:
ppm[idx] = self.__proton1_ppm
ppm_error[idx] = self.__proton1_ppm_error
if self.__proton1_atoms is not None and len(self.__proton1_atoms) == 1:
ass[idx] = self.__proton1_atoms
idx += 1
if self.__proton2_ppm is not None:
ppm[idx] = self.__proton2_ppm
ppm_error[idx] = self.__proton2_ppm_error
if self.__proton2_atoms is not None and len(self.__proton2_atoms) == 1:
ass[idx] = self.__proton2_atoms
idx += 1
if self.__hetero1_ppm is not None:
ppm[idx] = self.__hetero1_ppm
ppm_error[idx] = self.__hetero1_ppm_error
if self.__hetero1_atoms is not None and len(self.__hetero1_atoms) == 1:
ass[idx] = self.__hetero1_atoms
idx += 1
if self.__hetero2_ppm is not None:
ppm[idx] = self.__hetero2_ppm
ppm_error[idx] = self.__hetero2_ppm_error
if self.__hetero2_atoms is not None and len(self.__hetero2_atoms) == 1:
ass[idx] = self.__hetero2_atoms

if not all(a is not None and len(a) == 1 and 'seq_id' in a[0] and 'atom_id' in a[0] for a in ass):
ass = [None] * self.num_of_dim

if None in (ppm[0], ppm[1], ppm[2])\
or (self.__intensity is None and self.__volume is None):
self.peaks3D -= 1
return

index = self.__index

dstFunc = self.validatePeak3D(index, ppm[0], ppm[1], ppm[2],
ppm_error[0], ppm_error[1], ppm_error[2],
None, None, None, None, None, None, None, None, None,
Expand Down Expand Up @@ -524,6 +503,9 @@ def exitElement(self, ctx: XMLParser.ElementContext): # pylint: disable=unused-

if len(self.atomSelectionSet) == self.num_of_dim:
has_assignments = True
has_assignments &= self.fillAtomTypeInCase(1, a1['atom_id'][0])
has_assignments &= self.fillAtomTypeInCase(2, a2['atom_id'][0])
has_assignments &= self.fillAtomTypeInCase(3, a3['atom_id'][0])

if self.createSfDict__:
sf = self.getSf()
Expand Down Expand Up @@ -566,45 +548,11 @@ def exitElement(self, ctx: XMLParser.ElementContext): # pylint: disable=unused-
elif self.num_of_dim == 4:
self.peaks4D += 1

ppm = [None] * self.num_of_dim
ppm_error = [None] * self.num_of_dim
ass = [None] * self.num_of_dim

idx = 0
if self.__proton1_ppm is not None:
ppm[idx] = self.__proton1_ppm
ppm_error[idx] = self.__proton1_ppm_error
if self.__proton1_atoms is not None and len(self.__proton1_atoms) == 1:
ass[idx] = self.__proton1_atoms
idx += 1
if self.__proton2_ppm is not None:
ppm[idx] = self.__proton2_ppm
ppm_error[idx] = self.__proton2_ppm_error
if self.__proton2_atoms is not None and len(self.__proton2_atoms) == 1:
ass[idx] = self.__proton2_atoms
idx += 1
if self.__hetero1_ppm is not None:
ppm[idx] = self.__hetero1_ppm
ppm_error[idx] = self.__hetero1_ppm_error
if self.__hetero1_atoms is not None and len(self.__hetero1_atoms) == 1:
ass[idx] = self.__hetero1_atoms
idx += 1
if self.__hetero2_ppm is not None:
ppm[idx] = self.__hetero2_ppm
ppm_error[idx] = self.__hetero2_ppm_error
if self.__hetero2_atoms is not None and len(self.__hetero2_atoms) == 1:
ass[idx] = self.__hetero2_atoms

if not all(a is not None and len(a) == 1 and 'seq_id' in a[0] and 'atom_id' in a[0] for a in ass):
ass = [None] * self.num_of_dim

if None in (ppm[0], ppm[1], ppm[2], ppm[3])\
or (self.__intensity is None and self.__volume is None):
self.peaks4D -= 1
return

index = self.__index

dstFunc = self.validatePeak4D(index, ppm[0], ppm[1], ppm[2], ppm[3],
ppm_error[0], ppm_error[1], ppm_error[2], ppm_error[3],
None, None, None, None, None, None, None, None, None, None, None, None,
Expand Down Expand Up @@ -692,6 +640,10 @@ def exitElement(self, ctx: XMLParser.ElementContext): # pylint: disable=unused-

if len(self.atomSelectionSet) == self.num_of_dim:
has_assignments = True
has_assignments &= self.fillAtomTypeInCase(1, a1['atom_id'][0])
has_assignments &= self.fillAtomTypeInCase(2, a2['atom_id'][0])
has_assignments &= self.fillAtomTypeInCase(3, a3['atom_id'][0])
has_assignments &= self.fillAtomTypeInCase(4, a4['atom_id'][0])

if self.createSfDict__:
sf = self.getSf()
Expand Down
25 changes: 24 additions & 1 deletion wwpdb/utils/nmr/pk/BasePKParserListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
getPkLoop,
getMaxEffDigits,
roundString,
ISOTOPE_NUMBERS_OF_NMR_OBS_NUCS,
REPRESENTATIVE_MODEL_ID,
REPRESENTATIVE_ALT_ID,
MAX_PREF_LABEL_SCHEME_COUNT,
Expand Down Expand Up @@ -91,6 +92,7 @@
getPkLoop,
getMaxEffDigits,
roundString,
ISOTOPE_NUMBERS_OF_NMR_OBS_NUCS,
REPRESENTATIVE_MODEL_ID,
REPRESENTATIVE_ALT_ID,
MAX_PREF_LABEL_SCHEME_COUNT,
Expand Down Expand Up @@ -240,6 +242,7 @@ class BasePKParserListener():
num_of_dim = -1
acq_dim_id = 1
spectral_dim = {}
spectral_dim_transfer = {}

# whether to allow extended sequence temporary
__allow_ext_seq = False
Expand Down Expand Up @@ -407,6 +410,7 @@ def enter(self):
self.num_of_dim = -1
self.acq_dim_id = 1
self.spectral_dim = {}
self.spectral_dim_transfer = {}
self.listIdInternal = {}
self.chainNumberDict = {}
self.extResKey = []
Expand Down Expand Up @@ -713,7 +717,7 @@ def exit(self):
finally:
self.warningMessage = sorted(list(set(self.f)), key=self.f.index)

def fillCurrentSpectralDim(self):
def initSpectralDim(self):
self.cur_subtype = f'peak{self.num_of_dim}d'
if self.num_of_dim not in self.listIdInternal:
self.listIdInternal[self.num_of_dim] = 0
Expand All @@ -730,13 +734,32 @@ def fillCurrentSpectralDim(self):
or _dim_id not in self.cur_spectral_dim
else self.cur_spectral_dim[_dim_id])
self.spectral_dim[self.num_of_dim][self.cur_list_id][_dim_id]['freq_hint'] = []
if self.file_type == 'nm-pea-pip':
self.spectral_dim[self.num_of_dim][self.cur_list_id][_dim_id]['obs_freq_hint'] = []
if self.num_of_dim not in self.spectral_dim_transfer:
self.spectral_dim_transfer[self.num_of_dim] = {}
if self.cur_list_id not in self.spectral_dim_transfer[self.num_of_dim]:
self.spectral_dim_transfer[self.num_of_dim][self.cur_list_id] = []
if self.num_of_dim == 2:
self.peaks2D = 0
if self.num_of_dim == 3:
self.peaks3D = 0
if self.num_of_dim == 4:
self.peaks4D = 0

def fillAtomTypeInCase(self, _dim_id: int, atom_type: str) -> bool:
cur_spectral_dim = self.spectral_dim[self.num_of_dim][self.cur_list_id][_dim_id]
if cur_spectral_dim['atom_type'] is not None:
return cur_spectral_dim['atom_type'] == atom_type
if atom_type in ISOTOPE_NUMBERS_OF_NMR_OBS_NUCS:
cur_spectral_dim['atom_type'] = atom_type
cur_spectral_dim['atom_isotope_number'] = ISOTOPE_NUMBERS_OF_NMR_OBS_NUCS[atom_type][0]
return True
return False

def fillSpectralDimTransfer(self):
pass

def validatePeak2D(self, index: int, pos_1: float, pos_2: float,
pos_unc_1: Optional[float], pos_unc_2: Optional[float],
lw_1: Optional[float], lw_2: Optional[float],
Expand Down
Loading

0 comments on commit 48c1132

Please sign in to comment.