Example #1
0
 def test_add_multiple_key_value_pairs(self):
     p = Params()
     v1 = "some_value"
     v2 = "other_value"
     p.add(key1=v1, key2=v2)
     self.assertEqual(p.get("key1"), v1)
     self.assertEqual(p.get("key2"), v2)
	def test_samples_created_correctly_for_skipping_align(self, mock_find_files, mock_parse_method):
		"""
		Tests the case where we want to skip alignment and the target suffix is given for the BAM files
		In this test, the call to the find_file method is mocked out with a dummy return value
		"""
		# list of tuples linking the sample names and conditions:
		pairings = [('A', 'X'),('B', 'X'),('C', 'Y')]
		mock_parse_method.return_value = pairings
		
		# mock the find_file() method returning some paths to bam files:
		bamfiles = [['A.sort.bam'],['B.sort.bam'],['C.sort.bam']]
		mock_find_files.side_effect = bamfiles

		# setup all the necessary parameters that the method will look at:
		p = PipelineBuilder('')
		p.all_samples = []
		mock_pipeline_params = Params()
		mock_pipeline_params.add(project_directory = '/path/to/project_dir')
		mock_pipeline_params.add(sample_annotation_file = '/path/to/project_dir/samples.txt')
		mock_pipeline_params.add(skip_align = True)
		mock_pipeline_params.add(target_bam = 'sort.bam')
		p.builder_params = mock_pipeline_params

		p._PipelineBuilder__check_and_create_samples()
		for i,s in enumerate(p.all_samples):
			self.assertTrue(s.sample_name == pairings[i][0])
			self.assertTrue(s.read_1_fastq == None)
			self.assertTrue(s.read_2_fastq == None)
			self.assertTrue(s.bamfiles == bamfiles[i] )
Example #3
0
    def test_asking_for_missing_value_kills_pipeline(self):
        p = Params()

        # add some value. Not necessary to do this.
        v = "some_value"
        p.add(key=v)
        self.assertRaises(ParameterNotFoundException, p.get, "missing_val")
	def test_samples_created_correctly_for_skipping_align_with_mocked_directory_structure(self, mock_os, mock_join, mock_parse_method):
		"""
		Tests the case where we want to skip alignment and the target suffix is given for the BAM files
		In this case, we mock the return value from os.walk() and make the actual call to the find_files() method
		"""
		# list of tuples linking the sample names and conditions:
		pairings = [('A', 'X'),('B', 'X'),('C', 'Y')]
		mock_parse_method.return_value = pairings
		
		# mock the find_files() method returning some paths to bam files:
		bamfiles = ['/path/to/project/dir/Sample_A/A.sort.primary.bam','/path/to/project/dir/Sample_B/B.sort.primary.bam','/path/to/project/dir/C.sort.primary.bam']

		# setup all the necessary parameters that the method will look at:
		p = PipelineBuilder('')
		p.all_samples = []
		mock_pipeline_params = Params()
		mock_pipeline_params.add(project_directory = '/path/to/project_dir')
		mock_pipeline_params.add(sample_annotation_file = '/path/to/project_dir/samples.txt')
		mock_pipeline_params.add(skip_align = True)
		mock_pipeline_params.add(target_bam = 'sort.primary.bam')
		p.builder_params = mock_pipeline_params

		mock_os.walk.return_value = [('/path/to/project/dir', ['Sample_A', 'Sample_B'], ['C.sort.primary.bam']),
			('/path/to/project/dir/Sample_A', ['another_dir'], ['A.sort.primary.bam']),
			('/path/to/project/dir/Sample_B', [], ['B.sort.primary.bam']),
			('/path/to/project/dir/Sample_A/another_dir', [], ['A.sort.bam']),
			]

		p._PipelineBuilder__check_and_create_samples()
		for i,s in enumerate(p.all_samples):
			self.assertTrue(s.sample_name == pairings[i][0])
			self.assertTrue(s.read_1_fastq == None)
			self.assertTrue(s.read_2_fastq == None)
			self.assertTrue(s.bamfiles == [bamfiles[i]] )
