Skip to content

Commit

Permalink
Fix AttributeError exception of isReservedLigCode() and rename io/Che…
Browse files Browse the repository at this point in the history
…mCompIo.py -> io/ChemCompReader.py
  • Loading branch information
yokochi47 committed Jan 17, 2025
1 parent 25d0172 commit d9181e9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion wwpdb/utils/nmr/AlignUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 8 additions & 5 deletions wwpdb/utils/nmr/ChemCompUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
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,
isReservedLigCode)
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,
Expand All @@ -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', ''),
Expand Down Expand Up @@ -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', ''),
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion wwpdb/utils/nmr/NmrDpUtility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {'#', '!'}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##
# File: ChemCompIo.py
# File: ChemCompReader.py
# Date: 31-May-2010 John Westbrook
#
# Update:
Expand All @@ -19,7 +19,7 @@

import sys
import os
import traceback
import re

from mmcif.io.PdbxReader import PdbxReader
from typing import IO, List, Optional
Expand All @@ -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
"""
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -159,28 +160,24 @@ 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()

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]:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion wwpdb/utils/nmr/mr/IsdMRParserListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down
2 changes: 1 addition & 1 deletion wwpdb/utils/nmr/mr/SybylMRParserListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down

0 comments on commit d9181e9

Please sign in to comment.