0

Purpose: Given a PDB file, prints out all pairs of Cysteine residues forming disulfide bonds in the tertiary protein structure. Licence: GNU GPL Written By: Eric Miller

#!/usr/bin/env python
import math

def getDistance((x1,y1,z1),(x2,y2,z2)):
   d = math.sqrt(pow((x1-x2),2)+pow((y1-y2),2)+pow((z1-z2),2));
   return round(d,3);

def prettyPrint(dsBonds):
   print "Residue 1\tResidue 2\tDistance";
   for (r1,r2,d) in dsBonds:
   print " {0}\t\t {1}\t\t {2}".format(r1,r2,d);

def main():
   pdbFile = open('2v5t.pdb','r');
   maxBondDist = 2.5;

   isCysLine = lambda line: (line[0:4] == "ATOM" and line[13:15] == "SG");
   cysLines = [line for line in pdbFile if isCysLine(line)];
   pdbFile.close();

   getCoords = lambda line:(float(line[31:38]),
      float(line[39:46]),float(line[47:54]));
   cysCoords = map(getCoords, cysLines);
   dsBonds = [];

   for i in range(len(cysCoords)-1):
      for j in range(i+1,len(cysCoords)):
         dist = getDistance(cysCoords[i],cysCoords[j]);
         residue1 = int(cysLines[i][23:27]);
         residue2 = int(cysLines[j][23:27]);
         if (dist < maxBondDist):
            dsBonds.append((residue1,residue2,dist));

   prettyPrint(dsBonds);

if __name__ == "__main__":
   main()

When I try to run this script I get indentation problem. I have 2v5t.pdb (required to run the above script) in my working directory. Any solution?

2
  • Last line. main() should be indented. Commented Apr 3, 2013 at 10:13
  • 1
    Who uses semicolons in Python code? Eugh... Commented Apr 3, 2013 at 10:17

3 Answers 3

2

For me the indentation is broken within 'prettyPrint' and in 'main'. Also no need to use ';'. Try this:

#!/usr/bin/env python
import math

# Input: Two 3D points of the form (x,y,z).
# Output: Euclidean distance between the points.
def getDistance((x1, y1, z1), (x2, y2, z2)):
   d = math.sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2) + pow((z1 - z2), 2))
   return round(d, 3)

# Purpose: Prints a list of 3-tuples (r1,r2,d). R1 and r2 are
# residue numbers, and d is the distance between their respective
# gamma sulfur atoms.
def prettyPrint(dsBonds):
   print "Residue 1\tResidue 2\tDistance"
   for r1, r2, d in dsBonds:
       print " {0}\t\t {1}\t\t {2}".format(r1, r2, d)

# Purpose: Find all pairs of cysteine residues whose gamma sulfur atoms
# are within maxBondDist of each other.
def main():
   pdbFile = open('2v5t.pdb','r')
   #Max distance to consider a disulfide bond.
   maxBondDist = 2.5

   # Anonymous function to check if a line from the PDB file is a gamma
   # sulfur atom from a cysteine residue.
   isCysLine = lambda line: (line[0:4] == "ATOM" and line[13:15] == "SG")
   cysLines = [line for line in pdbFile if isCysLine(line)]
   pdbFile.close()

   # Anonymous function to get (x,y,z) coordinates in angstroms for
   # the location of a cysteine residue's gamma sulfur atom.
   getCoords = lambda line:(float(line[31:38]),
                            float(line[39:46]), float(line[47:54]))
   cysCoords = map(getCoords, cysLines)
   # Make a list of all residue pairs classified as disulfide bonds.
   dsBonds = []

   for i in range(len(cysCoords)-1):
      for j in range(i+1, len(cysCoords)):
         dist = getDistance(cysCoords[i], cysCoords[j])
         residue1 = int(cysLines[i][23:27])
         residue2 = int(cysLines[j][23:27])
         if dist < maxBondDist:
            dsBonds.append((residue1,residue2,dist))

   prettyPrint(dsBonds)

if __name__ == "__main__":
    main()
Sign up to request clarification or add additional context in comments.

Comments

0

This:

if __name__ == "__main__":
main()

Should be:

if __name__ == "__main__":
    main()

Also, the python interpreter will give you information on the IndentationError down to the line. I strongly suggest reading the error messages provided, as developers write them for a reason.

1 Comment

Thanks. Sorry it was typo. I corrected. I get the following error print " {0}\t\t {1}\t\t {2}".format(r1,r2,d);
0

You didn't say where the error was flagged to be but:

if __name__ == "__main__":
main()

Should be:

if __name__ == "__main__":
   main()

1 Comment

nah i forgot to flag it as code and it removed the whitespace

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.