Home > database >  loop for creating files
loop for creating files

Time:09-30

I want to do a loop.

I have a list of values created as follows:

list_snps<-list(20, 101, 109, 265, 465, 509, 526, 630, 743, 795, 836, 99,.....)

I would like to make a loop to automatize the following script:

seq20<-substr(mstn_seq, 20, 16757)

16757 remain always costant and only 20 should be substituted according list.

seq20 <- capture.output({
  cat('>seq20\n')
  cat(seq20)
})
write.table(seq20, "seq20.fasta", quote=F, row.names = F, 
col.names=F, sep="\t")  

I try something as ...

for(i in list_snps){
  paste(seq,i)<-substr(mstn_seq, i, 16757)  
}

But I am unable to continue.

CodePudding user response:

Since you don't provide a reproducible example it is a bit unclear what you want to do, so I try to guess:

list_snps<-as.list(1:10)
mstn_seq <- paste0(sample(letters,20,replace = T),collapse = "")
seq_list <- list()

for(i in 1:length(list_snps)){
  seq_list[[i]] <- assign(paste0("seq",i),substr(mstn_seq, list_snps[[i]], 11))
  write.table(seq_list[[i]], paste0("seq",i,".fasta"), quote=F, row.names = F, 
              col.names=F, sep="\t") 
}

you can't create a variable in the environment with paste(...)<- instead you have to use assign("var_name",object)

CodePudding user response:

It's hard to guess how changing the ID of a sequence is actually generating a specific FASTA sequence. From what I see, you are just generating the same FASTA file, just with a different filename and ID.

Anyway, looking at your code and what you said you want to get from it, this is my suggestion:

for(i in list_snps){
  seq_i<-substr(mstn_seq, i, 16757)
  
  seq_i <- capture.output({
    cat(paste0('>seq', i, '\n')
    cat(seq_i)
  })
    
  write.table(seq_i, paste0("seq", i, ".fasta"), quote=F, row.names = F, 
              col.names=F, sep="\t") 
  
}

But if what you actually want to do is to take the specific sequence with that specific ID, I would do this instead:

for(i in list_snps){
  seq_i<-grep(i, mstn_seq)
  seq_i = c(seq_i, seq_i   1)
  seq_i = mstn_seq[seq_i, ]
  
  seq_i <- capture.output({
    cat(paste0('>seq', i, '\n')
        cat(seq_i)
  })
    
    write.table(seq_i, paste0("seq", i, ".fasta"), quote=F, row.names = F, 
                col.names=F, sep="\t") 
    
}
  • Related