Example #5
0
    def test_raises_exception_if_skipping_align_and_pairing_not_specified(
            self, mock_find_files, mock_parse_method):
        """
		Tests the case where we want to skip alignment and the user has not given whether the BAM files were created
		from paired or unpaired protocol
		"""
        # list of tuples linking the sample names and conditions:
        pairings = [('A', 'X'), ('B', 'X'), ('C', 'Y')]
        mock_parse_method.return_value = pairings

        # mock the find_file() method returning some paths to bam files:
        bamfiles = [['A.sort.bam'], ['B.sort.bam'], ['C.sort.bam']]
        mock_find_files.side_effect = bamfiles

        # setup all the necessary parameters that the method will look at:
        p = PipelineBuilder('')
        p.all_samples = []
        mock_pipeline_params = Params()
        mock_pipeline_params.add(project_directory='/path/to/project_dir')
        mock_pipeline_params.add(
            sample_annotation_file='/path/to/project_dir/samples.txt')
        mock_pipeline_params.add(skip_align=True)
        mock_pipeline_params.add(target_bam='sort.bam')
        p.builder_params = mock_pipeline_params

        with self.assertRaises(ParameterNotFoundException):
            p._PipelineBuilder__check_and_create_samples()
    def test_group_countfiles_raises_exception_if_missing_type(self):
        """
		Test the method that aggregates all the countfiles generated from each 'type' of bam file.  That is, we may have multiple bam files for each sample (e.g. primary alignments, deduplicated, etc).
		We will be generating a countfile for each one of those.  When we assemble into a count matrix, we obviously group the files of a particular 'type' (e.g. those coming from deduplicated BAM files).
		This tests that the the glob methods are called with the correct parameters given the sample annotations prescribed.

		This one tests that an exception is raised if one of the countfile 'types' is missing.  Here, sample B is missing a countfile corresponding to the primary.counts- based BAM files
		"""

        p = Params()
        p.add(feature_counts_output_dir='/path/to/final/featureCounts')

        s1 = Sample('A', 'X')
        s1.countfiles = [
            '/path/to/final/featureCounts/A.counts',
            '/path/to/final/featureCounts/A.primary.counts',
            '/path/to/final/featureCounts/A.primary.dedup.counts'
        ]
        s2 = Sample('B', 'Y')
        s2.countfiles = [
            '/path/to/final/featureCounts/B.counts',
            '/path/to/final/featureCounts/B.primary.dedup.counts'
        ]
        s3 = Sample('C', 'Z')
        s3.countfiles = [
            '/path/to/final/featureCounts/C.counts',
            '/path/to/final/featureCounts/C.primary.counts',
            '/path/to/final/featureCounts/C.primary.dedup.counts'
        ]

        project = Project()
        project.add_parameters(p)
        project.add_samples([s1, s2, s3])

        mock_util_methods = mock.Mock()
        mock_case_insensitive_glob = mock.Mock()
        mock_case_insensitive_glob.side_effect = [
            [
                '/path/to/final/featureCounts/A.counts',
                '/path/to/final/featureCounts/B.counts',
                '/path/to/final/featureCounts/C.counts'
            ],
            [
                '/path/to/final/featureCounts/A.primary.counts',
                '/path/to/final/featureCounts/C.primary.counts'
            ],
            [
                '/path/to/final/featureCounts/A.primary.dedup.counts',
                '/path/to/final/featureCounts/B.primary.dedup.counts',
                '/path/to/final/featureCounts/C.primary.dedup.counts'
            ]
        ]
        with self.assertRaises(self.module.CountfileQuantityException):
            result = self.module.get_countfile_groupings(
                project, mock_case_insensitive_glob)
    def test_group_countfiles(self):
        """
		Test the method that aggregates all the countfiles generated from each 'type' of bam file.  That is, we may have multiple bam files for each sample (e.g. primary alignments, deduplicated, etc).
		We will be generating a countfile for each one of those.  When we assemble into a count matrix, we obviously group the files of a particular 'type' (e.g. those coming from deduplicated BAM files).
		This tests that the the glob methods are called with the correct parameters given the sample annotations prescribed.
		"""

        p = Params()
        cp = Params()
        cp.add(feature_counts_output_dir='/path/to/final/featureCounts')

        s1 = Sample('A', 'X')
        s1.countfiles = [
            '/path/to/final/featureCounts/A.counts',
            '/path/to/final/featureCounts/A.primary.counts',
            '/path/to/final/featureCounts/A.primary.dedup.counts'
        ]
        s2 = Sample('B', 'Y')
        s2.countfiles = [
            '/path/to/final/featureCounts/B.counts',
            '/path/to/final/featureCounts/B.primary.counts',
            '/path/to/final/featureCounts/B.primary.dedup.counts'
        ]
        s3 = Sample('C', 'Z')
        s3.countfiles = [
            '/path/to/final/featureCounts/C.counts',
            '/path/to/final/featureCounts/C.primary.counts',
            '/path/to/final/featureCounts/C.primary.dedup.counts'
        ]

        project = Project()
        project.add_parameters(p)
        project.add_samples([s1, s2, s3])

        result = self.module.get_countfile_groupings(project, cp)
        expected_result = [
            [
                '/path/to/final/featureCounts/A.counts',
                '/path/to/final/featureCounts/B.counts',
                '/path/to/final/featureCounts/C.counts'
            ],
            [
                '/path/to/final/featureCounts/A.primary.counts',
                '/path/to/final/featureCounts/B.primary.counts',
                '/path/to/final/featureCounts/C.primary.counts'
            ],
            [
                '/path/to/final/featureCounts/A.primary.dedup.counts',
                '/path/to/final/featureCounts/B.primary.dedup.counts',
                '/path/to/final/featureCounts/C.primary.dedup.counts'
            ]
        ]
        self.assertEqual(result, expected_result)
	def test_bad_genome_parameter_raises_exception(self, mock_os):
		"""
		Pass a genome parameter that is not in the genomes directory
		"""

		p = PipelineBuilder('/path/to/dir')
		mock_pipeline_params = Params()
		mock_pipeline_params.add(genomes_dir = 'genome_info', genome = 'XYZ')
		p.builder_params = mock_pipeline_params

		mock_os.listdir.return_value = ['hg19.cfg', 'mm10.cfg']			

		with self.assertRaises(IncorrectGenomeException):
			p._PipelineBuilder__check_genome_valid()
