[Seqinr-commits] r1780 - pkg/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 6 12:26:16 CEST 2014


Author: jeanlobry
Date: 2014-06-06 12:26:15 +0200 (Fri, 06 Jun 2014)
New Revision: 1780

Modified:
   pkg/R/write.fasta.R
Log:
add an as.string argument so that write.fasta can handle sequences when provided as strings

Modified: pkg/R/write.fasta.R
===================================================================
--- pkg/R/write.fasta.R	2014-06-04 13:33:57 UTC (rev 1779)
+++ pkg/R/write.fasta.R	2014-06-06 10:26:15 UTC (rev 1780)
@@ -1,4 +1,5 @@
-write.fasta <- function(sequences, names, file.out, open = "w", nbchar = 60){
+write.fasta <- function(sequences, names, file.out, open = "w", nbchar = 60, 
+    as.string = FALSE){
   #
   # Open output file:
   #
@@ -7,17 +8,16 @@
   #
   # Function to write one sequence in output file:
   #
-  write.oneseq<-function(sequence, name, nbchar){
+  write.oneseq<-function(sequence, name, nbchar, as.string){
     writeLines(paste(">", name, sep = ""), outfile)
+    if(as.string) sequence <- s2c(sequence)
     l <- length(sequence)
     q <- floor(l/nbchar)
     r <- l - nbchar*q
     if(q > 0){
-#      sapply(seq_len(q), function(x) writeLines(paste(sequence[(nbchar*(x-1)+1):(nbchar*x)], collapse = "", sep = ""), outfile))
       sapply(seq_len(q), function(x) writeLines(c2s(sequence[(nbchar*(x - 1) + 1):(nbchar*x)]), outfile))
     }
     if(r > 0){
-#      writeLines(paste(sequence[(nbchar*q+1):l], collapse = "", sep = ""), outfile)
       writeLines(c2s(sequence[(nbchar*q + 1):l]), outfile)
     }
   }
@@ -26,12 +26,12 @@
   # Write all sequences in output file:
   #
   if(!is.list(sequences)){
-    write.oneseq(sequence = sequences, name = names, nbchar = nbchar)
-  }else{
+    write.oneseq(sequence = sequences, name = names, nbchar = nbchar, as.string = as.string)
+  } else {
     n.seq <- length(sequences)
-    sapply(seq_len(n.seq), function(x) write.oneseq(sequence = as.character(sequences[[x]]), name = names[x], nbchar = nbchar))
+    sapply(seq_len(n.seq), function(x) write.oneseq(sequence = as.character(sequences[[x]]), 
+      name = names[x], nbchar = nbchar, as.string = as.string))
   }
-  
   #
   # Close output file:
   #



More information about the Seqinr-commits mailing list