Skip to content

Commit ebd24cb

Browse files
committed
2 parents 116359c + 85f6b2b commit ebd24cb

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

R/fold_change_class.R

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ setMethod(f="method.apply",
7777
X=log2(D$data)
7878
Y=D$sample_meta
7979

80-
# for paired data sort by sample id
81-
if (M$paired) {
82-
X=X[order(Y[[M$sample_name]]),drop=FALSE]
83-
Y=Y[order(Y[[M$sample_name]]),drop=FALSE]
84-
}
85-
8680
D$data=X
8781
D$sample_meta=Y
8882

@@ -119,10 +113,14 @@ setMethod(f="method.apply",
119113
# change to ordered factor so that we make use of control group
120114
FG$filtered$sample_meta[[M$factor_name]]=ordered(FG$filtered$sample_meta[[M$factor_name]],levels=L[c(A,B)])
121115
# apply t-test
122-
TT=ttest(alpha=M$alpha,mtc='none',factor_names=M$factor_name,paired=M$paired)
116+
TT=ttest(alpha=M$alpha,mtc='none',factor_names=M$factor_name,paired=M$paired,paired_factor=M$sample_name)
123117
TT=method.apply(TT,predicted(FG))
124118
# log2(fold change) is the difference in estimate.mean from ttest
125-
fc=TT$estimates[,1]-TT$estimates[,2] # log2 fold change
119+
if (M$paired) {
120+
fc=TT$estimates[,1]
121+
} else {
122+
fc=TT$estimates[,1]-TT$estimates[,2] # log2 fold change
123+
}
126124
FC[,counter]=fc
127125
LCI[,counter]=TT$conf_int[,1]
128126
UCI[,counter]=TT$conf_int[,2]

R/ttest_class.R

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ttest<-setClass(
1414
params.mtc='entity.stato',
1515
params.factor_names='entity',
1616
params.paired='entity',
17+
params.paired_factor='character',
1718
# OUTPUTS
1819
outputs.t_statistic='entity.stato',
1920
outputs.p_value='entity',
@@ -89,6 +90,36 @@ setMethod(f="method.apply",
8990
if (length(L)!=2) {
9091
stop('must have exactly two levels for this implmentation of t-statistic')
9192
}
93+
94+
if (M$paired){
95+
# check that we have a pair for each sample,
96+
# if not then remove
97+
u=unique(D$sample_meta[[M$paired_factor]])
98+
out=character(0) # list of sample_id to remove
99+
for (k in u) {
100+
n=sum(D$sample_meta[[M$paired_factor]]==k)
101+
if (n<2) {
102+
out=c(out,k)
103+
}
104+
105+
}
106+
X=X[!(D$sample_meta[[M$paired_factor]] %in% out),]
107+
y=y[!(D$sample_meta[[M$paired_factor]] %in% out)]
108+
109+
# sort the data by sample id so that theyre in the right order for paired ttest
110+
X=apply(X,2,function(x) {
111+
a=x[y==L[1]]
112+
b=x[y==L[2]]
113+
ay=y[y==L[1]]
114+
by=y[y==L[2]]
115+
a=a[order(ay)]
116+
b=b[order(by)]
117+
return(c(a,b))
118+
})
119+
# also make sure the meta data is in the right order
120+
y=y[order(D$sample_meta[[M$paired_factor]])]
121+
}
122+
92123
output=apply(X,2,function(x) {a=unlist(t.test(x[y==L[1]],x[y==L[2]],paired = M$paired)[c("statistic","p.value","parameter",'conf.int','estimate')])})
93124
output=as.data.frame(t(output),stringsAsFactors = FALSE)
94125
output$p.value=p.adjust(output$p.value,method = param.value(M,'mtc'))
@@ -98,8 +129,14 @@ setMethod(f="method.apply",
98129
output.value(M,'significant')=output$p.value<param.value(M,'alpha')
99130
M$conf_int=output[,4:5,drop=FALSE]
100131
colnames(M$conf_int)=c('lower','upper')
101-
M$estimates=output[,6:7,drop=FALSE]
102-
colnames(M$estimates)=as.character(interaction('estimate.mean',L))
132+
if (M$paired) {
133+
M$estimates=output[,6,drop=FALSE]
134+
colnames(M$estimates)='estimated_mean_difference'
135+
} else {
136+
M$estimates=output[,6:7,drop=FALSE]
137+
colnames(M$estimates)=as.character(interaction('estimate.mean',L))
138+
}
139+
103140
return(M)
104141
}
105142
)

0 commit comments

Comments
 (0)