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