If/endif statement stops in middle

Everything else that doesn't fall into one of the other PB categories.
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

If/endif statement stops in middle

Post by localmotion34 »

im trying to examine a string of DNA nucleotides and then translate that to the abbreviations for the corresponding amino acid but the if elseif statement dies a little more than halfway through:

Code: Select all

Procedure.s TranslateDNA(sequence$)
  type=FindString(sequence$, "ATG", 1)
  If type=1
    Trimresult$ = RemoveString(sequence$," ")
    Length1 = Len(Trimresult$)
    For a=1 To Length1 Step 3
      char1$ = Mid(Trimresult$, a, 3)
      If char1$="GCT" Or char1$="GCC" Or char1$="GCA" Or char1$="GCG"
        compliment$="Ala-"
      ElseIf char1$="TGT" Or char1$="TGC"
        compliment$="Cys-"
      ElseIf char1$="GAT" Or char1$="GAC"
        compliment$="Asp-"
      ElseIf char1$="GAA" Or char1$="GAG"
        compliment$="Glu-"
      ElseIf char1$="TTT" Or char1$="TCC"
        compliment$="Phe-"
      ElseIf char1$="GGT" Or char1$="GGC" Or char1$="GGA" Or char1$="GGG"
        compliment$="Gly-"
      ElseIf char1$="CAT" Or char1$="CAC"
        compliment$="His-"
      ElseIf char1$="ATT" Or char1$="ATC"
        compliment$="Ile-"
      ElseIf char1$="AAA" Or char1$="AAG"
        compliment$="Lys-"
      ElseIf char1$="CTT" Or char1$="CTC" Or char1$="CTA" Or char1$="CTG"
        compliment$="Leu-"
      ElseIf char1$="ATG" 
        compliment$="Met-"
      ElseIf char1$="AAT" Or char1$="AAC" 
        compliment$="Asn-"
      ElseIf char1$="CCT" Or char1$="CCC" Or char1$="CCA" Or char1$="CGG"
        compliment$="Pro-"
      ElseIf char1$="CCA" Or "CAG"
        compliment$="Gln-"  ;;;HERE IS THE DIE POINT
      ElseIf char1$="AGA" Or char1$="CGT" Or char1$="CGC" Or char1$="CGA" Or char1$="CGG"
        compliment$="Arg-"
      ElseIf char1$="TCT" Or char1$="TCC" Or char1$="TCA" Or char1$="TCG"
        compliment$="Ser-"
      ElseIf char1$="ACT" Or char1$="ACC" Or char1$="ACA" Or char1$="ACG"
        compliment$="Thr-"
      ElseIf char1$="GTT" Or char1$="GTC" Or char1$="GTA" Or char1$="GTG"
        compliment$="Val-"
      ElseIf char1$="TGG" 
        compliment$="Trp-"
      ElseIf char1$="TAT" Or char1$="TAC"
        compliment$="Tyr-"
      EndIf 
      cstrand$=cstrand$+compliment$ 
    Next 
    ProcedureReturn cstrand$
  EndIf 
EndProcedure 
any ideas on what is going on?????

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Look at the ElseIf statement right above the line you commented. After the Or, there is only
a string. With the string only, this is always a true statement.

You must put "char1$="CAG"" there.
quidquid Latine dictum sit altum videtur
User avatar
Derlidio
User
User
Posts: 77
Joined: Fri Feb 27, 2004 9:19 pm
Location: SP - Brazil

Post by Derlidio »

The line right above the break-point has a missing operand:

Code: Select all

ElseIf char1$="CCA" Or "CAG"
it should be:

Code: Select all

ElseIf char1$="CCA" Or char1$="CAG"
It's not uncommon to forget something among a lot of comparisons :wink:

- Derlidio
Derlidio Siqueira
User avatar
Derlidio
User
User
Posts: 77
Joined: Fri Feb 27, 2004 9:19 pm
Location: SP - Brazil

Post by Derlidio »

I think your code can became more clean and easy to inspect by using a "Select", like this:

Code: Select all

