00001 """
00002 3DCOMB module
00003
00004 Author: Niklaus Johner
00005
00006 This module is for structural alignment from OpenStructure using the external program 3DCOMB.
00007
00008 How To Use This Module:
00009 1. Import it (e.g. as "from ost.bindings import align_3dcomb")
00010 2. Use it (e.g. as "alignment,transformation_list = align_3dcomb.AlignStructures(view_list)")
00011
00012 Requirement:
00013 - 3DCOMB installed
00014 """
00015
00016 from ost.bindings import utils
00017 import subprocess,os
00018 from ost import settings
00019 from ost import io
00020 import ost
00021 import ost.geom
00022
00023 def _GetExecutable(comb_exe, comb_env):
00024 """
00025 Function to check if 3DCOMB executable is present
00026
00027 :param comb_exe: Explicit path to 3DCOMB executable
00028 :param msms_env: Environment variable pointing to 3DCOMB executable
00029 :returns: Path to the executable
00030 :raises: :class:`~ost.FileNotFound` if executable is not found
00031 """
00032 return settings.Locate(['3DCOMB_linux','3DCOMB_win.exe'], explicit_file_name=comb_exe,
00033 env_name=comb_env)
00034
00035 def _SetupFiles(structure_list):
00036 """
00037 Setup files for MSMS calculation in temporary directory
00038
00039 :param structure_list: A list of EntityView that will be aligned.\
00040 each EntityView should contain a single chain and each residue needs to have a CA atom.
00041 :returns: calss:settings.TempDir
00042 :raises: class:`RuntimeError` if on of the Views is not valid
00043 """
00044
00045
00046 if not all([ev.IsValid() for ev in structure_list]):
00047 raise RuntimeError, "Invalid EntityView in structure_list"
00048 tpd=utils.TempDirWithFiles(structure_list)
00049
00050
00051 outfile=open(os.path.join(tpd.dirname,'list'),'w')
00052 outfile.write('\n'.join(tpd.files))
00053 outfile.close()
00054 return tpd
00055
00056
00057 def _Run3DCOMB(command,tpd):
00058 """
00059 Run the 3DCOMB alignment command
00060
00061 This functions starts the external 3DCOMB executable and returns the stdout of
00062 3DCOMB.
00063
00064 :param command: Command to execute
00065 :returns: stdout of 3DCOMB
00066 :raises: :class:`CalledProcessError` for non-zero return value
00067 """
00068 outname=os.path.join(tpd.dirname,'align.out')
00069 outfile=open(outname,'w')
00070 returncode=subprocess.call(command, shell=True, stdout=outfile)
00071 outfile.close()
00072
00073 if returncode!=0:
00074 print "WARNING: 3DCOMB error\n"
00075 raise subprocess.CalledProcessError
00076 return returncode
00077
00078 def _ParseOutput(tpd):
00079
00080 ali=io.LoadAlignment(os.path.join(tpd.dirname,'list.ali'),'fasta')
00081 for i in range(ali.GetCount()):
00082 ali.SetSequenceName(i,'struc{0}'.format(i))
00083
00084 f=open(os.path.join(tpd.dirname,'list.rmt'),'r')
00085 Tl=[]
00086 for l in f:
00087 if l.startswith('>'):
00088 fl=ost.FloatList()
00089 for i in range(3):
00090 l=f.next()
00091 sl=l.split(',')
00092 fl.extend([float(el) for el in sl[0].split()+[sl[1]]])
00093 fl.extend([0,0,0,1])
00094 T=ost.geom.Transform()
00095 M=ost.geom.Mat4(*fl)
00096 T.SetMatrix(M)
00097 Tl.append(T)
00098
00099 outfile=open(os.path.join(tpd.dirname,'align.out'),'r')
00100 results={}
00101 for line in outfile:
00102 if line.startswith('Objective function value is'):results['objective_function']=float(line.split()[-1])
00103 if line.startswith('CORE_LEN'):
00104 l=line.split(',')
00105 for el in l:
00106 s=el.split('=')
00107 results[s[0]]=float(s[1])
00108 return ali,Tl,results
00109
00110
00111 def AlignStructures(structure_list,apply_transform=False,name_list=None,comb_exe=None,comb_env=None):
00112 comb_executable=_GetExecutable(comb_exe, comb_env)
00113 tpd=_SetupFiles(structure_list)
00114 command=' '.join([comb_executable,os.path.join(tpd.dirname,'list')])
00115 returncode=_Run3DCOMB(command,tpd)
00116 try:ali,Tl,results=_ParseOutput(tpd)
00117 except:
00118 print 'could not parse output'
00119 raise RuntimeError
00120 if apply_transform:
00121 for T,ev in zip(Tl,structure_list):
00122 ev.handle.FixTransform()
00123 ev.handle.SetTransform(T)
00124 tpd.Cleanup()
00125 return ali,Tl,results
00126
00127
00128
00129