This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R - Error no 'dimnames' attribute for array

I have written the following code:

library(TraMineR)

sts.data <- c(

"TTATAG",

  "AGATAT",

  "AGATAG",

  "ATATCT",

  "AGATAG",

  "AGATAG",

  "AGATAG",

  "TGATAA",

  "AGATAA",

  "AGATAA",

  "CGATAG",

  "AGAGTT",

  "TGATAA",

  "TGATAA",

  "AGATGG",

  "AGATAG",

  "AGATTG",

  "AGATAA",

  "TGATAA",

  "AGATAA",

  "AGATAG",

  "TGATAG",

  "TGATCA",

  "TTATCA",

  "AGATGG",

  "TGATAT",

  "AGATAG",

  "TGATAA",

  "GGATAC",

  "AGATAA",

  "CGATAA",

  "TGATAG",

  "AGATAA",

  "TGATTA",

  "AGATAA",

  "AGATAG",

  "TGATAT",

  "AGATAA",

  "TCAGAG",

  "AAGTAG",

  "AGATTA",

  "TGATAG",

  "TGATAG",

  "AGATAC",

  "TGATTG",

  "AGATTA",

  "AGAATA",

  "AGATAA",

  "AGATTA",

  "AGCTTC"

)

seq1 <- seqdef(seqdecomp(sts.data,sep=''))

seqstatd(seq1)

d <-c(A, C, G, T)

pwm<-matrix(data = d, nrow = 4, ncol = 6)

pwm


bg<- c(0.25, 0.25, 0.25, 0.25)

makepssm <-pwm/bg

makepssm <- makepssm+0.01

pssm <- log2(makepssm)

pssm


calscore<-function(seq,pssm){

score <- 0

for (j in 1:length(pssm[1,])){

  score<-score+pssm[seq[j],j]}

return(score)

}


calscore("TTTTTT",pssm)

and when I try to run it it produces the "Error in pssm[seq[j], j] : no 'dimnames' attribute for array"

How can I solve this?

Yesterday it worked multiple times. Could this be a problem of Rstudio?

r error dimnames

Are A, C, G, T variables or characters :

d <-c(A, C, G, T)

If they are variables, they are not in your script.

If they are characters, you cannot divide characters matrix by float values here :

makepssm <-pwm/bg

The

seq1 <- seqdef(seqdecomp(sts.data,sep=''))

seqstatd(seq1)

gives as an outcome three tables.

the first one (which I need) is:

[State frequencies]

   [1]  [2]  [3]  [4]  [5]  [6]

A 0.60 0.02 0.96 0.02 0.72 0.44

C 0.04 0.02 0.02 0.00 0.06 0.06

G 0.02 0.90 0.02 0.04 0.04 0.40

T 0.34 0.06 0.00 0.94 0.18 0.10



In the data frame A,C,G,T are characterised as values. 
Also the pssm matrix is calculated and gives an outcome of:

 [,1]       [,2]      [,3]      [,4]       [,5]       [,6]

[1,]  1.269033  1.5310695 -3.473931 -3.473931 -2.5563933 -6.6438562

[2,] -3.473931  0.8237494 -6.643856  1.851999  0.6870607  1.9145645

[3,]  1.944858 -2.5563933 -2.000000 -3.473931  0.4541759 -0.4540316

[4,] -3.473931 -3.4739312 -2.000000 -2.556393 -2.0000000 -1.2863042

I tried a different approach

library(TraMineR)

