160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306 | def parse_maya_file(maya_file):
""" Function to parse vHelix maya ascii file, extracting useful information about base """
objects = []
objects_by_full_name = dict()
helices = dict()
bases = []
aim_constraints = []
connections = []
""" Parse .ma file """
with open(maya_file) as fh:
linesBuffer = []
for line in fh:
words = line.split()
if words[0] == "createNode":
## Create a new object
if len(linesBuffer) > 0:
objType = linesBuffer[0].split()[1]
if objType == "vHelix":
h = MayaObj( linesBuffer )
helices[h.get_full_name()] = h
elif objType == "HelixBase":
bases.append( MayaBase( linesBuffer ) )
elif objType == "transform":
o = MayaObj( linesBuffer )
objects.append(o)
elif objType == "aimConstraint":
aim_constraints.append( MayaObj( linesBuffer ) )
## Clear lines buffer
linesBuffer = []
elif words[0] == "connectAttr":
connections.append(line)
## Extend lines buffer
linesBuffer.append(line)
""" Parse connections """
new_connections = []
base_dict = { b.name:b for b in bases }
for line in connections:
conn = ParseMayaConnection(line, base_dict)
if conn is not None:
new_connections.append(conn)
connections = new_connections
""" Assign base connectivity """
for c in connections:
h1 = c.helix1
bn1 = c.base1
s1 = c.suff1
h2 = c.helix2
bn2 = c.base2
s2 = c.suff2
b1 = helices[h1].children[bn1]
b2 = helices[h2].children[bn2]
if s1 == "lb" and s2 == "lb":
b1.add_basepair(b2)
elif s1 == "fw" and s2 == "bw":
b1.add_end5(b2)
elif s1 == "bw" and s2 == "fw":
b1.add_end3(b2)
else:
raise Exception("Unrecognized connection %s %s" % (s1,s2))
""" Find local basis of each base so that
model_from_basepair_stack_3prime can determine stacks """
ref_below_position = np.array((5.11, -0.4, -3.34))
ref_stack_position = np.array((-1.5, -4.7, 3.34))
ref_bp_position = np.array((0.042, -16.98, 0.0))
ref_bp_below_position = np.array((4.2, -12.9, 3.34))
ref_bp_stack_position = np.array((-4.42, -14.46, -3.34))
ref_positions = np.array((ref_below_position,
ref_stack_position,
ref_bp_position,
ref_bp_below_position,
ref_bp_stack_position))
shift = np.array((1.19, -3.52, 0.08))
if DEBUG:
write_pdb_psf(bases, 'before')
def update_geometry(b,beads):
""" Update the position and orientation of bead b using the 5
neighboring bases in "beads" as a reference """
def _get_beads_index(i):
j=0
while j <= i:
if beads[j] is None:
i+=1
j+=1
return j-1
""" Get position and filter out beads == None """
r0 = b.get_position()
tmp_ref_positions = ref_positions[[b2 is not None for b2 in beads]]
positions = np.array([b2.get_position()-r0 for b2 in beads if b2 is not None])
dist = np.linalg.norm(positions,axis=-1)
""" Discard neighboring bases that are very far from current base """
if np.any(dist > 20) and len(positions) > 3:
i = _get_beads_index(np.argmax(dist))
beads[i] = None
return update_geometry(b, beads)
""" Find transform that fits neighborhood around base to a reference """
R,comB,comA = minimizeRmsd(positions, tmp_ref_positions)
dr = np.array([R.T.dot(p-comB)-p0+comA for p,p0 in zip(positions, tmp_ref_positions)])
dr2 = (dr**2).sum(axis=-1)
msd = dr2.sum()/len(positions)
""" Discard neighboring bases that are very far from their expected position """
if msd > 17 and len(positions) > 3:
# print("WARNING:",b,msd)
# print(dr2)
i = _get_beads_index(np.argmax(dr2))
beads[i] = None
return update_geometry(b,beads)
""" Assign position and orientation to base """
if b.parent is None:
o_parent = np.eye(3)
else:
o_parent = b.parent.get_orientation()
b.orientation = o_parent.T.dot( R )
b.position = b.position + b.orientation.dot( shift )
""" Loop over bases updating their positions and orientations """
for b in bases:
if b.basepair is not None:
beads = [b.end5, b.end3, b.basepair, b.basepair.end5, b.basepair.end3]
update_geometry(b,beads)
if DEBUG:
write_pdb_psf(bases, 'after')
return bases
|