Skip to content

Commit 602c23b

Browse files
committed
minor refactoring
1 parent 08e0ee8 commit 602c23b

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/prodigy_prot/modules/parsers.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,32 @@
44

55
import logging
66
import sys
7+
import warnings
78
from pathlib import Path
89
from typing import Optional, Union
910

1011
from Bio.PDB.Chain import Chain
1112
from Bio.PDB.MMCIFParser import MMCIFParser
13+
from Bio.PDB.PDBExceptions import PDBConstructionWarning
1214
from Bio.PDB.PDBParser import PDBParser
1315
from Bio.PDB.Polypeptide import PPBuilder, is_aa
1416
from Bio.PDB.Structure import Structure
1517

18+
warnings.filterwarnings("ignore", category=PDBConstructionWarning)
1619
log = logging.getLogger("Prodigy")
1720

1821

22+
def get_parser(input_f: Path) -> Union[PDBParser, MMCIFParser]:
23+
if input_f.suffix == ".cif":
24+
return MMCIFParser()
25+
else:
26+
return PDBParser()
27+
28+
1929
def validate_structure(
2030
s: Structure, selection: Optional[list[str]] = None, clean: bool = True
2131
) -> Structure:
32+
2233
# setup logging
2334
logger = logging.getLogger("Prodigy")
2435

@@ -132,29 +143,24 @@ def parse_structure(path: str) -> tuple[Structure, int, int]:
132143
)
133144
sys.exit(1)
134145

135-
parser: Union[MMCIFParser, PDBParser]
136-
if extension == ".cif":
137-
parser = MMCIFParser()
138-
else:
139-
parser = PDBParser()
140-
146+
parser = get_parser(Path(path))
141147
structure_name = Path(path).stem
142148
structure_path = Path(path)
143149
try:
144-
structure = parser.get_structure(structure_name, structure_path)
150+
original_structure = parser.get_structure(structure_name, structure_path)
145151
except Exception as e:
146152
log.exception(e)
147153
sys.exit(1)
148154

149-
assert isinstance(structure, Structure)
155+
assert isinstance(original_structure, Structure)
150156

151-
# Validate the structure
152-
validated_structure = validate_structure(structure)
157+
structure = validate_structure(original_structure)
153158

154159
# Get number of chains
155-
number_of_chains = len(set([c.id for c in validated_structure.get_chains()]))
160+
number_of_chains = len(set([c.id for c in structure.get_chains()]))
156161

157162
# Get number of residues
158-
number_of_residues = len(list(validated_structure.get_residues()))
163+
number_of_residues = len(list(structure.get_residues()))
159164

160-
return (validated_structure, number_of_chains, number_of_residues)
165+
# structure, n_chains, n_res = parse_structure(path=str(struct_path))
166+
return (structure, number_of_chains, number_of_residues)

0 commit comments

Comments
 (0)