2013-04-03 56 views
0

目的:给出一个PDB文件,打印出所有形成三级蛋白质结构中二硫键的半胱氨酸残基对。许可证:GNU GPL撰写者:Eric MillerPython脚本中的缩进

#!/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() 

当我尝试运行此脚本时,出现缩进问题。我的工作目录中有2v5t.pdb(需要运行上面的脚本)。任何解决方案

+0

? –

+0

最后一行。 'main()'应该缩进。 – ehudt

+1

谁在Python代码中使用分号? Eugh ... – nneonneo

回答

2

对于我来说,缩进在'prettyPrint'和'main'中被打破。也不需要使用';'。试试这个:

#!/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() 
+0

非常感谢Artsiom Rudzenka。它的工作现在。 – user2176228

+0

@ user2176228不客气 –

0

此:

if __name__ == "__main__": 
main() 

应该是:

if __name__ == "__main__": 
    main() 

此外,Python解释器会给你的信息在IndentationError 下到线上。我强烈建议阅读提供的错误消息,因为开发人员出于某种原因编写它们。

+0

谢谢。对不起,这是错字。我纠正了。我得到以下错误打印“{0} \ t \ t {1} \ t \ t {2}”。格式(r1,r2,d); – user2176228

0

你没有说在错误被标记的,但却:

if __name__ == "__main__": 
main() 

应该是:

if __name__ == "__main__": 
    main() 
在最后一行
+0

我忘了把它标记为代码,它删除了空格 –

+1

删除那些'''格式化,你会没事的;) –

+0

嘿,欢呼声。感谢那。 –