IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/21
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  import importlib.machinery
47  name, ext = os.path.splitext(fn_params)
48  foo = importlib.machinery.SourceFileLoader(name,
49  fn_params).load_module()
50  exp = foo.Experiment()
51  # convert to absolute paths
52  exp.fn_pdbs = [IMP.get_relative_path(fn_params, fn) for fn in exp.fn_pdbs]
53  if hasattr(exp, "sampling_positions"):
54  exp.sampling_positions.read = IMP.get_relative_path(
55  fn_params, exp.sampling_positions.read)
56  if hasattr(exp, "benchmark"):
57  if hasattr(exp.benchmark, "fn_pdb_native"):
58  exp.benchmark.fn_pdb_native = IMP.get_relative_path(
59  fn_params, exp.benchmark.fn_pdb_native)
60  if hasattr(exp.benchmark, "fn_pdbs_native"):
61  fns = []
62  for fn in exp.benchmark.fn_pdbs_native:
63  fns.append(IMP.get_relative_path(fn_params, fn))
64  exp.benchmark.fn_pdbs_native = fns
65 
66  if hasattr(exp, "dock_transforms"):
67  for i in range(len(exp.dock_transforms)):
68  exp.dock_transforms[i][2] = IMP.get_relative_path(
69  fn_params, exp.dock_transforms[i][2])
70  if hasattr(exp, "em2d_restraints"):
71  for i in range(len(exp.em2d_restraints)):
72  exp.em2d_restraints[i][1] = IMP.get_relative_path(
73  fn_params, exp.em2d_restraints[i][1])
74  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.