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