IMP  2.2.0
The Integrative Modeling Platform
TALOSReader.py
1 #!/usr/bin/env python
2 
3 """@namespace IMP.isd.TALOSReader
4  Classes to handle TALOS files or folders.
5 """
6 
7 import sys
8 import os
9 from math import pi
10 from IMP.isd.utils import check_residue, read_sequence_file
11 
12 
14 
15  """ reads a TALOS file, or a TALOS folder, and stores the data """
16 
17  def __init__(self, sequence, detailed_input, keep_all=False,
18  sequence_match=(1, 1)):
19  """start the TALOSReader
20  sequence : a dictionnary of sequence number keys and 3-letter code
21  values.
22  detailed_input : True if the input will be either predAll.tab or the
23  pred/res???.tab files. False if it's pred.tab
24  keep_all : whether to keep outliers or not, when detailed_input==True.
25  sequence_match : in the form (talos_no, sequence_no), assigns a
26  correspondence between residue numberings.
27  """
28  self.detailed_input = detailed_input
29  self.data = {}
30  self.keep_all = keep_all
31  self.sequence = sequence
32  self.offset = sequence_match[1] - sequence_match[0]
33 
34  def add_full_datum(self, resno, phi, psi):
35  """in the case of a list of predictions for one residue, add an entry to
36  data which is:
37  'full' : always True
38  'num' : number of predictions
39  'phi' : the list of predictions for phi
40  'psi' : same for psi
41 
42  """
43  if resno not in self.data:
44  self.data[resno] = {
45  'full': True, 'num': len(phi), 'phi': phi, 'psi': psi}
46  else:
47  raise RuntimeError("would overwrite data for residue %d" % resno)
48 
49  def add_mean_datum(self, resno, num, phi, psi):
50  """in the case of a single (average) prediction output by talos for a
51  given residue, add an entry to data which is:
52  'full' : always False
53  'num' : the number of matches this average was calculated from
54  'phi' : a tuple in the form (mean, error)
55  'psi' : same as phi.
56 
57  """
58 
59  if resno not in self.data:
60  self.data[
61  resno] = {
62  'full': False,
63  'num': num,
64  'phi': phi,
65  'psi': psi}
66  else:
67  raise RuntimeError("would overwrite data for residue %d" % resno)
68 
69  def _read_one_residue(self, fname):
70  fl = open(fname)
71  resno = int(os.path.basename(fname)[3:6]) + self.offset
72  phi = []
73  psi = []
74  for line in fl:
75  tokens = line.split()
76  if len(tokens) < 1:
77  continue
78  if tokens[1] == 'RESNAMES':
79  check_residue(self.sequence[resno], tokens[3])
80  continue
81  if not tokens[0].isdigit():
82  continue
83  if float(tokens[4]) < 0.999 and not self.keep_all:
84  continue
85  phi.append(float(tokens[1]) * 2 * pi / 360.)
86  psi.append(float(tokens[2]) * 2 * pi / 360.)
87  self.add_full_datum(resno, phi, psi)
88 
89  def _read_predAll(self, fname):
90  fl = open(fname)
91  resno = -1
92  for line in fl:
93  tokens = line.split()
94  if len(tokens) == 0 or not tokens[0].isdigit():
95  continue
96  oldresno = resno
97  resno = int(tokens[1]) + self.offset
98  if resno != oldresno:
99  if oldresno != -1:
100  self.add_full_datum(resno, phi, psi)
101  phi = []
102  psi = []
103  resname = tokens[2][1]
104  check_residue(self.sequence[resno], resname)
105  if float(tokens[6]) < 0.999 and not self.keep_all:
106  continue
107  phi.append(float(tokens[3]) * 2 * pi / 360.)
108  psi.append(float(tokens[4]) * 2 * pi / 360.)
109 
110  def _read_observations(self, fname):
111  if fname.endswith('predAll.tab'):
112  self._read_predAll(fname)
113  else:
114  self._read_one_residue(fname)
115 
116  def _read_averages(self, fname):
117  fl = open(fname)
118  for line in fl:
119  tokens = line.split()
120  if not tokens[0].isdigit():
121  continue
122  resno = int(tokens[0]) + self.offset
123  check_residue(resno, tokens[1])
124  phi, psi, dphi, dpsi = map(lambda a: 2 * pi * float(a) / 360.,
125  tokens[2:6])
126  num = int(tokens[8])
127  if num == 0:
128  continue
129  self.add_mean_datum(resno, num, (phi, dphi), (psi, dpsi))
130 
131  def read(self, fname):
132  "reads a TALOS file and returns data. See add_datum methods."
133  if self.detailed_input:
134  self._read_observations(fname)
135  else:
136  self._read_averages(fname)
137 
138  def get_data(self):
139  return self.data
140 
141 
142 if __name__ == '__main__':
143 
144  talos = 'pred.tab'
145  sequence = read_sequence_file('seq.dat', sequence_match=(1, 5))
146  reader = TALOSReader(sequence)
147  reader.read(talos)
148  data = reader.get_data()
def add_full_datum
in the case of a list of predictions for one residue, add an entry to data which is: 'full' : always ...
Definition: TALOSReader.py:34
reads a TALOS file, or a TALOS folder, and stores the data
Definition: TALOSReader.py:13
def read
reads a TALOS file and returns data.
Definition: TALOSReader.py:131
def add_mean_datum
in the case of a single (average) prediction output by talos for a given residue, add an entry to dat...
Definition: TALOSReader.py:49
Miscellaneous utilities.
Definition: utils.py:1