Help needed in debugging a function


#1

HI all,
i want a little help in debugging this function. Here i want my function to check in the argument if the required character is present or not then according to that it solve the equation then print the answer. But i am unable to find why it is just solving the equation but not the whole code, also i am little confuse that if i have used the correct code for the same or not please help me.

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

}
print(pH)
print(NQ)
}

Created on 2018-05-04 by the reprex package (v0.2.0).

pi("DECYHKRIL")
[1] 0
[1] 3.999629

Created on 2018-05-04 by the reprex package (v0.2.0).

expected answer is 6.74 according to expasy ( an online tool to calculate isoelectric point of an amino acid sequence)


#2

Your question is too vague and for me it is not that simple to jump in your code and figure out what is going on.
Please, take a look at this webminar.

https://www.rstudio.com/resources/videos/debugging-techniques-in-rstudio/

I believe this will help you with many debugging techniques.


#3

Thank you so much for this, but could you please tell me the error which i have make or you could modify my algorithm little bit?