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