def make_equilibrium_pdb(trajectory, pdb, start=None, end=None): """ Calculates the equilibrium structure between start and end frames. Default: last half of trajectory. """ if start is None: start = trajectory.n_frame // 2 if end is None: end = trajectory.n_frame if start < 0 or start >= trajectory.n_frame: raise IndexError("Start frame out of range") if end < 0 or end > trajectory.n_frame+1: raise IndexError("End frame out of range") # sum_soup stores cumulative values sum_soup = trajectory.soup.copy() for a in sum_soup.atoms(): v3.set_vector(a.pos, 0, 0, 0) n_frame = 0 for i in range(start, end): trajectory.load_frame(i) n_frame += 1 for sum_atom, atom in zip( sum_soup.atoms(), trajectory.soup.atoms()): sum_atom.pos += atom.pos for a in sum_soup.atoms(): x = a.pos.x / float(n_frame) y = a.pos.y / float(n_frame) z = a.pos.z / float(n_frame) v3.set_vector(a.pos, x, y, z) trajectory.soup.write_pdb(pdb)
def make_equilibrium_pdb(trajectory, pdb, start=None, end=None): """ Calculates the equilibrium structure between start and end frames. Default: last half of trajectory. """ if start is None: start = trajectory.n_frame // 2 if end is None: end = trajectory.n_frame if start < 0 or start >= trajectory.n_frame: raise IndexError("Start frame out of range") if end < 0 or end > trajectory.n_frame + 1: raise IndexError("End frame out of range") # sum_soup stores cumulative values sum_soup = trajectory.soup.copy() for a in sum_soup.atoms(): v3.set_vector(a.pos, 0, 0, 0) n_frame = 0 for i in range(start, end): trajectory.load_frame(i) n_frame += 1 for sum_atom, atom in zip(sum_soup.atoms(), trajectory.soup.atoms()): sum_atom.pos += atom.pos for a in sum_soup.atoms(): x = a.pos.x / float(n_frame) y = a.pos.y / float(n_frame) z = a.pos.z / float(n_frame) v3.set_vector(a.pos, x, y, z) trajectory.soup.write_pdb(pdb)
def AtomFromPdbLine(line): """ Returns an Atom object from an atom line in a pdb file. """ atom = Atom() if line.startswith('HETATM'): atom.is_hetatm = True else: atom.is_hetatm = False atom.num = int(line[6:11]) atom.type = line[12:16].strip(" ") atom.alt_conform = line[16] atom.res_type = line[17:21].strip() atom.element = data.guess_element(atom.res_type, atom.type) atom.chain_id = line[21] atom.res_num = int(line[22:26]) atom.res_insert = line[26] if atom.res_insert == " ": atom.res_insert = "" x = float(line[30:38]) y = float(line[38:46]) z = float(line[46:54]) v3.set_vector(atom.pos, x, y, z) try: atom.occupancy = float(line[54:60]) except: atom.occupancy = 100.0 try: atom.bfactor = float(line[60:66]) except: atom.bfactor = 0.0 return atom
def apply(self, soup): residue = soup.residue(self.i_res) atoms = residue.atoms() n_chi = get_n_chi(residue) if self.mean_chis is None: self.mean_chis = [calculate_chi(residue, i) for i in range(n_chi)] rot_vels = [get_rot_vel_chi(residue, i) for i in range(n_chi)] for atom in atoms: v3.set_vector(atom.vel, 0.0, 0.0, 0.0) for i_chi in reversed(range(n_chi)): chi = calculate_chi(residue, i_chi) delta_chi = v3.normalize_angle(chi - self.mean_chis[i_chi]) target_rot_vel = get_random_chi_rot_vel(residue, i_chi, self.heating_temperature) if abs(delta_chi) > self.max_delta_chi: if delta_chi > self.max_delta_chi: target_rot_vel = -target_rot_vel else: if rot_vels[i_chi] < 0.0: target_rot_vel *= -target_rot_vel add_rot_vel_to_chi(residue, i_chi, target_rot_vel) anderson_velocity_scale(atoms, self.heating_temperature, 3 * len(atoms))
def apply(self, soup): residue = soup.residue(self.i_res) atoms = residue.atoms() n_chi = get_n_chi(residue) if self.mean_chis is None: self.mean_chis = [calculate_chi(residue, i) for i in range(n_chi)] rot_vels = [get_rot_vel_chi(residue, i) for i in range(n_chi)] for atom in atoms: v3.set_vector(atom.vel, 0.0, 0.0, 0.0) for i_chi in reversed(range(n_chi)): chi = calculate_chi(residue, i_chi) delta_chi = v3.normalize_angle(chi - self.mean_chis[i_chi]) target_rot_vel = get_random_chi_rot_vel( residue, i_chi, self.heating_temperature) if abs(delta_chi) > self.max_delta_chi: if delta_chi > self.max_delta_chi: target_rot_vel = -target_rot_vel else: if rot_vels[i_chi] < 0.0: target_rot_vel *= -target_rot_vel add_rot_vel_to_chi(residue, i_chi, target_rot_vel) anderson_velocity_scale(atoms, self.heating_temperature, 3*len(atoms))
def load_frame(self, i_frame): box, positions, velocities, forces = self.trr_reader[i_frame] for i, atom in enumerate(self.atoms): v3.set_vector(atom.pos, positions[i][0] * 10, positions[i][1] * 10, positions[i][2] * 10) v3.set_vector(atom.vel, velocities[i][0] * 10, velocities[i][1] * 10, velocities[i][2] * 10) self.i_frame = self.trr_reader.i_frame
def gas_randomize(atoms, temperature): """ Randomly assigns a velocity to atoms based on a Maxwellian distribution at temperature. """ for atom in atoms: v3.set_vector(atom.vel, maxwell_velocity(temperature, atom.mass), maxwell_velocity(temperature, atom.mass), maxwell_velocity(temperature, atom.mass))
def gas_randomize(atoms, temperature): """ Randomly assigns a velocity to atoms based on a Maxwellian distribution at temperature. """ for atom in atoms: v3.set_vector( atom.vel, maxwell_velocity(temperature, atom.mass), maxwell_velocity(temperature, atom.mass), maxwell_velocity(temperature, atom.mass))
def load_frame(self, i_frame): x, y, z = self.coor_dcd_reader[i_frame] atoms = self.soup.atoms() for i in range(len(atoms)): v3.set_vector(atoms[i].pos, x[i], y[i], z[i]) if self.vel_dcd_reader is not None: x, y, z = self.vel_dcd_reader[i_frame] for i in range(len(atoms)): v3.set_vector(atoms[i].vel, x[i], y[i], z[i]) self.i_frame = self.coor_dcd_reader.i_frame
def load_frame(self, i): # Load coordinates of soup with coordinates from self.trj_reader crds = self.trj_reader[i] vels = self.vel_trj_reader[i] if self.vel_trj_reader else None atoms = self.soup.atoms() for i in range(self.n_atom): atom = atoms[i] k = 3 * i v3.set_vector(atom.pos, crds[k], crds[k + 1], crds[k + 2]) if vels: v3.set_vector(atom.vel, vels[k], vels[k + 1], vels[k + 2]) self.i_frame = self.trj_reader.i_frame
def load_frame(self, i_frame): # Load coordinates of soup with coordinates from self.trj_reader crds = self.trj_reader[i_frame] vels = self.vel_trj_reader[i_frame] if self.vel_trj_reader else None atoms = self.soup.atoms() for i in range(self.n_atom): atom = atoms[i] k = 3*i v3.set_vector(atom.pos, crds[k], crds[k+1], crds[k+2]) if vels: v3.set_vector(atom.vel, vels[k], vels[k+1], vels[k+2]) self.i_frame = self.trj_reader.i_frame
def write_soup_to_crds_and_vels(in_soup, basename): """ From soup, writes out the coordinate/velocities, used for pulsing """ soup = in_soup.copy() convert_to_namd_atom_names(soup) coor = basename + '.coor' soup.write_pdb(coor) for atom in soup.atoms(): v3.set_vector(atom.pos, atom.vel[0], atom.vel[1], atom.vel[2]) vel = basename + '.vel' soup.write_pdb(vel) return coor, vel
def load_frame(self, i_frame): box, positions, velocities, forces = self.trr_reader[i_frame] for i, atom in enumerate(self.atoms): v3.set_vector( atom.pos, positions[i][0]*10, positions[i][1]*10, positions[i][2]*10) v3.set_vector( atom.vel, velocities[i][0]*10, velocities[i][1]*10, velocities[i][2]*10) self.i_frame = self.trr_reader.i_frame
def soup_from_restart_files(psf, in_coor, in_vel='', skip_solvent=False): """ Reads a Soup from restart files. """ soup = soup_from_psf(psf) coord_soup = pdbatoms.Soup(in_coor) for atom, coord_atom in zip(soup.atoms(), coord_soup.atoms()): p = coord_atom.pos v3.set_vector(atom.pos, p[0], p[1], p[2]) if in_vel: vel_soup = pdbatoms.Soup(in_vel) for atom, vel_atom in zip(soup.atoms(), vel_soup.atoms()): v = vel_atom.pos v3.set_vector(atom.vel, v[0], v[1], v[2]) return soup
def anderson_velocity_scale(atoms, temperature, n_degree_of_freedom): """ Scales the velocity of atoms such that average energy is consistent with the temperature. """ # This is the classic Anderson approach to temperature # regulation. Whilst deterministic, can be easily trapped in # local minima. target_energy = mean_energy(temperature, n_degree_of_freedom) kin = kinetic_energy(atoms) if v3.is_similar_mag(kin, 0): gas_randomize(atoms, temperature) else: scaling_factor = math.sqrt(target_energy / kin) for atom in atoms: v3.set_vector(atom.vel, v3.scale(atom.vel, scaling_factor))
def load_crd_or_rst_into_soup(soup, crd_or_rst): """ Loads the coordinates and velocities of .crd or .rst into the soup. """ f = open(crd_or_rst, "r") f.readline() # skip first line n_atom = int(f.readline().split()[0]) # calculate size of file based on field sizes n_crd = n_atom * 3 n_line = n_crd / 6 if n_crd % 6 > 0: n_line += 1 # read all the numbers in the coordinate section line_list = [f.readline()[:-1] for i in range(0, n_line)] s = "".join(line_list) vals = [float(s[i:i + 12]) for i in xrange(0, len(s), 12)] if len(vals) != n_crd: raise ValueError, "Improper number of coordinates in rst file." # load numbers into soup object for i, atom in enumerate(sorted(soup.atoms(), pdbatoms.cmp_atom)): v3.set_vector(atom.pos, vals[i * 3], vals[i * 3 + 1], vals[i * 3 + 2]) # if .rst file, then there will be velocity values if crd_or_rst.endswith('.rst'): line_list = [f.readline()[:-1] for i in range(0, n_line)] s = "".join(line_list) vals = [float(s[i:i + 12]) for i in xrange(0, len(s), 12)] if len(vals) != n_crd: raise ValueError, "Improper number of coordinates in rst file." # now convert amber velocities to angs/ps and load into soup convert_vel_to_angs_per_ps = 20.455 for i, atom in enumerate(sorted(soup.atoms(), pdbatoms.cmp_atom)): v3.set_vector(atom.vel, vals[i * 3], vals[i * 3 + 1], vals[i * 3 + 2]) atom.vel = v3.scale(atom.vel, convert_vel_to_angs_per_ps) f.close()
def load_crd_or_rst_into_soup(soup, crd_or_rst): """ Loads the coordinates and velocities of .crd or .rst into the soup. """ f = open(crd_or_rst, "r") f.readline() # skip first line n_atom = int(f.readline().split()[0]) # calculate size of file based on field sizes n_crd = n_atom * 3 n_line = n_crd / 6 if n_crd % 6 > 0: n_line += 1 # read all the numbers in the coordinate section line_list = [f.readline()[:-1] for i in range(0, n_line)] s = "".join(line_list) vals = [float(s[i:i+12]) for i in xrange(0, len(s), 12)] if len(vals) != n_crd: raise ValueError, "Improper number of coordinates in rst file." # load numbers into soup object for i, atom in enumerate(sorted(soup.atoms(), pdbatoms.cmp_atom)): v3.set_vector(atom.pos, vals[i*3], vals[i*3+1], vals[i*3+2]) # if .rst file, then there will be velocity values if crd_or_rst.endswith('.rst'): line_list = [f.readline()[:-1] for i in range(0, n_line)] s = "".join(line_list) vals = [float(s[i:i+12]) for i in xrange(0, len(s), 12)] if len(vals) != n_crd: raise ValueError, "Improper number of coordinates in rst file." # now convert amber velocities to angs/ps and load into soup convert_vel_to_angs_per_ps = 20.455 for i, atom in enumerate(sorted(soup.atoms(), pdbatoms.cmp_atom)): v3.set_vector(atom.vel, vals[i*3], vals[i*3+1], vals[i*3+2]) atom.vel = v3.scale(atom.vel, convert_vel_to_angs_per_ps) f.close()
def AtomFromGroLine(line): """ Returns an Atom object from a .gro atom line. """ atom = pdbatoms.Atom() atom.res_num = int(line[0:5]) atom.res_type = line[5:8].strip() atom.type = line[10:15].strip(" ") atom.element = data.guess_element(atom.res_type, line[12:15]) atom.num = int(line[15:20]) # 10 x multiplier converts from nm to angstroms x = 10.0 * float(line[20:28]) y = 10.0 * float(line[28:36]) z = 10.0 * float(line[36:44]) v3.set_vector(atom.pos, x, y, z) if len(line) > 62: # 10 x multiplier converts from nm to angstroms x = 10.0 * float(line[44:52]) y = 10.0 * float(line[52:60]) z = 10.0 * float(line[60:68]) v3.set_vector(atom.vel, x, y, z) return atom
def AtomFromGroLine(line): """ Returns an Atom object from a .gro atom line. """ atom = pdbatoms.Atom() atom.res_num = int(line[0:5]) atom.res_type = line[5:8].strip() atom.type = line[10:15].strip(" ") atom.element = data.guess_element( atom.res_type, line[12:15]) atom.num = int(line[15:20]) # 10 x multiplier converts from nm to angstroms x = 10.0*float(line[20:28]) y = 10.0*float(line[28:36]) z = 10.0*float(line[36:44]) v3.set_vector(atom.pos, x, y, z) if len(line) > 62: # 10 x multiplier converts from nm to angstroms x = 10.0*float(line[44:52]) y = 10.0*float(line[52:60]) z = 10.0*float(line[60:68]) v3.set_vector(atom.vel, x, y, z) return atom
def AtomFromPdbLine(line): """Returns an Atom object from an atom line in a pdb file.""" atom = Atom() if line.startswith('HETATM'): atom.is_hetatm = True else: atom.is_hetatm = False atom.num = int(line[6:11]) atom.type = line[12:16].strip(" ") element = '' for c in line[12:15]: if not c.isdigit() and c != " ": element += c if element[:2] in two_char_elements: atom.element = element[:2] else: atom.element = element[0] atom.res_type = line[17:20] atom.chain_id = line[21] atom.res_num = int(line[22:26]) atom.res_insert = line[26] if atom.res_insert == " ": atom.res_insert = "" x = float(line[30:38]) y = float(line[38:46]) z = float(line[46:54]) v3.set_vector(atom.pos, x, y, z) try: atom.occupancy = float(line[54:60]) except: atom.occupancy = 100.0 try: atom.bfactor = float(line[60:66]) except: atom.bfactor = 0.0 return atom
def transform(self, matrix): """ Transforms the pos vector by a v3.transform matrix. """ new_pos = v3.transform(matrix, self.pos) v3.set_vector(self.pos, new_pos)
def transform(self, matrix): new_pos = v3.transform(matrix, self.pos) v3.set_vector(self.pos, new_pos)