Example #9
0
    def test_shorthand_addition_of_param_objects(self):
        """
		This tests the operator overload of '+'
		"""
        p1 = Params()
        p1.add(a=1, b=2)
        p2 = Params()
        p2.add(c=3, d=4)

        p1 += p2
        self.assertEqual(p1.get("a"), 1)
        self.assertEqual(p1.get("b"), 2)
        self.assertEqual(p1.get("c"), 3)
        self.assertEqual(p1.get("d"), 4)
	def test_missing_pipeline_config_file(self, mock_os):
		"""
		The pipeline configuration file is missing (not likely, but possible!)
		"""
		
		p = PipelineBuilder('')
		mock_pipeline_params = Params()
		mock_pipeline_params.add(pipeline_home = '/path/to/dir')
		p.builder_params = mock_pipeline_params

		# mock the missing config file: 
		mock_os.listdir.return_value = []

		with self.assertRaises(ConfigFileNotFoundException):
			p._PipelineBuilder__read_pipeline_config()
	def test_missing_default_project_config_file_with_no_alternative_given(self, mock_os):
		"""
		No config file passed via commandline and none found in the project_configuration directory
		"""
		
		p = PipelineBuilder('/path/to/dir')
		mock_pipeline_params = Params()
		mock_pipeline_params.add(project_configuration_file = None,
					project_configurations_dir = '/path/to/dir')
		p.builder_params = mock_pipeline_params

		# mock the missing config file: 
		mock_os.listdir.return_value = ['something.cfg'] # anything here, just not 'default.cfg'

		with self.assertRaises(ConfigFileNotFoundException):
			p._PipelineBuilder__check_project_config()
