IMP logo
IMP Reference Guide  2.14.0
The Integrative Modeling Platform
utility.py
1 """@namespace IMP.EMageFit.utility
2  Utility functions.
3 """
4 
5 import os
6 import IMP
7 
8 def vararg_callback(option, opt_str, value, parser):
9  """
10  Snippet from Python website to process multiple values for
11  an option with OptionParser
12  """
13 
14  assert value is None
15  value = []
16 
17  def floatable(str):
18  try:
19  float(str)
20  return True
21  except ValueError:
22  return False
23 
24  for arg in parser.rargs:
25  # stop on --foo like options
26  if arg[:2] == "--" and len(arg) > 2:
27  break
28  # stop on -a, but not on -3 or -3.0
29  if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
30  break
31  value.append(arg)
32 
33  del parser.rargs[:len(value)]
34  setattr(parser.values, option.dest, value)
35 
36  return value
37 
38 
39 def get_experiment_params(fn_params):
40  """
41  Imports the configuration file
42  @param fn_params configuration file
43  @return Experiment Class with all the information from the config file
44  """
45  try:
46  import importlib.machinery
47  imp = None
48  except ImportError:
49  import imp
50  name, ext = os.path.splitext(fn_params)
51  if imp is None:
52  foo = importlib.machinery.SourceFileLoader(name,
53  fn_params).load_module()
54  else:
55  foo = imp.load_source(name, fn_params)
56  exp = foo.Experiment()
57  # convert to absolute paths
58  exp.fn_pdbs = [IMP.get_relative_path(fn_params, fn) for fn in exp.fn_pdbs]
59  if hasattr(exp, "sampling_positions"):
60  exp.sampling_positions.read = IMP.get_relative_path(
61  fn_params, exp.sampling_positions.read)
62  if hasattr(exp, "benchmark"):
63  if hasattr(exp.benchmark, "fn_pdb_native"):
64  exp.benchmark.fn_pdb_native = IMP.get_relative_path(
65  fn_params, exp.benchmark.fn_pdb_native)
66  if hasattr(exp.benchmark, "fn_pdbs_native"):
67  fns = []
68  for fn in exp.benchmark.fn_pdbs_native:
69  fns.append(IMP.get_relative_path(fn_params, fn))
70  exp.benchmark.fn_pdbs_native = fns
71 
72  if hasattr(exp, "dock_transforms"):
73  for i in range(len(exp.dock_transforms)):
74  exp.dock_transforms[i][2] = IMP.get_relative_path(
75  fn_params, exp.dock_transforms[i][2])
76  if hasattr(exp, "em2d_restraints"):
77  for i in range(len(exp.em2d_restraints)):
78  exp.em2d_restraints[i][1] = IMP.get_relative_path(
79  fn_params, exp.em2d_restraints[i][1])
80  return exp
def vararg_callback
Snippet from Python website to process multiple values for an option with OptionParser.
Definition: utility.py:8
def get_experiment_params
Imports the configuration file.
Definition: utility.py:39
std::string get_relative_path(std::string base, std::string relative)
Return a path to a file relative to another file.