annotate SMART/DiffExpAnal/DESeqTools/pairwiseSERE.R @ 31:0ab839023fe4

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
parents 94ab73e8a190
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
1 # pairwiseSERE
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
2 # compute pairwise SERE statistics
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 # input : counts
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 # output : matrix of SERE values
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7 # created october 19th, 2012
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8 # Marie-Agnes Dillies
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11 pairwiseSERE <- function( counts ){
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 sere <- matrix( NA, ncol=ncol(counts), nrow=ncol(counts) )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 for (i in 1:ncol(counts)){
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 for (j in 1:ncol(counts)){
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 sere[i,j] <- sigfun_Pearson( counts[,c(i,j)] )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 colnames(sere) <- rownames(sere) <- colnames(counts)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20 return( formatC(sere, format="f", digits=2) )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23 sigfun_Pearson <- function(observed) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24 #calculate lambda and expected values
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 laneTotals<- colSums(observed);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 total <- sum(laneTotals)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27 fullObserved <- observed[rowSums(observed)>0,];
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 fullLambda <- rowSums(fullObserved)/total;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 fullLhat <- fullLambda > 0;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 fullExpected<- outer(fullLambda, laneTotals);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32 #keep values
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 fullKeep <- which(fullExpected > 0);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 #calculate degrees of freedom (nrow*(ncol -1) >> number of parameters - calculated (just lamda is calculated >> thats why minus 1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 #calculate pearson and deviance for all values
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 oeFull <- (fullObserved[fullKeep] - fullExpected[fullKeep])^2/ fullExpected[fullKeep] # pearson chisq test
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 dfFull <- length(fullKeep) - sum(fullLhat!=0);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 return(c(sqrt(sum(oeFull)/dfFull)));
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 }