Example #12
0
    def test_raises_exception_if_adding_param_objects_with_same_values(self):
        """
		This tests the operator overload of '+'
		"""
        p1 = Params()
        p1.add(a=1, b=2)
        p2 = Params()
        p2.add(a=3, d=4)

        with self.assertRaises(ParameterOverwriteException):
            p3 = p1 + p2
	def test_bad_aligner_parameter_raises_exception(self):
		"""
		Pass a aligner parameter that is not specified in the aligners config file
		"""

		available_aligners = ('star', 'snapr')
		default_aligner = 'star'

		# create a PipelineBuilder and add mocked pipeline/project objects as patches
		p = PipelineBuilder('/path/to/dir')
		mock_pipeline_params = Params()
		mock_pipeline_params.add(available_aligners = available_aligners, 
					default_aligner = default_aligner,
					aligner='junk')
		p.builder_params = mock_pipeline_params
		p._PipelineBuilder__get_aligner_info = mock.Mock()
		
		with self.assertRaises(IncorrectAlignerException):
			p._PipelineBuilder__check_aligner_valid()
Example #14
0
    def test_missing_aligner_config_file_raises_exception(self, mock_os):

        available_aligners = ('star', 'snapr')
        default_aligner = 'star'

        # create a PipelineBuilder and add mocked pipeline/project objects as patches
        p = PipelineBuilder('/path/to/dir')
        mock_pipeline_params = Params()
        mock_pipeline_params.add(available_aligners=available_aligners,
                                 default_aligner=default_aligner,
                                 aligner=None,
                                 aligners_dir='/path/to/dir',
                                 genome='mm9')
        p.builder_params = mock_pipeline_params
        p._PipelineBuilder__get_aligner_info = mock.Mock()

        mock_os.listdir.return_value = []

        with self.assertRaises(ConfigFileNotFoundException):
            p._PipelineBuilder__check_aligner_valid()
Example #15
0
    def test_alignment_calls(self):

        mock_process = mock.Mock(name='mock_process')
        mock_process.communicate.return_value = (('', ''))
        mock_process.returncode = 0

        mock_popen = mock.Mock(name='mock_popen')
        mock_popen.return_value = mock_process

        self.module.subprocess = mock.Mock()
        self.module.subprocess.Popen = mock_popen
        self.module.subprocess.STDOUT = ''
        self.module.subprocess.PIPE = ''

        self.module.os.chmod = mock.Mock()
        self.module.assert_memory_reasonable = mock.Mock()
        self.module.assert_memory_reasonable.return_value = True
        p = Params()
        p.add(wait_length='10')
        p.add(wait_cycles='50')
        p.add(min_memory='40')
        paths = ['/path/to/a.sh', '/path/to/b.sh']
        self.module.execute_alignments(paths, p)

        calls = [
            mock.call('/path/to/a.sh',
                      shell=True,
                      stderr=self.module.subprocess.STDOUT,
                      stdout=self.module.subprocess.PIPE),
            mock.call('/path/to/b.sh',
                      shell=True,
                      stderr=self.module.subprocess.STDOUT,
                      stdout=self.module.subprocess.PIPE)
        ]
        self.module.subprocess.Popen.assert_has_calls(calls)
Example #16
0
    def test_alignment_call_raises_exception(self):
        import subprocess

        mock_process = mock.Mock(name='mock_process')
        mock_process.communicate.return_value = (('', ''))
        mock_process.returncode = 1

        mock_popen = mock.Mock(name='mock_popen')
        mock_popen.return_value = mock_process

        self.module.subprocess = mock.Mock()
        self.module.subprocess.Popen = mock_popen
        self.module.subprocess.STDOUT = ''
        self.module.subprocess.PIPE = ''

        self.module.os.chmod = mock.Mock()
        self.module.assert_memory_reasonable = mock.Mock()
        self.module.assert_memory_reasonable.return_value = True
        p = Params()
        p.add(wait_length='10')
        p.add(wait_cycles='50')
        p.add(min_memory='40')
        paths = ['/path/to/a.sh', '/path/to/b.sh']
        with self.assertRaises(self.module.AlignmentScriptErrorException):
            self.module.execute_alignments(paths, p)

        # assert that the second script was not called due to the first one failing.
        calls = [
            mock.call('/path/to/a.sh',
                      shell=True,
                      stderr=self.module.subprocess.STDOUT,
                      stdout=self.module.subprocess.PIPE)
        ]
        self.module.subprocess.Popen.assert_has_calls(calls)
