Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #62 from schillerlab/fix/marker_dendrogram
Browse files Browse the repository at this point in the history
refactor marker_dendrogram
  • Loading branch information
Zethson authored May 27, 2021
2 parents 70c959d + cc1dbfd commit 1de9e9b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion sc_toolbox/api/calc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def correlate_means_to_gene(means: pd.DataFrame, corr_gene: str = "EOMES"):
return cors


def generate_extended_marker_table(
def extended_marker_table(
adata: AnnData,
qval_thresh: float = 0.05,
cell_type_label: str = "cell_type",
Expand Down
33 changes: 17 additions & 16 deletions sc_toolbox/api/plot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,23 +623,25 @@ def split_boxplot(


def marker_dendrogram(
marker,
marker_table: pd.DataFrame,
threshold: float = 0.7,
column: str = "cluster",
log_fc_key: str = "log_FC",
label_size: int = 10,
orient: str = "top",
orientation: str = "top",
figsize: Tuple[int, int] = (10, 4),
save: str = None,
):
"""
Plots a dendogram of used marker genes.
Args:
marker:
threshold:
column:
label_size:
orient:
marker_table: A marker table as generated by sct.calc.extended_marker_table
threshold: Threshold for the log fold change
column: Column to create pivot by; usually just the clusters
log_fc_key: Key for the stored log fold changes in the marker table
label_size: Font size of the labels
orientation: Orientation of the figure; Currently just 'top' or no orientation
figsize: Size of the figure as specified in matplotlib
save: Path to save the plot to
Expand All @@ -648,23 +650,22 @@ def marker_dendrogram(
"""
import scipy.cluster.hierarchy as hc

log = "avg_logFC" if "avg_logFC" in marker.columns else "logfoldchange"
marker = marker[marker[log] > threshold]
marker_table = marker_table[marker_table[log_fc_key] > threshold]

marker = marker.pivot(index="gene", columns=column, values=log)
marker.fillna(value=0, inplace=True)
marker_table = marker_table.pivot(index="gene", columns=column, values=log_fc_key)
marker_table.fillna(value=0, inplace=True)

corr = 1 - marker.corr()
corr = 1 - marker_table.corr()
corr = hc.distance.squareform(corr) # convert to condensed
z = hc.linkage(corr, method="complete")
plt.figure(figsize=figsize)
rot = 90 if orient == "top" else 0
rot = 90 if orientation == "top" else 0
hc.dendrogram(
z,
labels=marker.columns,
labels=marker_table.columns,
leaf_rotation=rot,
color_threshold=0,
orientation=orient,
orientation=orientation,
leaf_font_size=label_size,
above_threshold_color="black",
)
Expand All @@ -673,7 +674,7 @@ def marker_dendrogram(
plt.show()
else:
plt.savefig("{save}")
print(f"Saving figure to {save}")
print(f"[bold blue]Saving figure to {save}")
plt.close()


Expand Down

0 comments on commit 1de9e9b

Please sign in to comment.