Skip to content

Commit c2786a8

Browse files
committed
change PCA scores type to dataset
Makes it similar to predicted output that and allows meta data to be kept with scores for plotting, further analysis etc.
1 parent d6013e9 commit c2786a8

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

R/PCA_class.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ PCA<-setClass(
3232
value=2,
3333
type='numeric'
3434
),
35-
outputs.scores=entity('name'='PCA scores matrix',
36-
'description'='A matrix of PCA scores where each column corresponds to a Principal Component')
35+
outputs.scores=entity('name'='PCA scores dataset',
36+
'description'='A matrix of PCA scores where each column corresponds to a Principal Component',
37+
'type'='dataset')
3738
)
3839
)
3940

@@ -60,7 +61,9 @@ setMethod(f="model.train",
6061
varnames[i]=paste0('PC',i)
6162
}
6263
colnames(scores)=varnames
63-
output.value(M,'scores')=scores
64+
S=D
65+
S$data=scores
66+
output.value(M,'scores')=S
6467

6568
P=as.data.frame(model$v)
6669
rownames(P)=colnames(X)

R/PCA_plotfcns.R

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ pca_scores_plot<-setClass(
6767
params.components='entity',
6868
params.points_to_label='enum',
6969
params.factor_name='entity',
70-
params.groups='entity',
7170
params.ellipse='enum',
72-
params.label_filter='entity'
71+
params.label_filter='entity',
72+
params.groups='factor'
7373
),
7474

7575
prototype = list(name='PCA scores plot',
@@ -91,12 +91,7 @@ pca_scores_plot<-setClass(
9191
params.factor_name=entity(name='Factor name',
9292
value='factor',
9393
type='character',
94-
description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
95-
),
96-
params.groups=entity(name='Groups',
97-
value=factor(),
98-
type='factor',
99-
description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
94+
description='The column name of sample meta to use for plotting.'
10095
),
10196
params.ellipse=enum(name = 'Plot ellipses',description=c(
10297
'"all" will plot all ellipses',
@@ -127,11 +122,8 @@ setMethod(f="chart.plot",
127122
if (obj$points_to_label=='outliers' & !(obj$ellipse %in% c('all','sample'))) {
128123
warning('Outliers are only labelled when plotting the sample ellipse')
129124
}
130-
131-
132125
opt=param.list(obj)
133-
134-
scores=output.value(dobj,'scores')
126+
scores=output.value(dobj,'scores')$data
135127
pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100 # percent variance
136128
pvar=round(pvar,digits = 2) # round to 2 decimal places
137129
shapes <- rep(19,nrow(scores)) # filled circles for all samples
@@ -141,6 +133,9 @@ setMethod(f="chart.plot",
141133
xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
142134
ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
143135

136+
# get the factor from meta data
137+
opt$groups=dobj$scores$sample_meta[[opt$factor_name]]
138+
144139
# add a space to the front of the labels to offset them from the points, because nudge_x is in data units
145140
for (i in 1:length(slabels))
146141
{
@@ -172,12 +167,10 @@ setMethod(f="chart.plot",
172167
stat_ellipse(type='norm') # ellipse for individual groups
173168
}
174169

175-
if (is(opt$groups,'factor')) # if a factor then plot by group using the colours from pmp package
176-
{
170+
if (is(opt$groups,'factor')) { # if a factor then plot by group using the colours from pmp package
177171
out=out+scale_colour_manual(values=plotClass$manual_colors,name=opt$factor_name)
178172
}
179-
else # assume continuous and use the default colour gradient
180-
{
173+
else {# assume continuous and use the default colour gradient
181174
out=out+scale_colour_viridis_c(limits=quantile(opt$groups,c(0.05,0.95),na.rm = TRUE),oob=squish,name=opt$factor_name)
182175
}
183176
out=out+theme_Publication(base_size = 12)
@@ -294,7 +287,7 @@ setMethod(f="chart.plot",
294287
definition=function(obj,dobj)
295288
{
296289
opt=param.list(obj)
297-
Ts=output.value(dobj,'scores')
290+
Ts=output.value(dobj,'scores')$data
298291
pvar=(colSums(Ts*Ts)/output.value(dobj,'ssx'))*100
299292
pvar=round(pvar,digits = 1)
300293
xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
@@ -318,7 +311,7 @@ setMethod(f="chart.plot",
318311
# additionaly scale the loadings
319312
sf=min(max(abs(Ts[,opt$components[1]]))/max(abs(P[,opt$components[1]])),
320313
max(abs(Ts[,opt$components[2]]))/max(abs(P[,opt$components[2]])))
321-
dobj$scores=as.data.frame(Ts) # nb object not returned, so only temporary scaling
314+
dobj$scores$data=as.data.frame(Ts) # nb object not returned, so only temporary scaling
322315

323316
# plot
324317
A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
@@ -474,7 +467,7 @@ setMethod(f="chart.plot",
474467
definition=function(obj,dobj)
475468
{
476469
## percent variance
477-
scores=output.value(dobj,'scores')
470+
scores=output.value(dobj,'scores')$data
478471
pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100
479472
A=data.frame("x"=1:length(pvar),"y"=c(pvar,cumsum(pvar)),"Variance"=as.factor(c(rep('Single component',length(pvar)),rep('Cumulative',length(pvar)))))
480473
labels=round(A$y,digits = 1)
@@ -531,7 +524,7 @@ setMethod(f="chart.plot",
531524
{
532525
opt=param.list(obj)
533526
a=param.value(obj,'number_components')
534-
scores=output.value(dobj,'scores')
527+
scores=output.value(dobj,'scores')$data
535528
I=nrow(scores) # number of samples
536529
sample_names=rownames(scores)
537530
scores=scores[,1:a]

0 commit comments

Comments
 (0)