def run_pdb2pqr(pdblines): """ Runs PDB2PQR on the input """ oldsyspath = sys.path pqrhandle, pqrfile = tempfile.mkstemp() tmphandle, tmpfile = tempfile.mkstemp() tmpf = open(tmpfile, "w") err = "/tmp/pdb2pqr.err" for l in pdblines: if l.find("XXX") == -1: print(l, file=tmpf) tmpf.close() try: import pdb2pqr args = [pdb2pqr.__file__, "--ff=AMBER", tmpfile, pqrfile] #if pdb2pqr.PACKAGE_PATH != "": # sys.path.extend(pdb2pqr.PACKAGE_PATH.split(":")) oldstdout = sys.stdout sys.stdout = sys.stderr oldargv = list(sys.argv) sys.argv[:] = args try: pdb2pqr.mainCommand(args) except ValueError as exc: msg = exc.args[0] if msg.startswith("This PDB file is missing too many"): raise FixError(msg) else: raise finally: sys.argv[:] = oldargv sys.stdout = oldstdout pqr = os.fdopen(pqrhandle) pqrlines = pqr.readlines() pqr.close() except: os.system("pdb2pqr --ff=AMBER %s %s 2> %s" % (tmpfile, pqrfile, err)) os.system("cat %s" % err) pqrlines = list(open(pqrfile)) finally: os.remove(tmpfile) os.remove(pqrfile) result = [] repl = ( (" H ", " HN "), (" H ", " HT1"), (" H2 ", " HT2"), (" H3 ", " HT3"), ) for l in pqrlines: result.append(l) atom2 = l[12:16] for pin, pout in repl: if atom2 == pin: p = l[:12] + pout + l[16:] result.append(p) return result
def _get_pqr(self): """Run PDB2PQR to get charge for each atom TODO: figure out why unknown residues fro hetatms are absent """ if self.pqr is None: pdbfd, tmp_pdb_path = tempfile.mkstemp() with os.fdopen(pdbfd, 'w') as tmp: # writer.set_structure(self.structure) # writer.save(tmp) # tmp.seek(0) self.save_pdb(tmp, True) _, tmp_pqr_path = tempfile.mkstemp() with silence_stdout(), silence_stderr(): mainCommand(["pdb2pqr.py", "--ff=amber", "--whitespace", tmp_pdb_path, tmp_pqr_path]) os.remove(tmp_pdb_path) self.pqr = {} with open(tmp_pqr_path) as pqr: for line in pqr: for line in pqr: if line.startswith("REMARK"): continue fields = line.rstrip().split() if len(fields) == 11: recordName, serial, atomName, residueName, chainID, residueNumber, X, Y, Z, charge, radius = fields elif len(fields) == 10: recordName, serial, atomName, residueName, residueNumber, X, Y, Z, charge, radius = fields else: print len(fields) raise RuntimeError("Invalid PQR File") resseq = int("".join([i for i in residueNumber if i.isdigit()])) icode = "".join([i for i in residueNumber if not i.isdigit()]) if icode == "": icode = " " if recordName == "HETATM": # hetero atom flag if residueName in ["HOH", "WAT"]: hetero_flag = "W" else: hetero_flag = "H_{}".format(residueName) else: hetero_flag = " " residue_id = (hetero_flag, resseq, icode) key = resseq #(residue_id, (atomName.strip(), ' ')) self.pqr[key] = float(charge) os.remove(tmp_pqr_path) return self.pqr
def get_pqr(pdb, sdie="78.5400", save_path="./"): sys.path.insert(0, VISUALDEP_CONFIG["pdb2pqr_path"]) import pdb2pqr pqr = os.path.join(save_path, pdb.split(os.sep)[-1][:-4] + ".pqr") l = "./ --ff AMBER -v --whitespace --chain %s %s" % (pdb, pqr) sys.argv = l.split() pdb2pqr.mainCommand(sys.argv) return pqr
def run_pdb2pqr(pdblines): """ Runs PDB2PQR on the input """ import pdb2pqr oldsyspath = sys.path pqrhandle, pqrfile = tempfile.mkstemp() tmphandle, tmpfile = tempfile.mkstemp() tmpf = open(tmpfile, "w") for l in pdblines: if l.find("XXX") == -1: print >> tmpf, l tmpf.close() try: args = [pdb2pqr.__file__, "--ff=charmm", tmpfile, pqrfile] #if pdb2pqr.PACKAGE_PATH != "": # sys.path.extend(pdb2pqr.PACKAGE_PATH.split(":")) oldstdout = sys.stdout sys.stdout = sys.stderr oldargv = list(sys.argv) sys.argv[:] = args pdb2pqr.mainCommand(args) sys.argv[:] = oldargv sys.stdout = oldstdout pqr = os.fdopen(pqrhandle) pqrlines = pqr.readlines() pqr.close() finally: os.remove(tmpfile) os.remove(pqrfile) result = [] repl = ( (" H ", " HN "), (" H ", " HT1"), (" H2 ", " HT2"), (" H3 ", " HT3"), ) for l in pqrlines: result.append(l) atom2 = l[12:16] for pin, pout in repl: if atom2 == pin: p = l[:12] + pout + l[16:] result.append(p) return result
# -*- coding: utf-8 -*- import re import sys from pdb2pqr import mainCommand if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit(mainCommand())