### R code from vignette source 'vignettes/FGNet/inst/doc/FGNet-vignette.Rnw'

###################################################
### code chunk number 1: FGNet-vignette.Rnw:33-35
###################################################
library(knitr)
opts_chunk$set(concordance=TRUE)


###################################################
### code chunk number 2: FGNet-vignette.Rnw:102-104 (eval = FALSE)
###################################################
## source("http://bioconductor.org/biocLite.R")
## biocLite("FGNet")


###################################################
### code chunk number 3: FGNet-vignette.Rnw:141-142
###################################################
library(FGNet)


###################################################
### code chunk number 4: FGNet-vignette.Rnw:144-146 (eval = FALSE)
###################################################
## library(FGNet)
## FGNet_GUI()


###################################################
### code chunk number 5: FGNet-vignette.Rnw:156-162 (eval = FALSE)
###################################################
## geneExpr <- c("YBL084C", "YDL008W", "YDR118W", "YDR301W", "YDR448W", 
##     "YFR036W", "YGL240W", "YHR166C", "YKL022C", "YLR102C", "YLR115W", 
##     "YLR127C", "YNL172W", "YOL149W", "YOR249C")
## geneExpr <- setNames(c(rep(1,10),rep(-1,5)), geneExpr)
## 
## FGNet_GUI(geneExpr)


###################################################
### code chunk number 6: FGNet-vignette.Rnw:176-177 (eval = FALSE)
###################################################
## ?FGNet_report


###################################################
### code chunk number 7: FGNet-vignette.Rnw:204-216
###################################################
library(FGNet)
genesMetabolism <- c("YGR175C", "YHR007C", "YMR202W", "YJL167W", 
"YNL280C", "YGR060W", "YGL001C", "YLR100W", "YLR056W", "YGL012W", 
"YMR015C", "YML008C", "YHR072W", "YHR190W", "YKL004W", "YBR036C", 
"YDR294C", "YDR072C", "YKL008C", "YHL003C", "YMR296C", "YDR062W", 
"YJL134W", "YOR171C", "YLR260W", "YMR298W", "YMR272C", "YPL057C",
"YDR297W", "YBR265W", "YPL087W", "YBR183W", "YKR053C")
# To add the gene label/symbol to the plots...
library(org.Sc.sgd.db)
geneLabels <- unlist(as.list(org.Sc.sgdGENENAME)[genesMetabolism])
# To add gene expression color to the plots...
gMetabolismExpr <- setNames(rnorm(33,mean=0,sd=3), geneLabels)


###################################################
### code chunk number 8: FGNet-vignette.Rnw:219-221 (eval = FALSE)
###################################################
## results_David <- fea_david(genesMetabolism, geneLabels=geneLabels, 
##     email="example@email.com")


###################################################
### code chunk number 9: FGNet-vignette.Rnw:229-231 (eval = FALSE)
###################################################
## results_topGO <- fea_topGO(genesMetabolism, geneIdType="ENSEMBL", 
##     geneLabels=geneLabels, organism="Sc") 


###################################################
### code chunk number 10: FGNet-vignette.Rnw:238-248
###################################################
genesYeast <- c("ADA2", "APC1", "APC11", "APC2", "APC4", "APC5", "APC9", "CDC16", 
    "CDC23", "CDC26", "CDC27", "CFT1", "CFT2", "DCP1", "DOC1", "FIP1", 
    "GCN5", "GLC7", "HFI1", "KEM1", "LSM1", "LSM2", "LSM3", "LSM4", 
    "LSM5", "LSM6", "LSM7", "LSM8", "MPE1", "NGG1", "PAP1", "PAT1", 
    "PFS2", "PTA1", "PTI1", "REF2", "RNA14", "RPN1", "RPN10", "RPN11", 
    "RPN13", "RPN2", "RPN3", "RPN5", "RPN6", "RPN8", "RPT1", "RPT3", 
    "RPT6", "SGF11", "SGF29", "SGF73", "SPT20", "SPT3", "SPT7", "SPT8", 
    "TRA1", "YSH1", "YTH1")
# Optional: Gene expression (1=UP, -1=DW)
genesYeastExpr <- setNames(c(rep(1,29), rep(-1,30)),genesYeast) 


