Skip to content

mecfs_bio.build_system.task.gene_tissue_expression_clustermap_task

Task to make a heatmap plot with genes as rows and tissue/cell types as columns

Classes:

Attributes:

logger module-attribute

logger = get_logger()

ExpressionMatrixClusterMapTaskV2

Bases: Task

Task to make a heatmap plot with genes as rows and tissue/cell types as columns Goal is to show the specificity of genes for tissue/cell types

Methods:

Attributes:

deps property

deps: list[Task]

gene_info_sources instance-attribute

gene_info_sources: Sequence[GeneInfoSource]

meta property

meta: Meta

plot_spec instance-attribute

plot_spec: XRHeatmapPlotSpec

specificity_matrix_source instance-attribute

specificity_matrix_source: SpecificityMatrixSource

tissue_info_sources instance-attribute

tissue_info_sources: Sequence[TissueInfoSource]

xr_pipe instance-attribute

xr_pipe: XRDataPipe

create_standard_gene_magma_heatmap classmethod

create_standard_gene_magma_heatmap(
    asset_id: str,
    extracted_magma_gene_results_source: GeneInfoSource,
    gene_specificity_matrix_source: SpecificityMatrixSource,
    gene_thesaurus_source: GeneInfoSource,
    num_genes_to_keep: int = 300,
)

Make a heatmap in which genes are selected according to their MAGMA significance. Useful for delving deeper into MAGMA results .

Source code in mecfs_bio/build_system/task/gene_tissue_expression_clustermap_task.py
@classmethod
def create_standard_gene_magma_heatmap(
    cls,
    asset_id: str,
    extracted_magma_gene_results_source: GeneInfoSource,
    gene_specificity_matrix_source: SpecificityMatrixSource,
    gene_thesaurus_source: GeneInfoSource,
    num_genes_to_keep: int = 300,
):
    """
    Make a heatmap in which genes are selected according to their MAGMA significance.
    Useful for delving deeper into MAGMA results .
    """
    source_meta = extracted_magma_gene_results_source.task.meta
    assert isinstance(source_meta, ResultTableMeta)
    meta = GWASPlotDirectoryMeta(
        id=AssetId(asset_id),
        trait=source_meta.trait,
        project=source_meta.project,
    )
    seaborn_palette = sns.color_palette("vlag", n_colors=100)
    plotly_colorscale = [
        f"rgb({int(c[0] * 255)}, {int(c[1] * 255)}, {int(c[2] * 255)})"
        for c in seaborn_palette
    ]
    return cls(
        meta=meta,
        specificity_matrix_source=gene_specificity_matrix_source,
        gene_info_sources=[
            extracted_magma_gene_results_source,
            gene_thesaurus_source,
        ],
        tissue_info_sources=[],
        xr_pipe=XRCompositePipe(
            [
                XRCluster(
                    array_name=XR_SPECIFICITY_MATRIX,
                    dim=XR_TISSUE_DIMENSION,
                ),
                XRSignificanceFilter(
                    p_threshold=0.01,
                    p_da=MAGMA_P_COLUMN,
                    z_da=MAGMA_Z_COLUMN,
                ),
                XRMostSignificant(
                    ordering_da=MAGMA_P_COLUMN,
                    num_to_keep=num_genes_to_keep,
                ),
                XRCluster(
                    array_name=XR_SPECIFICITY_MATRIX,
                    dim=XR_GENE_DIMENSION,
                ),
            ]
        ),
        plot_spec=XRHeatmapPlotSpec(
            GeneNormalizedPlotMode(max_multiple=5),
            y_array="Gene name",
            color_scale=plotly_colorscale,
        ),
    )

execute

execute(scratch_dir: Path, fetch: Fetch, wf: WF) -> Asset
Source code in mecfs_bio/build_system/task/gene_tissue_expression_clustermap_task.py
def execute(self, scratch_dir: Path, fetch: Fetch, wf: WF) -> Asset:
    ds = load_xr_gene_tissue_dataset(
        gt_specificity_matrix_source=self.specificity_matrix_source,
        gene_info_sources=self.gene_info_sources,
        tissue_info_sources=self.tissue_info_sources,
        fetch=fetch,
    )
    ds = self.xr_pipe.process(ds)
    fig = xr_make_gene_spec_heatmap(
        ds=ds,
        plot_spec=self.plot_spec,
    )
    write_plots_to_dir(scratch_dir, {"gene_tissue_heatmap": fig})
    return DirectoryAsset(scratch_dir)