Stat_Model.R 6.17 KB
Newer Older
svolant's avatar
svolant committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#@ This file contains all the functions for the 
#@ statistical modelling (dds object, interaction, ...),
#@ and the functions concerning the contrasts



##############################################################
##
##          Statistical modelling
##
##############################################################


## Get the possible interaction for the statistical model
15
GetInteraction2 <- function(target,VarInt)
svolant's avatar
svolant committed
16
17
{ 
  res=c()
18
19
20
21
22
  
  if(length(VarInt)>=2)
  {
    k=1
    for(i in 1:(length(VarInt)-1))
svolant's avatar
svolant committed
23
    { 
24
25
26
27
28
      for(j in (i+1):length(VarInt))
      { 
        res[k] = paste(VarInt[i],":",VarInt[j],sep="")
        k = k+1
      }
svolant's avatar
svolant committed
29
30
31
32
33
34
35
    }
  }
  return(res)
}


## Get the dds object of DESeq2
svolant's avatar
svolant committed
36
Get_dds_object <- function(input,counts,target,design,normFactorsOTU,CT_noNorm,CT_Norm,tree=NULL)
svolant's avatar
svolant committed
37
{
svolant's avatar
svolant committed
38
  dds <- DESeqDataSetFromMatrix(countData=counts, colData=target, design=design,ignoreRank = TRUE)
svolant's avatar
svolant committed
39
  sizeFactors(dds) = normFactorsOTU
40
41
  ddstmp = NULL
  
svolant's avatar
svolant committed
42
43
44
45
46
47
48
49
#   dds <- estimateDispersions(dds, fitType=input$fitType)
#   if(as.numeric(R.Version()$major)>=3 && as.numeric(R.Version()$minor) >=1.3){
#     dds <- nbinomWaldTest(dds)
#   }else{
#     dds <- nbinomWaldTest(dds,modelMatrixType = "expanded")
#   }
#   countsNorm = counts(dds, normalized = TRUE)

50
51
52
53
54
55
56
57
58
59
60
61
  # if(as.numeric(R.Version()$major)>=3 && as.numeric(R.Version()$minor) >=1.3){
  #   if(nrow(counts)*nrow(target)>=50000)
  #   {
  #     try(DESeq(dds,fitType=input$fitType,parallel = TRUE,minReplicatesForReplace = Inf) ->ddstmp,silent = TRUE)
  #     if(!is.null(ddstmp)) dds = ddstmp
  #   }
  #   if(nrow(counts)*nrow(target)<50000 || is.null(ddstmp))
  #   {
  #     dds <- estimateDispersions(dds, fitType=input$fitType)
  #     dds <- nbinomWaldTest(dds)
  #   }
  # }else{
62
63
64
    dds <- estimateDispersions(dds, fitType=input$fitType)
    dds <- nbinomWaldTest(dds,modelMatrixType = "expanded")
    # dds <-  DESeq(dds,fitType=input$fitType,modelMatrixType = "expanded",parallel = TRUE)
65
  # }
svolant's avatar
svolant committed
66
67
  countsNorm = counts(dds, normalized = TRUE)
  
svolant's avatar
svolant committed
68
  
69
  #save(dds,file="dds.RData")
svolant's avatar
svolant committed
70
71
72
73
74
  return(list(dds = dds,raw_counts=counts,countsNorm=countsNorm,target=target,design=design,normFactors = normFactorsOTU,CT_noNorm=CT_noNorm,CT_Norm=CT_Norm))
}


## Get the design according to the input
svolant's avatar
svolant committed
75
GetDesign <- function(input,target)
svolant's avatar
svolant committed
76
{
77
78
  design = NULL
  Interaction = NULL
svolant's avatar
svolant committed
79
  InterVar = input$InterestVar
svolant's avatar
svolant committed
80
81
82
83
84
85
  if(!any(!InterVar%in%colnames(target)))
  {
    if(length(InterVar)>1) Interaction = input$Interaction2
    alltmp = c(InterVar,Interaction)
    if(length(alltmp)>0) design = as.formula(paste("~", paste0(alltmp, collapse= "+")))
  }
svolant's avatar
svolant committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  return(design)
}



##############################################################
##
##          Contrasts
##
##############################################################

## Create the contrast base
BaseContrast <- function(input,names,namesfile)
{  
  
  v_tmp = c()
  filesize = file.info(namesfile)[,"size"]
  
  for(i in 1:length(names))
  {  
    Tinput = paste("input$",names[i],sep="")
    expr=parse(text=Tinput)
    val = eval(expr) 
    v_tmp[i] = as.numeric(val)
  }
  
  if(filesize!=0)
  { 
    oldContrast = read.table(namesfile,header=TRUE)
    colnamesTmp = c(colnames(oldContrast),input$ContrastName)
    mat = cbind(oldContrast,v_tmp)
  }
  else{ colnamesTmp = input$ContrastName; mat = v_tmp}
  
  write.table(mat,namesfile,row.names=FALSE,col.names = colnamesTmp)
}

