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