Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call module written with argparse in iPython notebook

I am trying to pass BioPython sequences to Ilya Stepanov's implementation of Ukkonen's suffix tree algorithm in iPython's notebook environment. I am stumbling on the argparse component.

I have never had to deal directly with argparse before. How can I use this without rewriting main()?

By the by, this writeup of Ukkonen's algorithm is fantastic.

like image 559
Niels Avatar asked Jun 05 '15 01:06

Niels


People also ask

Can I use Argparse in Jupyter notebook?

Basic usage of argparse in jupyter notebookWhen using argparse module in jupyter notebook, all required flag should be False . Before calling parser. parse_args() , we should declare as follows.

How do you use Argparse in Colab?

Using argparse can be broken up into three steps: Defining the primary argument parser object. Adding supported arguments to the argument parser object. Using the final argument parser to parse command-line arguments.


2 Answers

An alternative to use argparse in Ipython notebooks is passing a string to:

args = parser.parse_args() (line 303 from the git repo you referenced.)

Would be something like:

parser = argparse.ArgumentParser(         description='Searching longest common substring. '                     'Uses Ukkonen\'s suffix tree algorithm and generalized suffix tree. '                     'Written by Ilya Stepanov (c) 2013')  parser.add_argument(         'strings',         metavar='STRING',         nargs='*',         help='String for searching',     )  parser.add_argument(         '-f',         '--file',         help='Path for input file. First line should contain number of lines to search in'     ) 

and

args = parser.parse_args("AAA --file /path/to/sequences.txt".split())

Edit: It works

like image 87
tbrittoborges Avatar answered Oct 05 '22 00:10

tbrittoborges


I've had a similar problem before, but using optparse instead of argparse.

You don't need to change anything in the original script, just assign a new list to sys.argv like so:

if __name__ == "__main__":     from Bio import SeqIO     path = '/path/to/sequences.txt'     sequences = [str(record.seq) for record in  SeqIO.parse(path, 'fasta')]     sys.argv = ['-f'] + sequences     main() 
like image 38
BioGeek Avatar answered Oct 05 '22 01:10

BioGeek