Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
package org.apache.texera.amber.operator.visualization.volcanoPlot

import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.kjetland.jackson.jsonSchema.annotations.{JsonSchemaInject, JsonSchemaTitle}
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
import org.apache.texera.amber.pybuilder.PyStringTypes.{EncodableString, PythonLiteral}
import org.apache.texera.amber.operator.visualization.PlotlyStandaloneCode
import org.apache.texera.amber.operator.visualization.PythonVisualizationOpDesc
import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn}
Expand Down Expand Up @@ -76,6 +77,34 @@ class VolcanoPlotOpDesc extends PythonVisualizationOpDesc with PlotlyStandaloneC
@AutofillAttributeName
var hoverName: EncodableString = ""

// Numeric: both are compared against the data and the first is log10'd. contentAs
// names the boxed class — Option erases its element type, and a blank must not read as 0.
@JsonProperty(required = false)
@JsonSchemaTitle("Significance threshold")
@JsonPropertyDescription(
"P-value cut-off such as 0.05; draws a dashed horizontal line at -log10 of it"
)
@JsonSchemaInject(json = """{"exclusiveMinimum": 0, "maximum": 1}""")
@JsonDeserialize(contentAs = classOf[java.lang.Double])
var significanceThreshold: Option[Double] = None

@JsonProperty(required = false)
@JsonSchemaTitle("Effect-size threshold")
@JsonPropertyDescription(
"Effect-size cut-off such as 1; draws dashed vertical lines at plus and minus it"
)
@JsonSchemaInject(json = """{"exclusiveMinimum": 0}""")
@JsonDeserialize(contentAs = classOf[java.lang.Double])
var effectSizeThreshold: Option[Double] = None

@JsonProperty(required = false, defaultValue = "false")
@JsonSchemaTitle("Color by significance")
@JsonPropertyDescription(
"Color points Up, Down or Not significant by both thresholds instead of by effect size; " +
"needs both thresholds, otherwise the effect-size coloring is kept"
)
var colorBySignificance: Boolean = false

