-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
620 lines (498 loc) · 18.3 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
port module Main exposing (..)
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Regex
import Json.Decode as Json
--import MashTree
import Taxonomy.Rank as Rank exposing (..)
import Cluster as Clu exposing (..)
main : Program Never
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ distanceClusters : Maybe Clu.ClustersByRank
, taxonomicClusters : Maybe Clu.ClustersByRank
, displayedClusters : Maybe Clu.ClustersByRank
, parameters : List Parameters
, title : String
, min : Int
, max : Int
, numberOfClusters : Int
, histogramData : List Int
, pattern : String
, rank : Rank.Rank
, distance : Bool
, taxonomy : Bool
, showOrphan : Bool
}
type alias ModelPort =
{ distanceClusters : Clu.ClustersByRank
, taxonomicClusters : Clu.ClustersByRank
, parameters : List Parameters
}
type alias Range =
{ min : Int
, max : Int
}
type alias Parameters =
{ pvalue : Float
, distance : Float
, kmer : Int
, sketch : Int
}
init : ( Model, Cmd Msg )
init =
( Model Nothing Nothing Nothing [] "" 0 1 0 [ 0 ] "" (Rank.Species Nothing) True False True, Cmd.none )
-- UPDATE
type Msg
= TaxonomicCluster
| DistanceCluster
| DataClusters ModelPort
| SliderChange Range
| FilterClusters String
| ChangeRank String
| ShowOrphan
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
DataClusters m ->
let
rank =
Rank.Species Nothing
distanceClusters =
Just
(Clu.ClustersByRank
--m.distanceClusters.oid
m.distanceClusters.strain
m.distanceClusters.species
m.distanceClusters.genus
m.distanceClusters.family
m.distanceClusters.order
m.distanceClusters.class
m.distanceClusters.phylum
)
rankedDistanceClusters =
Clu.getClustersByRank rank distanceClusters
taxonomicClusters =
Just
(Clu.ClustersByRank
--m.taxonomicClusters.oid
m.taxonomicClusters.strain
m.taxonomicClusters.species
m.taxonomicClusters.genus
m.taxonomicClusters.family
m.taxonomicClusters.order
m.taxonomicClusters.class
m.taxonomicClusters.phylum
)
( min, max ) =
getMinMaxClusterSize rankedDistanceClusters
filteredClusters =
case rankedDistanceClusters of
Nothing ->
[]
Just clusters ->
clusters
|> List.filter (hasPattern pattern)
|> preprocessCluster min max showOrphan
numOfClusters =
(List.length filteredClusters)
showOrphan =
True
pattern =
"escherichia coli"
histogramData =
(List.map (\n -> List.length n.data) filteredClusters)
in
( Model
distanceClusters
taxonomicClusters
distanceClusters
m.parameters
"Distance Clusters"
min
max
numOfClusters
histogramData
pattern
rank
True
False
showOrphan
, defaultCommands min max filteredClusters histogramData
)
-- TAXONOMIC
TaxonomicCluster ->
let
clusters =
Clu.getClustersByRank model.rank model.taxonomicClusters
( min, max ) =
getMinMaxClusterSize clusters
filteredClusters =
case clusters of
Nothing ->
[]
Just clu ->
clu
|> List.filter (hasPattern model.pattern)
|> preprocessCluster min max model.showOrphan
newModel =
{ model
| title = "Taxonomic clusters"
, displayedClusters = model.taxonomicClusters
, numberOfClusters = List.length filteredClusters
, histogramData = List.map (\n -> List.length n.data) filteredClusters
, taxonomy = True
, distance = False
}
in
( newModel
, defaultCommands min max filteredClusters newModel.histogramData
)
-- DISTANCE
DistanceCluster ->
let
clusters =
Clu.getClustersByRank model.rank model.distanceClusters
filteredClusters =
case clusters of
Nothing ->
[]
Just c ->
c
|> List.filter (hasPattern model.pattern)
|> preprocessCluster min max model.showOrphan
( min, max ) =
getMinMaxClusterSize clusters
newModel =
{ model
| title = "Distance Clusters"
, displayedClusters = model.distanceClusters
, numberOfClusters = List.length filteredClusters
, histogramData = List.map (\n -> List.length n.data) filteredClusters
, taxonomy = False
, distance = True
}
in
( newModel
, defaultCommands min max filteredClusters newModel.histogramData
)
SliderChange range ->
let
displayedClusters =
Clu.getClustersByRank model.rank model.displayedClusters
filteredClusters =
case displayedClusters of
Nothing ->
[]
Just clust ->
clust
|> List.filter (hasPattern model.pattern)
|> preprocessCluster range.min range.max model.showOrphan
newModel =
{ model
| min = range.min
, max = range.max
, numberOfClusters = List.length filteredClusters
, histogramData = List.map (\n -> List.length n.data) filteredClusters
}
in
( newModel
, Cmd.batch
[ draw ( filteredClusters, newModel.histogramData )
]
)
-- FILTER CLUSTERS ON PATTERN
FilterClusters pattern ->
let
displayedClusters =
Clu.getClustersByRank model.rank model.displayedClusters
filteredClusters =
case displayedClusters of
Nothing ->
[]
Just clust ->
clust
|> List.filter (hasPattern pattern)
|> preprocessCluster model.min model.max model.showOrphan
( min, max ) =
getMinMaxClusterSize (Just filteredClusters)
maxCorrected =
if max <= 0 then
1
else
max
newModel =
{ model
| title = model.title
, numberOfClusters = List.length filteredClusters
, histogramData = List.map (\n -> List.length n.data) filteredClusters
, pattern = pattern
}
in
( newModel
, Cmd.batch
[ sliderValue [ model.min, model.max ]
, draw ( filteredClusters, model.histogramData )
]
)
-- CHANGE RANK
ChangeRank val ->
let
rank =
case (Rank.maybeRankOfString val Nothing) of
Nothing ->
Rank.Genus Nothing
Just rank ->
rank
displayedClusters =
Clu.getClustersByRank rank model.displayedClusters
( min, max ) =
getMinMaxClusterSize displayedClusters
filteredClusters =
case displayedClusters of
Nothing ->
[]
Just clust ->
clust
|> List.filter (hasPattern model.pattern)
|> preprocessCluster min max model.showOrphan
newModel =
{ model
| numberOfClusters = List.length filteredClusters
, histogramData = List.map (\n -> List.length n.data) filteredClusters
, rank = rank
}
in
( newModel
, defaultCommands min max filteredClusters newModel.histogramData
)
ShowOrphan ->
let
displayedClusters =
Clu.getClustersByRank model.rank model.displayedClusters
showOrphan =
not model.showOrphan
( min, max ) =
getMinMaxClusterSize displayedClusters
filteredClusters =
case displayedClusters of
Nothing ->
[]
Just clust ->
clust
|> List.filter (hasPattern model.pattern)
|> preprocessCluster min max showOrphan
numOfClusters =
(List.length filteredClusters)
newModel =
{ model
| showOrphan = showOrphan
, histogramData = List.map (\n -> List.length n.data) filteredClusters
, numberOfClusters = numOfClusters
}
in
( newModel
, defaultCommands min max filteredClusters newModel.histogramData
)
-- SUBSCRIPTIONS
-- send through port
port draw : ( Clusters, List Int ) -> Cmd msg
port deletePies : String -> Cmd msg
port sliderRange : List Int -> Cmd msg
port sliderValue : List Int -> Cmd msg
-- Get from port
port dataClusters : (ModelPort -> msg) -> Sub msg
port sliderChange : (Range -> msg) -> Sub msg
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ dataClusters DataClusters
, sliderChange SliderChange
]
-- VIEW
view : Model -> Html Msg
view model =
Html.form
[ classList
[ ( "ui", True )
, ( "form", True )
]
]
[ parameters model.parameters
, div
[ class "inline fields" ]
[ div
[ class "field" ]
[ select
[ class "ui dropdown"
, on "change" (Json.map ChangeRank targetValue)
]
(rankOptions model)
]
, div
[ class "field" ]
[ input
[ placeholder "Filter clusters"
, value model.pattern
, onInput FilterClusters
]
[]
]
, div
[ class "field" ]
[ div
[ class "ui checkbox" ]
[ input [ type' "checkbox", onClick ShowOrphan, checked model.showOrphan ] [ text "orphan" ]
, label [ checked model.showOrphan ] [ text "Show Orphan" ]
]
]
, div
[ class "field" ]
[ div
[ class "ui basic buttons" ]
[ div
[ classList
[ ( "ui", True )
, ( "button", True )
, ( "active", model.distance )
]
, onClick (DistanceCluster)
]
[ text "Distance Clusters" ]
, div
[ classList
[ ( "ui", True )
, ( "button", True )
, ( "active", model.taxonomy )
]
, onClick (TaxonomicCluster)
]
[ text "Taxonomic Clusters" ]
]
, a [ class "ui grey circular label" ]
[ text (toString model.numberOfClusters) ]
]
]
]
parameters :
List { e | distance : a, kmer : b, pvalue : c, sketch : d }
-> Html f
parameters params =
let
row param =
Html.tr
[]
[ Html.td [] [ text (toString (param.kmer)) ]
, Html.td [] [ text (toString (param.sketch)) ]
, Html.td [] [ text (toString (param.pvalue)) ]
, Html.td [] [ text (toString (param.distance)) ]
]
in
Html.table
[ class "ui celled table" ]
[ thead []
[ tr []
[ th [] [ text "Kmer size" ]
, th [] [ text "Number Sketches" ]
, th [] [ text "P-value Threshold" ]
, th [] [ text "Distance Threshold" ]
]
]
, tbody [] (List.map row params)
]
preprocessCluster : Int -> Int -> Bool -> Clusters -> Clusters
preprocessCluster min max showOrphan clusters =
let
betweenRange min max cluster =
List.length cluster.data >= min && List.length cluster.data <= max
isOrphan showOrphan cluster =
let
count =
List.foldr (\n c -> n.count + c) 0 cluster.data
in
if (not showOrphan && count == 1) then
False
else
True
in
clusters
|> List.filter (isOrphan showOrphan)
|> List.filter (betweenRange min max)
|> List.sortBy (\n -> List.length n.data)
|> List.reverse
getMinMaxClusterSize : Maybe Clusters -> ( Int, Int )
getMinMaxClusterSize cluster =
case cluster of
Nothing ->
( 0, 0 )
Just cluster ->
let
minMax cluster range =
let
currentLength =
List.length cluster.data
( min, max ) =
range
in
( Basics.min currentLength min, Basics.max currentLength max )
in
List.foldr minMax ( 0, 0 ) cluster
hasPattern : String -> Cluster -> Bool
hasPattern pattern cluster =
let
clusterContain d =
case d.name of
Nothing ->
False
Just name ->
Regex.contains (Regex.caseInsensitive (Regex.regex pattern)) name
in
case cluster.name of
Nothing ->
False
Just name ->
Regex.contains (Regex.caseInsensitive (Regex.regex pattern)) name
|| List.any clusterContain cluster.data
rankOptions : Model -> List (Html a)
rankOptions model =
let
ranks =
Rank.getAllRank
rankModel =
model.rank
toOption rank =
case rank of
Rank.Oid rankInfo ->
option [ value "oid", selected (rankModel == rank) ] [ text "Oid" ]
Rank.Strain rankInfo ->
option [ value "strain", selected (rankModel == rank) ] [ text "Strain" ]
Rank.Species rankInfo ->
option [ value "species", selected (rankModel == rank) ] [ text "Species" ]
Rank.Genus rankInfo ->
option [ value "genus", selected (rankModel == rank) ] [ text "Genus" ]
Rank.Family rankInfo ->
option [ value "family", selected (rankModel == rank) ] [ text "Family" ]
Rank.Order rankInfo ->
option [ value "order", selected (rankModel == rank) ] [ text "Order" ]
Rank.Class rankInfo ->
option [ value "class", selected (rankModel == rank) ] [ text "Class" ]
Rank.Phylum rankInfo ->
option [ value "phylum", selected (rankModel == rank) ] [ text "Phylum" ]
in
List.map toOption ranks
defaultCommands : Int -> Int -> Clusters -> List Int -> Cmd a
defaultCommands min max clusters histogram =
Cmd.batch
[ sliderRange [ min, max ]
, sliderValue [ min, max ]
, draw ( clusters, histogram )
]