## Create the contrast base with the buttons
BaseContrastEasy <- function(input,names,namesfile,target)
{  
  
  v_tmp = rep(0,length(names))
  filesize = file.info(namesfile)[,"size"]
  F1 = NULL
  nameContrast = ""
  
  ## Get the selected modalities
  M1 = input$Select1_contrast
  M2 = input$Select2_contrast
svolant's avatar
svolant committed
135

svolant's avatar
svolant committed
136
137
138
  if(length(input$Interaction2)>0) F1 = input$Select3_contrast
  ## Get the name of the parameter corresponding to the modalities
  InterVar = input$InterestVar
svolant's avatar
svolant committed
139
  if(length(InterVar)>1) {Sel_Var = InterVar[which(unlist(lapply(target[,InterVar],FUN = function(x){M1%in%x})))]} else Sel_Var = InterVar
svolant's avatar
svolant committed
140
141
  names1dds = paste(Sel_Var,M1,sep="")
  names2dds = paste(Sel_Var,M2,sep="")
svolant's avatar
svolant committed
142

svolant's avatar
svolant committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  ## fill the vector
  ind1 = which(names%in%names1dds)
  ind2 = which(names%in%names2dds)
  
  if(length(ind1)>0) v_tmp[ind1] = 1
  if(length(ind2)>0) v_tmp[ind2] = -1
  
  
  nameContrast = paste(M1,"_vs_",M2,sep="")
  
  if(F1!="All" && !is.null(F1)){
    Sel_Var_For = InterVar[which(unlist(lapply(target[,InterVar],FUN = function(x){F1%in%x})))]
    ## Depends on the interation
    namesfor1 = paste(Sel_Var,M1,".",Sel_Var_For,F1,sep="")
    namesfor1.1 = paste(Sel_Var_For,F1,".",Sel_Var,M1,sep="")
    namesfor2 = paste(Sel_Var,M2,".",Sel_Var_For,F1,sep="")
    namesfor2.1 = paste(Sel_Var_For,F1,".",Sel_Var,M2,sep="")
    ind1.for = which(names%in%c(namesfor1,namesfor1.1))
    ind2.for = which(names%in%c(namesfor2,namesfor2.1))
    if(length(ind1.for)>0) v_tmp[ind1.for] = 1
    if(length(ind2.for)>0) v_tmp[ind2.for] = -1
    nameContrast = paste(nameContrast,"_for_",F1,sep="")
  }
  
  if(filesize!=0)
  { 
    oldContrast = read.table(namesfile,header=TRUE)
    colnamesTmp = c(colnames(oldContrast),nameContrast)
    mat = cbind(oldContrast,v_tmp)
  }
  else{ colnamesTmp = nameContrast; mat = v_tmp}
  
  write.table(mat,namesfile,row.names=FALSE,col.names = colnamesTmp)
}


## Print the contrasts
PrintContrasts <- function (coefs, contrasts,contnames) 
{
  contrasts = as.matrix(contrasts)
  out <-""
  
  for (i in 1:ncol(contrasts)) 
  {
    contrast <- contrasts[,i]
    contrast <- paste(ifelse(contrast > 0, "+ ", ""), contrast, sep = "")
    contrast <- gsub("( 1)|(1)", "", contrast)
    out = paste(out,paste("<b>",contnames[i], "</b> <br/>", paste(contrast[contrast != 0], coefs[contrast != 0], collapse = " ", sep = " ")),"<br/>")
  }
  return(out)
  
}

196
197
198
199
## Check the rank of the matrix for the statistical model.
CheckMatrixRank <- function(design,target)
{
  testRank = FALSE
svolant's avatar
svolant committed
200

201
202
203
204
205
206
207
208
209
210
211
212
213
  if(!is.null(design) && length(as.character(design))) 
  {
    tmp = strsplit(as.character(design)[2],"[ ]*[+][ ]*")
    testDesign = any(unlist(tmp)%in%colnames(target))
  }
  
  if(nrow(target)>=1 && ncol(target)>=2 && nrow(target)>1 && !is.null(design) && testDesign){
    modelMatrix <- model.matrix(design, data = as.data.frame(target))
    rk = qr(modelMatrix)$rank
    testRank = (ncol(modelMatrix)==rk)
  }
    return(testRank)
}
svolant's avatar
svolant committed
214
215