From f44ef9ede1779d137e0ad2a0fe1b494977f86cd9 Mon Sep 17 00:00:00 2001 From: MatthieuBeukers Date: Wed, 3 Jul 2019 10:24:18 +0200 Subject: [PATCH 1/5] Small update to UtilParamCheck, use ReadIdObject for acceptor/donor reads when reading varcon in VariantContextFile --- VaSeUtils/UtilParamCheck.py | 5 +++-- VariantContextFile.py | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/VaSeUtils/UtilParamCheck.py b/VaSeUtils/UtilParamCheck.py index a4f5dcc..1b803d7 100644 --- a/VaSeUtils/UtilParamCheck.py +++ b/VaSeUtils/UtilParamCheck.py @@ -43,8 +43,9 @@ def required_params_set(self, utiltorun, paramlist): if utiltorun in self.required_util_params: for reqparam in self.required_util_params[utiltorun]: if paramlist[reqparam] is not None: - if not os.path.isfile(paramlist[reqparam]): - return False + if reqparam != "outfile": + if not os.path.isfile(paramlist[reqparam]): + return False else: return False return True diff --git a/VariantContextFile.py b/VariantContextFile.py index 00ccae1..76d33ca 100644 --- a/VariantContextFile.py +++ b/VariantContextFile.py @@ -2,6 +2,7 @@ import statistics from OverlapContext import OverlapContext from VariantContext import VariantContext +from ReadIdObject import ReadIdObject class VariantContextFile: @@ -148,9 +149,11 @@ def read_variant_context_file(self, fileloc, samplefilter=None, chrompass = self.passes_filter(filelinedata[2], chromfilter) if samplepass and varconpass and chrompass: + acceptor_reads = [ReadIdObject(readid) for readid in filelinedata[11].split(";")] + donor_reads = [ReadIdObject(readid) for readid in filelinedata[12].split(";")] varcon_obj = VariantContext(filelinedata[0], filelinedata[1], filelinedata[2], int(filelinedata[3]), int(filelinedata[4]), int(filelinedata[5]), - filelinedata[11].split(";"), filelinedata[12].split(";")) + acceptor_reads, donor_reads) if filelinedata[0] not in self.variant_contexts: self.variant_contexts[filelinedata[0]] = varcon_obj except IOError as ioe: From 2493409417f92a236615bb8d94952656cf489999 Mon Sep 17 00:00:00 2001 From: MatthieuBeukers Date: Thu, 4 Jul 2019 10:52:59 +0200 Subject: [PATCH 2/5] Updates to unittests and classes that they test --- OverlapContext.py | 2 +- VariantContext.py | 4 +- VcfVariant.py | 2 +- tests/TestOverlapContext.py | 3 +- tests/TestParamChecker.py | 55 ++++--- tests/TestVariantContext.py | 6 +- tests/TestVariantContextFile.py | 275 +++++++++++++++++++------------- tests/TestVcfVariant.py | 7 +- 8 files changed, 202 insertions(+), 152 deletions(-) diff --git a/OverlapContext.py b/OverlapContext.py index 3229531..0b5af78 100644 --- a/OverlapContext.py +++ b/OverlapContext.py @@ -75,7 +75,7 @@ def get_number_of_context_reads(self): def get_context_bam_read_ids(self): if self.context_bam_reads is None: return [None] - return [x.get_bam_read_id() for x in self.context_bam_reads] + return list(set([x.get_bam_read_id() for x in self.context_bam_reads])) # Returns a list of all left positions for all BAM reads. def get_context_bam_read_starts(self): diff --git a/VariantContext.py b/VariantContext.py index 5e9b68f..e6c6128 100644 --- a/VariantContext.py +++ b/VariantContext.py @@ -112,7 +112,7 @@ def get_number_of_acceptor_reads(self): def get_acceptor_read_ids(self): if self.variant_context_areads is None: return [None] - return [x.get_bam_read_id() for x in self.variant_context_areads] + return list(set([x.get_bam_read_id() for x in self.variant_context_areads])) # Returns the list of left most acceptor read positions, def get_acceptor_read_starts(self): @@ -151,7 +151,7 @@ def get_number_of_donor_reads(self): # Returns the identifiers of donor reads overlapping with the # variant context. def get_donor_read_ids(self): - return [x.get_bam_read_id() for x in self.variant_context_dreads] + return list(set([x.get_bam_read_id() for x in self.variant_context_dreads])) # Returns the list of variant context donor read starting positions. def get_donor_read_starts(self): diff --git a/VcfVariant.py b/VcfVariant.py index 64166ae..f705a37 100644 --- a/VcfVariant.py +++ b/VcfVariant.py @@ -65,4 +65,4 @@ def get_variant_id(self): # ToString method def to_string(self): return f"{self.vcf_variant_chrom}\t{self.vcf_variant_start}\t{self.vcf_variant_type}\t{self.vcf_variant_ref}" \ - f"\t{self.vcf_variant_alts}" + f"\t{self.vcf_variant_alts}\t{self.vcf_variant_filter}" diff --git a/tests/TestOverlapContext.py b/tests/TestOverlapContext.py index 5e8f0ce..6f12d6f 100644 --- a/tests/TestOverlapContext.py +++ b/tests/TestOverlapContext.py @@ -109,8 +109,7 @@ def test_get_number_of_context_reads(self): f"of context reads should have been {num_of_context_reads_answer}") def test_get_context_bam_read_ids(self): - context_read_ids_answer = ["HHKY2CCXX160108:1:2122:24160:2522", "HHKY2CCXX160108:1:2122:24160:2522", - "HHKY2CCXX160108:1:2122:24160:2522"] + context_read_ids_answer = ["HHKY2CCXX160108:1:2122:24160:2522"] self.assertEqual(self.overlap_context.get_context_bam_read_ids(), context_read_ids_answer, f"The list of context read ids should have been {context_read_ids_answer}") diff --git a/tests/TestParamChecker.py b/tests/TestParamChecker.py index 1cdb2d5..7c44b66 100644 --- a/tests/TestParamChecker.py +++ b/tests/TestParamChecker.py @@ -14,16 +14,16 @@ class TestParamChecker(unittest.TestCase): # Set up required def setUp(self): self.paramCheck = ParamChecker() - self.paramList = {'vcfin': ['testdata/vcfDir'], - 'bamin': ['testdata/bamDir'], - 'templatebam': 'testdata/valbam/SRR1039513.bam', - 'templatefq1': 'testdata/fqDir/SRR1039513_1.fastq.gz', - 'templatefq2': 'testdata/fqDir/SRR1039513_2.fastq.gz', - 'fastqout': 'testdata/outDir', - 'varcon': 'testdata/outDir/varcon.txt', - 'varbread': 'testdata/outDir/varbread.txt', - 'templatebread': 'testdata/outDir/nistbread.txt' - } + self.param_list = {'vcfin': ['testdata/vcfDir'], + 'bamin': ['testdata/bamDir'], + 'templatebam': 'testdata/valbam/SRR1039513.bam', + 'templatefq1': 'testdata/fqDir/SRR1039513_1.fastq.gz', + 'templatefq2': 'testdata/fqDir/SRR1039513_2.fastq.gz', + 'fastqout': 'testdata/outDir', + 'varcon': 'testdata/outDir/varcon.txt', + 'varbread': 'testdata/outDir/varbread.txt', + 'templatebread': 'testdata/outDir/nistbread.txt' + } # Tests that the --log will be written to the log file. def test_checkLog_pos(self): @@ -34,21 +34,26 @@ def test_checkLog_pos(self): def test_checkLog_noParamSet(self): self.assertEqual(self.paramCheck.check_log(None), "VaSeBuilder.log", "The log location should have been VaSeBuilder.log") - # Tests that the --log parameter has been set but the filename does not end with .log and will therefore be written to VaSeBuilder.log + # Tests that the --log parameter has been set but the filename does not end with .log and will therefore be written + # to VaSeBuilder.log def test_checkLog_noLog(self): self.assertEqual(self.paramCheck.check_log("testdata/outDir/logfile.file"), "VaSeBuilder.log", "The log location should have been VaSeBuilder.log") - # Test that the --log parameter has been set but the filename does not end with .txt and will therefore be written to VaSeBuilder.log + # Test that the --log parameter has been set but the filename does not end with .txt and will therefore be written + # to VaSeBuilder.log def test_checkLog_noTxt(self): - self.assertEqual(self.paramCheck.check_log("testdata/outDir/logfile.file"), "VaSeBuilder.log", "The log location should have been VaSeBuilder.log") + self.assertEqual(self.paramCheck.check_log("testdata/outDir/logfile.file"), "VaSeBuilder.log", + "The log location should have been VaSeBuilder.log") # Tests that the povided testdata folder 'bamDir' indeed contains two BAM files. def test_checkFolderContents_pos(self): - self.assertEqual(self.paramCheck.check_folder_contents("testdata/bamDir", "bam"), 2, "Two bam files should have been found") + self.assertEqual(self.paramCheck.check_folder_contents("testdata/bamDir", "bam"), 2, + "Two bam files should have been found") # Tests that the provided testdata folder 'bamDir' contains no VCF files def test_checkFolderContents_neg(self): - self.assertEqual(self.paramCheck.check_folder_contents("testdata/bamDir", "vcf"), 0, "No VCF files should have been found") + self.assertEqual(self.paramCheck.check_folder_contents("testdata/bamDir", "vcf"), 0, + "No VCF files should have been found") # Tests that the provided testdata folder 'bamDir' exists and contains BAM files. def test_checkFoldersExist_pos(self): @@ -84,49 +89,49 @@ def test_getFolderName_neg(self): # Tests that all provided parameters are ok. First test should return True, all others false due to one parameter missing def test_checkParameters_pos(self): - self.assertTrue(self.paramCheck.check_parameters(self.paramList)) + self.assertTrue(self.paramCheck.check_parameters(self.param_list)) def test_checkParameters_noVcfIn(self): - parList = self.paramList.copy() + parList = self.param_list.copy() parList['vcfin'] = ['testdata/doesnotexist'] # Set the list VCF folders to a folder that does not exist. self.assertFalse(self.paramCheck.check_parameters(parList)) def test_checkParameters_noBamIn(self): - parList = self.paramList.copy() + parList = self.param_list.copy() parList['bamin'] = ['testdata/doesnotexist'] # Set the list BAM folders to a folder that does not exist. self.assertFalse(self.paramCheck.check_parameters(parList)) def test_checkParameters_noValBam(self): - parList = self.paramList.copy() + parList = self.param_list.copy() parList['templatebam'] = 'testdata/doesnotexist.bam' # Set the location of the bam file to one that does not exist. self.assertFalse(self.paramCheck.check_parameters(parList)) def test_checkParameters_noValFq1(self): - parList = self.paramList.copy() + parList = self.param_list.copy() parList['templatefq1'] = 'testdata/doesnotexist.fq' # Set the location of the fastq file to one that does not exist. self.assertFalse(self.paramCheck.check_parameters(parList)) def test_checkParameters_noValFq2(self): - parList = self.paramList.copy() + parList = self.param_list.copy() parList['templatefq2'] = 'testdata/doesnotexist.fq' # Set the location of the fastq file to one that does not exist. self.assertFalse(self.paramCheck.check_parameters(parList)) def test_checkParameters_noFqOut(self): - parList = self.paramList.copy() + parList = self.param_list.copy() parList['fastqout'] = 'testdata/doesnotexist/faqOut.fq' # Set the output location of the fastq file to a folder that does not exist self.assertFalse(self.paramCheck.check_parameters(parList)) def test_checkParameters_noVarcon(self): - parList = self.paramList.copy() + parList = self.param_list.copy() parList['varcon'] = 'testdata/doesnotexist/varcon.txt' # Set the location for the varcon output file to a folder that does not exist. self.assertFalse(self.paramCheck.check_parameters(parList)) def test_checkParameters_noVarBread(self): - parList = self.paramList.copy() + parList = self.param_list.copy() parList['varbread'] = 'testdata/doesnotexist/varbread.txt' # Set the location for the varbread output file to a folder that does not exist. self.assertFalse(self.paramCheck.check_parameters(parList)) def test_checkParameters_noNistBread(self): - parList = self.paramList.copy() + parList = self.param_list.copy() parList['templatebread'] = 'testdata/doesnotexist/nistbread.txt' # Set the location for the nistbread output file to a folder that does not exist. self.assertFalse(self.paramCheck.check_parameters(parList)) diff --git a/tests/TestVariantContext.py b/tests/TestVariantContext.py index 68fe547..c24d87f 100644 --- a/tests/TestVariantContext.py +++ b/tests/TestVariantContext.py @@ -27,10 +27,8 @@ def setUp(self): self.read_pos_answer, self.read_donor_len_answer, self.read_seq_answer, self.read_quals_answer, self.read_map_q_answer) - self.acceptor_read_ids_answer = ["HHKY2CCXX160108:1:2122:24160:2522", "HHKY2CCXX160108:1:2122:24160:2522", - "HHKY2CCXX160108:1:2122:24160:2522"] - self.donor_read_ids_answer = ["HHKY2CCXX160108:1:2122:24160:2555", "HHKY2CCXX160108:1:2122:24160:2555", - "HHKY2CCXX160108:1:2122:24160:2555"] + self.acceptor_read_ids_answer = ["HHKY2CCXX160108:1:2122:24160:2522"] + self.donor_read_ids_answer = ["HHKY2CCXX160108:1:2122:24160:2555"] self.read_lens_answer = [self.read_len_answer, self.read_len_answer, self.read_len_answer] self.donor_read_lens_answer = [self.read_donor_len_answer, self.read_donor_len_answer, self.read_donor_len_answer] diff --git a/tests/TestVariantContextFile.py b/tests/TestVariantContextFile.py index 14a9484..8a788cd 100644 --- a/tests/TestVariantContextFile.py +++ b/tests/TestVariantContextFile.py @@ -99,194 +99,241 @@ def setUp(self): self.pos_otherVariantContextFile = VariantContextFile() self.pos_otherVariantContextFile.set_variant_context(self.context_id_answer, self.variantContextAnswer) self.neg_otherVariantContextFile = VariantContextFile() - self.neg_otherVariantContextFile.add_variant_context('20_150', 'testsample2', '20', 150, 0, 350, self.var_context_d_reads_answer, self.var_context_a_reads_answer) - - - + self.neg_otherVariantContextFile.add_variant_context('20_150', 'testsample2', '20', 150, 0, 350, + self.var_context_d_reads_answer, + self.var_context_a_reads_answer) + # ====================PERFORMS THE TESTS FOR THE GETTER METHODS==================== - def test_getVariantContexts(self): + def test_get_variant_contexts(self): variant_contexts_answer = [self.variantContextAnswer.to_string()] obtained_contexts_answer = [x.to_string() for x in self.variantContextFile.get_variant_contexts()] self.assertListEqual(obtained_contexts_answer, variant_contexts_answer, "The returned variant contexts are not what was expected") - - def test_getVariantContext(self): + + def test_get_variant_context(self): self.assertEqual(self.variantContextFile.get_variant_context(self.context_id_answer).to_string(), self.variantContextAnswer.to_string(), "The returned variant context is not what was expected") - - def test_getVariantContext_None(self): + + def test_get_variant_context_none(self): self.assertIsNone(self.variantContextFile.get_variant_context('22_9411255'), "The requested variant context should not have existed and should have therefore been None") - - def test_getAcceptorContext(self): + + def test_get_acceptor_context(self): self.assertEqual(self.variantContextFile.get_acceptor_context(self.context_id_answer).to_string(), self.acceptor_context_answer.to_string(), "The returned acceptor contexts is not what was expected") - - def test_getAcceptorContext_None(self): + + def test_get_acceptor_context_none(self): self.assertIsNone(self.variantContextFile.get_acceptor_context("22_9411255"), "The requested acceptor context should not have existed and should have therefore been None") - - def test_getDonorContext(self): + + def test_get_donor_context(self): self.assertEqual(self.variantContextFile.get_acceptor_context(self.context_id_answer).to_string(), self.acceptor_context_answer.to_string(), "The returned donor context is not what was expected") - - def test_getDonorContext_None(self): + + def test_get_donor_context_none(self): self.assertIsNone(self.variantContextFile.get_donor_context('22_9411255'), "The requested donor context should not have existed and should have therefore been None") - - def test_getAllVariantContextAcceptorReads(self): + + def test_get_all_variant_context_acceptor_reads(self): obtained_reads = [x.to_string() for x in self.variantContextFile.get_all_variant_context_acceptor_reads()] answer_reads = [x.to_string() for x in self.var_context_a_reads_answer] self.assertListEqual(obtained_reads, answer_reads, "Both lists should have contained reads with the exact same " "data") - - def test_getAllVariantContextDonorReads(self): + + def test_get_all_variant_context_donor_reads(self): obtained_reads = [x.to_string() for x in self.variantContextFile.get_all_variant_context_donor_reads()] answer_reads = [x.to_string() for x in self.var_context_d_reads_answer] self.assertListEqual(obtained_reads, answer_reads, "Both lists should have contained reads with the exact same " "data") - - def test_getAllVariantContextAcceptorReadIds(self): - vca_read_ids = [self.va_read_id, self.va_read_id] + + def test_get_all_variant_context_acceptor_read_ids(self): + vca_read_ids = [self.va_read_id] self.assertListEqual(self.variantContextFile.get_all_variant_context_acceptor_read_ids(), vca_read_ids, f"The list of returned acceptor read ids should have been: {vca_read_ids}") - def test_getAllVariantContextDonorReadIds(self): - vcd_read_ids = [self.vd_read_id, self.vd_read_id] + def test_get_all_variant_context_donor_read_ids(self): + vcd_read_ids = [self.vd_read_id] self.assertListEqual(self.variantContextFile.get_all_variant_context_donor_read_ids(), vcd_read_ids, f"The list of returned donor read ids should have been: {vcd_read_ids}") - - + # ====================PERFORM THE TESTS FOR READING A VARIANT CONTEXT FILE==================== #def test_readVariantContextFile_pos(self) - - def test_passesFilter_pos(self): + def test_passes_filter_pos(self): pos_filter_val = 'aap' - self.assertTrue(self.variantContextFile.passes_filter(pos_filter_val, self.filterListToUse), f"The value {pos_filter_val} should have been in the filter list {self.filterListToUse} and therefore return True") - - def test_passesFilter_neg(self): + self.assertTrue(self.variantContextFile.passes_filter(pos_filter_val, self.filterListToUse), + f"The value {pos_filter_val} should have been in the filter list {self.filterListToUse} " + "and therefore return True") + + def test_passes_filter_neg(self): neg_filter_val = 'jan' - self.assertFalse(self.variantContextFile.passes_filter(neg_filter_val, self.filterListToUse), f"The value {neg_filter_val} should not have been in the filter list {self.filterListToUse} and therefore return False") - - - + self.assertFalse(self.variantContextFile.passes_filter(neg_filter_val, self.filterListToUse), + f"The value {neg_filter_val} should not have been in the filter list {self.filterListToUse} " + "and therefore return False") + # ====================PERFORM THE TESTS FOR IN CONTEXT METHODS==================== - def test_variantIsInContext_pos(self): + def test_variant_is_in_context_pos(self): pos_variant_type = 'snp' - self.assertTrue(self.variantContextFile.variant_is_in_context(pos_variant_type, self.context_chrom_answer, self.context_origin_answer, self.context_origin_answer), f"The variant of type {pos_variant_type} on {self.context_origin_answer}, starting at {self.context_origin_answer} should have been in a context") - - def test_variantIsInContext_neg(self): + self.assertTrue(self.variantContextFile.variant_is_in_context(pos_variant_type, self.context_chrom_answer, + self.context_origin_answer, + self.context_origin_answer), + f"The variant of type {pos_variant_type} on {self.context_origin_answer}, starting at " + f"{self.context_origin_answer} should have been in a context") + + def test_variant_is_in_context_neg(self): neg_variant_type = 'aap' - self.assertIsNone(self.variantContextFile.variant_is_in_context(neg_variant_type, self.context_chrom_answer, self.context_origin_answer, self.context_origin_answer), f"The variant of type {neg_variant_type} should have returned None") - - def test_snpVariantIsInContext_pos(self): - self.assertTrue(self.variantContextFile.snp_variant_is_in_context(self.context_chrom_answer, self.context_origin_answer), f"The SNP on chromosome {self.context_chrom_answer} at position {self.context_origin_answer} should have been in a variant context") + self.assertIsNone(self.variantContextFile.variant_is_in_context(neg_variant_type, self.context_chrom_answer, + self.context_origin_answer, + self.context_origin_answer), + f"The variant of type {neg_variant_type} should have returned None") - def test_snpVariantIsInContext_neg(self): + def test_snp_variant_is_in_context_pos(self): + self.assertTrue(self.variantContextFile.snp_variant_is_in_context(self.context_chrom_answer, + self.context_origin_answer), + f"The SNP on chromosome {self.context_chrom_answer} at position {self.context_origin_answer} " + "should have been in a variant context") + + def test_snp_variant_is_in_context_neg(self): neg_snppos = 325632 - self.assertFalse(self.variantContextFile.snp_variant_is_in_context(self.context_chrom_answer, neg_snppos), f"The SNP on chromosome {self.context_chrom_answer} at position {neg_snppos} should not have been in any variant context") - - def test_indelVariantIsInContext_pos(self): + self.assertFalse(self.variantContextFile.snp_variant_is_in_context(self.context_chrom_answer, neg_snppos), + f"The SNP on chromosome {self.context_chrom_answer} at position {neg_snppos} should not have " + "been in any variant context") + + def test_indel_variant_is_in_context_pos(self): pos_indel_start = 9411050 pos_indel_end = 9411150 - self.assertTrue(self.variantContextFile.indel_variant_is_in_context(self.context_chrom_answer, pos_indel_start, pos_indel_end), f"The indel on chromosome {self.context_chrom_answer}, starting at {pos_indel_start} and ending at {pos_indel_end} should have been in a variant context") - - def test_indelVariantIsInContext_neg(self): + self.assertTrue(self.variantContextFile.indel_variant_is_in_context(self.context_chrom_answer, pos_indel_start, + pos_indel_end), + f"The indel on chromosome {self.context_chrom_answer}, starting at {pos_indel_start} and " + f"ending at {pos_indel_end} should have been in a variant context") + + def test_indel_variant_is_in_context_neg(self): neg_indel_start = 8000000 neg_indel_end = 8000100 - self.assertFalse(self.variantContextFile.indel_variant_is_in_context(self.context_chrom_answer, neg_indel_start, neg_indel_end), f"The indel on chromosome {self.context_chrom_answer}, starting at {neg_indel_start} and ending at {neg_indel_end} should not ahve been in any variant context") - - - + self.assertFalse(self.variantContextFile.indel_variant_is_in_context(self.context_chrom_answer, neg_indel_start, + neg_indel_end), + f"The indel on chromosome {self.context_chrom_answer}, starting at {neg_indel_start} and " + f"ending at {neg_indel_end} should not ahve been in any variant context") + # ====================PERFORM THE TESTS FOR ADDING CONTEXTS TO THE VARIANT CONTEXT FILE==================== - def test_setVariantContext(self): + def test_set_variant_context(self): self.variantContextFile.set_variant_context('21_9411000', self.setVarContextAnswer) - self.assertEqual(self.variantContextFile.get_variant_context('21_9411000').to_string(), self.setVarContextAnswer.to_string(), f"The variant context that was just set and the one otained are different") - - def test_addVariantContext(self): - varcon_obj_answer = VariantContext('21_9411000', 'testanswer', '21', 9411000, 94110900, 9411100, self.var_context_a_reads_answer, self.var_context_d_reads_answer, self.setAccContextAnswer, self.setDonContextAnswer) - self.variantContextFile.add_variant_context('21_9411000', 'testanswer', '21', 9411000, 94110900, 9411100, self.var_context_a_reads_answer, self.var_context_d_reads_answer, self.setAccContextAnswer, self.setDonContextAnswer) - self.assertEqual(self.variantContextFile.get_variant_context('21_9411000').to_string(), varcon_obj_answer.to_string(), f"The obtained variant context for {self.context_id_answer} should have been the same as what was just added") - - def test_setAcceptorContext(self): + self.assertEqual(self.variantContextFile.get_variant_context('21_9411000').to_string(), + self.setVarContextAnswer.to_string(), "The variant context that was just set and the one " + "otained are different") + + def test_add_variant_context(self): + varcon_obj_answer = VariantContext('21_9411000', 'testanswer', '21', 9411000, 94110900, 9411100, + self.var_context_a_reads_answer, self.var_context_d_reads_answer, + self.setAccContextAnswer, self.setDonContextAnswer) + self.variantContextFile.add_variant_context('21_9411000', 'testanswer', '21', 9411000, 94110900, 9411100, + self.var_context_a_reads_answer, self.var_context_d_reads_answer, + self.setAccContextAnswer, self.setDonContextAnswer) + self.assertEqual(self.variantContextFile.get_variant_context('21_9411000').to_string(), + varcon_obj_answer.to_string(), f"The obtained variant context for {self.context_id_answer} " + "should have been the same as what was just added") + + def test_set_acceptor_context(self): self.variantContextFile.set_variant_context('21_9411000', self.setVarContextAnswer) self.variantContextFile.set_acceptor_context('21_9411000', self.setAccContextAnswer) - self.assertEqual(self.variantContextFile.get_acceptor_context('21_9411000').to_string(), self.setAccContextAnswer.to_string(), f"The obtained acceptor context for 21_9411000 should have been the same as what was just set") - - def test_addAcceptorContext(self): + self.assertEqual(self.variantContextFile.get_acceptor_context('21_9411000').to_string(), + self.setAccContextAnswer.to_string(), + "The obtained acceptor context for 21_9411000 should have been the same as what was just set") + + def test_add_acceptor_context(self): self.variantContextFile.set_variant_context('21_9411000', self.setVarContextAnswer) - acccon_obj_answer = OverlapContext('21_9411000', 'accanswer', '21', 9411000, 94110900, 9411050, self.acc_context_reads_answer) - self.variantContextFile.add_acceptor_context('21_9411000', 'accanswer', '21', 9411000, 94110900, 9411050, self.acc_context_reads_answer) - self.assertEqual(self.variantContextFile.get_acceptor_context('21_9411000').to_string(), acccon_obj_answer.to_string()) - - def test_setDonorContext(self): + acccon_obj_answer = OverlapContext('21_9411000', 'accanswer', '21', 9411000, 94110900, 9411050, + self.acc_context_reads_answer) + self.variantContextFile.add_acceptor_context('21_9411000', 'accanswer', '21', 9411000, 94110900, 9411050, + self.acc_context_reads_answer) + self.assertEqual(self.variantContextFile.get_acceptor_context('21_9411000').to_string(), + acccon_obj_answer.to_string()) + + def test_set_donor_context(self): self.variantContextFile.set_variant_context('21_9411000', self.setVarContextAnswer) self.variantContextFile.set_donor_context('21_9411000', self.setDonContextAnswer) - self.assertEqual(self.variantContextFile.get_donor_context('21_9411000').to_string(), self.setDonContextAnswer.to_string(), f"The obtained donor context for 21_9411000 should have been the same as what was just set") - - def test_addDonorContext(self): + self.assertEqual(self.variantContextFile.get_donor_context('21_9411000').to_string(), + self.setDonContextAnswer.to_string(), "The obtained donor context for 21_9411000 should have " + "been the same as what was just set") + + def test_add_donor_context(self): self.variantContextFile.set_variant_context('21_9411000', self.setVarContextAnswer) - doncon_obj_answer = OverlapContext('21_9411000', 'donanswer', '21', 9411000, 94110950, 9411100, self.don_context_reads_answer) - self.variantContextFile.add_donor_context('21_9411000', 'donanswer', '21', 9411000, 94110950, 9411100, self.don_context_reads_answer) - self.assertEqual(self.variantContextFile.get_donor_context('21_9411000').to_string(), doncon_obj_answer.to_string(), f"The obtained donor context for '21_9411000' should have been the same as what was just added") - - - + doncon_obj_answer = OverlapContext('21_9411000', 'donanswer', '21', 9411000, 94110950, 9411100, + self.don_context_reads_answer) + self.variantContextFile.add_donor_context('21_9411000', 'donanswer', '21', 9411000, 94110950, 9411100, + self.don_context_reads_answer) + self.assertEqual(self.variantContextFile.get_donor_context('21_9411000').to_string(), + doncon_obj_answer.to_string(), "The obtained donor context for '21_9411000' should have been " + "the same as what was just added") + # ====================PERFORM THE TESTS FOR SET OPERATIONS ON TWO VARIANT CONTEXT FILES==================== - def test_getVariantContextsUnion(self): + def test_get_variant_contexts_union(self): varcon_union_answer = [self.context_id_answer] - self.assertListEqual(self.variantContextFile.get_variant_contexts_union(self.pos_otherVariantContextFile), varcon_union_answer, f"The variant context union should have been {varcon_union_answer}") - - def getVariantContextsIntersect_pos(self): + self.assertListEqual(self.variantContextFile.get_variant_contexts_union(self.pos_otherVariantContextFile), + varcon_union_answer, f"The variant context union should have been {varcon_union_answer}") + + def get_variant_contexts_intersect_pos(self): pos_intersect_answer = [self.context_id_answer] - self.assertListEqual(self.variantContextFile.get_variant_contexts_intersect(self.pos_otherVariantContextFile), pos_intersect_answer, f"The variant context intersect should have been {pos_intersect_answer}") - - def test_getVariantContextsIntersect_neg(self): + self.assertListEqual(self.variantContextFile.get_variant_contexts_intersect(self.pos_otherVariantContextFile), + pos_intersect_answer, "The variant context intersect should have been " + f"{pos_intersect_answer}") + + def test_get_variant_contexts_intersect_neg(self): neg_intersect_answer = [] - self.assertListEqual(self.variantContextFile.get_variant_contexts_intersect(self.neg_otherVariantContextFile), neg_intersect_answer, f"The variant context intersect should have been empty") - - def test_getVariantContextsDifference_pos(self): + self.assertListEqual(self.variantContextFile.get_variant_contexts_intersect(self.neg_otherVariantContextFile), + neg_intersect_answer, f"The variant context intersect should have been empty") + + def test_get_variant_contexts_difference_pos(self): pos_difference_answer = ['21_9411259'] - self.assertListEqual(self.variantContextFile.get_variant_contexts_difference(self.neg_otherVariantContextFile), pos_difference_answer, f"the difference in variant context files should have been {pos_difference_answer} ") - - def test_getVariantContextsDifference_neg(self): + self.assertListEqual(self.variantContextFile.get_variant_contexts_difference(self.neg_otherVariantContextFile), + pos_difference_answer, "the difference in variant context files should have been " + f"{pos_difference_answer} ") + + def test_get_variant_contexts_difference_neg(self): neg_difference_answer = [] - self.assertListEqual(self.variantContextFile.get_variant_contexts_difference(self.pos_otherVariantContextFile), neg_difference_answer, f"The differences in variant context files should have been empty") - - def test_getVariantContextsSymmetricDifference_pos(self): - pos_symdifference_answer = [] - self.assertListEqual(self.variantContextFile.get_variant_contexts_symmetric_difference(self.pos_otherVariantContextFile), pos_symdifference_answer, f"") - - def test_getVariantContextsSymmetricDifference_neg(self): - neg_symdifference_answer = ['20_150', self.context_id_answer] - self.assertListEqual(self.variantContextFile.get_variant_contexts_symmetric_difference(self.neg_otherVariantContextFile), neg_symdifference_answer, f"The symmetric differences ") + self.assertListEqual(self.variantContextFile.get_variant_contexts_difference(self.pos_otherVariantContextFile), + neg_difference_answer, f"The differences in variant context files should have been empty") + def test_get_variant_contexts_symmetric_difference_pos(self): + pos_symdifference_answer = [] + self.assertListEqual( + self.variantContextFile.get_variant_contexts_symmetric_difference(self.pos_otherVariantContextFile), + pos_symdifference_answer, f"") + def test_get_variant_contexts_symmetric_difference_neg(self): + neg_symdifference_answer = [self.context_id_answer, "20_150"] + self.assertListEqual(self.variantContextFile.get_variant_contexts_symmetric_difference( + self.neg_otherVariantContextFile), neg_symdifference_answer, "The symmetric differences are not the same") # ====================PERFORM THE TESTS FOR SETTING UNMAPPED MATE IDS==================== - def test_setAcceptorContextUnmappedMateIds(self): + def test_set_acceptor_context_unmapped_mate_ids(self): acu_read_ids = ['acuRead1', 'acuRead3', 'acuRead5'] self.variantContextFile.set_acceptor_context_unmapped_mate_ids(self.context_id_answer, acu_read_ids) - self.assertListEqual(self.variantContextFile.get_acceptor_context_unmapped_mate_ids(self.context_id_answer), acu_read_ids, f"The set and returned acceptor context unmapped mate ids should have been {acu_read_ids}") + self.assertListEqual(self.variantContextFile.get_acceptor_context_unmapped_mate_ids(self.context_id_answer), + acu_read_ids, "The set and returned acceptor context unmapped mate ids should have been " + f"{acu_read_ids}") - def test_setDonorContextUnmappedMateIds(self): + def test_set_donor_context_unmapped_mate_ids(self): dcu_read_ids = ['dcuRead2', 'dcuRead4', 'dcuRead6'] self.variantContextFile.set_donor_context_unmapped_mate_ids(self.context_id_answer, dcu_read_ids) - self.assertListEqual(self.variantContextFile.get_donor_context_unmapped_mate_ids(self.context_id_answer), dcu_read_ids, f"The set and returned donor context unmapped mate ids should have been {dcu_read_ids}") + self.assertListEqual(self.variantContextFile.get_donor_context_unmapped_mate_ids(self.context_id_answer), + dcu_read_ids, "The set and returned donor context unmapped mate ids should have been " + f"{dcu_read_ids}") - def test_setUnmappedAcceptorMateIds(self): + def test_set_unmapped_acceptor_mate_ids(self): vcau_read_ids = ['vcauRead1', 'vcauRead3', 'vcauRead5'] self.variantContextFile.set_unmapped_acceptor_mate_ids(self.context_id_answer, vcau_read_ids) - self.assertListEqual(self.variantContextFile.get_unmapped_acceptor_mate_ids(self.context_id_answer), vcau_read_ids, f"The set and returned unmapped acceptor mate ids should have been: {vcau_read_ids}") + self.assertListEqual(self.variantContextFile.get_unmapped_acceptor_mate_ids(self.context_id_answer), + vcau_read_ids, "The set and returned unmapped acceptor mate ids should have been: " + f"{vcau_read_ids}") - def test_setUnmappedDonorMateIds(self): + def test_set_unmapped_donor_mate_ids(self): vcdu_read_ids = ['vcduRead2', 'vcduRead4', 'vcduRead6'] self.variantContextFile.set_unmapped_donor_mate_ids(self.context_id_answer, vcdu_read_ids) - self.assertListEqual(self.variantContextFile.get_unmapped_donor_mate_ids(self.context_id_answer), vcdu_read_ids, f"The set and returned unmapped donor mate ids should have been: {vcdu_read_ids}") - - + self.assertListEqual(self.variantContextFile.get_unmapped_donor_mate_ids(self.context_id_answer), + vcdu_read_ids, "The set and returned unmapped donor mate ids should have been: " + f"{vcdu_read_ids}") # ====================PERFORM THE TESTS FOR WRITING OUTPUT DATA==================== diff --git a/tests/TestVcfVariant.py b/tests/TestVcfVariant.py index d0c539f..911ea80 100644 --- a/tests/TestVcfVariant.py +++ b/tests/TestVcfVariant.py @@ -14,7 +14,8 @@ def setUp(self): self.vcfvariant_obj_answer = VcfVariant(self.variant_chrom_answer, self.variant_pos_answer, self.variant_ref_answer, self.variant_alts_answer, self.variant_filter_answer, self.variant_type_answer) - self.to_string_answer = "" + self.to_string_answer = f"{self.variant_chrom_answer}\t{self.variant_pos_answer}\t{self.variant_type_answer}" \ + f"\t{self.variant_ref_answer}\t{self.variant_alts_answer}\t{self.variant_filter_answer}" def test_get_variant_chrom(self): self.assertEqual(self.vcfvariant_obj_answer.get_variant_chrom(), self.variant_chrom_answer, "Both variant " @@ -73,8 +74,8 @@ def test_get_variant_type(self): def test_set_variant_type(self): vartype_touse = "indel" self.vcfvariant_obj_answer.set_variant_type(vartype_touse) - self.assertEqual(self.vcfvariant_obj_answer.vcf_variant_type, "The set variant type should have been " - f"{vartype_touse}") + self.assertEqual(self.vcfvariant_obj_answer.vcf_variant_type, vartype_touse, "The set variant type should have " + f"been {vartype_touse}") def test_get_variant_id(self): self.assertEqual(self.vcfvariant_obj_answer.get_variant_id(), self.variant_id_answer, "Both variant identifiers" From ed2467955cd317c282884c3982de73eda5b41974 Mon Sep 17 00:00:00 2001 From: MatthieuBeukers Date: Thu, 4 Jul 2019 14:24:33 +0200 Subject: [PATCH 3/5] Update ParamChecker and TestParamChecker --- ParamChecker.py | 74 +++++---------- tests/TestParamChecker.py | 188 ++++++++++++++++++++------------------ 2 files changed, 123 insertions(+), 139 deletions(-) diff --git a/ParamChecker.py b/ParamChecker.py index 3fd1acb..689f091 100644 --- a/ParamChecker.py +++ b/ParamChecker.py @@ -18,20 +18,18 @@ def __init__(self): self.log_location = "" self.variantlist_location = "" - # Check the logging parameter to determine where to write the - # logfile to. + # Check the logging parameter to determine where to write the logfile to. def check_log(self, logparam): logloc = "VaSeBuilder.log" if logparam is not None: - # Check the location of the log file if the --log parameter - # has been set. + # Check the location of the log file if the --log parameter has been set. if (not(os.path.isfile(logparam)) and (logparam.endswith(".log") or logparam.endswith(".txt"))): logloc = logparam # Check to make sure the provided --log parameter value is - # not a directory. (Directories could be named "something.log"). + # not a directory. (Directories could be named "something.log"). if os.path.isdir(logparam): logloc = logparam + "/VaSeBuilder.log" self.log_location = logloc @@ -49,8 +47,7 @@ def check_folders_exist(self, paramvals, file_exts): f"Using the folder {foldername} " "as input folder") - # Check if the supplied value is a folder or not and - # contains any vcf/bam files. + # Check if the supplied value is a folder or not and contains any vcf/bam files. if not (os.path.isdir(foldername)): self.vaselogger.warning(f"Folder {foldername} was not found " "and will therefore be skipped") @@ -64,8 +61,7 @@ def check_folders_exist(self, paramvals, file_exts): existing_folders.append(foldername) return existing_folders - # Checks whether at least one file with a provided extension (.vcf - # or .bam) is present. + # Checks whether at least one file with a provided extension (.vcf or .bam) is present. def check_folder_contents(self, folder_to_check, file_exts): vb_count = 0 for vbfile in os.listdir(folder_to_check): @@ -90,95 +86,76 @@ def check_file_exists(self, fileloc): def is_valid_output_location(self, outfilename): return os.path.isdir(os.path.dirname(outfilename)) - # Checks whether the values of the parameters are correct (do - # files/folders exist for example). + # Checks whether the values of the parameters are correct (do files/folders exist for example). # [Function should perhaps be split into smaller functions] def check_parameters(self, vase_arg_vals): # Loop over the provided parameters. for param in vase_arg_vals: - # If the current parameter is vcfin, check whether there are - # any valid VCF folders to use. + # If the current parameter is donorvcf, check that the file containing the list of donor VCFs exists. if param == "donorvcf": if not os.path.isfile(vase_arg_vals["donorvcf"]): self.vaselogger.critical("No VCF/BCF donor list file found") return False self.vcf_filelist = vase_arg_vals["donorvcf"] - # If the current parameter is bamin, check whether there are - # any valid BAM folders to use. + # If the current parameter is donorbam, check that the file containing the list of donor BAMs exists. if param == "donorbam": if not os.path.isfile(vase_arg_vals["donorbam"]): self.vaselogger.critical("No BAM/CRAM donor list file found") return False self.bam_filelist = vase_arg_vals["donorbam"] - # If the current parameter is bam, check whether a valid - # BAM file is provided. + # If the current parameter is acceptorbam, check whether a valid BAM file is provided. if param == "acceptorbam": if not self.check_file_exists(vase_arg_vals[param]): - self.vaselogger.critical("No valid NIST BAM file supplied " - ":(") + self.vaselogger.critical("No valid acceptor/template BAM file supplied :(") return False self.acceptorbam = vase_arg_vals[param] - # If the current parameter is valfastq1, check whether a - # valid R1 fastq file is provided. + # If the current parameter is valfastq1, check whether one or more valid R1 fastq files are provided. if param == "templatefq1": if not self.check_file_exists(vase_arg_vals[param]): - self.vaselogger.critical("Provided R1 FastQ input file " - "does not exist") + self.vaselogger.critical("No valid R1 FastQ file(s) provided") return False self.fastq_in1 = vase_arg_vals[param] - # If the current parameter is valfastq2, check whether a - # valid R2 fastq file is provided. + # If the current parameter is valfastq2, check whether one or more valid R2 fastq files are provided. if param == "templatefq2": if not self.check_file_exists(vase_arg_vals[param]): - self.vaselogger.critical("Provided R2 FastQ input file " - "does not exist") + self.vaselogger.critical("No valid R2 FastQ file(s) provided") return False self.fastq_in2 = vase_arg_vals[param] - # If the current parameter is out, check whether it is a - # valid output location. + # If the current parameter is out, check whether it is a valid output location. if param == "out": if not self.is_valid_output_location(vase_arg_vals[param]): return False self.outdir = vase_arg_vals[param] - # If the current parameters is fastqout, check if a name has - # been provided. + # If the current parameters is fastqout, check if a name has been provided. if param == "fastqout": - self.fastq_out_location = self.get_output_name(vase_arg_vals[param], - "VaSe") + self.fastq_out_location = self.get_output_name(vase_arg_vals[param], "VaSe") - # If the current parameter is varcon, check whether a valid - # output location is provided. + # If the current parameter is varcon, check whether a valid output location is provided. if param == "varcon": - self.varcon_out_location = self.get_output_name(vase_arg_vals[param], - "varcon.txt") + self.varcon_out_location = self.get_output_name(vase_arg_vals[param], "varcon.txt") # Checks if the provided variant list file exists if param == "variantlist": if vase_arg_vals[param] is not None: if self.check_file_exists(vase_arg_vals[param]): self.variantlist_location = vase_arg_vals[param] - - # Return the lists of valid VCF and BAM folders that can be used - # by the program. return True - # Returns thename of the folder name of a parameter value (if the - # parameter value is ). + # Returns the name of the folder name of a parameter value (if the parameter value is ). def get_folder_name(self, foldername): if os.path.isfile(foldername) or (not os.path.isdir(foldername)): return os.path.dirname(foldername) return foldername - # Returns the name of an output file (is used for parameters - # fastqout, varcon, donorbread and acceptorbread). + # Returns the name of an output file (is used for parameters fastqout, varcon, donorbread and acceptorbread). def get_output_name(self, outfilename, defaultoutname): if outfilename is not None: if "/" in outfilename: @@ -208,8 +185,7 @@ def get_first_fastq_in_location(self): def get_second_fastq_in_location(self): return self.fastq_in2 - # Returns the location(s) and names of the two (R1 and R2) fastq - # input files. + # Returns the location(s) and names of the two (R1 and R2) fastq input files. def get_fastq_in_locations(self): return [self.fastq_in1, self.fastq_in2] @@ -217,13 +193,11 @@ def get_fastq_in_locations(self): def get_out_dir_location(self): return self.outdir - # Returns the location of the FastQ file that will be produced by - # VaSeBuilder. + # Returns the location of the FastQ file that will be produced by VaSeBuilder. def get_fastq_out_location(self): return self.outdir + "/" + self.fastq_out_location - # Returns the location of file that will contain the variants and - # their context start and stops. + # Returns the location of file that will contain the variants and their context start and stops. def get_variant_context_out_location(self): return self.outdir + "/" + self.varcon_out_location diff --git a/tests/TestParamChecker.py b/tests/TestParamChecker.py index 7c44b66..441488f 100644 --- a/tests/TestParamChecker.py +++ b/tests/TestParamChecker.py @@ -13,125 +13,135 @@ class TestParamChecker(unittest.TestCase): # Set up required def setUp(self): - self.paramCheck = ParamChecker() - self.param_list = {'vcfin': ['testdata/vcfDir'], - 'bamin': ['testdata/bamDir'], - 'templatebam': 'testdata/valbam/SRR1039513.bam', - 'templatefq1': 'testdata/fqDir/SRR1039513_1.fastq.gz', - 'templatefq2': 'testdata/fqDir/SRR1039513_2.fastq.gz', - 'fastqout': 'testdata/outDir', - 'varcon': 'testdata/outDir/varcon.txt', - 'varbread': 'testdata/outDir/varbread.txt', - 'templatebread': 'testdata/outDir/nistbread.txt' + self.param_check = ParamChecker() + self.param_list = {"donorvcf": "testdata/vcfDir/vcflistfile.txt", + "donorbam": "testdata/bamDir/bamlistfile.txt", + "templatebam": "testdata/valbam/SRR1039513.bam", + "templatefq1": "testdata/fqDir/SRR1039513_1.fastq.gz", + "templatefq2": "testdata/fqDir/SRR1039513_2.fastq.gz", + "out": "testdata/outDir", + 'fastqout': "testdata/outDir", + 'varcon': "testdata/outDir/varcon.txt", + "variantlist": "testdata/variantlist.txt" } + self.default_log_answer = "VaSeBuilder.log" + self.default_varcon_answer = "varcon.txt" # Tests that the --log will be written to the log file. - def test_checkLog_pos(self): - self.assertEqual(self.paramCheck.check_log("testdata/outDir/vaseutest.log"), "testdata/outDir/vaseutest.log", - "The log location should have been testdata/outDir/vaseutest.log") + def test_check_log_pos(self): + log_pos_answer = "testdata/outDir/vaseutest.log" + self.assertEqual(self.param_check.check_log(log_pos_answer), log_pos_answer, + f"The log location should have been {log_pos_answer}") # Tests that the --log parameter has not been set and the log will therfore be written to VaSeBuilder.log - def test_checkLog_noParamSet(self): - self.assertEqual(self.paramCheck.check_log(None), "VaSeBuilder.log", "The log location should have been VaSeBuilder.log") + def test_check_log_no_param_set(self): + self.assertEqual(self.param_check.check_log(None), self.default_log_answer, + f"The log file location should have been {self.default_log_answer}") # Tests that the --log parameter has been set but the filename does not end with .log and will therefore be written # to VaSeBuilder.log - def test_checkLog_noLog(self): - self.assertEqual(self.paramCheck.check_log("testdata/outDir/logfile.file"), "VaSeBuilder.log", "The log location should have been VaSeBuilder.log") + def test_check_log_no_log(self): + invalid_log_name = "testdata/outDir/logfile.file" + self.assertEqual(self.param_check.check_log(invalid_log_name), self.default_log_answer, + f"The log location should have been {self.default_log_answer}") # Test that the --log parameter has been set but the filename does not end with .txt and will therefore be written # to VaSeBuilder.log - def test_checkLog_noTxt(self): - self.assertEqual(self.paramCheck.check_log("testdata/outDir/logfile.file"), "VaSeBuilder.log", - "The log location should have been VaSeBuilder.log") + def test_check_log_no_txt(self): + invalid_log_name = "testdata/outDir/logfile.file" + self.assertEqual(self.param_check.check_log(invalid_log_name), self.default_log_answer, + f"The log location should have been {self.default_log_answer}") # Tests that the povided testdata folder 'bamDir' indeed contains two BAM files. - def test_checkFolderContents_pos(self): - self.assertEqual(self.paramCheck.check_folder_contents("testdata/bamDir", "bam"), 2, + def test_check_folder_contents_pos(self): + self.assertEqual(self.param_check.check_folder_contents("testdata/bamDir", "bam"), 2, "Two bam files should have been found") # Tests that the provided testdata folder 'bamDir' contains no VCF files - def test_checkFolderContents_neg(self): - self.assertEqual(self.paramCheck.check_folder_contents("testdata/bamDir", "vcf"), 0, + def test_check_folder_contents_neg(self): + self.assertEqual(self.param_check.check_folder_contents("testdata/bamDir", "vcf"), 0, "No VCF files should have been found") # Tests that the provided testdata folder 'bamDir' exists and contains BAM files. - def test_checkFoldersExist_pos(self): - self.assertListEqual(self.paramCheck.check_folders_exist(['testdata/bamDir'], 'bam'), ['testdata/bamDir'], "The folder should have been testdata/bamDir") + def test_check_folders_exist_pos(self): + self.assertListEqual(self.param_check.check_folders_exist(['testdata/bamDir'], 'bam'), ['testdata/bamDir'], + "The folder should have been testdata/bamDir") # Tests that the provided testdata folder 'bamDir' exists and does not contain VCF files. - def test_checkFoldersExist_neg(self): - self.assertListEqual(self.paramCheck.check_folders_exist(['testdata/bamDir'], 'vcf'), [], "There should be no folder") + def test_check_folders_exist_neg(self): + self.assertListEqual(self.param_check.check_folders_exist(['testdata/bamDir'], 'vcf'), [], + "There should be no folder") # Tests that a provided file indeed exists. - def test_checkFileExists_pos(self): - self.assertTrue(self.paramCheck.check_file_exists("testdata/bamDir/SRR1039508.bam"), "File should exist") + def test_check_file_exists_pos(self): + existing_file = "testdata/bamDir/SRR1039508.bam" + self.assertTrue(self.param_check.check_file_exists(existing_file), f"{existing_file} should exist") # Tests whether a non existing file indeed does not exist. - def test_checkFileExists_neg(self): - self.assertFalse(self.paramCheck.check_file_exists(""), "There should be no file") + def test_check_file_exists_neg(self): + nonexisting_file = "" + self.assertFalse(self.param_check.check_file_exists(nonexisting_file), + f"Non existing file {nonexisting_file} should not exist") # Test that the output location for out.txt is valid. - def test_isValidOutputLocation_pos(self): - self.assertTrue(self.paramCheck.is_valid_output_location("testdata/outDir/out.txt")) + def test_is_valid_output_location_pos(self): + valid_out_loc = "testdata/outDir/out.txt" + self.assertTrue(self.param_check.is_valid_output_location(valid_out_loc), + f"{valid_out_loc} should have been a valid output location") # Test that the output location for out.txt is invalid. - def test_isValidOutputLocation_neg(self): - self.assertFalse(self.paramCheck.is_valid_output_location("testdata/doesnotexist/out.txt")) + def test_is_valid_output_location_neg(self): + invalid_out_loc = "testdata/doesnotexist/out.txt" + self.assertFalse(self.param_check.is_valid_output_location(invalid_out_loc), + f"{invalid_out_loc} should have been an invalid output location") # Tests that the foldername a file is located in gets returned properly. - def test_getFolderName_pos(self): - self.assertEqual(self.paramCheck.get_folder_name("testdata/noSampleDir/noSampleBam.bam"), "testdata/noSampleDir", "Folder names should be equal bur are not") + def test_get_folder_name_pos(self): + valid_path = "testdata/noSampleDir/noSampleBam.bam" + valid_path_answer = "testdata/noSampleDir" + self.assertEqual(self.param_check.get_folder_name(valid_path), valid_path_answer, + f"Folder name should have been {valid_path_answer}") # Test that the foldername is empty. - def test_getFolderName_neg(self): - self.assertEqual(self.paramCheck.get_folder_name(""), "", "Folder names should both be empty.") - - # Tests that all provided parameters are ok. First test should return True, all others false due to one parameter missing - def test_checkParameters_pos(self): - self.assertTrue(self.paramCheck.check_parameters(self.param_list)) - - def test_checkParameters_noVcfIn(self): - parList = self.param_list.copy() - parList['vcfin'] = ['testdata/doesnotexist'] # Set the list VCF folders to a folder that does not exist. - self.assertFalse(self.paramCheck.check_parameters(parList)) - - def test_checkParameters_noBamIn(self): - parList = self.param_list.copy() - parList['bamin'] = ['testdata/doesnotexist'] # Set the list BAM folders to a folder that does not exist. - self.assertFalse(self.paramCheck.check_parameters(parList)) - - def test_checkParameters_noValBam(self): - parList = self.param_list.copy() - parList['templatebam'] = 'testdata/doesnotexist.bam' # Set the location of the bam file to one that does not exist. - self.assertFalse(self.paramCheck.check_parameters(parList)) - - def test_checkParameters_noValFq1(self): - parList = self.param_list.copy() - parList['templatefq1'] = 'testdata/doesnotexist.fq' # Set the location of the fastq file to one that does not exist. - self.assertFalse(self.paramCheck.check_parameters(parList)) - - def test_checkParameters_noValFq2(self): - parList = self.param_list.copy() - parList['templatefq2'] = 'testdata/doesnotexist.fq' # Set the location of the fastq file to one that does not exist. - self.assertFalse(self.paramCheck.check_parameters(parList)) - - def test_checkParameters_noFqOut(self): - parList = self.param_list.copy() - parList['fastqout'] = 'testdata/doesnotexist/faqOut.fq' # Set the output location of the fastq file to a folder that does not exist - self.assertFalse(self.paramCheck.check_parameters(parList)) - - def test_checkParameters_noVarcon(self): - parList = self.param_list.copy() - parList['varcon'] = 'testdata/doesnotexist/varcon.txt' # Set the location for the varcon output file to a folder that does not exist. - self.assertFalse(self.paramCheck.check_parameters(parList)) - - def test_checkParameters_noVarBread(self): - parList = self.param_list.copy() - parList['varbread'] = 'testdata/doesnotexist/varbread.txt' # Set the location for the varbread output file to a folder that does not exist. - self.assertFalse(self.paramCheck.check_parameters(parList)) - - def test_checkParameters_noNistBread(self): - parList = self.param_list.copy() - parList['templatebread'] = 'testdata/doesnotexist/nistbread.txt' # Set the location for the nistbread output file to a folder that does not exist. - self.assertFalse(self.paramCheck.check_parameters(parList)) + def test_get_folder_name_neg(self): + self.assertEqual(self.param_check.get_folder_name(""), "", "Folder names should both be empty.") + + # Tests that all provided parameters are ok. + # First test should return True, all others False due to one parameter missing + def test_check_parameters_pos(self): + self.assertTrue(self.param_check.check_parameters(self.param_list)) + + def test_check_parameters_no_donor_vcf(self): + par_list = self.param_list.copy() + par_list['donorvcf'] = 'testdata/doesnotexist.txt' # Set donorvcf parameter tp nonexisting vcf list file. + self.assertFalse(self.param_check.check_parameters(par_list)) + + def test_check_parameters_no_donor_bam(self): + par_list = self.param_list.copy() + par_list['donorbam'] = 'testdata/doesnotexist.txt' # Set donorbam parameter to nonexisting bam list file. + self.assertFalse(self.param_check.check_parameters(par_list)) + + def test_check_parameters_no_acceptor_bam(self): + par_list = self.param_list.copy() + par_list["acceptorbam"] = "testdata/doesnotexist.bam" # Set acceptorbam parameter to nonexisting bam file. + self.assertFalse(self.param_check.check_parameters(par_list)) + + def test_check_parameters_no_acceptor_fq1(self): + par_list = self.param_list.copy() + par_list["templatefq1"] = "testdata/doesnotexist.fq" # Set templatefq1 parameter to nonexisting fastq file. + self.assertFalse(self.param_check.check_parameters(par_list)) + + def test_check_parameters_no_acceptor_fq2(self): + par_list = self.param_list.copy() + par_list['templatefq2'] = 'testdata/doesnotexist.fq' # Set templatefq2 parameter to nonexisting fastq file. + self.assertFalse(self.param_check.check_parameters(par_list)) + + def test_check_parameters_no_fq_out(self): + par_list = self.param_list.copy() + par_list["fastqout"] = "testdata/doesnotexist/faqOut.fq" # Set fastqout parameter to nonexisting location + self.assertTrue(self.param_check.check_parameters(par_list)) + + def test_check_parameters_no_varcon(self): + par_list = self.param_list.copy() + par_list["varcon"] = "testdata/doesnotexist/varcon.txt" # Set varcon parameter to nonexisting location. + self.assertTrue(self.param_check.check_parameters(par_list)) From c77ce0356f5e3c6b75275bec31445de24ae156af Mon Sep 17 00:00:00 2001 From: MatthieuBeukers Date: Fri, 5 Jul 2019 10:19:13 +0200 Subject: [PATCH 4/5] Small update to TestVcfBamScanner --- tests/TestVase.py | 1 + tests/TestVcfBamScanner.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/TestVase.py diff --git a/tests/TestVase.py b/tests/TestVase.py new file mode 100644 index 0000000..10e6159 --- /dev/null +++ b/tests/TestVase.py @@ -0,0 +1 @@ +import unittest diff --git a/tests/TestVcfBamScanner.py b/tests/TestVcfBamScanner.py index 257954c..e9ebce5 100644 --- a/tests/TestVcfBamScanner.py +++ b/tests/TestVcfBamScanner.py @@ -44,6 +44,12 @@ def test_scan_bam_folders_neg(self): result_dict = self.vb_scanner.scan_bam_folders(self.none_folders) self.assertDictEqual(result_dict, answer_dict, "Both dicts should have been empty") + # Tests that a VCF file has sample information + def test_vcf_has_sample_name_pos(self): + valid_vcf_file = "testdata/vcfDir/" + self.assertTrue(self.vb_scanner.vcf_has_sample_name(valid_vcf_file), + f"VCF file {valid_vcf_file} should have had a sample identifier") + # Tests that a BAM file has sample information. def test_bam_has_sample_name_pos(self): bam_file = pysam.AlignmentFile("testdata/bamDir/SRR1039508.bam") @@ -58,6 +64,19 @@ def test_bam_has_sample_name_neg(self): bam_file.close() self.assertFalse(result_bool) + # Tests tha obtaining the BAM sample name is obtained + def test_get_bam_sample_name_pos(self): + valid_bam_file = "testdata/bamDir/SRR1039508.bam" + sample_id_answer = "" + self.assertEqual(self.vb_scanner.get_bam_sample_name(valid_bam_file), sample_id_answer, + f"Both BAM sample ids should have been {sample_id_answer}") + + # Tests that no BAM sample id is obtained + def test_get_bam_sample_name_neg(self): + no_sample_bam = "testdata/noSampleDir/noSampleBam.bam" + self.assertIsNone(self.vb_scanner.get_bam_sample_name(no_sample_bam), + f"BAM file {no_sample_bam} should have no sample") + # Tests whether the VCF to BAM map will be constructed properly. def test_get_vcf_to_bam_map(self): answer_dict = {'testdata/vcfDir/SRR1039508.vcf': 'testdata/bamDir/SRR1039508.bam', From de5a57e1209bb962aa92f6d9b2795b1c9dfa4cd9 Mon Sep 17 00:00:00 2001 From: MatthieuBeukers Date: Fri, 5 Jul 2019 12:49:49 +0200 Subject: [PATCH 5/5] First tests in TestVase --- tests/TestVase.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/tests/TestVase.py b/tests/TestVase.py index 10e6159..ab60ed6 100644 --- a/tests/TestVase.py +++ b/tests/TestVase.py @@ -1 +1,66 @@ import unittest +import logging + +from vase import VaSe +from ParamChecker import ParamChecker + + +class TestVase(unittest.TestCase): + def setUp(self): + self.vase = VaSe() + self.paramchecker = ParamChecker() + self.log_out_location = "testdata/outDir" + self.valid_varlist_file_loc = "testdata/variantlist.txt" + self.valid_config_file_loc = "testdata/configdata.cfg" + self.invalid_varlist_file_loc = "testdata/invalid_variantlist.txt" + self.invalid_config_file_loc = "tesdata/invalid_configdata.cfg" + + # Tests that the logger is started + def test_start_logger(self): + print("aap") + vase_logger = self.vase.start_logger(self.paramchecker, self.log_out_location) + self.assertIsInstance(vase_logger, logging.Logger, "A logging.Logger object should have been returned") + + # Tests that the logging level of the logger is indeed set to DEBUG + def test_start_logger_isindebug(self): + is_debug = True + vase_logger = self.vase.start_logger(self.paramchecker, self.log_out_location, is_debug) + self.assertTrue(vase_logger.getEffectiveLevel() == 10, "The log level should have been set to DEBUG") + + # Tests that the variant list is read correctly + def test_read_variant_list(self): + varlist_answer = {} + self.assertDictEqual(self.vase.read_variant_list(self.valid_varlist_file_loc), varlist_answer, + f"The read variant list should have been {varlist_answer}") + + # Tests that an empty variant list is returned if no file is submitted + def test_read_variant_list_nofile(self): + varlist_answer = {} + non_existing_varlist = "" + self.assertDictEqual(self.vase.read_variant_list(non_existing_varlist), varlist_answer, + "Both variant lists should have been empty") + + # Tests that an exception is raised if an invalid variant list file is provided + def test_read_variant_list_invalidfile(self): + invalid_varlist_file = "" + print("aap") + + # Tests that a valid config file is read correctly. + def test_read_config_file(self): + config_data_answer = {"donorvcf": "testdata/vcfDir/vcflistfile.txt", + "donorbam": "testdata/bamDir/bamlistfile.txt", + "acceptorbam": "testdata/valbam/SRR1039513.bam", + "templatefq1": "testdata/fqDir/SRR1039513_1.fq.gz", + "templatefq2": "testdata/fqDir/SRR1039513_2.fq.gz", + "out": "testdata/outDir", + "reference": "testdata/ref/reference.fa"} + self.assertDictEqual(self.vase.read_config_file(self.valid_config_file_loc), config_data_answer, + f"The read config data should have been: {config_data_answer}") + + def test_read_config_file_nofile(self): + config_data_answer = {} + non_existing_config_file = "" + self.assertDictEqual(self.vase.read_config_file(non_existing_config_file), config_data_answer, + "Both config data maps should have been empty") + + # def test_read_config_file_invalidfile(self):