def test_subtract_quaternions(self): q1 = Quaternion(3.0, 1.0, -1.0, 0.0) q2 = Quaternion(2.0, 1.0, -1.0, 0.0) expected = Quaternion() assert expected == (q1 - q2)
def _axis(pose, length=2): """Axis primitive""" q = Quaternion(pose[3], pose[4], pose[5], pose[6]) bild = ".color cornflower blue" + os.linesep bild += _sphere(pose, 0.3) + os.linesep bild += ".color 1 0 0" + os.linesep c = [length, 0, 0] end = q.rotate(c) bild += ".arrow %f %f %f %f %f %f%s" % (pose[0], pose[1], pose[2], pose[0] + end[0], pose[1] + end[1], pose[2] + end[2], os.linesep) bild += ".color 1 1 0" + os.linesep c = [0, length, 0] end = q.rotate(c) bild += ".arrow %f %f %f %f %f %f%s" % (pose[0], pose[1], pose[2], pose[0] + end[0], pose[1] + end[1], pose[2] + end[2], os.linesep) bild += ".color 0 0 1" + os.linesep c = [0, 0, length] end = q.rotate(c) bild += ".arrow %f %f %f %f %f %f%s" % (pose[0], pose[1], pose[2], pose[0] + end[0], pose[1] + end[1], pose[2] + end[2], os.linesep) return bild
def test_slerp_same_quaternion(self): q1 = Quaternion(0.707106781, 0.0, 0.0, 0.707106781) q2 = Quaternion(0.707106781, 0.0, 0.0, 0.707106781) s = q1.slerp(q2, 0.1) assert s == q2
def test_lerp_t_1(self): q1 = Quaternion(1, 0, 0, 2) q2 = Quaternion(3, -1, 4, 3) s = q1.lerp(q2, 1.0) assert s == q2
def setUp(self): self.path = os.path.dirname(os.path.realpath(__file__)) self.test_path = self.path + '/scratch/' try: shutil.rmtree(self.test_path) except: pass os.mkdir(self.test_path) self.golden_data_path = os.path.normpath( os.path.dirname(os.path.realpath(__file__))) + '/golden_data/' self.quaternions = [ Quaternion(-0.122207449, -0.084565307, 0.804585130, 0.574940708), Quaternion(0.334565328, 0.706772732, 0.229644422, -0.579484069), Quaternion(-0.122207350, 0.541338054, 0.601216794, -0.574940729), Quaternion(-0.239073775, 0.739073825, -0.329056839, -0.536968536), Quaternion(-0.189936901, -0.272159950, -0.302264234, 0.893582267) ] self.translations = [ [-15.734048025344688, -16.449850861746139, 0.64470115806562056], [-28.314891508567385, 12.036561774174739, 10.178799979062202], [-13.844755498611772, -12.788839194052038, 23.000473566520469], [21.7408596820244, -3.0846843852631736, 22.997219312770568], [0.089556216428448465, -0.67458762985527621, -21.185751778087777] ]
def test_slerp_same_quaternion(self): q1 = Quaternion(0.707106781, 0.0, 0.0, 0.707106781) q2 = Quaternion(0.707106781, 0.0, 0.0, 0.707106781) s = q1.slerp(q2, 0.1) assert s == q2
def test_inverse(self): q1 = Quaternion(1, 0, 0, 2) q2 = Quaternion(3, -1, 4, 3) expected = Quaternion(-3.0 / 175, 9.0 / 175, -2.0 / 175, -9.0 / 175) assert expected == (q1 * q2).inverse()
def test_lerp_t_1(self): q1 = Quaternion(1, 0, 0, 2) q2 = Quaternion(3, -1, 4, 3) s = q1.lerp(q2, 1.0) assert s == q2
def test_add_quaternions(self): q1 = Quaternion(1.0, 1.0, -1.0, 2.0) q2 = Quaternion(1.0, 1.0, 3.0, 0.0) expected = Quaternion(2.0, 2.0, 2.0, 2.0) assert expected == (q1 + q2)
def __init__(self, id_swarm=0, id_glowworm=0, receptor_id=0, ligand_id=0, luciferin=0.0, num_neighbors=0, vision_range=0.0, pose=None, rmsd=-1.0, pdb_file='', contacts=0, scoring=0.0): self.id_swarm = id_swarm self.id_glowworm = id_glowworm self.receptor_id = receptor_id self.ligand_id = ligand_id self.luciferin = luciferin self.num_neighbors = num_neighbors self.vision_range = vision_range self.pose = pose self.translation = np.array(pose[:3]) self.rotation = Quaternion(pose[3], pose[4], pose[5], pose[6]).normalize() self.coord = DockingResult.pose_repr(pose) self.rmsd = rmsd self.pdb_file = pdb_file self.contacts = contacts self.scoring = scoring
def test_slerp_t_1(self): q1 = Quaternion(1, 0, 0, 2) q2 = Quaternion(3, -1, 4, 3) expected = Quaternion(0.50709255, -0.16903085, 0.67612340, 0.50709255) s = q1.slerp(q2, 1.0) assert expected == s
def test_slerp_t_0(self): q1 = Quaternion(1, 0, 0, 2) q2 = Quaternion(3, -1, 4, 3) expected = Quaternion(0.44721360, 0.000000, 0.000000, 0.89442719) s = q1.slerp(q2, 0.0) assert expected == s
def test_slerp_t_1(self): q1 = Quaternion(1, 0, 0, 2) q2 = Quaternion(3, -1, 4, 3) expected = Quaternion(0.50709255, -0.16903085, 0.67612340, 0.50709255) s = q1.slerp(q2, 1.0) assert expected == s
def test_slerp_t_0(self): q1 = Quaternion(1, 0, 0, 2) q2 = Quaternion(3, -1, 4, 3) expected = Quaternion(0.44721360, 0.000000, 0.000000, 0.89442719) s = q1.slerp(q2, 0.0) assert expected == s
def test_conjugate_and_multiplication(self): q1 = Quaternion(1, 0, 0, 2) q2 = Quaternion(3, -1, 4, 3) expected = Quaternion(35, 0, 0, 0) assert (q1*q2).conjugate() == q2.conjugate() * q1.conjugate() assert expected == q2.conjugate()*q2
def test_quaternion_from_vectors4(self): a = np.array([1., 0., 0.]) b = np.array([-1., 0., 0.]) q = quaternion_from_vectors(a, b) # Orthogonal rotation, two possibilities: e1 = Quaternion(w=0., x=0., y=1.0, z=0.) e2 = Quaternion(w=0., x=0., y=-1.0, z=0.) assert e1 == q or e2 == q
def test_rotation(self): q = Quaternion(0.707106781, 0.0, 0.707106781, 0.0) v = [1.0, 0.0, 0.0] v2 = q.rotate(v) assert_almost_equals(0.0, v2[0]) assert_almost_equals(0.0, v2[1]) assert_almost_equals(-1.0, v2[2])
def test_get_random(self): q1 = Quaternion.random() assert_almost_equals(1.0, q1.norm()) rng = MTGenerator(1379) q2 = Quaternion.random(rng) expected = Quaternion(0.53224081, -0.00589472, 0.84280352, 0.07979471) assert expected == q2
def test_slerp_t_half_y(self): q1 = Quaternion(1, 0, 0, 0) q2 = Quaternion(0, 0, 1, 0) s = q1.slerp(q2, 0.5) expected = Quaternion(0.707106781, 0.0, 0.707106781, 0.0) assert expected == s
def test_rotation(self): q = Quaternion(0.707106781, 0.0, 0.707106781, 0.0) v = [1.0, 0.0, 0.0] v2 = q.rotate(v) assert_almost_equals(0.0, v2[0]) assert_almost_equals(0.0, v2[1]) assert_almost_equals(-1.0, v2[2])
def test_conjugate(self): q1 = Quaternion(1, 3, 4, 3) q2 = Quaternion(1, -1, -1, 3) expected1 = Quaternion(1, -3, -4, -3) expected2 = Quaternion(1, 1, 1, -3) assert expected1 == q1.conjugate() assert expected2 == q2.conjugate()
def test_get_random(self): q1 = Quaternion.random() assert_almost_equals(1.0, q1.norm()) rng = MTGenerator(1379) q2 = Quaternion.random(rng) expected = Quaternion(0.53224081, -0.00589472, 0.84280352, 0.07979471) assert expected == q2
def test_slerp_t_half_bank_zero(self): q1 = Quaternion(0.707106781, 0.0, 0.0, 0.707106781) q2 = Quaternion(0.0, 0.707106781, 0.707106781, 0.0) s = q1.slerp(q2, 0.5) expected = Quaternion(0.5, 0.5, 0.5, 0.5) assert expected == s
def test_slerp_t_half_bank_zero(self): q1 = Quaternion(0.707106781, 0.0, 0.0, 0.707106781) q2 = Quaternion(0.0, 0.707106781, 0.707106781, 0.0) s = q1.slerp(q2, 0.5) expected = Quaternion(0.5, 0.5, 0.5, 0.5) assert expected == s
def test_slerp_t_half_y(self): q1 = Quaternion(1, 0, 0, 0) q2 = Quaternion(0, 0, 1, 0) s = q1.slerp(q2, 0.5) expected = Quaternion(0.707106781, 0.0, 0.707106781, 0.0) assert expected == s
def update_landscape_position(self, optimized_vector): """Updates the current pose""" self.translation = optimized_vector[:3] self.rotation = Quaternion(optimized_vector[3], optimized_vector[4], optimized_vector[5], optimized_vector[6]) self.rec_extent = optimized_vector[ 7:7 + self.num_rec_nmodes] if self.num_rec_nmodes > 0 else np.array([]) self.lig_extent = optimized_vector[ -self.num_lig_nmodes:] if self.num_lig_nmodes > 0 else np.array([])
def test_conjugate_and_multiplication(self): q1 = Quaternion(1, 0, 0, 2) q2 = Quaternion(3, -1, 4, 3) expected = Quaternion(35, 0, 0, 0) assert (q1 * q2).conjugate() == q2.conjugate() * q1.conjugate() assert expected == q2.conjugate() * q2
def test_quaternion_from_vectors4(self): a = np.array([1., 0., 0.]) b = np.array([-1., 0., 0.]) q = quaternion_from_vectors(a, b) # Orthogonal rotation e = Quaternion(w=0., x=0.00000000, y=-1.0, z=0.) assert e == q
def test_quaternion_from_vectors3(self): a = np.array([1., 0., 0.]) b = np.array([0., 2., 0.]) q = quaternion_from_vectors(a, b) # 90 degrees in Z axis e = Quaternion(w=0.70710678, x=0.00000000, y=0.00000000, z=0.70710678) assert e == q
def test_get_quaternion_for_restraint2d_different_quadrant(self): # Origin is 0,0,0 # Ligand center l_center = [5., -5., 0.] # Ligand residue l_res = [7., -7., 0.] # Receptor residue r_res = [-3., 1., 0.] # Calculate quaternion for rotation from l_res to point to r_res rec_residue = Residue.dummy(r_res[0], r_res[1], r_res[2]) lig_residue = Residue.dummy(l_res[0], l_res[1], l_res[2]) q = get_quaternion_for_restraint( rec_residue, lig_residue, l_center[0], l_center[1], l_center[2], # translation [0., 0., 0.], # receptor translation [5., -5., 0.] # ligand translation ) e = Quaternion(0.07088902, 0., 0., -0.99748421) assert e == q
def test_get_quaternion_for_restraint2d(self): # Origin is 0,0,0 # Ligand center l_center = [5., 5., 0.] # Ligand residue l_res = [7., 7., 0.] # Receptor residue r_res = [3., 1., 0.] # Calculate quaternion for rotation from l_res to point to r_res rec_residue = Residue.dummy(r_res[0], r_res[1], r_res[2]) lig_residue = Residue.dummy(l_res[0], l_res[1], l_res[2]) q = get_quaternion_for_restraint( rec_residue, lig_residue, l_center[0], l_center[1], l_center[2], # translation [0., 0., 0.], # receptor translation [5., 5., 0.] # ligand translation ) e = Quaternion(0.16018224, 0., 0., -0.98708746) assert e == q
def test_get_quaternion_for_restraint2(self): # Origin is 0,0,0 # Ligand center l_center = [-5., 5., -5.] # Ligand residue l_res = [-7., 6., -7.] # Receptor residue r_res = [3., 1., 0.] # Calculate quaternion for rotation from l_res to point to r_res rec_residue = Residue.dummy(r_res[0], r_res[1], r_res[2]) lig_residue = Residue.dummy(l_res[0], l_res[1], l_res[2]) q = get_quaternion_for_restraint( rec_residue, lig_residue, l_center[0], l_center[1], l_center[2], # translation [0., 0., 0.], # receptor translation [5., -5., 5.] # ligand translation ) e = Quaternion(0.10977233, -0.44451098, -0.88902195, 0.) assert e == q
def parse_output_file(lightdock_output): """Parses a lightdock output file to get relevant simulation information for each glowworm""" translations = [] rotations = [] receptor_ids = [] ligand_ids = [] rec_extents = [] lig_extents = [] data_file = open(lightdock_output) lines = data_file.readlines() data_file.close() counter = 0 for line in lines: if line[0] == '(': counter += 1 last = line.index(')') coord = line[1:last].split(',') translations.append([float(coord[0]),float(coord[1]),float(coord[2])]) rotations.append(Quaternion(float(coord[3]),float(coord[4]),float(coord[5]),float(coord[6]))) if len(coord) == (7 + DEFAULT_NMODES_REC + DEFAULT_NMODES_LIG): rec_extents.append(np.array([float(x) for x in coord[7:7+DEFAULT_NMODES_REC]])) lig_extents.append(np.array([float(x) for x in coord[-DEFAULT_NMODES_LIG:]])) raw_data = line[last+1:].split() receptor_id = int(raw_data[0]) ligand_id = int(raw_data[1]) receptor_ids.append(receptor_id) ligand_ids.append(ligand_id) log.info("Read %s coordinate lines" % counter) return translations, rotations, receptor_ids, ligand_ids, rec_extents, lig_extents
def parse_output_file(lightdock_output): translations = [] rotations = [] luciferin = [] neighbors = [] vision_range = [] scoring = [] data_file = open(lightdock_output) lines = [line.rstrip(os.linesep) for line in data_file.readlines()] data_file.close() counter = 0 for line in lines: if line[0] == '(': counter += 1 last = line.index(')') coord = line[1:last].split(',') translations.append( [float(coord[0]), float(coord[1]), float(coord[2])]) rotations.append( Quaternion(float(coord[3]), float(coord[4]), float(coord[5]), float(coord[6]))) values = line[last + 1:].split() luciferin.append(float(values[0])) neighbors.append(int(values[1])) vision_range.append(float(values[2])) scoring.append(float(values[3])) log.info("Read %s coordinate lines" % counter) return translations, rotations, luciferin, neighbors, vision_range, scoring
def test_get_quaternion_for_restraint1(self): # Origin is 0,0,0 # Ligand center l_center = [-5., 5., -5.] # Ligand residue l_res = [-7., 7., -7.] # Receptor residue r_res = [3., 1., 0.] # Calculate quaternion for rotation from l_res to point to r_res rec_residue = Residue.dummy(r_res[0], r_res[1], r_res[2]) lig_residue = Residue.dummy(l_res[0], l_res[1], l_res[2]) q = get_quaternion_for_restraint( rec_residue, lig_residue, l_center[0], l_center[1], l_center[2], # translation [0., 0., 0.], # receptor translation [5., -5., 5.] # ligand translation ) e = Quaternion(w=0.14518697, x=0.19403814, y=-0.58211441, z=-0.77615254) assert e == q
def parse_output_file(lightdock_output, glowworm_id, num_anm_rec, num_anm_lig): """Parses a LightDock simulation output file and returns only data for given glowworm_id""" with open(lightdock_output) as data_file: lines = data_file.readlines() num_glowworms = 0 for line in lines: line = line.rstrip(os.linesep) if line.startswith('('): if num_glowworms == glowworm_id: last = line.index(')') coord = line[1:last].split(',') translation = [ float(coord[0]), float(coord[1]), float(coord[2]) ] rotation = Quaternion(float(coord[3]), float(coord[4]), float(coord[5]), float(coord[6])) rec_extent = None lig_extent = None if len(coord) > 7: rec_extent = np.array( [float(x) for x in coord[7:7 + num_anm_rec]]) lig_extent = np.array( [float(x) for x in coord[-num_anm_lig:]]) return translation, rotation, rec_extent, lig_extent num_glowworms += 1 return None
def parse_output_file(lightdock_output, num_anm_rec, num_anm_lig): translations = [] rotations = [] receptor_ids = [] ligand_ids = [] rec_extents = [] lig_extents = [] data_file = open(lightdock_output) lines = data_file.readlines() data_file.close() counter = 0 for line in lines: if line[0] == '(': counter += 1 last = line.index(')') coord = line[1:last].split(',') translations.append([float(coord[0]), float(coord[1]), float(coord[2])]) rotations.append(Quaternion(float(coord[3]), float(coord[4]), float(coord[5]), float(coord[6]))) if len(coord) > 7: rec_extents.append(np.array([float(x) for x in coord[7:7+num_anm_rec]])) lig_extents.append(np.array([float(x) for x in coord[-num_anm_lig:]])) raw_data = line[last+1:].split() receptor_id = int(raw_data[0]) ligand_id = int(raw_data[1]) receptor_ids.append(receptor_id) ligand_ids.append(ligand_id) log.info("Read %s coordinate lines" % counter) return translations, rotations, receptor_ids, ligand_ids, rec_extents, lig_extents
def test_move_step_rot_half_step_trans_half(self): atoms, residues, chains = parse_complex_from_file( self.golden_data_path + '1PPElig.pdb') ligand = Complex(chains, atoms) adapter = MJ3hAdapter(self.receptor, ligand) scoring_function = MJ3h() coordinates1 = Coordinates([0., 0., 0., 1., 0., 0., 0.]) landscape_position1 = DockingLandscapePosition(scoring_function, coordinates1, adapter.receptor_model, adapter.ligand_model, step_translation=5.0, step_rotation=0.5) adapter2 = MJ3hAdapter(self.receptor, ligand) coordinates2 = Coordinates([10., 0., 0., 0., 0., 1., 0.]) landscape_position2 = DockingLandscapePosition(scoring_function, coordinates2, adapter2.receptor_model, adapter2.ligand_model) landscape_position1.move(landscape_position2) expected_translation = np.array([5., 0., 0.]) expected_rotation = Quaternion(0.707106781, 0.0, 0.707106781, 0.0) assert (expected_translation == landscape_position1.translation).all() assert expected_rotation == landscape_position1.rotation
def parse_file(file_name, glowworm_id): """Parses a given GSO step output file. Returns the translation, the quaternion and the normal modes (if any) """ try: lines = open(file_name).readlines()[1:] except IOError: return None for line_id, line in enumerate(lines): if line_id == glowworm_id: if line[0] == '(': coord = line[1:].split(')')[0].split(',') translation = [ float(coord[0]), float(coord[1]), float(coord[2]) ] q = Quaternion(float(coord[3]), float(coord[4]), float(coord[5]), float(coord[6])) nm_rec = None nm_lig = None if len(coord) > 7: nm_rec = np.array( [float(x) for x in coord[7:7 + DEFAULT_NMODES_REC]]) nm_lig = np.array( [float(x) for x in coord[7:7 + DEFAULT_NMODES_LIG]]) return translation, q, nm_rec, nm_lig return None
def test_conjugate(self): q1 = Quaternion(1, 3, 4, 3) q2 = Quaternion(1, -1, -1, 3) expected1 = Quaternion(1, -3, -4, -3) expected2 = Quaternion(1, 1, 1, -3) assert expected1 == q1.conjugate() assert expected2 == q2.conjugate()
def _axis(pose, length=2): """Axis primitive""" q = Quaternion(pose[3], pose[4], pose[5], pose[6]) bild = ".color cornflower blue" + os.linesep bild += _sphere(pose, 0.3) + os.linesep bild += ".color 1 0 0" + os.linesep c = [length, 0, 0] end = q.rotate(c) bild += ".arrow %f %f %f %f %f %f%s" % (pose[0], pose[1], pose[2], pose[0] + end[0], pose[1] + end[1], pose[2] + end[2], os.linesep) bild += ".color 1 1 0" + os.linesep c = [0, length, 0] end = q.rotate(c) bild += ".arrow %f %f %f %f %f %f%s" % (pose[0], pose[1], pose[2], pose[0] + end[0], pose[1] + end[1], pose[2] + end[2], os.linesep) bild += ".color 0 0 1" + os.linesep c = [0, 0, length] end = q.rotate(c) bild += ".arrow %f %f %f %f %f %f%s" % (pose[0], pose[1], pose[2], pose[0] + end[0], pose[1] + end[1], pose[2] + end[2], os.linesep) return bild
class DockingResult(object): """Represents a LightDock docking result line""" def __init__(self, id_cluster=0, id_glowworm=0, receptor_id=0, ligand_id=0, luciferin=0.0, num_neighbors=0, vision_range=0.0, pose=None, rmsd=-1.0, pdb_file='', contacts=0, scoring=0.0): self.id_cluster = id_cluster self.id_glowworm = id_glowworm self.receptor_id = receptor_id self.ligand_id = ligand_id self.luciferin = luciferin self.num_neighbors = num_neighbors self.vision_range = vision_range self.pose = pose self.translation = np.array(pose[:3]) self.rotation = Quaternion(pose[3], pose[4], pose[5], pose[6]).normalize() self.coord = DockingResult.pose_repr(pose) self.rmsd = rmsd self.pdb_file = pdb_file self.contacts = contacts self.scoring = scoring def __str__(self): return "%5d %6d %60s %6d %6d %11.5f %5d %7.3f %8.3f %16s %6d %8.3f" % (self.id_cluster, self.id_glowworm, self.coord, self.receptor_id, self.ligand_id, self.luciferin, self.num_neighbors, self.vision_range, self.rmsd, self.pdb_file, self.contacts, self.scoring) def distance_trans(self, other): return np.linalg.norm(self.translation - other.translation) def distance_rot(self, other): return self.rotation.distance(other.rotation) @staticmethod def pose_repr(coord): fields = [("%5.3f" % c) for c in coord] return "(%s)" % (', '.join(fields))
def __init__(self, id_cluster=0, id_glowworm=0, receptor_id=0, ligand_id=0, luciferin=0.0, num_neighbors=0, vision_range=0.0, pose=None, rmsd=-1.0, pdb_file='', contacts=0, scoring=0.0): self.id_cluster = id_cluster self.id_glowworm = id_glowworm self.receptor_id = receptor_id self.ligand_id = ligand_id self.luciferin = luciferin self.num_neighbors = num_neighbors self.vision_range = vision_range self.pose = pose self.translation = np.array(pose[:3]) self.rotation = Quaternion(pose[3], pose[4], pose[5], pose[6]).normalize() self.coord = DockingResult.pose_repr(pose) self.rmsd = rmsd self.pdb_file = pdb_file self.contacts = contacts self.scoring = scoring
def test_copy_quaternions(self): q1 = Quaternion(1.0, 2.0, -1.0, 3.0) q2 = q1.clone() assert q1 == q2 q2.w = 3.0 assert q1 != q2
def test_distance_composite_rotation(self): q1 = Quaternion(1.0, 0, 0, 0) q2 = Quaternion(0.5, 0.5, 0.5, 0.5) assert_almost_equals(0.75, q1.distance(q2))
def test_distance_is_half(self): q1 = Quaternion(0.707106781, 0.0, 0.707106781, 0.0) q2 = Quaternion(0, 0, 1.0, 0) assert_almost_equals(0.5, q1.distance(q2))
def test_distance_is_one(self): q1 = Quaternion(0.707106781, 0.0, 0.707106781, 0.0) q2 = Quaternion(0.707106781, 0.0, -0.707106781, 0.0) assert_almost_equals(1.0, q1.distance(q2))
def test_distance_is_zero(self): q = Quaternion(0.707106781, 0.0, 0.707106781, 0.0) assert_almost_equals(0.0, q.distance(q))
def test_norm(self): q1 = Quaternion(1, -3, 4, 3) q2 = Quaternion(3, -1, 4, 3) assert_almost_equals(5.91607978, q1.norm()) assert_almost_equals((q1*q2).norm(), q1.norm()*q2.norm())
def test_dot_product(self): q = Quaternion(0.707106781, 0.0, 0.707106781, 0.0) assert_almost_equals(1., q.dot(q))