def load_crd(filename, object=''): """ Load a fDynamo coordinates file (.crd) Parameters ---------- filename : str file path object : str, optional name of the object (def: filename prefix) """ if not object: object = "".join(os.path.basename(filename).rpartition('.')[:-2]) # read file as list of strings with open(filename, "rt") as f: crd_file = f.readlines() # remove comment lines, trailing comments and space split crd_file = [ line.split("!")[0].split() for line in crd_file if line.strip() and not line.startswith("!") ] # reversed dictionary of atomic numbers to elements atomic_number_inv = {n: elem for elem, n in atomic_number.items()} # create new model and append crd's atoms model = Indexed() a = Atom() a.hetatm = False for line in crd_file: if line[0].lower() == "subsystem": a.segi = str(line[2]) elif line[0].lower() == "residue": a.resi_number = int(line[1]) a.resn = str(line[2]) elif len(line) != 6: continue else: a.name = str(line[1]) a.symbol = atomic_number_inv[int(line[2])] a.coord = (float(line[3]), float(line[4]), float(line[5])) model.add_atom(deepcopy(a)) model.update_index() cmd.load_model(model, object) cmd.rebond(object) cmd.dss(object) print(f" pyDYNAMON: \"{filename}\" loaded as \"{object}\"")
def fromList(self,list): # currently no handling of conect records model = Indexed() # read atoms cnt = 0 at = None for rec in list: if (rec[0:4]=='ATOM') or (rec[0:6]=='HETATM'): at = Atom() if rec[0]=='A': at.hetatm=0 # default is 1 at.index = cnt at.name = string.strip(rec[12:16]) at.alt = string.strip(rec[16:17]) at.resn = string.strip(rec[17:20]) at.chain = string.strip(rec[21:22]) at.resi = string.strip(rec[22:27]) # note: insertion is part of resi at.resi_number = int(rec[22:26]) at.coord = [float(rec[30:38]), float(rec[38:46]), float(rec[46:54])] try: at.q = float(rec[54:60]) except ValueError: at.q = 1.0 try: at.b = float(rec[60:66]) except ValueError: at.b = 0.0 at.segi = string.strip(rec[72:76]) at.symbol = string.strip(rec[76:78]) if not len(at.symbol): at.symbol = at.name[0:1] if at.symbol in '012345678': at.symbol = at.name[1:2] cnt = cnt + 1 model.add_atom(at) elif (rec[0:3]=='TER'): if at: at.ter=1 return(model)
def convert_to_chempy_model(atom_array): """ Convert an :class:`AtomArray` into a :class:`chempy.models.Indexed` object. Returns ------- chempy_model : Indexed The converted structure. """ model = IndexedModel() annot_cat = atom_array.get_annotation_categories() for i in range(atom_array.array_length()): atom = Atom() atom.segi = atom_array.chain_id[i] atom.chain = atom_array.chain_id[i] atom.resi_number = atom_array.res_id[i] atom.ins_code = atom_array.ins_code[i] res_name = atom_array.res_name[i] atom.resn = res_name if len(res_name) == 1: atom.resn_code = res_name else: try: atom.resn_code = ProteinSequence.convert_letter_3to1(res_name) except KeyError: atom.resn_code = "X" atom.hetatm = 1 if atom_array.hetero[i] else 0 atom.name = atom_array.atom_name[i] atom.symbol = atom_array.element[i] if "b_factor" in annot_cat: atom.b = atom_array.b_factor[i] if "occupancy" in annot_cat: atom.q = atom_array.occupancy[i] if "charge" in annot_cat: atom.formal_charge = atom_array.charge[i] atom.coord = tuple(atom_array.coord[..., i, :]) atom.index = i + 1 model.add_atom(atom) if atom_array.bonds is not None: for i, j, bond_type in atom_array.bonds.as_array(): bond = Bond() bond.order = BOND_ORDER[bond_type] bond.index = [i, j] model.add_bond(bond) else: warnings.warn( "The given atom array (stack) has no associated bond information") return model