This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to write a i in loop for readfastq function

Hi guys,

I try to read fastq files from a RNA-Seq result. I had 19 fastq files, but the manual of ShortRead said "Methods read all files into a single R object; a typical use is to restrict input to a single FASTQ file."

I do not know how to write a i loop to make one vector for all the 19 files. Could you kindly help, please? Many thanks,

Jerry

Below is what I write, I guess is kind of stupid....

fastqFilesdir <- dir("/Users/jerry/Desktop/RStudio/Key/fastq/fastq", full=TRUE)

fq1<- readFastq(fastqFilesdir[1])

fq2<- readFastq(fastqFilesdir[2])

fq3<- readFastq(fastqFilesdir[3])


...

fq18<- readFastq(fastqFilesdir[18])

fq19<- readFastq(fastqFilesdir[19])
rna-seq

3 answers

You have to provide a pattern as a parameter to readFastq function. Read the parameter descriptions, you don't have to write a loop.

UPDATE: this is what you need, pattern works like grep search.

fq=readFastq('/Users/jerry/Desktop/RStudio/Key/fastq/fastq',pattern='.q$')

Dear Sukhdeep Singh,

I used this code as well:

fq1<- readFastq("/Users/Jerry/Desktop/RStudio/Key/fastq/fastq", pattern = "jerry_S1_L001_R1_001.fastq")

It provides same result as above. All the 19 files are in the dirpath "/Users/Jerry/Desktop/RStudio/Key/fastq/fastq", but each file name is different, from "jerry_S1_L001_R1_001.fastq" to ""jerry_S1_L019_R1_001.fastq".

May I ask how to write pattern to includes all the 19 files, please? Thank you~

This is very easy, if you read the parameter information carefully. I don't have the package installed to test it for you.

I updated the answer, should work for you. Be careful, it might bog down your machine, depending on the size of files that you are reading in + a single S4 object in R will contain all the information from all files, this will be heavy duty!!

Try this (since I don't have the program to check it, it is possible the presence of a little mistake in the code):

COUNTER=1
while [  $COUNTER -lt 19 ]; do
             fq"$COUNTER"<-readFastq(fastqFilesdir["$COUNTER"])
             let COUNTER=COUNTER+1 
done

I'm sorry I was working with bash when I read your post and I didn't see that you where asking about R

I figure out it. Just need to define a list container fastfilelist, Like this:

fastqFilesdir <- dir("/Users/Jerry/Desktop/RStudio/Key/fastq/fastq", full=TRUE)

fastfilelist= list()

for (i in c(1:19) ) {

fastfilelist[i]<- readFastq(fastqFilesdir[i])

}

Log in to answer this question.