Reputation: 253
I am trying to match files with similar filenames to use as input into a function. For example, if file names in the directory are:
atac.macaque.R1.fastq.gz
atac.macaque.R2.fastq.gz
atac.human.R1.fastq.gz
atac.human.R2.fastq.gz
Is there a function that can recognize that atac.macaque.R1.fastq.gz and atac.macaque.R2.fastq.gz are pairs and should be inputted as x and y, respectively into the function which will read these files?
I am hoping to find a function which can iterate through all of the file pairs in the directory (which all start with different names i.e. atac.human vs. atac.macaque)and apply then to the file read function that I using.
Upvotes: 0
Views: 276
Reputation: 389215
Would every file have a pair? If yes, then you can get the vector of file paths and put them in a matrix after sorting the names.
x <- sort(list.files('/path/to/directory', pattern = "\\.gz$"))
mat <- matrix(x, ncol = 2)
mat
# [,1] [,2]
#[1,] "atac.human.R1.fastq.gz" "atac.macaque.R1.fastq.gz"
#[2,] "atac.human.R2.fastq.gz" "atac.macaque.R2.fastq.gz"
Now, every column is a pair, if you have some function which takes these 2 files as an argument you can use apply
column-wise to apply these function to every pair.
some_func <- function(x, y) #does some thing with x & y
apply(mat, 2, some_func)
Upvotes: 2