This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to use cmd?

"Hello world", "...again",

I am using Python (2.7) and, with previous help from Biostars, created a class that parses a FASTA file in a dictionary output with this code:

class FastaFile(object):
    def __init__(self, path):
        self.path = path
        self._map = {}
        self.__fasta_iter()

    def __str__(self):
        return self._map.__str__()

    def __fasta_iter(self):
        fasta = open(self.path)
        fasta_iter = (x[1] for x in groupby(fasta, lambda line: line[0] == ">"))
        for header in fasta_iter:
            header = header.next()[1:9].strip()
            seq = "".join(s.strip() for s in fasta_iter.next())
            self._map[header] = seq

    def getitem(self, k):
        return self._map[k]

    def __iter__(self):
        for k in self._map:
            yield k, self._map[k]

cff = FastaFile("obs.fasta")

for sequence_id, sequence in cff:
    print sequence_id, sequence

What I would like to be able to do is have a user input a sequence identification number into the command line and the output given will be the sequence associated with that number. The problem is I don't know how!

I've been reading into raw_input and cmd on the python library website though not entirely sure how to begin as I find it difficult to read large paragraphs with no clear examples (dyslexic). I think cmd is what I should be using and I would like to use class as much as possible. Not entirely sure what the next step forward is. Any suggestions would be very helpful either useful code (me likey) or even links to websites with clear (maybe colourful) examples.

dictionary fasta sequences python command line

You already have everything in place with cff._map['sequence identifier number'], so read that with sys.argv[1] (or use the argparse module).

1 answer

Hi, is this along the lines of what your're looking for? Add this function to your FastaFile class:

def user_input(self):
    # This function takes a key as a user-input (from keyboard)
    # and looks it up in the 'map' dictionary, then
    # prints the value for whatever is in the dictionary.

    while True:
        # while True loops indefinitely (unless we tell it to stop)
        # Prompt user for input
        command = raw_input("Please provide key: ")
        # Command is now whatever the user have written.
        # First, if 'exit' is written, we'll just stop asking for input
        if command.lower() == "exit":
            print "Goodbye"
            break
        # We'll now use whatever they wrote (i.e. the 'command'
        # variable) as a lookup-key in your dictionary. Note
        # that since the user can type anything they want, there's
        # no guarantee they'll write a valid key, so we'll check
        # for that first:
        if command not in self._map.keys():
            # Key is not valid
            print "Sorry, %s is not a key in the dictionary" % command
        else:
            # Key is valid: Do something (e.g. look it up and print the value)
            value = self.getitem(command)
            print "%s: %s" % (command, value)

Then simply call that function from somewhere after your FastaFile object is created, e.g. in the bottom of your current script:

cff.user_input()

EDIT: Wops, forgot a self in there (value = getitem(command) corrected to value = self.getitem(command)). I've update the answer, it should be OK now. Thanks for pointing it out in the comments, Devon Ryan.

Hi! Thanks for responding!

I'm currently in the process of using the code. However, I get a dreaded red error message saying NameError: global name 'getitem' is not defined. But when the user inputs an incorrect ID number it works smoothly. :)

Fixed it! Used self.getitem(command) Thanks again!

Log in to answer this question.