Procedure.s TranslateDNA(sequence$) 
  type=FindString(sequence$, "ATG", 1) 
  If type=1 
    Trimresult$ = RemoveString(sequence$," ") 
    Length1 = Len(Trimresult$) 
    For a=1 To Length1 Step 3 
      char1$ = Mid(Trimresult$, a, 3)
      Select char1$

        Case "GCT": compliment$="Ala-" 
        Case "GCC": compliment$="Ala-" 
        Case "GCG": compliment$="Ala-" 
        
        Case "TGT": compliment$="Cys-" 
        Case "TGC": compliment$="Cys-" 
        
        Case "GAT": compliment$="Asp-" 
        Case "GAC": compliment$="Asp-" 
         
        Case "GAA": compliment$="Glu-" 
        Case "GAG": compliment$="Glu-" 
         
        Case "TTT": compliment$="Phe-" 
        Case "TCC": compliment$="Phe-" 
        
        Case "GGT": compliment$="Gly-" 
        Case "GGC": compliment$="Gly-" 
        Case "GGA": compliment$="Gly-" 
        Case "GGG": compliment$="Gly-" 
        
        Case "CAT": compliment$="His-" 
        Case "CAC": compliment$="His-" 
        
        Case "ATT": compliment$="Ile-" 
        Case "ATC": compliment$="Ile-" 
        
        Case "AAA": compliment$="Lys-" 
        Case "AAG": compliment$="Lys-" 
        
        Case "CTT": compliment$="Leu-" 
        Case "CTC": compliment$="Leu-" 
        Case "CTA": compliment$="Leu-" 
        Case "CTG": compliment$="Leu-" 
        
        Case "ATG": compliment$="Met-"  
        
        Case "AAT": compliment$="Asn-" 
        Case "AAC": compliment$="Asn-" 
        
        Case "CCT": compliment$="Pro-" 
        Case "CCC": compliment$="Pro-" 
        Case "CCA": compliment$="Pro-" 
        Case "CGG": compliment$="Pro-" 
        
        Case "CCA": compliment$="Gln-"
        Case "CAG": compliment$="Gln-"
        
        Case "CCA": compliment$="Gln-"
        Case "CAG": compliment$="Gln-"
      
        Case "AGA": compliment$="Arg-" 
        Case "CGT": compliment$="Arg-" 
        Case "CGC": compliment$="Arg-" 
        Case "CGA": compliment$="Arg-" 
        Case "CGG": compliment$="Arg-" 
        
        Case "TCT": compliment$="Ser-" 
        Case "TCC": compliment$="Ser-" 
        Case "TCA": compliment$="Ser-" 
        Case "TCG": compliment$="Ser-" 
        
        Case "ACT": compliment$="Thr-" 
        Case "ACC": compliment$="Thr-" 
        Case "ACA": compliment$="Thr-" 
        Case "ACG": compliment$="Thr-" 
      
        Case "GTT": compliment$="Val-" 
        Case "GTC": compliment$="Val-" 
        Case "GTA": compliment$="Val-" 
        Case "GTG": compliment$="Val-" 
        
        Case "TGG": compliment$="Trp-" 
        
        Case "TAT": compliment$="Tyr-" 
        Case "TAC": compliment$="Tyr-" 
        
      EndSelect
      cstrand$=cstrand$+compliment$ 
    Next 
    ProcedureReturn cstrand$ 
  EndIf 
EndProcedure 
Derlidio Siqueira
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Personally I would do it this way...
It's easier to expand on and the code looks pretty :)

Code: Select all

Structure DNAdata
  nuc.s
  acid.s
EndStructure
NewList dna.DNAdata()

For tmp=1 To 55
  AddElement(dna())
  Read dna()\nuc
  Read dna()\acid
Next


Procedure.s TranslateDNA(sequence$)
  If FindString(sequence$,"ATG",1)=1
    Trimresult$ = RemoveString(sequence$," ")
    Length1 = Len(Trimresult$)
    For a=1 To Length1 Step 3
      char1$ = Mid(Trimresult$, a, 3)
      ForEach dna()
        If char1$=dna()\nuc
          cstrand$+dna()\acid
          Break
        EndIf
      Next
    Next
    ProcedureReturn cstrand$
  EndIf
EndProcedure 



