def launch_from_gui(self): # If there is no internet connection, everything is blocked. if not check_network_connection('https://www.google.it/'): self.pymod.main_window.show_error_message( "Connection Error", "No internet connection. Can not update PyMod databases.") return None # Associates the correct installer to each component. self.components_dict_installer = {} self.components_list = [] # Builds Qthread objects. _all_components_list = all_components_list[:] if self.pymod.use_blast_v5_databases: _all_components_list.pop(0) # Remove BLAST v4 databases. else: _all_components_list.pop(1) # Remove BLAST v5 databases. for comp, installer_class in zip( _all_components_list, [BLAST_db_installer, Pfam_db_installer, BLAST_db_installer]): component_qthread = installer_class(component=comp) self.components_dict_installer[comp.name] = component_qthread if comp.name == 'hmmscan_databases': comp.installer.hmmpress_exe_filepath = os.path.join( self.pymod.hmmer_tool["exe_dir_path"].get_value(), get_exe_file_name("hmmpress")) self.components_list.append(comp) comp.reinitialize() # Showing GUI. self.window = InstallerUpdaterWindow(parent=self.pymod.main_window, installer_protocol=self)
def get_similariry_search_exe_path(self, return_dict=False): """ Get the absolute filepaths of the executables files needed to perform the similarity search. """ if self.protocol_name != "hmmsearch": # Returns only one executable filepath. exe_filepath_list = [os.path.join(self.tool["exe_dir_path"].get_value(), get_exe_file_name(self.exe_filename))] exe_filepath_dict = {self.exe_filename: exe_filepath_list[0]} else: # Returns two executables filepath. exe_filepath_list = [] exe_filepath_dict = {} for exe_filename in self.all_exe_filenames: exe_filepath = os.path.join(self.tool["exe_dir_path"].get_value(), get_exe_file_name(exe_filename)) exe_filepath_list.append(exe_filepath) exe_filepath_dict[exe_filename] = exe_filepath if return_dict: return exe_filepath_dict else: return exe_filepath_list
def search_domains(self, query_element, database, evaluecutoff): self._initializes_domains_search(query_element) self.evaluecutoff = evaluecutoff exe_filepath = os.path.join( self.pymod.hmmer_tool["exe_dir_path"].get_value(), get_exe_file_name("hmmscan")) out_filepath = os.path.join( self.output_directory, "hmmscan_out_" + self.query_element_name + ".txt") cline = [ exe_filepath, "-o", out_filepath, "-E", str(evaluecutoff), database.replace('.h3m', ''), self.query_filepath ] # self.pymod.new_execute_subprocess(cline) self.pymod.execute_subprocess(cline, new_shell=False) return out_filepath
def check_psipred_parameters( self): # predict_secondary_structure(self, elements=None): """ Checks that the files needed to run PSIPRED exists on users' machines. """ # First checks for PSIPRED installation. if not self.pymod.psipred.tool_dir_exists( parameter_name="exe_dir_path"): self.pymod.psipred.tool_dir_not_found() return False psipred_exe_dirpath = self.pymod.psipred["exe_dir_path"].get_value() for _exe_filename in ("chkparse", "psipred", "psipass2"): exe_filename = pmos.get_exe_file_name(_exe_filename) if not os.path.isfile( os.path.join(psipred_exe_dirpath, exe_filename)): message = ( "A PSIPRED executable file ('%s') does not exists in" " the PSIPRED executables directory specified in the PyMod" " Options Window ('%s'). If you want to use PSIPRED, please" " specify in the Options Window a PSIPRED executables directory" " where a '%s' file is found." % (exe_filename, psipred_exe_dirpath, exe_filename)) self.pymod.main_window.show_error_message( self.error_title, message) return False # Then checks for PSIPRED datafiles. if not self.pymod.psipred.tool_dir_exists( parameter_name="data_dir_path"): message = ( "PSIPRED 'data' directory not found! Please specify an existent" " directory in the PSIPRED Options Window of PyMod.") self.pymod.main_window.show_error_message(self.error_title, message) return False psipred_data_dirpath = self.pymod.psipred["data_dir_path"].get_value() if len(os.listdir(psipred_data_dirpath)) == 0: message = ( "PSIPRED 'data' directory is empty! Please specify a data" " directory actually containing PSIPRED data file in the" " in the Options Window of PyMod.") self.pymod.main_window.show_error_message(self.error_title, message) return False # Checks for PSI-BLAST on the user's system. if not self.pymod.blast_plus.tool_dir_exists( parameter_name="exe_dir_path"): self.pymod.blast_plus.tool_dir_not_found() return False psiblast_exe_dirpath = self.pymod.blast_plus["exe_dir_path"].get_value( ) if not os.path.isfile( os.path.join(psiblast_exe_dirpath, pmos.get_exe_file_name("psiblast"))): message = ( "The PSI-BLAST executable file ('psiblast') does not exists in" " the BLAST+ suite executables directory specified in the PyMod" " Options Window ('%s'). If you want to use PSIPRED, please" " specify in the Options Window a BLAST+ suite executables directory" " where a 'psiblast' file is found." % (psiblast_exe_dirpath)) self.pymod.main_window.show_error_message(self.error_title, message) return False # And finally checks for a BLAST database. if not self.pymod.psipred.tool_dir_exists( parameter_name="database_dir_path"): message = ( "A directory containing a BLAST database was not found! Please" " specify an existent directory in the PSIPRED options in the options window of PyMod." ) self.pymod.main_window.show_error_message(self.error_title, message) return False dbpath = self.pymod.psipred["database_dir_path"].get_value() if not self.verify_valid_blast_dbdir(dbpath, remove_temp_files=True): message = ( "The database '%s' directory does not contain a valid set" " of database files." % (dbpath)) self.pymod.main_window.show_error_message(self.error_title, message) return False return True
def run_psipred(self, element): """ Actually runs PSIPRED, collects its results and map them on the sequences in PyMod main window. """ print_output = False sequence_header = element.my_header if print_output: print("- Beginning PSIPRED prediction for:", sequence_header) # The name of the BLAST database file. # If the database files are contained in a folder like this: /home/user/pymod/databases/swissprot/swissprot dbpath = self.pymod.psipred["database_dir_path"].get_value( ) # e.g.: /home/user/pymod/databases/swissprot if print_output: print("- dbpath:", dbpath) # Where the NCBI programs have been installed. ncbidir = self.pymod.blast_plus["exe_dir_path"].get_value() if print_output: print("- ncbidir:", ncbidir) # Where the PSIPRED V2 programs have been installed. execdir = self.pymod.psipred["exe_dir_path"].get_value() if print_output: print("- execdir:", execdir) # Where the PSIPRED V2 data files have been installed. datadir = self.pymod.psipred["data_dir_path"].get_value() if print_output: print("- datadir", datadir) # Write the temporary input fasta file, setting its basename. basename = "psipred_temp" if print_output: print("- basename: ", basename) self.pymod.build_sequence_file( [element], basename, file_format="fasta", remove_indels=True, new_directory=self.pymod.psipred_dirpath) #--------------------- # Execute PSI-BLAST. - #--------------------- if print_output: print("- Running PSI-BLAST with sequence", basename, "...") try: self.execute_psiblast( ncbi_dir=ncbidir, db_path=dbpath, query=os.path.join(self.pymod.psipred_dirpath, basename + ".fasta"), inclusion_ethresh=0.001, out_pssm=os.path.join(self.pymod.psipred_dirpath, basename + ".chk"), out=os.path.join(self.pymod.psipred_dirpath, basename + ".blast"), num_iterations=3, num_alignments=0) # psiblast_output = open("%s.blast" % os.path.join(self.pymod.psipred_dirpath, basename),"w") # self.pymod.execute_subprocess(psiblast_command, new_stdout=psiblast_output) # psiblast_output.close() except: if print_output: print( "- FATAL: Error whilst running psiblast - script terminated!" ) raise Exception( "There was an error while running PSI-BLAST, so PSIPRED cannot perform a prediction for %s." % (sequence_header)) #-------------------- # Execute chkparse. - #-------------------- if print_output: print("- Predicting secondary structure...") chkdir_command = ( pmos.build_commandline_path_string( os.path.join(execdir, pmos.get_exe_file_name("chkparse"))) + " " + pmos.build_commandline_path_string( "%s.chk" % os.path.join(self.pymod.psipred_dirpath, basename))) try: chkdir_output = open( "%s.mtx" % os.path.join(self.pymod.psipred_dirpath, basename), "w") self.pymod.execute_subprocess(chkdir_command, new_stdout=chkdir_output) chkdir_output.close() except: if print_output: print( "- FATAL: Error whilst running chkdir - script terminated!" ) raise Exception( "No homologous sequences were found by PSI-BLAST for %s, so PSIPRED cannot perform a prediction for this sequence." % (sequence_header)) #-------------------------- # Execute PSIPRED pass 1. - #-------------------------- psipass1_command = ( pmos.build_commandline_path_string( os.path.join(execdir, pmos.get_exe_file_name("psipred"))) + " " + pmos.build_commandline_path_string( "%s.mtx" % os.path.join(self.pymod.psipred_dirpath, basename)) + " " + pmos.build_commandline_path_string( os.path.join(datadir, "weights.dat")) + " " + pmos.build_commandline_path_string( os.path.join(datadir, "weights.dat2")) + " " + pmos.build_commandline_path_string( os.path.join(datadir, "weights.dat3"))) try: psipass1_output = open( "%s.ss" % os.path.join(self.pymod.psipred_dirpath, basename), "w") self.pymod.execute_subprocess(psipass1_command, new_stdout=psipass1_output) psipass1_output.close() except Exception as e: if print_output: print( "- FATAL: Error whilst running psipred 1 - script terminated!" ) raise Exception( "There was an error while running PSIPRED and no prediction was made for %s." % (sequence_header)) #-------------------------- # Execute PSIPRED pass 2. - #-------------------------- psipass2_command = ( pmos.build_commandline_path_string( os.path.join(execdir, pmos.get_exe_file_name("psipass2"))) + " " + "%s 1 1.0 1.0" % pmos.build_commandline_path_string( os.path.join(datadir, "weights_p2.dat")) + " " + pmos.build_commandline_path_string( "%s.ss2" % os.path.join(self.pymod.psipred_dirpath, basename)) + " " + pmos.build_commandline_path_string( "%s.ss" % os.path.join(self.pymod.psipred_dirpath, basename))) try: psipass2_output = open( "%s.horiz" % os.path.join(self.pymod.psipred_dirpath, basename), "w") self.pymod.execute_subprocess(psipass2_command, new_stdout=psipass2_output) psipass2_output.close() except: if print_output: print( "- FATAL: Error whilst running psipass 2 - script terminated!" ) raise Exception( "There was an error while running PSIPRED and no prediction was made for %s." % (sequence_header)) #-------------------------- # Clean up PSIPRED files. - #-------------------------- if print_output: print("- Cleaning up ...") # Remove temporary files. self.remove_psipred_temp_files() # Renames the output files. output_files_name = pmos.clean_file_name(element.my_header) for ext in psipred_output_extensions: os.rename( os.path.join(self.pymod.psipred_dirpath, basename + ext), os.path.join(self.pymod.psipred_dirpath, output_files_name + ext)) if print_output: print("- Final output files:" + output_files_name + ".ss2 " + output_files_name + ".horiz") print("- Finished.") #---------------------------------------------- # Parses the results from .horiz output file. - #---------------------------------------------- results_file = open( os.path.join(self.pymod.psipred_dirpath, output_files_name + ".horiz"), "r") confs = "" # String for confidence scores of each residue. preds = "" # String for the secondary structure elements prediction of each residue. for l in results_file.readlines(): if l.startswith("Conf:"): rl = l[6:66].replace(" ", "").replace("\n", "") confs += rl elif l.startswith("Pred:"): rl = l[6:66].replace(" ", "").replace("\n", "") preds += rl results_file.close() # Actually stores in the PyMod elements the results. element.psipred_elements_list = [] for c, e, res in zip(confs, preds, element.get_polymer_residues()): res.psipred_result = {"confidence": int(c), "sec-str-element": e} return True
def build_psiblast_commandline(self, ncbi_dir, db_path, query, inclusion_ethresh=0.001, num_iterations=3, evalue=None, max_target_seqs=None, num_alignments=None, out=None, outfmt=None, out_pssm=None, blast_version="psiblast"): self.check_blast_version(blast_version) # blastdbcmd -db "\"Users\joeuser\My Documents\Downloads\mydb\"" -info # blastdbcmd -db ' "path with spaces/mydb" ' -info psiblast_path = pmos.build_commandline_path_string( os.path.join(ncbi_dir, pmos.get_exe_file_name(blast_version))) db_path = pmos.build_commandline_file_argument("db", db_path) query = pmos.build_commandline_file_argument("query", query) if inclusion_ethresh != None: inclusion_ethresh = " -inclusion_ethresh %s" % (inclusion_ethresh) else: inclusion_ethresh = "" if num_iterations != None: num_iterations = " -num_iterations %s" % (num_iterations) else: num_iterations = "" if evalue != None: evalue = " -evalue %s" % (evalue) else: evalue = "" if outfmt != None: outfmt = " -outfmt %s" % (outfmt ) # 5 produces an .xml output file. else: outfmt = "" if out != None: out = pmos.build_commandline_file_argument("out", out) else: out = "" if max_target_seqs != None: max_target_seqs = " -max_target_seqs %s" % (max_target_seqs) else: max_target_seqs = "" if out_pssm != None: out_pssm = pmos.build_commandline_file_argument( "out_pssm", out_pssm) else: out_pssm = "" if num_alignments != None: num_alignments = " -num_alignments %s" % (num_alignments) else: num_alignments = "" psiblast_command = (psiblast_path + db_path + query + inclusion_ethresh + out + outfmt + out_pssm + num_iterations + evalue + max_target_seqs + num_alignments) return psiblast_command
def check_hmmscan_program(self): if self.father_protocol.domain_search_mode == "local": exe_dirpath = self.pymod.hmmer_tool["exe_dir_path"].get_value() if not (exe_dirpath and os.path.isdir(exe_dirpath)): title = "Hmmer Suite Executable Error" message = ( "The default Hmmer suite executables directory is missing. Please set one" " in the 'Tools -> Options' menu.") self.pymod.main_window.show_error_message(title, message) return False exe_filename = get_exe_file_name("hmmscan") exe_filepath = os.path.join(exe_dirpath, exe_filename) if not os.path.isfile(exe_filepath): title = "Hmmer Suite Executable Error" message = ( "A '%s' file is missing in the Hmmer executables directory. Please specify" " in the 'Tools -> Options' menu a Hmmer executables directory where a" " '%s' file is found." % (exe_filename, exe_filename)) self.pymod.main_window.show_error_message(title, message) return False db_dirpath = self.pymod.hmmer_tool[ "hmmscan_db_dir_path"].get_value() if db_dirpath.replace(" ", "") == "": title = "No Database Directory" message = ( "No databases directory is defined for HMMSCAN. Please define a database" " directory in the PyMod options window in order to perform a HMMSCAN search." ) self.pymod.main_window.show_error_message(title, message) return False if not os.path.isdir(db_dirpath): title = "Database Directory not Found" message = ( "The specified database directory does not exists. Please specify an" " existing one in the Tools -> Options menu.") self.pymod.main_window.show_error_message(title, message) return False self.hmmscan_db_list = [ d for d in sorted(os.listdir(db_dirpath)) if d.endswith("hmm.h3m") ] if len(self.hmmscan_db_list) == 0: title = "Hmmscan Database Error" message = ( "No valid databases files were found in the hmmscan database directory." " Please refer to the PyMod manual to know how to install them." ) self.pymod.main_window.show_error_message(title, message) return False return True elif self.father_protocol.domain_search_mode == "remote": if not check_network_connection("https://google.com", timeout=3): title = "Connection Error" message = ( "An internet connection is not available, can not connect to the EBI" " server to run HMMSCAN.") self.pymod.main_window.show_error_message(title, message) return False return True