###################################################
### code chunk number 11: FGNet-vignette.Rnw:250-251 (eval = FALSE)
###################################################
## jobID <- fea_gtLinker(geneList=genesYeast,organism="Sc")


###################################################
### code chunk number 12: FGNet-vignette.Rnw:255-257
###################################################
jobID <- 3907019
results_gtLinker <- fea_gtLinker_getResults(jobID=jobID, organism="Sc")


###################################################
### code chunk number 13: FGNet-vignette.Rnw:263-269 (eval = FALSE)
###################################################
## library(gage); data(gse16873)
## results_gage <- fea_gage(eset=gse16873, 
##     refSamples=grep('HN',colnames(gse16873), ignore.case =T), 
##     compSamples=grep('DCIS',colnames(gse16873), ignore.case=T), 
##     geneIdType="ENTREZID", organism="Hs", annotations="REACTOME")
## FGNet_report(results_gage)


###################################################
### code chunk number 14: FGNet-vignette.Rnw:276-277 (eval = FALSE)
###################################################
## ?format_results()


###################################################
### code chunk number 15: FGNet-vignette.Rnw:303-305 (eval = FALSE)
###################################################
## results <- format_david("http://david.abcc.ncifcrf.gov/data/download/90128.txt")
## results <- fea_gtLinker_getResults(jobID=3907019)


###################################################
### code chunk number 16: FGNet-vignette.Rnw:313-317 (eval = FALSE)
###################################################
## FGNet_report(results_topGO, geneExpr=gMetabolismExpr)  
## FGNet_report(results_David, geneExpr=gMetabolismExpr) 
## FGNet_report(results_gtLinker, geneExpr=genesYeastExpr)
## FGNet_report(results_gage)


###################################################
### code chunk number 17: FGNet-vignette.Rnw:321-323 (eval = FALSE)
###################################################
## data(FEA_tools)
## FEA_tools


###################################################
### code chunk number 18: FGNet-vignette.Rnw:325-326 (eval = FALSE)
###################################################
## FGNet_report(results_gtLinker, filterThreshold=0.3)


###################################################
### code chunk number 19: FGNet-vignette.Rnw:328-329 (eval = FALSE)
###################################################
## ?FGNet_report


###################################################
### code chunk number 20: FGNet-vignette.Rnw:335-338
###################################################
results <- results_gtLinker
incidMat <- fea2incidMat(results)
incidMat_terms <- fea2incidMat(results, key="Terms")


###################################################
### code chunk number 21: FGNet-vignette.Rnw:342-344
###################################################
functionalNetwork(incidMat, geneExpr=genesYeastExpr,
    plotTitleSub="Default gene view")


###################################################
### code chunk number 22: FGNet-vignette.Rnw:346-347
###################################################
getTerms(results)[1]


###################################################
### code chunk number 23: FGNet-vignette.Rnw:351-352 (eval = FALSE)
###################################################
## functionalNetwork(incidMat_terms, plotOutput="dynamic", vLabelCex=0.4)


###################################################
### code chunk number 24: FGNet-vignette.Rnw:355-356
###################################################
functionalNetwork(incidMat_terms, plotTitleSub="Terms network", legendText=FALSE)


###################################################
### code chunk number 25: FGNet-vignette.Rnw:359-361
###################################################
functionalNetwork(incidMat_terms, plotType="bipartite",
    plotTitleSub="Terms in several metagroups")


###################################################
### code chunk number 26: FGNet-vignette.Rnw:367-369
###################################################
jobID <- 1639610
feaAlzheimer <- fea_gtLinker_getResults(jobID=jobID, organism="Hs")


###################################################
### code chunk number 27: FGNet-vignette.Rnw:373-374
###################################################
names(feaAlzheimer)


###################################################
### code chunk number 28: FGNet-vignette.Rnw:377-378
###################################################
head(feaAlzheimer$metagroups)


###################################################
### code chunk number 29: FGNet-vignette.Rnw:382-383
###################################################
getTerms(feaAlzheimer)[3:4]


###################################################
### code chunk number 30: FGNet-vignette.Rnw:394-395
###################################################
incidMat <- fea2incidMat(feaAlzheimer)