Example #17
0
 def test_add_multiple_dicts(self):
     p = Params()
     d1 = {"a": 1, "b": 2}
     d2 = {"c": 3, "d": 4}
     p.add(d1, d2)
     self.assertEqual(p.get("a"), 1)
     self.assertEqual(p.get("b"), 2)
     self.assertEqual(p.get("c"), 3)
     self.assertEqual(p.get("d"), 4)
	def test_default_aligner_used_when_not_specified(self, mock_os):
		"""
		If no commandline arg given for aligner, check that it resorts to the default
		"""
	
		available_aligners = ('star', 'snapr')
		default_aligner = 'star'

		# create a PipelineBuilder and add mocked pipeline/project objects as patches
		p = PipelineBuilder('/path/to/dir')
		mock_pipeline_params = Params()
		mock_pipeline_params.add(available_aligners = available_aligners, 
					default_aligner = default_aligner,
					aligner=None,
					aligners_dir = '/path/to/dir',
					genome = 'hg19')
		p.builder_params = mock_pipeline_params
		p.all_components = []
		p._PipelineBuilder__get_aligner_info = mock.Mock()

		mock_os.listdir.return_value = ['star.cfg']
		
		p._PipelineBuilder__check_aligner_valid()
		self.assertEqual(p.builder_params.get('aligner'), default_aligner)
Example #19
0
    def test_add_both_simultaneously(self):
        p = Params()
        v = "some_value"
        d = {"a": 1, "b": 2}

        p.add(d, key=v)

        self.assertEqual(p.get("a"), 1)
        self.assertEqual(p.get("b"), 2)
        self.assertEqual(p.get("key"), v)
	def test_creates_contrasts_correctly_with_no_contrast_file_given(self):

		#make some samples:
		all_samples = [Sample('A', 'X'), Sample('B', 'Y'), Sample('C', 'Z')]
		p = PipelineBuilder('')
		mock_pipeline_params = Params()
		mock_pipeline_params.add(skip_analysis = False)
		mock_pipeline_params.add(contrast_file = None)
		p.builder_params = mock_pipeline_params
		p.all_samples = all_samples

		p._PipelineBuilder__check_contrast_file()
		expected_result = set([('X','Y'),('X','Z'),('Y','Z')])
		self.assertTrue(set_of_tuples_is_equivalent(p.contrasts, expected_result))
	def test_alignment_calls(self):
		self.module.subprocess = mock.Mock()
		self.module.os.chmod = mock.Mock()
		self.module.assert_memory_reasonable = mock.Mock()
		self.module.assert_memory_reasonable.return_value = True
		p = Params()
		p.add(wait_length = '10')
		p.add(wait_cycles = '50')
		p.add(min_memory = '40')
		paths = ['/path/to/a.sh', '/path/to/b.sh']
		self.module.execute_alignments(paths, p)

		calls = [mock.call('/path/to/a.sh', shell=True),
			mock.call('/path/to/b.sh', shell=True)]
		self.module.subprocess.check_call.assert_has_calls(calls)
	def test_raises_exception_if_non_sensible_contrast_specified(self, mock_parse):

		#make some samples:
		all_samples = [Sample('A', 'X'), Sample('B', 'Y'), Sample('C', 'Z')]
		p = PipelineBuilder('')
		mock_pipeline_params = Params()
		mock_pipeline_params.add(skip_analysis = False)
		mock_pipeline_params.add(contrast_file = 'contrast.txt')
		p.builder_params = mock_pipeline_params
		p.all_samples = all_samples

		# note the specification of a contrast of Y against A.  However, we have no samples from condition A.
		mock_parse.return_value = set([('X','Y'),('X','Z'),('Y','A')]) 

		with self.assertRaises(ContrastSpecificationException):
			p._PipelineBuilder__check_contrast_file()
