R Language: How to use Specan function on my own wav file? - r

I am trying to use specan function from warbleR package. I want to pass my own wav file as an argument to the function. I have seen only one example in docs which is not much self explanatory.
wave_file <- readWave("C:/Users/ABC/Downloads/file_example_WAV_1MG.wav", from = 1, to = Inf, units = c("seconds"), header = FALSE, toWaveMC = NULL)
head(wave_file)
mono_file <- mono(wave_file, which = c("both"))
head(mono_file)
auto_file <- autodetec(X = "C:/Users/ABC/Downloads/file_example_WAV_1MG.wav")
head(auto_file)
dataframe <- data.frame(list = c("sound.files", "selec", "start", "end"))
dataframe <- data.frame(wave_file, "abc", 1, Inf)
dataframe
# Existing Example found in R docs
#setwd('C:/Users/ABC/Downloads')
#data1 <- data(list = c("Phae.long1", "Phae.long2", "Phae.long3", "Phae.long4", "selec.table"))
#writeWave(Phae.long1,"Phae.long1.wav")
#writeWave(Phae.long2,"Phae.long2.wav")
#writeWave(Phae.long3,"Phae.long3.wav")
#writeWave(Phae.long4,"Phae.long4.wav")
#writeWave(Phae.long1,"file_example_WAV_1MG.wav")
#writeWave(Phae.long2," ")
#writeWave(Phae.long3,"1")
#writeWave(Phae.long4,"Inf")
getwd()
#file <- specan(X = selec.table, bp = c(0, 22))
#head(file)
file <- specan(X = dataframe, bp = c(0,22))
How to give my own .wav file as argument to the specan function?

Instead of passing the actual wav file to the dataframe, pass the name of that file. So your code should look like this;
dataframe <- data.frame(list = c("sound.files", "selec", "start", "end"))
dataframe <- data.frame("file_example_WAV_1MG.wav", 2, 1, 20)
names(dataframe) <- c("sound.files", "selec", "start", "end")
a <- specan(X=dataframe, bp=c(0,22))
You can then view a. The extracted features will be stored in the dataframe. Make sure your file is stored in the working directory.

Related

How to simultaneously perform same code on multiple datasets of different lengths in Rstudio?

I need to use the functions detrend() and chron() from the dplR package on >300 tree-ring width datasets (.rwl files), of differing lengths. Rather than copying and pasting the code for each object, I would like to do this simultaneously. After some google-ing, it looks like I need to develop a for loop, but I have not had much luck after some troubleshooting. Could someone help put me in the right direction? Below is my current code.
##read data files in
or001 <- read.rwl("or001.rwl", format = "tucson")
or002 <- read.rwl("or002.rwl", format = "tucson")
or004 <- read.rwl("or004.rwl", format = "tucson")
#detrend - negex method
or001.negex <- detrend(or001, nyrs = NULL, method = "ModNegExp", f = 0.5,
pos.slope = FALSE)
or002.negex <- detrend(or002, nyrs = NULL, method = "ModNegExp", f = 0.5,
pos.slope = FALSE)
or004.negex <- detrend(or004, nyrs = NULL, method = "ModNegExp", f = 0.5,
pos.slope = FALSE)
#build final chronology
or001.negex.crn <- chron(or001.negex, prefix = 'OR')
or002.negex.crn <- chron(or002.negex, prefix = 'OR')
or004.negex.crn <- chron(or004.negex, prefix = 'OR')
#export final chronologies
write_excel_csv(or001.negex.crn, path = "or001.negex.crn.csv")
write_excel_csv(or002.negex.crn, path = "or002.negex.crn.csv")
write_excel_csv(or004.negex.crn, path = "or004.negex.crn.csv")
Consider reading the datasets in a list and apply the same function by creating a function ('f1')
f1 <- function(file, filenm) {
dat <- read.rwl(file, format = "tucson")
negex <- detrend(dat, nyrs = NULL, method = "ModNegExp", f = 0.5,
pos.slope = FALSE)
negex.crn <- chron(negex, prefix = 'OR')
write_excel_csv(negex.crn, path = filenm)
return(negex.crn)
}
# // get all the files with the `.rwl` pattern
# // from the current working directory
files <- list.files(pattern = "\\.rwl$", full.names = TRUE)
# // change the file names by replacing the suffix with negex.crn.csv
# // loop over the files, and apply the function
nm1 <- sub("\\.rwl", "negex.crn.csv", basename(files))
Map(f1, file = files, filenm = nm1)

