add logging

This commit is contained in:
2024-03-07 14:17:57 +08:00
parent 63eb5384f7
commit 74b31db5a5

View File

@@ -30,7 +30,8 @@ from copy import deepcopy
from pymol import cmd from pymol import cmd
from pymol import CmdException from pymol import CmdException
import pymol import pymol
import os import logging
from logging.handlers import RotatingFileHandler
# 使用 BioPython 导入氨基酸缩写 # 使用 BioPython 导入氨基酸缩写
AMINO_ACIDS = set(IUPACData.protein_letters) AMINO_ACIDS = set(IUPACData.protein_letters)
@@ -101,19 +102,32 @@ class PDBAnalyzer:
biodf: PandasPdb = field(init=False) biodf: PandasPdb = field(init=False)
protein_state: str = field(init=False) # Apo or Holo protein_state: str = field(init=False) # Apo or Holo
chain_id_list: List[str] = field(init=False) chain_id_list: List[str] = field(init=False)
logger: logging.Logger = field(init=False)
def __post_init__(self): def __post_init__(self):
""" """
Initialize the PDB structure after the object is created. Initialize the PDB structure after the object is created.
""" """
self.initialize_properties() self._initialize_logging()
self._initialize_structure()
self._initialize_properties()
def _initialize_logging(self):
self.logger = logging.getLogger(f"PDBAnalyzer_{self.pdb_file.stem}")
self.logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler(f"{self.pdb_file.stem}.log", maxBytes=1048576, backupCount=5)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
self.logger.addHandler(handler)
def _initialize_structure(self):
self.structure = PDBParser(QUIET=True).get_structure('PDB_structure', self.pdb_file.as_posix())
self.protein_structure = self.structure
def initialize_properties(self): def _initialize_properties(self):
"""Initialize properties based on the pdb_file.""" """Initialize properties based on the pdb_file."""
self.pdb_file_stem = self.pdb_file.stem.split('.')[0] self.pdb_file_stem = self.pdb_file.stem.split('.')[0]
self.pid = self.pdb_file_stem.lower() if len(self.pdb_file_stem) == 4 else None self.pid = self.pdb_file_stem.lower() if len(self.pdb_file_stem) == 4 else None
self.structure = PDBParser(QUIET=True).get_structure('PDB_structure', self.pdb_file.as_posix())
self.protein_structure = PDBParser(QUIET=True).get_structure('PDB_structure', self.pdb_file.as_posix())
self.biodata = PandasPdb().read_pdb(self.pdb_file.as_posix()) self.biodata = PandasPdb().read_pdb(self.pdb_file.as_posix())
self.protein_state = 'Holo' if 'HETATM' in self.biodata.df.keys() else 'Apo' self.protein_state = 'Holo' if 'HETATM' in self.biodata.df.keys() else 'Apo'
self.chain_id_list = self.biodata.df['ATOM']['chain_id'].drop_duplicates().to_list() self.chain_id_list = self.biodata.df['ATOM']['chain_id'].drop_duplicates().to_list()