def test_untrimmed_paired_output(): with temporary_path("tmp-untrimmed.1.fastq") as untrimmed1: with temporary_path("tmp-untrimmed.2.fastq") as untrimmed2: def callback(aligner, infiles, outfiles, result): assert files_equal(cutpath('paired-untrimmed.1.fastq'), untrimmed1) assert files_equal(cutpath('paired-untrimmed.2.fastq'), untrimmed2) run_paired( [ '-a', 'TTAGACATAT', '--untrimmed-output', untrimmed1, '--untrimmed-paired-output', untrimmed2, ], in1='paired.1.fastq', in2='paired.2.fastq', expected1='paired-trimmed.1.fastq', expected2='paired-trimmed.2.fastq', callback=callback, )
def test_interleaved_no_paired_output(): with temporary_path("temp-paired.1.fastq") as p1: with temporary_path("temp-paired.2.fastq") as p2: params = '-a XX --interleaved'.split() with raises(SystemExit), redirect_stderr(): params += [ '-o', p1, '-p1', p2, 'paired.1.fastq', 'paired.2.fastq' ] execute_cli(params)
def test_explicit_format_with_paired(): # Use --format=fastq with input files whose extension is .txt with temporary_path("paired.1.txt") as txt1: with temporary_path("paired.2.txt") as txt2: shutil.copyfile(datapath("paired.1.fastq"), txt1) shutil.copyfile(datapath("paired.2.fastq"), txt2) run_paired( '--format=fastq -a TTAGACATAT -m 14', in1=txt1, in2=txt2, expected1='paired.m14.1.fastq', expected2='paired.m14.2.fastq', )
def run_paired( params, in1, in2, expected1, expected2, aligners=('adapter', ), callback=None, assert_files_equal=True, error_on_rc=True, ): if type(params) is str: params = params.split() for aligner in aligners: with temporary_path('tmp1-' + expected1.format(aligner=aligner)) as p1: with temporary_path('tmp2-' + expected2.format(aligner=aligner)) as p2: p = params.copy() p += ['--aligner', aligner, '-o', p1, '-p', p2] infiles = [ datapath(i.format(aligner=aligner)) for i in (in1, in2) ] for infile_args in zip(('-pe1', '-pe2'), infiles): p.extend(infile_args) command = get_command('trim') result = command.execute(p) assert isinstance(result, tuple) assert len(result) == 2 if error_on_rc: err = result[1]['exception'] if result[ 1] and 'exception' in result[1] else None # from traceback import format_exception if result[0] != 0: if err is None: raise AssertionError("Return code {} != 0".format( result[0])) else: raise AssertionError("Return code {} != 0".format( result[0])) from err['details'][1] if assert_files_equal: assert files_equal( cutpath(expected1.format(aligner=aligner)), p1) assert files_equal( cutpath(expected2.format(aligner=aligner)), p2) if callback: callback(aligner, infiles, (p1, p2), result)
def test_path_pointing_to_filename(self): with temporary_path("custom.perf.yml"): with record(path="custom.perf.yml"): caches["default"].get("foo") assert os.path.exists("custom.perf.yml")
def test_path_pointing_to_filename_record_twice(self): with temporary_path("custom.perf.yml"): with record(path="custom.perf.yml"): caches["default"].get("foo") with record(path="custom.perf.yml"): caches["default"].get("foo")
def test_too_short_paired_output(): with temporary_path("temp-too-short.1.fastq") as p1: with temporary_path("temp-too-short.2.fastq") as p2: def callback(aligner, infiles, outfiles, result): assert files_equal(cutpath('paired-too-short.1.fastq'), p1) assert files_equal(cutpath('paired-too-short.2.fastq'), p2) run_paired( '-a TTAGACATAT -A CAGTGGAGTA -m 14 --too-short-output ' '{0} --too-short-paired-output {1}'.format(p1, p2), in1='paired.1.fastq', in2='paired.2.fastq', expected1='paired_{aligner}.1.fastq', expected2='paired_{aligner}.2.fastq', aligners=BACK_ALIGNERS, callback=callback, )
def test_mode_once(self): temp_dir = os.path.join(FILE_DIR, "perf_files/") with temporary_path(temp_dir): with record(path="perf_files/api/"): caches["default"].get("foo") full_path = os.path.join(FILE_DIR, "perf_files", "api", "test_api.perf.yml") assert os.path.exists(full_path)
def test_too_short_output_paired_option_missing(): with raises(SystemExit), temporary_path("temp-too-short.1.fastq") as p1: run_paired( '-a TTAGACATAT -A CAGTGGAGTA -m 14 --too-short-output ' '{0}'.format(p1), in1='paired.1.fastq', in2='paired.2.fastq', expected1='paired.1.fastq', expected2='paired.2.fastq', aligners=BACK_ALIGNERS, )
def test_second_too_short(): with temporary_path("truncated.2.fastq") as trunc2: # Create a truncated file in which the last read is missing with open(datapath('paired.2.fastq')) as f: lines = f.readlines() lines = lines[:-4] with open(trunc2, 'w') as f: f.writelines(lines) with raises(SystemExit), redirect_stderr(): execute_cli('-a XX --paired-output out.fastq'.split() + [datapath('paired.1.fastq'), trunc2])
def test_mode_all(self): temp_dir = os.path.join(FILE_DIR, "perf_files/") with temporary_path(temp_dir): with pytest.raises(AssertionError) as excinfo: with record(path="perf_files/api/"): caches["default"].get("foo") assert "Original performance record did not exist" in str(excinfo.value) full_path = os.path.join(FILE_DIR, "perf_files", "api", "test_api.perf.yml") assert os.path.exists(full_path)
def run_interleaved(params, inpath, expected, aligners=('adapter', )): if type(params) is str: params = params.split() for aligner in aligners: with temporary_path(expected.format(aligner=aligner)) as tmp: p = params.copy() p += [ '--aligner', aligner, '-l', datapath(inpath.format(aligner=aligner)), '-L', tmp, ] command = get_command('trim') result = command.execute(p) assert isinstance(result, tuple) assert len(result) == 2 assert files_equal(cutpath(expected.format(aligner=aligner)), tmp)
def test_unmatched_read_names(): with temporary_path("swapped.1.fastq") as swapped: try: # Create a file in which reads 2 and are swapped with open(datapath('paired.1.fastq')) as f: lines = f.readlines() lines = lines[0:4] + lines[8:12] + lines[4:8] + lines[12:] with open(swapped, 'w') as f: f.writelines(lines) with redirect_stderr(): command = get_command('trim') result = command.execute( '-a XX -o out1.fastq -p out2.fastq'.split() + ['-pe1', swapped, '-pe2', datapath('paired.2.fastq')]) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] != 0 finally: os.remove('out1.fastq') os.remove('out2.fastq')