Mercurial > repos > eschen42 > w4mkmeans
annotate w4mkmeans_routines.R @ 2:c415b7dc6f37 draft default tip
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
author | eschen42 |
---|---|
date | Mon, 05 Mar 2018 12:40:17 -0500 |
parents | 02cafb660b72 |
children |
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 |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
7 w4mkmeans_usage <- function() { |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
8 return ( |
0
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.", |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
11 " contract:", |
0
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:", |
1
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
13 " log_print - a logging function with the signature function(x, ...) expecting strings as x and ...", |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
14 " variableMetadata - the corresponding W4M data.frame having feature metadata", |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
15 " sampleMetdata - the corresponding W4M data.frame having sample metadata", |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
16 " dataMatrix - the corresponding W4M matrix", |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
17 " slots - the number of parallel slots for calculating kmeans", |
0
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:", |
1
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
19 " kfeatures - an array of integers, the k's to apply for clustering by feature (default, empty array)", |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
20 " ksamples - an array of integers, the k's to apply for clustering by sample (default, empty array)", |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
21 " iter_max - the maximum number of iterations when calculating a cluster (default = 20)", |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
22 " nstart - how many random sets of centers should be chosen (default = 20)", |
1
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
23 " algorithm - string from c('Hartigan-Wong', 'Lloyd', 'Forgy', 'MacQueen') (default = Hartigan-Wong)", |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
24 " categorical_prefix - string from c('Hartigan-Wong', 'Lloyd', 'Forgy', 'MacQueen') (default = Hartigan-Wong)", |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
25 " ", |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
26 " this routine will return a list comprising:", |
1
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
27 " variableMetadata - the input variableMetadata data.frame with updates, if any", |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
28 " sampleMetadata - the input sampleMetadata data.frame with updates, if any", |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
29 " scores - an array of strings, each representing a line of a tsv having the following header:", |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
30 " clusterOn TAB k TAB totalSS TAB betweenSS TAB proportion" |
0
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 |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
35 w4mkmeans <- function(env) { |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
36 # 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
|
37 if ( is.null(env) || ! is.environment(env) ) { |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
38 lapply(w4mkmeans_usage(),print) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
39 } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
40 # extract parameters from 'env' |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
41 log_action <- env$log_print |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
42 # supply default arguments |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
43 if ( ! exists("iter_max" , env) ) env$iter_max <- 20 |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
44 if ( ! exists("nstart" , env) ) env$nstart <- 20 |
1
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
45 if ( ! exists("algorithm" , env) ) env$algorithm <- 'Hartigan-Wong' |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
46 if ( ! exists("categorical_prefix", env) ) env$categorical_prefix <- 'c' |
1
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
47 if ( ! exists("ksamples" , env) ) env$ksamples <- c() |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
48 if ( ! exists("kfeatures" , env) ) env$kfeatures <- c() |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
49 # check mandatory arguments |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
50 expected <- c( |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
51 "log_print" |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
52 , "variableMetadata" |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
53 , "sampleMetadata" |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
54 , "dataMatrix" |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
55 , "slots" |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
56 ) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
57 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
|
58 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
|
59 print(paste(c('expected environment members not found: ', as.character(missing_from_env)), collapse = ", ")) |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
60 lapply(w4mkmeans_usage(),log_action) |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
61 stop("w4mkmeans: contract has been broken") |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
62 } |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
63 # extract parameters from 'env' |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
64 log_action <- env$log_print |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
65 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
|
66 sampleMetadata <- env$sampleMetadata |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
67 featureMetadata <- env$variableMetadata |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
68 slots <- env$slots |
1
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
69 positive_ints <- function(a, what) { |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
70 i <- as.integer(a) # may introduce NAs by coercion |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
71 i <- i[!is.na(i)] # eliminate NAs |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
72 i <- i[i > 0] # eliminate non-positive integers |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
73 i <- unique(sort(i)) # eliminate redundancy and disorder |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
74 if (length(a)!=length(i)) { |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
75 log_action("Some values for '", what, "' were skipped where not unique, not positive, or not convertible to an integer.") |
1
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
76 } |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
77 return (i) # return results, if any |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
78 } |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
79 ksamples <- positive_ints(env$ksamples , "ksamples") |
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
80 kfeatures <- positive_ints(env$kfeatures, "kfeatures") |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
81 |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
82 log_action("w4mkmeans: preparing data matrix") |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
83 # prepare data matrix (normalize, eliminate zero-variance rows, etc.; no transformation) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
84 dm_en <- new.env() |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
85 dm_en$log <- c() |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
86 preparation_result <- tryCatchFunc(function(){ |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
87 dm <- prepare.data.matrix( |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
88 x.matrix = env$dataMatrix |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
89 , data.transformation = function(x) { x } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
90 , en = dm_en |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
91 ) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
92 my_log <- dm_en$log |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
93 for ( i in 1:length(my_log) ) { |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
94 log_action(my_log[i]) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
95 } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
96 dm |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
97 }) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
98 if ( !preparation_result$success ) { |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
99 postmortem <- paste("prepare.data.matrix failed:", preparation_result$msg) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
100 log_action(postmortem) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
101 stop(postmortem) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
102 } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
103 |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
104 env$preparedDataMatrix <- preparation_result$value |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
105 |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
106 log_action("w4mkmeans: determining evaluation mode") |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
107 |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
108 myLapply <- parLapply |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
109 cl <- NULL |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
110 tryCatch( |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
111 expr = { |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
112 outfile <- "" |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
113 # outfile: Where to direct the stdout and stderr connection output from the workers. |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
114 # - "" indicates no redirection (which may only be useful for workers on the local machine). |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
115 # - Defaults to ‘/dev/null’ (‘nul:’ on Windows). |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
116 # - The other possibility is a file path on the worker's host. |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
117 # - Files will be opened in append mode, as all workers log to the same file. |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
118 cl <- makePSOCKcluster( names = slots, outfile = outfile ) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
119 } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
120 , error = function(e) { |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
121 log_action(sprintf("w4kmeans: falling back to serial evaluation because makePSOCKcluster(names = %d) threw an exception", slots)) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
122 # mimic parLapply, but without parallelization (as a last resort) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
123 myLapply <<- function(cl, ...) lapply(...) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
124 } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
125 ) |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
126 if ( identical(myLapply, parLapply) ) { |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
127 log_action(sprintf("w4mkmeans: using parallel evaluation with %d slots", slots)) |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
128 # from ?makePSOCKcluster: "It is good practice to shut down the workers by calling stopCluster." |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
129 # note varlist: names of references must be passed to the cluster so that they can be resolved |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
130 clusterExport( |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
131 cl = cl |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
132 , varlist = c( |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
133 "tryCatchFunc" # required by calc_kmeans_one_dimension_one_k |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
134 , "format_error" # required by tryCatchFunc when errors are caught |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
135 , "iso_date" # required by log_print |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
136 , "log_print" # required by calc_kmeans_one_dimension_one_k |
0
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 final <- function(cl) { |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
140 # 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
|
141 if ( !is.null(cl) ) { |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
142 log_action("w4mkmeans: stopping cluster used for parallel evaluation") |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
143 stopCluster(cl) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
144 } |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
145 } |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
146 } else { |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
147 log_action("w4mkmeans: using sequential evaluation (one slot)") |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
148 final <- function(cl) { } |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
149 } |
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 tryCatch( |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
152 expr = { |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
153 # 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
|
154 # - 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
|
155 # - 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
|
156 |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
157 # 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
|
158 ksamples_length <- length(ksamples) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
159 if ( ksamples_length > 0 ) { |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
160 smpl_result_list <- myLapply( |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
161 cl = cl |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
162 , ksamples |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
163 , 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
|
164 , env = env |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
165 , dimension = "samples" |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
166 ) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
167 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
|
168 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
|
169 if (result$success) { |
1
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
170 sampleMetadata[sprintf("k%d",ksamples[i])] <- sprintf("%s%d", env$categorical_prefix, result$value$clusters) |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
171 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
|
172 } |
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 } |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
175 |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
176 # 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
|
177 kfeatures_length <- length(kfeatures) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
178 if ( kfeatures_length > 0 ) { |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
179 feat_result_list <- myLapply( |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
180 cl = cl |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
181 , kfeatures |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
182 , 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
|
183 , env = env |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
184 , dimension = "features" |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
185 ) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
186 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
|
187 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
|
188 if (result$success) { |
1
02cafb660b72
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit f600ce8a783df16e49272341dce0fc6bbc299b0a
eschen42
parents:
0
diff
changeset
|
189 featureMetadata[sprintf("k%d",kfeatures[i])] <- sprintf("%s%d", env$categorical_prefix, result$value$clusters) |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
190 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
|
191 } |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
192 } |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
193 } |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
194 |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
195 return ( |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
196 list( |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
197 variableMetadata = featureMetadata |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
198 , sampleMetadata = sampleMetadata |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
199 , scores = scores |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
200 ) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
201 ) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
202 } |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
203 , finally = { |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
204 final(cl) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
205 } |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
206 ) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
207 } |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
208 |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
209 # 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
|
210 # - 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
|
211 # return value: |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
212 # list(clusters = km$cluster, scores = scores) |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
213 # arguments: |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
214 # env: |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
215 # environment having dataMatrix |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
216 # dimension: |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
217 # - "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
|
218 # - 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
|
219 # - "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
|
220 # k: |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
221 # 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
|
222 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
|
223 # 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
|
224 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
|
225 stop("calc_kmeans_one_dimension_one_k - argument 'env' is not an environment") |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
226 } |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
227 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
|
228 stop("calc_kmeans_one_dimension_one_k - argument 'env' - environment does not include log_print or it is not a function") |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
229 } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
230 log_action <- env$log_print |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
231 # 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
|
232 if ( ! is.numeric(k) ) { |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
233 stop(sprintf("calc_kmeans_one_dimension_one_k - expected numeric argument 'k' but type is %s", typeof(k))) |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
234 } |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
235 k <- as.integer(k) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
236 # abort if dimension is not as expected |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
237 if ( ! is.character(dimension) |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
238 || ! 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
|
239 stop("calc_kmeans_one_dimension_one_k - argument 'dimension' is neither 'features' nor 'samples'") |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
240 } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
241 dm <- env$preparedDataMatrix |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
242 iter_max <- env$iter_max |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
243 nstart <- env$nstart |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
244 algorithm <- env$algorithm |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
245 dim_features <- dimension == "features" |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
246 |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
247 # tryCatchFunc produces a list |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
248 # On success of func(), tryCatchFunc produces |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
249 # list(success = TRUE, value = func(), msg = "") |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
250 # On failure of func(), tryCatchFunc produces |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
251 # list(success = FALSE, value = NA, msg = "the error message") |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
252 result_list <- tryCatchFunc( func = function() { |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
253 # 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
|
254 # - 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
|
255 # - to calculate feature-clusters, transposition is needed so that features will be the rows |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
256 if ( ! dim_features ) { |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
257 dm <- t(dm) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
258 } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
259 |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
260 # 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
|
261 set.seed(4567) |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
262 |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
263 # do the k-means clustering |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
264 withCallingHandlers( |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
265 { |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
266 km <<- kmeans( x = dm, centers = k, iter.max = iter_max, nstart = nstart, algorithm = algorithm ) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
267 } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
268 , warning = function(w) { |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
269 lw <- list(w) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
270 smplwrn <- as.character(w[[1]]) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
271 log_print( |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
272 sprintf( "Warning for %s: center = %d, nstart = %d, iter_max = %d: %s" |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
273 , if (dim_features) "features" else "samples" |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
274 , k |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
275 , nstart |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
276 , iter_max |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
277 , smplwrn |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
278 ) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
279 ) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
280 } |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
281 ) |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
282 |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
283 # collect the scores |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
284 scores <- |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
285 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
|
286 , dimension |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
287 , k |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
288 , km$totss |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
289 , km$betweenss |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
290 , km$betweenss/km$totss |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
291 ) |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
292 |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
293 # return list of results |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
294 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
|
295 }) |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
296 |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
297 # return either |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
298 # list(success = TRUE, value = func(), msg = "") |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
299 # or |
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
300 # list(success = FALSE, value = NA, msg = "the error message") |
0
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
301 return ( result_list ) |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
302 } |
6ccbe18131a6
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 299e5c7fdb0d6eb0773f3660009f6d63c2082a8d
eschen42
parents:
diff
changeset
|
303 |
2
c415b7dc6f37
planemo upload for repository https://github.com/HegemanLab/w4mkmeans_galaxy_wrapper/tree/master commit 3e916537da6bb37e6f3927d7a11e98e0ab6ef5ec
eschen42
parents:
1
diff
changeset
|
304 # vim: sw=2 ts=2 et : |