use pkg_resources to access bundled files

* egg file can remain zipped
* see http://peak.telecommunity.com/DevCenter/PythonEggs#accessing-package-resources
  for details
This commit is contained in:
Oliver Beckstein
2013-07-25 11:28:56 -07:00
parent b928c18bab
commit 7008ed9f24
4 changed files with 31 additions and 30 deletions

View File

@@ -4,6 +4,7 @@ from __future__ import print_function
import pickle,sys,os,math,propka.calculations import pickle,sys,os,math,propka.calculations
import pkg_resources
class bondmaker: class bondmaker:
def __init__(self): def __init__(self):
@@ -26,8 +27,7 @@ class bondmaker:
self.max_sq_distance = max(list(self.distances_squared.values())+[self.default_dist_squared]) self.max_sq_distance = max(list(self.distances_squared.values())+[self.default_dist_squared])
# protein bonding data # protein bonding data
path = os.path.split(__file__)[0] self.data_file_name = pkg_resources.resource_filename(__name__, 'protein_bonds.dat')
self.data_file_name = os.path.join(path,'protein_bonds.dat')
data = open(self.data_file_name,'rb') data = open(self.data_file_name,'rb')
self.protein_bonds = pickle.load(data) self.protein_bonds = pickle.load(data)

View File

@@ -1,26 +1,27 @@
#!/usr/bin/python
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
import string, sys, copy, math, os import string, sys, copy, math, os
import pkg_resources
# #
# file I/O # file I/O
# #
def open_file_for_reading(filename): def open_file_for_reading(filename):
if not os.path.isfile(filename): try:
f = open(filename,'r')
except:
raise Exception('Cannot find file %s' %filename) raise Exception('Cannot find file %s' %filename)
return f
return open(filename,'r')
def open_file_for_writing(filename): def open_file_for_writing(filename):
res = open(filename,'w') try:
if not res: res = open(filename,'w')
except:
raise Exception('Could not open %s'%filename) raise Exception('Could not open %s'%filename)
return res return res
# #
# bookkeeping etc. # bookkeeping etc.
# #
@@ -46,7 +47,7 @@ def make_molecule(atom, atoms):
if ba in atoms: if ba in atoms:
atoms.remove(ba) atoms.remove(ba)
res_atoms.extend(make_molecule(ba, atoms)) res_atoms.extend(make_molecule(ba, atoms))
return res_atoms return res_atoms
@@ -64,7 +65,7 @@ def generate_combinations(interactions):
res.remove([]) res.remove([])
return res return res
def make_combination(combis, interaction): def make_combination(combis, interaction):
res = [] res = []
@@ -88,37 +89,37 @@ def loadOptions():
parser = OptionParser(usage) parser = OptionParser(usage)
# loading the parser # loading the parser
parser.add_option("-f", "--file", action="append", dest="filenames", parser.add_option("-f", "--file", action="append", dest="filenames",
help="read data from <filename>, i.e. <filename> is added to arguments") help="read data from <filename>, i.e. <filename> is added to arguments")
parser.add_option("-r", "--reference", dest="reference", default="neutral", parser.add_option("-r", "--reference", dest="reference", default="neutral",
help="setting which reference to use for stability calculations [neutral/low-pH]") help="setting which reference to use for stability calculations [neutral/low-pH]")
parser.add_option("-c", "--chain", action="append", dest="chains", parser.add_option("-c", "--chain", action="append", dest="chains",
help="creating the protein with only a specified chain, note, chains without ID are labeled 'A' [all]") help="creating the protein with only a specified chain, note, chains without ID are labeled 'A' [all]")
parser.add_option("-t", "--thermophile", action="append", dest="thermophiles", parser.add_option("-t", "--thermophile", action="append", dest="thermophiles",
help="defining a thermophile filename; usually used in 'alignment-mutations'") help="defining a thermophile filename; usually used in 'alignment-mutations'")
parser.add_option("-a", "--alignment", action="append", dest="alignment", parser.add_option("-a", "--alignment", action="append", dest="alignment",
help="alignment file connecting <filename> and <thermophile> [<thermophile>.pir]") help="alignment file connecting <filename> and <thermophile> [<thermophile>.pir]")
parser.add_option("-m", "--mutation", action="append", dest="mutations", parser.add_option("-m", "--mutation", action="append", dest="mutations",
help="specifying mutation labels which is used to modify <filename> according to, e.g. N25R/N181D") help="specifying mutation labels which is used to modify <filename> according to, e.g. N25R/N181D")
parser.add_option("-v", "--version", dest="version_label", default="Jan15", parser.add_option("-v", "--version", dest="version_label", default="Jan15",
help="specifying the sub-version of propka [Jan15/Dec19]") help="specifying the sub-version of propka [Jan15/Dec19]")
parser.add_option("-p", "--parameters",dest="parameters", default="propka.cfg", parser.add_option("-p", "--parameters",dest="parameters", default=pkg_resources.resource_filename(__name__, "propka.cfg"),
help="set the parameter file") help="set the parameter file [%default]")
parser.add_option("-z", "--verbose", dest="verbose", action="store_true", default=True, parser.add_option("-z", "--verbose", dest="verbose", action="store_true", default=True,
help="sleep during calculations") help="sleep during calculations")
parser.add_option("-q", "--quiet", dest="verbose", action="store_false", parser.add_option("-q", "--quiet", dest="verbose", action="store_false",
help="sleep during calculations") help="sleep during calculations")
parser.add_option("-s", "--silent", dest="verbose", action="store_false", parser.add_option("-s", "--silent", dest="verbose", action="store_false",
help="not activated yet") help="not activated yet")
parser.add_option("--verbosity", dest="verbosity", action="store_const", parser.add_option("--verbosity", dest="verbosity", action="store_const",
help="level of printout - not activated yet") help="level of printout - not activated yet")
parser.add_option("-o", "--pH", dest="pH", type="float", default=7.0, parser.add_option("-o", "--pH", dest="pH", type="float", default=7.0,
help="setting pH-value used in e.g. stability calculations [7.0]") help="setting pH-value used in e.g. stability calculations [7.0]")
parser.add_option("-w", "--window", dest="window", nargs=3, type="float", default=(0.0, 14.0, 1.0), parser.add_option("-w", "--window", dest="window", nargs=3, type="float", default=(0.0, 14.0, 1.0),
help="setting the pH-window to show e.g. stability profiles [0.0, 14.0, 1.0]") help="setting the pH-window to show e.g. stability profiles [0.0, 14.0, 1.0]")
parser.add_option("-g", "--grid", dest="grid", nargs=3, type="float", default=(0.0, 14.0, 0.1), parser.add_option("-g", "--grid", dest="grid", nargs=3, type="float", default=(0.0, 14.0, 0.1),
help="setting the pH-grid to calculate e.g. stability related properties [0.0, 14.0, 0.1]") help="setting the pH-grid to calculate e.g. stability related properties [0.0, 14.0, 0.1]")
parser.add_option("--mutator", dest="mutator", parser.add_option("--mutator", dest="mutator",
help="setting approach for mutating <filename> [alignment/scwrl/jackal]") help="setting approach for mutating <filename> [alignment/scwrl/jackal]")
parser.add_option("--mutator-option", dest="mutator_options", action="append", parser.add_option("--mutator-option", dest="mutator_options", action="append",
help="setting property for mutator [e.g. type=\"side-chain\"]") help="setting property for mutator [e.g. type=\"side-chain\"]")
@@ -159,7 +160,7 @@ def makeTidyAtomLabel(name,element):
""" """
Returns a 'tidier' atom label for printing the new pdbfile Returns a 'tidier' atom label for printing the new pdbfile
""" """
if len(name)>4:# if longer than 4, just truncate the name if len(name)>4:# if longer than 4, just truncate the name
label=name[0:4] label=name[0:4]
elif len(name)==4:# if lenght is 4, otherwise use the name as it is elif len(name)==4:# if lenght is 4, otherwise use the name as it is

View File

@@ -6,6 +6,7 @@ import math
import propka.lib as lib import propka.lib as lib
import sys, os import sys, os
import pkg_resources
# names and types of all key words in configuration file # names and types of all key words in configuration file
matrices = ['interaction_matrix'] matrices = ['interaction_matrix']
@@ -57,8 +58,7 @@ class Parameters:
def read_parameters(self, file): def read_parameters(self, file):
# try to locate the parameters file # try to locate the parameters file
try: try:
path = os.path.dirname(__file__) ifile = pkg_resources.resource_filename(__name__, file)
ifile = os.path.join(path,'../'+file)
input = lib.open_file_for_reading(ifile) input = lib.open_file_for_reading(ifile)
except: except:
input = lib.open_file_for_reading(file) input = lib.open_file_for_reading(file)

View File

@@ -47,5 +47,5 @@ using the tutorial http://propka.ki.ku.dk/~luca/wiki/index.php/PROPKA_3.1_Tutori
'propka31 = propka.run:main', 'propka31 = propka.run:main',
], ],
}, },
zip_safe=False, zip_safe=True,
) )