|
| 1 | +""" |
| 2 | +Distance between two center-of-masses |
| 3 | +""" |
| 4 | + |
| 5 | +from collections import deque |
| 6 | + |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from mdadash.backend.widgets.base import WidgetBase |
| 11 | + |
| 12 | + |
| 13 | +class COMDistance(WidgetBase): |
| 14 | + """COM Distance |
| 15 | +
|
| 16 | + Distance between two center-of-masses (COMs) |
| 17 | +
|
| 18 | + """ |
| 19 | + |
| 20 | + name = "COMDistance" |
| 21 | + description = "Distance between two COMs" |
| 22 | + |
| 23 | + _inputs = [ |
| 24 | + { |
| 25 | + "attribute": "selection1", |
| 26 | + "name": "Selection 1", |
| 27 | + "description": "First MDAnalysis selection phrase", |
| 28 | + "type": "str", |
| 29 | + "validations": ["required"], |
| 30 | + }, |
| 31 | + { |
| 32 | + "attribute": "selection2", |
| 33 | + "name": "Selection 2", |
| 34 | + "description": "Second MDAnalysis selection phrase", |
| 35 | + "type": "str", |
| 36 | + "validations": ["required"], |
| 37 | + }, |
| 38 | + { |
| 39 | + "attribute": "periodic", |
| 40 | + "name": "Periodic", |
| 41 | + "description": "Select with periodic boundary conditions", |
| 42 | + "type": "switch", |
| 43 | + }, |
| 44 | + { |
| 45 | + "attribute": "updating", |
| 46 | + "name": "Updating", |
| 47 | + "description": "Update selection during each timestep", |
| 48 | + "type": "switch", |
| 49 | + }, |
| 50 | + { |
| 51 | + "attribute": "custom_title", |
| 52 | + "name": "Custom title", |
| 53 | + "description": "Custom title for the plot", |
| 54 | + "type": "str", |
| 55 | + }, |
| 56 | + { |
| 57 | + "attribute": "maxlen", |
| 58 | + "name": "Max values", |
| 59 | + "description": "Max values to show in plot", |
| 60 | + "type": "int", |
| 61 | + }, |
| 62 | + { |
| 63 | + "attribute": "max_distance", |
| 64 | + "name": "Max distance", |
| 65 | + "description": "Max distance for alert check", |
| 66 | + "type": "int", |
| 67 | + }, |
| 68 | + { |
| 69 | + "attribute": "max_distance_alert", |
| 70 | + "name": "Alert if distance > 'Max distance'", |
| 71 | + "type": "switch", |
| 72 | + }, |
| 73 | + { |
| 74 | + "attribute": "x_type", |
| 75 | + "name": "X-axis", |
| 76 | + "type": "toggle", |
| 77 | + "options": [ |
| 78 | + {"name": "Step", "value": "step"}, |
| 79 | + {"name": "Time", "value": "time"}, |
| 80 | + ], |
| 81 | + }, |
| 82 | + ] |
| 83 | + |
| 84 | + def __init__(self): |
| 85 | + super().__init__() |
| 86 | + self.maxlen = 100 |
| 87 | + self.steps = deque(maxlen=self.maxlen) |
| 88 | + self.times = deque(maxlen=self.maxlen) |
| 89 | + self.y_values = deque(maxlen=self.maxlen) |
| 90 | + self.selection1 = "protein" |
| 91 | + self.selection2 = "resid 1" |
| 92 | + self.periodic = True |
| 93 | + self.updating = False |
| 94 | + self.ag1 = None |
| 95 | + self.ag2 = None |
| 96 | + self.title = "Distance between COMs" |
| 97 | + self.custom_title = None |
| 98 | + self.max_distance = 5 |
| 99 | + self.max_distance_alert = False |
| 100 | + self.x_type = "step" |
| 101 | + self.x_values = self.steps |
| 102 | + self.x_label = "Step" |
| 103 | + |
| 104 | + def _update_selections(self, s1=False, s2=False): |
| 105 | + """Update atom groups when selection phrases change""" |
| 106 | + if s1: |
| 107 | + self.ag1 = self.u.select_atoms( |
| 108 | + self.selection1, periodic=self.periodic, updating=self.updating |
| 109 | + ) |
| 110 | + if s2: |
| 111 | + self.ag2 = self.u.select_atoms( |
| 112 | + self.selection2, periodic=self.periodic, updating=self.updating |
| 113 | + ) |
| 114 | + self.title = f"{self.selection1} <-> {self.selection2}" |
| 115 | + |
| 116 | + def _set_x_values(self): |
| 117 | + """Set the values for the x-axis""" |
| 118 | + if self.x_type == "step": |
| 119 | + self.x_label = "Step" |
| 120 | + self.x_values = self.steps |
| 121 | + else: |
| 122 | + self.x_label = "Time (ps)" |
| 123 | + self.x_values = self.times |
| 124 | + |
| 125 | + def post_connect(self): |
| 126 | + """post_connect handler""" |
| 127 | + self._update_selections(s1=True, s2=True) |
| 128 | + |
| 129 | + def on_input_change(self, attribute, _old_value, new_value): |
| 130 | + """on_input_change handler""" |
| 131 | + reset_plot = False |
| 132 | + if attribute == "maxlen": |
| 133 | + reset_plot = True |
| 134 | + elif attribute == "x_type": |
| 135 | + self._set_x_values() |
| 136 | + elif attribute == "selection1": |
| 137 | + self._update_selections(s1=True) |
| 138 | + reset_plot = True |
| 139 | + elif attribute == "selection2": |
| 140 | + self._update_selections(s2=True) |
| 141 | + reset_plot = True |
| 142 | + elif attribute in ("periodic", "updating"): |
| 143 | + self._update_selections(s1=True, s2=True) |
| 144 | + if reset_plot: |
| 145 | + self.steps = deque(maxlen=self.maxlen) |
| 146 | + self.times = deque(maxlen=self.maxlen) |
| 147 | + self.y_values = deque(maxlen=self.maxlen) |
| 148 | + self._set_x_values() |
| 149 | + |
| 150 | + def run(self): |
| 151 | + """run handler""" |
| 152 | + com1 = self.ag1.center_of_mass() |
| 153 | + com2 = self.ag2.center_of_mass() |
| 154 | + self.y_values.append(np.linalg.norm(com1 - com2)) |
| 155 | + self.steps.append(self.u.trajectory.ts.data["step"]) |
| 156 | + self.times.append(self.u.trajectory.ts.data["time"]) |
| 157 | + plt.plot(self.x_values, self.y_values) |
| 158 | + plt.ylabel("Distance (Å)") |
| 159 | + plt.xlabel(self.x_label) |
| 160 | + plt.title(self.custom_title if self.custom_title else self.title) |
| 161 | + plt.grid(True) |
| 162 | + plt.show() |
0 commit comments