2016-10-22 11 views
-1

pdbファイルからあらかじめ定義されたシーケンスを出力するコードを以下に示します。今度は、それぞれのchain_idに別々の出力ファイルを保存したいと思います。各chain_idごとに別々の出力ファイルを保存する方法は?

各chain_idの出力を個別に保存するにはどうすればよいですか?

予想される出力:

が、私は、各CHAIN_ID用の出力ファイルを保存したいです。 1AHIA.txt1AHIB.txt1AHIC.txt1AHID.txt

我々は4つの鎖のID A、B、C、Dを持っている場合は、入力ファイル名は、このファイルには、1AHI.PDBある場合は、私は、出力ファイルをしたいです。これは各入力ファイルに適用されます。私は自分の入力ディレクトリに2000個以上の入力ファイルを持っています。

コード:

from glob import glob 

in_loc = r'C:/Users/Documents/NAD/NAD/result/test_result_file/' 
out_loc = r'C:/Users/Documents/NAD/NAD/result/test_result_file/Final_result/' 


def test(): 
    fnames = glob(in_loc+'*.pdb') 

    for each in fnames: 
    # This is the new generated file out of input file (.txt). 
     formatted_file = each.replace('pdb', 'txt') 

     formatted_file = formatted_file.replace(in_loc, out_loc) 

    # This is the input file 
     in_f = open(each, 'r') 

    # A new file to be opened. 
     out_f = open(formatted_file, "w") 

    # Filtering results from input file 
     try: 
      out_chain_list = filter_file(in_f) 
      for each_line in out_chain_list: 
       out_f.write(each_line) 

     # Closing all the opened files. 
      out_f.close() 
      in_f.close() 

     except Exception as e: 
      print('Exception for file: ', each, '\n', e) 
      out_f.close() 
      in_f.close() 


def filter_file(in_f): 
    atom_ids = ['C4B', 'O4B', 'C1B', 'C2B', 'C3B'] 
    chain_ids = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] 
    order = [0, 1, 4, 3, 2] 

    previous_chain_id = None 
    chain_list = [] 
    out_chain_list = [] 

    for line in in_f: 

     if line.startswith('HETATM '): 
      line = line.replace('HETATM ', 'HETATM') 

     if line.startswith('HETATM'): 
      line_list = line.split() 
      chain_id = line_list[3][0] 
      atom_id = line_list[1] 

      if atom_id in atom_ids and chain_id in chain_ids: 

       if chain_id != previous_chain_id: 
        c_ls = [] 
        if chain_list: 
         c_l = chain_list[-5:] 
         c_l = [c_l[i] for i in order] 

         for i in range(5): 
          c_ls += c_l[:4] 
          c_ls.append('\n') 
          c_l = c_l[-4:] + c_l[:1] 

        out_chain_list += c_ls 
        chain_list.append('\n') 

       chain_list.append(line) 
       previous_chain_id = chain_id 

    c_ls = [] 
    if chain_list: 
     c_l = chain_list[-5:] 
     c_l = [c_l[i] for i in order] 

     for i in range(5): 
      c_ls += c_l[:4] 
      c_ls.append('\n') 
      c_l = c_l[-4:] + c_l[:1] 
    out_chain_list += c_ls 

    return out_chain_list 

test() 

*アンス後に編集*

エラー:

Traceback (most recent call last): 
    File "C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file/Test_10.py", line 31, in test 
suffix), 'w') 
OSError: [Errno 22] Invalid argument: 'C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file/Final_result//C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file\\1A4ZHETATM15207 C4B NAD A 501  47.266 101.038 7.214 1.00 11.48   C \n.txt' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file/Test_10.py", line 94, in <module> 
test() 
    File "C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file/Test_10.py", line 40, in test 
out_f.close() 
UnboundLocalError: local variable 'out_f' referenced before assignment 
+0

、あなたが意味するかあなたが形式{'chain_id':out_chain_list}out_chain_dictがあるでしょう場合は、簡単に各chain_idための個別のファイルを作成することができます"1AHIA.txt、1AHIB.txt、1AHIC.txt、1AHID.txt"? 1AHIB.txtは2回あり、1AHID.txtは表示されません。 –

+0

あなたのコードには何の問題がありますか? – Shasha99

+0

@ Shasha99、プログラムには問題ありません。しかし、私は変更したい。私はしようとしましたが、各chain_idの出力をどのように取得するのか分かりません。 – Vish

答えて

0

あなたの現在のコードは、各入力ファイルに対して1つの出力を開きます。しかし、各out_chain項目の出力ファイルが必要で、各入力ファイルには複数のout_chain項目があります。したがって、out_chain項目を処理している内部ループで出力ファイルを開いたり閉じたりする必要があります。ここではあなたがそれを行うことができ一つの方法です:

