def get_atom_list_from_pdb_frame (frame): """ Function doc """ #print ('\nstarting: parse_pdb - building atom list') #initial = time.time() pdb_file_lines = frame.split('\n') atoms = [] for line in pdb_file_lines: if line[:4] == 'ATOM' or line[:6] == 'HETATM': at_name = line[12:16].strip() at_pos = np.array([float(line[30:38]), float(line[38:46]), float(line[46:54])]) at_res_i = int(line[22:26]) at_res_n = line[17:20].strip() at_ch = line[21] atom = mm.Atom(name = at_name, #index = index, pos = at_pos, resi = at_res_i, resn = at_res_n, chain = at_ch, #atom_id = counter, ) #Vobject.atoms.append(atom) atoms.append(atom) print ('Numeber of atoms: ', len(atoms)) #print ('ending parse_pdb: - building atom list', final - initial, '\n') return atoms
def parse_pdb_old(infile = None, counter = 0, atom_dic_id = None, Vobject_id = None): """ Function doc """ print ('\nstarting pdb parser') initial = time.time() with open(infile, 'r') as pdb_file: label = os.path.basename(infile) Vobject = mm.Vobject(label = label) pdb_file_lines = pdb_file.readlines() atoms = [] frame = [] #for line in pdb_file: for line in pdb_file_lines: if line[:4] == 'ATOM' or line[:6] == 'HETATM': at_name = line[12:16].strip() #at_index = int(line[6:11]) at_pos = np.array([float(line[30:38]), float(line[38:46]), float(line[46:54])]) at_res_i = int(line[22:26]) at_res_n = line[17:20].strip() at_ch = line[21] atom = mm.Atom(name = at_name, #index = index, pos = at_pos, resi = at_res_i, resn = at_res_n, chain = at_ch, #atom_id = counter, Vobject = Vobject, ) Vobject.atoms.append(atom) if 'ENDMDL' in line: break atom_dic_id = Vobject.generate_chain_structure(counter = counter, atom_dic_id = atom_dic_id) final = time.time() print ('ending pdb parser: ', final - initial, '\n') return Vobject, atom_dic_id
def parse_pdb_old (infile = None, counter = 0, atom_dic_id = None, Vobject_id = None ): """ Function doc """ #frames = [] with open(infile, 'r') as pdb_file: label = os.path.basename(infile) Vobject = mm.Vobject() Vobject.label = label.split('.') Vobject.label = Vobject.label[0] chains_m = {} residues = {} atoms = [] index = 1 sum_x = 0 sum_y = 0 sum_z = 0 frame = [] residue_list = [] parser_resi = None parser_resn = None for line in pdb_file: if line[:4] == 'ATOM' or line[:6] == 'HETATM': at_name = line[12:16].strip() #at_index = int(line[6:11]) at_pos = np.array([float(line[30:38]), float(line[38:46]), float(line[46:54])]) at_res_i = int(line[22:26]) at_res_n = line[17:20].strip() at_ch = line[21] atm = mm.Atom(name = at_name, index = index, pos = at_pos, resi = at_res_i, resn = at_res_n, chain = at_ch, atom_id = counter, #Vobject_id = Vobject_id, #Vobject_name = Vobject.label, Vobject = Vobject, ) if atom_dic_id is not None: atom_dic_id[counter] = atm counter += 1 index += 1 if at_ch in chains_m.keys(): ch = chains_m[at_ch] #print 'existe' else: ch = mm.Chain(name=at_ch, label = 'UNK') chains_m[at_ch] = ch #print 'n existe' if at_res_i == parser_resi:# and at_res_n == parser_resn: atm.residue = ch.residues[-1] ch.residues[-1].atoms.append(atm) #res.atoms.append(atm) Vobject.atoms.append(atm) frame.append([atm.pos[0],atm.pos[1],atm.pos[2]]) #ch.residues[at_res_i].atoms.append(atm) #Vobject.atoms.append(atm) else: residue = mm.Residue(name=at_res_n, index=at_res_i, chain=at_ch) atm.residue = residue residue.atoms.append(atm) ch.residues.append(residue) #ch.residues[at_res_i] = res #ch.residues[at_res_i].atoms.append(atm) Vobject.atoms.append(atm) frame.append([atm.pos[0],atm.pos[1],atm.pos[2]]) parser_resi = at_res_i parser_resn = at_res_n #if at_res_i in ch.residues.keys(): # res = ch.residues[at_res_i] # #print 'existe' # ch.residues[at_res_i].atoms.append(atm) # Vobject.atoms.append(atm) # frame.append([atm.pos[0],atm.pos[1],atm.pos[2]]) #else: # #print at_res_n, at_res_i, at_ch # res = mm.Residue(name=at_res_n, index=at_res_i, chain=at_ch) # ch.residues[at_res_i] = res # ch.residues[at_res_i].atoms.append(atm) # Vobject.atoms.append(atm) # frame.append([atm.pos[0],atm.pos[1],atm.pos[2]]) if atm.name == 'CA': ch.backbone.append(atm) #ch.backbone.append(atm.pos[0]) #ch.backbone.append(atm.pos[1]) #ch.backbone.append(atm.pos[2]) sum_x += atm.pos[0] sum_y += atm.pos[1] sum_z += atm.pos[2] # add a new frame to frames list Vobject.frames.append(frame) Vobject.chains = chains_m total = len(Vobject.atoms) Vobject.list_atom_lines = [True] *total Vobject.list_atom_ribbons = [False]*total Vobject.list_atom_dots = [False]*total Vobject.list_atom_sticks = [False]*total Vobject.list_atom_spheres = [False]*total Vobject.list_atom_surface = [False]*total Vobject.list_atom_ball_and_stick = [False]*total Vobject.mass_center[0] = sum_x / total Vobject.mass_center[1] = sum_y / total Vobject.mass_center[2] = sum_z / total #print Vobject.mass_center for chain in Vobject.chains: print('number_of_residues', len(Vobject.chains[chain].residues)) #print('number_of_residues', len()) return Vobject, atom_dic_id
def parse_xyz(infile = None, counter = 0, atom_dic_id = None, Vobject_id = None ): """ Function doc """ #frames = [] with open(infile, 'r') as xyz_file: label = os.path.basename(infile) xyz_lines = xyz_file.readlines() total_size = len(xyz_lines) xyz_model_size = int(xyz_lines[0]) model_size = xyz_model_size+2 # include first and secound lines in a xyzfile models = [] for i in range(0, int(total_size/model_size)): #print (i*model_size) , (i+1)*model_size model = xyz_lines[(i*model_size) : (i+1)*model_size] models.append(model) pprint (models[-1][2:]) Vobject = mm.Vobject() Vobject.label = label.split('.') Vobject.label = Vobject.label[0] chains_m = {} residues = {} atoms = [] index = 1 sum_x = 0 sum_y = 0 sum_z = 0 residue_list = [] parser_resi = None parser_resn = None frame = [] for line in models[0][2:]: line2 = line.split() #print line2, [float(line[1]), float(line[2]), float(line[3])] at_name = line2[0].strip() at_pos = np.array([float(line2[1]), float(line2[2]), float(line2[3])]) at_res_i = 1 at_res_n = 'UNK' at_ch = 'A' #print at_name, '<--------' atm = mm.Atom(name = at_name, index = index, pos = at_pos, resi = at_res_i, resn = at_res_n, chain = at_ch, atom_id = counter, #Vobject_id = Vobject_id, #Vobject_name = Vobject.label, Vobject = Vobject ) if atom_dic_id is not None: atom_dic_id[counter] = atm counter += 1 index += 1 coords = [] if at_ch in chains_m.keys(): ch = chains_m[at_ch] else: ch = mm.Chain(name=at_ch, label = 'UNK') chains_m[at_ch] = ch if at_res_i == parser_resi:# and at_res_n == parser_resn: atm.residue = ch.residues[-1] ch.residues[-1].atoms.append(atm) #res.atoms.append(atm) Vobject.atoms.append(atm) frame.append([atm.pos[0],atm.pos[1],atm.pos[2]]) #ch.residues[at_res_i].atoms.append(atm) #Vobject.atoms.append(atm) else: residue = mm.Residue(name=at_res_n, index=at_res_i, chain=at_ch) atm.residue = residue residue.atoms.append(atm) ch.residues.append(residue) Vobject.atoms.append(atm) frame.append([atm.pos[0],atm.pos[1],atm.pos[2]]) parser_resi = at_res_i parser_resn = at_res_n #if at_res_i in ch.residues.keys(): # res = ch.residues[at_res_i] # ch.residues[at_res_i].atoms.append(atm) # Vobject.atoms.append(atm) # frame.append([atm.pos[0],atm.pos[1],atm.pos[2]]) # #else: # res = mm.Residue(name=at_res_n, index=at_res_i, chain=at_ch) # ch.residues[at_res_i] = res # ch.residues[at_res_i].atoms.append(atm) # Vobject.atoms.append(atm) # frame.append([atm.pos[0],atm.pos[1],atm.pos[2]]) sum_x += atm.pos[0] sum_y += atm.pos[1] sum_z += atm.pos[2] # add a new frame to frames list Vobject.frames.append(frame) Vobject.chains = chains_m total = len(Vobject.atoms) Vobject.list_atom_lines = [True] *total Vobject.list_atom_ribbons = [False]*total Vobject.list_atom_dots = [False]*total Vobject.list_atom_sticks = [False]*total Vobject.list_atom_spheres = [False]*total Vobject.list_atom_surface = [False]*total Vobject.list_atom_ball_and_stick = [False]*total Vobject.mass_center[0] = sum_x / total Vobject.mass_center[1] = sum_y / total Vobject.mass_center[2] = sum_z / total n = 1 for model_i in models[1:]: frame = [] for line in model_i[2:]: line2 = line.split() #print line2, [float(line[1]), float(line[2]), float(line[3])] #at_name = line2[0].strip() #at_pos = np.array([float(line2[1]), float(line2[2]), float(line2[3])]) frame.append([float(line2[1]), float(line2[2]), float(line2[3])]) #pprint (n) #pprint (frame) Vobject.frames.append(frame) n += 1 #print atom_dic_id return Vobject, atom_dic_id
def parse_pdb(infile): """ """ frames = [] with open(infile, 'r') as pdb_file: frame = mm.Frame() chains_m = {} frame.chains = chains_m atoms = [] for line in pdb_file: if line[:4] == 'ATOM' or line[:6] == 'HETATM': at_name = line[12:16].strip() at_index = int(line[6:11]) at_pos = np.array([ float(line[30:38]), float(line[38:46]), float(line[46:54]) ]) at_res_i = int(line[22:26]) at_res_n = line[17:20].strip() at_ch = line[21] atm = mm.Atom(name=at_name, index=at_index, pos=at_pos, residue=at_res_i) atm.sphere = True atm.ball = True atm.vdw = True atm.pretty_vdw = True atm.dot = True atm.wires = True if at_ch == ' ': at_ch = 'A' #if chains_m.has_key(at_ch): if at_ch in chains_m: ch = chains_m[at_ch] else: ch = mm.Chain(name=at_ch, frame=frame) chains_m[at_ch] = ch #if ch.residues.has_key(at_res_i): if at_res_i in ch.residues: res = ch.residues[at_res_i] else: res = mm.Residue(name=at_res_n, index=at_res_i, chain=at_ch) ch.residues[at_res_i] = res if atm.name == 'CA': ch.backbone.append(atm) #if not res.atoms.has_key(at_index): if at_index not in res.atoms: res.atoms[at_index] = atm atoms.append(atm) elif line[:6] == 'ENDMDL': frame.chains = chains_m frame.atoms = atoms frame.load_bonds() frame.load_ribbons() frames.append(frame) frame = mm.Frame() chains_m = {} atoms = [] if len(frame.chains.values()) > 0 and len(frames) == 0: frame.atoms = atoms frame.load_bonds() frame.load_ribbons() frames.append(frame) return frames