Example #23
0
    def test_alignment_call_waits_properly(self):
        import subprocess

        mock_process = mock.Mock(name='mock_process')
        mock_process.communicate.return_value = (('', ''))
        mock_process.returncode = 0

        mock_popen = mock.Mock(name='mock_popen')
        mock_popen.return_value = mock_process

        self.module.subprocess = mock.Mock()
        self.module.subprocess.Popen = mock_popen
        self.module.subprocess.STDOUT = ''
        self.module.subprocess.PIPE = ''

        self.module.os.chmod = mock.Mock()
        self.module.assert_memory_reasonable = mock.Mock()
        self.module.sleep = mock.Mock()
        self.module.assert_memory_reasonable.side_effect = [
            False, False, True, True
        ]
        p = Params()
        p.add(wait_length='10')
        p.add(wait_cycles='50')
        p.add(min_memory='40')
        paths = ['/path/to/a.sh', '/path/to/b.sh']

        self.module.execute_alignments(paths, p)

        # assert that the script was eventually called.
        calls = [
            mock.call('/path/to/a.sh',
                      shell=True,
                      stderr=self.module.subprocess.STDOUT,
                      stdout=self.module.subprocess.PIPE),
            mock.call('/path/to/b.sh',
                      shell=True,
                      stderr=self.module.subprocess.STDOUT,
                      stdout=self.module.subprocess.PIPE)
        ]
        self.module.subprocess.Popen.assert_has_calls(calls)

        # assert that it had to wait twice (via calling sleep() twice)
        calls = [mock.call(600), mock.call(600)]
        self.module.sleep.assert_has_calls(calls)
	def test_alignment_call_raises_exception(self):
		import subprocess
		self.module.subprocess = mock.Mock()
		self.module.os.chmod = mock.Mock()
		self.module.assert_memory_reasonable = mock.Mock()
		self.module.assert_memory_reasonable.return_value = True
		p = Params()
		p.add(wait_length = '10')
		p.add(wait_cycles = '50')
		p.add(min_memory = '40')
		paths = ['/path/to/a.sh', '/path/to/b.sh']
		self.module.subprocess.check_call.side_effect = [subprocess.CalledProcessError(0,'cmd'), None]
		with self.assertRaises(subprocess.CalledProcessError):
			self.module.execute_alignments(paths, p)

		# assert that the second script was not called due to the first one failing.
		calls = [mock.call('/path/to/a.sh', shell=True)]
		self.module.subprocess.check_call.assert_has_calls(calls)
	def test_alignment_call_timesout_properly(self):
		import subprocess
		self.module.subprocess = mock.Mock()
		self.module.os.chmod = mock.Mock()
		self.module.assert_memory_reasonable = mock.Mock()
		self.module.sleep = mock.Mock()
		self.module.assert_memory_reasonable.side_effect = [False, False, False, False]
		p = Params()
		p.add(wait_length = '10')
		p.add(wait_cycles = '3')
		p.add(min_memory = '40')
		paths = ['/path/to/a.sh']
			
		with self.assertRaises(self.module.AlignmentTimeoutException):
			self.module.execute_alignments(paths, p)


		# assert that it had to wait the proper amount of times (via calling sleep())
		calls = [mock.call(600), mock.call(600), mock.call(600)]
		self.module.sleep.assert_has_calls(calls)
	def test_alignment_call_waits_properly(self):
		import subprocess
		self.module.subprocess = mock.Mock()
		self.module.os.chmod = mock.Mock()
		self.module.assert_memory_reasonable = mock.Mock()
		self.module.sleep = mock.Mock()
		self.module.assert_memory_reasonable.side_effect = [False, False, True, True]
		p = Params()
		p.add(wait_length = '10')
		p.add(wait_cycles = '50')
		p.add(min_memory = '40')
		paths = ['/path/to/a.sh', '/path/to/b.sh']
			
		self.module.execute_alignments(paths, p)

		# assert that the script was eventually called.
		calls = [mock.call('/path/to/a.sh', shell=True),
			mock.call('/path/to/b.sh', shell=True)]
		self.module.subprocess.check_call.assert_has_calls(calls)

		# assert that it had to wait twice (via calling sleep() twice)
		calls = [mock.call(600), mock.call(600)]
		self.module.sleep.assert_has_calls(calls)
