简体   繁体   English

argparse 在调用脚本时有效,但在合并到新脚本时无效

[英]argparse works when calling script but not when incorporated to new script

Here is my basic issue:这是我的基本问题:

I have the following: file name: parseFastq.py execution: via command line code to run it: python3 parseFastq.py --fastq /Users/remaining_dir/test1.fastq我有以下内容: 文件名:parseFastq.py 执行:通过命令行代码运行它:python3 parseFastq.py --fastq /Users/remaining_dir/test1.fastq

This code works!!!此代码有效!

However, when I copy the components of parseFastq.py issues arise.但是,当我复制 parseFastq.py 的组件时,就会出现问题。

Below is the code:下面是代码:

Class is first defined...this part works and runs fine on my new script. Class 是首先定义的……这部分在我的新脚本上运行良好。

import argparse
import gzip
#Example use is 
# python parseFastq.py --fastq /Users/remaining_dir/test1.fastq

################################################
# You can use this code and put it in your own script
class ParseFastQ(object):
    """Returns a read-by-read fastQ parser analogous to file.readline()"""
    def __init__(self,filePath,headerSymbols=['@','+']):
        """Returns a read-by-read fastQ parser analogous to file.readline().
        Exmpl: parser.__next__()
        -OR-
        Its an iterator so you can do:
        for rec in parser:
            ... do something with rec ...

        rec is tuple: (seqHeader,seqStr,qualHeader,qualStr)
        """
        if filePath.endswith('.gz'):
            self._file = gzip.open(filePath)
        else:
            self._file = open(filePath, 'rU')
        self._currentLineNumber = 0
        self._hdSyms = headerSymbols

    def __iter__(self):
        return self

    def __next__(self):
        """Reads in next element, parses, and does minimal verification.
        Returns: tuple: (seqHeader,seqStr,qualHeader,qualStr)"""
        # ++++ Get Next Four Lines ++++
        elemList = []
        for i in range(4):
            line = self._file.readline()
            self._currentLineNumber += 1 ## increment file position
            if line:
                elemList.append(line.strip('\n'))
            else: 
                elemList.append(None)

        # ++++ Check Lines For Expected Form ++++
        trues = [bool(x) for x in elemList].count(True)
        nones = elemList.count(None)
        # -- Check for acceptable end of file --
        if nones == 4:
            raise StopIteration
        # -- Make sure we got 4 full lines of data --
        assert trues == 4,\
               "** ERROR: It looks like I encountered a premature EOF or empty line.\n\
               Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._currentLineNumber)
        # -- Make sure we are in the correct "register" --
        assert elemList[0].startswith(self._hdSyms[0]),\
               "** ERROR: The 1st line in fastq element does not start with '%s'.\n\
               Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._hdSyms[0],self._currentLineNumber) 
        assert elemList[2].startswith(self._hdSyms[1]),\
               "** ERROR: The 3rd line in fastq element does not start with '%s'.\n\
               Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._hdSyms[1],self._currentLineNumber) 
        # -- Make sure the seq line and qual line have equal lengths --
        assert len(elemList[1]) == len(elemList[3]), "** ERROR: The length of Sequence data and Quality data of the last record aren't equal.\n\
               Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._currentLineNumber) 

        # ++++ Return fatsQ data as tuple ++++
        return tuple(elemList)
##########################################################################

This is the code that will not work when calling it in the same script;这是在同一个脚本中调用它时不起作用的代码; it has to do with putting the pieces in:它与将碎片放入:

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process fasq files and seperaate into 4 categories')
parser.add_argument("-f",  "--fastq", required=True, help="Place fastq inside here")
args = parser.parse_args()

fastqfile = ParseFastQ(args.fastq)

I tried the following and I cannot get fastqfile which should contain a tuple with the following: (seqHeader,seqStr,qualHeader,qualStr)我尝试了以下方法,但无法获得应包含以下元组的 fastqfile: (seqHeader,seqStr,qualHeader,qualStr)

Attemp:尝试:

parser.add_argument("-/Users/remaining_dir/test1.fastq",  "--fastq", required=True, help="Place fastq inside here")

Error:错误:

argument -/Users/remaining_dir/test1.fastq/--fastq: conflicting option string: --fastq

Attemp:尝试:

parser.add_argument("-/Users/remaining_dir/test1.fastq",  "-@", required=True, help="Place fastq inside here")

Out[332]:输出[332]:

_StoreAction(option_strings=['-/Users/remaining_dir/test1.fastq', '-@'], dest='/Users/remaining_dir/test1.fastq', nargs=None, const=None, default=None, type=None, choices=None, help='Place fastq inside here', metavar=None)

next line:下一行:

Error:错误:

usage:  [-h] -/Users/remaining_dir/test1.fastq
        /USERS/REMAINING_DIR/TEST1.FASTQ
: error: the following arguments are required: -/Users/remaining_dir/test1.fastq/-@
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

 when %tb selected the following info was give: 
 File "/Users/brownbear/opt/anaconda3/lib/python3.7/argparse.py", line 2508, in error
    self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

  File "/Users/brownbear/opt/anaconda3/lib/python3.7/argparse.py", line 2495, in exit
    _sys.exit(status)

if helpful, I am including some sample fastq data如果有帮助,我将包含一些示例 fastq 数据

@seq13534-419
GCAGTAGCGGTCATAAGTGGTACATTACGAGATTCGGAGTACCATAGATTCGCATGAATCCCTGTGGATACGAGAGTGTGAGATATATGTACGCCAATCCAGTGTGATACCCATGAGATTTAGGACCGATGATGGTTGAGGACCAAGGATTGACCCGATGGATGCAGATTTGACCCCAGATAGAATAAATGCGATGAGATGATTTGGCCGATAGATAGATAGTGTCGTGAGGTGACGTCCGTCACTGGACGAA
+
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIDIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFFFFDFFDFFDDFDFDFFFFDDFFDDFDDFF
@seq86249-867
GGATTAGCGGTCATAAGTCGTACATTACGAGATTCGGAGTACCATAGATTCGCATGAATCCCTGTGGATACGAGAGTGTGAGATATATGTACGCCAATCCAGTGTGATACCCATGAGATTTAGGACCGATGATGGTTGAGGACCAAGGATTGACCCGATGGATGCAGATTTGACCCCAGATAGAATAAATGCGATGAGATGATTTGGCCGATAGATAGATAGAGGTCAGTATAACCTCTCAAAGCTTTATCTACGGATGGATCCGCGC
+
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIDIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIDDFDDDDDDFFDFDDFDDDFDFFDDFFFFFFFFFDDFDFFDDFDDF
@seq46647-928
GACCTAGCGGTCATAAGTGGTACATTACGAGATTCGGAGTACCATAGATTCGCATGAATCCCTGTGGATACGAGAGTGTGAGATATATGTACGCCAATCCAGTGTGATACCCATGAGATTTAGGACCGATGATGGTTGACGACCAAGGATTGACCCGATGGATGCAGATTTGACCCCAGATAGAATAAATGCGATGAGATGATTTGGCCGATAGATAGATAGTAAGTAAATGCCACGGACTCGTCACGTG
+
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIDIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIDDDFDFDFFFFFDFFDFDFDDDDDFDFF

Any help would be appreciated on why this works when I run the script but now when I try and incorporate within a script任何帮助将不胜感激为什么当我运行脚本时它会起作用,但现在当我尝试并合并到脚本中时

To answer your question to my understanding, you could simply add another argument to the parser like so.要按照我的理解回答您的问题,您可以像这样简单地向解析器添加另一个参数。

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Process fasq files and seperaate into 4 
            categories')
    parser.add_argument("-f",  "--fastq", required=True, help="Place fastq inside here")
    parser.add_argument("-t",  "--type", required=True, help="The type of file")
    args = parser.parse_args()

    print(args.fastq)
    print(args.type)

And then call it like so.然后这样称呼它。

python3 parseFastq.py --fastq /Users/remaining_dir/test1.fastq --type fastq

the solution was two main parts解决方案是两个主要部分

I was trying to run the argparse via an IDE (Spyder), and running only selected code as opposed to the whole scripts.我试图通过 IDE (Spyder) 运行 argparse,并且只运行选定的代码而不是整个脚本。

For those who are new to python and are utilizing argparse for the first time... this tool only works when calling from the command line.对于那些不熟悉 python 并且第一次使用 argparse 的人...此工具仅在从命令行调用时才有效。

Therefore, once you've created your args table因此,一旦您创建了 args 表

you will run as belowL你将运行如下L

from command line:从命令行:

python3 parseFastq.py --fastq test1.fastq 

To break this down further from the initial set up, you are basically labeling your test1.fastq file, to the tag --fastq... this is critical, if you get error that it is required in a particular format is that you have to add them in pairs... in this particular example, you can also label with the short hand of "-f".为了从初始设置中进一步分解,您基本上将您的 test1.fastq 文件标记为标签 --fastq ...这是至关重要的,如果您收到错误,它需要以特定格式是您有成对添加它们...在这个特定的示例中,您还可以使用“-f”的简写形式 label。 Therefore, it could also be run as...因此,它也可以作为...运行

from command line:从命令行:

python3 parseFastq.py -f test1.fastq 

as long as you're py script is run in the same directory as your called files, you do not need the full extension.只要您的 py 脚本与您调用的文件在同一目录中运行,您就不需要完整的扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM