Browse code

fixed cellMatchR

Genevieve Stein-O'Brien authored on 16/04/2018 19:17:21
Showing 3 changed files

... ...
@@ -27,7 +27,6 @@ cellMatchR <- function(Atot,nSets, cnt, minNS=NULL, maxNS=NULL, ignore.NA=FALSE,
27 27
   }}
28 28
   if(ignore.NA==TRUE){Atot<-Atot[complete.cases(Atot),]}
29 29
 
30
-
31 30
 corcut<-function(Atot,minNS,cnt,cluster.method){
32 31
   corr.dist=cor(Atot)
33 32
   corr.dist=1-corr.dist
... ...
@@ -55,30 +54,35 @@ corcut<-function(Atot,minNS,cnt,cluster.method){
55 54
             RtoMeanPattern[[i]] <- sapply(1:nIN,function(j) {round(cor(x=Atot[,cut==i][,j],y=cMNs[,i]),3)})
56 55
         }
57 56
       }
58
-    PByClust[sapply(PByClust,is.null)]<-NULL
57
+    AByClust[sapply(AByClust,is.null)]<-NULL
59 58
     RtoMeanPattern[sapply(RtoMeanPattern,is.null)]<-NULL
60 59
     return(list("RtoMeanPattern"=RtoMeanPattern,"AByClust"=AByClust))
61 60
   }   
62
-
63 61
   cc<-corcut(Atot,minNS,cnt,cluster.method)
64 62
 
65 63
     ### split by maxNS
66
-    indx<-which(unlist(lapply(cc$PByClust,function(x) dim(x)[1]>maxNS)))
67
-    while(length(indx)>0){
68
-          icc<-corcut(cc$AByClust[[indx[1]]],minNS,2,cluster.method)
69
-          cc$AByClust[[indx[1]]]<-icc[[2]][[2]]
70
-          cc$RtoMeanPattern[[indx[1]]]<-icc[[1]][[2]]
71
-          if(length(icc[[2]])>1){
72
-                cc$AByClust<-append(cc$AByClust,icc[[2]][1])
73
-                cc$RtoMeanPattern<-append(cc$RtoMeanPattern,icc[[1]][1])
74
-          }
75
-      indx<-which(unlist(lapply(cc$PByClust,function(x) dim(x)[1]>maxNS)))
64
+    indx<-which(unlist(lapply(cc$AByClust,function(x) dim(x)[1]>maxNS)))
65
+    i<-1
66
+    while(length(indx)>0){ 
67
+            icc<-corcut(cc$AByClust[[indx[1]]],minNS,2,cluster.method)
68
+            if(length(icc[[2]])==0){
69
+              indx<-indx[-1]
70
+              next
71
+            } else{
72
+              cc$AByClust[[indx[1]]]<-icc[[2]][[1]]
73
+              cc$RtoMeanPattern[[indx[1]]]<-icc[[1]][[1]]
74
+              if(length(icc[[2]])>1){
75
+                cc$AByClust<-append(cc$AByClust,icc[[2]][2])
76
+                cc$RtoMeanPattern<-append(cc$RtoMeanPattern,icc[[1]][2])
77
+              } 
78
+              indx<-which(unlist(lapply(cc$AByClust,function(x) dim(x)[1]>maxNS)))
79
+            }
76 80
     }
77 81
 
82
+
78 83
 #weighted.mean(AByClustDrop[[1]],RtoMPDrop[[1]])
79 84
 AByCDSWavg<- t(sapply(1:length(cc$AByClust),function(z) apply(cc$AByClust[[z]],1,function(x) weighted.mean(x,(cc$RtoMeanPattern[[z]])^3))))
80 85
 rownames(AByCDSWavg) <- lapply(1:length(cc$AByClust),function(x) paste("Pattern",x))
81
-
82 86
 #scale As
83 87
 Amax <- apply(AByCDSWavg,1,max)
84 88
 AByCDSWavgScaled <- t(sapply(1:dim(AByCDSWavg)[1],function(x) AByCDSWavg[x,]/Amax[x]))
... ...
@@ -7,14 +7,14 @@
7 7
 #' @param nSets number of sets to partition the data into
8 8
 #' @param simulationName name used to identify files created by this simulation
9 9
 #' @param samplingRatio vector of relative quantities to use for sampling celltypes
10
-#' @param annotionObj vector of same length as number of columns of D 
10
+#' @param anotionObj vector of same length as number of columns of D 
11 11
 #' @param path character string indicating were to save resulting data objects. default is current working dir
12 12
 #' @return simulationName used to identify saved files
13 13
 #' @examples
14 14
 #' data(SimpSim)
15 15
 #' createscCoGAPSSets(SimpSim.D, SimpSim.S, nSets=2, "example")
16 16
 #' @export
17
-createscCoGAPSSets <- function(D, nSets, simulationName,samplingRatio=NULL,path="",annotionObj=NULL)
17
+createscCoGAPSSets <- function(D, nSets, simulationName,samplingRatio=NULL,path="",anotionObj=NULL)
18 18
 {
19 19
     # check gene names
20 20
     if (length(unique(colnames(D))) != length(colnames(D)))
... ...
@@ -34,8 +34,8 @@ createscCoGAPSSets <- function(D, nSets, simulationName,samplingRatio=NULL,path=
34 34
             cellset <- sample(cells, sampleSize, replace=FALSE)
35 35
             cells <- cells[!(cells %in% cellset)]
36 36
         } else {
37
-        if(length(unique(annotionObj))!=length(samplingRatio)){warning("Not all celltypes will be sampled from.")}
38
-        ct.indx<-lapply(unique(annotionObj),function(x) which(annotionObj == x))
37
+        if(length(unique(anotionObj))!=length(samplingRatio)){warning("Not all celltypes will be sampled from.")}
38
+        ct.indx<-lapply(unique(anotionObj),function(x) which(anotionObj == x))
39 39
         cellset<-sample(colnames(D)[ct.indx[[x]]], samplingRatio[x],replace=TRUE)
40 40
         }
41 41
 
... ...
@@ -8,17 +8,15 @@
8 8
 postFixed4SC <- function(AP.fixed, setAs)
9 9
 {
10 10
     ASummary <- AP.fixed[[1]]$Amean
11
-
12
-
13 11
     PSummary <- do.call(cbind,lapply(AP.fixed, function(x) x$Pmean))
14 12
     Psd <- do.call(cbind,lapply(AP.fixed, function(x) x$Psd))
15 13
 
16
-    Pmax <- apply(PSummary,2,max)
17
-    Pneu <- sweep(PSummary,2,Pmax,FUN="/")
18
-    Aneu <- sweep(ASummary,1,Pmax,FUN="*")
14
+    Amax <- apply(ASummary,2,max)
15
+    Aneu <- sweep(ASummary,2,Amax,FUN="/")
16
+    Pneu <- sweep(PSummary,1,Amax,FUN="*")
19 17
 
20
-    X <- apply(Pneu,2,range)
21
-    Y <- apply(setPs,2,range)
18
+    X <- apply(Aneu,2,range)
19
+    Y <- apply(setAs,2,range)
22 20
     colnames(X) <- colnames(Y)
23 21
     if (all.equal(X,Y,tolerance=0.01) != TRUE)
24 22
         warning("Patterns do not match fixed values.")