Scrip pdb distance check.py

From Bioinformatikpedia

<source lang="python">

  1. !/usr/bin/python

import sys import os import re import numpy import math


input=sys.argv[1]#pdb file with coordinates

pdb_dist=[]#[pos1, pos2, state] state~TP or FP coordinates={}# contains x,y,z coordinates in a list of all atoms for every residue. {residue: [[x,y,z],[x,y,z],......]}

  1. calculates euklidean distance between two atoms

def calc_dist(co1, co2):#dictionary with xyz

       p1=numpy.array((co1["x"],co1["y"],co1["z"]))
       p2=numpy.array((co2["x"],co2["y"],co2["z"]))
       dist=numpy.linalg.norm(p1-p2)
       return dist
  1. checks distance between any atom of two residues

def any_atom(atom1_list, atom2_list):

               for a1 in atom1_list:
                       for a2 in atom2_list:
                               dist=calc_dist(a1,a2)
                               if(dist<5.0):
                                       return "TP"
               return "FP"
  1. checks TP or FP for all possible residue pairs

def all_dist():

       checker=len(coordinates.keys())+1
       for i in range(1,checker):#for every residue              
               for j in range(i,checker):
                       atoms1=coordinates[i]#of all atoms of residue 1 (list of dictionarys)
                       atoms2=coordinates[j]#of all atoms of residue 2 (list of dictionarys)
                       state=any_atom(atoms1, atoms2)
                       pdb_dist.append([i,j,state])


def read_pdb():

       file=open(input, 'r')
       lines=file.readlines()
       for line in lines:
               if(re.match("^ATOM",line)):
                       info=line.split()
                       if(info[2]=="N"):#every new residue starts with N
                               res_co=[{"x":float(info[6]),"y":float(info[7]),"z":float(info[8])}] #list of coordinates of all atoms
                       else:
                               res_co.append({"x":float(info[6]),"y":float(info[7]),"z":float(info[8])})
                       coordinates[int(info[5])]=res_co #key:aa position
       file.close()


  1. writes CN_dist into file

def make_output(a):

       file_name="contac_states_%s"%(input)
       file=open(file_name, 'w')
       for i in a:
               file.write("%i %i %s\n"%(i[0],i[1],i[2]))
       file.close()


if __name__ == '__main__':

       read_pdb()
       all_dist()
       make_output(pdb_dist)

</source>