def load_chimerax(filename): """ Load a UCSF ChimeraX file written by SwissDock Parameters ---------- filename : str chimerax file (XML file with URL of target and ligands cluster) """ # UCSF Chimera Web Data Format # www.cgl.ucsf.edu/chimera/docs/ContributedSoftware/webdata/chimerax.html docked = get_docked() print(f" PyViewDock: Loading \"{filename}\"") # read ChimeraX file as XML try: chimerax_xml = ET.parse(filename).getroot() target_url = chimerax_xml.find('web_files').find('file').get('loc') commands = chimerax_xml.find('commands').find('py_cmd').text cluster_url = re.findall('"(http[^"]+pdb)"', commands)[0] target_filename = target_url.split('/')[-1] cluster_filename = cluster_url.split('/')[-1] if not all( [target_url, cluster_url, target_filename, cluster_filename]): raise ValueError except FileNotFoundError: print(" PyViewDock: Failed reading 'chimerax' file. File not found.") except: print(" PyViewDock: Failed reading 'chimerax' file. Invalid format.") else: # fetch files from server try: target_pdb = urlopen(target_url).read().decode('utf-8') cluster_pdb = urlopen(cluster_url).readlines() cluster_pdb = [i.decode('utf-8') for i in cluster_pdb] cmd.read_pdbstr(target_pdb, 'target') docked.load_dock4(cluster_pdb, 'cluster', 0) except HTTPError: print( " PyViewDock: Failed reading 'chimerax' file. Bad server response. Too old?" ) # find local files that match names in .chimerax directory chimerax_directory = os.path.dirname(os.path.realpath(filename)) target_file = os.path.join(chimerax_directory, target_filename) cluster_file = os.path.join(chimerax_directory, cluster_filename) if os.path.isfile(target_file) and os.path.isfile(target_file): print( f" PyViewDock: Files found locally ({target_filename}, {cluster_filename}). Loading..." ) importing.load(target_file) load_dock4(cluster_file)
def load_ext(filename, object='', state=0, format='', finish=1, discrete=-1, quiet=1, multiplex=None, zoom=-1, partial=0, mimic=1, object_props=None, atom_props=None, *, _self=cmd): """ Wrapper to load function with extended funtionality Formats ------- .ff fDynamo's force field file .crd fDynamo's coordinates file .dynn DYNAMON options/selection file .* default supported by PyMOL """ if not format: format = os.path.basename(filename).rpartition('.')[-1] # fDynamo's force field if format.lower() == "ff": load_ff(filename) # DYNAMON dynn options/selection elif format.lower() == "dynn": load_dynn(filename) # fDynamo's crd coordinates elif format.lower() == "crd": load_crd(filename, object) # original load function else: importing.load(filename, object, state, format, finish, discrete, quiet, multiplex, zoom, partial)
def load_ext(filename, object='', state=0, format='', finish=1, discrete=-1, quiet=1, multiplex=None, zoom=-1, partial=0, mimic=1, object_props=None, atom_props=None, *, _self=cmd): """ Wrapper to load function with extended funtionality Formats ------- .chimerax XML file with URL of target and ligands cluster .ene / .eneRST energy table with reference numbers of structures from pyDock """ if not format: file_dot_separated = os.path.basename(filename).rpartition('.') name = "".join(file_dot_separated[:-2]) format = file_dot_separated[-1] # Chimera X if format.lower() == "chimerax": load_chimerax(filename) # pyDock elif format.lower() in ("ene", "enerst"): load_pydock(filename, name) # original load function else: importing.load(filename, object, state, format, finish, discrete, quiet, multiplex, zoom, partial)
def load_xyz(self, filename, object): """ Load a group of structures as an object from .xyz with multiple states and docking information Parameters ---------- filename : str coordinates file (.xyz) object : str name to be include the new object """ self.__init__() # read comments from xyz file with open(filename, 'rt') as f: xyz_file = f.readlines() nline = 0 comments = [] while nline < len(xyz_file): natoms = int(xyz_file[nline]) comments.append(xyz_file[nline + 1].strip()) nline += natoms + 2 # process comments # TODO: broader processing and pattern recognition if all(isinstance(i, float) for i in comments): comments = [float(i) for i in comments] # add entries to data class for n, comm in enumerate(comments): remarks = {'value': comm} self.entries.append({ 'internal': { 'object': object, 'state': n + 1 }, 'remarks': remarks }) # load structures into PyMOL importing.load(filename, object=object, format='xyz', quiet=1)
def load_pydock(self, filename, object, max_n): """ Load a PyDock's group of structures as an object with multiple states and read the docking information Parameters ---------- filename : str energy resume file (.ene / .eneRST) object : str basename to include the new objects (_rec / _lig) max_n : int maximum number of structures to load """ self.__init__() rec_obj = object + "_rec" lig_obj = object + "_lig" basename = os.path.basename(filename).split('.')[0] directory = os.path.dirname(os.path.realpath(filename)) # get list of matching pdb files pdb_files = glob.glob(os.path.join(directory, "*.pdb")) # find receptor and ligand files try: receptor_file = [i for i in pdb_files if "_rec.pdb" in i][0] ligand_file = [i for i in pdb_files if "_lig.pdb" in i][0] except: print( " PyViewDock: Failed loading pyDock file. Missing '_rec.pdb' or '_lig.pdb'." ) return # read energy file with open(filename, "rt") as f: energy_file = [ line.strip() for line in f.readlines() if line.strip() and not line.startswith("----") ] header = energy_file.pop(0).split() for line in energy_file: remarks = { h: (int(v) if h in {'Conf', 'RANK'} else float(v)) for h, v in zip(header, line.split()) } self.entries.append({ 'remarks': deepcopy(remarks), 'internal': deepcopy(self.internal_empty) }) # load receptor importing.load(receptor_file, rec_obj) # load ligands loaded = [] not_found_warning = False for n, entry in enumerate(self.entries): if n + 1 > max_n: break conf_num = entry['remarks']['Conf'] try: conf_file = [i for i in pdb_files if f"_{conf_num}.pdb" in i][0] except IndexError: not_found_warning = True continue else: loaded.append(n) entry['internal']['object'] = lig_obj entry['internal']['state'] = len(loaded) importing.load(conf_file, lig_obj, 0) if not_found_warning: print( " PyViewDock: WARNING: Some ligands defined on the energy file could not been found and loaded." ) # delete entries which pdb has not been found for i in reversed(range(self.n_entries)): if i not in loaded: del self.entries[i] # remove atoms of receptor from ligand cmd.remove(f"{lig_obj} in {rec_obj}")