-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomwrite.py
More file actions
130 lines (108 loc) · 4.02 KB
/
atomwrite.py
File metadata and controls
130 lines (108 loc) · 4.02 KB
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
#!/usr/bin/env python
#===== atomwrite.py =====
#Routines for writing configurational coordinates
#to popular molecular file formats.
import os
import numpy as np
import gzip
def MinImage(Pos, L):
"""Returns a new Pos array with minimum-imaged positions."""
return Pos - L * np.round_(Pos / L)
def PdbStr(Pos, L = None, AtomNames = ["C"], ModelNum = 1):
"""Gets a Pdb string.
Input:
Pos: (N,3) numpy array of atomic positions
L: scalar or vector of box lengths (or None to skip min-imaging)
AtomNames: list of atom names; duplicated as necessary to length N
"""
N = len(Pos)
#make a new AtomNames the same length as the position array
#by repeating it over and over
AtomNames = AtomNames * (round(N / len(AtomNames)) + 1)
AtomNames = AtomNames[:N]
#minimum image the positions
if not L is None:
Pos = MinImage(Pos, L)
#make the pdb header
s = "MODEL %-4i\n" % ModelNum
#add the coordinates to the pdb string
for (i, (x,y,z)) in enumerate(Pos):
an = AtomNames[i].strip()
s += "HETATM%5i %4s %3s %4i %8.3f%8.3f%8.3f %2s \n" % (
i+1, an.ljust(4)[:4], "SYS", i+1, x, y, z, an.rjust(2)[:2])
#make the footer
s += "TER\nENDMDL\n"
return s
def WritePdb(Filename, Pos, L = None, AtomNames = ["C"], First = False):
"""Writes a position array to a pdb file.
Input:
Filename: string filename of file to write
Pos: (N,3) numpy array of atomic positions
L: scalar or vector of box lengths (or None to skip min-imaging)
AtomNames: list of atom names; duplicated as necessary to length N
First: True will overwrite or start a new Pdb file; False will append
"""
#check to see if the file exists;
#if so, and we are appending, get the model number
ModelNum = 1
if not First and os.path.isfile(Filename):
for line in file(Filename, "r"):
if line.startswith("MODEL"):
ModelNum = int(line[10:].strip())
ModelNum += 1
#get the data
s = PdbStr(Pos, L, AtomNames, ModelNum)
#write to the file
if First:
file(Filename, "w").write(s)
else:
file(Filename, "a").write(s)
def write_csv_file(Pos, radius, FileName, step = 0, L = None, Compressed = False):
"""Write to a csv file. One file per configuration.
Input:
Filename: string filename prefix of file to write.
step: step of the simulation, to be appended to the filename.
AtomNames: list of atom names; duplicated as necessary to length N
Compressed: True will write to a gzipped file (default False)
"""
# Set file name
if Compressed:
FileName = FileName + "." + str(step) + ".csv.gz"
else:
FileName = FileName + "." + str(step) + ".csv"
# Minimum image the positions
if not L is None:
Pos = MinImage(Pos, L)
# Save atoms coordinates to csv file
np.savetxt(FileName, Pos, delimiter=', ')
return
class pdbfile:
def __init__(self, FileName, L = None, AtomNames = ["C"],
Compressed = False):
"""Creates a new class for writing to a pdb file.
Input:
Filename: string filename of file to write
L: scalar or vector of box lengths (or None to skip min-imaging)
AtomNames: list of atom names; duplicated as necessary to length N
Compressed: True will write to a gzipped file (default True)
"""
self.L = L
self.AtomNames = AtomNames
self.ModelNum = 1
if Compressed:
self.FileName = FileName + ".gz"
self.fobj = gzip.GzipFile(self.FileName, "w")
else:
self.FileName = FileName
self.fobj = open(self.FileName, "w")
def write(self, Pos):
"""Writes positions to a Pdb file.
Input:
Pos: (N,3) numpy array of atomic positions
"""
s = PdbStr(Pos, self.L, self.AtomNames, self.ModelNum)
self.fobj.write(s)
self.ModelNum += 1
def close(self):
"""Closes Pdb file object."""
self.fobj.close()