File: dx2cube.py

package info (click to toggle)
pdb2pqr 2.1.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 44,396 kB
  • ctags: 8,475
  • sloc: python: 44,146; cpp: 9,839; xml: 9,092; sh: 108; makefile: 50; ansic: 36
file content (177 lines) | stat: -rw-r--r-- 5,701 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import argparse

parser = argparse.ArgumentParser(description='Translate a dx file to cube format')
parser.add_argument('dx_input',
                   help='Name of the dx_input file (required arguement)')
parser.add_argument('pqr_input',
                   help='Name of the pqr_input file (required arguement)')
parser.add_argument('output',
                   help='Name of the output file (required arguement)')

args = parser.parse_args()  
counter = 0                 

#DX STUFF
if args.dx_input.endswith('.dx'):
    print "Success"
else:
    print "Error converting file"

try:
    with open(args.dx_input, 'r') as in_f, open(args.output, 'w') as out_f, open(args.pqr_input, 'r') as in_pqr:
        out_f.write("CPMD CUBE FILE.\n"
                    "OUTER LOOP: X, MIDDLE LOOP: Y, INNER LOOP: Z\n")
        
        #Discard comments at top of file.
        line = in_f.readline()
        newline = in_pqr.readline()
        while line.startswith('#'):
            line = in_f.readline()
        while newline.startswith('REMARK'):
            newline = in_pqr.readline()
        
        split_line = line.split()        
        grid_sizes = [int(x)*-1 for x in split_line[-3:]]
                    
        split_line = in_f.readline().split()
        
        origin = [float(x) for x in split_line[-3:]]
        
        parameter_fmt = "{:>4} {:>11.6f} {:>11.6f} {:>11.6f}\n"
        try:    
            while newline.startswith('ATOM'):
                pqr_line =  in_pqr.readline()
                new_split_line = pqr_line.split()
                atom_num = new_split_line[1]
        except IndexError:
            pass
        in_pqr.seek(0)
        newline = in_pqr.readline()
        while newline.startswith('REMARK'):
            newline = in_pqr.readline()
        
        origin_line = parameter_fmt.format(atom_num, *origin)
        out_f.write(origin_line)
        
        
        for x in xrange(3):
            split_line = in_f.readline().split()
            grid_dims = [float(item) for item in split_line[-3:]]
            
            dim_lin = parameter_fmt.format(grid_sizes[x], *grid_dims)
            out_f.write(dim_lin)
            
        atoms_parameter_fmt = "{:>4} {:>11.6f} {:>11.6f} {:>11.6f} {:>11.6f}\n"
        a = True
        xreal_center = []
        yreal_center = []
        zreal_center = []
        try:
            while a == True:
                if not newline.startswith('TER'):
                    new_split_line = newline.split()
                    radius = new_split_line[-1]
                    xyz = new_split_line[-5:-2]
                    line_atom_num = new_split_line[1]
                    atom_radius = new_split_line[-1]
                    pqr_lin = atoms_parameter_fmt.format(int(line_atom_num), float(new_split_line[-2]), float(xyz[0]), float(xyz[1]), float(xyz[2]))
                    out_f.write(pqr_lin)
                    newline = in_pqr.readline()
                    xreal_center.append(float(xyz[0]))
                    yreal_center.append(float(xyz[1]))
                    zreal_center.append(float(xyz[2]))                    
                else:
                    a = False
        except IndexError:
            pass
            
        x_avg = sum(xreal_center)/float(atom_num)
        y_avg = sum(yreal_center)/float(atom_num)
        z_avg = sum(zreal_center)/float(atom_num)       
        print x_avg, y_avg, z_avg
            
        #print origin
        #new_origin = []
        #for item in origin:
        #    newitem = item/0.529177
            #new_new = item/2 + newitem/2
        #    new_origin.append(newitem)
        #print new_origin

        #Consume unneeded object lines.
        in_f.readline()
        in_f.readline()
        
        ##TODO: put atoms here
        
        value_format = ["{:< 13.5E}"]
        value_format = ' '.join(value_format * 6) + '\n'
        print value_format 
        group = []
        line = in_f.readline()
        while not line.startswith('attribute'):
            values = [float(item) for item in line.split()]
            group.extend(values)
            
            if len(group) >= 6:
                out_f.write(value_format.format(*group))
                group = []
                
            line = in_f.readline()
            
        if group:
            group_strs = ["{:< 13.5E}".format(item) for item in group]
            out_f.write(' '.join(group_strs))
        
            

except IOError:
    print "file doesn't exist"
'''
for line in dx_data:
    dx_data_2 = dx_data
    data_point = line.strip("\n")
    dx_data_2.pop(0)
    dx_data._2append(data_point)
    

for line in dx_data_2:
    if line.startswith('#'):
        dx_data_2.remove(line)
        continue
    elif "gridpositions" in line:
        new_line = line.split()
        grid_values = new_line[-3:]
        #grid values for cube file, will need grid_valies[0]
        break
    elif line.startswith('delta'):
        line.insert('0')
        line.remove(delta)



#PQR STUFF
if args.pqr_input.endswith('.pqr'):
    print "yay"
else:
    print "nay"

try:
    with open(args.pqr_input, 'r') as f:
        pqr_data = f.readlines()
        
except IOError:
    print "file doesn't exist"
    
for line in pqr_data:
    data_point = line.strip("\n")
    pqr_data.pop(0)
    pqr_data.append(data_point)

for line in pqr_data:
    if line.startswith('REMARK'):
        pqr_data.remove(line)
        continue
    counter +=1
    #the number next to the origin in cube format
'''