00001 """ 00002 Some functions for analyzing structures 00003 00004 Author: Niklaus Johner (Niklaus.Johner@unibas.ch) 00005 """ 00006 import os 00007 import ost 00008 00009 def GetFrameFromEntity(eh): 00010 """ 00011 This function returns a CoordFrame from an EntityHandle 00012 00013 :param eh: 00014 :type eh: :class:`~ost.mol.EntityHandle` 00015 00016 :return: :class:`ost.mol.CoordFrame` 00017 """ 00018 return ost.mol.CreateCoordFrame(eh.GetAtomPosList(ordered_by_index=True)) 00019 00020 def GetDistanceBetwCenterOfMass(sele1,sele2): 00021 """ 00022 This function calculates the distance between the centers of mass 00023 of **sele1** and **sele2**, two selections from the same Entity. 00024 00025 :param sele1: 00026 :param sele2: 00027 :type sele1: :class:`~ost.mol.EntityView` 00028 :type sele2: :class:`~ost.mol.EntityView` 00029 00030 :return: :class:`float` 00031 """ 00032 if not sele1.IsValid() and sele2.IsValid(): 00033 print 'invalid view' 00034 return 00035 eh=sele1.GetHandle() 00036 if not eh==sele2.GetHandle(): 00037 print 'The two views must be from the same entity' 00038 return 00039 f=GetFrameFromEntity(eh) 00040 return f.GetDistanceBetwCenterOfMass(sele1,sele2) 00041 00042 def GetMinDistanceBetweenViews(sele1,sele2): 00043 """ 00044 This function calculates the minimal distance between 00045 **sele1** and **sele2**, two selections from the same Entity. 00046 00047 :param sele1: 00048 :param sele2: 00049 :type sele1: :class:`~ost.mol.EntityView` 00050 :type sele2: :class:`~ost.mol.EntityView` 00051 00052 :return: :class:`float` 00053 """ 00054 if not sele1.IsValid() and sele2.IsValid(): 00055 print 'invalid view' 00056 return 00057 eh=sele1.GetHandle() 00058 if not eh==sele2.GetHandle(): 00059 print 'The two views must be from the same entity' 00060 return 00061 f=GetFrameFromEntity(eh) 00062 return f.GetMinDistance(sele1,sele2) 00063 00064 def GetMinDistBetwCenterOfMassAndView(sele1,sele2): 00065 """ 00066 This function calculates the minimal distance between **sele2** and 00067 the center of mass of **sele1**, two selections from the same Entity. 00068 00069 :param sele1: The selection from which the center of mass is taken 00070 :param sele2: 00071 :type sele1: :class:`~ost.mol.EntityView` 00072 :type sele2: :class:`~ost.mol.EntityView` 00073 00074 :return: distance (\ :class:`float`\ ) 00075 """ 00076 if not sele1.IsValid() and sele2.IsValid(): 00077 print 'invalid view' 00078 return 00079 eh=sele1.GetHandle() 00080 if not eh==sele2.GetHandle(): 00081 print 'The two views must be from the same entity' 00082 return 00083 f=GetFrameFromEntity(eh) 00084 return f.GetMinDistBetwCenterOfMassAndView(sele1,sele2) 00085 00086 00087 def GetAlphaHelixContent(sele1): 00088 """ 00089 This function calculates the content of alpha helix in a view. 00090 All residues in the view have to ordered and adjacent (no gaps allowed) 00091 00092 :param sele1: 00093 :type sele1: :class:`~ost.mol.EntityView` 00094 00095 :return: :class:`float` 00096 """ 00097 if not sele1.IsValid(): 00098 print 'invalid view' 00099 return 00100 eh=sele1.GetHandle() 00101 f=GetFrameFromEntity(eh) 00102 return f.GetAlphaHelixContent(sele1) 00103 00104 00105 def CalculateBestFitLine(sele1): 00106 """ 00107 This function calculates the best fit line to the atoms in **sele1**. 00108 00109 :param sele1: 00110 :type sele1: :class:`~ost.mol.EntityView` 00111 00112 :return: :class:`~ost.geom.Line3` 00113 """ 00114 if not sele1.IsValid(): 00115 print 'invalid view' 00116 return 00117 eh=sele1.GetHandle() 00118 f=GetFrameFromEntity(eh) 00119 return f.GetODRLine(sele1) 00120 00121 def CalculateBestFitPlane(sele1): 00122 """ 00123 This function calculates the best fit plane to the atoms in **sele1**. 00124 00125 :param sele1: 00126 :type sele1: :class:`~ost.mol.EntityView` 00127 00128 :return: :class:`~ost.geom.Plane` 00129 """ 00130 if not sele1.IsValid(): 00131 print 'invalid view' 00132 return 00133 eh=sele1.GetHandle() 00134 f=GetFrameFromEntity(eh) 00135 return f.GetODRPlane(sele1) 00136 00137 def CalculateHelixAxis(sele1): 00138 """ 00139 This function calculates the best fit cylinder to the CA atoms in **sele1**, 00140 and returns its axis. Residues should be ordered correctly 00141 in **sele1**. 00142 00143 :param sele1: 00144 :type sele1: :class:`~ost.mol.EntityView` 00145 00146 :return: :class:`~ost.geom.Line3` 00147 """ 00148 if not sele1.IsValid(): 00149 print 'invalid view' 00150 return 00151 eh=sele1.GetHandle() 00152 f=GetFrameFromEntity(eh) 00153 return f.FitCylinder(sele1)[0] 00154 00155 00156 def CalculateDistanceDifferenceMatrix(sele1,sele2): 00157 """ 00158 This function calculates the pairwise distance differences between two selections (\ :class:`~ost.mol.EntityView`\ ). 00159 The two selections should have the same number of atoms 00160 It returns an NxN DistanceDifferenceMatrix M (where N is the number of atoms in sele1) 00161 where M[i,j]=||(sele2.atoms[i].pos-sele2.atoms[j].pos)||-||(sele1.atoms[i].pos-sele1.atoms[j].pos)|| 00162 00163 :param sele1: 00164 :param sele2: 00165 :type sele1: :class:`~ost.mol.EntityView` 00166 :type sele2: :class:`~ost.mol.EntityView` 00167 00168 :return: NxN numpy matrix 00169 """ 00170 try:import numpy as npy 00171 except ImportError: 00172 LogError("Function needs numpy, but I could not import it.") 00173 raise 00174 if not sele1.IsValid() and sele2.IsValid(): 00175 print 'invalid view' 00176 return 00177 if not sele1.GetAtomCount()==sele2.GetAtomCount(): 00178 print 'The two views must have the same number of atoms' 00179 return 00180 n_atoms=sele1.GetAtomCount() 00181 M=npy.zeros([n_atoms,n_atoms]) 00182 for i,a1 in enumerate(sele1.atoms): 00183 for j,a2 in enumerate(sele1.atoms): 00184 if i>=j:continue 00185 d1=ost.geom.Distance(a1.pos,a2.pos) 00186 d2=ost.geom.Distance(sele2.atoms[i].pos,sele2.atoms[j].pos) 00187 M[i,j]=d2-d1 00188 M[j,i]=d2-d1 00189 return M 00190 00191