def run(self): file_traf = self.file_TRAF file_seq = self.file_SEQ self.setup_job() if self.reference_pdb: self.parse_reference(self.reference_pdb) with_cabs = None in (file_traf, file_seq) if with_cabs: self.setup_cabs_run() self.execute_cabs_run() if self.save_cabs_files: self.save_cabs_res() self.load_output(file_traf, file_seq) self.score_results(n_filtered=self.filtering_count, number_of_medoids=self.clustering_medoids, number_of_iterations=self.clustering_iterations) self.save_models() if self.reference_pdb: try: self.calculate_rmsd() except (ValueError, AlignError) as e: logger.critical(module_name=_name, msg=e.message) self.save_config_file() self.draw_plots(colors=self.colors) if self.load_cabs_files: for _file in _CABS_files: os.remove(os.path.join(self.work_dir, _file)) logger.info(module_name=_name, msg='Simulation completed successfully')
def run(cabs_cmd, cmd_line=sys.argv[1:]): if cabs_cmd not in ['dock', 'flex']: raise IndexError module_name = 'CABS' + cabs_cmd parser = getattr(optparser, cabs_cmd + '_parser') pre_parser = argparse.ArgumentParser(add_help=False) pre_parser.add_argument('-c', '--config') pre_parser.add_argument('--version', action='store_true') pre_parser.add_argument('-h', '--help', action='store_true') pre_args, remains = pre_parser.parse_known_args(cmd_line) if pre_args.help: _help = parser.format_help() print re.sub('\n( *)\n( *)\n', '\n\n', _help) sys.exit(0) elif pre_args.version: print __version__ sys.exit(0) elif pre_args.config: remains = optparser.ConfigFileParser(pre_args.config).args + remains config = vars(parser.parse_args(remains)) import CABS.job task = getattr(CABS.job, cabs_cmd.title() + 'Task') job = task(**config) try: job.run() except KeyboardInterrupt: logger.critical(module_name, 'Interrupted by user.') except Exception as e: logger.exit_program(module_name, 'Error occurred', traceback=_tr.format_exc(), exc=e) finally: logger.close_log() for _file in _JUNK: rmtree(_file, ignore_errors=True)
def build_exe(params, src, exe='cabs', build_command=FORTRAN_COMMAND, destination='.'): with open(src) as f: lines = f.read() names = ['NDIM', 'NREPS', 'NMOLS', 'MAXRES'] for name, value in zip(names, params): lines = re.sub(name + '=\d+', name + '=%i' % value, lines) run_cmd = join(destination, exe) cmd = build_command.split() + ['-o', run_cmd, '-x', 'f77', '-'] try: out, err = Popen(cmd, stdin=PIPE, stderr=PIPE).communicate(lines) if err: raise Exception(err) except Exception as e: logger.critical(_name, msg=e.message) return run_cmd
def load_cabs_results(self): if not os.path.exists(self.load_cabs_files): logger.exit_program( module_name=_name, msg="Provided CABS files path does not exist (%s)" % self.load_cabs_files, traceback=False) try: files = glob.glob(os.path.join(self.load_cabs_files, "*.cbs")) if len(files) > 1: logger.critical( module_name=_name, msg="More than one .cbs file in provided directory %s " % " \n".join(files)) logger.exit_program( module_name=_name, msg="Please re-run with --load-cabs-files <filename> " "or remove the files you do not need. Quiting.", traceback=False) elif len(files) == 1: logger.info(module_name=_name, msg="Loading CABS files from %s" % files[0]) with tarfile.open(files[0], "r:gz") as f: f.extractall(os.path.join(self.work_dir)) else: raise IOError except IOError: files_loc = self.load_cabs_files try: with tarfile.open(files_loc, "r:gz") as f: logger.info(module_name=_name, msg="Loading CABS files from %s" % files_loc) f.extractall(os.path.join(self.work_dir)) except IOError: raise return
def dssp(self, output=''): """Runs dssp on the read pdb file and returns a dictionary with secondary structure""" out = err = None try: proc = Popen([self.DSSP_COMMAND, '/dev/stdin'], stdin=PIPE, stdout=PIPE, stderr=PIPE) out, err = proc.communicate(input=self.body) logger.debug( module_name=_name, msg='Running DSSP' ) except OSError: logger.warning( module_name=_name, msg='DSSP not found.' ) tempfile = mkstemp(suffix='.pdb', prefix='.tmp.dssp.', dir=PDB_CACHE)[1] with open(tempfile, 'wb') as f: f.write(self.body) try: logger.debug( module_name=_name, msg='Submitting structure to the DSSP server' ) out, err = self.xssp(tempfile) except (HTTPError, ConnectionError): logger.warning( module_name=_name, msg='Cannot connect to the DSSP server. DSSP was not ran at all.' ) return None finally: try: os.remove(tempfile) except OSError: pass if err: logger.critical( module_name=_name, msg='DSSP ERROR: %s' % err.replace('\n', ' ') ) return None else: logger.debug(_name, 'DSSP successful') if logger.log_files() and output: output = os.path.join(output, 'output_data', 'DSSP_output_%s.txt' % self.name) d = os.path.dirname(output) if not isdir(d): os.makedirs(d) logger.to_file( filename=output, content=out, msg='Saving DSSP output to %s' % output ) sec = OrderedDict() p = '^([0-9 ]{5}) ([0-9 ]{4}.)([A-Z ]) ([A-Z]) ([HBEGITS ])(.*)$' for line in out.split('\n'): m = re.match(p, line) if m: key = str(m.group(2).strip() + ':' + m.group(3)) if m.group(5) in 'HGI': val = 'H' elif m.group(5) in 'BE': val = 'E' elif m.group(5) in 'T': val = 'T' else: val = 'C' sec[key] = val return sec