Example #27
0
    def test_missing_countfile_raises_exception(self):
        """
		Test one of the files is ok (the first), but the second is not found (for whatever reason).  Test that we throw an exception, 
		and that the one successful call was indeed made correctly.
		"""
        self.module.call_script = mock.Mock()
        project = Project()
        project.raw_count_matrices = [
            '/path/to/raw_counts/raw_count_matrix.primary.counts',
            '/path/to/raw_counts/raw_count_matrix.primary.dedup.counts'
        ]

        project_params = Params()
        component_params = Params()
        project_params.add(raw_count_matrix_file_prefix='raw_count_matrix')
        project_params.add(feature_counts_file_extension='counts')
        component_params.add(deseq_output_dir='/path/to/final/deseq_dir')
        component_params.add(deseq_script='deseq_original.R')
        project_params.add(sample_annotation_file='/path/to/samples.txt')
        component_params.add(deseq_output_tag='deseq')
        component_params.add(deseq_contrast_flag='_vs_')
        component_params.add(number_of_genes_for_heatmap='30')
        component_params.add(heatmap_file_tag='heatmap.png')

        project.add_parameters(project_params)
        project.contrasts = [('X', 'Y'), ('X', 'Z')]

        # construct the expected call strings:
        call_1 = '/path/to/raw_counts/raw_count_matrix.primary.counts /path/to/samples.txt X Y /path/to/final/deseq_dir/Y_vs_X.primary.deseq /path/to/final/deseq_dir/Y_vs_X.primary.heatmap.png 30'
        call_2 = '/path/to/raw_counts/raw_count_matrix.primary.counts /path/to/samples.txt X Z /path/to/final/deseq_dir/Z_vs_X.primary.deseq /path/to/final/deseq_dir/Z_vs_X.primary.heatmap.png 30'

        m = mock.MagicMock(side_effect=[True, False])
        path = self.module.os.path
        with mock.patch.object(path, 'isfile', m):
            with self.assertRaises(
                    self.module.MissingCountMatrixFileException):
                self.module.call_deseq(project, component_params)
            calls = [
                mock.call('deseq_original.R', call_1),
                mock.call('deseq_original.R', call_2)
            ]
            self.module.call_script.assert_has_calls(calls)
Example #28
0
    def test_correct_calls_are_made(self):
        """
		Tests that the correct arguments are passed to the method which calls the DESeq script.
		Mostly tests the path renaming, etc.
		"""
        self.module.call_script = mock.Mock()
        project = Project()
        project.raw_count_matrices = [
            '/path/to/raw_counts/raw_count_matrix.primary.counts',
            '/path/to/raw_counts/raw_count_matrix.primary.dedup.counts'
        ]
        project_params = Params()
        component_params = Params()
        project_params.add(raw_count_matrix_file_prefix='raw_count_matrix')
        project_params.add(feature_counts_file_extension='counts')
        component_params.add(deseq_output_dir='/path/to/final/deseq_dir')
        component_params.add(deseq_script='deseq_original.R')
        project_params.add(sample_annotation_file='/path/to/samples.txt')
        component_params.add(deseq_output_tag='deseq')
        component_params.add(deseq_contrast_flag='_vs_')
        component_params.add(number_of_genes_for_heatmap='30')
        component_params.add(heatmap_file_tag='heatmap.png')

        project.add_parameters(project_params)
        project.contrasts = [('X', 'Y'), ('X', 'Z')]

        # construct the expected call strings:
        call_1 = '/path/to/raw_counts/raw_count_matrix.primary.counts /path/to/samples.txt X Y /path/to/final/deseq_dir/Y_vs_X.primary.deseq /path/to/final/deseq_dir/Y_vs_X.primary.heatmap.png 30'
        call_2 = '/path/to/raw_counts/raw_count_matrix.primary.counts /path/to/samples.txt X Z /path/to/final/deseq_dir/Z_vs_X.primary.deseq /path/to/final/deseq_dir/Z_vs_X.primary.heatmap.png 30'
        call_3 = '/path/to/raw_counts/raw_count_matrix.primary.dedup.counts /path/to/samples.txt X Y /path/to/final/deseq_dir/Y_vs_X.primary.dedup.deseq /path/to/final/deseq_dir/Y_vs_X.primary.dedup.heatmap.png 30'
        call_4 = '/path/to/raw_counts/raw_count_matrix.primary.dedup.counts /path/to/samples.txt X Z /path/to/final/deseq_dir/Z_vs_X.primary.dedup.deseq /path/to/final/deseq_dir/Z_vs_X.primary.dedup.heatmap.png 30'

        m = mock.MagicMock(side_effect=[True, True])
        path = self.module.os.path
        with mock.patch.object(path, 'isfile', m):
            self.module.call_deseq(project, component_params)
            calls = [
                mock.call('deseq_original.R', call_1),
                mock.call('deseq_original.R', call_2),
                mock.call('deseq_original.R', call_3),
                mock.call('deseq_original.R', call_4)
            ]
            self.module.call_script.assert_has_calls(calls)
