def call(container, command, cwd='', paths={}, remove=True): ###load container Client.load(container) ###setup mount point paths #{"/path/outside":"/path/incontainer"} volumes = [] if paths: for key in paths.keys(): volumes.append(key + ':' + paths[key]) ###setup command as a list command_list = shlex.split(command) ###run the container output = Client.execute(command_list, bind=volumes, options=['--pwd', cwd, '--cleanenv'], stream=True) try: ###stream the output for line in output: yield line.strip() #catch singularity errors from non-zero exit #TODO figure out how to read them except subprocess.CalledProcessError: pass
def make_separate_directory(file_all, protein, source): #shutil.rmtree('trajectories') isdir = os.path.isdir('trajectories') if isdir == False: print('Trajectories do not exist') os.mkdir('trajectories') else: print('exist') for count, file in enumerate(file_all, start=0): ismod = os.path.isdir(f'./trajectories/model_{count}') if ismod == False: os.mkdir(f'./trajectories/model_{count}') with open(f'./trajectories/model_{count}/position_ligand_{count}.pdbqt', 'w') as file_traj: file_traj.write(file) shutil.copy(protein, f'./trajectories/model_{count}/') # nakopiruje navic potrebna data shutil.copy(f'{source}/_Xqmin_tmp.in', f'./trajectories/model_{count}/') shutil.copy(f'{source}/_11_run_tleap.sh', f'./trajectories/model_{count}/') shutil.copy(f'{source}/_21_run-mm_meta.sh', f'./trajectories/model_{count}/') shutil.copy(f'{source}/{protein}', f'./trajectories/model_{count}/') shutil.copy(f'{source}/ligand.prepi', f'./trajectories/model_{count}/') try: subprocess.run(f'rm ./trajectories/model_{count}/emin2*', shell = True) except: pass try: subprocess.run(f'rm ./trajectories/model_{count}/complex.inpcrd', shell = True) except: pass try: subprocess.run(f'rm ./trajectories/model_{count}/complex.prmtop', shell = True) except: pass # convert traj_position_ligand_{count}.pdbqt to pdb -> singularity Client.load('/home/petrahrozkova/Stažené/caverdock_1.1.sif') Client.execute(['/opt/mgltools-1.5.6/bin/pythonsh', '/opt/mgltools-1.5.6/MGLToolsPckgs/AutoDockTools/Utilities24/pdbqt_to_pdb.py', '-f',os.getcwd()+'/trajectories/model_'+str(count)+'/position_ligand_*.pdbqt', '-o', os.getcwd()+'/trajectories/model_'+str(count)+'/ligand.pdb']) subprocess.call(f'sed -i \'s/<0> d/TIP d/g\' ./trajectories/model_{count}/ligand.pdb', shell = True) #ligand_for_complex.pdb subprocess.call(f'tail -n +2 \"./trajectories/model_{count}/ligand.pdb\" > ' f'\"./trajectories/model_{count}/ligand_for_complex.pdb\"', shell = True) # spojit 'hloupe" ligand, protein do complex.pdb subprocess.call( #remove last line in file with END. IMPORTANT! f'head -n -1 ./trajectories/model_{count}/{protein} > ./trajectories/model_{count}/complex.pdb | ' f'echo TER >> ./trajectories/model_{count}/complex.pdb', shell=True) subprocess.call(f'cat ./trajectories/model_{count}/ligand_for_complex.pdb' f' >> ./trajectories/model_{count}/complex.pdb ', #f'| echo TER >> ./trajectories/model_{count}/complex.pdb', shell=True) subprocess.call(f'echo END >> ./trajectories/model_{count}/complex.pdb', shell=True)
def run(self, cmd, bind_dir): self._make_dir() self._pull_image() client.load(os.path.join(self.img_dir, self.img_name)) output = client.execute(cmd, bind=bind_dir + ":/data", options=['--pwd', '/data', '--cleanenv'], stream=True) return output
def run_case(image_dir, image_name, testcase_dir): client.load(os.path.join(image_dir, image_name)) output = client.execute( ['mpirun', '-np', '6', '/SU2/bin/SU2_CFD', 'Jones.cfg'], bind=testcase_dir + ":/data:", options=['--pwd', '/data'], stream=True) for val in output: print(val.strip('\n'))
def run(self, cmd, cores, cfg, log): self._pull_image() client.load(os.path.join(self.image_dir, self.image_name)) totalcmd = ['mpirun', '-np', str(cores), '/SU2/bin/' + cmd, cfg.fname] output = client.execute( totalcmd, bind=','.join([self.case_dir + ":/data", self.mesh_dir + ":/mesh"]), options=['--pwd', '/data', '--cleanenv'], stream=True) with open(log.fname, 'w', buffering=10) as lfile: for line in output: lfile.write(line)
def remove_ligand_from_emin(protein, verbose, configfile): # ted defaultne vypocitany emin5.pdb with open(protein) as oldfile, open('protein.pdb', 'w+') as newfile: for line in oldfile: if not configfile["LIGAND"]["name"] in line: newfile.write(line) # convert pdb to pdbqt prepare_receptor = '' if int(configfile["SINGULARITY"]["value"]) == 1: Client.load(configfile["SINGULARITY"]["singularity"]) prepare_receptor = Client.execute([ configfile["PREPARE_RECEPTOR"]["path_prepare_receptor"], '-r', 'protein.pdb' ]) if verbose: print( f'{configfile["PREPARE_RECEPTOR"]["path_prepare_receptor"]} -r protein.pdb' ) if not os.path.isfile('protein.pdbqt'): logging.error( f'Cannot run prepare_receptor. Try: \n {configfile["PREPARE_RECEPTOR"]["path_prepare_receptor"]} -r protein.pdb \n Message: \n {prepare_receptor}' ) if verbose: print( f'Cannot run prepare_receptor. Try: \n {configfile["PREPARE_RECEPTOR"]["path_prepare_receptor"]} -r protein.pdb \n {prepare_receptor}' ) sys.exit(1) else: subprocess.call( f'{configfile["PREPARE_RECEPTOR"]["path_prepare_receptor"]} -r protein.pdb', shell=True) logging.info( f'Run prepare_receptor. Message: \n {configfile["PREPARE_RECEPTOR"]["path_prepare_receptor"]} -r protein.pdb ' ) if verbose: print( f'{configfile["PREPARE_RECEPTOR"]["path_prepare_receptor"]} -r protein.pdb' ) print(f'Message{prepare_receptor}') if not os.path.isfile('protein.pdbqt'): logging.error( f'Cannot run prepare_receptor. Try: \n {configfile["PREPARE_RECEPTOR"]["path_prepare_receptor"]} -r protein.pdb \n' ) if verbose: print( f'Cannot run prepare_receptor. Try: \n {configfile["PREPARE_RECEPTOR"]["path_prepare_receptor"]} -r protein.pdb' ) sys.exit(1) check_exist_file('protein.pdbqt')
def call(container, command, cwd='', paths={}, remove=True): ###load container container = 'docker://' + container Client.load(container) ###setup mount point paths #{"/path/outside":"/path/incontainer"} volumes = [] if paths: for key in paths.keys(): volumes.append(key + ':' + paths[key]) ###setup command as a list command_list = command.split() ###run the container output = Client.execute(command_list, bind=volumes, writable=True, options=['--pwd', cwd]) #once container is finished return output as a string return output
def run_cd_energy_profile(tunnel, traj, configfile, verbose): file = '' try: if int(configfile["SINGULARITY"]["value"]) == 1: Client.load(str(configfile['SINGULARITY']['singularity'])) if verbose: print( f'{configfile["CD-ENERGYPROFILE"]["path_cd-energyprofile"]} -d {os.getcwd()}/{str(tunnel)} -t {str(traj)} -s {str(configfile["CPU"]["cpu"])}' ) if int(configfile["SINGULARITY"]["value"]) == 1: file = Client.execute([ 'cd-energyprofile', '-d', os.getcwd() + '/' + str(tunnel), '-t', str(traj), '-s', str(configfile['CPU']['cpu']) ]) with open(f'{os.getcwd()}/energy.dat', 'w') as file_energy_dat: file_energy_dat.write(str(file)) else: subprocess.call( f'{configfile["CD-ENERGYPROFILE"]["path_cd-energyprofile"]} -d {os.getcwd()}/{str(tunnel)} -t {str(traj)} -s {str(configfile["CPU"]["cpu"])} > energy.dat', shell=True) if verbose: print(f'Message from cd-energyprofile: \n {file}') logging.info(f'Message from cd-energyprofieL: \n {file}') except: if verbose: print('cd-energyprofile returncode is not 0. Check logfile.') logging.error(f'cd-energyprofile returncode is not 0.') sys.exit(1) if not os.path.exists('energy.dat'): logging.error(f'Cannot make energy.dat') if verbose: print('Energy.dat not exists') sys.exit(1)
def build_to_singularity(definition_entry, container_location): """Builds a Singularity container from a Dockerfile or Singularity file within the definition db. Parameters: definition_entry (str): Entry of definition db entry to build singularity container from. container_location (str): Path to location to build the container. Returns: container_location: Returns the location of the Singularity container or None if it fails to save. """ definition_id = definition_entry["definition_id"] pull_s3_dir(definition_id) Client.load(PROJECT_ROOT + definition_id) Client.build(image=os.path.join(PROJECT_ROOT, container_location), sudo=False) shutil.rmtree(PROJECT_ROOT + definition_id) #TODO Find a better way to error check if os.path.exists(PROJECT_ROOT + container_location): logging.info(f"Successfully built {container_location}") return container_location else: return None
def run_caverdock(ligand, tunnel, configfile, verbose): # IN: protein, ligand, tunel # OUT: pdbqt file with lb and ub prepare_conf = '' if int(configfile["SINGULARITY"]["value"]) == 1: singularity = Client.load(str( configfile["SINGULARITY"]["singularity"])) logging.info( f'Singularity for caverdock: {configfile["SINGULARITY"]["singularity"]} \n' ) logging.info(f'Message from singularity: \n {singularity}') if verbose: print( f'Singularity for caverdock: {configfile["SINGULARITY"]["singularity"]} \n' ) print(f'Message from singularity: \n {singularity} \n') prepare_conf = Client.execute([ configfile["CD-PREPARECONF"]["path_cd-prepareconf"], '-r', 'protein.pdbqt', '-l', str(ligand), '-t', str(tunnel) ]) with open('caverdock.conf', 'w+') as file_conf: file_conf.write(prepare_conf) if not os.path.isfile('caverdock.conf'): logging.error( f'cd-prepareconf -r protein.pdbqt -l {ligand} -t {tunnel} > caverdock.conf' ) logging.error(f'Message from cd-prepareconf: \n {prepare_conf}') if verbose: print( f'ERROR: {configfile["CD-PREPARECONF"]["path_cd-prepareconf"]} -r protein.pdbqt -l {ligand} -t {tunnel} > caverdock.conf' ) print(f'ERROR: Message from cd-prepareconf: \n {prepare_conf}') sys.exit(1) else: subprocess.call( f'{configfile["CD-PREPARECONF"]["path_cd-prepareconf"]} -r protein.pdbqt -l {ligand} -t {tunnel} > caverdock.conf', shell=True) logging.info( f'cd-prepareconf -r protein.pdbqt -l {ligand} -t {tunnel} > caverdock.conf' ) #logging.info(f'Message from cd-prepareconf: \n {prepare_conf}') if verbose: print( f'{configfile["CD-PREPARECONF"]["path_cd-prepareconf"]} -r protein.pdbqt -l {ligand} -t {tunnel} > caverdock.conf' ) #print(f'Message from cd-prepareconf: \n {prepare_conf}') if not os.path.isfile('caverdock.conf'): logging.error( f'cd-prepareconf -r protein.pdbqt -l {ligand} -t {tunnel} > caverdock.conf' ) #logging.error(f'Message from cd-prepareconf: \n {prepare_conf}') if verbose: print( f'ERROR: {configfile["CD-PREPARECONF"]["path_cd-prepareconf"]} -r protein.pdbqt -l {ligand} -t {tunnel} > caverdock.conf' ) #print(f'ERROR: Message from cd-prepareconf: \n {prepare_conf}') sys.exit(1) mpirun = '' if int(configfile["SINGULARITY"]["value"]) == 1: mpirun = Client.execute([ 'mpirun', '-np', configfile["CPU"]["cpu"], 'caverdock', '--config', 'caverdock.conf', '--out', str(configfile["RESULT_CD"]["name"]) ]) else: subprocess.call( f'/usr/bin/mpirun.mpich -np {str(configfile["CPU"]["cpu"])} {configfile["CAVERDOCK"]["path_caverdock"]} --config caverdock.conf --out {str(configfile["RESULT_CD"]["name"])}', shell=True) logging.info( f'mpirun -np {str(configfile["CPU"]["cpu"])} caverdock --config caverdock.conf --out {str(configfile["RESULT_CD"]["name"])}' ) logging.info(f'Message from mpirun: \n {mpirun}') if verbose: print( f'mpirun -np {str(configfile["CPU"]["cpu"])} caverdock --config caverdock.conf --out {str(configfile["RESULT_CD"]["name"])}' ) print(f'Message from mpirun: \n {mpirun}') if not (os.path.isfile(f'{configfile["RESULT_CD"]["name"]}-lb.pdbqt') or os.path.isfile(f'{configfile["RESULT_CD"]["name"]}-ub.pdbqt')): print(f'{configfile["RESULT_CD"]["name"]}-lb.pdbqt') logging.error( f'mpirun -np {str(configfile["CPU"]["cpu"])} caverdock --config caverdock.conf --out {str(configfile["RESULT_CD"]["name"])}' ) logging.error(f'Message from mpirun: \n {mpirun}') if verbose: print( f'ERROR: mpirun -np {str(configfile["CPU"]["cpu"])} caverdock --config caverdock.conf --out {str(configfile["RESULT_CD"]["name"])}' ) print(f'ERROR: Message from mpirun: \n {mpirun}') sys.exit(1)
def make_separate_directory(file_all, protein, source, configfile): try: shutil.rmtree('trajectories') isdir = False except: isdir = os.path.isdir('trajectories') if isdir == False: print('Trajectories do not exist') os.mkdir('trajectories') else: print('exist') for count, file in enumerate(file_all, start=0): ismod = os.path.isdir(f'./trajectories/model_{count}') if ismod == False: os.mkdir(f'./trajectories/model_{count}') with open( f'./trajectories/model_{count}/position_ligand_{count}.pdbqt', 'w') as file_traj: file_traj.write(file) shutil.copy(protein, f'./trajectories/model_{count}/') # nakopiruje navic potrebna data #shutil.copy(f'{source}/_11_run_tleap', f'./trajectories/model_{count}/') #shutil.copy(f'{source}/_21_run_prepare_sander', f'./trajectories/model_{count}/') shutil.copy(f'{source}/{protein}', f'./trajectories/model_{count}/') try: subprocess.run(f'rm ./trajectories/model_{count}/emin*', shell=True) except: pass try: subprocess.run(f'rm ./trajectories/model_{count}/complex.inpcrd', shell=True) except: pass try: subprocess.run(f'rm ./trajectories/model_{count}/complex.prmtop', shell=True) except: pass try: subprocess.run(f'rm ./trajectories/model_{count}/ligand.prepi', shell=True) except: pass # convert traj_position_ligand_{count}.pdbqt to pdb -> singularity if int(configfile["SINGULARITY"]["value"]) == 1: Client.load('/home/petrahrozkova/Stažené/caverdock_1.1.sif') Client.execute([ '/opt/mgltools-1.5.6/bin/pythonsh', '/opt/mgltools-1.5.6/MGLToolsPckgs/AutoDockTools/Utilities24/pdbqt_to_pdb.py', '-f', os.getcwd() + '/trajectories/model_' + str(count) + '/position_ligand_*.pdbqt', '-o', os.getcwd() + '/trajectories/model_' + str(count) + '/ligand_H.pdb' ]) else: subprocess.call( f'{configfile["PDBQT_TO_PDB"]["path_pdbqt_to_pdb"]} -f {os.getcwd()}/trajectories/model_{str(count)}/position_ligand_{str(count)}.pdbqt ' f'-o {os.getcwd()}/trajectories/model_{str(count)}/ligand_H.pdb', shell=True) check_exist_file( f'{os.getcwd()}/trajectories/model_{str(count)}/ligand_H.pdb') subprocess.call( f'sed -i \'s/<0> d/{configfile["LIGAND"]["name"]} d/g\' ./trajectories/model_{count}/ligand_H.pdb', shell=True) #ligand_for_complex.pdb #subprocess.call(f'pdb4amber -i ./trajectories/model_{count}/ligand_H.pdb -o ./trajectories/model_{count}/ligand.pdb --nohyd', shell = True) check_exist_file( f'{os.getcwd()}/trajectories/model_{str(count)}/ligand_H.pdb') #subprocess.call(f'antechamber -i {ligand} -fi pdbqt -o ligand.pdb -if pdb', shell = True) subprocess.call( f'antechamber -i ./trajectories/model_{count}/ligand_H.pdb -fi pdb -o ./trajectories/model_{count}/ligand.prepi -fo prepi', shell=True) check_exist_file( f'{os.getcwd()}/trajectories/model_{str(count)}/ligand.prepi') subprocess.call( f'cat \"./trajectories/model_{count}/ligand_H.pdb\" > ' f'\"./trajectories/model_{count}/ligand_for_complex.pdb\"', shell=True) # split (ugly) ligand and protein into complex.pdb subprocess.call( f'pdb4amber -i ./trajectories/model_{count}/{protein} -o ./trajectories/model_{count}/protein_w_H.pdb --nohyd', shell=True) subprocess.call( #remove last line in file with END. IMPORTANT! f'head -n -1 ./trajectories/model_{count}/protein_w_H.pdb > ./trajectories/model_{count}/complex.pdb | ' f'echo TER >> ./trajectories/model_{count}/complex.pdb', shell=True) subprocess.call( f'cat ./trajectories/model_{count}/ligand_for_complex.pdb' f' >> ./trajectories/model_{count}/complex.pdb ', #f'| echo TER >> ./trajectories/model_{count}/complex.pdb', shell=True) subprocess.call( f'echo END >> ./trajectories/model_{count}/complex.pdb', shell=True) check_exist_file( f'{os.getcwd()}/trajectories/model_{str(count)}/complex.pdb') print(f'{os.getcwd()}') #subprocess.call(f'pdb4amber -i ./trajectories/model_{count}/complex_H.pdb -o ./trajectories/model_{count}/complex.pdb --nohyd', shell = True) check_exist_file( f'{os.getcwd()}/trajectories/model_{str(count)}/complex.pdb')