user1357015
user1357015

Reputation: 11686

parsing pdb file with format change

I have a file that looks something like this:

ATOM   7748  CG2 ILE A 999      53.647  54.338  82.768  1.00 82.10           C  
ATOM   7749  CD1 ILE A 999      51.224  54.016  84.367  1.00 83.16           C  
ATOM   7750  N   ASN A1000      55.338  57.542  83.643  1.00 80.67           N  
ATOM   7751  CA  ASN A1000      56.604  58.163  83.297  1.00 80.45           C  
ATOM   7752  C   ASN A1000      57.517  58.266  84.501  1.00 80.30           C  

As you can see the " " disappears between column 4 and 5 (starting to count at 0). Thus the code below fails. I'm new to python (total time now a whole 3 days!) and was wondering what's the best way to handle this. As long as there is a space, the line.split() works. Do I have to do a character count and then parse the string with an absolute reference?

import string
visited = {}
outputfile = open(file_output_location, "w")
for line in open(file_input_location, "r"):
    list = line.split()
    id = list[0]
    if id == "ATOM":
        type = list[2]
        if type == "CA":
            residue = list[3]
            if len(residue) == 4:
                residue = residue[1:]
            type_of_chain = list[4]
            atom_count = int(list[5])
            position = list[6:9]
            if(atom_count >= 1):
                if atom_count not in visited and type_of_chain == chain_required:
                    visited[atom_count] = 1
                    result_line = " ".join([residue,str(atom_count),type_of_chain," ".join(position)])
                    print result_line
                    print >>outputfile, result_line
outputfile.close()

Upvotes: 1

Views: 2458

Answers (3)

user1277476
user1277476

Reputation: 2909

Use string slicing:

print '0123456789'[3:6]
345

There's an asymmetry there - the first number is the 0-based index of the first character you need. The second number is the 0-based index of the first character you no longer need.

Upvotes: 1

Harpal
Harpal

Reputation: 12587

It may be worth installing Biopython as it has a module to Parse PDBs.

I used the following code on your example data:

from Bio.PDB.PDBParser import PDBParser

pdb_reader = PDBParser(PERMISSIVE=1)
structure_id="Test"
filename="Test.pdb" # Enter file name here or path to file.
structure = pdb_reader.get_structure(structure_id, filename)

model = structure[0]

for chain in model: # This will loop over every chain in Model
    for residue in chain:
        for atom in residue:
            if atom.get_name() == 'CA': # get_name strips spaces, use this over get_fullname() or get_id()
                print atom.get_id(), residue.get_resname(), residue.get_id()[1], chain.get_id(), atom.get_coord() 
                # Prints Atom Name, Residue Name, Residue number, Chain Name, Atom Co-Ordinates

This prints out:

CA ASN 1000 A [ 56.60400009  58.1629982   83.29699707]

I then tried it on a larger protein which has 14 chains (1aon.pdb) and it worked fine.

Upvotes: 0

John Gaines Jr.
John Gaines Jr.

Reputation: 11534

PDB files appear to be fixed column width files, not space delimited. So if you must parse them manually (rather than using an existing tool like pdb-tools), you'll need to chop the line up using something more along the lines of:

id = line[0:4]
type = line[4:9].strip()
# ad nausium

Upvotes: 1

Related Questions