Count letters in a string in R with while ()

0

good afternoon.

I'm trying to do a while loop, for a letter count. The objective is to obtain the count of the first 20 letters of a DNA sequence.

The Script that I have is:

sec <- "ACGTGCATGACGTAGCTATGCAGTCATACACGTGCATGACGTAGCTATGCAGTCATCGATA"
DNA <- strsplit(sec,"")

C <- 0
T <- 0
G <- 0
conteos <- A+C+T+G
while (conteos < 20) {
  DNA_A <- gsub("A","",DNA)
  A <- nchar(DNA)-nchar(DNA_A)
  print("El número de Adeninas en la secuencia es:")
  print(A)
    DNA_C <- gsub("C","",DNA)
  C <- nchar(DNA)-nchar(DNA_C)
  print("El número de Citocinas en la secuencia es:") 
  print(C)
  DNA_G <- gsub("G","",DNA)
  G <- nchar(DNA)-nchar(DNA_G)
  print("El número de Guaninas en la secuencia es:") 
  print(G)
  DNA_T <- gsub("T","",DNA)
  T <- nchar(DNA)-nchar(DNA_T)
  print("El número de Tiaminas en la secuencia es:") 
  print(T)
  conteos = A+C+T+G
  }

The problem I have is that the cycle is giving me the total of nitrogenous bases, but it does not stop at base 20.

Any suggestions?

Thanks

    
asked by Carlos Dominguez 14.11.2016 в 19:12
source

1 answer

1

Based on your comment I understand that you can do the following:

First, you must obtain the subsequence: an alternative is substr

subsec <- substr(sec, 0, 20)

I separate the elements:

els <- strsplit(subsec, "")

The above leaves it in a list, which we can "unlink" with the unlist command

els <- unlist(els)

And to count, the table command

tabla <- table(els)
tabla
A C G T 
5 4 6 5 

Always check!     sum (table)     20

I hope it's what you need, and in that case, I hope you remember some commands:)

Morality

Most functions in R are vectorized, what does that mean? that you very rarely need the for or while ;)!

    
answered by 15.11.2016 / 21:21
source