Example #1
0
def test_from_names(my_genes):
    genome = ExpGenome.from_gene_names(my_genes)
    assert len(genome) == len(my_genes)
    for i, (g, eg) in enumerate(zip(my_genes, genome)):
        assert eg.name == g
        assert eg.chromosomes == []
        assert eg.ensembl_ids == []
Example #2
0
def my_genome():
    # we're creating a fake genome consisting of all lowercase ascii characters
    genes = [text(c) for c in ascii_lowercase]
    genome = ExpGenome.from_gene_names(genes)
    return genome
Example #3
0
    def run(self):
        """Perform GO-PCA.

        Parameters
        ----------

        Returns
        -------
        `GOPCARun` or None
            The GO-PCA run, or ``None`` if the run failed.
        """
        t0 = time.time()  # remember the start time
        timestamp = str(datetime.datetime.utcnow())  # timestamp for the run

        ### Phase 1: Make sure all configurations are valid
        all_configs_valid = True
        for config in self.configs:
            if not config.user_params.check_params():
                # problems with the configuration
                all_configs_valid = False
            config.finalize_params(self.matrix.p)
            if not config.params.check_params():
                all_configs_valid = False

        if not all_configs_valid:
            logger.error('Invalid configuration settings. '
                         'Aborting GO-PCA run.')
            return None

        # print some information
        p, n = self.matrix.shape
        logger.info('Timestamp: %s', timestamp)
        logger.info(
            'Size of expression matrix: ' + 'p=%d genes x n=%d samples.', p, n)

        # Report hash values for expression matrix and configurations
        expression_hash = self.matrix.hash
        logger.info('Expression matrix hash: %s', expression_hash)
        config_hashes = []
        for i, config in enumerate(self.configs):
            config_hashes.append(config.hash)
            logger.info('Configuration #%d hash: %s', i + 1, config_hashes[-1])

        ### Phase 2: Determine the number of principal components
        num_components = self.num_components
        if num_components == 0:
            # estimate the number of non-trivial PCs using a permutation test
            num_components = self.estimate_num_components()
            if num_components == 0:
                logger.error('The estimated number of non-trivial '
                             'principal components is 0. '
                             'Aborting GO-PCA run.')
                return None
            if 0 < self.pc_max_components < num_components:
                num_components = self.pc_max_components
                logger.info('Limiting the number of PCs to test to %d.',
                            num_components)

        else:
            # determine the total number of principal components
            # (i.e., the number of dimensions spanned by the data)
            max_components = min(self.matrix.p, self.matrix.n - 1)
            if self.num_components > max_components:
                logger.error(
                    'The number of PCs to test was specified as '
                    '%d, but the data spans only %d dimensions. '
                    'Aborting GO-PCA run.', num_components, max_components)
                return None

        if num_components == 0:
            logger.error('No principal components to test.'
                         'Aborting GO-PCA run.')
            return None

        ### Phase 3: Perform PCA
        logger.info('Performing PCA...')
        pca = PCA(n_components=num_components)
        Y = pca.fit_transform(self.matrix.X.T)

        # output fraction of variance explained for the PCs tested
        frac = pca.explained_variance_ratio_
        cum_frac = np.cumsum(frac)
        logger.info(
            'Fraction of total variance explained by the first '
            '%d PCs: %.1f%%', num_components, 100 * cum_frac[-1])

        ### Phase 4: Run GO-PCA for each configuration supplied
        enr_logger = logging.getLogger(enrichment.__name__)

        genome = ExpGenome.from_gene_names(self.matrix.genes.tolist())
        W = pca.components_.T  # the loadings matrix

        msg = logger.debug
        if self.verbose:
            # enable more verbose "INFO" messages
            msg = logger.info

        all_signatures = []
        for k, config in enumerate(self.configs):

            logger.info(
                'Generating GO-PCA signatures for configuration '
                '%d...', k + 1)

            # create GeneSetEnrichmentAnalysis object
            enr_logger.setLevel(logging.ERROR)
            gse_analysis = GeneSetEnrichmentAnalysis(genome, config.gene_sets)
            enr_logger.setLevel(logging.NOTSET)

            # generate signatures
            final_signatures = []
            var_expl = 0.0
            for d in range(num_components):
                var_expl += frac[d]
                msg('')
                msg('-' * 70)
                msg('PC %d explains %.1f%% of the variance.', d + 1,
                    100 * frac[d])
                msg(
                    'The new cumulative fraction of variance explained '
                    'is %.1f%%.', 100 * var_expl)

                signatures_dsc = self._generate_pc_signatures(
                    self.matrix, config.params, gse_analysis, W, d + 1)
                signatures_asc = self._generate_pc_signatures(
                    self.matrix, config.params, gse_analysis, W, -(d + 1))
                signatures = signatures_dsc + signatures_asc
                msg('# signatures: %d', len(signatures))

                # apply global filter (if enabled)
                if not config.params.no_global_filter:
                    before = len(signatures)
                    signatures = self._global_filter(config.params, signatures,
                                                     final_signatures,
                                                     config.gene_ontology)
                    msg('Global filter: kept %d / %d signatures.',
                        len(signatures), before)

                # self.print_signatures(signatures, debug=True)
                final_signatures.extend(signatures)
                msg('Total no. of signatures generated so far: %d',
                    len(final_signatures))

            logger.info('')
            logger.info('=' * 70)
            logger.info(
                'GO-PCA for configuration #%d generated %d '
                'signatures.', k + 1, len(final_signatures))
            logger.info('-' * 70)
            self.print_signatures(final_signatures)
            logger.info('=' * 70)
            logger.info('')
            all_signatures.extend(final_signatures)

        ### Phase 5: Generate signature matrix and return a `GOPCARun` instance
        sig_matrix = GOPCASignatureMatrix.from_signatures(all_signatures)
        t1 = time.time()
        exec_time = t1 - t0
        logger.info('This GO-PCA run took %.2f s.', exec_time)
        gopca_run = GOPCARun(sig_matrix, gopca.__version__, timestamp,
                             exec_time, expression_hash, config_hashes,
                             self.matrix.genes, self.matrix.samples, W, Y)

        return gopca_run
