ghlist contains two manually gated samples (from two centers).

ghlist
## [[1]]
## Sample:  sample 1 
## GatingHierarchy with  24  gates
## 
## 
## [[2]]
## Sample:  sample 2 
## GatingHierarchy with  24  gates

plot the popluations of interest

pops <- c("cd4", "cd8")
for(gh in ghlist)
  print(plotGate(gh, pops))

## NULL

## NULL

We want to match these two populations across two samples:

convert the data to data.frame so that they can be read by flowMap

matlist <- lapply(ghlist, function(gh){
  matlist<- lapply(seq_along(pops), function(i){
              mat <- data.table(exprs(getData(gh, pops[i])))
              mat[, id := i]
            })
  rbindlist(matlist)

})

to make life easy for flowMap, we pre-filter out those extreme values for sample 2

sam1 <- matlist[[1]]
sam2 <- matlist[[2]]
sam2 <- sam2[(CD4 >0 & CD4 <1500 & CD8 > 3e3)|(CD4 >= 1500 & CD8 > 1e3), ]

#only use CD4 CD8 dimensions
sam1 <- data.frame(sam1)[, c("CD4", "CD8", "id")]
sam2 <- data.frame(sam2)[, c("CD4", "CD8", "id")]

mat1 = sam1[sam1$id==1,]
mat2 = sam2[sam2$id==2,]

# combine events from the two cell populations to 
# make pooled data
mat = rbind(mat1,mat2)

Verify these two pops are indeed well seperated.

xyplot(`CD4`~`CD8`, mat)

Then try minimum spinning tree on two different pops. (using the code from flowMap vignette)

library(flowMap)
# sample 100 events from the pooled data
sampleSize = 100

# among the 100 events, sample events from the two cell populations
# such that the ratio of the cell population membership is the same
# as that in the pooled data
nn1 = round(sampleSize*table(mat$id)[1]/nrow(mat))
nn2 = round(sampleSize*table(mat$id)[2]/nrow(mat))
submat = rbind(mat1[sample(nrow(mat1),nn1),],mat2[sample(nrow(mat2),nn2),])
submat <- data.frame(submat)
colnames(submat)[3] = "sam"

str(submat)
## 'data.frame':    100 obs. of  3 variables:
##  $ CD4: num  2538 2825 2754 2716 2605 ...
##  $ CD8: num  829 926 891 617 962 ...
##  $ sam: int  1 1 1 1 1 1 1 1 1 1 ...
# plot MST of the 100 events
g1 = makeFRMST(submat)
par(mar=c(0,0,0,0))
plot(g1$g,vertex.label.cex=0.01,
    layout=layout.fruchterman.reingold(g1$g))

Pretty good, then try MST on the same population (cd4),

mat1 = sam1[sam1$id==1,]
mat2 = sam2[sam2$id==1,]
mat2$id = 2
# combine events from the two cell populations to 
# make pooled data
mat = rbind(mat1,mat2)

Knowing that these two pops are pretty much overlapped

xyplot(`CD4`~`CD8`, mat)

# sample 100 events from the pooled data
sampleSize = 100

# among the 100 events, sample events from the two cell populations
# such that the ratio of the cell population membership is the same
# as that in the pooled data
nn1 = round(sampleSize*table(mat$id)[1]/nrow(mat))
nn2 = round(sampleSize*table(mat$id)[2]/nrow(mat))
submat = rbind(mat1[sample(nrow(mat1),nn1),],mat2[sample(nrow(mat2),nn2),])
submat <- data.frame(submat)
colnames(submat)[3] = "sam"

# plot MST of the 100 events
g1 = makeFRMST(submat)
par(mar=c(0,0,0,0))
plot(g1$g,vertex.label.cex=0.01,
    layout=layout.fruchterman.reingold(g1$g))

###Dots are not as quite evenly distributed as expected.

res1 = getFRest(sam1,sam2,sampleMethod="proportional",sampleSize=100,
    ndraws=100,estStat="median",ncores=NULL)
res1@ww
##           1         2
## 1 -6.429404 -9.823640
## 2 -9.868873 -4.946072
library(gplots)
par(mar=c(0,0,0,0))
heatmapCols <- colorRampPalette(c("red","yellow","white","blue"))(50)
heatmap.2(res1@ww,trace="none",col=heatmapCols,symm=FALSE,dendrogram="none",
    Rowv=FALSE,Colv=FALSE,xlab="Sample 2",ylab="Sample 1")

par(mar=c(0,0,0,0))
heatmapCols <- colorRampPalette(c("red","yellow","white","blue"))(50)
heatmap.2(res1@pNorm,trace="none",col=heatmapCols,symm=FALSE,dendrogram="none",
    Rowv=FALSE,Colv=FALSE,xlab="Sample 2",ylab="Sample 1")

hist(res1@pNorm,xlab="log10 p-value histogram",main="")

resMulti = makeDistmat(samples=list(sam1,sam2),sampleSize=100,ndraws=100)
require(gplots)
par(mar=c(0,0,0,0))
heatmapCols <- colorRampPalette(c("red","yellow","white","blue"))(50)
heatmap.2(resMulti$distmat,trace="none",col=heatmapCols,symm=FALSE,dendrogram="none",
    Rowv=FALSE,Colv=FALSE)