-5

I'm just starting to learn how to code in python, applying it to the bioinformatics field. Nevertheless, I'm having troubles with the next program:

  • First you introduce a dna sequence (made from g, c, t, a, and n), with the command dna=input("enter your sequence: ")

  • Then I try to identify if the DNA sequence has only the g, c, t, a, and n characters (not specifically in that order). If it doesn't, I want the program to say something like: That's not right, enter a sequence again; and then let you enter a new sequence (and repeat the checking process). If it does only have those characters, I want the program to move forward, but I am not able to do that.

This is more or less what I have done so far... It works for sequences with 3 or more characters, but if you write for example one letter (whichever) after "Not right, enter a sequence again: ", it understands that it's a valid sequence when it's not.

def Start():

dna=input("Enter a sequence:  ")

for i in range(len(dna)):
    if dna[i] not in "actgn":
        dna=input("Not right, enter a sequence again:  ")
    else:
       break
print("here the program will continue")
Pikamander2
  • 7,332
  • 3
  • 48
  • 69

3 Answers3

0

Try this:

dna=input("Enter a sequence:  ")
sequenceCorrect = False
while not sequenceCorrect:
    sequenceCorrect = True
    for i in range(len(dna)):
        if dna[i] not in "actgn":
            sequenceCorrect = False
    if not sequenceCorrect:
        dna=input("Incorrect Sequence Please Try Again:  ")
print("here the program will continue")

user14678216
  • 2,886
  • 2
  • 16
  • 37
0

This is a good start but you didn't the break (to exit the loop) at the right place. Here is my proposition (with comments):


def is_valid(dna_sequence):
    for nucleotide in dna: # we iterate through the chars of our DNA using a foreach
        if nucleotide not in "actgn":
            return False
    return True # if no nucleotide made us return false, it's valid!

def start():
    dna=input("Enter a sequence:  ")
    while not is_valid(dna):
        dna=input("Wrong squence, please enter a new one:  ")

    print("here the program will continue with a valid dna sequence")
Th0rgal
  • 703
  • 8
  • 27
0

I think that it is a very good use case to get introduced to the Python's set. Using set you can make set operations in a single line, clean and elegant. Check out this post for further info.

And, concerning the code:

def is_valid_dna(dna):
  dna_ref = set("actgn") # breaks the string and returns: {'a', 'c', 'g', 'n', 't'}
  dna = set(dna)
  # check: characters in your string belongs to the reference group
  #        the input contains at least 1 valid character
  return dna.issubset(dna_ref) and len(dna)>0 


dna=''
while not is_valid_dna(dna):
  dna = input("Enter a sequence:\n")

After executing it, it returns for different inputs:

Enter a sequence:
1
Enter a sequence:

Enter a sequence:
asdf
Enter a sequence:
actactact
Dharman
  • 30,962
  • 25
  • 85
  • 135
gv12
  • 21
  • 3