How can I use read.fasta to load a lot of sequences that are stored in a folder and then run a program for all of this files in R. (loops)
0
0
Entering edit mode
5.6 years ago
imda ▴ 10

I need your help. I have one folder that stores a lot of fasta alignment files. I want to use a program in R and run it for all the files that are stored in my folder. So, I do not know how to loop my script

Here I set my working directory

w.d <- '/share/Part1/icruz/data/orthofinder_all/all_solanaceas_protein_cds_correction/cafe_headers/Results_Apr15/cds_extractions/Rmultiplefiles'

setwd(w.d)

cds.fa <- read.fasta( '/share/Part1/icruz/data/orthofinder_all/all_solanaceas_protein_cds_correction/cafe_headers/Results_Apr15/cds_extractions/all_cds_solanaceae.fasta', seqtype="DNA", strip.desc=T, as.string=T ) 

# Here is the problem because I do not know how to read all the fasta files instead of set manually one by one

msa.aa <- read.fasta( '/home/icruz/data/orthofinder_all/all_solanaceas_protein_cds_correction/cafe_headers/Results_Apr15/cds_extractions/Rmultiplefiles/*.fa', seqtype="AA", strip.desc=T, as.string=T ) 

#Also I need to modify the headers in all my fasta files
names(msa.aa) 
msa.san <- msa.aa
names(msa.san) <- sub("^.*_", "", names(msa.aa))
names(msa.san)

#THIS IS THE PROGRAM THAT I NEED TO LOOP once I have load all my fasta files
cds.msa <- alignCDSSetWithAlignedAAsAsGuide(cds.fa, msa.san)
R • 2.5k views
ADD COMMENT
1
Entering edit mode

first part reading multiple fasta files (at once) after setting working directory (where the fasta files are kept):

> fl=list.files(pattern = ".fa")
> library(seqinr)
> for (i in 1:length(fl)) assign(fl[i], read.fasta(fl[i],seqtype="DNA", strip.desc=T, as.string=T ))
> ls()
 [1] "fl"        "i"         "test1.fa"  "test10.fa" "test2.fa"  "test3.fa"  "test4.fa"  "test5.fa" 
 [9] "test6.fa"  "test7.fa"  "test8.fa"  "test9.fa"

script saves each fasta file in individual data frames for each fasta file.

ADD REPLY

Login before adding your answer.

Traffic: 2717 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6