Peter Hanson
Peter Hanson

Reputation: 193

Optional Parameter Python?

I need to create a program called extractGenes.py

The command line parameters need to take 2 OR 3 parameters:

  1. -s is an optional parameter, or switch, indicating that the user wwants the spliced gene sequence(introns removed). The user does not have to provide this (meaning he wants the entire gene sequence), but it he does provide it then it must be the first parameter

  2. input file (with the genes)

  3. output file (where the program will create to store the fasta file

The file contains lines like this:

NM_001003443 chr11 + 5925152 592608098 2 5925152,5925652, 5925404,5926898,

However, I am not sure how to include the -s optional parameter into the starting function.

So I started with:

getGenes(-s, input, output):
fp = open(input, 'r')
wp = open(output, "w")

but am unsure as to how to include the -s.

Upvotes: 3

Views: 867

Answers (3)

Raymond Hettinger
Raymond Hettinger

Reputation: 226231

This case is simple enough to use sys.argv directly:

import sys

spliced = False
if '-s' in sys.argv:
    spliced = True
    sys.argv.remove('-s')
infile, outfile = sys.argv[1:]

Alternatively, you can also use the more powerful tools like argparse and optparse to generate a command-line parser:

import argparse

parser = argparse.ArgumentParser(description='Tool for extracting genes')
parser.add_argument('infile', help='source file with the genes')
parser.add_argument('outfile', help='outfile file in a FASTA format')
parser.add_argument('-s', '--spliced', action='store_true', help='remove introns')

if __name__ == '__main__':
    result = parser.parse_args('-s myin myout'.split())
    print vars(result)

Upvotes: 3

zallarak
zallarak

Reputation: 5515

Try something like this:

def getGenes(input, output, s=False):
    if s:
        ...
    else:
        ...

If you input 2 parameters, s will be False; getGenes(input, output)

If you call getGenes() with 3 parameters, s will be the 3rd parameter, so in this case calling it with any non False value will yield the else clause.

Upvotes: 0

dm03514
dm03514

Reputation: 55952

Argparse is a python library that willl take care of optional paremeters for you. http://docs.python.org/library/argparse.html#module-argparse

Upvotes: 2

Related Questions