Optional Python parameter?

I need to create extractGenes.py program

Command line parameters must accept 2 OR 3 parameters:

  • -sis an optional parameter or switch indicating that the user has a sequence of spliced ​​genes (introns removed). The user does not have to provide this (which means that he wants the whole sequence of genes), but he provides it to him, then he should be the first parameter

  • input file (with genes)

  • output file (where the program will create a fasta p>

The file contains lines like this:

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

However, I'm not sure how to include the parameter -sin the start function.

So, I started with:

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

but I'm not sure how to turn it on -s.

+3
3

sys.argv:

import sys

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

, argparse optparse, :

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)
+3

Try something like this:

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

If you enter 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 False value will result in an else clause.

0
source

All Articles