///|
/// velociraptor module demo - Single-cell RNA velocity analysis.
///
/// Demonstrates steady-state and kinetic (EM) models for estimating
/// transcription/splicing/degradation rates, velocity computation,
/// and embedding projection on simulated single-cell data.
///
/// Reference: Bergen V et al. (2020) scVelo. Nat Biotechnol 38:1408.
/// Run: moon run examples/velociraptor_demo
fn main {
  println("=== velociraptor Module Demo ===")
  println("Single-cell RNA velocity analysis")
  println("Reference: Bioconductor velociraptor / scVelo")
  println("")

  // ------------------------------------------------------------------
  // 1. Load sample data
  // ------------------------------------------------------------------
  println("=== 1. Load Sample Data ===")
  let gene_data = @bio.velocity_sample_data()
  println("Genes: " + gene_data.length().to_string())
  println("Cells: " + gene_data[0].spliced().length().to_string())
  for gd in gene_data {
    let mean_s = gd.spliced()[0]
    let mean_u = gd.unspliced()[0]
    println(
      "  " +
      gd.gene_name() +
      ": spliced[0]=" +
      mean_s.to_string() +
      " unspliced[0]=" +
      mean_u.to_string(),
    )
  }
  println("")

  // ------------------------------------------------------------------
  // 2. Steady-state model
  // ------------------------------------------------------------------
  println("=== 2. Steady-State Model ===")
  for gd in gene_data {
    let (ratio, ss_s, ss_u) = @bio.velocity_steady_state(
      gd.spliced(),
      gd.unspliced(),
    )
    println(
      "  " +
      gd.gene_name() +
      ": gamma/beta=" +
      ratio.to_string() +
      " ss_s=" +
      ss_s.to_string() +
      " ss_u=" +
      ss_u.to_string(),
    )
  }
  println("")

  // ------------------------------------------------------------------
  // 3. Kinetic model (EM)
  // ------------------------------------------------------------------
  println("=== 3. Kinetic Model (EM) ===")
  let kinetics : Array[@bio.GeneKinetics] = Array(capacity=0)
  for gd in gene_data {
    let k = @bio.velocity_kinetic_model(
      gd.gene_name(),
      gd.spliced(),
      gd.unspliced(),
      50,
    )
    kinetics.push(k)
    println(
      "  " +
      k.gene_name() +
      ": alpha=" +
      k.alpha().to_string() +
      " beta=" +
      k.beta().to_string() +
      " gamma=" +
      k.gamma().to_string() +
      " likelihood=" +
      k.likelihood().to_string(),
    )
  }
  println("")

  // ------------------------------------------------------------------
  // 4. Compute velocity vectors
  // ------------------------------------------------------------------
  println("=== 4. Velocity Vectors ===")
  let velocities = @bio.velocity_compute(gene_data, kinetics)
  println("Cells with velocity: " + velocities.length().to_string())
  let mut max_speed = 0.0
  let mut max_speed_cell = 0
  for i in 0.. max_speed {
      max_speed = velocities[i].speed()
      max_speed_cell = i
    }
  }
  println(
    "Max speed cell: " +
    max_speed_cell.to_string() +
    " (speed=" +
    max_speed.to_string() +
    ")",
  )
  // Show velocity for first 5 cells.
  for i in 0..<5 {
    let v = velocities[i]
    let mut vel_str = "["
    for j in 0.. 0.0 {
      row_str = row_str +
        "cell" +
        j.to_string() +
        "(" +
        matrix[0][j].to_string() +
        ") "
    }
  }
  println(row_str)
  println("")

  println("=== Demo complete ===")
}