-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaders.py
128 lines (103 loc) · 4.03 KB
/
readers.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
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
"""
Reads oxDNA configuration and topology files.
readers.py includes the class: LorenzoReader
"""
import base
try:
import numpy as np
except:
import mynumpy as np
import os.path
import sys
class LorenzoReader:
"""
Reader for oxDNA configurations and topologies.
Args:
configuration: oxDNA configuration/trajectory file
topology: oxDNA topology file
check_overlap: boolean. If true, check for nucleotide overlaps in the configuration.
"""
def __init__(self, configuration, topology, check_overlap=False):
self._conf = False
if not os.path.isfile(configuration):
base.Logger.die("Configuration file '%s' is not readable" % configuration)
if not os.path.isfile(topology):
base.Logger.die("Topology file '%s' is not readable" % topology)
self._check_overlap = check_overlap
self._conf = open(configuration, "r")
f = open(topology, "r")
f.readline()
self._top_lines = f.readlines()
def __del__(self):
if self._conf: self._conf.close()
def _read(self, only_strand_ends=False, skip=False):
timeline = self._conf.readline()
time = 0.
if len(timeline) == 0:
return False
else:
time = float(timeline.split()[2])
box = np.array([float(x) for x in self._conf.readline().split()[2:]])
[E_tot, E_pot, E_kin] = [float(x) for x in self._conf.readline().split()[2:5]]
if skip:
for tl in self._top_lines:
self._conf.readline()
return False
system = base.System(box, time=time, E_pot=E_pot, E_kin=E_kin)
base.Nucleotide.index = 0
base.Strand.index = 0
s = False
strandid_current = 0
for tl in self._top_lines:
tls = tl.split()
n3 = int(tls[2])
n5 = int(tls[3])
strandid = int(tls[0])
if (len (tls[1]) == 1):
b = base.base_to_number[tls[1]]
bb = b
else:
try:
tmp = int (tls[1])
except:
raise ValueError ("problems in topology file with specific base pairing")
if tmp > 0:
b = tmp % 4
else:
b = (3 - ((3 - tmp) % 4))
bb = tmp
if strandid != strandid_current:
# check for circular strand
if n3 != -1:
iscircular = True
else:
iscircular = False
if s:
system.add_strand(s, self._check_overlap)
s = base.Strand()
if iscircular:
s.make_circular()
strandid_current = strandid
ls = self._conf.readline().split()
cm = [float(x) for x in ls[0:3]]
a1 = [float(x) for x in ls[3:6]]
a3 = [float(x) for x in ls[6:9]]
v = [float(x) for x in ls[9:12]]
L = [float(x) for x in ls[12:15]]
if not only_strand_ends or n3 == -1 or n5 == -1:
s.add_nucleotide(base.Nucleotide(cm, a1, a3, b, bb, v, L, n3))
system.add_strand(s, self._check_overlap)
return system
# if only_strand_ends == True then a strand will contain only the first and the last nucleotide
# useful for some analysis like csd for coaxial interactions
def get_system(self, only_strand_ends=False, N_skip=0):
"""
Returns a base.System object by reading one configuration from the given configuration/trajectory file.
After reaching the end of the configuration file, returns False.
Args:
only_strand_ends: boolean. If true, a strand will contain only the first and the last nucleotide.
N_skip: number of configurations to skip in the trajectory file
"""
for i in range(N_skip):
self._read(skip=True)
return self._read(only_strand_ends=only_strand_ends, skip=False)