|
| 1 | +#' HCA method class |
| 2 | +#' |
| 3 | +#' HCA method class. Calculate a hierarchical clustering for the input data |
| 4 | +#' |
| 5 | +#' @export HCA |
| 6 | +HCA<-setClass( |
| 7 | + "HCA", |
| 8 | + contains=c('method'), |
| 9 | + slots=c( |
| 10 | + # INPUTS |
| 11 | + params.dist_method='enum', |
| 12 | + params.cluster_method='enum', |
| 13 | + params.minkowski_power='numeric', |
| 14 | + params.factor_name='character', |
| 15 | + # OUTPUTS |
| 16 | + outputs.dist_matrix='entity', |
| 17 | + outputs.hclust='entity', |
| 18 | + outputs.factor_df='data.frame' |
| 19 | + ), |
| 20 | + prototype = list(name='Hierarchical Cluster Analysis', |
| 21 | + description='Applies hierarchical clustering to a dataset.', |
| 22 | + type="univariate", |
| 23 | + predicted='dist_matrix', |
| 24 | + |
| 25 | + |
| 26 | + params.dist_method=enum(name='Distance method', |
| 27 | + value='euclidean', |
| 28 | + type='character', |
| 29 | + description='The distance measure to be used. This must be one of "euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski"', |
| 30 | + list=c("euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski") |
| 31 | + ), |
| 32 | + params.cluster_method=enum(name='Clustering method', |
| 33 | + value='complete', |
| 34 | + type='character', |
| 35 | + description='The agglomeration method to be used. This should be one of "ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median" or "centroid"', |
| 36 | + list=c("ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", "centroid") |
| 37 | + ), |
| 38 | + |
| 39 | + outputs.dist_matrix=entity(name='distance structure', |
| 40 | + type='dist', |
| 41 | + description='An object containing pairwise distance information between samples' |
| 42 | + ), |
| 43 | + outputs.hclust=entity(name='clustering object', |
| 44 | + type='hclust', |
| 45 | + description='An object of class hclust which describes the tree produced by the clustering process' |
| 46 | + ) |
| 47 | + ) |
| 48 | +) |
| 49 | + |
| 50 | +#' @export |
| 51 | +setMethod(f="method.apply", |
| 52 | + signature=c("HCA",'dataset'), |
| 53 | + definition=function(M,D) |
| 54 | + { |
| 55 | + |
| 56 | + M$dist_matrix=dist(D$data, method = M$dist_method, diag = FALSE, upper = FALSE, p = M$minkowski_power) |
| 57 | + |
| 58 | + M$hclust=hclust(M$dist_matrix, method = M$cluster_method, members = NULL) |
| 59 | + |
| 60 | + df=D$sample_meta[,M$factor_name,drop=FALSE] |
| 61 | + df$orig_order=1:nrow(df) |
| 62 | + df$label=rownames(D$data) |
| 63 | + M$factor_df=df |
| 64 | + return(M) |
| 65 | + } |
| 66 | +) |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +#' hca_dendrogram class |
| 71 | +#' |
| 72 | +#' plots a dendrogram for HCA |
| 73 | +#' |
| 74 | +#' @export hca_dendrogram |
| 75 | +#' @import ggdendro |
| 76 | +#' @include hca_class.R |
| 77 | +hca_dendrogram<-setClass( |
| 78 | + "hca_dendrogram", |
| 79 | + contains='chart' |
| 80 | +) |
| 81 | + |
| 82 | +#' @export |
| 83 | +setMethod(f="chart.plot", |
| 84 | + signature=c("hca_dendrogram",'HCA'), |
| 85 | + definition=function(obj,dobj) |
| 86 | + { |
| 87 | + hcdata=dendro_data(dobj$hclust) |
| 88 | + |
| 89 | + A=label(hcdata) |
| 90 | + |
| 91 | + A=A[order(dobj$factor_df$label),,drop=FALSE] |
| 92 | + dobj$factor_df[order(dobj$factor_df$label),,drop=FALSE] |
| 93 | + A$group=dobj$factor_df[,1] |
| 94 | + |
| 95 | + g= ggplot() + |
| 96 | + geom_segment(data=segment(hcdata), aes(x=x, y=y, xend=xend, yend=yend)) + |
| 97 | + geom_point(data=A, aes(x=x, y=y,color=group))+ |
| 98 | + structToolbox:::scale_colour_Publication() + |
| 99 | + structToolbox:::theme_Publication(base_size = 12) + |
| 100 | + labs(color = colnames(dobj$factor_df)[1]) + |
| 101 | + theme(axis.title.x=element_blank(), |
| 102 | + axis.text.x=element_blank(), |
| 103 | + axis.ticks.x=element_blank()) + |
| 104 | + ylab('dissimilarity') |
| 105 | + |
| 106 | + return(g) |
| 107 | + } |
| 108 | +) |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
0 commit comments