Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.15 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.     i = 0;
  55.  
  56.     with open(filepath, 'r') as pdbfile:
  57.  
  58.         for line in pdbfile:
  59.             if line[:4] == "ATOM":
  60.                
  61.                 x_pos = float(line[30:38])
  62.                 y_pos = float(line[38:46])
  63.                 z_pos = float(line[46:54])
  64.                
  65.                 if (i==0):
  66.                     positions = np.array([x_pos,y_pos,z_pos])
  67.                     i += 1;
  68.  
  69.                 else:
  70.                     positions = np.vstack([positions, [x_pos, y_pos, z_pos]]);
  71.  
  72.     return positions;
  73.  
  74.  
  75.  
  76. def eigen_vectors(pdbfile):
  77.     """Return the eigenvectors from a pdb file
  78.  
  79.    Args:
  80.        pdbfile (str) a path to the pdbfile to parse
  81.  
  82.    Return:
  83.        A numpy vector containing the eigenvectors (length from origin = 1).
  84.    """
  85.  
  86.     # Get the matrix of x,y,z position
  87.     pos_mtx = parse_pdb(pdbfile);
  88.  
  89.     pos_mtx[:,0] = pos_mtx[:,0]- np.mean(pos_mtx[:,0])
  90.     pos_mtx[:,1] = pos_mtx[:,1]- np.mean(pos_mtx[:,1])
  91.     pos_mtx[:,2] = pos_mtx[:,2]- np.mean(pos_mtx[:,2])
  92.     # Calculate the inertia matrix multiplying the transpose matrix to itself
  93.     square_mtx = np.transpose(pos_mtx).dot(pos_mtx)
  94.    
  95.     # eigenvectors  
  96.     eig_vec = np.linalg.eig(square_mtx)[1][:,0]
  97.    
  98.     return eig_vec;
  99.  
  100.  
  101.  
  102. def zaxis_angles(axis):
  103.     """Determine the angle between z-axis and the axis specified
  104.    
  105.    Args:
  106.        axis (numpy vector) the normalized (length=1) vectors to calculate the
  107.        angle
  108.    
  109.    Return:
  110.        A tuple containing angles values in radian unit
  111.    """
  112.  
  113.     angle = []
  114.     # z axis vector
  115.     z_vec = np.array([0,0,1])
  116.  
  117.     # calculate angle in radian
  118.     angle.append(math.acos(z_vec.dot(axis))*57.3)
  119.  
  120.     return angle;
  121.  
  122.  
  123.  
  124. def z_orientations(filepath):
  125.     """Return a list of rotation from a directory containing frame's
  126.    trajectory as .pdb files
  127.  
  128.    Args:
  129.        filepath (str) path to the directory containing trajectory .pdb files
  130.  
  131.    Return:
  132.        list of angles from the z_axis for each frame
  133.    """
  134.    
  135.     cor_idx = cor_eigvec(filepath + "/traj0.pdb")
  136.  
  137.     nb_fichier = len(os.listdir(filepath))
  138.  
  139.     for i in xrange(nb_fichier):
  140.         pdb = filepath + "/traj%i.pdb"%i
  141.  
  142.         eig_vec = eigen_vectors(pdb)
  143.         atoms = parse_pdb(pdb)
  144.  
  145.         cor_vec = vecteur_unitaire(atoms, cor_idx[0], cor_idx[1])
  146.        
  147.         same_direction(eig_vec, cor_vec)
  148.  
  149.                    
  150.  
  151.  
  152. def vecteur_unitaire(atoms, i, j):
  153.     """
  154.    """
  155.    
  156.     vect_cor = atoms[j,:] - atoms[i,:]
  157.      
  158.     distance = math.sqrt((atoms[j,0]-atoms[i,0])**2+
  159.                          (atoms[j,1]-atoms[i,1])**2+
  160.                          (atoms[j,2] - atoms[i,2])**2)
  161.  
  162.     vect_cor = vect_cor / distance;
  163.     return vect_cor;
  164.  
  165.  
  166. def same_direction(eig_vec, vec_cor):
  167.  
  168.     scal = vec_cor.dot(eig_vec)
  169.  
  170.     if scal > 0:
  171.         return eig_vec
  172.  
  173.     else:
  174.         return -eig_vec
  175.    
  176.  
  177. def cor_eigvec(filepath, min_dis=17, correl = 0.9):
  178.     """
  179.    """
  180.  
  181.     atoms = parse_pdb(filepath)
  182.     eig_vec = eigen_vectors(filepath)
  183.     is_ok = False
  184.    
  185.  
  186.     for i in xrange(len(atoms)):
  187.         for j in xrange(len(atoms)):
  188.  
  189.             if (i==j):
  190.                 continue;
  191.  
  192.             vect_cor = atoms[j,:] - atoms[i,:]
  193.            
  194.             distance = math.sqrt((atoms[j,0]-atoms[i,0])**2+
  195.                             (atoms[j,1]-atoms[i,1])**2+
  196.                             (atoms[j,2] - atoms[i,2])**2)
  197.            
  198.    
  199.             if (distance <= min_dis):
  200.                 continue;
  201.            
  202.             vect_cor = vect_cor/distance
  203.             scal = vect_cor.dot(eig_vec)
  204.            
  205.             if (scal >= correl):
  206.                 is_ok = True
  207.                 break
  208.  
  209.         if is_ok == True:
  210.             break
  211.  
  212.     if i==j and i==len(atoms[:,0]):
  213.         print "Attention, pas de vecteur de correction trouve pour ces valeurs de distance et de correlation"
  214.         return (-1, -1);
  215.  
  216.     return (i, j);
  217.  
  218.  
  219. if __name__ == "__main__":
  220.  
  221.     ori = z_orientations("pdbs/");
  222.  
  223.     time = frange(0, 20, 0.1) # 101 frame avec un pas de 0.2 nm.
  224.  
  225.     plt.plot(time, ori)
  226.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement