How to call module written with argparse in iPython notebook

后端 未结 9 1450
半阙折子戏
半阙折子戏 2020-12-29 02:04

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 argp

相关标签:
9条回答
  • 2020-12-29 02:58

    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()
    
    0 讨论(0)
  • 2020-12-29 03:00

    If you don't want to change any of the arguments and working mechanisms from the original argparse function you have written or copied.

    To let the program work then there is a simple solution that works most of the time.

    You could just install jupyter-argparser using the below command:

    pip install jupyter_argparser
    

    The codes work without any changes thanks to the maintainer of the package.

    0 讨论(0)
  • 2020-12-29 03:01

    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

    0 讨论(0)
提交回复
热议问题