Commit cdcc1ba3 authored by Jacob Durrant's avatar Jacob Durrant

Now works seamlessly with both posix and windows paths.

parent ab212cd8
......@@ -12,6 +12,7 @@ Changes
5. Improved formatting (black) and docstrings.
6. Fixed minor bug that assigned atoms with names like "HG21" to element "HG"
rather than "H". No longer supporting element "HG".
7. Moved examples directory to `povme/examples/`.
2.0.3
-----
......
# POVME 2.1 is released under the GNU General Public License (see
# http://www.gnu.org/licenses/gpl.html). If you have any questions, comments,
# or suggestions, please don't hesitate to contact me, Jacob Durrant, at
# durrantj [at] pitt [dot] edu.
#
# If you use POVME in your work, please cite:
#
# Durrant, J. D., C. A. de Oliveira, et al. (2011). "POVME: An algorithm
# for measuring binding-pocket volumes." J Mol Graph Model 29(5): 773-776.
#
# Durrant, J. D., L. Votapka, et al. (2014). "Povme 2.0: An Enhanced Tool
# for Determining Pocket Shape and Volume Characteristics." J Chem Theory
# Comput 10.11: 5047-56.
from pathlib import Path
import gzip
import os
try:
from pathlib import WindowsPath as WinPath
t = WinPath()
except:
# This is all that's available on linux. But doesn't work with absolute
# windows paths. On the other hand, paths starting with "C:\" don't make
# sense on linux anyway.
from pathlib import PureWindowsPath as WinPath
def fix_filename(path, must_exist=True):
"""Gets the proper filename, regardless of whether windows or unix
formatted.
path -- A string, the path.
must_exist -- A boolean, whether to use file existance in determining which
path to use.
Returns:
A standardized version of the filename.
"""
if must_exist:
# Figure out path format based on whether the file exists.
path_fixed = Path(path)
if not path_fixed.exists():
# The path doesn't exist. Could it be a windows-formatted path?
path_fixed = Path(WinPath(path))
if not path_fixed.exists():
# No, it really doesn't exist.
raise IOError("File doesn't exist: " + path)
else:
# Try to figure it out without testing if file exists.
if not "/" in path and not "\\" in path:
# No directory, so it doesn't really matter.
path_fixed = Path(path)
elif os.path.dirname(path) == "":
# It must be a windows path.
path_fixed = Path(WinPath(path))
else:
# It's probably a unix path.
path_fixed = Path(path)
return path_fixed
def openfile(path, mode="r"):
"""Opens a file, regardless of whether unix or windows formatted path.
path -- A string, the path.
mode -- A string, the file mode. Defaults to "r".
Returns:
The file object.
"""
return open(fix_filename(path, "r" in mode), mode)
def gzopenfile(path, mode="r"):
"""Opens a gz compressed file, regardless of whether unix or windows
formatted path.
path -- A string, the path.
mode -- A string, the file mode. Defaults to "r".
Returns:
The file object.
"""
return gzip.open(fix_filename(path, "r" in mode), mode)
# f = openfile(".\common.py", "r")
# print(f.read().decode())
......@@ -85,7 +85,7 @@ SavePoints true # You can optionally
# not the pdb file.
# Load the PDB trajectory file
PDBFileName 4NSS.pdb # Load in the PDB trajectory file with
PDBFileName .\4NSS.pdb # Load in the PDB trajectory file with
# the pocket you want to analyze.
# Tell POVME how to identify points that are within the binding pocket.
......
......@@ -85,7 +85,7 @@ SavePoints true # You can optionally
# not the pdb file.
# Load the PDB trajectory file
PDBFileName 4NSS.pdb # Load in the PDB trajectory file with
PDBFileName ./4NSS.pdb # Load in the PDB trajectory file with
# the pocket you want to analyze.
# Tell POVME how to identify points that are within the binding pocket.
......
......@@ -14,6 +14,7 @@ import getopt
from numpy.lib.recfunctions import append_fields
import multiprocessing
import warnings
from .common import openfile
# POVME Pocket ID 1.0 is a program for identifying protein pockets and
# generating appropriate pocket-encompassing inclusion spheres. These spheres,
......@@ -115,7 +116,7 @@ class FileIO:
"""
# open/read the file
afile = open(filename, "r")
afile = openfile(filename, "r")
self.load_pdb_into_using_file_object(afile)
afile.close()
......@@ -1529,7 +1530,7 @@ def run_pocket_id(parameters):
for i, pts in enumerate(all_pockets):
filename = "pocket" + str(i + 1) + ".pdb"
printit("\tSaving " + filename + "...")
f = open(filename, "w")
f = openfile(filename, "w")
f.write("REMARK Pocket #" + str(i + 1) + "\n")
# do I need to whiten stuff here? not sure what whitening is.
......
......@@ -17,7 +17,6 @@ import sys
import time
import numpy
from . import pymolecule
import gzip
import os
import shutil
import random
......@@ -25,7 +24,7 @@ import multiprocessing
import platform
from functools import reduce
from .__init__ import __version__
from .common import openfile, gzopenfile, fix_filename
try:
from io import StringIO
......@@ -52,7 +51,7 @@ def log(astr, parameters):
# Save it to the output file as well.
try:
if parameters["CompressOutput"] == True:
f = gzip.open(parameters["OutputFilenamePrefix"] + "output.txt.gz", "ab")
f = gzopenfile(parameters["OutputFilenamePrefix"] + "output.txt.gz", "ab")
else:
f = open(parameters["OutputFilenamePrefix"] + "output.txt", "a")
......@@ -737,11 +736,11 @@ component "data" value 3"""
# 3. write the header and footer
if parameters["SaveVolumetricDensityMap"] == True:
if parameters["CompressOutput"] == True:
dx_file = gzip.open(
dx_file = gzopenfile(
parameters["OutputFilenamePrefix"] + "volumetric_density.dx.gz", "wb"
)
else:
dx_file = open(
dx_file = openfile(
parameters["OutputFilenamePrefix"] + "volumetric_density.dx", "w"
)
......@@ -1037,7 +1036,7 @@ class MultithreadingCalcVolumeTask(MultithreadingTaskGeneral):
frame_text = frame_text + "END\n"
if parameters["CompressOutput"] == True:
fl = gzip.open(
fl = gzopenfile(
parameters["OutputFilenamePrefix"]
+ "frame_"
+ str(frame_indx)
......@@ -1045,7 +1044,7 @@ class MultithreadingCalcVolumeTask(MultithreadingTaskGeneral):
"wb",
)
else:
fl = open(
fl = openfile(
parameters["OutputFilenamePrefix"]
+ "frame_"
+ str(frame_indx)
......@@ -1261,7 +1260,7 @@ class ConfigFile:
"""
f = open(filename, "r")
f = openfile(filename, "r")
lines = f.readlines()
f.close()
......@@ -1457,7 +1456,7 @@ class RunPOVME:
"SavePocketVolumesTrajectory",
]
int_parameters = ["NumFrames", "ContiguousPointsCriteria", "NumProcessors"]
string_parameters = [
filename_parameters = [
"OutputFilenamePrefix",
"PDBFileName",
"LoadPointsFilename",
......@@ -1487,8 +1486,10 @@ class RunPOVME:
pass
try:
index = [p.upper() for p in string_parameters].index(entity[0])
parameters[string_parameters[index]] = entity[1].strip()
index = [p.upper() for p in filename_parameters].index(entity[0])
parameters[filename_parameters[index]] = fix_filename(
entity[1].strip(), False
).as_posix() # so a string
except:
pass
......@@ -1634,14 +1635,15 @@ class RunPOVME:
points_filename = parameters["OutputFilenamePrefix"] + "point_field.pdb"
if parameters["CompressOutput"] == True:
afile = gzip.open(points_filename + ".gz", "wb")
afile = gzopenfile(points_filename + ".gz", "wb")
else:
afile = open(points_filename, "w")
afile = openfile(points_filename, "w")
afile.write(numpy_to_pdb(pts, "X").encode())
afile.close()
# save the points as npy
numpy.save(points_filename + ".npy", pts)
log(
......@@ -1681,9 +1683,9 @@ class RunPOVME:
)
if parameters["CompressOutput"] == True:
afile = gzip.open(points_filename + ".gz", "wb")
afile = gzopenfile(points_filename + ".gz", "wb")
else:
afile = open(points_filename, "w")
afile = openfile(points_filename, "w")
afile.write(numpy_to_pdb(contig_pts, "X").encode())
afile.close()
......@@ -1747,12 +1749,12 @@ class RunPOVME:
# if the user requested a separate volume file, save that as well
if parameters["SaveTabbedVolumeFile"] == True:
if parameters["CompressOutput"] == True:
f = gzip.open(
f = gzopenfile(
parameters["OutputFilenamePrefix"] + "volumes.tabbed.txt.gz",
"wb",
)
else:
f = open(
f = openfile(
parameters["OutputFilenamePrefix"] + "volumes.tabbed.txt", "w"
)
......@@ -1764,19 +1766,19 @@ class RunPOVME:
# volumes, generate that here.
if parameters["SavePocketVolumesTrajectory"] == True:
if parameters["CompressOutput"] == True:
traj_file = gzip.open(
traj_file = gzopenfile(
parameters["OutputFilenamePrefix"] + "volume_trajectory.pdb.gz",
"wb",
)
else:
traj_file = open(
traj_file = openfile(
parameters["OutputFilenamePrefix"] + "volume_trajectory.pdb",
"w",
)
for frame_index in range(1, len(list(results_dic.keys())) + 1):
if parameters["CompressOutput"] == True:
frame_file = gzip.open(
frame_file = gzopenfile(
parameters["OutputFilenamePrefix"]
+ "frame_"
+ str(frame_index)
......@@ -1784,7 +1786,7 @@ class RunPOVME:
"rb",
)
else:
frame_file = open(
frame_file = openfile(
parameters["OutputFilenamePrefix"]
+ "frame_"
+ str(frame_index)
......
......@@ -36,6 +36,7 @@ import copy
import sys
import pickle as pickle
import shutil
from .common import openfile
version = "beta"
......@@ -917,7 +918,7 @@ class FileIO:
# first, get the files that must exist
self.parent_molecule.information.atom_information = pickle.load(
open(filename + "atom_information", "rb")
openfile(filename + "atom_information", "rb")
)
self.parent_molecule.information.coordinates = numpy.load(
filename + "coordinates.npz"
......@@ -926,15 +927,15 @@ class FileIO:
# now look for other possible files (optional output)
if os.path.exists(filename + "remarks"):
self.parent_molecule.information.remarks = pickle.load(
open(filename + "remarks", "rb")
openfile(filename + "remarks", "rb")
)
if os.path.exists(filename + "hierarchy"):
self.parent_molecule.information.hierarchy = pickle.load(
open(filename + "hierarchy", "rb")
openfile(filename + "hierarchy", "rb")
)
if os.path.exists(filename + "filename"):
self.parent_molecule.information.filename = pickle.load(
open(filename + "filename", "rb")
openfile(filename + "filename", "rb")
)
if os.path.exists(filename + "bonds.npz"):
self.parent_molecule.information.bonds = numpy.load(filename + "bonds.npz")[
......@@ -945,7 +946,7 @@ class FileIO:
filename + "coordinates_undo_point.npz"
)["arr_0"]
# self.parent_molecule.information = pickle.load( open( filename, "rb" ) )
# self.parent_molecule.information = pickle.load( openfile( filename, "rb" ) )
def load_pdb_into(
self,
......@@ -972,7 +973,7 @@ class FileIO:
self.parent_molecule.information.filename = filename
# open/read the file
afile = open(filename, "r")
afile = openfile(filename, "r")
self.load_pdb_into_using_file_object(
afile, bonds_by_distance, serial_reindex, resseq_reindex
)
......@@ -1236,19 +1237,19 @@ class FileIO:
# be pickled.
pickle.dump(
self.parent_molecule.information.hierarchy,
open(filename + "hierarchy", "wb"),
openfile(filename + "hierarchy", "wb"),
-1,
)
if save_remarks == True:
pickle.dump(
self.parent_molecule.information.remarks,
open(filename + "remarks", "wb"),
openfile(filename + "remarks", "wb"),
-1,
) # using the latest protocol
if save_filename == True:
pickle.dump(
self.parent_molecule.information.filename,
open(filename + "filename", "wb"),
openfile(filename + "filename", "wb"),
-1,
)
......@@ -1259,7 +1260,7 @@ class FileIO:
# protocol parameter, so let's just use cPickle directly
pickle.dump(
self.parent_molecule.information.atom_information,
open(filename + "atom_information", "wb"),
openfile(filename + "atom_information", "wb"),
-1,
)
......@@ -1312,7 +1313,7 @@ class FileIO:
self.resseq_reindex()
if return_text == False:
afile = open(filename, "w")
afile = openfile(filename, "w")
else:
return_string = ""
......
# POVME 2.1 is released under the GNU General Public License (see
# http://www.gnu.org/licenses/gpl.html). If you have any questions, comments,
# or suggestions, please don't hesitate to contact me, Jacob Durrant, at
# durrantj [at] pitt [dot] edu.
#
# If you use POVME in your work, please cite:
#
# Durrant, J. D., C. A. de Oliveira, et al. (2011). "POVME: An algorithm
# for measuring binding-pocket volumes." J Mol Graph Model 29(5): 773-776.
#
# Durrant, J. D., L. Votapka, et al. (2014). "Povme 2.0: An Enhanced Tool
# for Determining Pocket Shape and Volume Characteristics." J Chem Theory
# Comput 10.11: 5047-56.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment