Невозможно вывести файл: файл не создан

Я новичок в python и изо всех сил пытаюсь понять, почему моя программа не будет печатать, несмотря на все мои усилия понять ввод-вывод и обработку файлов.

Приведенный ниже код должен принять файл fastQ или fasta (для последовательностей ДНК или белков) и обрезать последовательности в соответствии с заданным пользователем качеством, а затем создать новый файл с сокращенными последовательностями.

Проблема возникает, когда я пытаюсь запустить программу из командной строки:

python endtrim --min_q 35 --in_33 fQ.txt --out_33 fQ_out.txt

Программа работает без происшествий (нет ошибок или проблем с трассировкой), но я не вижу создаваемого файла fQ_out.txt. Мне кажется, проблема кроется где-то в argparse, так как при запуске я не получаю справочного сообщения:

python endtrim --help

Может кто-нибудь указать мне в правильном направлении?

from __future__ import division, print_function
import argparse
import collections
import sys
import re
from string import punctuation
from fastRead import *
ready2trim = ()

def parse_arguments():
    """Creates a bevvy of possible sort arguments from command line and
    binds them to their respective names"""
    parser = argparse.ArgumentParser("--h", "--help", description=__doc__, \
                                     formatter_class=argparse.\
                                     RawDescriptionHelpFormatter)
    options = parse_arguments()

    #quality argument
    parser.add_argument("--min_qual", action='store', default=30, \
                        dest='min_qual', help="""Lowest quality value 
                        that can appear in the output""")
    #input arguments
    parser.add_argument("--in_33", action='store', default=sys.stdin, \
                        dest='in_33', nargs='?', help="""Input file in fastq format, using Phred+33 coding""")
    parser.add_argument("--in_64", action='store', default=sys.stdin, \
                        dest='in_64', nargs='?', help="""Input file in fastq format, using Phred+64 coding""")
    parser.add_argument("--in_fasta", action='store', default=sys.stdin, \
                        dest='in_fasta', nargs='?', help="""Input fasta format, requires concurrent --in_qual argument""")
    parser.add_argument("--in_qual", action='store', default=sys.stdin, \
                        dest='in_qual', nargs='?', help="""Input quality format, requires concurrent --in_fasta argument""")                    
    #output arguments
    parser.add_argument("--out_33", action='store', default=sys.stdout, \
                        dest='out_33', nargs='?', help="""Output file in fastq format, 
                        using Phred+33 coding""")
    parser.add_argument("--out_64", action='store', default=sys.stdout, \
                        dest='out_64', nargs='?', help="""Output file in fastq format, 
                        using Phred+33 coding""")                   
    parser.add_argument("--out_fasta", action='store', default=sys.stdout, \
                        dest='out_fasta', nargs='?', help="""Output fasta format, 
                        """)
    parser.add_argument("--out_qual", action='store', default=False, \
                        dest='out_qual', nargs='?', help="""Output quality format, 
                        """)    
    args = parser.parse_args()
    return args

def incoming(args):
    """interprets argparse command and assigns appropriate format for
    incoming file"""
    if options.in_fasta and options.in_qual:
        #ready2trim is the input after being read by fastRead.py
        ready2trim = read_fasta_with_quality(open(options.in_fasta), \
        open(options.in_qual))
        return ready2trim
    elif options.in_33:
        ready2trim = read_fastq(open(options.in_33))
        #phredCode_in specifies the Phred coding of the input fastQ
        phredCode_in = 33
        return ready2trim
    elif options.in_64:
        ready2trim = read_fastq(open(options.in_64))
        phredCode_in = 64
        return ready2trim
    else: sys.stderr.write("ERR: insufficient input arguments")

def print_output(seqID, seq, comm, qual):
    """interprets argparse command and creates appropriate format for
    outgoing file"""
    #Printing a fastQ
    if options.out_33 or options.out_64:
        if options.out_33:
            #phredCode_out specifies the Phred coding of the output fastQ
            phredCode_out = 33
            if comm:
                #outputfh is the file handle of new output file
                with open(options.out_33,'a') as outputfh:
                    outputfh.write("@{}\n{}\n{}\n+".format(seqID, seq, comm))
            else:
                with open(options.out_33,'a') as outputfh:
                    outputfh.write("@{}\n{}\n+".format(seqID, seq))             
        else: 
            phredCode_out = 64
            if comm:
                #outputfh is the file handle of new output file
                with open(options.out_33,'a') as outputfh:
                    outputfh.write("@{}\n{}\n{}\n+".format(seqID, seq, comm))
            else:
                with open(options.out_33,'a') as outputfh:
                    outputfh.write("@{}\n{}\n+".format(seqID, seq))
        print(''.join(str(chr(q+phredCode_out)) for q in qual))
    #Print a fasta
    if options.out_fasta:
        outputfh = open(options.out_fasta, "a")
        if(comment == ''):
            output.write('>{}\n{}\n'.format(seqID, seq))
        else: output.write('>{} {}\n{}\n'.format(seqID, comm, seq))
    #Print a qual
    if options.out_qual:
        outputfh = open(options.out_qual, "a")
        if(comment == ''):
            output.write('>{}\n{}\n'.format(seqID, seq))
        else: output.write('>{} {}\n{}\n'.format(seqID, comm, seq))

def main(args):
    """Prints combined fastq sequence from separate fasta and quality
    files according to user-generated arguments """
    for (seqID, seq, comm, qual) in ready2trim:
        for q in qual:
            #i counts satisfactory bases to later print that number of
            i = 0
            if ord(q) - phredCode_in >= min_qual:
                i += 1
            print_output(seqID, seq[0:i], comm, qual[0:i])  
        sys.stderr.write("ERR: sys.stdin is without sequence data")

if __name__ == "__main__" :
    sys.exit(main(sys.argv))

person Thomas Matthew    schedule 19.10.2014    source источник
comment
Я не вижу, чтобы parse_arguments вызывали где-либо, кроме как внутри себя.   -  person hpaulj    schedule 23.10.2014
comment
Если у вас есть какие-либо сомнения относительно того, как работает argparse, напечатайте пространство имен args, которое возвращает parse_args().   -  person hpaulj    schedule 23.10.2014


Ответы (1)


parse_arguments вроде бы вызывает себя рекурсивно, при этом вообще нигде в программе не вызывается

def parse_arguments():
    """Creates a bevvy of possible sort arguments from command line and
    binds them to their respective names"""
    parser = argparse.ArgumentParser("--h", "--help", description=__doc__, \
                                     formatter_class=argparse.\
                                     RawDescriptionHelpFormatter)
    options = parse_arguments()

Возможно, эта строка параметров должна быть в функции main или глобальной?

person Peter Gibson    schedule 20.10.2014