def test_demultiplex(): multiout = os.path.join(os.path.dirname(__file__), 'data', 'tmp-demulti.{name}.fasta') params = [ '-a', 'first=AATTTCAGGAATT', '-a', 'second=GTTCTCTAGTTCT', '-o', multiout, '-se', datapath('twoadapters.fasta'), ] command = get_command('trim') result = command.execute(params) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] == 0 assert files_equal(cutpath('twoadapters.first.fasta'), multiout.format(name='first')) assert files_equal(cutpath('twoadapters.second.fasta'), multiout.format(name='second')) assert files_equal(cutpath('twoadapters.unknown.fasta'), multiout.format(name='unknown')) os.remove(multiout.format(name='first')) os.remove(multiout.format(name='second')) os.remove(multiout.format(name='unknown'))
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 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_demultiplex(): multiout = os.path.join(os.path.dirname(__file__), 'data', 'tmp-demulti.{name}.fasta') params = ['-a', 'first=AATTTCAGGAATT', '-a', 'second=GTTCTCTAGTTCT', '-o', multiout, '-se', datapath('twoadapters.fasta')] command = get_command('trim') result = command.execute(params) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] == 0 assert files_equal(cutpath('twoadapters.first.fasta'), multiout.format(name='first')) assert files_equal(cutpath('twoadapters.second.fasta'), multiout.format(name='second')) assert files_equal(cutpath('twoadapters.unknown.fasta'), multiout.format(name='unknown')) os.remove(multiout.format(name='first')) os.remove(multiout.format(name='second')) os.remove(multiout.format(name='unknown'))
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 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 run( params, expected, inpath=None, inpath2=None, qualfile=None, interleaved_input=False, interleaved_output=False, sra_accn=None, ): if type(params) is str: params = params.split() with temporary_path(expected) as tmp_fastaq: if sra_accn: params += ['-sra', sra_accn] elif interleaved_input: params += ['-l', inpath] elif inpath2: params += ['-pe1', datapath(inpath)] params += ['-pe2', datapath(inpath2)] else: params += ['-se', datapath(inpath)] if qualfile: params += ['-sq', datapath(qualfile)] if interleaved_output: params += ['-L', tmp_fastaq] else: params += ['-o', tmp_fastaq] # TODO not parallelizable # print(params) command = get_command('trim') retcode, summary = command.execute(params) assert summary is not None assert isinstance(summary, dict) if 'exception' in summary and summary['exception'] is not None: assert retcode != 0 err = summary['exception'] traceback.print_exception(*err['details']) raise Exception("Unexpected error: {}".format(err['message'])) else: assert retcode == 0 # TODO redirect standard output assert os.path.exists(tmp_fastaq) assert files_equal(cutpath(expected), tmp_fastaq)
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')
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')
def run( params, expected, inpath=None, inpath2=None, qualfile=None, interleaved_input=False, interleaved_output=False, sra_accn=None): if type(params) is str: params = params.split() with temporary_path(expected) as tmp_fastaq: if sra_accn: params += ['-sra', sra_accn] elif interleaved_input: params += ['-l', inpath] elif inpath2: params += ['-pe1', datapath(inpath)] params += ['-pe2', datapath(inpath2)] else: params += ['-se', datapath(inpath)] if qualfile: params += ['-sq', datapath(qualfile)] if interleaved_output: params += ['-L', tmp_fastaq] else: params += ['-o', tmp_fastaq] # TODO not parallelizable #print(params) command = get_command('trim') retcode, summary = command.execute(params) assert summary is not None assert isinstance(summary, dict) if 'exception' in summary and summary['exception'] is not None: assert retcode != 0 err = summary['exception'] traceback.print_exception(*err['details']) raise Exception("Unexpected error: {}".format(err['message'])) else: assert retcode == 0 # TODO redirect standard output assert os.path.exists(tmp_fastaq) assert files_equal(cutpath(expected), tmp_fastaq)