-- | Small module for converting Vienna energy tables to partition function
-- value tables.
--
-- Temperature scaling should happen on the level of energy tables, followed by
-- conversion here.
--
-- TODO after the switch to the Energy type, change too, the partitionTables
-- generation

module Biobase.Vienna.Modification.Partition
  ( partitionTables
  ) where

import Biobase.Types.Partition
import Biobase.Types.Energy
import Biobase.Turner.Tables
import Biobase.Vienna
import Biobase.Constants


-- Testing

{-
import Biobase.Vienna.Default
import Data.PrimitiveArray
import Debug.Trace.Tools
import Biobase.RNA
import Biobase.Types.Ring

(g,h) = turner2004GH

-- x = toList $ stack $ partitionTables 37 g
x = {- toList $ -} (stack $ partitionTables 37 g) ! (vpGC,vpGC)
-}

-- | Create partition tables out of normal energy tables. Note that these
-- values are not normalized. An additional term 1/Z(1/kt) would be required
-- which is missing until we are done calculating the partition function.
--
-- All energy contributions are rescaled from dekacals back to kcal/mol.
--
-- We explicitly set all probabilities =0 where the energy is >= eMax.

partitionTables :: Temperature -> ViennaEnergyTables -> ViennaPartitionTables
partitionTables tempe trnr = dmap f trnr where
  kt = (tempe + kelvinC0) * gasconst
  -- convert energy into probability; encapsulate as a (logFloat) partition
  f (Energy e') = {- traceVal (show (e',e,p)) -} p where
    e = fromIntegral e' / 100
    p = if e' >= eMax then Partition 0 else Partition . exp $ -e / kt -- can NOT use mkPartition as these values are NOT NORMALIZED