@JsonProperty(required = false)
@JsonSchemaTitle("Title")
@JsonPropertyDescription("The figure title; leave empty for Volcano Plot")
Expand Down Expand Up @@ -133,6 +162,106 @@ class VolcanoPlotOpDesc extends PythonVisualizationOpDesc with PlotlyStandaloneC
|""".stripMargin
else ""

/** A cut-off is used only when it can be drawn: finite, above zero and, for the
* p-value, at most one. Anything else counts as unset. The schema bounds keep such
* values out of the property panel; this keeps a hand-edited workflow from failing.
*/
private def isUsable(cutoff: Double): Boolean =
!cutoff.isNaN && !cutoff.isInfinite && cutoff > 0

private def significanceCutoff: Option[Double] =
significanceThreshold.filter(sig => isUsable(sig) && sig <= 1)

private def effectCutoff: Option[Double] = effectSizeThreshold.filter(isUsable)

/** Both cut-offs are needed to call a point Up or Down. */
private def colorByCategory: Boolean =
colorBySignificance && significanceCutoff.isDefined && effectCutoff.isDefined

/** Strict comparisons: a point exactly on a cut-off is not significant. The categories
* go to a new column, Significance with underscores appended until no column has that
* name, so no input column is overwritten. px reads it by name: an array passed as
* `color` is named "color" by px and clashes with a column of that name that x, y or
* hover_name uses.
*/
private def significanceCategories: PythonTemplateBuilder =
if (colorByCategory) {
val sig = significanceCutoff.get
val eff = effectCutoff.get
pyb"""significant = table[$pvalueColumn] < $sig
| significance_column = "Significance"
| while significance_column in table.columns:
| significance_column += "_"
| table[significance_column] = np.select(
| [significant & (table[$effectColumn] > $eff), significant & (table[$effectColumn] < -$eff)],
| ["Up", "Down"],
| default="Not significant",
| )
|"""
} else pyb""

// ColorBrewer RdBu end points plus a neutral grey, so the figure reads like other
// volcano-plot tooling; the continuous RdBu scale stays when not coloring by category.
// Shared by both code paths so the exported figure cannot drift from the engine's.
// The legend and hover call the category column Significance, except when the effect
// column has that name: px would merge the two hover lines that share the label.
private def categoryColorArgs: PythonLiteral = {
val label =
if (effectColumn == "Significance") Seq.empty
else Seq("""labels={significance_column: "Significance"},""")
(Seq("color=significance_column,") ++ label ++ Seq(
"""color_discrete_map={"Up": "#B2182B", "Down": "#2166AC", "Not significant": "#9E9E9E"},""",
"""category_orders={significance_column: ["Up", "Down", "Not significant"]},"""
)).mkString("\n ")
}

private def colorArgs: PythonTemplateBuilder =
if (colorByCategory) pyb"$categoryColorArgs"
else
pyb"""color=$effectColumn,
| color_continuous_scale="RdBu","""

// The exported script's twins of the two helpers above: the same Python with the
// column names as literals rather than decoded, and the very same threshold literals.
private def standaloneSignificanceCategories: String =
if (colorByCategory) {
val sig = significanceCutoff.get
val eff = effectCutoff.get
val pvalueLit = pyStringLiteral(pvalueColumn)
val effectLit = pyStringLiteral(effectColumn)
s"""significant = table[$pvalueLit] < $sig
| significance_column = "Significance"
| while significance_column in table.columns:
| significance_column += "_"
| table[significance_column] = np.select(
| [significant & (table[$effectLit] > $eff), significant & (table[$effectLit] < -$eff)],
| ["Up", "Down"],
| default="Not significant",
| )
|""".stripMargin
} else ""

private def standaloneColorArgs: String =
if (colorByCategory) categoryColorArgs
else
s"""color=${pyStringLiteral(effectColumn)},
| color_continuous_scale="RdBu",""".stripMargin

/** One dashed line per cut-off that is set: horizontal at -log10 of the p-value
* cut-off, vertical at plus and minus the effect-size cut-off.
*/
private def thresholdLines: PythonLiteral = {
val lines = scala.collection.mutable.ArrayBuffer[PythonTemplateBuilder]()
significanceCutoff.foreach { sig =>
lines += pyb"""fig.add_hline(y=-np.log10($sig), line_dash="dash", line_color="gray")"""
}
effectCutoff.foreach { eff =>
lines += pyb"""fig.add_vline(x=$eff, line_dash="dash", line_color="gray")"""
lines += pyb"""fig.add_vline(x=-$eff, line_dash="dash", line_color="gray")"""
}
lines.mkString("\n ")
}

/** The y column is named after the selected p-value column, so a figure drawn
* from an adjusted column says so instead of always reading "-log10(pvalue)".
*/
Expand Down Expand Up @@ -172,21 +301,21 @@ class VolcanoPlotOpDesc extends PythonVisualizationOpDesc with PlotlyStandaloneC
| return
|
| table[$yColumn] = -np.log10(table[$pvalueColumn])
|
| $significanceCategories
| fig = px.scatter(
| table,
| x=$effectColumn,
| y=$yColumn,
| hover_name=$hoverColumn,
| color=$effectColumn,
| color_continuous_scale="RdBu",
| $colorArgs
| title=$titleText
| )
| fig.update_layout(
| xaxis_title=$xAxisTitle,
| yaxis_title=$yAxisTitle,
| coloraxis_colorbar_title_text=$xAxisTitle
| )
| $thresholdLines
|
| html = plotly.io.to_html(fig, include_plotlyjs='cdn', auto_play=False)
| yield {"html-content": html}
Expand Down Expand Up @@ -217,20 +346,21 @@ class VolcanoPlotOpDesc extends PythonVisualizationOpDesc with PlotlyStandaloneC
| output.write(render_error("No rows with valid p-values."))
| else:
| table[$yLit] = -np.log10(table[$pvalueLit])
| $standaloneSignificanceCategories
| fig = px.scatter(
| table,
| x=$effectLit,
| y=$yLit,
| hover_name=$standaloneHoverColumn,
| color=$effectLit,
| color_continuous_scale="RdBu",
| $standaloneColorArgs
| title=${pyStringLiteral(titleText)}
| )
| fig.update_layout(
| xaxis_title=${pyStringLiteral(xAxisTitle)},
| yaxis_title=${pyStringLiteral(yAxisTitle)},
| coloraxis_colorbar_title_text=${pyStringLiteral(xAxisTitle)}
| )
| $thresholdLines
| fig.write_json(outputJson)
| fig.write_html(outputHtml)
| print("Volcano plot saved to " + outputHtml)""".stripMargin
Expand Down
Loading
Loading