def rotate(degree, point_x, point_y): radians = math.radians(degree) x = point_x.x - point_y.x y = point_x.y - point_y.y temp_x = x*math.cos(radians) - y*math.sin(radians) temp_y = x*math.sin(radians) + y*math.cos(radians) return Point(temp_x+point_y.x,temp_y+point_y.y,point_y.z)
def rotate240(point_x, point_y): ''' point_x rotate about point_y 240 degree ''' sin240 = -math.sqrt(3) / 2 cos240 = -0.5 pointz = Point(point_y.x, point_y.y, point_y.z) newpoint, magnitude = unit(pointz) x = (cos240+newpoint.x**2*(1-cos240)) * point_x.x +\ (newpoint.x*newpoint.y*(1-cos240) - newpoint.z*sin240) * point_x.y +\ (newpoint.x*newpoint.z*(1-cos240) + newpoint.y*sin240) * point_x.z y = (newpoint.y*newpoint.x*(1-cos240) + newpoint.z*sin240) * point_x.x +\ (cos240 + newpoint.y**2*(1-cos240)) * point_x.y +\ (newpoint.y*newpoint.z*(1-cos240) - newpoint.x*sin240) * point_x.z z = (newpoint.z*newpoint.x*(1-cos240) - newpoint.y*sin240) * point_x.x +\ (newpoint.z*newpoint.y*(1-cos240) + newpoint.x*sin240) * point_x.y +\ (cos240 + newpoint.z**2*(1-cos240))*point_x.z result_point = Point(x, y, z) return result_point
def plot(): f = open("points","r") content = f.read().splitlines() point_1_list = content[0].split() Point_1 = Point(float(point_1_list[0]),float(point_1_list[1]),float(point_1_list[2]),int(point_1_list[3])) Point_1.setFather(1) Point_1.editIndex(1) old_point_2_list = content[1].split() old_Point_2 = Point(float(old_point_2_list[0]),float(old_point_2_list[1]),float(old_point_2_list[2]),int(old_point_2_list[3])) global R_radius R_radius = Point_1.z d_radius = distance(old_Point_2,Point_1) Point_2 = Point(d_radius,0,R_radius,2) Point_3 = rotate120(Point_2,Point_1) Point_4 = rotate240(Point_2,Point_1) Point_2.setFather(1) Point_3.setFather(1) Point_4.setFather(1) Point_2.editIndex(2) Point_3.editIndex(3) Point_4.editIndex(4) index_list = [] for i in content: index_list.append(int(i.split()[3])) dict_points = { 1:Point_1, 2:Point_2, 3:Point_3, 4:Point_4, } for i in dict_points.keys(): index_list.remove(i) index_list.reverse() while index_list: temp = index_list.pop() # 1,2,5,6 if temp%2: temp_father = (temp-1)/2 temp_grandfather = dict_points[temp_father].getFather() dict_points[temp] = rotate(120,dict_points[temp_grandfather],dict_points[temp_father]) else: temp_father = (temp-2)/2 temp_grandfather = dict_points[temp_father].getFather() dict_points[temp] = rotate(240,dict_points[temp_grandfather],dict_points[temp_father]) dict_points[temp].setFather(temp_father) dict_points[temp].editIndex(temp) # global R_radius dict_points[temp].z = R_radius T_x_points_array = [] T_y_points_array = [] w = open("points_2D","w") for i in sorted(dict_points.keys()): w.write(str(dict_points[i])) w.write("\n") temp_father = dict_points[i].getFather() T_x_points_array.append(dict_points[temp_father]) T_y_points_array.append(dict_points[i]) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # linesPlot.drawLines(T_x_points_array,T_y_points_array) for i in range(len(T_y_points_array)): ax.plot([T_x_points_array[i].x,T_y_points_array[i].x],\ [T_x_points_array[i].y,T_y_points_array[i].y],\ zs = [T_x_points_array[i].z,T_y_points_array[i].z],\ c = "b") plt.savefig('lines_plot_2D.png') plt.show()
def main(): print 'The default filename is points.' fileName = raw_input('Input file name:') inputFile = open(fileName,'r') inputFileList = inputFile.read().splitlines() pointMap = {} for i in inputFileList: tempList = i.split() pointMap[int(tempList[3])] = Point(float(tempList[0]),float(tempList[1]),float(tempList[2]),int(tempList[3])) Radius = float(inputFileList[0].split()[2]) r = distance(pointMap[1],pointMap[2]) print 'Eg: R.pdb or R.pse' outPutFile = raw_input('Input output file name:') # cmd.pseudoatom('center') # cmd.set('surface_mode', 1) # cmd.translate([0,0,0], 'center', camera=1) # cmd.ramp_new('test', 'center', [640, 646, 647], ['black', 'forest', 'green']) # cmd.set('surface_color', 'test', 't*') #Plot the first 4 trimer-center's coordinates. cmd.load('t1.pdb') #first trimerIndex cmd.select('/t1/PSDO') # cmd.color('blue','t1') cmd.show('line', 't1') cmd.rotate ('z', -30, 't1', camera=1) cmd.translate([0,0,Radius],'t1',camera=1) #second trimerIndex cmd.create('t2', 't1') # cmd.color('red', 't2') cmd.select('/t2/PSDO') # cmd.color('yellow', 't2') cmd.show('line', 't2') cmd.translate([r, 0, 0], 't2', camera=1) cmd.orient('t2') cmd.rotate('z', 60, 't2', camera=1) cmd.rotate('z', -30 ) #this angle changes to bring involved pseudo atoms to horizontal positions cmd.rotate('y', 4.726, 't2', camera=1) cmd.rotate('x', 0.004, 't2', camera=1) # because we are rotating only one trimer, the 2 central pseudo atoms will not align. correction commands are: cmd.translate([0, -0.0440605685747639, -2.14176156973178], 't2', camera=1) # cmd.alter('/t2//A', chain='D') # cmd.alter('/t2//B', chain='E') # cmd.alter('/t2//C', chain='F') #third trimerIndex cmd.create('t3','t2') cmd.select('/t3/PSDO') cmd.show('line','t3') cmd.origin('t1') cmd.rotate(pointMap[1].getCoordinate(),120,'t3',camera=1) #forth trimerIndex cmd.create('t4','t2') cmd.select('/t4/PSDO') cmd.show('line','t4') cmd.origin('t1') cmd.rotate(pointMap[1].getCoordinate(),240,'t4',camera=1) #According to the first 4 trimer-center coordinates, to plot the following trimers. keyList = pointMap.keys() keyList.sort() # testCount = 1 for i in keyList: if (i>4): # testCount += 1 tempTName = 't'+str(i) if (i%2)==1: tempTFatherName = 't'+str((i-1)/2) tempDegree = 120 else: tempTFatherName = 't'+str((i-2)/2) tempDegree = 240 tempTGrandFatherIndex = ((i-1)//2-1)//2 if tempTGrandFatherIndex == 0: tempTGrandFatherIndex = 1 tempTGrandFatherName = 't'+str(tempTGrandFatherIndex) cmd.create(tempTName,tempTGrandFatherName) cmd.select('/'+tempTName+'/PSDO') cmd.show('line',tempTName) cmd.origin(tempTFatherName) cmd.rotate(pointMap[(i-1)//2].getCoordinate(),tempDegree,tempTName,camera=1) cmd.save(outPutFile)
def algorithm(R, r, writeInFile=True): # The IO for researcher to input the R # r = 52.0769942809 cosphi = (2 * R**2 - r**2) / (2 * R**2) sinphi = math.sqrt(1 - cosphi**2) K_dict = {} K_dict[1] = Point(0, 0, R, 1) K_dict[2] = Point(sinphi * R, 0, cosphi * R) K_dict[2].editIndex(2) K_dict[3] = rotate120(K_dict[2], K_dict[1]) K_dict[3].editIndex(3) K_dict[4] = rotate240(K_dict[2], K_dict[1]) K_dict[4].editIndex(4) K_queue = [2, 3, 4] K_keepers = [K_dict[1], K_dict[2], K_dict[3], K_dict[4]] K_index = 5 K_count = 4 # open the file to write, w is write the points' coordinations if writeInFile: w = open("points", "w") w_plot = open("lines", "w") # To write the first 4 points' coordination the file "points", for i in K_keepers: w.write(str(i)) w.write("\n") # To write the first 3 lines 2 ends' points coordinations in the file "lines" w_plot.write(str(K_keepers[0]) + " " + str(K_keepers[1]) + "\n") w_plot.write(str(K_keepers[0]) + " " + str(K_keepers[2]) + "\n") w_plot.write(str(K_keepers[0]) + " " + str(K_keepers[3]) + "\n") # Repeat the step which finds the coordinations # according to the points in the K_queue # and push the new points in the K_queue # Until we use all the points in the K_queue while len(K_queue) != 0: father = K_queue.pop(0) grandfather = get_root_index(father) if K_dict.has_key(father): new_point = rotate120(K_dict[grandfather], K_dict[father]) K_index = father * 2 + 1 new_point.editIndex(K_index) if check_everydistance(r, new_point, K_keepers): K_keepers.append(new_point) K_dict[K_index] = new_point K_queue.append(K_index) K_count += 1 if writeInFile: w.write(str(new_point) + "\n") w_plot.write( str(new_point) + " " + str(K_dict[father]) + "\n") K_index = father * 2 + 2 new_point = rotate240(K_dict[grandfather], K_dict[father]) new_point.editIndex(K_index) if check_everydistance(r, new_point, K_keepers): K_keepers.append(new_point) K_dict[K_index] = new_point K_queue.append(K_index) K_count += 1 if writeInFile: w.write(str(new_point) + "\n") w_plot.write( str(new_point) + " " + str(K_dict[father]) + "\n") if writeInFile: print "There are " + str(K_count) + " points." print "There are " + str(T_points_count) + " T_points." w_plot.close w.close return K_count