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