R) Counts.csv.gz file to Seurat object

I usually import filtered feature bc matrix including barcodes.tsv.gz, features.tsv.gz, and matrix.mtx.gz files to R environment by Read10X function, and convert the data to Seurat object by CreateSeuratObject function.
However, I found out that some publicly available processed scRNA-seq data was shared only in the format of counts.csv.gz file.
So, I tried to convert the counts.csv.gz files to Seurat object via following commands;
countsData<-read.delim(file = "~path/TUMOR1_counts.csv.gz", header = TRUE, sep = ",")
Tumor2 <- CreateSeuratObject(counts = countsData, project = "Tumor2", min.cells = 3, min.features = 200)
However, the following error occured.
Error in CreateAssayObject(counts = counts, min.cells = min.cells, min.features = min.features) :
No feature names (rownames) names present in the input matrix
Here is the counts.csv file that looks like this.
How can I solve this problem?
At first, count matrix as an input for CreateSeuratObject() should have the cells in column and features in row. It seems like that you should use t() to convert your imported counts with the rownames.
I recommend you do like this:
countsData <- read.csv(file = "~path/TUMOR1_counts.csv", header = TRUE, row.names = 1)
Tumor2 <- CreateSeuratObject(counts = t(countsData), project = "Tumor2", min.cells = 3, min.features = 200)
I think you have empty cells. You should fill them with zeros.

Add date from the file name for multiple files in one df in R

I am trying to save multiple csv files in one df and include a new column with the date of the file in the df. I already read all the files to get one df but I can't add the date column per file. Im using the next code
ccn_files <- list.files(pattern = '*.csv', path = "input/CCN/") ##Creates a list of all the files
ccn_data_raw <- do.call("rbind", ##Apply the bind to the files
lapply(ccn_files, ##call the list
function(x) ##apply the next function
read.csv(paste("input/CCN/", x, sep=''),fill = T, header = TRUE,
skip = 4)))
I was also able to get the date from all the files in a vector using this line
test <- ymd(substr(ccn_files,14,19))
How can I add this line inside the first chunk of code so it does what I want?
We can use Map
ccn_data_raw <- do.call(rbind, Map(cbind, lapply(ccn_files,
function(x) read.csv(paste("input/CCN/", x, sep=''),fill = TRUE,
header = TRUE, skip = 4)), date = test))
Or using purrr functions :
library(purrr)
ccn_data_raw <- map2_df(map(ccn_files, function(x)
read.csv(paste("input/CCN/", x, sep=''), fill = TRUE, header = TRUE,
skip = 4)), test, cbind)

For-loop in R to create a new file (but gives incorrect/unexpected output)

