diff --git a/wwpdb/utils/nmr/AlignUtil.py b/wwpdb/utils/nmr/AlignUtil.py index b3bbf284..6c291083 100644 --- a/wwpdb/utils/nmr/AlignUtil.py +++ b/wwpdb/utils/nmr/AlignUtil.py @@ -95,7 +95,7 @@ def isReservedLigCode(compId: str) -> bool: """ if compId in reservedLigCode: return True - if len(compId) == 2 and compId[0].isdigit() and compId[1].digit() and compId != '00': + if len(compId) == 2 and compId[0].isdigit() and compId[1].isdigit() and compId != '00': return True return False diff --git a/wwpdb/utils/nmr/ChemCompUtil.py b/wwpdb/utils/nmr/ChemCompUtil.py index 72c0ddb8..8c10baba 100644 --- a/wwpdb/utils/nmr/ChemCompUtil.py +++ b/wwpdb/utils/nmr/ChemCompUtil.py @@ -24,7 +24,8 @@ try: from wwpdb.utils.config.ConfigInfo import getSiteId from wwpdb.utils.config.ConfigInfoApp import ConfigInfoAppCc - from wwpdb.utils.nmr.io.ChemCompIo import ChemCompReader + from wwpdb.utils.nmr.io.ChemCompReader import (ChemCompReader, + ccd_id_pattern) from wwpdb.utils.nmr.AlignUtil import (emptyValue, monDict3, protonBeginCode, @@ -32,7 +33,8 @@ cICc = ConfigInfoAppCc(getSiteId()) CC_CVS_PATH = cICc.get_site_cc_cvs_path() except ImportError: - from nmr.io.ChemCompIo import ChemCompReader + from nmr.io.ChemCompReader import (ChemCompReader, + ccd_id_pattern) from nmr.AlignUtil import (emptyValue, monDict3, protonBeginCode, @@ -58,7 +60,7 @@ def __init__(self, verbose: bool = False, log: IO = sys.stderr): self.lastAtomList = None self.lastBonds = None - # taken from wwpdb.utils.nmr.io.ChemCompIo + # taken from wwpdb.utils.nmr.io.ChemCompReader _chemCompAtomDict = [ ('_chem_comp_atom.comp_id', '%s', 'str', ''), ('_chem_comp_atom.atom_id', '%s', 'str', ''), @@ -114,7 +116,7 @@ def __init__(self, verbose: bool = False, log: IO = sys.stderr): cTerminalAtomFlag = next(d for d in _chemCompAtomDict if d[0] == '_chem_comp_atom.pdbx_c_terminal_atom_flag') self.ccaCTerminalAtomFlag = _chemCompAtomDict.index(cTerminalAtomFlag) - # taken from wwpdb.utils.nmr.io.ChemCompIo + # taken from wwpdb.utils.nmr.io.ChemCompReader _chemCompBondDict = [ ('_chem_comp_bond.comp_id', '%s', 'str', ''), ('_chem_comp_bond.atom_id_1', '%s', 'str', ''), @@ -156,7 +158,8 @@ 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 or isReservedLigCode(compId): + if compId in emptyValue or not isinstance(compId, str)\ + or not ccd_id_pattern.match(compId) or isReservedLigCode(compId): return False compId = compId.upper() diff --git a/wwpdb/utils/nmr/NmrDpUtility.py b/wwpdb/utils/nmr/NmrDpUtility.py index c66f692b..8d761aa9 100644 --- a/wwpdb/utils/nmr/NmrDpUtility.py +++ b/wwpdb/utils/nmr/NmrDpUtility.py @@ -614,7 +614,7 @@ possible_typo_for_comment_out_pattern = re.compile(r'\s*([13])$') -sparky_assignment_pattern = re.compile(r'[0-9A-Za-z_\+\*\?\'\"]+-[0-9A-Za-z_\+\*\?\'\"]+\S*') +sparky_assignment_pattern = re.compile(r'[\w\+\*\?\'\"]+-[\w\+\*\?\'\"]+\S*') comment_code_mixed_set = {'#', '!'} diff --git a/wwpdb/utils/nmr/io/ChemCompIo.py b/wwpdb/utils/nmr/io/ChemCompReader.py similarity index 94% rename from wwpdb/utils/nmr/io/ChemCompIo.py rename to wwpdb/utils/nmr/io/ChemCompReader.py index 7ab879da..f8108e4a 100644 --- a/wwpdb/utils/nmr/io/ChemCompIo.py +++ b/wwpdb/utils/nmr/io/ChemCompReader.py @@ -1,5 +1,5 @@ ## -# File: ChemCompIo.py +# File: ChemCompReader.py # Date: 31-May-2010 John Westbrook # # Update: @@ -19,7 +19,7 @@ import sys import os -import traceback +import re from mmcif.io.PdbxReader import PdbxReader from typing import IO, List, Optional @@ -33,6 +33,9 @@ isReservedLigCode) +ccd_id_pattern = re.compile(r'(\w{1,3}|\w{5})') + + class ChemCompReader: """ Accessor methods chemical component definition data files """ @@ -134,7 +137,8 @@ def setCompId(self, compId: str) -> bool: """ Set chemical component definition data file path of the input chemical component """ - if compId in emptyValue: + if compId in emptyValue or not isinstance(compId, str)\ + or not ccd_id_pattern.match(compId) or isReservedLigCode(compId): return False self.__ccU = compId.upper() @@ -144,11 +148,8 @@ def setCompId(self, compId: str) -> bool: self.__filePath = os.path.join(self.__topCachePath, hashKey, self.__ccU, self.__ccU + '.cif') if not os.access(self.__filePath, os.R_OK): - - if self.__verbose and len(compId) in (3, 5) and compId[-1] not in ('+', '-')\ - and not isReservedLigCode(compId): + if self.__verbose: self.__lfh.write(f"+{self.__class_name__}.setCompId() ++ Error - Missing file {self.__filePath}\n") - return False return True @@ -159,7 +160,8 @@ def setFilePath(self, filePath: str, compId: Optional[str] = None) -> bool: try: - if compId in emptyValue: + if compId in emptyValue or not isinstance(compId, str)\ + or not ccd_id_pattern.match(compId) or not isReservedLigCode(compId): return False self.__ccU = str(compId).upper() @@ -167,20 +169,15 @@ def setFilePath(self, filePath: str, compId: Optional[str] = None) -> bool: self.__filePath = filePath if not os.access(self.__filePath, os.R_OK): - - if self.__verbose and len(compId) in (3, 5) and compId[-1] not in ('+', '-')\ - and not isReservedLigCode(compId): + if self.__verbose: self.__lfh.write(f"+{self.__class_name__}.setFilePath() ++ Error - Missing file {self.__filePath}\n") - return False return True except Exception as e: - if self.__verbose: self.__lfh.write(f"+{self.__class_name__}.setFilePath() ++ Error - Set {self.__filePath} failed {str(e)}\n") - return False def getAtomList(self) -> List[list]: @@ -226,8 +223,9 @@ def __getComp(self) -> bool: return self.__setDataBlock(block) - except Exception: - traceback.print_exc(file=sys.stdout) + except Exception as e: + if self.__verbose: + self.__lfh.write(f"+{self.__class_name__}.__getComp() ++ Error - {str(e)}\n") return False def __getDataBlock(self, filePath: str, blockId: Optional[str] = None): @@ -261,8 +259,9 @@ def __getDataBlock(self, filePath: str, blockId: Optional[str] = None): return None - except Exception: - traceback.print_exc(file=self.__lfh) + except Exception as e: + if self.__verbose: + self.__lfh.write(f"+{self.__class_name__}.__getDataBlock() ++ Error - {str(e)}\n") return None def __setDataBlock(self, dataBlock) -> bool: diff --git a/wwpdb/utils/nmr/mr/IsdMRParserListener.py b/wwpdb/utils/nmr/mr/IsdMRParserListener.py index fd587aa5..dea68e73 100644 --- a/wwpdb/utils/nmr/mr/IsdMRParserListener.py +++ b/wwpdb/utils/nmr/mr/IsdMRParserListener.py @@ -374,7 +374,7 @@ def __init__(self, verbose: bool = True, log: IO = sys.stdout, self.distRestraints = 0 # ISD: Distance restraints - self.atom_sele_pat = re.compile(r'([A-Z][0-9A-Z]{2})(\d+)([A-Z][A-Z0-9]*)') + self.atom_sele_pat = re.compile(r'([A-Z][0-9A-Z]{2})(\d+)([A-Z][0-9A-Z]*)') self.sfDict = {} diff --git a/wwpdb/utils/nmr/mr/SybylMRParserListener.py b/wwpdb/utils/nmr/mr/SybylMRParserListener.py index 53bd5e6b..f4921931 100644 --- a/wwpdb/utils/nmr/mr/SybylMRParserListener.py +++ b/wwpdb/utils/nmr/mr/SybylMRParserListener.py @@ -374,7 +374,7 @@ def __init__(self, verbose: bool = True, log: IO = sys.stdout, self.distRestraints = 0 # SYBYL: Distance restraints - self.atom_sele_pat = re.compile(r'([A-Z][0-9A-Z]{2})(\d+)\.([A-Z][A-Z0-9]*)') + self.atom_sele_pat = re.compile(r'([A-Z][0-9A-Z]{2})(\d+)\.([A-Z][0-9A-Z]*)') self.sfDict = {}