3

I am brand new to python and I am trying to figure out how to validate user input. I want to ask the user to submit a DNA sequence and want to validate that it is a DNA sequence. Acceptable inputs can have upper or lowercase ATCGs and spaces and I'm not sure exactly how to do it.

So far I can ask for the input but not verify it.

import sys
Var1 = raw_input("Enter Sequence 1:")

I then want to do something like:

if Var1 != ATCG (somehow put 'characters that match ATCG or space)
    print "Please input valid DNA sequence"
    sys.exit() (to have it close the program)

Any help? I feel like this should be rather simple but I don't know how to specify that it can be any ATCG, atcg or space.

scooterdude32
  • 65
  • 1
  • 6

2 Answers2

4

You can use all, str.lower, and a generator expression:

if not all(x in "agct " for x in Var1.lower()):
    print "Please input valid DNA sequence"
    sys.exit(1) 

In the above code, the last two lines will be run if any character in Var1 is not one of the following:

"A", "T", "C", "G", " ", "a", "t", "c", "g"
  • As a best practice, [sys.exit](http://docs.python.org/2/library/sys.html#sys.exit) should probably have a non-zero exit code argument since the program didn't succeed. – jpmc26 Mar 03 '14 at 22:34
  • @jpmc26 - Good point. I updated my post. –  Mar 03 '14 at 23:07
1

Also (similar to In Python, how to check if a string only contains certain characters?):

>>> import re
>>> def special_match(strg, search=re.compile(r'[^atcgATCG\s]').search):
...   return bool(search(strg))
>>> if (special_match("atcF")):
...   print "Invalid input"
...
>>> Invalid input
Community
  • 1
  • 1
user2314737
  • 27,088
  • 20
  • 102
  • 114