Example #29
0
 def test_missing_count_matrix_files_raises_exception(self):
     project = Project()
     cp = Params()
     with self.assertRaises(self.module.NoCountMatricesException):
         self.module.call_deseq(project, cp)
	def test_exception_raised_if_multiple_fastq_found(self, mock_os, mock_glob, mock_join, mock_parse_method):
		"""
		Tests that an exception is raised if there is more than 1 fastq file (e.g. if glob's matching
		routine matches more than one fastq file)
		"""
		# list of tuples linking the sample names and conditions:
		pairings = [('A', 'X'),('B', 'X'),('C', 'Y')]
		mock_parse_method.return_value = pairings
		
		# setup all the necessary parameters that the method will look at:
		p = PipelineBuilder('')
		p.all_samples = []
		mock_pipeline_params = Params()
		mock_pipeline_params.add(project_directory = '/path/to/project_dir')
		mock_pipeline_params.add(read_1_fastq_tag = '_R1_001.fastq.gz')
		mock_pipeline_params.add(read_2_fastq_tag = '_R2_001.fastq.gz')
		mock_pipeline_params.add(sample_annotation_file = '/path/to/project_dir/samples.txt')
		mock_pipeline_params.add(skip_align = False)
		mock_pipeline_params.add(paired_alignment = False)
		mock_pipeline_params.add(target_bam = 'sort.bam')
		mock_pipeline_params.add(sample_dir_prefix = 'Sample_')
		p.builder_params = mock_pipeline_params


		# mock the missing config file: 
		mock_os.path.isdir.return_value = True

		# mock there being only R1 fastq files, and have one of the entries return >1 in the list:
		glob_return = [['A_R1_001.fastq.gz'],[],['B_R1_001.fastq.gz', 'B_AT_R1_001.fastq.gz'],[], ['C_R1_001.fastq.gz'],[]]
		mock_glob.glob.side_effect = glob_return

		with self.assertRaises(MultipleFileFoundException):
			p._PipelineBuilder__check_and_create_samples()

		# mock there being both R1 and R2 fastq files (And we want paired alignment), and have one of the R2 entries return >1 in the list:
		p.builder_params.reset_param('paired_alignment', True)
		glob_return = [['A_R1_001.fastq.gz'],['A_R2_001.fastq.gz'],['B_R1_001.fastq.gz'],['B_R2_001.fastq.gz'], ['C_R1_001.fastq.gz'],['C_R2_001.fastq.gz', 'C_AT_R2_001.fastq.gz']]
		mock_glob.glob.side_effect = glob_return

		with self.assertRaises(MultipleFileFoundException):
			p._PipelineBuilder__check_and_create_samples()