summaryrefslogtreecommitdiff
path: root/cad/src/tools/AMBER/SplitDotInFile.py
blob: 8d753502b92c1080462ff23111215cd7eea542fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python

# Copyright 2008 Nanorex, Inc.  See LICENSE file for details.

"""
SplitDotInFile.py

Splits an AMBER .in file containing residues with internal coordinates
into multiple .in_frag files, one for each residue.
"""

import sys

def parseInFile(fileName):
    file = open(fileName)
    lines = file.readlines()
    file.close()
    inFile = False
    nextIndex = -1
    line1 = line2 = line3 = line4 = line5 = line6 = ''
    for line in lines:

        line6 = line5
        line5 = line4
        line4 = line3
        line3 = line2
        line2 = line1
        line1 = line

        columns = line.strip().split()
        if (len(columns) < 10):
            if (inFile):
                # After the last line in a residue, there's a blank
                # line, which will get us here.
                nextIndex = -1
                inFile = False
                out.close()
            continue
        if (columns[0] == '1' and columns[1] == 'DUMM' and columns[2] == 'DU'):
            # This is the start of a new residue (dummy atoms for
            # defining the internal coordinate system).  Earlier lines
            # (saved in lineX) contain various info about the residue.
            # We extract short and long names to build the filename.
            fullName = line6.strip()
            fullName = fullName.replace(' ', '_')
            fullName = fullName.replace('*', '_x_')
            fullName = fullName.replace('/', '_s_')
            fullName = fullName.replace("'", '_p_')
            fullName = fullName.replace('[', '_os_')
            fullName = fullName.replace(']', '_cs_')
            fullName = fullName.replace('(', '_op_')
            fullName = fullName.replace(')', '_cp_')
            cols4 = line4.strip().split()
            shortName = cols4[0]
            newFileName = "%s_%s.in_frag" % (shortName, fullName)
            out = open(newFileName, 'w')
            nextIndex = 1
            inFile = True
            warned = False

        if (inFile):
            # Within the residue itself, column 0 is an index number
            # which usually increments sequentially.
            index = int(columns[0])
            igraph = columns[1]
            isymbl = columns[2]
            itree = columns[3]
            na = int(columns[4])
            nb = int(columns[5])
            nc = int(columns[6])
            r = float(columns[7])
            theta = float(columns[8])
            phi = float(columns[9])
            print >>out, "%3d %4s %2s %s %3d %3d %3d %10.3f %10.3f %10.3f" % (nextIndex, igraph, isymbl, itree, na, nb, nc, r, theta, phi)
            if (index != nextIndex and not warned):
                print "%s %s index not sequential" % (fileName, newFileName)
                warned = True
            nextIndex = nextIndex + 1


if (__name__ == '__main__'):
    parseInFile(sys.argv[1])