Skip to content

Commit e7084b0

Browse files
committed
-added fgsea
1 parent e05b698 commit e7084b0

File tree

3 files changed

+105
-4
lines changed

3 files changed

+105
-4
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export(annSetUp)
55
export(butter_classGenes)
66
export(butter_classify)
77
export(cn_correctZmat)
8+
export(compileGSEA)
89
export(corplot_sub)
910
export(corplot_triangle)
1011
export(cutTreeToNames)
@@ -15,6 +16,7 @@ export(easy_assess)
1516
export(enrDiff)
1617
export(expression)
1718
export(extractImport)
19+
export(fgsea.wrapper.set)
1820
export(findVarGenes)
1921
export(find_genes_byGo)
2022
export(find_top_pc)

R/annotation.R

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,75 @@ ks.wrapper.set<-function(
8686
ansList
8787
}
8888

89+
# fgsea.wrapper.set
90+
91+
#' run fgsea on list of diffExprs
92+
#'
93+
#' find gene sets enriched in provided ranked gene list
94+
#'
95+
#' @param diffExprList named list of diffExpr results, for example from running gnrAll()
96+
#' @param geneSets named list of gene sets
97+
#' @param minSize defaults to 10
98+
#' @param nPerm defaults to 1e-4
99+
#'
100+
#' @return list of and (2) list of ks.wrapper results
101+
#'
102+
#' @export
103+
#'
104+
fgsea.wrapper.set<-function(
105+
diffExprList,
106+
geneSets,
107+
minSize=10,
108+
nPerm=1e4)
109+
{
110+
111+
112+
ansList<-list()
113+
qnames<-names(diffExprList)
114+
for(qname in qnames){
115+
qdiffRes<-diffExprList[[qname]]
116+
teststat<-qdiffRes$cval
117+
names(teststat)<-rownames(qdiffRes)
118+
fRes<-as.data.frame(fgsea(pathways=geneSets, stats=teststat, minSize=minSize, nperm=nPerm))
119+
ansList[[qname]]<-fRes
120+
}
121+
ansList
122+
}
123+
124+
125+
#' compileGSEA
126+
#'
127+
#' make a long table from fgsea.wrapper.set
128+
#'
129+
#' make a long table from fgsea.wrapper.set
130+
#'
131+
#' @param gseaEnr from rnunning gsea.wrapper.set
132+
#' @param thresh defaults 0.05
133+
#'
134+
#' @return df df
135+
#'
136+
#' @export
137+
#'
138+
compileGSEA<-function(
139+
gseaEnr,
140+
thresh=0.05){
141+
142+
longX<-data.frame()
143+
for(xname in names(gseaEnr)){
144+
tmpDF<-gseaEnr[[xname]]
145+
tmpDF<-cbind(tmpDF, cluster=rep(xname, nrow(tmpDF)))
146+
tmpDF<-tmpDF[order(tmpDF$NES, decreasing=TRUE),]
147+
xi<-which(tmpDF$padj<thresh)
148+
sig_gs<-rep(0, nrow(tmpDF))
149+
sig_gs[xi]<-1
150+
tmpDF<-cbind(tmpDF, isSig=sig_gs)
151+
longX<-rbind(longX, tmpDF)
152+
}
153+
#longX<-longX[longX$padj<thresh,]
154+
longX<-cbind(longX, lpval=log10(longX$padj))
155+
longX[longX$NES>0,]$lpval<- -1 * longX[longX$NES>0,]$lpval
156+
longX
157+
}
89158

90159
# ks.extract
91160

@@ -325,3 +394,5 @@ ks.test.2 <- function (x, y, ..., alternative = c("two.sided", "less", "greater"
325394

326395

327396

397+
398+

R/wash.R

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ prewash<-function
7474
}
7575

7676

77-
77+
if(FALSE){
7878
#' weighted subtraction from mapped reades
7979
#'
8080
#' Simulate expression profile of _total_ mapped reads
@@ -90,12 +90,39 @@ total=1e5){
9090

9191
totalSignal<-sum(vector)
9292
wAve<-vector/totalSignal
93-
resid<-sum(vector)-total #num to subtract from sample
93+
### resid<-sum(vector)-total #num to subtract from sample
94+
resid<-totalSignal-total #num to subtract from sample
9495
residW<-wAve*resid # amount to substract from each gene
9596
ans<-vector-residW
9697
ans[which(ans<0)]<-0
9798
ans
9899
}
100+
}
101+
102+
#' weighted subtraction from mapped reades
103+
#'
104+
#' Simulate expression profile of _total_ mapped reads
105+
#' @param vector of total mapped reads per gene/transcript
106+
#' @param total post transformation sum of read counts
107+
#'
108+
#' @return vector of downsampled read mapped to genes/transcripts
109+
#'
110+
#' @export
111+
downSampleW<-function
112+
(vector,
113+
total=1e5,
114+
dThresh=0){
115+
116+
totalSignal<-sum(vector)
117+
wAve<-vector/totalSignal
118+
### resid<-sum(vector)-total #num to subtract from sample
119+
resid<-totalSignal-total #num to subtract from sample
120+
residW<-wAve*resid # amount to substract from each gene
121+
ans<-vector-residW
122+
ans[which(ans<dThresh)]<-0
123+
ans
124+
}
125+
99126

100127
#' weighted subtraction from mapped reades, applied to all
101128
#'
@@ -108,9 +135,10 @@ total=1e5){
108135
#' @export
109136
weighted_down<-function
110137
(expRaw,
111-
total
138+
total,
139+
dThresh=0
112140
){
113-
expCountDnW<-apply(expRaw, 2, downSampleW, total)
141+
expCountDnW<-apply(expRaw, 2, downSampleW, total=total, dThresh=dThresh)
114142
#log(1+expCountDnW)
115143
expCountDnW
116144
}

0 commit comments

Comments
 (0)