I'm currently busy with some data and I need to check their validity.
Therefore, I would like to use a for-loop to go through all my data files.
In this for-loop, I would like to calculate some things (like mean, min,max...).
My code below works but produced an incorrectly written csv file. The problem occurs after the calculations (and their values) are done during csv file creation. CSV:
"c.1..1..1004.89081855716..630.174466667434..461.738905906677.." "c.1..1..950.990843858612..479.98560814955..517.955102920532.."
1 1
1 1
1004.89081855716 950.990843858612
630.174466667434 479.98560814955
461.738905906677 517.955102920532
1535.86795806885 1452.30199813843
-13.3948961645365 3.72026950120926
1259.26423788071 1159.17089223862
Approach/What I'm expecting:
So I start from some data files with eye tracking data in it.
As you can see at the beginning of the code, I try to get some values out of this eye tracking data (validity, new file with only validity == 1 data...). Once I created the filtered_data dataframe, I want to calculate some extra values out of it (mean, sd, min/max).
My plan is to create a new csv file (validity_loop.csv) in which I can find all my calculations (validity_left, validity_right,mean_eye_x, mean_eye_y, min_eye_x,max_eye_x,min_eye_y,max_eye_y). All in a row. One row for each data set (file_list[i]).
Can someone help me in how to tackle and solve this issue?
Here is my code:
set <- setwd("/Users/Sarah/Documents")
file_list <- list.files(set, pattern = ".csv", all.files = TRUE)
validity_list <- data_list <- vector("list", "length" = length(file_list))
for(i in seq_along(file_list)){
filename = file_list[i]
#read files
data_frame = read.csv(filename, sep = ",", dec = ".",
header = TRUE,
stringsAsFactors = FALSE)
#what has to be done
#validity
validity_left <- mean(is.numeric(data_frame$left_gaze_point_validity))
validity_right <-mean(is.numeric(data_frame$right_gaze_point_validity))
#Zuiver dataframe (validity ==1)
to_keep = which(data_frame$left_gaze_point_validity == 1 &
data_frame$right_gaze_point_validity==1)
filtered_data = data_frame[to_keep,]
filtered_data$left_eye_x = as.numeric(filtered_data$left_eye_x)
filtered_data$left_eye_y = as.numeric(filtered_data$left_eye_y)
filtered_data$right_eye_x = as.numeric(filtered_data$right_eye_x)
filtered_data$right_eye_y = as.numeric(filtered_data$right_eye_y)
#1 eye-data
filtered_data$eye_x <- (filtered_data$left_eye_x+filtered_data$right_eye_x)/2
filtered_data$eye_y <- (filtered_data$left_eye_y+filtered_data$right_eye_y)/2
#Pixels
filtered_data$eye_x <- (filtered_data$eye_x)*1920
filtered_data$eye_y <- (filtered_data$eye_y)*1080
#SD and Mean + min-max
mean_eye_x<- mean(filtered_data$eye_x)
mean_eye_y <- mean(filtered_data$eye_y)
sd_eye_x <- sd(filtered_data$eye_x)
sd_eye_y <- sd(filtered_data$eye_y)
min_eye_x <- min(filtered_data$eye_x)
min_eye_y <- min(filtered_data$eye_y)
max_eye_x <- max(filtered_data$eye_x)
max_eye_y <- max(filtered_data$eye_y)
#add everything to new file
validity_list[[i]] <- c(validity_left, validity_right,
mean_eye_x, mean_eye_y,
min_eye_x, min_eye_y,
max_eye_x, max_eye_y)
}
#new document
write.table(validity_list,
file = "Master T&O/Thesis /Loop/Validity/validity_loop.csv",
col.names = TRUE, row.names = FALSE)
I managed to get a new data frame in R, which contains the value of my validity_list as a matrix form.
#FOR LOOP poging 2
set <- setwd("/Users/Sarah/Documents/Master T&O/Thesis /Loop")
file_list <- list.files(set, pattern = ".csv", all.files = TRUE)
validity_list <- vector("list", "length" = length(file_list))
for(i in seq_along(file_list)){
filename = file_list[i]
#read files
data_frame = read.csv(filename, sep = ",", dec = ".", header = TRUE, stringsAsFactors = FALSE)
#what has to be done
#validity
validity_left <- mean(is.numeric(data_frame$left_gaze_point_validity))
validity_right <-mean(is.numeric(data_frame$right_gaze_point_validity))
#Zuiver dataframe (validity ==1)
to_keep = which(data_frame$left_gaze_point_validity == 1 & data_frame$right_gaze_point_validity==1)
filtered_data = data_frame[to_keep,]
filtered_data$left_eye_x = as.numeric(filtered_data$left_eye_x)
filtered_data$left_eye_y = as.numeric(filtered_data$left_eye_y)
filtered_data$right_eye_x = as.numeric(filtered_data$right_eye_x)
filtered_data$right_eye_y = as.numeric(filtered_data$right_eye_y)
#1 eye-data
filtered_data$eye_x <- (filtered_data$left_eye_x+filtered_data$right_eye_x)/2
filtered_data$eye_y <- (filtered_data$left_eye_y+filtered_data$right_eye_y)/2
#Pixels
filtered_data$eye_x <- (filtered_data$eye_x)*1920
filtered_data$eye_y <- (filtered_data$eye_y)*1080
#SD and Mean + min-max
mean_eye_x<- mean(filtered_data$eye_x)
mean_eye_y <- mean(filtered_data$eye_y)
sd_eye_x <- sd(filtered_data$eye_x)
sd_eye_y <- sd(filtered_data$eye_y)
min_eye_x <- min(filtered_data$eye_x)
min_eye_y <- min(filtered_data$eye_y)
max_eye_x <- max(filtered_data$eye_x)
max_eye_y <- max(filtered_data$eye_y)
#add everything to new file
validity_list[[i]] <- c(validity_left, validity_right,mean_eye_x, mean_eye_y, min_eye_x,max_eye_x,min_eye_y,max_eye_y)
validity_matrix <- matrix(unlist(validity_list), ncol = 8, byrow = TRUE)
}
#new document
write.table(validity_matrix, file = "/Users/Sarah/Documents/Master T&O/Thesis /Loop/Validity/validity_loop.csv", dec = ".")
The only problem I have now, is the fact that my values for the validity_list items are wrong, but that's another problem and I'm trying to fix it!
If I get it then the following line grabs all your data together:
validity_list[[i]] <- c (validity_left, validity_right,mean_eye_x,
mean_eye_y, min_eye_x,max_eye_x,min_eye_y,max_eye_y).
if it's like in python then I would have:
validity_list = (validity_left, validity_right,mean_eye_x,
mean_eye_y, min_eye_x,max_eye_x,min_eye_y,max_eye_y)
... whereas the '=' tell the interpreter that everything behind it is a tuple '(', data, ')' ...which makes it one single dataset and if I then write it... it would be end up in one column. If you do a pick using a for-loop I would get "validity_left" writing in a separate column. In your case adding this to your below code an option?
for item in validity_list:
function to process item..etc.

