def Read_PDB_2_SimpleAtom_list(filename): ''' 把每个atom的信息写到一个Simple_atom的类中,然后输出一个Atom_class的list , 部分异常处理未完成。 ''' try: fp = open(filename , 'r') except: print "Error: No such file: "+filename exit(1) allLines = fp.readlines() list = [] for eachline in allLines: if ("ATOM" in eachline) or ("HETEAM" in eachline): temp_Atom = unit_atom.unit_atom() try: temp_Atom.atom_serial = int(string.strip(eachline[6:11])) except: pass temp_Atom.atom_name = string.strip(eachline[12:16]) temp_Atom.residue_name = string.strip(eachline[17:20]) try: temp_Atom.residue_serial = int(string.strip(eachline[22:26])) except: pass try: temp_Atom.coor_x = float(string.strip(eachline[30:38])) temp_Atom.coor_y = float(string.strip(eachline[38:46])) temp_Atom.coor_z = float(string.strip(eachline[46:54])) except: pass list.append(temp_Atom) return list
def Read_PDB_2_SimpleAtom_list(filename): ''' 把每个atom的信息写到一个Simple_atom的类中,然后输出一个Atom_class的list , 部分异常处理未完成。 ''' try: fp = open(filename, 'r') except: print "Error: No such file: " + filename exit(1) allLines = fp.readlines() list = [] for eachline in allLines: if ("ATOM" in eachline) or ("HETEAM" in eachline): temp_Atom = unit_atom.unit_atom() try: temp_Atom.atom_serial = int(string.strip(eachline[6:11])) except: pass temp_Atom.atom_name = string.strip(eachline[12:16]) temp_Atom.residue_name = string.strip(eachline[17:20]) try: temp_Atom.residue_serial = int(string.strip(eachline[22:26])) except: pass try: temp_Atom.coor_x = float(string.strip(eachline[30:38])) temp_Atom.coor_y = float(string.strip(eachline[38:46])) temp_Atom.coor_z = float(string.strip(eachline[46:54])) except: pass list.append(temp_Atom) return list
def Traj_2_coor(in_coor_file,traj_file,out_coor_file,atom_list,skip): ''' Reading a trajectory file. output the frames to files like out_coor_file_1.pdb \n atom_list : a list of atom serial.\n out_coor_file: a file name, in pdb or gro format. a list of file based on the name\ will be created for output.\n skip: a int number like 1,2,3,10. ''' Alist=Simple_atom.Get_Simple_atom_list(in_coor_file) Blist=[] for atom in Alist: if atom.atom_serial in atom_list: Blist.append(atom) '''Get the result atom list in Simple_atom class.''' u=MDAnalysis.Universe(in_coor_file,traj_file) for ts in u.trajectory: if ts.frame % skip ==0 : a,b=os.path.splitext(out_coor_file) temp_out_file=a+"_%d" %(int(ts.frame)/skip) + b if os.path.isfile(temp_out_file): #print "backup %s to %s" %(temp_out_file,"#"+temp_out_file+"#") usage.echo("backup %s to %s\r" %(temp_out_file,"#"+temp_out_file+"#")) try: os.rename(temp_out_file,"#"+temp_out_file+"#") except OSError,e: print e print "the file %s will be overwrited!" %temp_out_file pass fp=open(temp_out_file,'w+') if temp_out_file.endswith(".gro"): fp.write("%d\n" %len(Blist)) else: pass for atom in Blist: temp_class=unit_atom.unit_atom(atom_name=atom.atom_name,\ atom_serial=atom.atom_serial,\ residue_name=atom.residue_name,\ residue_serial=atom.residue_serial) temp_class.atom_coor_x=ts._x[atom.atom_serial-1]/10 temp_class.atom_coor_y=ts._y[atom.atom_serial-1]/10 temp_class.atom_coor_z=ts._z[atom.atom_serial-1]/10 if temp_out_file.endswith(".pdb"): fp.write(temp_class.atom_2_PDBformat()+"\n") elif temp_out_file.endswith(".gro"): fp.write(temp_class.atom_2_GROformat()+"\n") else: pass # print "write frame %5d to %16s" %(ts.frame,temp_out_file) usage.echo("write frame %5d to %16s\r" %(ts.frame,temp_out_file)) else: pass
def Read_GRO_2_SimpleAtom_list(filename): ''' Read in a gro file and return a GRO class list. ''' try: fp = open(filename, 'r') except: print "Error: No such file: " + filename exit(1) allLines = fp.readlines() atom_list = [] gro_name = allLines[0] gro_atom_number = int(allLines[1]) gro_box_size = re.split(string.strip(allLines[-1]), "\s") for i in range(len(allLines) - 3): atom_temp = unit_atom.unit_atom() try: atom_temp.residue_serial = int(string.strip(allLines[i + 2][0:5])) except: pass atom_temp.residue_name = string.strip(allLines[i + 2][5:10]) atom_temp.atom_name = string.strip(allLines[i + 2][10:15]) try: atom_temp.atom_serial = int(string.strip(allLines[i + 2][15:20])) except: pass try: atom_temp.atom_coor_x = float(string.strip(allLines[i + 2][20:28])) atom_temp.atom_coor_y = float(string.strip(allLines[i + 2][28:36])) atom_temp.atom_coor_z = float(string.strip(allLines[i + 2][36:44])) except: pass atom_list.append(atom_temp) if len(atom_list) != gro_atom_number: print "Warning: the atom number in gro file %d not match the gro file title %d." % ( len(atom_list), gro_atom_number) return atom_list
def Read_GRO_2_SimpleAtom_list(filename): ''' Read in a gro file and return a GRO class list. ''' try: fp=open(filename,'r') except: print "Error: No such file: "+filename exit(1) allLines = fp.readlines() atom_list=[] gro_name=allLines[0] gro_atom_number=int(allLines[1]) gro_box_size=re.split(string.strip(allLines[-1]),"\s") for i in range(len(allLines)-3): atom_temp=unit_atom.unit_atom() try: atom_temp.residue_serial=int(string.strip(allLines[i+2][0:5])) except: pass atom_temp.residue_name=string.strip(allLines[i+2][5:10]) atom_temp.atom_name=string.strip(allLines[i+2][10:15]) try: atom_temp.atom_serial=int(string.strip(allLines[i+2][15:20])) except: pass try: atom_temp.atom_coor_x=float(string.strip(allLines[i+2][20:28])) atom_temp.atom_coor_y=float(string.strip(allLines[i+2][28:36])) atom_temp.atom_coor_z=float(string.strip(allLines[i+2][36:44])) except: pass atom_list.append(atom_temp) if len(atom_list) != gro_atom_number : print "Warning: the atom number in gro file %d not match the gro file title %d." %(len(atom_list),gro_atom_number) return atom_list
residue_label=string.split(string.join(residue_label[1:])) residue_pointer=string.split(string.join(residue_pointer[1:])) atom=[] for name in atom_name[1:]: for i in range(len(name)/4): atom.append(string.strip(name[4*i:4*i+4])) atom_name=atom result_list=[] temp_num=0 for i in range(len(atom_name)): atom_unit=unit_atom.unit_atom() atom_unit.atom_name=atom_name[i] atom_unit.atom_serial=(i+1) # for j in range(len(residue_pointer)-1): # if (i+1) > int(residue_pointer[j])-1 and (i+1) < int(residue_pointer[j+1]): # atom_unit.residue_name=residue_label[j] # atom_unit.residue_serial=j+1 # elif (i+1) > int(residue_pointer[-1])-1: # atom_unit.residue_name=residue_label[-1] # atom_unit.residue_serial=len(residue_pointer) # else: # pass if (i+1) > int(residue_pointer[temp_num])-1 and (i+1) < int(residue_pointer[temp_num+1]): atom_unit.residue_name=residue_label[temp_num] atom_unit.residue_serial=temp_num+1 elif (i+1) > int(residue_pointer[-1])-1:
pass residue_label = string.split(string.join(residue_label[1:])) residue_pointer = string.split(string.join(residue_pointer[1:])) atom = [] for name in atom_name[1:]: for i in range(len(name) / 4): atom.append(string.strip(name[4 * i:4 * i + 4])) atom_name = atom result_list = [] temp_num = 0 for i in range(len(atom_name)): atom_unit = unit_atom.unit_atom() atom_unit.atom_name = atom_name[i] atom_unit.atom_serial = (i + 1) # for j in range(len(residue_pointer)-1): # if (i+1) > int(residue_pointer[j])-1 and (i+1) < int(residue_pointer[j+1]): # atom_unit.residue_name=residue_label[j] # atom_unit.residue_serial=j+1 # elif (i+1) > int(residue_pointer[-1])-1: # atom_unit.residue_name=residue_label[-1] # atom_unit.residue_serial=len(residue_pointer) # else: # pass if (i + 1) > int(residue_pointer[temp_num]) - 1 and (i + 1) < int( residue_pointer[temp_num + 1]): atom_unit.residue_name = residue_label[temp_num] atom_unit.residue_serial = temp_num + 1