###################################################
### code chunk number 31: FGNet-vignette.Rnw:397-399
###################################################
head(incidMat$metagroupsMatrix)
incidMat$gtSetsMatrix[1:5, 14:18]


###################################################
### code chunk number 32: FGNet-vignette.Rnw:403-405 (eval = FALSE)
###################################################
## data(FEA_tools)
## FEA_tools


###################################################
### code chunk number 33: FGNet-vignette.Rnw:407-409
###################################################
incidMatFiltered <- fea2incidMat(feaAlzheimer, 
    filterAttribute="Silhouette Width", filterOperator="<", filterThreshold=0.2)


###################################################
### code chunk number 34: FGNet-vignette.Rnw:413-414 (eval = FALSE)
###################################################
## incidMatFiltered$filteredOut


###################################################
### code chunk number 35: FGNet-vignette.Rnw:423-426
###################################################
# (Fake expression data)
geneList <- rownames(incidMat$metagroupsMatrix)
geneExpr <- setNames(c(rep(1,40), rep(-1,37)), geneList)


###################################################
### code chunk number 36: FGNet-vignette.Rnw:430-432
###################################################
fNw <- functionalNetwork(incidMatFiltered, geneExpr=geneExpr, 
    keepColors=FALSE)


###################################################
### code chunk number 37: FGNet-vignette.Rnw:436-438 (eval = FALSE)
###################################################
## functionalNetwork(incidMatFiltered, geneExpr=geneExpr, plotOutput="dynamic") 
## fNw <- functionalNetwork(incidMatFiltered, plotOutput="none") 


###################################################
### code chunk number 38: FGNet-vignette.Rnw:442-447
###################################################
names(fNw)
names(fNw$iGraph)
library(igraph)
clNw <- fNw$iGraph$commonClusters
clNw


###################################################
### code chunk number 39: FGNet-vignette.Rnw:449-453 (eval = FALSE)
###################################################
## vcount(clNw)
## ecount(clNw)
## sort(betweenness(clNw), decreasing=TRUE)[1:10]
## igraph.to.graphNEL(clNw)


###################################################
### code chunk number 40: FGNet-vignette.Rnw:457-461 (eval = FALSE)
###################################################
## functionalNetwork(incidMatFiltered, plotOutput="dynamic") 
## # Modify the layout...
## saveLayout <- tkplot.getcoords(1)   # tkp.id (ID of the tkplot window)
## functionalNetwork(incidMatFiltered, vLayout=saveLayout)


###################################################
### code chunk number 41: FGNet-vignette.Rnw:467-470
###################################################
mgKeyTerm <- keywordsTerm(getTerms(feaAlzheimer), 
    nChar=100)[-c(as.numeric(incidMatFiltered$filteredOut))]
functionalNetwork(incidMatFiltered, plotType="bipartite", legendText=mgKeyTerm)


###################################################
### code chunk number 42: FGNet-vignette.Rnw:474-476
###################################################
functionalNetwork(incidMatFiltered, geneExpr=geneExpr, plotType="bipartite",
    keepAllNodes=TRUE, plotTitleSub="Bipartite network will all nodes")


###################################################
### code chunk number 43: FGNet-vignette.Rnw:483-484
###################################################
incidMatTerms <- fea2incidMat(feaAlzheimer, key="Terms")


###################################################
### code chunk number 44: FGNet-vignette.Rnw:486-488
###################################################
functionalNetwork(incidMatTerms, plotType="bipartite", 
    plotTitle="Terms in several metagroups")


###################################################
### code chunk number 45: FGNet-vignette.Rnw:492-493 (eval = FALSE)
###################################################
## functionalNetwork(incidMatTerms,  weighted=TRUE, plotOutput="dynamic")


###################################################
### code chunk number 46: FGNet-vignette.Rnw:497-501
###################################################
incidMatTerms <- fea2incidMat(feaAlzheimer$metagroups, clusterColumn="Metagroup", 
    key="Terms",
    filterAttribute="Silhouette.Width", filterThreshold=0.2)
functionalNetwork(incidMatTerms, legendText=FALSE, plotOutput="dynamic")


###################################################
### code chunk number 47: FGNet-vignette.Rnw:503-504
###################################################
functionalNetwork(incidMatTerms, legendText=FALSE)


###################################################
### code chunk number 48: FGNet-vignette.Rnw:509-515
###################################################
incidMatTerms <- fea2incidMat(feaAlzheimer, key="Terms", removeFilteredGtl=FALSE)
par(mfrow=c(1,2))
functionalNetwork(incidMatTerms, vLabelCex=0.2,
    plotTitle="Including filtered terms", legendText=FALSE)
functionalNetwork(incidMatTerms, plotType="bipartite", vLabelCex=0.4,
    plotTitle="Including filtered terms")


###################################################
### code chunk number 49: FGNet-vignette.Rnw:529-531
###################################################
txtFile <- paste(file.path(system.file('examples', package='FGNet')), "David_Metabolism.txt", sep=.Platform$file.sep)
feaMetabolism <- format_david(txtFile, jobName="David_Metabolism",geneLabels=geneLabels)


###################################################
### code chunk number 50: FGNet-vignette.Rnw:534-535 (eval = FALSE)
###################################################
## feaMetabolism <- fea_david(genesMetabolism, email="...", geneLabels=geneLabels)


###################################################
### code chunk number 51: FGNet-vignette.Rnw:537-540
###################################################
gtSets <- feaMetabolism$geneTermSets
gtSets <- gtSets[gtSets$Cluster %in% c(9),] 
gtSets <- gtSets[gtSets$Pop.Hits<500,]


###################################################
### code chunk number 52: FGNet-vignette.Rnw:544-549
###################################################
termsGenes <- t(fea2incidMat(gtSets, clusterColumn="Terms")$clustersMatrix)
library(R.utils)
rownames(termsGenes) <- sapply(strsplit(rownames(termsGenes), ":"), 
    function(x) capitalize(x[length(x)]))
termsGenes[1:5,1:5]


###################################################
### code chunk number 53: FGNet-vignette.Rnw:553-556
###################################################
functionalNetwork(t(termsGenes), plotType="bipartite", keepAllNodes=TRUE,
    legendPrefix="", plotTitle="Genes - Terms network", plotTitleSub="",
    geneExpr=gMetabolismExpr, plotExpression="Fill")


###################################################
### code chunk number 54: FGNet-vignette.Rnw:561-563
###################################################
functionalNetwork(termsGenes, plotType="bipartite", keepAllNodes=TRUE,
    legendPrefix="", plotTitle="Genes - Terms network", plotTitleSub="")


###################################################
### code chunk number 55: FGNet-vignette.Rnw:572-575 (eval = FALSE)
###################################################
## feaMetabolism <- fea_david(genesMetabolism, email="...", geneLabels=geneLabels)
## incidMat <- fea2incidMat(feaMetabolism)
## functionalNetwork(incidMat, legendText=FALSE) 


###################################################
### code chunk number 56: FGNet-vignette.Rnw:577-579
###################################################
incidMat <- fea2incidMat(feaMetabolism)
functionalNetwork(incidMat) 


###################################################
### code chunk number 57: FGNet-vignette.Rnw:582-583
###################################################
incidMatTerms <- fea2incidMat(feaMetabolism, key="Terms")


###################################################
### code chunk number 58: FGNet-vignette.Rnw:585-587 (eval = FALSE)
###################################################
## functionalNetwork(incidMatTerms$clustersMatrix, plotOutput="dynamic",
##   weighted=TRUE, eColor="grey")


###################################################
### code chunk number 59: FGNet-vignette.Rnw:589-591
###################################################
functionalNetwork(incidMatTerms$clustersMatrix, plotType="bipartite", 
 plotTitle="Terms in several clusters")


###################################################
### code chunk number 60: FGNet-vignette.Rnw:597-598
###################################################
colnames(feaMetabolism$clusters)


###################################################
### code chunk number 61: FGNet-vignette.Rnw:602-621
###################################################
par(mfrow=c(1,2))

# Highest enrichment score
filterProp <- as.numeric(as.character(
    feaMetabolism$clusters$ClusterEnrichmentScore))
quantile(filterProp, c(0.10, 0.25, 0.5, 0.75, 0.9))
incidMatFiltered <- fea2incidMat(feaMetabolism, 
    filterAttribute="ClusterEnrichmentScore",
    filterOperator="<", filterThreshold=10)
functionalNetwork(incidMatFiltered, eColor=NA,
    plotTitle="Highest enrichment score")

# Lowest genes
quantile(as.numeric(as.character(feaMetabolism$clusters$nGenes)),
    c(0.10, 0.25, 0.5, 0.75, 0.9))
incidMatFiltered <- fea2incidMat(feaMetabolism, 
 filterAttribute="nGenes", filterOperator=">", filterThreshold=15)
functionalNetwork(incidMatFiltered, eColor=NA,
    plotTitle="Smaller clusters")


###################################################
### code chunk number 62: FGNet-vignette.Rnw:628-632
###################################################
keywords <- c("sphingolipid") 
selectedClusters <- sapply(getTerms(feaMetabolism), 
    function(x) 
    any(grep(paste("(", paste(keywords, collapse="|") ,")",sep=""), tolower(x))))


###################################################
### code chunk number 63: FGNet-vignette.Rnw:634-635 (eval = FALSE)
###################################################
## getTerms(feaMetabolism)[selectedClusters]


###################################################
### code chunk number 64: FGNet-vignette.Rnw:637-642
###################################################
tmpFea <- feaMetabolism
tmpFea$clusters <- cbind(tmpFea$clusters, keywords=selectedClusters)
incidMatSelection <- fea2incidMat(tmpFea, 
 filterAttribute="keywords", filterOperator="!=",filterThreshold="TRUE")
functionalNetwork(incidMatSelection, plotType="bipartite", plotOutput="dynamic")


###################################################
### code chunk number 65: FGNet-vignette.Rnw:648-649
###################################################
distMat <- clustersDistance(incidMat)


###################################################
### code chunk number 66: FGNet-vignette.Rnw:653-661
###################################################
selectedClusters <- rep(FALSE, nrow(feaMetabolism$clusters))
selectedClusters[c(1,7,9)] <- TRUE

tmpFea <- feaMetabolism
tmpFea$clusters <- cbind(tmpFea$clusters, select=selectedClusters)
incidMatSelection <- fea2incidMat(tmpFea, 
  filterAttribute="select", filterOperator="!=",filterThreshold="TRUE")
functionalNetwork(incidMatSelection, eColor=NA)


###################################################
### code chunk number 67: FGNet-vignette.Rnw:669-672
###################################################
incidMatFiltered <- fea2incidMat(feaAlzheimer, 
    filterAttribute="Silhouette Width", filterOperator="<", filterThreshold=0.2)
stats <- analyzeNetwork(incidMatFiltered)


###################################################
### code chunk number 68: FGNet-vignette.Rnw:674-676
###################################################
names(stats)
stats$transitivity


###################################################
### code chunk number 69: FGNet-vignette.Rnw:681-682
###################################################
head(stats$betweenessMatrix)


###################################################
### code chunk number 70: FGNet-vignette.Rnw:686-687
###################################################
stats$hubsList$Global


###################################################
### code chunk number 71: FGNet-vignette.Rnw:690-691
###################################################
stats$hubsList$"9"


###################################################
### code chunk number 72: FGNet-vignette.Rnw:695-697
###################################################
incidMat_metab <- fea2incidMat(feaMetabolism)
analyzeNetwork(incidMat_metab)


###################################################
### code chunk number 73: FGNet-vignette.Rnw:703-705
###################################################
goIds <- getTerms(feaMetabolism, returnValue="GO")[[10]]
plotGoAncestors(goIds, ontology="MF")


###################################################
### code chunk number 74: FGNet-vignette.Rnw:708-714 (eval = FALSE)
###################################################
## genesAlz <- rownames(fea2incidMat(feaAlzheimer)$metagroupsMatrix)
## genesAlzExpr <- setNames(c(rep(1,50), rep(-1,27)),genesAlz) 
## 
## keggIds <- getTerms(feaAlzheimer, returnValue="KEGG")[[3]]
## plotKegg("hsa05010", geneExpr=genesAlzExpr, geneIDtype="GENENAME")
## # Saved as .png in current directory