Modify columns in a dataframe by using function

I'm trying to modify my data frame columns and positions. Finally I found some solution to do that but I want to do all process in a function for all data sets in the directory and overwrite the real data.
kw <- matrix(1:11400, ncol = 19) # to make sample data
kw <- kw[, !(colnames(kw) %in% c("V18","V19"))] # to remove last two cols
add <- c(kw$V18 <- 0,kw$V19<- 0) # add new columns with all zero values
kw$V1 <- kw$V1 * 1000 # to modify first col of data frame
kw <- kw[ ,c(1,18:19,2:17)] # to replace col positions
lets say I have data set in the directory
kw<-read.table("5LSTT-test10.avgm", header = FALSE,fill=FALSE) # example which shows how I read single data
`5LSTT-test10.avgm`
.
.
.
.
5LSTT-test10.avgm`
how can apply this column modification process to each data separately and overwrite or make new data?
edit output readLines("5LSTT-test10.avgm", n = 1)
you can see 19 columns and think this data has 600 rows
[1] " 9.0000E-02 0.0000E+00 2.3075E-03 -6.4467E-03 9.9866E-01 9.8648E-02 4.5981E-02 9.8004E-01 1.2359E-01 6.1175E-02 9.7701E-01 8.6662E-02 3.0034E-02 9.7884E-01 7.0891E-02 8.2247E-03 9.8564E-01 -8.7967E-11 4.3105E-02"
With "data.table" you would be able to do something like:
setcolorder(
fread(yourfile)[, c("V1", "V18", "V19") := list(V1 * 1000, 0, 0)], c(1, 18:19, 2:17))
Thus, if you really needed a function, you can do something like:
myFun <- function(infile) {
require(data.table)
write.table(
setcolorder(
fread(infile)[
, c("V1", "V18", "V19") := list(V1 * 1000, 0, 0)],
c(1, 18:19, 2:17)),
file = gsub("(.*)(\\..*)", "\\1_new\\2", infile),
row.names = FALSE)
}
You can then use myFun within lapply over a vector of the files you want to read and process.
In other words:
lapply(myListOfFilePaths, myFun)
By default, this function renames (rather than overwrites) your file appending "_new" at the end, but before the extension.
This could be another way
Read all the files and store it in a list like this
# to list down all the files in the directory
files.new = list.files(directory.path, recursive = TRUE, pattern=".avgm")
# to read all the files and store it in list
file.contents = lapply(paste(directory.path,files.new, sep="/"), read.table, sep='\t', header = TRUE)
Next you can do the modifications to each of the dataset in the list something like this
outlist = lapply(file.contents, function(x){
# modifications
kw <- x[, !(colnames(x) %in% c("V18","V19"))]
add <- c(kw$V18 <- 0,kw$V19<- 0)
kw$V1 <- kw$V1 * 1000
kw <- kw[ ,c(1,18:19,2:17)]
})
and write the modified data into new files using the function below
# function to write files from a list object
write.files = function(modified.list, path){
outlist = file.contents[sapply(modified.list, function(x) length(x) > 1)]
sapply(names(outlist), function(x)
write.table( outlist[[x]], file= paste(path, x, sep="/"),
sep="\t", row.names=FALSE))
}
Writing the data to files
write.files(outlist, "/directory/path")

Resources