DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Structural Bioinformatics 2008: Script 2

09.12.2008
| 2479 views |
  • submit to reddit
        Script2: How to do something with every line in a PDB file (loops)

'''
    *********************************
    Script 2
    *********************************
    1) Running a script
    2) if, == vs =, indentation
    3) for .. in
    4) files: open, go through lines
    5) calling a function e.g. open()
    *********************************
'''
pdb = open("1sgt.pdb", "r")

for line in pdb:
    if line[:4] == 'ATOM' and line[12:16] == " CA ":
        print line[:-1]

'''
Here is the output from the file (stuff omitted where the "..." are)
ATOM      2  CA  VAL A  16      30.031  20.461  38.668  1.00 15.43           C  
ATOM      9  CA  VAL A  17      32.801  20.965  36.104  1.00 15.69           C  
ATOM     16  CA  GLY A  18      35.477  23.530  37.004  1.00 19.96           C  
ATOM     20  CA  GLY A  19      34.201  23.473  40.601  1.00 23.67           C  
...
ATOM   1595  CA  ARG A 243       0.674  23.618  50.775  1.00 41.21           C  
ATOM   1606  CA  THR A 244      -1.252  20.461  49.773  1.00 45.60           C  
ATOM   1613  CA  LEU A 245       1.631  18.110  50.598  1.00 48.95           C