-
Notifications
You must be signed in to change notification settings - Fork 0
/
pos2pdb.py
82 lines (61 loc) · 2.22 KB
/
pos2pdb.py
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 python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 23 10:02:41 2019
@author: jiedeng
"""
#!/usr/local/bin/python
#Author: Jeff Houze
#Converts vasp's POSCAR file into a pdb file.
import re #regular expression package
datain = open('POTCAR', 'r')
potcar = datain.read()
datain.close()
p = re.compile(r"""VRHFIN\s=\S*:""") #raw triple quote string, for regexp.
symbols = p.findall(potcar) #list of strings containing atoms,
#and junk spliced out later.
whatfile = input("File to convert: ") #or "echo file | pos2pdb.py"
datain = open(whatfile, 'r')
datain.readline() #POSCAR comment line, useless to me.
scale=float(datain.readline()) #assuming ONE value
(a1, a2, a3) = datain.readline().split() #read in unit cell vectors,
(b1, b2, b3) = datain.readline().split() #but there are strings
(c1, c2, c3) = datain.readline().split()
a1=float(a1); a2=float(a2); a3=float(a3); #Now I have numbers.
b1=float(b1); b2=float(b2); b3=float(b3);
c1=float(c1); c2=float(c2); c3=float(c3);
atoms = datain.readline().split() #List containing how many of each.
tmp = datain.readline().split() #tmp need to figure out if this is...
#Selective dynamics, or not.
if (tmp[0][0] == 'S'):
SD = True
else:
SD = False
if (SD):
CorD = datain.readline().split()
else:
CorD = tmp
#filename="" #Building up descriptive filename.
#for i in range(len(atoms)):
# filename += symbols[i][8:-1]
#filename += ".pdb"
dataout = open('temp.pdb', 'w')
count = 0
for i in range(len(atoms)): #loop over different atoms
symbol = symbols[i][8:-1]
for j in range(int(atoms[i])): #loop over different posistions
nt=count+1
tmp = datain.readline().split()
a=float(tmp[0]); b=float(tmp[1]); c=float(tmp[2]);
if (CorD[0][0] == 'D'): #if DLV calculate cart. pos.
x = scale * (a1*a + b1*b + c1*c)
y = scale * (a2*a + b2*b + c2*c)
z = scale * (a3*a + b3*b + c3*c)
else: #are ready in cart.
x = a
y = b
z = c
S="ATOM %5d %2s MOL 1 %8.3f%8.3f%8.3f 1.00 0.00\n" % (count, symbol, float(x), float(y), float(z))
dataout.write(S)
datain.close()
dataout.close()