DataSection
  Data.s "GCT","Ala-","GCC","Ala-","GCA","Ala-","GCG","Ala-","TGT","Cys-","TGC","Cys-"
  Data.s "GAT","Asp-","GAC","Asp-","GAA","Glu-","GAG","Glu-","TTT","Phe-","TCC","Phe-"
  Data.s "GGT","Gly-","GGC","Gly-","GGA","Gly-","GGG","Gly-","CAT","His-","CAC","His-"
  Data.s "ATT","Ile-","ATC","Ile-","AAA","Lys-","AAG","Lys-","CTT","Leu-" 
  Data.s "CTC","Leu-","CTA","Leu-","CTG","Leu-","ATG","Met-","AAT","Asn-","AAC","Asn-"
  Data.s "CCT","Pro-","CCC","Pro-","CCA","Pro-","CGG","Pro-","CCA","Gln-","CAG","Gln-"
  Data.s "AGA","Arg-","CGT","Arg-","CGC","Arg-","CGA","Arg-","CGG","Arg-" 
  Data.s "TCT","Ser-","TCC","Ser-","TCA","Ser-","TCG","Ser-"
  Data.s "ACT","Thr-","ACC","Thr-","ACA","Thr-","ACG","Thr-"
  Data.s "GTT","Val-","GTC","Val-","GTA","Val-","GTG","Val-"
  Data.s "TGG","Trp-","TAT","Tyr-","TAC","Tyr-"
EndDataSection
Image Image
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

ARRRGGGGGGGGHHHHHHHHHHHHHHHHHHHHHHHHHH!!

see, i was working on that all day and by then i couldnt see my mistakes. thanks so much guys for helping out. and i never thought to use select/case either. and paul's method looks really good, except that i am going to make a DLL out of the DNA string procedures and i dont know if the DATA SECTION will be usable from calling the DNA DLL. thanks again for your help.

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Sure, compile this as DLL (save as dna.dll)

Code: Select all

Structure DNAdata
  nuc.s
  acid.s
EndStructure

ProcedureDLL AttachProcess(Instance)
  NewList dna.DNAdata()
  For tmp=1 To 55
    AddElement(dna())
    Read dna()\nuc
    Read dna()\acid
  Next
EndProcedure


ProcedureDLL.s TranslateDNA(sequence$)
  Global cstrand$
  cstrand$=""
  If FindString(sequence$,"ATG",1)=1
    Trimresult$ = RemoveString(sequence$," ")
    Length1 = Len(Trimresult$)
    For a=1 To Length1 Step 3
      char1$ = Mid(Trimresult$, a, 3)
      ForEach dna()
        If char1$=dna()\nuc
          cstrand$+dna()\acid
          Break
        EndIf
      Next
    Next
  EndIf
  ProcedureReturn cstrand$
EndProcedure


DataSection
  Data.s "GCT","Ala-","GCC","Ala-","GCA","Ala-","GCG","Ala-","TGT","Cys-","TGC","Cys-"
  Data.s "GAT","Asp-","GAC","Asp-","GAA","Glu-","GAG","Glu-","TTT","Phe-","TCC","Phe-"
  Data.s "GGT","Gly-","GGC","Gly-","GGA","Gly-","GGG","Gly-","CAT","His-","CAC","His-"
  Data.s "ATT","Ile-","ATC","Ile-","AAA","Lys-","AAG","Lys-","CTT","Leu-"
  Data.s "CTC","Leu-","CTA","Leu-","CTG","Leu-","ATG","Met-","AAT","Asn-","AAC","Asn-"
  Data.s "CCT","Pro-","CCC","Pro-","CCA","Pro-","CGG","Pro-","CCA","Gln-","CAG","Gln-"
  Data.s "AGA","Arg-","CGT","Arg-","CGC","Arg-","CGA","Arg-","CGG","Arg-"
  Data.s "TCT","Ser-","TCC","Ser-","TCA","Ser-","TCG","Ser-"
  Data.s "ACT","Thr-","ACC","Thr-","ACA","Thr-","ACG","Thr-"
  Data.s "GTT","Val-","GTC","Val-","GTA","Val-","GTG","Val-"
  Data.s "TGG","Trp-","TAT","Tyr-","TAC","Tyr-"
EndDataSection
and use this to test DLL

Code: Select all

If OpenLibrary(0,"dna.dll")
  TranslateDNA=IsFunction(0,"TranslateDNA")
  
  Debug PeekS(CallFunctionFast(TranslateDNA,"ATGAAAACT"))
  Debug PeekS(CallFunctionFast(TranslateDNA,"ATGGGAACT"))
  Debug PeekS(CallFunctionFast(TranslateDNA,"ATGACGGCC"))
  
  CloseLibrary(0)
EndIf
Image Image
Post Reply