Statistiques
| Révision :

root / ase / io / pdb.py @ 11

Historique | Voir | Annoter | Télécharger (1,84 ko)

1
import numpy as np 
2

    
3
from ase.atoms import Atom, Atoms
4
from ase.parallel import paropen
5

    
6
"""Module to read and write atoms in PDB file format"""
7

    
8

    
9
def read_pdb(fileobj, index=-1):
10
    """Read PDB files.
11

12
    The format is assumed to follow the description given in
13
    http://www.wwpdb.org/documentation/format32/sect9.html."""
14
    if isinstance(fileobj, str):
15
        fileobj = open(fileobj)
16

    
17
    atoms = Atoms()
18
    for line in fileobj.readlines():
19
        if line.startswith('ATOM') or line.startswith('HETATM'):
20
            try:
21
                symbol = line[12:16].strip()
22
                # we assume that the second character is a label 
23
                # in case that it is upper case
24
                if len(symbol) > 1 and symbol[1].isupper():
25
                    symbol = symbol[0]
26
                words = line[30:55].split()
27
                position = np.array([float(words[0]), 
28
                                     float(words[1]),
29
                                     float(words[2])])
30
                atoms.append(Atom(symbol, position))
31
            except:
32
                pass
33

    
34
    return atoms
35

    
36
def write_pdb(fileobj, images):
37
    """Write images to PDB-file."""
38
    if isinstance(fileobj, str):
39
        fileobj = paropen(fileobj, 'w')
40

    
41
    if not isinstance(images, (list, tuple)):
42
        images = [images]
43

    
44
    format = 'ATOM  %5d %-4s              %8.3f%8.3f%8.3f  0.00  0.00\n'
45

    
46
    # RasMol complains if the atom index exceeds 100000. There might
47
    # be a limit of 5 digit numbers in this field.
48
    MAXNUM = 100000
49

    
50
    symbols = images[0].get_chemical_symbols()
51
    natoms = len(symbols)
52
    
53
    for atoms in images:
54
        fileobj.write('MODEL         1\n')
55
        p = atoms.get_positions()
56
        for a in range(natoms):
57
            x, y, z = p[a]
58
            fileobj.write(format % (a % MAXNUM, symbols[a], x, y, z))
59
        fileobj.write('ENDMDL\n')