| 1 | window = 2 # number of adjacent frames to average
|
|---|
| 2 | stride = 1 # only use every Nth frame of trajectory (to save memory for large trjectories)
|
|---|
| 3 |
|
|---|
| 4 | from chimerax.atomic import Structure
|
|---|
| 5 | for s in session.models:
|
|---|
| 6 | if not isinstance(s, Structure) or s.num_coordsets == 1:
|
|---|
| 7 | continue
|
|---|
| 8 | session.logger.status("Smoothing trajectory %s" % s)
|
|---|
| 9 |
|
|---|
| 10 | session.logger.status("Gathering coordinates", secondary=True)
|
|---|
| 11 | if stride > 1:
|
|---|
| 12 | coordset_ids = s.coordset_ids
|
|---|
| 13 | coord_sets = [s.coordset(coordset_ids[i]).xyzs for i in range(0, len(coordset_ids), stride)]
|
|---|
| 14 | else:
|
|---|
| 15 | coord_sets = [s.coordset(cs_id).xyzs for cs_id in s.coordset_ids]
|
|---|
| 16 |
|
|---|
| 17 | session.logger.status("Computing smoothed coordinates", secondary=True)
|
|---|
| 18 | import numpy
|
|---|
| 19 | smoothed = numpy.zeros((len(coord_sets), len(coord_sets[0]), 3), type(coord_sets[0][0][0]))
|
|---|
| 20 | for i in range(len(coord_sets)):
|
|---|
| 21 | weight_tot = 0
|
|---|
| 22 | avg = smoothed[i]
|
|---|
| 23 | for j in range(i-window, i+window+1):
|
|---|
| 24 | if j < 0 or j >= len(coord_sets):
|
|---|
| 25 | continue
|
|---|
| 26 | weight = window + 1 - abs(i-j)
|
|---|
| 27 | weight_tot += weight
|
|---|
| 28 | avg += weight * coord_sets[j]
|
|---|
| 29 | avg /= weight_tot
|
|---|
| 30 |
|
|---|
| 31 | session.logger.status("Setting coordinates", secondary=True)
|
|---|
| 32 | s.add_coordsets(smoothed)
|
|---|
| 33 |
|
|---|
| 34 | session.logger.status("Smoothed trajectory %s" % s)
|
|---|