IMP logo
IMP Reference Guide  2.12.0
The Integrative Modeling Platform
process_output.py
1 #! /usr/bin/env python
2 
3 from __future__ import print_function
4 try:
5  import argparse
6 except ImportError:
7  from IMP._compat_argparse import argparse
8 
9 
10 import difflib
11 
12 p = argparse.ArgumentParser(
13  description="Process output data file saved as dictionaries. "
14  "It has two modalities: print selected fields for all "
15  "lines or print a particular line where a field has a "
16  "given value. Example of usage: process_output.py "
17  "--soft -s To E S -f log.3.native-2-no-red. "
18  "process_output.py --soft --search_field EV0 "
19  "--search_value 5.67750116023 -f log.3.native-2-no-red")
20 p.add_argument('-f', action="store", dest="filename",
21  help="file name to process")
22 p.add_argument('-s', dest="fields", nargs="+",
23  help="Specify all fields to be printed. Multiple flags "
24  "will append a list of fields to be printed")
25 p.add_argument('-t', dest="single_column_field",
26  help="Specify a single column field to be printed. It "
27  "will be printed as a column. If the field name is "
28  "not complete, it will print all fields whose name "
29  "contain the queried string.")
30 p.add_argument('-p', action="store_true", dest="print_fields",
31  default=False, help="print the fields contained in the file")
32 p.add_argument('--head', action="store_true", dest="print_header",
33  default=False,
34  help="print the fields contained in the file (only stat2)")
35 p.add_argument('-n', action="store", dest="print_raw_number",
36  help="print the selected raw")
37 p.add_argument('--soft', action="store_true", dest="soft_match", default=False,
38  help="Soft match. Closest matching field will be printed, "
39  "e.g. S will give Step_Number, En will give energy, etc. ")
40 p.add_argument('--search_field', dest="search_field",
41  help="Search a line from the file. Specify the field to "
42  "be searched for. ")
43 p.add_argument('--search_value', dest="search_value",
44  help="Search a line from the file. Specify the value to "
45  "be searched for. ")
46 p.add_argument('--nframe', action="store_true", dest="nframe", default=False,
47  help="Print the frame number as initial column")
48 
49 result = p.parse_args()
50 
51 isstat1 = False
52 isstat2 = False
53 
54 # open the file
55 if not result.filename is None:
56  f = open(result.filename, "r")
57 else:
58  raise ValueError("No file name provided. Use -h for help")
59 
60 # get the keys from the first line
61 for line in f.readlines():
62  d = eval(line)
63  klist = list(d.keys())
64  # check if it is a stat2 file
65  if "STAT2HEADER" in klist:
66  import operator
67  isstat2 = True
68  for k in klist:
69  if "STAT2HEADER" in str(k):
70  if result.print_header:
71  print(k, d[k])
72  del d[k]
73  stat2_dict = d
74  # get the list of keys sorted by value
75  kkeys = [k[0]
76  for k in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
77  klist = [k[1]
78  for k in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
79  invstat2_dict = {}
80  for k in kkeys:
81  invstat2_dict.update({stat2_dict[k]: k})
82  else:
83  isstat1 = True
84  klist.sort()
85 
86  break
87 f.close()
88 
89 # print the keys
90 if result.print_fields:
91  for key in klist:
92  if len(key) <= 100:
93  print(key)
94  else:
95  print(key[0:100], "... omitting the rest of the string (>100 characters)")
96 
97 
98 # the field string matching is by default strict, i.e., the input string
99 # must be the same as the one in the file
100 match_strictness = 1.0
101 if result.soft_match:
102  match_strictness = 0.1
103 
104 # print the queried fields
105 if not result.fields is None:
106  field_list = []
107  # check whether the fields exist and convert them to best maching existing
108  # field names
109  for field in result.fields:
110  found_entries = difflib.get_close_matches(
111  field,
112  klist,
113  1,
114  match_strictness)
115  if len(found_entries) == 0:
116  raise ValueError("field " + field + " non found")
117  else:
118  field_list.append(found_entries[0])
119 
120  # print comment line
121  s0 = ' '.join(["%20s" % (field) for field in field_list])
122  print("# " + s0)
123 
124  # print fields values
125  f = open(result.filename, "r")
126  line_number = 0
127  for line in f.readlines():
128  line_number += 1
129  try:
130  d = eval(line)
131  except:
132  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
133  continue
134  if isstat1:
135  s0 = ' '.join(["%20s" % (str(d[field])) for field in field_list])
136  elif isstat2:
137  if line_number == 1:
138  continue
139  s0 = ' '.join(["%20s" % (str(d[invstat2_dict[field]]))
140  for field in field_list])
141  if not result.nframe:
142  print("> " + s0)
143  else:
144  print(str(line_number)+ " > " + s0)
145  f.close()
146 
147 
148 if not result.single_column_field is None:
149  field_list = []
150  for k in klist:
151  if result.single_column_field in k:
152  field_list.append(k)
153 
154  f = open(result.filename, "r")
155  line_number = 0
156  for line in f.readlines():
157  line_number += 1
158  try:
159  d = eval(line)
160  except:
161  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
162  continue
163  if isstat1:
164  for key in field_list:
165  print(key, d[key])
166  elif isstat2:
167  if line_number == 1:
168  continue
169  for key in field_list:
170  print(key, d[invstat2_dict[key]])
171  print(" ")
172  f.close()
173 
174 if (not result.search_field is None) and (not result.search_value is None):
175  # check whether the fields exist and convert them to best maching existing
176  # field names
177  found_entries = difflib.get_close_matches(
178  result.search_field,
179  klist,
180  1,
181  match_strictness)
182  if len(found_entries) == 0:
183  raise ValueError("field " + results.search_field + " non found")
184  else:
185  corrected_field = found_entries[0]
186  # print fields values
187  f = open(result.filename, "r")
188  line_number = 0
189  for line in f.readlines():
190  line_number += 1
191  try:
192  d = eval(line)
193  except:
194  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
195  continue
196 
197  if isstat1:
198  if (str(d[corrected_field]) == result.search_value):
199  for key in klist:
200  print(key, d[key])
201  elif isstat2:
202  if line_number == 1:
203  continue
204  if (str(d[invstat2_dict[corrected_field]]) == result.search_value):
205  for key in klist:
206  print(key, d[invstat2_dict[key]])
207  f.close()
208 
209 if not result.print_raw_number is None:
210  # check whether the fields exist and convert them to best maching existing
211  # field names
212  f = open(result.filename, "r")
213  line_number = 0
214  for line in f.readlines():
215  line_number += 1
216  if isstat1:
217  if (line_number == int(result.print_raw_number)):
218  try:
219  d = eval(line)
220  except:
221  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
222  break
223  for key in klist:
224  print(key, d[key])
225 
226  elif isstat2:
227  if (line_number == int(result.print_raw_number) + 1):
228  try:
229  d = eval(line)
230  except:
231  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
232  break
233  for key in klist:
234  print(key, d[invstat2_dict[key]])
235  f.close()