def COM(object): com_n = Vector(0,0,0) com_d = 0.0 for atom in object.get_atoms(): position = atom.get_vector() com_n += Vector(position._ar*np.array(atom.mass)) com_d += atom.mass com = com_n.__div__(com_d) return com
def analyse(input_file_name, refer_file_name, moved_chain_id, fixed_chain_id, r_moved_chain_id, r_fixed_chain_id, output_file1, output_file2, r_model_number=0): structure = PDBParser(PERMISSIVE=1).get_structure('to_analyse', input_file_name) reference = PDBParser(PERMISSIVE=1).get_structure('reference', refer_file_name) r_chain_moved = reference[r_model_number][r_moved_chain_id] r_chain_fixed = reference[r_model_number][r_fixed_chain_id] theta = [] phi = [] theta_x = [] theta_y = [] theta_z = [] d = [] coords_x = [] coords_y = [] coords_z = [] matrix_entries = [_[:] for _ in [[]] * 9] for model_number, model in enumerate(structure): chain_moved = structure[model_number][moved_chain_id] chain_fixed = structure[model_number][fixed_chain_id] com_denominator = 0.0 com_numerator = Vector(0, 0, 0) for atom in chain_moved.get_atoms(): position = atom.get_vector() com_numerator += Vector(position._ar * np.array(atom.mass)) com_denominator += atom.mass moved_centre = com_numerator.__div__(com_denominator) com_denominator = 0.0 com_numerator = Vector(0, 0, 0) for atom in chain_fixed.get_atoms(): position = atom.get_vector() com_numerator += Vector(position._ar * np.array(atom.mass)) com_denominator += atom.mass fixed_centre = com_numerator.__div__(com_denominator) com_denominator = 0.0 com_numerator = Vector(0, 0, 0) reference_set = np.asarray([ coord for coord in [atom.get_coord() for atom in r_chain_fixed.get_atoms()] ]) coordinate_set = np.asarray([ coord for coord in [atom.get_coord() for atom in chain_fixed.get_atoms()] ]) sup = SVDSuperimposer() sup.set(reference_set, coordinate_set) sup.run() R, V = sup.get_rotran() for atom in model.get_atoms(): atom.transform(R, V) for atom in chain_moved.get_atoms(): com_numerator += Vector( (atom.get_vector())._ar * np.array(atom.mass)) com_denominator += atom.mass moved_centre = com_numerator.__div__(com_denominator) com_denominator = 0.0 com_numerator = Vector(0, 0, 0) for atom in chain_fixed.get_atoms(): com_numerator += Vector( (atom.get_vector())._ar * np.array(atom.mass)) com_denominator += atom.mass fixed_centre = com_numerator.__div__(com_denominator) if fixed_centre.norm() > 0.5: print("Fixed chain norm is " + str(fixed_centre.norm()) + " in model " + str(model_number) + ". Should have been at the origin. Check code...") com_denominator = 0.0 com_numerator = Vector(0, 0, 0) x = moved_centre._ar[0] y = moved_centre._ar[1] z = moved_centre._ar[2] coords_x.append(x) coords_y.append(y) coords_z.append(z) d.append((moved_centre - fixed_centre).norm()) if moved_centre.norm() > 1e-6: theta.append(moved_centre.angle(Vector(0, 0, 1))) norm = np.sqrt(x * x + y * y) if norm > 1e-6: phi.append(np.arctan2(y, x)) else: theta.append(0.0) reference_set = np.asarray([ coord for coord in [atom.get_coord() for atom in r_chain_moved.get_atoms()] ]) coordinate_set = np.asarray([ coord for coord in [atom.get_coord() for atom in chain_moved.get_atoms()] ]) sup = SVDSuperimposer() sup.set(reference_set, coordinate_set) sup.run() R, V = sup.get_rotran() theta_x.append(np.arctan2(R[2][1], R[2][2])) theta_y.append( np.arctan2(-R[2][0], np.sqrt(R[2][1] * R[2][1] + R[2][2] * R[2][2]))) theta_z.append(np.arctan2(R[1][0], R[0][0])) for _ in range(3): matrix_entries[_].append(R[0][_]) matrix_entries[_ + 3].append(R[1][_]) matrix_entries[_ + 6].append(R[2][_]) f_results1 = open(output_file1, "w+") for frame in range(0, len(structure)): f_results1.write( str(frame) + '\t' + str(d[frame]) + '\t' + str(theta[frame]) + '\t' + str(phi[frame]) + '\t' + str(theta_x[frame]) + '\t' + str(theta_y[frame]) + '\t' + str(theta_z[frame]) + '\n') f_results1.close() f_results2 = open(output_file2, "w+") for frame in range(0, len(structure)): f_results2.write( str(frame) + '\t' + str(coords_x[frame]) + '\t' + str(coords_y[frame]) + '\t' + str(coords_z[frame]) + '\t') for _ in range(3): f_results2.write(str(matrix_entries[_][frame]) + '\t') f_results2.write(str(matrix_entries[_ + 3][frame]) + '\t') f_results2.write(str(matrix_entries[_ + 6][frame]) + '\t') f_results2.write('\n') f_results2.close()
def create_random_pdb(separation_distance, move_chain_id, fix_chain_id, input_file_name, output_pdb_name, model_number = 0): results = {} structure = PDBParser(PERMISSIVE=1).get_structure('whatever', input_file_name) chain_moved = structure[model_number][move_chain_id] chain_fixed = structure[model_number][fix_chain_id] old_fixed_centre = COM(chain_fixed) old_moved_centre = COM(chain_moved) com_denominator=0.0 com_numerator = Vector(0,0,0) for atom in chain_moved.get_atoms(): position = atom.get_vector() atom.set_coord(position - old_fixed_centre) #first step is to move origin to the com of fixed_chain. #So far the atoms in the moved_chain have been relocated. for atom in chain_fixed.get_atoms(): position = atom.get_vector() atom.set_coord(position - old_fixed_centre) #now fixed_chain has been relocated. All coordinates are now wrt com of fixed_chain moved_centre = old_moved_centre - old_fixed_centre fixed_centre = Vector(0,0,0) d = (old_fixed_centre - old_moved_centre).norm() results["1_Input_Separation"] = d results["1_Old_fixed_chain_com"]=old_fixed_centre results["1_Old_moved_chain_com"]= old_moved_centre results["0_Intended_Output_Separation"] = separation_distance R1 = generate_3d() R2 = generate_3d() max_distance = 0.0 com_numerator = Vector(0,0,0) com_denominator=0.0 #Now we scale the separation distance and also rotate the chain_moved for atom in chain_moved.get_atoms(): position = atom.get_vector() a = moved_centre.normalized()._ar * np.array(separation_distance) atom.set_coord((position - moved_centre).left_multiply(R2) + Vector(a)) max_distance = max(max_distance, (atom.get_vector().norm())) position = atom.get_vector() com_numerator += Vector(position._ar*np.array(atom.mass)) com_denominator +=atom.mass final_moved_centre = com_numerator.__div__(com_denominator) com_denominator=0.0 com_numerator = Vector(0,0,0) #Now we rotate the chain_fixed for atom in chain_fixed.get_atoms(): position = atom.get_vector() atom.set_coord(position.left_multiply(R1)) max_distance = max(max_distance, (atom.get_vector().norm())) position = atom.get_vector() com_numerator += Vector(position._ar*np.array(atom.mass)) com_denominator +=atom.mass final_fixed_centre = com_numerator.__div__(com_denominator) d = (final_fixed_centre - final_moved_centre).norm() w = PDBIO() w.set_structure(structure) w.save(output_pdb_name) results["2_Output_Separation"]=d results["2_fixed_chain_com"]=final_fixed_centre results["2_moved_chain_com"]=final_moved_centre results["Max_distance"]=max_distance return results