Help needed in debugging this function


#1

Hi, help me in debugging this function, it is giving me the same answer every time despite of different arguments. also please let me know if i need to add further more data into it.

ref=c("I=9.68", "L=9.60", "M=9.21", "F=9.13", "T=9.10", "W=9.39", "V=9.62", "A=9.69", "N=8.84", "Q=9.13", "G=9.60", "P=10.60", "S=9.15", "D=3.9", "E=4.7", "C=8.18", "Y=10.46", "H=6.04", "K=10.54", "R=12.48")

I=9.68
L=9.60
M=9.21
F=9.13
T=9.10
W=9.39
V=9.62
A=9.69
N=8.84
Q=9.13
G=9.60
P=10.60
S=9.15
D=3.9
E=4.7
C=8.18
Y=10.46
H=6.04
K=10.54
R=12.48



pi<-function(compound)
{
molecules=unlist(strsplit(compound,""))

DNum=0
ENum=0
CNum=0
YNum=0
HNum=0
KNum=0
RNum=0

for (i in molecules){
    if (i == 'D')
         DNum= DNum +1

    if (i == 'E')
         ENum= ENum +1

    if (i == 'C')
         CNum= CNum +1

    if (i == 'Y')
         YNum= YNum +1

    if (i == 'H')
         HNum= HNum +1

    if (i == 'K')
         KNum= KNum +1

    if (i == 'R')
         RNum= RNum +1
}

QN1=0  
QN2=0  
QN3=0  
QN4=0  
QN5=0 
QP1=0
QP2=0
QP3=0  
QP4=0

    
pH=0
test=1;

while(1){
    QN1=-1/((1+(10)^(3.65-pH)))       
    QN2=-DNum/(1+((10)^(D-pH)))           
    QN3=-ENum/(1+((10)^(E-pH)))            
    QN4=-CNum/(1+((10)^(C-pH)))            
    QN5=-YNum/(1+((10)^(Y-pH)))        
    QP1=HNum/(1+((10)^(pH-H)))          
    QP2=1/(1+((10)^(pH-8.2)))           
    QP3=KNum/(1+((10)^(pH-K)))            
    QP4=RNum/(1+((10)^(pH-R)))            

    NQ=QN1+QN2+QN3+QN4+QN5+QP1+QP2+QP3+QP4

    if(pH>=14)
        print ("Something is wrong, pH is greater than 14")
        break


    if(NQ<=0)
        print(NQ)
        break

    pH=pH+0.1

}
    
    return(c("pI of a protein:"=pH))
}

And the result


pi("DECYHKR")
[1] 0
#> Error: <text>:61:3: unexpected '}'
#> 60: + 
#> 61: + }
#>       ^

Created on 2018-03-27 by the reprex package (v0.2.0).

This is the answer i am getting but the right answer should be 6.74 (according to Expasy(an online tool to calculate pI value of a protein))


#5

#7

First of all you haven't included a reprex. The error you are showing says R ran into an issue on line 61 of what what executed. But there is nothing in what you are showing in your code snippets here that could produce that error. That's why you need to post a reprex so we can see what is actually happening.

Just an inspection of your code shows raises some questions.

What is the purpose of while(1) ?

You initialize pH to zero with pH <- 0 but your code never changes that value, that is why it is returning 0 for pH..

The while(1) block in your code only executed to the first break in your code then exits that block.


#8

What should i use to make this as a infinity loop? I used 'while' for the same purpose ( for which i am not sure if it is correcor not).


#9

The R package Peptides has a function pI that will help you.


#10

You make a loop infinite by never exiting it, but why would you want to do that. If you did the function would never return.

But you are exiting it at the first break in your code, so what you have isn't a loop at all.

Here is an example of an loop like yours, but it will only execute once no matter what.


a = 0
while(1) {
    if(TRUE)
        a <- a + 2
# this break will always be executed.
    break
}
a
#> [1] 2

Created on 2018-03-27 by the reprex package (v0.2.0).


#11

Thankyou so much sir, but i am willing to recreate a function for myself. If you know anything about the formula or algorithm which would help me then please let me know.
Thankyou!