Problem with a function iterating over a matrix in R
0
0
Entering edit mode
3.6 years ago
Pia ▴ 10

I am trying to calculate my own substitution matrix from a multiple sequence alignment. In one of the steps, I need a function that will calculate the number of times each residue pair shows up and the total number of ungapped pair slots. The code is difficult to understand without context, but I think my problem is with syntax, so if anyone has any idea why it's returning 0, it would help a lot.

0 is the initial value of the variable, so I think my code is not even entering the loops for some reason

    ResiduePairCalculator = function(combination_alignmentmatrix, aa_pairs)

{

totalpaircount=0
for(i in 1:ncol(combination_alignmentmatrix))
{
list_1=strsplit(combination_alignmentmatrix[1,i],split="")
list_2=strsplit(combination_alignmentmatrix[2,i],split="")

for(j in 1:length(list_1))
{
if((list_1[[j]]!="-") && (list_2[[j]]!="-"))
{
totalpaircount=totalpaircount+1

for(k in 1:ncol(aa_pairs))
{
pair=paste(aa_pairs[1,k] , aa_pairs[2,k], sep = "")
match1=paste(list_1[[j]],list_2[[j]], sep = "")
match2=paste(list_2[[j]],list_1[[j]], sep = "")

if((pair==match1) || (pair==match2))
{
aa_pairs[3,k]=aa_pairs[3,k]+1
break
}
}

}
}

}
return(totalpaircount)
}

R Error Loop Iteration • 468 views
0
Entering edit mode

0 is the initial value of the variable, so I think my code is not even entering the loops for some reason

No, all you can say is the the incrementing line is probably not being run. No statement can be made about the loop with just the return value.