OpenStructure
tmtools.py
Go to the documentation of this file.
1 #------------------------------------------------------------------------------
2 # This file is part of the OpenStructure project <www.openstructure.org>
3 #
4 # Copyright (C) 2008-2009 by the OpenStructure authors
5 #
6 # This library is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation; either version 3.0 of the License, or (at your option)
9 # any later version.
10 # This library is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this library; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #------------------------------------------------------------------------------
19 """
20 Wrappers for the tmalign and tmscore utilities.
21 
22 References:
23 
24 tmscore: Yang Zhang and Jeffrey Skolnick, Proteins 2004 57: 702-710
25 tmalign: Y. Zhang and J. Skolnick, Nucl. Acids Res. 2005 33, 2302-9
26 
27 
28 Authors: Pascal Benkert, Marco Biasini
29 """
30 
31 import subprocess, os, tempfile, platform
32 from ost import settings, io, geom, seq
33 
34 def _SetupFiles(models):
35  # create temporary directory
36  tmp_dir_name=tempfile.mkdtemp()
37  for index, model in enumerate(models):
38  io.SavePDB(model, os.path.join(tmp_dir_name, 'model%02d.pdb' % (index+1)))
39  return tmp_dir_name
40 
41 def _CleanupFiles(dir_name):
42  import shutil
43  shutil.rmtree(dir_name)
44 
46  def __init__(self, rmsd, tm_score, aligned_length, transform, ref_sequence, alignment):
47  self.rmsd=rmsd
48  self.tm_score=tm_score
49  self.aligned_length=aligned_length
50  self.transform=transform
51  self.ref_sequence =ref_sequence
52  self.alignment=alignment
53 
54 def _ParseTmAlign(lines):
55  info_line=lines[11].split(',')
56  aln_length=float(info_line[0].split('=')[1].strip())
57  rmsd=float(info_line[1].split('=')[1].strip())
58  tm_score=float(info_line[2].split('=')[1].strip())
59  tf1=[float(i.strip()) for i in lines[15].split()]
60  tf2=[float(i.strip()) for i in lines[16].split()]
61  tf3=[float(i.strip()) for i in lines[17].split()]
62  rot=geom.Mat3(tf1[2], tf1[3], tf1[4], tf2[2], tf2[3],
63  tf2[4], tf3[2], tf3[3], tf3[4])
64  tf=geom.Mat4(rot)
65  tf.PasteTranslation(geom.Vec3(tf1[1], tf2[1], tf3[1]))
66  seq1 = seq.CreateSequence("1",lines[26].strip())
67  seq2 = seq.CreateSequence("2",lines[28].strip())
68  alignment = seq.CreateAlignment()
69  alignment.AddSequence(seq2)
70  alignment.AddSequence(seq1)
71  return TMAlignResult(rmsd, tm_score, aln_length, tf, seq2, alignment)
72 
73 def _RunTmAlign(tmalign, tmp_dir):
74  model1_filename=os.path.join(tmp_dir, 'model01.pdb')
75  model2_filename=os.path.join(tmp_dir, 'model02.pdb')
76  if platform.system() == "Windows":
77  tmalign_path=settings.Locate('tmalign.exe', explicit_file_name=tmalign)
78  command="\"%s\" %s %s" %(os.path.normpath(tmalign_path), model1_filename, model2_filename)
79  else:
80  tmalign_path=settings.Locate('tmalign', explicit_file_name=tmalign)
81  command="\"%s\" \"%s\" \"%s\"" %(tmalign_path, model1_filename, model2_filename)
82  ps=subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
83  ps.wait()
84  lines=ps.stdout.readlines()
85  if (len(lines))<22:
86  raise RuntimeError("tmalign superposition failed")
87  return _ParseTmAlign(lines)
88 
90  def __init__(self, rmsd_common, tm_score, max_sub,
91  gdt_ts, gdt_ha, rmsd_below_five, transform):
92  self.rmsd_common=rmsd_common
93  self.tm_score=tm_score
94  self.max_sub=max_sub
95  self.gdt_ts=gdt_ts
96  self.gdt_ha=gdt_ha
97  self.rmsd_below_five=rmsd_below_five
98  self.transform=transform
99 
100 def _ParseTmScore(lines):
101  tf1=[float(i.strip()) for i in lines[23].split()]
102  tf2=[float(i.strip()) for i in lines[24].split()]
103  tf3=[float(i.strip()) for i in lines[25].split()]
104  rot=geom.Mat3(tf1[2], tf1[3], tf1[4], tf2[2], tf2[3],
105  tf2[4], tf3[2], tf3[3], tf3[4])
106  tf=geom.Mat4(rot)
107  tf.PasteTranslation(geom.Vec3(tf1[1], tf2[1], tf3[1]))
108  result=TMScoreResult(float(lines[14].split()[-1].strip()),
109  float(lines[16].split()[2].strip()),
110  float(lines[17].split()[1].strip()),
111  float(lines[18].split()[1].strip()),
112  float(lines[19].split()[1].strip()),
113  float(lines[27].split()[-1].strip()),
114  tf)
115  return result
116 
117 
118 def _RunTmScore(tmscore, tmp_dir):
119  model1_filename=os.path.join(tmp_dir, 'model01.pdb')
120  model2_filename=os.path.join(tmp_dir, 'model02.pdb')
121  if platform.system() == "Windows":
122  tmscore_path=settings.Locate('tmscore.exe', explicit_file_name=tmscore)
123  command="\"%s\" %s %s" %(os.path.normpath(tmscore_path), model1_filename,
124  model2_filename)
125  else:
126  tmscore_path=settings.Locate('tmscore', explicit_file_name=tmscore)
127  command="\"%s\" \"%s\" \"%s\"" % (tmscore_path, model1_filename,
128  model2_filename)
129  ps=subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
130  ps.wait()
131  lines=ps.stdout.readlines()
132  if (len(lines))<22:
133  raise RuntimeError("tmscore superposition failed")
134  return _ParseTmScore(lines)
135 
136 def TMAlign(model1, model2, tmalign=None):
137  """
138  Run tmalign on two protein structures
139  """
140  tmp_dir_name=_SetupFiles((model1, model2))
141  result=_RunTmAlign(tmalign, tmp_dir_name)
142  model1.handle.EditXCS().ApplyTransform(result.transform)
143  _CleanupFiles(tmp_dir_name)
144  return result
145 def TMScore(model1, model2, tmscore=None):
146  """
147  Run tmscore on two protein structures
148  """
149  tmp_dir_name=_SetupFiles((model1, model2))
150  result=_RunTmScore(tmscore, tmp_dir_name)
151  model1.handle.EditXCS().ApplyTransform(result.transform)
152  _CleanupFiles(tmp_dir_name)
153  return result