IMP logo
IMP Reference Guide  2.20.0
The Integrative Modeling Platform
process_output_rmf.py
1 #! /usr/bin/env python
2 
3 
4 from __future__ import print_function
5 import argparse
6 
7 import IMP
8 import IMP.rmf
9 import RMF
10 import difflib
11 
12 
13 parser = argparse.ArgumentParser(
14  description='Process output data file saved as rmfs. It has two modality: '
15  'print selected fields for all lines or print a particular line '
16  'where a filed has a given value.')
17 parser.add_argument(
18  '-f',
19  action="store",
20  dest="filename",
21  help="file name to process")
22 parser.add_argument(
23  '-s',
24  dest="fields",
25  nargs="+",
26  help="Specify all fields to be printed. Multiple flags will append a "
27  "list of fields to be printed")
28 parser.add_argument(
29  '-p',
30  action="store_true",
31  dest="print_fields",
32  default=False,
33  help="print the fields contained in the file")
34 parser.add_argument(
35  '-n',
36  action="store",
37  dest="nframe",
38  default=None,
39  help="Print frame number n")
40 
41 result = parser.parse_args()
42 
43 
44 # open the file
45 if result.filename is not None:
46  try:
47  # let's see if that is an rmf file
48  rh = RMF.open_rmf_file_read_only(result.filename)
49  cat = rh.get_category('stat')
50  rmf_klist = rh.get_keys(cat)
51  rmf_names_keys = dict([(rh.get_name(k), k) for k in rmf_klist])
52  klist = rmf_names_keys.keys()
53  del rh
54  except IOError:
55  raise IOError("Not an RMF file")
56 else:
57  raise ValueError("No file name provided. Use -h for help")
58 
59 
60 # print the keys
61 if result.print_fields:
62  for key in klist:
63  print(key)
64 
65 
66 # the field string matching is by default strict, i.e., the input string
67 # must be the same as the one in the file
68 match_strictness = 1.0
69 
70 # print the queried fields
71 if result.fields is not None:
72  field_list = []
73  # check whether the fields exist and convert them to best matching existing
74  # field names
75  for field in result.fields:
76  found_entries = difflib.get_close_matches(
77  field,
78  klist,
79  1,
80  match_strictness)
81  if len(found_entries) == 0:
82  raise ValueError("field " + field + " non found")
83  else:
84  field_list.append(found_entries[0])
85 
86  # print comment line
87  s0 = ' '.join(["%20s" % (field) for field in field_list])
88  print("# " + s0)
89 
90  # print fields values
91  rh = RMF.open_rmf_file_read_only(result.filename)
92 
93  for frame_number in range(rh.get_number_of_frames()):
94  IMP.rmf.load_frame(rh, RMF.FrameID(frame_number))
95  rn = rh.get_root_node()
96  for name in klist:
97  s0 = ' '.join("%20s" % (str(rn.get_value(rmf_names_keys[name])))
98  for name in field_list)
99 
100  if not result.nframe:
101  print("> " + s0)
102  else:
103  print(str(frame_number) + " > " + s0)
104  del rh
105 
106 # print given frame number
107 if result.nframe is not None:
108  # print comment line
109  # print fields values
110  rh = RMF.open_rmf_file_read_only(result.filename)
111  IMP.rmf.load_frame(rh, RMF.FrameID(int(result.nframe)))
112  for k in klist:
113  s0 = str(k)+" "+str(rh.get_root_node().get_value(rmf_names_keys[k]))
114  print(s0)
115  del rh
void load_frame(RMF::FileConstHandle file, RMF::FrameID frame)
Load the given RMF frame into the state of the linked objects.
Support for the RMF file format for storing hierarchical molecular data and markup.