Skip to content

Commit

Permalink
Minor code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yokochi47 committed Jan 16, 2025
1 parent 3c34cc3 commit 25d0172
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 237 deletions.
8 changes: 5 additions & 3 deletions wwpdb/utils/nmr/ChemCompUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
from wwpdb.utils.nmr.io.ChemCompIo import ChemCompReader
from wwpdb.utils.nmr.AlignUtil import (emptyValue,
monDict3,
protonBeginCode)
protonBeginCode,
isReservedLigCode)
cICc = ConfigInfoAppCc(getSiteId())
CC_CVS_PATH = cICc.get_site_cc_cvs_path()
except ImportError:
from nmr.io.ChemCompIo import ChemCompReader
from nmr.AlignUtil import (emptyValue,
monDict3,
protonBeginCode)
protonBeginCode,
isReservedLigCode)
CC_CVS_PATH = os.path.dirname(__file__) + '/ligand_dict' # need to setup 'ligand_dict' CCD resource for NMR restraint processing


Expand Down Expand Up @@ -154,7 +156,7 @@ def updateChemCompDict(self, compId: str) -> bool:
@return: True for successfully update CCD information or False for the case a given comp_id does not exist in CCD
"""

if compId in emptyValue:
if compId in emptyValue or isReservedLigCode(compId):
return False

compId = compId.upper()
Expand Down
37 changes: 22 additions & 15 deletions wwpdb/utils/nmr/CifToNmrStar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#
# Updates:
# 13-Oct-2021 M. Yokochi - code revision according to PEP8 using Pylint (DAOTHER-7389, issue #5)
# 20-Apr-2022 M. Yokochi - enable to fix broken data block order of CIF formatted NMR-STAR using NMR-STAR schema (DAOTHER-7407, NMR restraint remediation)
# 20-Apr-2022 M. Yokochi - enable to fix broken datablock order of CIF formatted NMR-STAR using NMR-STAR schema (DAOTHER-7407, NMR restraint remediation)
# 28-Jul-2022 M. Yokochi - enable to fix format issue of CIF formatted NMR-STAR (You cannot have two loops with the same category in one saveframe. Category: '_Audit')
# 27-Sep-2022 M. Yokochi - auto fill list ID and entry ID (NMR restraint remediation)
# 13-Jun-2023 M. Yokochi - sort loops in a saveframe based on schema
# 30-May-2024 M. Yokochi - resolve duplication of data block/saveframe name (DAOTHER-9437)
# 30-May-2024 M. Yokochi - resolve duplication of datablock/saveframe name (DAOTHER-9437)
# 25-Jun-2024 M. Yokochi - strip white spaces in a datablock name derived from the model file (DAOTHER-9511)
# 07-Jan-2025 M. Yokochi - retrieve symbolic label representations (DAOTHER-1728, 9846)
##
Expand Down Expand Up @@ -62,23 +62,20 @@ def has_key_value(d: dict, key: Any) -> bool:
return False


def get_value_safe(d: Optional[dict] = None, key: Any = None) -> Any:
""" Return value of a given dictionary for a key.
@return: value for a key, None otherwise
def get_value_safe(d: Optional[Union[dict, list, tuple]] = None, key: Optional = None, default: Optional = None) -> Any:
""" Return value of a given dictionary, list, or tuple for a key.
@return: value for a key, None (by default) otherwise
"""

if d is None or key is None:
return None

if key not in d:
return None
return default

return d[key]
return d.get(key, default)


def get_first_sf_tag(sf: pynmrstar.Saveframe, tag: str, default: str = '') -> str:
def get_first_sf_tag(sf: pynmrstar.Saveframe, tag: str, default: str = '') -> Any:
""" Return the first value of a given saveframe tag with decoding symbol notation.
@return: The first tag value, default string otherwise.
@return: The first tag value, '' (by default) otherwise.
"""

if not isinstance(sf, pynmrstar.Saveframe) or tag is None:
Expand All @@ -92,11 +89,14 @@ def get_first_sf_tag(sf: pynmrstar.Saveframe, tag: str, default: str = '') -> st
if not isinstance(array[0], str):
return array[0]

if array[0] == '$':
return default

value = array[0]

while value.startswith('$$'):
value = value[1:]

if len(value) == 0 or value == '$':
return default

