Skip to content

Commit 116359c

Browse files
committed
add HCA classes
1 parent 866b888 commit 116359c

File tree

6 files changed

+138
-1
lines changed

6 files changed

+138
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Collate:
3535
'ggplot_theme_pub.R'
3636
'glog_class.R'
3737
'grid_search_1d_class.R'
38+
'hca_class.R'
3839
'kfold_xval_class.R'
3940
'kfold_xval_charts.R'
4041
'knn_impute_class.R'

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(ANOVA)
4+
export(HCA)
45
export(HSD)
56
export(HSDEM)
67
export(PCA)
@@ -35,6 +36,7 @@ export(fs_line)
3536
export(glog_transform)
3637
export(grid_search_1d)
3738
export(gs_line)
39+
export(hca_dendrogram)
3840
export(kfold_xval)
3941
export(kfoldxcv_grid)
4042
export(kfoldxcv_metric)
@@ -84,6 +86,7 @@ exportMethods(run)
8486
import(agricolae)
8587
import(car)
8688
import(emmeans)
89+
import(ggdendro)
8790
import(ggplot2)
8891
import(ggthemes)
8992
import(grid)

R/hca_class.R

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+

man/HCA-class.Rd

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/hca_dendrogram-class.Rd

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/structToolbox.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)