Skip to content

Commit 70bbb17

Browse files
author
bvenn
committed
add value dependent cell coloring
add table sequence representation
1 parent 0e6adcd commit 70bbb17

File tree

2 files changed

+112
-5
lines changed

2 files changed

+112
-5
lines changed

docsrc/content/table.fsx

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This example shows how to create tables in F#.
1313
open FSharp.Plotly
1414
open FSharp.Plotly.StyleParam
1515

16-
let header = ["RowIndex";"A";"simple";"table"]
16+
let header = ["<b>RowIndex</b>";"A";"simple";"table"]
1717
let rows =
1818
[
1919
["0";"I";"am";"a"]
@@ -40,16 +40,19 @@ let table2 =
4040
rows,
4141
//sets global header alignment
4242
AlignHeader= [HorizontalAlign.Center],
43-
//sets alignment for each column separately (The last alignment is applied to all potential following columns)
43+
//sets alignment for each column separately
44+
//(The last alignment is applied to all potential following columns)
4445
AlignCells= [HorizontalAlign.Left;HorizontalAlign.Center;HorizontalAlign.Right],
4546
//sets global header color
4647
ColorHeader="#45546a",
47-
//sets single header color to each header column
48+
//sets specific header color to each header column
4849
//ColorHeader=["#45546a";"#deebf7";"#45546a";"#deebf7"],
4950
//sets global cell color
5051
//ColorRows="#deebf7",
51-
//sets single header color to each header column
52+
//sets cell column colors
5253
ColorCells=["#deebf7";"lightgrey";"#deebf7";"lightgrey"],
54+
//sets cell row colors
55+
//ColorCells=[["#deebf7";"lightgrey"]],
5356
//sets font of header
5457
FontHeader=Font.init(FontFamily.Courier_New, Size=12, Color="white"),
5558
//sets the height of the header
@@ -67,10 +70,114 @@ table2 |> Chart.Show
6770
(*** include-value:table2 ***)
6871

6972
(**
70-
FastA Representation
73+
Value dependent cell coloring:
74+
*)
75+
76+
let header2 = ["Identifier";"T0";"T1";"T2";"T3"]
77+
let rowvalues =
78+
[
79+
[10001.;0.2;2.0;4.0;5.0]
80+
[10002.;2.1;2.0;1.8;2.1]
81+
[10003.;4.5;3.0;2.0;2.5]
82+
[10004.;0.0;0.1;0.3;0.2]
83+
[10005.;1.0;1.6;1.8;2.2]
84+
[10006.;1.0;0.8;1.5;0.7]
85+
[10007.;2.0;2.0;2.1;1.9]
86+
]
87+
|> Seq.sortBy (fun x -> x.[1])
88+
89+
//map color from value to hex representation
90+
let mapColor min max value =
91+
let percentage = (value - min) / (max - min)
92+
Colors.fromRgb 255 (255 - (int (255. * percentage))) (int (255. * percentage))
93+
|> Colors.toWebColor
94+
95+
//assign a color to every cell seperately. Matrix must be transposed for correct orientation
96+
let cellcolor =
97+
rowvalues
98+
|> Seq.map (fun row ->
99+
row
100+
|> Seq.mapi (fun index value ->
101+
if index = 0 then "white"
102+
else mapColor 0. 5. value
103+
)
104+
)
105+
|> Seq.transpose
106+
107+
let table3 = Chart.Table(header2,rowvalues,ColorCells=cellcolor)
108+
109+
(***do-not-eval***)
110+
table3 |> Chart.Show
111+
112+
113+
(*** include-value:table3 ***)
114+
115+
116+
(**
117+
Sequence representation
71118
72119
*)
73120

121+
let sequence =
122+
[
123+
"ATGAGACGTCGAGACTGATAGACGTCGATAGACGTCGATAGACCG"
124+
"ATAGACTCGTGATAGACGTCGATAGACGTCGATAGAGTATAGACC"
125+
"GTGATAGACGTCGAGAAGACGTCGATAGACGTCGATAGACGTCGA"
126+
"TAGAGATAGACGTCGATAGACCGTATAGAAGACGTCGATAGATAG"
127+
"ACGTCGATAGACCGTAGACGTCGATAGACGTCGATAGACCGT"
128+
]
129+
|> String.concat ""
130+
131+
let elementsperrow = 60
132+
133+
let headers =
134+
[0..elementsperrow]
135+
|> Seq.map (fun x ->
136+
if x%10=0 && x <> 0 then "|"
137+
else ""
138+
)
139+
140+
let cells =
141+
sequence
142+
|> Seq.chunkBySize elementsperrow
143+
|> Seq.mapi (fun i x -> Seq.append [string (i * elementsperrow)] (Seq.map string x))
144+
145+
let cellcolors =
146+
cells
147+
|> Seq.map (fun row ->
148+
row
149+
|> Seq.map (fun nucleotid ->
150+
match nucleotid with
151+
//colors taken from DRuMS
152+
//(http://biomodel.uah.es/en/model4/dna/atgc.htm)
153+
| "A" -> "#5050FF"
154+
| "T" -> "#E6E600"
155+
| "G" -> "#00C000"
156+
| "C" -> "#E00000"
157+
| "U" -> "#B48100"
158+
| _ -> "white"
159+
)
160+
)
161+
|> Seq.transpose
162+
|> Seq.map (fun x -> Seq.append x (seq ["white"]))
163+
164+
let font = Font.init(FontFamily.Consolas,Size=14)
165+
let line = Line.init(0,"white")
166+
let chartwidth = 50. + 10. * float elementsperrow
167+
168+
let table4 =
169+
Chart.Table(
170+
headers,cells,LineCells=line,LineHeader=line,HeightCells=20,
171+
FontHeader=font,FontCells=font,ColumnWidth=[50;10],
172+
AlignCells=[HorizontalAlign.Right;HorizontalAlign.Center],ColorCells=cellcolors)
173+
|> Chart.withSize(chartwidth,nan)
174+
|> Chart.withTitle "Sequence A"
175+
176+
177+
(***do-not-eval***)
178+
table4 |> Chart.Show
179+
74180

181+
(*** include-value:table4 ***)
75182

76183

lib/Formatting/FSharp.Plotly.dll

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)