return value if len(value) < 2 or value[0] != '$' else value[1:]


Expand All @@ -107,9 +107,16 @@ def set_sf_tag(sf: pynmrstar.Saveframe, tag: str, value: Any):
tagNames = [t[0] for t in sf.tags]

if isinstance(value, str):

if len(value) == 0:
value = None

while value.startswith('$$'):
value = value[1:]

if len(value) == 0 or value == '$':
value = None

if tag not in tagNames:
sf.add_tag(tag, value)
return
Expand Down
2 changes: 1 addition & 1 deletion wwpdb/utils/nmr/NmrDpReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@ def updateNonStandardResidueByExptlData(self, chain_id: str, seq_id: int, conten
# if self.__verbose:
# self.__lfh.write(f'+{self.__class_name__}.updateNonStandardResidueByExptlData() ++ Error - Unknown seq_id {seq_id}\n')
# raise KeyError(f"+{self.__class_name__}.updateNonStandardResidueByExptlData() ++ Error - Unknown seq_id {seq_id}")
#

except StopIteration:
if self.__verbose:
self.__lfh.write(f'+{self.__class_name__}.updateNonStandardResidueByExptlData() ++ Error - Unknown chain_id {chain_id}')
Expand Down
12 changes: 6 additions & 6 deletions wwpdb/utils/nmr/NmrDpUtility.py
Original file line number Diff line number Diff line change
Expand Up @@ -10591,7 +10591,7 @@ def __detectContentSubTypeOfLegacyMr(self) -> bool:

s = re.split('[ ()]', _line)

_t_lower = ""
_t_lower = ''

for t in s:

Expand Down Expand Up @@ -10678,7 +10678,7 @@ def __detectContentSubTypeOfLegacyMr(self) -> bool:

s = re.split('[ ()=]', _line)

_t_lower = ""
_t_lower = ''

for t in s:

Expand Down Expand Up @@ -11740,7 +11740,7 @@ def __detectContentSubTypeOfLegacyMr(self) -> bool:
if not is_aux_amb and not is_aux_gro and not is_aux_cha and not has_chem_shift and not has_dist_restraint and not has_dihed_restraint and not has_rdc_restraint\
and not has_plane_restraint and not has_hbond_restraint and not has_ssbond_restraint and not valid:

hint = ""
hint = ''
if len(concat_nmr_restraint_names(content_subtype)) == 0:
if file_type in ('nm-res-xpl', 'nm-res-cns') and not has_rdc_origins:
hint = 'assign ( segid $ and resid # and name $ ) ( segid $ and resid # and name $ ) #.# #.# #.#'
Expand All @@ -11762,7 +11762,7 @@ def __detectContentSubTypeOfLegacyMr(self) -> bool:

elif is_aux_amb and not has_amb_coord and not has_topology:

subtype_name = ""
subtype_name = ''
if has_chem_shift:
subtype_name += "Assigned chemical shifts, "
if has_dist_restraint:
Expand Down Expand Up @@ -11802,7 +11802,7 @@ def __detectContentSubTypeOfLegacyMr(self) -> bool:

elif is_aux_gro and not has_topology:

subtype_name = ""
subtype_name = ''
if has_chem_shift:
subtype_name += "Assigned chemical shifts, "
if has_dist_restraint:
Expand Down Expand Up @@ -11836,7 +11836,7 @@ def __detectContentSubTypeOfLegacyMr(self) -> bool:

elif is_aux_cha and not has_topology:

subtype_name = ""
subtype_name = ''
if has_chem_shift:
subtype_name += "Assigned chemical shifts, "
if has_dist_restraint:
Expand Down
2 changes: 1 addition & 1 deletion wwpdb/utils/nmr/ann/BMRBAnnTasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import copy
import pynmrstar

from typing import IO, List, Optional
from packaging import version
from operator import itemgetter
from typing import IO, List, Optional

try:
from wwpdb.utils.nmr.ChemCompUtil import ChemCompUtil
Expand Down
1 change: 1 addition & 0 deletions wwpdb/utils/nmr/ann/OneDepAnnTasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import collections

from typing import IO, List

from wwpdb.utils.align.alignlib import PairwiseAlign # pylint: disable=no-name-in-module

try:
Expand Down
Loading

0 comments on commit 25d0172

Please sign in to comment.