sts.data <- c(

"TTATAG",

  "AGATAT",

  "AGATAG",

  "ATATCT",

  "AGATAG",

  "AGATAG",

  "AGATAG",

  "TGATAA",

  "AGATAA",

  "AGATAA",

  "CGATAG",

  "AGAGTT",

  "TGATAA",

  "TGATAA",

  "AGATGG",

  "AGATAG",

  "AGATTG",

  "AGATAA",

  "TGATAA",

  "AGATAA",

  "AGATAG",

  "TGATAG",

  "TGATCA",

  "TTATCA",

  "AGATGG",

  "TGATAT",

  "AGATAG",

  "TGATAA",

  "GGATAC",

  "AGATAA",

  "CGATAA",

  "TGATAG",

  "AGATAA",

  "TGATTA",

  "AGATAA",

  "AGATAG",

  "TGATAT",

  "AGATAA",

  "TCAGAG",

  "AAGTAG",

  "AGATTA",

  "TGATAG",

  "TGATAG",

  "AGATAC",

  "TGATTG",

  "AGATTA",

  "AGAATA",

  "AGATAA",

  "AGATTA",

  "AGCTTC"

)

seq1 <- seqdef(seqdecomp(sts.data,sep=''))

seqstatd(seq1)

**t1<-c(A)

t2<-c(C)

t3<-c(G)

t4<-c(T)

pwm <- rbind(t1,t2,t3,t4)

pwm**

bg<- c(0.25, 0.25, 0.25, 0.25)

makepssm <-pwm/bg

makepssm <- makepssm+0.01

pssm <- log2(makepssm)

pssm


calscore<-function(seq,pssm){

score <- 0

for (j in 1:length(pssm[1,])){

  score<-score+pssm[seq[j],j]}

return(score)

}

but now the error is Error in pssm[seq[j], j] : subscript out of bounds.

You cannot access A, C, G, T as you do

seqstatd(seq1) outputs a list of element, you need to grab the "State frequencies" informations

Try to run only these lines :

seq1 <- seqdef(seqdecomp(sts.data,sep=''))
seqstatd(seq1)
t1<-c(A)

You will see that t1 is empty because A cannot be found

I tried that but t1 gives

t1<-c(A)

 t1

[1] 0.60 0.02 0.96 0.02 0.72 0.44
  

so does t2 etc. For some reason the code worked fine yesterday even though I received a warning that A cannot be found and today I don't receive this warning but the code doesn't work... Oh temporas, oh mores...

1 answer

You can try this :

library(Biostrings)

sts.data <- c(

"TTATAG",

  "AGATAT",

  "AGATAG",

  "ATATCT",

  "AGATAG",

  "AGATAG",

  "AGATAG",

  "TGATAA",

  "AGATAA",

  "AGATAA",

  "CGATAG",

  "AGAGTT",

  "TGATAA",

  "TGATAA",

  "AGATGG",

  "AGATAG",

  "AGATTG",

  "AGATAA",

  "TGATAA",

  "AGATAA",

  "AGATAG",

  "TGATAG",

  "TGATCA",

  "TTATCA",

  "AGATGG",

  "TGATAT",

  "AGATAG",

  "TGATAA",

  "GGATAC",

  "AGATAA",

  "CGATAA",

  "TGATAG",

  "AGATAA",

  "TGATTA",

  "AGATAA",

  "AGATAG",

  "TGATAT",

  "AGATAA",

  "TCAGAG",

  "AAGTAG",

  "AGATTA",

  "TGATAG",

  "TGATAG",

  "AGATAC",

  "TGATTG",

  "AGATTA",

  "AGAATA",

  "AGATAA",

  "AGATTA",

  "AGCTTC"

)

d <- apply(t(consensusMatrix(DNAStringSet(sts.data))[1:4,]), 1, function(x){x/sum(x)})
pwm<-matrix(data = d, nrow = 4, ncol = 6)
rownames(pwm) <- c("A", "C", "G", "T")
pwm

bg<- c(0.25, 0.25, 0.25, 0.25)
makepssm <-pwm/bg
makepssm <- makepssm+0.01
pssm <- log2(makepssm)
pssm


calscore<-function(seq,pssm){
score <- 0
for (j in 1:length(pssm[1,])){
  score<-score+pssm[seq[j],j]}
return(score)
}

calscore(strsplit("TTTTTT", "")[[1]],pssm)

Thank you so much, that seems to do the job :)

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one if they work.
Upvote|Bookmark|Accept

Log in to answer this question.