def test(): 
    fnames = glob(in_loc+'*.pdb') 

    for each in fnames: 
    # This is the new generated file out of input file (.txt). 
     formatted_file = each.replace('pdb', 'txt') 
     suffix = 'txt' 

     formatted_file = formatted_file.replace(in_loc, out_loc) 
     ofstem = each.replace('.pdb', '') 

    # This is the input file 
     in_f = open(each, 'r') 

    # A new file to be opened. 
     # out_f = open(formatted_file, "w") 

    # Filtering results from input file 
     try: 
      out_chain_list = filter_file(in_f) 
      for each_line in out_chain_list: 
      # open and write output file for each out_chain item 
       out_f = open('{}/{}{}.{}'.format(out_loc, 
             ofstem, 
             each_line, 
             suffix), 'a') 
       out_f.write(each_line) 
       out_f.close() 

     # Closing all the opened files. 
      in_f.close() 

     except Exception as e: 
      print('Exception for file: ', each, '\n', e) 
      out_f.close() 
      in_f.close() 
0

あなたがキーとしてchain_idで辞書を受け取ることができ、あなたのfilter_fileを変更することができます。

def test(): 
    fnames = glob(in_loc+'*.pdb') 

    for each in fnames: 
    # This is the path for new generated file. 
     path_file = each.replace(in_loc, out_loc) 

    # This is the input file and iltering results from input file 
     with open(each, 'r') as in_f: 
      try: 
       out_chain_dict = filter_file(in_f) 

      except Exception as e: 
       print('Exception for file: ', each, '\n', e) 
       continue 

      for (chain_id, out_chain_list) in out_chain_dict.items(): 
       # This is the new generated file out of input file (.txt). 
       formatted_file = path_file.replace('.pdb', chain_id + '.txt') 

       # A new file to be opened. 
       with open(formatted_file, "w") as out_f: 
        for each_line in out_chain_list: 
         out_f.write(each_line) 

はEDIT filter_file:出力ファイルのリストに

def filter_file(in_f): 
    atom_ids = ['C4B', 'O4B', 'C1B', 'C2B', 'C3B'] 
    chain_ids = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] 
    order = [0, 1, 4, 3, 2] 

    previous_chain_id = None 
    chain_list = [] 
    out_chain_dict = {}  # Change out_chain_list to dict 

    for line in in_f: 

     if line.startswith('HETATM '): 
      line = line.replace('HETATM ', 'HETATM') 

     if line.startswith('HETATM'): 
      line_list = line.split() 
      chain_id = line_list[3][0] 
      atom_id = line_list[1] 

      if atom_id in atom_ids and chain_id in chain_ids: 

       if chain_id != previous_chain_id: 
        c_ls = [] 
        if chain_list: 
         c_l = chain_list[-5:] 
         c_l = [c_l[i] for i in order] 

         for i in range(5): 
          c_ls += c_l[:4] 
          c_ls.append('\n') 
          c_l = c_l[-4:] + c_l[:1] 

        try:           # Here add c_ls to an existing key chain_id 
         out_chain_dict[chain_id] += c_ls # 
        except KeyError:        # or create new chain_id key 
         out_chain_dict[chain_id] = c_ls # if it appears at the first time 
        chain_list.append('\n') 

       chain_list.append(line) 
       previous_chain_id = chain_id 

    c_ls = [] 
    if chain_list: 
     c_l = chain_list[-5:] 
     c_l = [c_l[i] for i in order] 

     for i in range(5): 
      c_ls += c_l[:4] 
      c_ls.append('\n') 
      c_l = c_l[-4:] + c_l[:1] 
    # I guess here we add the last chain_id which corresponds to `chain_id` key 
    try: 
     out_chain_dict[chain_id] += c_ls 
    except KeyError: 
     out_chain_dict[chain_id] = c_ls 

    return out_chain_dict 
+0

エラー: 'トレースバック(最新の最後の呼び出し): ファイル" C:/Users/Vishnu/Documents/NAD/NAD/result/test_result_file/Test_10.py "、行82、 test() ファイル" C :/Users/Vishnu/Documents/NAD/NAD/result/test_result_file/Test_10.py "、22行目、テスト中 for(chain_id、out_chain_list)for out_chain_dict.items(): AttributeError: 'list'オブジェクトには属性がありません'items'' – Vish

+0

'filter_file'を修正して辞書を出力として取得する必要があります。 –

+0

私は何を修正すべきなのか理解できません。 – Vish

関連する問題