def test_translated_search_unaligned_reads_annotations_gene_length(self): """ Test the unaligned reads and the store alignments Test with a rapsearch2 output file Test the different annotation formats are recognized for gene length """ # create a set of alignments alignments=store.Alignments() unaligned_reads_store=store.Reads() # load the rapsearch2 output with the unaligned reads function unaligned_file_fasta=translated_search.unaligned_reads(unaligned_reads_store, cfg.rapsearch_file_annotations, alignments) # remove temp file utils.remove_temp_file(unaligned_file_fasta) # there should be 4 hits identified all_hits=alignments.get_hit_list() self.assertEqual(len(all_hits),4) # check for set and default gene lengths for hit in all_hits: query, bug, reference, evalue, length = hit if reference == "UniRef50": self.assertEqual(length,2000) else: self.assertEqual(length,1000)
def test_Read_print_fasta_id_count(self): """ Read class: Test the loading of a full fasta file Test the total number of expected ids are loaded """ reads_store=store.Reads(cfg.small_fasta_file) # Check that the total number of expected reads are loaded self.assertEqual(len(reads_store.id_list()), cfg.small_fasta_file_total_sequences)
def test_Read_delete_id(self): """ Read class: Test the deleting of ids """ reads_store=store.Reads(cfg.small_fasta_file) # delete all of the reads and check structure is empty for id in reads_store.id_list(): reads_store.remove_id(id) self.assertEqual(len(reads_store.id_list()), 0)
def test_translated_search_unaligned_reads_annotations_bug(self): """ Test the unaligned reads and the store alignments Test with a rapsearch2 output file Test the different annotation formats are recognized for bug """ # create a set of alignments alignments=store.Alignments() unaligned_reads_store=store.Reads() # load the rapsearch2 output with the unaligned reads function unaligned_file_fasta=translated_search.unaligned_reads(unaligned_reads_store, cfg.rapsearch_file_annotations, alignments) # remove temp file utils.remove_temp_file(unaligned_file_fasta) # there should be one bug name and the other should be unclassified self.assertEqual(sorted(alignments.bug_list()),sorted(["s__Bacteroides_xylanisolvens","unclassified"]))
def test_Read_print_fasta_id_list(self): """ Read class: Test the loading of a full fasta file Test the expected ids are loaded """ reads_store=store.Reads(cfg.small_fasta_file) # Check the reads are printed correctly printed_stored_fasta=reads_store.get_fasta() compare_fasta={} # organize the fasta from the read class and the # file of correct fasta output file_handle=open(cfg.small_fasta_file_single_line_sequences) for input in [printed_stored_fasta.split("\n"), file_handle]: id="" seq="" for line in input: if re.search(">",line): # store prior id if id and seq: compare_fasta[id]=compare_fasta.get(id,[])+[seq] id=line.strip() seq="" else: seq=line.strip() # store the last sequence found if id and seq: compare_fasta[id]=compare_fasta.get(id,[])+[seq] file_handle.close() # check there are still the same number of ids self.assertEqual(len(compare_fasta.keys()),cfg.small_fasta_file_total_sequences) # check the sequences match for id, sequences in compare_fasta.items(): self.assertTrue(len(sequences)==2) self.assertEqual(sequences[0], sequences[1])
def test_translated_search_unaligned_reads_annotations_reference(self): """ Test the unaligned reads and the store alignments Test with a rapsearch2 output file Test the different annotation formats are recognized for reference """ # create a set of alignments alignments=store.Alignments() unaligned_reads_store=store.Reads() # load the rapsearch2 output with the unaligned reads function unaligned_file_fasta=translated_search.unaligned_reads(unaligned_reads_store, cfg.rapsearch_file_annotations, alignments) # remove temp file utils.remove_temp_file(unaligned_file_fasta) # three of the hits should be for gene "UniRef50" hits=alignments.hits_for_gene("UniRef50") self.assertEqual(len(hits),3)
def test_translated_search_unaligned_reads_rapsearch_log(self): """ Test the unaligned reads function Test with a rapsearch output file Test that log of evalue is taken """ # create a set of alignments alignments=store.Alignments() # load the rapsearch output file_handle=open(cfg.rapsearch2_output_file_with_header) for line in file_handle: if not re.search("^#",line): data=line.strip().split(config.blast_delimiter) referenceid=data[config.blast_reference_index] queryid=data[config.blast_query_index] evalue=float(data[config.blast_evalue_index]) alignments.add(referenceid, 0, queryid, evalue,"unclassified") file_handle.close() alignments_test=store.Alignments() unaligned_reads_store=store.Reads() # load the rapsearch output with the unaligned reads function unaligned_file_fasta=translated_search.unaligned_reads(unaligned_reads_store, cfg.rapsearch2_output_file_with_header, alignments_test) # remove temp file utils.remove_temp_file(unaligned_file_fasta) # check the evalues are changed hit1_evalue=sorted(alignments.get_hit_list())[0][-2] hit1_evalue_test=sorted(alignments_test.get_hit_list())[0][-2] self.assertAlmostEqual(math.pow(10.0,math.log(hit1_evalue)*-1), math.log(hit1_evalue_test)*-1,places=7)
def test_translated_search_unaligned_reads_blastm8(self): """ Test the unaligned reads and the store alignments Test with a blastm8-like output file Test with empty reads structure Test that log of evalue is not taken Test that function does not require gene lengths in reference id """ # create a set of alignments alignments=store.Alignments() # load the blastm8-like output file_handle=open(cfg.rapsearch2_output_file_without_header) for line in file_handle: if not re.search("^#",line): data=line.strip().split(config.blast_delimiter) referenceid=data[config.blast_reference_index] queryid=data[config.blast_query_index] evalue=float(data[config.blast_evalue_index]) alignments.add(referenceid, 0, queryid, evalue,"unclassified") file_handle.close() alignments_test=store.Alignments() unaligned_reads_store=store.Reads() # load the blastm8-like output with the unaligned reads function unaligned_file_fasta=translated_search.unaligned_reads(unaligned_reads_store, cfg.rapsearch2_output_file_without_header, alignments_test) # remove temp file utils.remove_temp_file(unaligned_file_fasta) # check the evalues are unchanged self.assertEqual(sorted(alignments.get_hit_list()), sorted(alignments_test.get_hit_list()))