Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. """Calculate the eigenvalues and eigeinvectors from residues to
  4. determine the rotation of the protein.
  5. """
  6.  
  7. import os
  8. import numpy as np
  9. import math
  10. import matplotlib.pyplot as plt
  11.  
  12.  
  13. __author__ = ""
  14. __date__ = "14/11/14"
  15. __version__ = "1.0.0"
  16. __copyright__ = "CC_by_SA"
  17. __dependencies__ = "os, numpy, math, matplotlib"
  18.  
  19.  
  20.  
  21. def frange(start, stop, step, dec = 3):
  22.     """Range like function accepting float step
  23.  
  24.    Args:
  25.        start (int)
  26.        stop (int)
  27.        step (int)
  28.        dec (int) number of decimal
  29.  
  30.    Return:
  31.        A list number from start to stop with the given step
  32.    """
  33.     f_tab = []
  34.     val = start
  35.    
  36.     while(val<=stop):
  37.         f_tab.append(val)
  38.         val = round(val + step, dec)
  39.  
  40.     return f_tab
  41.  
  42.  
  43.  
  44. def parse_pdb(filepath):
  45.     """Parse pdb file and return numpy matrix with atoms positions
  46.  
  47.    Args:
  48.        filepath (str) path to the .pdb file
  49.  
  50.    Retrun:
  51.        A numpy matrix containing atoms positions (x, y, z as column).
  52.  
  53.    """
  54.  
  55.     with open(filepath, 'r') as pdbfile:
  56.  
  57.         for line in pdbfile:
  58.             if line[:4] == "ATOM":
  59.                
  60.                 x_pos = float(line[30:38])
  61.                 y_pos = float(line[38:46])
  62.                 z_pos = float(line[46:54])
  63.                
  64.                 try:
  65.                     positions = np.vstack((positions, [x_pos, y_pos, z_pos]));
  66.                 except:
  67.                     positions = np.array([x_pos,y_pos,z_pos])
  68.  
  69.     return positions
  70.  
  71.  
  72.  
  73. def eigen_vectors(pdbfile):
  74.     """Return the eigenvectors from a pdb file
  75.  
  76.    Args:
  77.        pdbfile (str) a path to the pdbfile to parse
  78.  
  79.    Return:
  80.        A numpy vector containing the eigenvectors (length from origin = 1).
  81.    """
  82.  
  83.     # Get the matrix of x,y,z position
  84.     pos_mtx = parse_pdb(pdbfile);
  85.  
  86.     pos_mtx[:,0] = pos_mtx[:,0]- np.mean(pos_mtx[:,0])
  87.     pos_mtx[:,1] = pos_mtx[:,1]- np.mean(pos_mtx[:,1])
  88.     pos_mtx[:,2] = pos_mtx[:,2]- np.mean(pos_mtx[:,2])
  89.     # Calculate the inertia matrix multiplying the transpose matrix to itself
  90.     square_mtx = np.transpose(pos_mtx).dot(pos_mtx)
  91.    
  92.     # eigenvectors  
  93.     eig_vec = np.linalg.eig(square_mtx)[1][:,0]
  94.     print eig_vec;
  95.     return eig_vec;
  96.  
  97.  
  98.  
  99. def zaxis_angles(axis):
  100.     """Determine the angle between z-axis and the axis specified
  101.    
  102.    Args:
  103.        axis (numpy vector) the normalized (length=1) vectors to calculate the
  104.        angle
  105.    
  106.    Return:
  107.        A tuple containing angles values in radian unit
  108.    """
  109.  
  110.     angle = []
  111.     # z axis vector
  112.     z_vec = np.array([0,0,1])
  113.  
  114.     # calculate angle in radian
  115.     angle.append(math.acos(z_vec.dot(axis))*57.3)
  116.     #angles.append(math.acos(z_vec.dot(axis[:,1]))*57.3)
  117.     #angles.append(math.acos(z_vec.dot(axis[:,2]))*57.3)
  118.  
  119.     return angle;
  120.  
  121.  
  122.  
  123. def z_orientations(filepath):
  124.     """Return a list of rotation from a directory containing frame's
  125.    trajectory as .pdb files
  126.  
  127.    Args:
  128.        filepath (str) path to the directory containing trajectory .pdb files
  129.  
  130.    Return:
  131.        list of angles from the z_axis for each frame
  132.    """
  133.  
  134.     files = os.listdir(filepath)
  135.  
  136.     orientation = []
  137.     for pdb in files:
  138.         eig_vec = eigen_vectors(filepath + pdb)
  139.         angle = zaxis_angles(eig_vec)
  140.  
  141.         orientation.append(angle)
  142.         #orientations[1].append(angles[1])
  143.         #orientations[2].append(angles[2])
  144.  
  145.     return orientation;
  146.  
  147.  
  148.  
  149. if __name__ == "__main__":
  150.  
  151.     ori = z_orientations("pdbs/");
  152.  
  153.     time = frange(0, 20, 0.1) # 101 frame avec un pas de 0.2 nm.
  154.  
  155.     plt.plot(time, ori)
  156.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement