Skip to content

Commit

Permalink
edited the description and reading ads files ( id it is in a directory)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgejr45 committed Sep 25, 2024
1 parent 3a63927 commit af937f5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 244 deletions.
64 changes: 0 additions & 64 deletions R/extract.metadata.R

This file was deleted.

60 changes: 0 additions & 60 deletions R/read.sig.R

This file was deleted.

69 changes: 35 additions & 34 deletions R/read_asd_summar.R
Original file line number Diff line number Diff line change
@@ -1,115 +1,116 @@
#' Read ASD Spectral Files and Create a SummarizedExperiment Object
#'
#' This function will read a single or multiple ASD files and extracts the spectral data
#' and associated metadata, combines them into a "SummarizedExperiment" object. This object
#'
#' This function will read a single or multiple ASD files and extracts the spectral data
#' and associated metadata, combines them into a "SummarizedExperiment" object. This object
#' contains the spectral data as an assay matrix and metadata about the samples and wavelengths.
#'
#' @name read_asd_summar
#' @title Read the ASD files and return as SummarizedExperiment object.
#'
#'
#' @param path A character string specifying the path to a directory containing the ".asd" files or a single
#' ".asd" file.
#'
#'
#' @return A `SummarizedExperiment` object containing:
#' \itemize{
#' \item{\code{assays}}{A list with a single assay matrix named \code{counts},
#' where rows represent wavelengths and columns represent different samples.}
#' \item{\code{colData}}{A `DataFrame` with sample metadata, including the sample names.}
#' \item{\code{rowData}}{A `DataFrame` with wavelength information.}
#' }
#'
#'
#' @details
#' This function uses 2 functions such as "read.asd" and "extract.metadata" from the package "FieldSpectra"
#' to read the data and metadata from the given files. This function will then uses the data from the files
#' and make a SummarizedExperiment object out of it which can be used for further data analysis.
#'
#'
#' @import FieldSpectra
#' @import SummarizedExperiment
#'
#'
#' @author Methun George, Steffen Neumann
#'
#'
#'
#' @author Methun George, Dr.Steffen Neumann
#'
#' @examples
#' # path <- "path/to/your/asd/files"
#' # se <- read_asd_summar(path)
#' # se
#' # colnames(se)
#' # rownames(se)
#'
#'
#' @export


library(FieldSpectra)
library(SummarizedExperiment)

read_asd_summar <- function(path) {
path_info <- file.info(path)
if (path_info$isdir) {
asd_files <- list.files(path = path, pattern = "\\.asd$", full.names = TRUE)
} else {
asd_files <- path
}


read_asd_summar <- function(path) {
if (length(path) == 1 && file.info(path)$isdir) {
asd_files <- list.files(path = path, pattern = "\\.asd$", full.names = TRUE)
} else {
asd_files <- path
}


# Initialize lists to store the data frame and metadata
asd_data_list <- list()
asd_metadata_list <- list()
assay_data <- list()

# Loop over each file and read it (in case of directory)
for (file in asd_files) {
asd_data <- read.asd(file.dir = file, out.dir = '~')
wavelengths <- asd_data$Wavelength
spectra <- asd_data$Spectra

# Store data for the assay matrix
assay_data[[basename(file)]] <- spectra
combined_data <- data.frame(wavelengths, spectra)
asd_data_list[[basename(file)]] <- combined_data

# Extract the metadata
asd_metadata <- extract.metadata(file.dir = file, out.dir = '~' ,instrument = "ASD")
file_name <- asd_metadata$Spectra_File_Name
spectral_time <- asd_metadata$Spectrum_Time_UTC
combined_metadata <- data.frame(file_name, spectral_time)
asd_metadata_list <- append(asd_metadata_list, list(combined_metadata))
}

# Combine metadata for better readability
asd_metadata_list <- do.call(rbind, asd_metadata_list)

# Initialize the assay matrix data frame
assay_matrix <- do.call(cbind, assay_data)
wavelengths <- asd_data_list[[1]]$wavelengths
rownames(assay_matrix) <- wavelengths
assay_df <- data.frame(assay_matrix)

# Set column names based on file names from metadata
colnames(assay_df) <- sapply(asd_metadata_list$file_name, function(x) basename(x))

# Create ColData
colData <- DataFrame(samplenames = colnames(assay_df))

# Create rowData
rowData <- DataFrame(Wavelengths = rownames(assay_df))

# Create the SummarizedExperiment object
se <- SummarizedExperiment(assays = list(counts = as.matrix(assay_df)),
colData = colData,
rowData = rowData)

return(se)
}

# Example
# Example
# path <- "/Users/methungeorge/Desktop/IPB/test/data"
# se <- read_asd_summar(path)
# se

# # Check the SummarizedExperiment object
#
# # # Check the SummarizedExperiment object
# print(se)
# colnames(se)
# rownames(se)
# # Access the assay data
# # # Access the assay data
# assay_data <- assay(se, "counts")
# head(assay_data)
##note : now the coldata is 1D and we need to include the trait information, eg look at the article
34 changes: 17 additions & 17 deletions R/read_sig_summar.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' This function reads .sig files from a specified directory or a file path, extracts wavelength and reflectance data,
#' and combines them into a \code{SummarizedExperiment} object. Each .sig file is expected to contain reflectance data
#' and a sample name in its content.
#'
#'
#' @name read_sig_summar
#' @title Read the SIG files and return as SummarizedExperiment object.
#'
Expand Down Expand Up @@ -31,8 +31,8 @@
#' print(se)
#'
#' @import SummarizedExperiment
#' @author Methun George, Steffen Neumann
#'
#' @author Methun George, Dr.Steffen Neumann
#'
#' @export

library(SummarizedExperiment)
Expand All @@ -46,55 +46,55 @@ read_sig_to_se <- function(path) {
} else {
sig_files <- path
}

# Create an empty list to store data frames and sample names
all_data <- list()
sample_names <- c()

# Read each file and extract data
for (file in sig_files) {
file_content <- readLines(file)

# Extract sample name
name_line <- grep("^name=", file_content, value = TRUE)
if (length(name_line) > 0) {
sample_name <- sub("^name=", "", name_line)
} else {
sample_name <- basename(file)
sample_name <- basename(file)
}
sample_names <- c(sample_names, sample_name)

# Extract data (reflectance %)
data_start <- grep("^data=", file_content)
data_lines <- file_content[(data_start + 1):length(file_content)]
data <- read.table(text = data_lines, header = FALSE, fill = TRUE)

# Select columns for wavelengths and reflectance
data <- data[, c(1, 4)]
colnames(data) <- c("Wavelengths(nm)", sample_name)

# Add the data frame to the list
all_data[[sample_name]] <- data
}

# Combine all data frames by wavelengths
combined_data <- Reduce(function(x, y) merge(x, y, by = "Wavelengths(nm)", all = TRUE), all_data)

# Set row names to wavelengths and remove the "Wavelengths(nm)" column
rownames(combined_data) <- combined_data$`Wavelengths(nm)`
combined_data <- combined_data[, -1, drop = FALSE]
combined_data <- combined_data[, -1, drop = FALSE]

# Create colData
colData <- DataFrame(sampleNames = colnames(combined_data))

# Create rowData
rowData <- DataFrame(Wavelengths = rownames(combined_data))

# Create the SummarizedExperiment object
se <- SummarizedExperiment(assays = list(counts = as.matrix(combined_data)),
colData = colData,
rowData = rowData)

return(se)
}

Expand Down
30 changes: 0 additions & 30 deletions tests/testthat/test-extract.metadata.R

This file was deleted.

Loading

0 comments on commit af937f5

Please sign in to comment.