def test_error_more_than_one_sample_without_sample_name(self):
     with self.assertRaises(Exception) as context:
         command = [
             os.path.join(self.test_data_dir, 'multiple_samples.vcf'),
             os.path.join(self.test_data_dir, 'snvs.bam_readcount'),
             'DNA',
         ]
         vcf_readcount_annotator.main(command)
     self.assertTrue('contains more than one sample. Please use the -s option to specify which sample to annotate.' in str(context.exception))
 def test_error_more_than_one_sample_with_wrong_sample_name(self):
     with self.assertRaises(Exception) as context:
         command = [
             os.path.join(self.test_data_dir, 'multiple_samples.vcf'),
             os.path.join(self.test_data_dir, 'snvs.bam_readcount'),
             'DNA',
             '-s', 'nonexistent_sample',
         ]
         vcf_readcount_annotator.main(command)
     self.assertTrue('does not contain a sample column for sample nonexistent_sample.' in str(context.exception))
 def test_input_AF_is_of_number_1(self):
     temp_path = tempfile.TemporaryDirectory()
     os.symlink(os.path.join(self.test_data_dir, 'af_number_1.vcf'), os.path.join(temp_path.name, 'input.vcf'))
     command = [
         os.path.join(temp_path.name, 'input.vcf'),
         os.path.join(self.test_data_dir, 'af_number_1.bam-readcount.tsv'),
         'DNA',
         '-s', 'TUMOR'
     ]
     vcf_readcount_annotator.main(command)
 def test_mutation_without_matching_readcount_value(self):
     temp_path = tempfile.TemporaryDirectory()
     os.symlink(os.path.join(self.test_data_dir, 'no_matching_readcount.vcf'), os.path.join(temp_path.name, 'input.vcf'))
     command = [
         os.path.join(temp_path.name, 'input.vcf'),
         os.path.join(self.test_data_dir, 'snvs.bam_readcount'),
         'DNA',
     ]
     vcf_readcount_annotator.main(command)
     self.assertTrue(cmp(os.path.join(self.test_data_dir, 'no_matching_readcount.readcount.vcf'), os.path.join(temp_path.name, 'input.readcount.vcf')))
     temp_path.cleanup()
 def test_single_sample_vcf_with_existing_readcount_annotations(self):
     temp_path = tempfile.TemporaryDirectory()
     os.symlink(os.path.join(self.test_data_dir, 'input.readcount.vcf'), os.path.join(temp_path.name, 'input.vcf'))
     command = [
         os.path.join(temp_path.name, 'input.vcf'),
         os.path.join(self.test_data_dir, 'snvs.bam_readcount'),
         'DNA',
     ]
     vcf_readcount_annotator.main(command)
     self.assertTrue(cmp(os.path.join(self.test_data_dir, 'single_sample_with_existing_readcount_annotations.readcount.vcf'), os.path.join(temp_path.name, 'input.readcount.vcf')))
     temp_path.cleanup()
 def test_multi_sample_vcf(self):
     temp_path = tempfile.TemporaryDirectory()
     os.symlink(os.path.join(self.test_data_dir, 'multiple_samples.vcf'), os.path.join(temp_path.name, 'input.vcf'))
     command = [
         os.path.join(temp_path.name, 'input.vcf'),
         os.path.join(self.test_data_dir, 'snvs.bam_readcount'),
         'DNA',
         '-s', 'H_NJ-HCC1395-HCC1395',
     ]
     vcf_readcount_annotator.main(command)
     self.assertTrue(cmp(os.path.join(self.test_data_dir, 'multiple_samples.readcount.vcf'), os.path.join(temp_path.name, 'input.readcount.vcf')))
     temp_path.cleanup()
 def test_hom_ref_genotype(self):
     temp_path = tempfile.TemporaryDirectory()
     os.symlink(os.path.join(self.test_data_dir, 'hom_ref.vcf'), os.path.join(temp_path.name, 'input.vcf'))
     command = [
         os.path.join(temp_path.name, 'input.vcf'),
         os.path.join(self.test_data_dir, 'hom_ref.bam_readcount'),
         'DNA',
         '-s', 'NORMAL'
     ]
     vcf_readcount_annotator.main(command)
     self.assertTrue(cmp(os.path.join(self.test_data_dir, 'hom_ref.readcount.vcf'), os.path.join(temp_path.name, 'input.readcount.vcf')))
     temp_path.cleanup()
    def test_mnp(self):
        temp_path = tempfile.TemporaryDirectory()
        os.symlink(os.path.join(self.test_data_dir, 'input.mnp.vcf.gz'), os.path.join(temp_path.name, 'input.vcf.gz'))
        command = [
            os.path.join(temp_path.name, 'input.vcf.gz'),
            os.path.join(self.test_data_dir, 'complex_indel.bam_readcount'),
            'DNA',
            '-s', 'TUMOR',
        ]
        vcf_readcount_annotator.main(command)

        self.assertTrue(cmp(os.path.join(self.test_data_dir, 'mnp.readcount.vcf.gz'), os.path.join(temp_path.name, 'input.readcount.vcf.gz')))
        temp_path.cleanup()
    def test_indel_mode(self):
        temp_path = tempfile.TemporaryDirectory()
        os.symlink(os.path.join(self.test_data_dir, 'input.snvs_and_indels.vcf'), os.path.join(temp_path.name, 'input.vcf'))
        command = [
            os.path.join(temp_path.name, 'input.vcf'),
            os.path.join(self.test_data_dir, 'indels.bam_readcount'),
            'DNA',
            '--variant-type', 'indel',
        ]
        vcf_readcount_annotator.main(command)

        self.assertTrue(cmp(os.path.join(self.test_data_dir, 'indel_mode.bam_readcount.vcf'), os.path.join(temp_path.name, 'input.readcount.vcf')))
        temp_path.cleanup()
    def test_duplicate_bam_readcount_entries_same_depth(self):
        temp_path = tempfile.TemporaryDirectory()
        os.symlink(os.path.join(self.test_data_dir, 'duplicate_entries.vcf'), os.path.join(temp_path.name, 'input.vcf'))
        logging.disable(logging.NOTSET)
        with LogCapture() as l:
            command = [
                os.path.join(temp_path.name, 'input.vcf'),
                os.path.join(self.test_data_dir, 'duplicate_entries_same_depths.bam_readcount'),
                'DNA', '-s', 'H_NJ-HCC1395-HCC1395'
            ]
            vcf_readcount_annotator.main(command)
            warn_message = "Both depths match, so this field will be written, but count and frequency fields will be skipped."
            logged_str = "".join(l.actual()[0])
            self.assertTrue(warn_message in logged_str)

            self.assertTrue(cmp(os.path.join(self.test_data_dir, 'duplicate_entries_same_depths.bam_readcount.vcf'), os.path.join(temp_path.name, 'input.readcount.vcf')))
        temp_path.cleanup()
    def test_duplicate_bam_readcount_entries_discrepant_depth(self):
        temp_path = tempfile.TemporaryDirectory()
        os.symlink(os.path.join(self.test_data_dir, 'duplicate_entries.vcf'), os.path.join(temp_path.name, 'input.vcf'))
        logging.disable(logging.NOTSET)
        with LogCapture() as l:
            command = [
                os.path.join(temp_path.name, 'input.vcf'),
                os.path.join(self.test_data_dir, 'duplicate_entries_discrepant_depths.bam_readcount'),
                'DNA'
            ]
            vcf_readcount_annotator.main(command)
            warn_message = "Depths are discrepant, so neither entry will be included in the output vcf."
            logged_str = "".join(l.actual()[0])
            #the warning is broken into several lines when written to the log; manually extract the log, which is returned as 
            #a list of tuples. grab the relevant (and in this case only) tuple, the first, then combine into one string for comparison
            self.assertTrue(warn_message in logged_str)

            self.assertTrue(cmp(os.path.join(self.test_data_dir, 'duplicate_entries_discrepant_depths.bam_readcount.vcf'), os.path.join(temp_path.name, 'input.readcount.vcf')))
        temp_path.cleanup()