1

Currently this is my python script (TagGen) which has one function:

def SJtag(file,len_tag):

    import csv
    reader = csv.reader(open(file), dialect='excel-tab' )
    for row in reader:
        qstarts = row[1].split(",")[1:-1] 
        n = len_tag/2
        for i in qstarts:  
            name = row[0]

            start = int(i)-n
            if start<0:
                start = 0      

            end = int(i)+n
            if end>len(row[2]):
                end=len(row[2])

            tag = row[2][start:end]    
            print name, i, tag, len(tag)

SJtag("QstartRefseqhg19.head",80)

I want to give the file and len_tag parameters of the SJtag function using bash comand line, something like this:

python ./TagGen QstartRefseqhg19.head 80

How can I do this, or some thing similar?

Thanks for your help!

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
Geparada
  • 2,898
  • 9
  • 31
  • 43

4 Answers4

3

You could use the argparse module for that.

From the docs,

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
           help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
           const=sum, default=max,
           help='sum the integers (default: find the max)')

args = parser.parse_args()
print args.accumulate(args.integers)

If you are using a version of python earlier than 2.7, you should use the optparse module, to achieve a similar effect.

lprsd
  • 84,407
  • 47
  • 135
  • 168
  • That's probably a bit of overkill in this case. argparse really only comes in handy when you want to take command line switches, etc, but in a simple case like this, argv works. – Rafe Kettler May 11 '11 at 04:21
  • Rafe, If all you need is a couple of command line parameters, may be. But not too far when you complicate that too much and are much better to use an inbuilt library function. :) – lprsd May 11 '11 at 04:29
  • oh, absolutely. It's also worth noting that argparse is 2.7 and up. – Rafe Kettler May 11 '11 at 04:32
2

sys.argv is the arguments list, with the first element being the script name. It's a list of strings, so if any of the parameters are numbers, you'll have to convert them using int() or float().

So, if you called a script like so:

$ python myscript.py 1 foo bar baz

sys.argv would be this:

["myscript.py", "1", "foo", "bar", "baz"]

In your case, you could make your script this:

import sys
import csv

def SJtag(file,len_tag):
    reader = csv.reader(open(file), dialect='excel-tab' )
    for row in reader:
        qstarts = row[1].split(",")[1:-1] 
        n = len_tag/2
        for i in qstarts:  
            name = row[0]

            start = int(i)-n
            if start<0:
                start = 0      

            end = int(i)+n
            if end>len(row[2]):
                end=len(row[2])

            tag = row[2][start:end]    
            print name, i, tag, len(tag)

if __name__ == '__main__':
    SJtag(sys.argv[1], int(sys.argv[2]))
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
1

I'd recommend argparse and a nice wrapper for it called plac. In your case all you'd need to do is:

if __name__ == '__main__':
    import plac; plac.call(SJtag)

...and placwill handle everything, as it is able to figure out the command-line arguments to use from the signature of your function. Very cool, check out the docs for more (it can do a lot more).

Zach Kelling
  • 52,505
  • 13
  • 109
  • 108
0

Just refer to this.

http://www.dalkescientific.com/writings/NBN/python_intro/command_line.html

Aater Suleman
  • 2,286
  • 18
  • 11