2016-08-05 3 views
0

これは私の最初の試みであり、速く汚れたsys.argv[]以外のコマンドライン引数を使用し、より適切なpythonスクリプトを書くことです。何らかの理由で私は今理解できませんが、コマンドラインから入力ファイルをどのように使用しようとしているのかとは反対のようです。biopythonでコマンドラインからファイルを処理する問題SeqIO

スクリプトは、入力ファイルといくつかの数値インデックスを取り、ファイルのサブセット領域をスライスすることを意図していますが、渡しているファイルに与えた変数が定義されていません:

[email protected]:~/Documents/Warwick/PhD/Scripts$ python slice_genbank.py --input PAU_06042014.gbk -o test.gbk -s 3907329 -e 3934427 
Traceback (most recent call last): 
    File "slice_genbank.py", line 70, in <module> 
    sub_record = record[start:end] 
NameError: name 'record' is not defined 

ここにコードがありますが、どこが間違っていますか? (私はそのシンプル確信している):

#!/usr/bin/python 

# This script is designed to take a genbank file and 'slice out'/'subset' 
# regions (genes/operons etc.) and produce a separate file. 

# Based upon the tutorial at http://biopython.org/DIST/docs/tutorial/Tutorial.html#htoc44 

# Set up and handle arguments: 
from Bio import SeqIO 
import getopt 


def main(argv): 
    record = '' 
    start = '' 
    end = '' 
    try: 
     opts, args = getopt.getopt(argv, 'hi:o:s:e:', [ 
                'help', 
                'input=', 
                'outfile=', 
                'start=', 
                'end=' 
                ] 
          ) 
     if not opts: 
      print "No options supplied. Aborting." 
      usage() 
      sys.exit(2) 
    except getopt.GetoptError: 
     print "Some issue with commandline args.\n" 
     usage() 
     sys.exit(2) 

    for opt, arg in opts: 
     if opt in ("-h", "--help"): 
      usage() 
      sys.exit(2) 
     elif opt in ("-i", "--input"): 
      filename = arg 
      record = SeqIO.read(arg, "genbank") 
     elif opt in ("-o", "--outfile"): 
      outfile = arg 
     elif opt in ("-s", "--start"): 
      start = arg 
     elif opt in ("-e", "--end"): 
      end = arg 
    print("Slicing " + filename + " from " + str(start) + " to " + str(end)) 

def usage(): 
    print(
""" 
This script 'slices' entries such as genes or operons out of a genbank, 
subsetting them as their own file. 

Usage: 
python slice_genbank.py -h|--help -i|--input <genbank> -o|--output <genbank> -s|--start <int> -e|--end <int>" 

Options: 

-h|--help  Displays this usage message. No options will also do this. 
-i|--input  The genbank file you which to subset a record from. 
-o|--outfile The file name you wish to give to the new sliced genbank. 
-s|--start  An integer base index to slice the record from. 
-e|--end  An integer base index to slice the record to. 
""" 
    ) 

#Do the slicing 
sub_record = record[start:end] 
SeqIO.write(sub_record, outfile, "genbank") 

if __name__ == "__main__": 
main(sys.argv[1:]) 

それがSeqIO.write構文の問題だが、私はまだ限りそのように持っていないことも可能です。

EDIT:

はまた、私は `、レコード= SeqIO.read(「file.gbk」、「GenBankの」)を使用して、スクリプトに直接ファイル名を書くとき、それが正しく動作することを言及するのを忘れてしまいました。

+1

あなたは正しいインデントを持っているエントリを編集することはできますか?私が問題と言うことができる限り、 'record'は' main() 'メソッドでのみ定義されているので、メインプログラムはそれについて知識がありません。 – Markus

+0

ああ、私はそれがインデントを保存していなかったことに気付かなかった。編集します –

+0

OKこれは編集して適切にインデントされていると思います。後でスクリプト内でサブレコードコールをすることで何かに繋がるかもしれないと思います。私はまだbashで考えています... –

答えて

1

としては、コメントでは、あなたの変数recordsだけので、それはプログラムの残りの部分のために表示されていない、(同じことがstartendについても同様です)メソッドmain()で定義されていると述べました。 あなたはこのような値を返すことができ、次のいずれか

def main(argv): 
    ... 
    ... 
    return record, start, end 

あなたのコールmain()には、その後、次のようになります。

record, start, end = main(sys.argv[1:]) 

を別の方法として、あなたが行ったように、あなたは(main機能にあなたの主要な機能を移動することができます)。

(もう一つの方法は、メインプログラム内の変数とあなたの関数で使用globalキーワードを定義することで、これは、しかし、お勧めしません。)

関連する問題