Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaPrime7 committed Dec 19, 2023
1 parent 187e266 commit 0b7cafa
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 26 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(lambda_check_source)
export(make_wavelength)
export(min_max_norm)
export(qc_attributes)
export(read_denovix)
export(read_denovix_data)
export(tidyDenovix)
import(data.table)
Expand Down
87 changes: 67 additions & 20 deletions R/tD_subordinates.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,77 @@
#' fpath <- system.file("extdata", "rnaspec2018.csv", package = "tidyDenovix", mustWork = TRUE)
#' rna_data = read_denovix_data(fpath, file_type = 'csv')

read_denovix_data = function(dfile, file_type = c('csv','excel','txt')){
read_denovix_data = function(dfile, file_type = c('csv','txt','excel') ){

if(is.null(dfile)){
stop('Enter the string for a file or path to the Denovix-DS11 file')
}

if('csv' %in% file_type){
csvfile = read.csv(file = dfile, header = TRUE)
return(csvfile)
if ('csv' %in% file_type ){
csvfile = read.csv(file = dfile, header = TRUE)
return(csvfile)

} else if('excel' %in% file_type ){
xlfile = read_excel(dfile, col_names = TRUE)
return(xlfile)

} else if('txt' %in% file_type ){
txtfile = read.table(file = dfile, header = TRUE, skip = 0)
return(txtfile)

} else if('excel' %in% file_type){
xlfile = read_excel(dfile, col_names = TRUE)
return(xlfile)
}

} else if('txt' %in% file_type){
txtfile = read.table(file = dfile, header = TRUE, skip = 0)
return(txtfile)
}

} else {
if( is.null(file_type) || file_ext(dfile) == 'csv'){
#' Title: Read Denovix files
#'
#' @author Tingwei Adeck
#'
#' @description
#' A function read Denovix data files.
#'
#' @import readxl
#' @importFrom utils read.csv
#' @importFrom utils read.table
#'
#' @param dfile A Denovix file or path to the Denovix file.
#'
#' @return A data frame.
#'
#' @export
#'
#' @seealso [read_denovix_data()]
#'
#' @note
#' Denovix files can be saved as csv, txt or even excel files.
#' This function accounts for these file types.
#'
#' @examples
#' fpath <- system.file("extdata", "rnaspec2018.csv", package = "tidyDenovix", mustWork = TRUE)
#' rna_data = read_denovix(fpath)

read_denovix = function(dfile){

nft = c(file_ext(dfile))

if(is.null(dfile)){
stop('Enter the string for a file or path to the Denovix-DS11 file')
}

if ('csv' %in% nft ){
csvfile = read.csv(file = dfile, header = TRUE)
return(csvfile)

} else if(is.null(file_type) || file_ext(dfile) == 'xlsx'){
} else if('xlsx' %in% nft ){
xlfile = read_excel(dfile, col_names = TRUE)
return(xlfile)

} else if(is.null(file_type) || file_ext(dfile) == 'txt'){
} else if('txt' %in% nft ){
txtfile = read.table(file = dfile, header = TRUE, skip = 0)
return(txtfile)

}

}

}

#' Title: Extract key colnames from the Denovix data frame
Expand Down Expand Up @@ -107,7 +143,11 @@ extract_col_names = function(xdf){

extract_sample_names = function(dfile, file_type=NULL){

xdf = read_denovix_data(dfile, file_type)
if(!is.null(file_type)){
xdf = read_denovix_data(dfile, file_type)
} else {
xdf = read_denovix(dfile)
}

xdf = janitor::clean_names(xdf)
sample_names = xdf[, c("sample_name")]
Expand Down Expand Up @@ -278,7 +318,7 @@ min_max_norm <- function(x){

#' Title: File Extension Finder
#'
#' @author Tingwei Adeck
#' @author Unknown (Adapted by Tingwei Adeck)
#'
#' @param epath File path.
#'
Expand All @@ -290,6 +330,13 @@ min_max_norm <- function(x){
#' fpath <- system.file("extdata", "rnaspec2018.csv", package = "tidyDenovix", mustWork = TRUE)
#' ext = file_ext(fpath)

file_ext <- function(epath) {
sub(pattern = "^(.*\\.|[^.]+)(?=[^.]*)", replacement = "", epath, perl = TRUE)
file_ext = function(epath) {
# or use .Platform$file.sep in stead of '/'
splitted <- strsplit(x=epath, split='/')[[1]]
epath = splitted [length(splitted)]
ext = ''
splitted = strsplit(x=epath, split='\\.')[[1]]
l = length (splitted)
if (l > 1 && sum(splitted[1:(l-1)] != '')) ext <-splitted [l]
return(as.character(ext))
}
16 changes: 13 additions & 3 deletions R/tidyDenovix.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,25 @@
#' fpath <- system.file("extdata", "rnaspec2018.csv", package = "tidyDenovix", mustWork = TRUE)
#' rna_data = tidyDenovix(fpath, file_type = 'csv', sample_type = 'RNA', check_level = 'lax')

tidyDenovix = function(dfile, file_type= c('csv','excel','txt'), sample_type = c('RNA','DNA'), check_level = c('strict','lax'), qc_omit = NULL, normalized = c('yes','no'), fun = NA){
tidyDenovix = function(dfile, file_type= NULL, sample_type = c('RNA','DNA'), check_level = c('strict','lax'), qc_omit = NULL, normalized = c('yes','no'), fun = NA){

suppressWarnings({

nofun <- is.na(fun)

#import
xdf = read_denovix_data(dfile=dfile, file_type = file_type)
xdf_qc = read_denovix_data(dfile=dfile, file_type = file_type) #for QC
if(!is.null(file_type)){
xdf = read_denovix_data(dfile=dfile, file_type = file_type)
xdf_qc = read_denovix_data(dfile=dfile, file_type = file_type) #for QC

} else {
xdf = read_denovix(dfile=dfile)
xdf_qc = read_denovix(dfile=dfile) #for QC
}
#xdf = read_denovix(dfile=dfile)
#xdf_qc = read_denovix(dfile=dfile) #for QC


cutoff = which( colnames(xdf)=="Exposure" )
aftercut = as.numeric(cutoff + 1)

Expand Down
2 changes: 1 addition & 1 deletion man/file_ext.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions man/read_denovix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/read_denovix_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/tidyDenovix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b7cafa

Please sign in to comment.