0

I have this code that creates a random sequence.

  `$seq.mat <- matrix(NA, nrow=20, ncol=2)
   colnames(seq.mat) <- c("SmapleID","Sequence")
   set.seed(123)
   for (i in 1:nrow(seq.mat)) {
   seq.mat[i,1] <- paste0("Sample_", i)
   seq.mat[i,2] <- paste(sample(c("C","G","A","T"), size=1000, replace = 
   TRUE), collapse="")
   }
   seq.mat <- cbind(seq.mat, RNASeq=NA, Seq.Count=NA)`

How can I use these 3? functions together in I guess a "multi-function" to run all at once?

  `$DNA.RNA = function(check.string){
   grep = grepl("[^ACGT]",seq.mat[1:20,2])
   DNAorRNA = ifelse(grep == "FALSE", print("Seq is DNA, converting to 
   RNA.."), print("Seq is RNA"))
   DNAorRNA = as.list(DNAorRNA)
   DNAorRNA
   }
   new.dat = gsub("T", "U", seq.mat[,2])
   replaceDNA = function(replDNA){
   seq.mat[,3] = new.dat
   }`

I wanted to put "new.dat" into a function (that's why I said 3 functions) but I had trouble getting the gsub to place the data into seq.mat[,3]. Any way to do that instead and then just combine the 2 functions?

I can run it as a stand alone code but I wanted to put it into one function. If I just add "seq.mat[,3] <- gsub("T","U",seq.mat[,2])" to the original function it doesn't work and the print part only prints one line.

jclabrat
  • 35
  • 6
  • Two methods leap out at me: (1) take the code from the 2nd function and append it to the code of the 1st function, changing variable names as needed; or (2) modify the 1st function to call the 2nd function when complete. – r2evans Oct 27 '17 at 23:46
  • Interesting. I wanted to put "new.dat" into a function (that's why I said 3 functions) but I had trouble getting the gsub to place the data into seq.mat[,3]. Any way to do that too? – jclabrat Oct 27 '17 at 23:51
  • Can't you just use `seq.mat[,3] <- gsub("T","U",seq.mat[,2])`? – r2evans Oct 27 '17 at 23:53
  • Yes, I can run it as a stand alone code but I wanted to put it into one function. If I just add "seq.mat[,3] <- gsub("T","U",seq.mat[,2])" to the original function it doesn't work and the print part only prints one line. – jclabrat Oct 27 '17 at 23:57
  • Functional programming is not something you're going to learn in a question on SO, unfortunately. Your code is hard to read, breaks scope boundaries, misuses vectorized functions, and overwrites function names. I'm not intending to criticize (everybody needs to start somewhere), just stating that I don't really know where to start to help, since I don't know what your anticipated outputs look like (assuming `seq.mat` is your input). Perhaps simplify your question, taking hints from https://stackoverflow.com/help/mcve and https://stackoverflow.com/questions/5963269. – r2evans Oct 28 '17 at 00:05

0 Answers0