From 811b444dc5005c151e0db5e790e7e5136e2d9f4e Mon Sep 17 00:00:00 2001 From: mimminou Date: Sat, 7 May 2022 03:52:39 +0100 Subject: [PATCH 1/2] Eliminate need to write temp file run_propka() in main.py now uses StringIO(), making it run entirely in memory and avoiding creating temp file on disk. --- pdb2pqr/main.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/pdb2pqr/main.py b/pdb2pqr/main.py index 9cd7332d..a0ddb51c 100644 --- a/pdb2pqr/main.py +++ b/pdb2pqr/main.py @@ -10,7 +10,7 @@ import argparse import sys from collections import OrderedDict -from tempfile import NamedTemporaryFile +from io import StringIO from pathlib import Path import propka.lib import propka.output as pk_out @@ -27,7 +27,6 @@ from .utilities import noninteger_charge from .config import VERSION, TITLE_STR, CITATIONS, FORCE_FIELDS from .config import REPAIR_LIMIT -import os _LOGGER = logging.getLogger(f"PDB2PQR{VERSION}") @@ -506,21 +505,18 @@ def run_propka(args, biomolecule): pKa information from PROPKA) :rtype: (list, str) """ - # TODO - eliminate need to write temporary file + lines = io.print_biomolecule_atoms( atomlist=biomolecule.atoms, chainflag=args.keep_chain, pdbfile=True ) - pdb_path = NamedTemporaryFile(suffix=".pdb", delete=True).name - with open(pdb_path, "w") as fpdb: - for line in lines: - fpdb.write(line) - - parameters = pk_in.read_parameter_file(args.parameters, Parameters()) - molecule = MolecularContainer(parameters, args) - molecule = pk_in.read_molecule_file(pdb_path, molecule) + pdb_path = "input.pdb" + with StringIO() as fpdb: + fpdb.writelines(lines) + parameters = pk_in.read_parameter_file(args.parameters, Parameters()) + molecule = MolecularContainer(parameters, args) + molecule = pk_in.read_molecule_file(pdb_path, molecule, fpdb) - os.remove(pdb_path) molecule.calculate_pka() From f47407fa5b602f98d3efd7467cc6b29aca881b47 Mon Sep 17 00:00:00 2001 From: mimminou Date: Sat, 7 May 2022 15:13:48 +0100 Subject: [PATCH 2/2] Eliminated need for temporary file in main.py replaced temp file in run_propka() with StringIO() --- pdb2pqr/main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pdb2pqr/main.py b/pdb2pqr/main.py index a0ddb51c..e8868010 100644 --- a/pdb2pqr/main.py +++ b/pdb2pqr/main.py @@ -510,12 +510,13 @@ def run_propka(args, biomolecule): atomlist=biomolecule.atoms, chainflag=args.keep_chain, pdbfile=True ) - pdb_path = "input.pdb" with StringIO() as fpdb: fpdb.writelines(lines) parameters = pk_in.read_parameter_file(args.parameters, Parameters()) molecule = MolecularContainer(parameters, args) - molecule = pk_in.read_molecule_file(pdb_path, molecule, fpdb) + # needs a mock name with .pdb extension to work with stream data, hence the "input.pdb" + molecule = pk_in.read_molecule_file("input.pdb", molecule, fpdb) + molecule.calculate_pka()