def test_run_all_success(self): temp_dir = tempfile.mkdtemp() try: fakeftp = FtpFileTransfer(None) mftp = D3RParameters() fakeftp.set_connection(mftp) fakeftp.set_remote_dir('/foo2') mftp.get = Mock() params = D3RParameters() params.pdbfileurl = 'file://' + temp_dir params.compinchi = 'file://' + temp_dir make_blast = MakeBlastDBTask(temp_dir, params) make_blast.create_dir() open(os.path.join(make_blast.get_dir(), D3RTask.COMPLETE_FILE), 'a').close() task = DataImportTask(temp_dir, params) task.set_file_transfer(fakeftp) task._retrysleep = 0 open(os.path.join(temp_dir, task.NONPOLYMER_TSV), 'a').close() open(os.path.join(temp_dir, task.SEQUENCE_TSV), 'a').close() open(os.path.join(temp_dir, task.OLDSEQUENCE_TSV), 'a').close() open(os.path.join(temp_dir, task.CRYSTALPH_TSV), 'a').close() open(os.path.join(temp_dir, task.COMPINCHI_ICH), 'a').close() task.run() self.assertEquals(task.get_error(), None) # check line count is 1 now which indicates # standard was added self.assertEqual( util.get_file_line_count(task.get_nonpolymer_tsv()), 1) self.assertEqual(util.get_file_line_count(task.get_sequence_tsv()), 1) self.assertEqual( util.get_file_line_count(task.get_oldsequence_tsv()), 1) self.assertEqual( util.get_file_line_count(task.get_crystalph_tsv()), 1) mftp.get.assert_called_with('/foo2/' + DataImportTask.PARTICIPANT_LIST_CSV, local=task.get_participant_list_csv()) finally: shutil.rmtree(temp_dir)
def test_run_all_success_except_participant_download_fails(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() params.pdbfileurl = 'file://' + temp_dir params.compinchi = 'file://' + temp_dir make_blast = MakeBlastDBTask(temp_dir, params) make_blast.create_dir() open(os.path.join(make_blast.get_dir(), D3RTask.COMPLETE_FILE), 'a').close() task = DataImportTask(temp_dir, params) task._retrysleep = 0 open(os.path.join(temp_dir, task.NONPOLYMER_TSV), 'a').close() open(os.path.join(temp_dir, task.SEQUENCE_TSV), 'a').close() open(os.path.join(temp_dir, task.OLDSEQUENCE_TSV), 'a').close() open(os.path.join(temp_dir, task.CRYSTALPH_TSV), 'a').close() open(os.path.join(temp_dir, task.COMPINCHI_ICH), 'a').close() task.run() self.assertEquals(task.get_error(), None) # check line count is 1 now which indicates # standard was added self.assertEqual( util.get_file_line_count(task.get_nonpolymer_tsv()), 1) self.assertEqual(util.get_file_line_count(task.get_sequence_tsv()), 1) self.assertEqual( util.get_file_line_count(task.get_oldsequence_tsv()), 1) self.assertEqual( util.get_file_line_count(task.get_crystalph_tsv()), 1) self.assertTrue(task.get_email_log().startswith( '\nWARNING: Unable to download')) finally: shutil.rmtree(temp_dir)
def run(self): """Runs blastnfilter task after verifying dataimport was good Method requires can_run() to be called before hand with successful outcome Otherwise method invokes D3RTask.start then this method creates a directory and invokes blastnfilter script and postanalysis script. Upon completion results are analyzed and success or error status is set appropriately and D3RTask.end is invoked """ super(BlastNFilterTask, self).run() if self._can_run is False: logger.debug(self.get_dir_name() + ' cannot run cause _can_run flag ' 'is False') return data_import = DataImportTask(self._path, self._args) make_blastdb = MakeBlastDBTask(self._path, self._args) try: loglevel = self.get_args().loglevel except AttributeError: logger.debug('No log level set in arguments using WARNING') loglevel = 'WARNING' # verify sequence.tsv file exists on filesystem. # if not fall back to oldsequence.tsv file sequencetsv = data_import.get_sequence_tsv() if not os.path.isfile(sequencetsv): logger.warning(sequencetsv + ' file not found. falling ' 'back to old file') self.append_to_email_log('\n ' + sequencetsv + ' file not found ' + 'falling back to ' + data_import.get_oldsequence_tsv() + '\n') sequencetsv = data_import.get_oldsequence_tsv() cmd_to_run = (self.get_args().blastnfilter + ' --nonpolymertsv ' + data_import.get_nonpolymer_tsv() + ' --sequencetsv ' + sequencetsv + ' --pdbblastdb ' + make_blastdb.get_dir() + ' --compinchi ' + data_import.get_components_inchi_file() + ' --crystalpH ' + data_import.get_crystalph_tsv() + ' --pdbdb ' + self.get_args().pdbdb + ' --log ' + loglevel + ' --outdir ' + self.get_dir()) blastnfilter_name = os.path.basename(self.get_args().blastnfilter) self.run_external_command( blastnfilter_name, cmd_to_run, False, ) self.set_status(D3RTask.COMPLETE_STATUS) cmd_to_run = (self.get_args().postanalysis + ' --compinchi ' + data_import.get_components_inchi_file() + ' ' + self.get_dir()) postanalysis_name = os.path.basename(self.get_args().postanalysis) self.run_external_command(postanalysis_name, cmd_to_run, False) try: # examine output to get candidate hit count DR-12 hit_stats = self._parse_blastnfilter_output_for_hit_stats() if hit_stats is not None: self.append_to_email_log(hit_stats) except Exception: logger.exception("Error caught exception") # assess the result self.end()
def test_run_with_blast_success_useoldseq_and_postanalysis_fail(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() params.blastnfilter = '/bin/echo' params.postanalysis = os.path.join(temp_dir, 'foo.py') params.pdbdb = '/pdbdb' blasttask = BlastNFilterTask(temp_dir, params) blasttask._can_run = True txt_file = os.path.join(blasttask.get_dir(), 'summary.txt') txt_contents = ('INPUT SUMMARY\\n' + ' sequences: 177\\n' + ' complexes: 149\\n') # create fake blastnfilter script that makes csv files f = open(params.postanalysis, 'w') f.write('#! /usr/bin/env python\n\n') f.write('f = open(\'' + txt_file + '\', \'w\')\n') f.write('f.write(\'' + txt_contents + '\\n\')\n') f.write('f.flush()\nf.close()\n') f.flush() f.close() os.chmod(params.postanalysis, stat.S_IRWXU) blasttask.run() self.assertEqual(blasttask.get_status(), D3RTask.COMPLETE_STATUS) self.assertEqual(blasttask.get_error(), None) complete_file = os.path.join(blasttask.get_dir(), D3RTask.COMPLETE_FILE) self.assertEqual(os.path.isfile(complete_file), True) std_err_file = os.path.join(blasttask.get_dir(), 'echo.stderr') self.assertEqual(os.path.isfile(std_err_file), True) std_out_file = os.path.join(blasttask.get_dir(), 'echo.stdout') dataimport = DataImportTask(temp_dir, params) makeblast = MakeBlastDBTask(temp_dir, params) f = open(std_out_file, 'r') echo_out = f.read().replace('\n', '') echo_out.index('--nonpolymertsv ' + os.path.join(temp_dir, dataimport.get_dir_name(), DataImportTask.NONPOLYMER_TSV)) echo_out.index(' --sequencetsv ' + os.path.join(temp_dir, dataimport.get_dir_name(), DataImportTask.OLDSEQUENCE_TSV)) echo_out.index(' --pdbblastdb ' + os.path.join(temp_dir, makeblast.get_dir_name())) echo_out.index(' --compinchi ' + os.path.join(temp_dir, dataimport.get_dir_name(), DataImportTask.COMPINCHI_ICH)) echo_out.index(' --outdir ' + os.path.join(temp_dir, blasttask.get_dir_name())) echo_out.index(' --crystalpH ' + os.path.join(temp_dir, dataimport.get_dir_name(), DataImportTask.CRYSTALPH_TSV)) echo_out.index(' --pdbdb /pdbdb ') f.close() self.assertEqual(os.path.isfile(std_out_file), True) self.assertEquals(blasttask.get_status(), D3RTask.COMPLETE_STATUS) self.assertEquals( os.path.exists( os.path.join(blasttask.get_dir(), 'foo.py.stderr')), True) self.assertEquals( os.path.exists( os.path.join(blasttask.get_dir(), 'foo.py.stdout')), True) res = blasttask.get_email_log().rstrip('\n') res.index('/bin/echo') res.index('# txt files found: 0') res.index('Output from summary.txt') res.index(' sequences: 177') res.index(' complexes: 149') res.index(dataimport.get_sequence_tsv() + ' file not found falling back to ' + dataimport.get_oldsequence_tsv()) finally: shutil.rmtree(temp_dir)