Example #4
0
    def run(self):
        """Perform GO-PCA.

        Parameters
        ----------

        Returns
        -------
        `GOPCARun` or None
            The GO-PCA run, or ``None`` if the run failed.
        """
        t0 = time.time()  # remember the start time
        timestamp = str(datetime.datetime.utcnow())  # timestamp for the run


        ### Phase 1: Make sure all configurations are valid
        all_configs_valid = True
        for config in self.configs:
            if not config.user_params.check_params():
                # problems with the configuration
                all_configs_valid = False
            config.finalize_params(self.matrix.p)
            if not config.params.check_params():
                all_configs_valid = False

        if not all_configs_valid:
            logger.error('Invalid configuration settings. '
                         'Aborting GO-PCA run.')
            return None

        # print some information
        p, n = self.matrix.shape
        logger.info('Timestamp: %s', timestamp)
        logger.info('Size of expression matrix: ' +
                    'p=%d genes x n=%d samples.', p, n)

        # Report hash values for expression matrix and configurations
        expression_hash = self.matrix.hash
        logger.info('Expression matrix hash: %s', expression_hash)
        config_hashes = []
        for i, config in enumerate(self.configs):
            config_hashes.append(config.hash)
            logger.info('Configuration #%d hash: %s', i+1, config_hashes[-1])


        ### Phase 2: Determine the number of principal components
        num_components = self.num_components
        if num_components == 0:
            # estimate the number of non-trivial PCs using a permutation test
            num_components = self.estimate_num_components()
            if num_components == 0:
                logger.error('The estimated number of non-trivial '
                             'principal components is 0. '
                             'Aborting GO-PCA run.')
                return None
            if 0 < self.pc_max_components < num_components:
                num_components = self.pc_max_components
                logger.info('Limiting the number of PCs to test to %d.', num_components)


        else:
            # determine the total number of principal components
            # (i.e., the number of dimensions spanned by the data)
            max_components = min(self.matrix.p, self.matrix.n - 1)
            if self.num_components > max_components:
                logger.error('The number of PCs to test was specified as '
                             '%d, but the data spans only %d dimensions. '
                             'Aborting GO-PCA run.',
                             num_components, max_components)
                return None

        if num_components == 0:
            logger.error('No principal components to test.'
                         'Aborting GO-PCA run.')
            return None


        ### Phase 3: Perform PCA
        logger.info('Performing PCA...')
        pca = PCA(n_components=num_components)
        Y = pca.fit_transform(self.matrix.X.T)

        # output fraction of variance explained for the PCs tested
        frac = pca.explained_variance_ratio_
        cum_frac = np.cumsum(frac)
        logger.info('Fraction of total variance explained by the first '
                    '%d PCs: %.1f%%', num_components, 100 * cum_frac[-1])


        ### Phase 4: Run GO-PCA for each configuration supplied
        enr_logger = logging.getLogger(enrichment.__name__)

        genome = ExpGenome.from_gene_names(self.matrix.genes.tolist())
        W = pca.components_.T  # the loadings matrix

        msg = logger.debug
        if self.verbose:
            # enable more verbose "INFO" messages
            msg = logger.info

        all_signatures = []
        for k, config in enumerate(self.configs):

            logger.info('Generating GO-PCA signatures for configuration '
                        '%d...', k+1)

            # create GeneSetEnrichmentAnalysis object
            enr_logger.setLevel(logging.ERROR)
            gse_analysis = GeneSetEnrichmentAnalysis(genome, config.gene_sets)
            enr_logger.setLevel(logging.NOTSET)

            # generate signatures
            final_signatures = []
            var_expl = 0.0
            for d in range(num_components):
                var_expl += frac[d]
                msg('')
                msg('-'*70)
                msg('PC %d explains %.1f%% of the variance.',
                    d+1, 100*frac[d])
                msg('The new cumulative fraction of variance explained '
                    'is %.1f%%.', 100*var_expl)

                signatures_dsc = self._generate_pc_signatures(
                    self.matrix, config.params, gse_analysis, W, d+1)
                signatures_asc = self._generate_pc_signatures(
                    self.matrix, config.params, gse_analysis, W, -(d+1))
                signatures = signatures_dsc + signatures_asc
                msg('# signatures: %d', len(signatures))

                # apply global filter (if enabled)
                if not config.params.no_global_filter:
                    before = len(signatures)
                    signatures = self._global_filter(
                        config.params, signatures, final_signatures,
                        config.gene_ontology)
                    msg('Global filter: kept %d / %d signatures.',
                        len(signatures), before)

                # self.print_signatures(signatures, debug=True)
                final_signatures.extend(signatures)
                msg('Total no. of signatures generated so far: %d',
                    len(final_signatures))

            logger.info('')
            logger.info('='*70)
            logger.info('GO-PCA for configuration #%d generated %d '
                        'signatures.', k+1, len(final_signatures))
            logger.info('-'*70)
            self.print_signatures(final_signatures)
            logger.info('='*70)
            logger.info('')
            all_signatures.extend(final_signatures)


        ### Phase 5: Generate signature matrix and return a `GOPCARun` instance
        sig_matrix = GOPCASignatureMatrix.from_signatures(all_signatures)
        t1 = time.time()
        exec_time = t1 - t0
        logger.info('This GO-PCA run took %.2f s.', exec_time)
        gopca_run = GOPCARun(sig_matrix,
                             gopca.__version__, timestamp, exec_time,
                             expression_hash, config_hashes,
                             self.matrix.genes, self.matrix.samples, W, Y)

        return gopca_run