def test_optionalthird(self): ''' Test that given a correct third argument it still runs ''' infa = ungzip(INPUT_PATH) bwa.index_ref(REF_PATH) # I don't have a mates file so just use infa again mem = BWAMem(REF_PATH, infa, infa, bwa_path=BWA_PATH) eq_(0, mem.run())
def test_optionspassed(self): ''' Make sure all options passed make it to bwa ''' bwa = self.mkbwa('$@') mem = BWAMem(self.fa, self.fa2, t=5, a=3, bwa_path=bwa) mem.run('output') with open('output') as fh: result_output = fh.read().strip() assert '-t 5' in result_output assert '-a 3' in result_output assert self.fa + ' ' + self.fa2 in result_output
def test_requiredargs_gt2(self): ''' Greater than 3 args is not correct ''' mem = BWAMem(self.fa, self.fa2, self.fa2, 'arg4', bwa_path=self.bwa_path)
def bwa_mem( read1, mate=None, ref=None, output='bwa.sai', **kwargs ): ''' Runs the bwa mem algorithm on read1 against ref. If mate is given then run that file with the read1 file so paired alignment is done. TODO: bwa_path should be an option to specify where the executable is @param read1 - File path to read @param mate - Mate file path @param ref - Reference file path or directory of references @param output - The output destination @returns the output path if sucessful or -1 if something went wrong ''' if os.path.isdir( ref ): # Compile ref directory logger.debug( "Compiling references inside of {0}".format(ref) ) ref = compile_refs( ref ) logger.info( "Refs are all compiled into {0}".format(ref) ) # First, make sure the reference is indexed logger.debug( "Ensuring {0} is indexed".format(ref) ) if not index_ref(ref): raise InvalidReference("{0} cannot be indexed by bwa") # Setup BWA Mem mem = None if mate: mem = BWAMem( ref, read1, mate, bwa_path=which_bwa(), **kwargs ) else: mem = BWAMem( ref, read1, bwa_path=which_bwa(), **kwargs ) ret = mem.run( output ) print "Ret: " + str(ret) if ret != 0: return ret else: return output
def bwa_mem(read1, mate=None, ref=None, output='bwa.sai', **kwargs): ''' Runs the bwa mem algorithm on read1 against ref. If mate is given then run that file with the read1 file so paired alignment is done. TODO: bwa_path should be an option to specify where the executable is @param read1 - File path to read @param mate - Mate file path @param ref - Reference file path or directory of references @param output - The output destination @returns the output path if sucessful or -1 if something went wrong ''' if os.path.isdir(ref): # Compile ref directory logger.debug("Compiling references inside of {0}".format(ref)) ref = compile_refs(ref) logger.info("Refs are all compiled into {0}".format(ref)) # First, make sure the reference is indexed logger.debug("Ensuring {0} is indexed".format(ref)) if not index_ref(ref): raise InvalidReference("{0} cannot be indexed by bwa") # Setup BWA Mem mem = None if mate: mem = BWAMem(ref, read1, mate, bwa_path=which_bwa(), **kwargs) else: mem = BWAMem(ref, read1, bwa_path=which_bwa(), **kwargs) ret = mem.run(output) print "Ret: " + str(ret) if ret != 0: return ret else: return output
def test_bwareturncode_count(self): ''' Fixed tests for pattern matching ''' # First item should be the sum of read counts tests = [ (1, '[M::main_mem] read 1 sequences (111350 bp)...'), (1000, '[M::main_mem] read 100 sequences (111350 bp)...\n[M::main_mem] read 900 sequences (111350 bp)...' ), ] for reads, testline in tests: filename = 'fasta{0}.fa'.format(reads) util.create_fakefasta(filename, reads) print "Reads In File: {0}".format(reads) print "Read Lines: {0}".format(testline) bwa = BWAMem(self.fa, filename, bwa_path=BWA_PATH) eq_(0, bwa.bwa_return_code(testline))
def test_bwareturncode_noendline(self): bwa = BWAMem(self.fa, self.fa2, bwa_path=BWA_PATH) eq_(1, bwa.bwa_return_code('[main] Version: 0.7.4-r385'))
def test_bwareturncode_nocount(self): ''' Make sure if no read sequences lines exist error is returned ''' endline = '[main] Version: {0}.{1}.{2}-r{3}' bwa = BWAMem(self.fa, self.fa2, bwa_path=BWA_PATH) eq_(1, bwa.bwa_return_code(endline.format(0, 7, 4, 385)))
def test_run_nonfastainput(self): ''' Invalid fasta input file(file exists but not fasta/fastq ''' filename = 'test.fa' with open(filename, 'w') as fh: fh.write('not a fasta') bwa = BWAMem(REF_PATH, filename, bwa_path=BWA_PATH, command='mem')
def test_bwamem_run(self): ''' Make sure it actually runs bwa with correct input ''' #infa = ungzip( INPUT_PATH ) bwa.index_ref(self.fa2) mem = BWAMem(self.fa2, self.fa, bwa_path=BWA_PATH) eq_(0, mem.run())
def test_requiredargs_third_invalid(self): ''' Test reads file is valid path to file ''' BWAMem(self.fa, self.fa2, '/invalid/path/in.fa', bwa_path=self.bwa_path)
def test_requiredargs_firstarg_nonindexed(self): ''' Test non indexed file for index arg ''' # Should just check to make sure self.fa2 has same filename but with .bwt shutil.copy(self.fa2, 'noindex.fa') BWAMem('noindex.fa', self.fa, bwa_path=self.bwa_path)
def test_requiredargs_firstarg_invalid(self): ''' Test invalid path for database arg ''' BWAMem('/invalid/path/in.fa', self.fa2, bwa_path=self.bwa_path)
def test_requiredargs_lt2(self): ''' Needs 2 args ''' mem = BWAMem(self.fa, bwa_path=self.bwa_path)