def run_RenameAtoms(self): """ Get the PDB file and run the renaming @type self: koala.CheckPDBStructure.CheckPDBStructure """ try: self.path_runs.set_path_execute() self.path_runs.set_execution_directory() self.opts.pdbName = self.opts.pdbName.replace(" ", "-").replace("(", "").replace(")", "") link_name = os.path.join(self.opts.outputdir, os.path.basename(self.opts.pdbName)) if not os.path.exists(link_name): os.symlink(self.opts.inputPDB, link_name) os.system("cp %s %s" % (link_name, self.path_runs.get_path_execution())) self.rename_atoms_structure( os.path.join(self.path_runs.get_path_execution(), self.opts.pdbName), get_path_gromacs(), self.opts.forceField, ) path_output, file_output = os.path.split(self.opts.output) send_output_results( path_output, file_output, os.path.join(self.path_runs.get_path_execution(), self.opts.pdbName) ) except Exception, e: show_error_message(str(e))
def run_CalculateGDTTS(self): """ Get the PDB input files and run the calculation @type self: koala.CalculateGDTTS.CalculateGDTTS """ try: self.path_runs.set_path_execute() self.path_runs.set_execution_directory() if self.opts.compressedFile == '1': inputFiles = self.opts.inputPdbs.split(",") for input_f in inputFiles: if zipfile.is_zipfile(input_f): extract_zip_file( input_f, self.path_runs.get_path_execution()) else: try: inF = gzip.GzipFile(input_f, 'rb') f = inF.read() inF.close() if f: extract_gz_file( input_f, self.path_runs.get_path_execution()) except Exception, e: show_error_message("The input file could not be read.\n%s" % e) else:
def copy_pdbs_from_input( path_execute, path_to, inputnames, inputPDBs): try: nomes_arquivos = [] for n in inputnames.split(','): if len(n) > 0: name, ext = os.path.splitext(n) name = name.replace(' ', '-').replace('(', '').replace(')', '') if ext.find('.pdb') < 0: raise Exception('%s is not a PDB file' % name) else: if not ext.endswith('.pdb'): extname = ext.replace( '.pdb', '').strip().replace('(', '-').replace(')', '') new_name = name + extname + '.pdb' nomes_arquivos.append(new_name) arquivos = [] arqs = inputPDBs.partition(',') arquivos.append(arqs[0]) while(len(arqs[2]) > 0): arqs = arqs[2].partition(',') arquivos.append(arqs[0]) for i, n in enumerate(nomes_arquivos): link_name = os.path.join(path_to, os.path.basename(n)) if not os.path.exists(link_name): os.symlink(arquivos[i], link_name) os.system("cp %s %s" % (link_name, path_execute)) except Exception, e: show_error_message("Error when copyPDBsFromInput:\n%s" % e)
def create_local_pop_file(path, pop_file, framework): if not isinstance(framework, Params): show_error_message("The variable is not a Params class.") try: if(framework.get_framework() == '2PG'): arq_pop = open(path + "pop_0.pdb", "wr") models = 0 for line in file(pop_file, "r"): arq_pop.write(line) if line.startswith("MODEL"): models += 1 arq_pop.close() return models else: arq_pop = open(path + "pop_meamt.txt", "wr") ind = 0 for line in file(pop_file, "r"): arq_pop.write(line) if line.startswith("##"): ind += 1 arq_pop.close() return ind except Exception, e: show_error_message("Error when CreateLocalPopFile\n%s" % e)
def copy_pdb_reference( path_to, path_execution, pdb_ref_name, pdb_ref_file, ): """ Copy the input PDB referente to the execute path """ try: pdb_ref_name = pdb_ref_name.replace( '(', '_').replace( ')', '').replace( " ", "").strip() link_name = os.path.join( path_to, os.path.basename(pdb_ref_name)) if not os.path.exists(link_name): os.symlink(pdb_ref_file, link_name) shutil.copy(link_name, path_execution) except Exception, e: show_error_message("Erro on copyPDBReference method:\n%s" % e)
def check_pdb(path_execution, path_galaxy): """ Create a subprocess to check the PDB structure using pdb2gmx @type self: koala.CalculateRMSD.CalculateRMSD @type path: string @type path_gromacs: string """ try: cl = [ '%s/scripts/check_structures_gromacs.py' % path_galaxy, path_execution, get_path_gromacs(), '&'] retProcess = subprocess.Popen(cl, 0, None, None, None, False) pvalue = retProcess.wait() if pvalue != 0: return False directory = os.path.join(path_execution, 'no_accepted_by_pdb2gmx') if os.path.exists(directory): pdbs = os.listdir(directory) show_message('These files could not be accepted by Gromacs.\n%s\n\n' % pdbs) return True except Exception, e: show_error_message("Error while checking PDBs:\n%s" % e)
def format_fitness(fitness, tool=None): try: if(tool != 'Dominance_Ranking'): fe = string.split(fitness.rstrip(), ',') no_obj = len(fe) if fe[0] == 'None': raise Exception("Please, use checkboxes to specify the objectives.\n") if int(no_obj) < 2: raise Exception("Please, you must select more than 1 objective.\n") fit = '' for obj in fe: fit += '%s, ' % obj fit = fit.strip(', ') return no_obj, fit else: comb = [] # fitness combination i = 0 fe = fitness.split('#') for fit in range(len(fe)): if len(fe[fit]) > 0: fitn = fe[fit].strip().replace(str(i), '') comb.append(fitn) i = i + 1 return comb except Exception, e: show_error_message("Error when FormatFitness:\n%s" % e)
def execute_program( self, path_execution, program, config, path_output, tool, email, galaxydir="None", outputID="None"): try: stdout_file = open("%sstdout.txt" % path_execution, "wr") retProcess = None retProcess = subprocess.Popen([ 'nohup', program, self.getCommand(), config, path_execution, galaxydir, path_output, outputID, tool, self.get_framework(), email, '&'], stdout=stdout_file, stderr=subprocess.STDOUT, shell=False) if retProcess is not None: pass except Exception, e: show_error_message("Error when _execute_program:\n%s" % e)
def run_SortByFront(self, fitness): """ Run the 2PG Sort algorithm for each objectivies combinations @type self: koala.DominanceRanking.DominanceRanking @type fitness: string """ NumberObjective, Fitness_Energy = format_fitness(fitness) self.framework.set_parameter('NumberObjective', NumberObjective) self.framework.set_parameter('Fitness_Energy', Fitness_Energy) create_configuration_file(self.path_runs.get_path_execution(), self.framework) config = 'configuration.conf' cl = [self.framework.get_command(), config, '&'] retProcess = subprocess.Popen( cl, 0, stdout=None, stderr=subprocess.STDOUT, shell=False) retCode = retProcess.wait() if(retCode != 0): show_error_message( "The 2PG framework finished wrong.\n" "Try to check and rename the PDB input files or contact the system administrator.")
def getPDB(self): """ Connect to pdb.org and download PDB files using PDBID @type self: koala.PDBDownload.PDBDownload """ try: pdb_org = "http://www.rcsb.org/pdb/files/" pdbs = [] # pdbs id i = 0 pdbsid = self.opts.inputIDs.split('#') for pdbid in range(len(pdbsid)): if len(pdbsid[pdbid]) > 0: pdbidn = pdbsid[pdbid].strip() pdbs.append(pdbidn) i = i + 1 path_output, file_output = os.path.split(self.opts.output) pdb_files = [] for i, pdbid in enumerate(pdbs): url = "%s%s.pdb" % (pdb_org, pdbid) open_url(url, path_output, "%s.pdb" % pdbid) pdb_files.append(os.path.join(path_output, "%s.pdb" % pdbid)) send_multiple_outputs( path_output, pdb_files, self.opts.galaxydir, self.opts.outputID) except Exception, e: show_error_message(str(e))
def send_output_results(path_output, file_output, file_result): try: dest = path_output + "/" + file_output copia = "cp " + file_result + " " + dest os.system(copia) # shutil.copy(arquivo, new_path) except Exception, e: show_error_message("Error when sendOutputResults:\n%s" % e)
def clear_path_execute(path): try: if os.path.isdir(path): shutil.rmtree(path) return True else: show_error_message("It is not a path:\n%s" % path) return False except Exception, e: show_error_message("Error when clearPathExecute:\n%s" % e)
def getTMScoreValues(self): """ Calculate the TM-Score value to the input PDB files @type self: koala.CalculateTMScore.CalculateTMScore """ try: pdbs = list_directory(self.path_runs.get_path_execution(), "*.pdb") if not pdbs: raise "getTMScoreValues: No PDB file found.\n" dict_tmscore = {} for pdb in pdbs: if not (pdb == self.opts.inputPDBRefName): name_pdb, ext = os.path.splitext(pdb) name_reference, ext = os.path.splitext(self.opts.inputPDBRefName) aux_command = self.command.replace( "@PATH@", self.path_runs.get_path_execute()).replace( "@MODEL@", (os.path.join(self.path_runs.get_path_execution(), pdb))).replace( "@NATIVE@", (os.path.join( self.path_runs.get_path_execution(), self.opts.inputPDBRefName))) aux_command = aux_command.replace( "@PATHEXECUTE@", self.path_runs.get_path_execution()).replace( "@NAMEMODEL@", name_pdb).replace( "@NAMENATIVE@", name_reference) os.system(aux_command) # TODO: USAR SUBPROCESS os.chdir(self.path_runs.get_path_execution()) result_file = name_reference + '_' + name_pdb + '.txt' temp_tmscore = open(result_file, "r") for line in temp_tmscore.readlines(): if line.startswith("TM-score"): tmscore_value = line.split("=", 1)[1].strip() only_pdb_file_name = os.path.basename(pdb) dict_tmscore[only_pdb_file_name] = tmscore_value temp_tmscore.close() return OrderedDict(sorted(dict_tmscore.items(), key=lambda x: x[1], reverse=True)) except Exception, e: show_error_message("Erro on getTMScoreValues method:\n%s" % e)
def getRMSDValues(self): """ Calculate the RMSD value to the input PDB files @type self: koala.CalculateRMSD.CalculateRMSD """ try: pdbs = list_directory(self.path_runs.get_path_execution(), "*.pdb") if not pdbs: raise Exception("getRMSDValues: No PDB file found.\n") dict_rmsd = {} for pdb in pdbs: if not (pdb == self.opts.inputPDBRefName): # aux_command = self.command.replace( # "@PATH_GROMACS@", # self.ClassColection.getPathGromacs()).replace( # "@PROT@", # (os.path.join(self.path_runs.get_path_execution(), pdb))).replace( # "@NATIVE@", # (os.path.join(self.path_runs.get_path_execution(), self.opts.inputPDBRefName))) # os.system(aux_command) # temp_rmsd = open("temporary_rmsd.xvg", "r") # for line in temp_rmsd.readlines(): # if line.find("@") < 0 and line.find("#") < 0: # rmsd_value = float(str(line).split()[1]) # only_pdb_file_name = os.path.basename(pdb) # dict_rmsd[only_pdb_file_name] = rmsd_value # temp_rmsd.close() # os.remove("temporary_rmsd.xvg") # Load Structures a = os.path.join(self.path_runs.get_path_execution(), pdb) aname, ext = os.path.splitext(pdb) b = os.path.join(self.path_runs.get_path_execution(), self.opts.inputPDBRefName) bname, ext = os.path.splitext(self.opts.inputPDBRefName) pymol.cmd.load(a, aname) pymol.cmd.load(b, bname) ret = pymol.cmd.align(aname, bname) rms = list(ret) print 'rmds %s, atoms %s, residues %s' % (rms[0], rms[1], rms[-1]) only_pdb_file_name = os.path.basename(pdb) dict_rmsd[only_pdb_file_name] = round(rms[0], 3) sleep(0.25) # (in seconds) pymol.cmd.reinitialize() return OrderedDict(sorted(dict_rmsd.items(), key=lambda x: x[1])) except Exception, e: show_error_message("Erro on getRMSDValues method:\n%s" % e)
def open_url(url, path_output, file_name): """ Download the file from `url` and save it locally under `file_name` """ try: response = urllib2.urlopen(url) file_result = os.path.join(path_output, file_name) out_file = open(file_result, 'wb') out_file.write(response.read()) out_file.close() except Exception, e: show_error_message("Error when openURL:\n%s" % e)
def set_command(self, path_execution, algorithm): try: if self.get_framework() == '2PG': self.command = '/usr/local/bin/%s' % (algorithm) else: self.command = '%s%s' % (path_execution, algorithm) # elif self.get_framework() in ('MEAMT', 'i-paes'): # self.command = '%s%s' % (get_path_algorithms(self.get_framework()), algorithm) # else: # self.command = '%s%s' % (path_execution, algorithm) except Exception, e: show_error_message("Error when set_command\n%s" % e)
def move_structures(self, pdbfile): """ Move a PDB file to a specific directory @type self: koala.CheckPDBStructure.CheckPDBStructure @type pdbfile: file """ try: directory = os.path.join( self.path_runs.get_path_execution(), 'no_accepted_by_pdb2gmx') if(not os.path.exists(directory)): os.makedirs(directory) shutil.move(pdbfile, directory) except Exception, e: show_error_message(str(e))
def delete_check_files(path_execution): """ Delete the temporary files generated by GROMACS @type self: koala.CheckPDBStructure.CheckPDBStructure """ try: if os.path.isfile(os.path.join(path_execution, "check.top")): os.remove(os.path.join(path_execution, "check.top")) if os.path.isfile(os.path.join(path_execution, "check.gro")): os.remove(os.path.join(path_execution, "check.gro")) if os.path.isfile(os.path.join(path_execution, "posre.itp")): os.remove(os.path.join(path_execution, "posre.itp")) except Exception, e: show_error_message(str(e))
def loadMatrixFile(self, matrix): """ Read the input matrix file with the objectivies values """ try: newResultFiles = open(self.path_runs.get_path_execution() + "objectivies.txt", "wr") for line in file(matrix, "r"): newResultFiles.write(line) newResultFiles.close() except Exception, e: show_error_message("Error on loadMatrixFile.\n%s" % e)
def send_multiple_outputs(path, files, path_output, outputID): try: for pdb in files: # pdb_name = pdb.split(".")[0].split("_")[2] name, ext = os.path.splitext(pdb) ext = ext.replace('.', '') pdb_name = os.path.basename(pdb) new_name = "%s_%s_%s_%s_%s" % ('primary', outputID, pdb_name, 'visible', ext) f = os.path.join(path, pdb) dest = path_output + '/' + new_name copia = "cp " + f + " " + dest os.system(copia) except Exception, e: show_error_message("Error when sendMultipleOutputs:\n%s" % e)
def buildTMScoreTable(self): """ Create an HTML table with the calculated TM-Score values @type self: koala.CalculateTMScore.CalculateTMScore """ try: html = '' for key, value in self.tmscore_value.items(): html += '<tr>' html += '<td>%s</td>' % str(key) # PDBFile html += '<td align="center">%s</td>' % str(value) # TMScore html += '</tr>' return html except Exception, e: show_error_message("Error while buildTMScoreTable:\n%s" % e)
def prepare_pdb(path_execution, path_galaxy): try: cl = [ '%s/scripts/prepare_structures.py' % path_galaxy, path_execution, '&'] retProcess = subprocess.Popen(cl, 0, None, None, None, False) pvalue = retProcess.wait() if pvalue != 0: return False return True except Exception, e: show_error_message("Error while preparing PDBs:\n%s" % e)
def residue_renumber(path_execution, path_galaxy): try: cl = [ '%s/scripts/residue_renumber_all_pdbs.py' % path_galaxy, path_execution, get_path_gromacs(), '&'] retProcess = subprocess.Popen(cl, 0, None, None, None, False) pvalue = retProcess.wait() if pvalue != 0: return False return True except Exception, e: show_error_message("Error while renumbering PDBs:\n%s" % e)
def merge_pdb(path, pdbs): try: if not pdbs: show_error_message("There is no PDB file to merge.") new_pdb = [] header = [] for idx, pdb_file in enumerate(pdbs): pdb = open(os.path.join(path, pdb_file), "r") for line in pdb: if idx == 0: # primeiro arquivo, copia o cabeçalho if ( not line.startswith("ATOM") and not line.startswith("MODEL") and not line.startswith("TER") and not line.startswith("ENDMDL") ): header.append(line) elif not line.startswith("ATOM"): continue else: new_pdb.append(line) else: if not line.startswith("ATOM"): continue else: new_pdb.append(line) new_pdb.append("TER\n") new_pdb.append("ENDMDL\n") if (idx + 2) <= len(pdbs): new_pdb.append("MODEL %s\n" % (idx + 2)) pdbf = os.path.join(path, "MergedPDB.pdb") new_pdb_f = file(pdbf, "wr") new_pdb_f.write("".join(header)) new_pdb_f.write("".join("MODEL 1\n")) new_pdb_f.write("".join(new_pdb)) new_pdb_f.write("\n") new_pdb_f.close() return pdbf except Exception, e: show_error_message("Error when mergePDB:\n%s" % e)
def minimization(path_execution, path_galaxy, pdbPrefix=''): try: cl = ['%s/min.sh' % path_execution, path_execution, get_path_gromacs(), pdbPrefix, '&'] shutil.copy( os.path.join( '%s/scripts/%s' % (path_galaxy, 'min.sh')), path_execution) retProcess = subprocess.Popen(cl, 0, None, None, None, False) pvalue = retProcess.wait() if pvalue != 0: return False return True except Exception, e: show_error_message("Error while minimization PDBs:\n%s" % e)
def build_images(methods, path_execution): """ Build images from PDB files using PyMol package. @type self: koala.CalculateGDTTS.CalculateGDTTS """ import __main__ __main__.pymol_argv = ['pymol', '-qc'] import pymol __main__.pymol = pymol pymol.finish_launching() try: os.chdir(path_execution) limit = 20 if len(methods) < 20: limit = len(methods) for i in range(0, limit): pdb = methods[i] arq = os.path.join(path_execution, pdb) name, ext = os.path.splitext(pdb) # Load Structures pymol.cmd.load(arq, pdb) pymol.cmd.disable("all") pymol.cmd.set('ray_opaque_background', 0) pymol.cmd.set('antialias', 1) pymol.cmd.hide("everything") pymol.cmd.show("cartoon") pymol.cmd.show("ribbon") pymol.cmd.enable(pdb) pymol.cmd.ray() pymol.cmd.png("%s.png" % name, dpi=300) sleep(0.25) # (in seconds) pymol.cmd.reinitialize() except Exception, e: show_error_message("Erro on build_images method:\n%s" % e)
def copy_files_to_execute_folder(path, prefix_filename=None): try: job_folder = os.path.join(path, 'out') contents = os.walk(job_folder) for root, folders, files in contents: for folder in folders: pass for file_name in files: name, ext = os.path.splitext(file_name) if(ext == '.pdb'): src = os.path.join(root, file_name) if(prefix_filename): new_filename = prefix_filename + '-' + file_name dst = os.path.join(path, new_filename) else: dst = os.path.join(path, file_name) shutil.copy(src, dst) except Exception, e: show_error_message("Error when copyFilesToExecuteFolder:\n%s" % e)
def buildRMSDTable(self): """ Create an HTML table with the calculated RMSD values @type self: koala.CalculateRMSD.CalculateRMSD """ try: html = '' for key, value in self.rmsd_values.items(): html += '<tr>' html += '<td>%s</td>' % str(key) # PDBFile # if(math.isinf(value)): # value = '> 15 Å' # else: # value = '%d Å' % value * 10 html += '<td align="center">%s Å</td>' % str(value) # RMSD html += '</tr>' return html except Exception, e: show_error_message("Error while buildRMSDTable:\n%s" % e)
def log_by_pdb2gmx(self, pdbfile, stderr=None, stdout=None): """ Write the log file with the stderr from pdb2gmx @type self: koala.CheckPDBStructure.CheckPDBStructure @type pdbfile: file @type stderr: string @type stdout: string """ try: f_log = open( os.path.join(self.path_runs.get_path_execution(), self.check_pdb_log), "a+") pdbfile = os.path.split(pdbfile)[1] outline = "STARTING LOG OF " + pdbfile + "\n" f_log.write(outline) outline = stderr + "\n" f_log.write(outline) outline = "FINISHED LOG OF " + pdbfile + "\n\n\n" f_log.write(outline) f_log.close() except Exception, e: show_error_message(str(e))
def set_execution_directory(self, email=None): try: now = datetime.datetime.now() tupla = now.timetuple() rand = random.randint(0, 1000) try: if email is None: nome_diretorio = ( str(tupla[2]) + str(tupla[1]) + str(tupla[0]) + "_" + str(tupla[3]) + str(tupla[4]) + str(tupla[5]) + "_" + str(rand) + "/" ) else: nome_diretorio = ( email + str(tupla[2]) + str(tupla[1]) + str(tupla[0]) + "_" + str(tupla[3]) + str(tupla[4]) + str(tupla[5]) + "_" + str(rand) + "/" ) os.mkdir(os.path.join(self.get_path_execute(), nome_diretorio)) os.chmod(os.path.join(self.get_path_execute(), nome_diretorio), stat.S_IRWXU) except Exception, e: show_error_message("Error when CreateExecutionDirectory\n%s" % e) self.path_execution = os.path.join(self.get_path_execute(), nome_diretorio)