annotate w4mkmeans_routines.R @ 0:6ccbe18131a6 draft

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