Commit 1a448020 authored by mahendra-mariadassou's avatar mahendra-mariadassou

Refactored data16S() by defining internal functions.

parent ab6113a2
## High level Helper functions
.import_biom <- function(input) {
## Select appropriate import_function
import_function <- switch(input$biomFormat,
"std" = import_biom,
"frogs" = import_frogs)
## Import
return(import_function(input$fileBiom$datapath, ## biom file
input$fileTree$datapath ## tree file
))
}
.import_sample_data <- function(input, physeq) {
## Unhappy path
if (is.null(input$fileMeta)) {
return(data.frame(SampleID = sample_names(physeq) , row.names = sample_names(physeq)))
}
## Happy path: excel version
if (input$CSVsep == "excel") {
## For efficiency and consistency, maybe replace with readr::read_xl
sdf <- RcmdrMisc::readXL(input$fileMeta$datapath,
rownames = TRUE,
header = TRUE)
sdf$SampleID <- rownames(sdf)
return(sdf)
}
## Happy path: csv version
sdf <- read.csv(
input$fileMeta$datapath,
header = TRUE,
sep = input$CSVsep,
row.names = 1,
na.strings = NA
)
sdf$SampleID <- rownames(sdf)
return(sdf)
}
.format_tax_table <- function(tdf) {
## explicit rank names
colnames(tdf) <- c("Kingdom", "Phylum", "Class", "Order",
"Family", "Genus", "Species", "Strain")[1:ncol(tdf)]
## Replace unknown by NA
tdf[grep("unknown ", tdf)] <- NA
#tdf[grep("Unclassified", tdf)] <- NA
return(tdf)
}
\ No newline at end of file
......@@ -49,60 +49,28 @@ shinyServer
"https://raw.githubusercontent.com/mahendra-mariadassou/phyloseq-extended/master/R/load-extra-functions.R"
})
source("internals.R")
data16S <- reactive({
## BIOM input
if (input$dataset == "input")
{
## Unhappy path
if (is.null(input$fileBiom))
{
return()
}
if (input$biomFormat == "std")
{
d <- import_biom(
BIOMfilename = input$fileBiom$datapath,
treefilename = input$fileTree$datapath# ,
# refseqfilename = input$fileSeq$datapath
)
} else if (input$biomFormat == "frogs") {
d <- import_frogs(
biom = input$fileBiom$datapath,
treefilename = input$fileTree$datapath# ,
# refseqfilename = input$fileSeq$datapath
)
}
colnames(tax_table(d)) <-
c("Kingdom",
"Phylum",
"Class",
"Order",
"Family",
"Genus",
"Species",
"Strain")[1:length(rank_names(d))]
tax_table(d)[grep("unknown ", tax_table(d))] <- NA
#tax_table(d)[grep("Unclassified", tax_table(d))] <- NA
if (!is.null(input$fileMeta)) {
if (input$CSVsep == "excel") {
sample_data(d) <-
RcmdrMisc::readXL(input$fileMeta$datapath,
rownames = TRUE,
header = TRUE)
} else {
sample_data(d) <- read.csv(
input$fileMeta$datapath,
header = TRUE,
sep = input$CSVsep,
row.names = 1,
na.strings = NA
)
sample_data(d)$SampleID <- rownames(sample_data(d))
}
} else {
n <- data.frame(sample_names(d) , row.names = sample_names(d))
names(n) <- "SampleID"
sample_data(d) <- n
}
## Happy path
## Import biom
d <- .import_biom(input)
## Format tax table
tax_table(d) <- .format_tax_table(tax_table(d))
## import metadata and store it in phyloseq object
sample_data(d) <- .import_sample_data(input, d)
## Rarefy data
if (input$rareData) {
d <- rarefy_even_depth(
d,
......@@ -114,21 +82,19 @@ shinyServer
return(d)
}
## Rdata input
if (input$dataset == "rdata")
{
if (is.null(input$fileRData))
{
return()
}
load(input$fileRData$datapath)
if (exists("data"))
{
## Happy path
if (!is.null(input$fileRData))
load(input$fileRData$datapath)
if (exists("data") && class(data) == "phyloseq")
return(data)
} else {
return()
}
## Unhappy path
return()
}
## Default case
load("demo/demo.RData")
return(get(input$dataset))
......@@ -174,7 +140,7 @@ shinyServer
validate(
need(
data16S(),
"Firstly, you should select a demo dataset or upload an abundance BIOM file.\nFor example, with Galaxy, a BIOM file can be obtained at the end of FROGS workflow with the 'FROGS BIOM to std BIOM' tool"
"Firstly, you should select a demo dataset or upload an abundance BIOM file.\nFor example, with Galaxy, a BIOM file can be obtained at the end of FROGS workflow with the 'FROGS BIOM to std BIOM' tool.\n Make sure that the phyloseq object in the RData file is called 'data'."
)
)
data16S()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment