Skip to content

Commit

Permalink
Changes to work on Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
peastman committed Mar 13, 2014
1 parent 16e09ec commit 111fd4c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
6 changes: 3 additions & 3 deletions pdbfixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __init__(self, structure):
templatesPath = os.path.join(os.path.dirname(__file__), 'templates')
for file in os.listdir(templatesPath):
templatePdb = app.PDBFile(os.path.join(templatesPath, file))
name = templatePdb.topology.residues().next().name
name = next(templatePdb.topology.residues()).name
self.templates[name] = templatePdb

def _addAtomsToTopology(self, heavyAtomsOnly, omitUnknownMolecules):
Expand Down Expand Up @@ -256,7 +256,7 @@ def _addMissingResiduesToChain(self, chain, residueNames, startPosition, endPosi
newResidue = chain.topology.addResidue(residueName, chain)
translate = startPosition+(endPosition-startPosition)*(i+1.0)/(len(residueNames)+1.0)
templateAtoms = list(template.topology.atoms())
if newResidue == chain.residues().next():
if newResidue == next(chain.residues()):
templateAtoms = [atom for atom in templateAtoms if atom.name not in ('P', 'OP1', 'OP2')]
for atom in templateAtoms:
newAtom = chain.topology.addAtom(atom.name, atom.element, newResidue)
Expand Down Expand Up @@ -443,7 +443,7 @@ def addMissingAtoms(self):

# Set any previously existing atoms to be massless, they so won't move.

for atom in existingAtomMap.itervalues():
for atom in existingAtomMap.values():
system.setParticleMass(atom.index, 0.0)

# If any heavy atoms were omitted, add them back to avoid steric clashes.
Expand Down
19 changes: 12 additions & 7 deletions ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import uiserver
import webbrowser
import os.path
import urllib2
import gzip
from cStringIO import StringIO
from io import BytesIO
try:
from urllib.request import urlopen
from io import StringIO
except:
from urllib2 import urlopen
from cStringIO import StringIO

def loadHtmlFile(name):
htmlPath = os.path.join(os.path.dirname(__file__), 'html')
Expand All @@ -25,16 +30,16 @@ def startPageCallback(parameters, handler):
global fixer
if 'type' in parameters:
if parameters.getfirst('type') == 'local':
pdb = PdbStructure(parameters['pdbfile'].value.splitlines())
pdb = PdbStructure(parameters['pdbfile'].value.decode().splitlines())
fixer = PDBFixer(pdb)
displayDeleteChainsPage()
else:
id = parameters.getfirst('pdbid')
url = "ftp://ftp.wwpdb.org/pub/pdb/data/structures/all/pdb/pdb"+id.lower()+".ent.gz"
try:
response = urllib2.urlopen(url)
content = gzip.GzipFile(fileobj=StringIO(response.read())).read()
pdb = PdbStructure(content.splitlines())
response = urlopen(url)
content = gzip.GzipFile(fileobj=BytesIO(response.read())).read()
pdb = PdbStructure(content.decode().splitlines())
fixer = PDBFixer(pdb)
displayDeleteChainsPage()
except:
Expand Down Expand Up @@ -160,7 +165,7 @@ def displayConvertResiduesPage():
def displayMissingAtomsPage():
uiserver.setCallback(missingAtomsPageCallback)
fixer.findMissingAtoms()
allResidues = list(set(fixer.missingAtoms.iterkeys()).union(fixer.missingTerminals.iterkeys()))
allResidues = list(set(fixer.missingAtoms.keys()).union(fixer.missingTerminals.keys()))
allResidues.sort(key=lambda x: x.index)
if len(allResidues) == 0:
fixer.addMissingAtoms()
Expand Down
16 changes: 13 additions & 3 deletions uiserver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from threading import Thread
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from urlparse import parse_qs
import cgi
import sys
try:
from socketserver import ThreadingMixIn
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import parse_qs
except:
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from urlparse import parse_qs

class _Handler(BaseHTTPRequestHandler):
def do_GET(self):
Expand Down Expand Up @@ -40,6 +46,8 @@ def sendResponse(self, response, type="text/html"):
self.send_header("Content-type", type)
self.send_header("Content-length", str(len(response)))
self.end_headers()
if sys.version_info.major > 2:
response = bytes(response, 'UTF-8')
self.wfile.write(response)

def sendDownload(self, download, filename):
Expand All @@ -49,6 +57,8 @@ def sendDownload(self, download, filename):
self.send_header("Content-length", str(len(download)))
self.send_header("Content-Disposition", 'attachment; filename="%s"' % filename)
self.end_headers()
if sys.version_info.major > 2:
download = bytes(download, 'UTF-8')
self.wfile.write(download)

class _ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
Expand Down

0 comments on commit 111fd4c

Please sign in to comment.