pdbファイルからあらかじめ定義されたシーケンスを出力するコードを以下に示します。今度は、それぞれのchain_idに別々の出力ファイルを保存したいと思います。各chain_idごとに別々の出力ファイルを保存する方法は?
各chain_idの出力を個別に保存するにはどうすればよいですか?
予想される出力:
が、私は、各CHAIN_ID用の出力ファイルを保存したいです。 1AHIA.txt
、1AHIB.txt
、1AHIC.txt
、1AHID.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
、あなたが意味するかあなたが形式
{'chain_id':out_chain_list}
でout_chain_dict
があるでしょう場合は、簡単に各chain_id
ための個別のファイルを作成することができます"1AHIA.txt、1AHIB.txt、1AHIC.txt、1AHID.txt"? 1AHIB.txtは2回あり、1AHID.txtは表示されません。 –あなたのコードには何の問題がありますか? – Shasha99
@ Shasha99、プログラムには問題ありません。しかし、私は変更したい。私はしようとしましたが、各chain_idの出力をどのように取得するのか分かりません。 – Vish