annotate w4mkmeans_routines.R @ 3:afb738a080b9 draft

planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 0e530bff0b41dc97ac7b3665a79840017bc36b40
author eschen42
date Wed, 09 Aug 2017 14:42:38 -0400
parents 330ee1d840db
children 06385448ff7f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
1 ##------------------------------------------------------------------------------------------------------
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
2 ## these are the batch-independent and file-structure-independent routines to support the w4mkmeans tool
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
3 ##------------------------------------------------------------------------------------------------------
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
4
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
5 library(parallel)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
6
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
7 w4kmeans_usage <- function() {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
8 return (
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
9 c(
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
10 "w4mkmeans: bad input.",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
11 "# contract:",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
12 " required - caller will provide an environment comprising:",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
13 " log_print - a logging function with the signature function(x, ...) expecting strings as x and ...",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
14 " variableMetadata - the corresponding W4M data.frame having feature metadata",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
15 " sampleMetdata - the corresponding W4M data.frame having sample metadata",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
16 " dataMatrix - the corresponding W4M matrix",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
17 " slots - the number of parallel slots for calculating kmeans",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
18 " optional - environment may comprise:",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
19 " kfeatures - an array of integers, the k's to apply for clustering by feature (default, empty array)",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
20 " ksamples - an array of integers, the k's to apply for clustering by sample (default, empty array)",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
21 " iter.max - the maximum number of iterations when calculating a cluster (default = 10)",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
22 " nstart - how many random sets of centers should be chosen (default = 1)",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
23 " algorithm - string from c('Hartigan-Wong', 'Lloyd', 'Forgy', 'MacQueen') (default = Hartigan-Wong)",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
24 " ",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
25 " this routine will return a list comprising:",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
26 " variableMetadata - the input variableMetadata data.frame with updates, if any",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
27 " sampleMetadata - the input sampleMetadata data.frame with updates, if any",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
28 " scores - an array of strings, each representing a line of a tsv having the following header:",
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
29 " clusterOn TAB k TAB totalSS TAB betweenSS TAB proportion"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
30 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
31 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
32 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
33
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
34 w4mkmeans <- function(env) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
35 # abort if 'env' is null or is not an environment
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
36 if ( is.null(env) || ! is.environment(env) ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
37 lapply(w4kmeans_usage(),print)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
38 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
39 # supply default arguments
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
40 if ( ! exists("iter.max" , env) ) env$iter.max <- 10
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
41 if ( ! exists("nstart" , env) ) env$nstart <- 1
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
42 if ( ! exists("algorithm", env) ) env$algorithm <- 'Hartigan-Wong'
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
43 if ( ! exists("ksamples" , env) ) env$ksamples <- c()
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
44 if ( ! exists("kfeatures", env) ) env$kfeatures <- c()
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
45 # check mandatory arguments
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
46 expected <- c(
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
47 "log_print"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
48 , "variableMetadata"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
49 , "sampleMetadata"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
50 , "dataMatrix"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
51 , "slots"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
52 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
53 missing_from_env <- setdiff(expected, (ls(env)))
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
54 if ( length(missing_from_env) > 0 ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
55 print(paste(c('expected environment members not found: ', as.character(missing_from_env)), collapse = ", "))
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
56 lapply(w4kmeans_usage(),print)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
57 stop("w4mkmeans: contract has been broken")
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
58 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
59 # extract parameters from 'env'
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
60 failure_action <- env$log_print
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
61 scores <- c( "clusterOn\tk\ttotalSS\tbetweenSS\tproportion" )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
62 sampleMetadata <- env$sampleMetadata
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
63 featureMetadata <- env$variableMetadata
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
64 ksamples <- as.numeric(env$ksamples)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
65 kfeatures <- as.numeric(env$kfeatures)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
66 slots <- env$slots
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
67
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
68 myLapply <- parLapply
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
69 # uncomment the next line to mimic parLapply, but without parallelization (for testing/experimentation)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
70 # myLapply <- function(cl, ...) lapply(...)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
71 cl <- NULL
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
72 if ( identical(myLapply, parLapply) ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
73 failure_action(sprintf("w4mkmeans: using parallel evaluation with %d slots", slots))
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
74 failure_action(names(cl))
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
75 cl <- makePSOCKcluster(names = slots)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
76 # from ?makePSOCKcluster: "It is good practice to shut down the workers by calling stopCluster."
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
77 clusterExport(
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
78 cl = cl
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
79 , varlist = c(
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
80 "tryCatchFunc"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
81 , "calc_kmeans_one_dimension_one_k"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
82 , "prepare.data.matrix"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
83 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
84 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
85 final <- function(cl) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
86 # from ?makePSOCKcluster: "It is good practice to shut down the workers by calling stopCluster."
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
87 if ( !is.null(cl) ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
88 failure_action("w4mkmeans: stopping cluster used for parallel evaluation")
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
89 stopCluster(cl)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
90 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
91 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
92 } else {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
93 failure_action("w4mkmeans: using sequential evaluation (1 slot)")
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
94 final <- function(cl) { }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
95 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
96
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
97 tryCatch(
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
98 expr = {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
99 # These myLapply calls produce lists of lists of results:
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
100 # - The outer list has no keys and its members are accessed by index
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
101 # - The inner list has keys "clusters" and "scores"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
102
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
103 # for each $i in ksamples, append column 'k$i' to data frame sampleMetadata
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
104 ksamples_length <- length(ksamples)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
105 if ( ksamples_length > 0 ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
106 smpl_result_list <- myLapply(
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
107 cl = cl
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
108 , ksamples
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
109 , calc_kmeans_one_dimension_one_k
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
110 , env = env
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
111 , dimension = "samples"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
112 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
113 for ( i in 1:ksamples_length ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
114 result <- smpl_result_list[[i]]
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
115 if (result$success) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
116 sampleMetadata[sprintf("k%d",ksamples[i])] <- result$value$clusters
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
117 scores <- c(scores, result$value$scores)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
118 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
119 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
120 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
121
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
122 # for each $i in kfeatures, append column 'k$i' to data frame featureMetadata
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
123 kfeatures_length <- length(kfeatures)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
124 if ( kfeatures_length > 0 ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
125 feat_result_list <- myLapply(
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
126 cl = cl
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
127 , kfeatures
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
128 , calc_kmeans_one_dimension_one_k
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
129 , env = env
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
130 , dimension = "features"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
131 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
132 for ( i in 1:kfeatures_length ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
133 result <- feat_result_list[[i]]
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
134 if (result$success) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
135 featureMetadata[sprintf("k%d",kfeatures[i])] <- result$value$clusters
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
136 scores <- c(scores, result$value$scores)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
137 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
138 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
139 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
140
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
141 return (
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
142 list(
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
143 variableMetadata = featureMetadata
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
144 , sampleMetadata = sampleMetadata
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
145 , scores = scores
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
146 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
147 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
148 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
149 , finally = final(cl)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
150 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
151 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
152
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
153 # calculate k-means for features or samples
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
154 # - recall that the dataMatrix has features in rows and samples in columns
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
155 # return value:
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
156 # list(clusters = km$cluster, scores = scores)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
157 # arguments:
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
158 # env:
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
159 # environment having dataMatrix
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
160 # dimension:
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
161 # - "samples": produce clusters column to add to the sampleMetadata table
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
162 # - this is the default case
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
163 # - "variables": produce clusters column to add to the variableMetadata table
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
164 # k:
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
165 # integer, the number of clusters to make
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
166 calc_kmeans_one_dimension_one_k <- function(k, env, dimension = "samples") {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
167 # abort if environment is not as expected
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
168 if ( is.null(env) || ! is.environment(env) ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
169 stop("calc_kmeans_one_dimension_one_k - argument 'env' is not an environment")
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
170 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
171 if ( ! exists("log_print", env) || ! is.function(env$log_print) ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
172 stop("calc_kmeans_one_dimension_one_k - argument 'env' - environment does not include log_print or it is not a function")
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
173 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
174 # abort if k is not as expected
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
175 if ( ! is.numeric(k) ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
176 stop(sprintf("calc_kmeans_one_dimension_one_k - expected numeric argument 'k' but type is %s", typeof(k)))
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
177 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
178 k <- as.integer(k)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
179 # abort if dimension is not as expected
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
180 if ( ! is.character(dimension)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
181 || ! Reduce( f =`|`, x = sapply(X = c("features","samples"), FUN = `==`, dimension), init = FALSE) ) {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
182 stop("calc_kmeans_one_dimension_one_k - argument 'dimension' is neither 'features' nor 'samples'")
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
183 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
184 dm <- env$dataMatrix
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
185 iter.max <- env$iter.max
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
186 nstart <- env$nstart
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
187 algorithm <- env$algorithm
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
188 dim_features <- dimension == "features"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
189 # tryCatchFunc produces a list
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
190 # On success of expr(), tryCatchFunc produces
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
191 # list(success TRUE, value = expr(), msg = "")
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
192 # On failure of expr(), tryCatchFunc produces
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
193 # list(success = FALSE, value = NA, msg = "the error message")
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
194 result_list <- tryCatchFunc( expr = function() {
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
195 # kmeans clusters the rows; features are the columns of args_env$dataMatrix; samples, the rows
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
196 # - to calculate sample-clusters, no transposition is needed because samples are rows
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
197 # - to calculate feature-clusters, transposition is needed so that features will be the rows
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
198 if ( ! dim_features ) dm <- t(dm)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
199 dm <- prepare.data.matrix( x.matrix = dm, data.transformation = function(x) { x } )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
200 # need to set.seed to get reproducible results from kmeans
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
201 set.seed(4567)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
202 # do the k-means clustering
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
203 km <- kmeans( x = dm, centers = k, iter.max, nstart = nstart, algorithm = algorithm )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
204 scores <-
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
205 sprintf("%s\t%d\t%0.5e\t%0.5e\t%0.5f"
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
206 , dimension
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
207 , k
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
208 , km$totss
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
209 , km$betweenss
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
210 , km$betweenss/km$totss
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
211 )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
212 list(clusters = km$cluster, scores = scores)
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
213 })
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
214 return ( result_list )
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
215 }
330ee1d840db planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit ae8ee04dc44eedf9121644ce8577c622da2a5f8c-dirty
eschen42
parents:
diff changeset
216