我必须处理一个巨大的文本文件,其中包含以空格分隔的块形式的数据。它如下:
>3D_helix;140
protein_name:AChR pore alpha subunit (Torpedo marmorata)
file_name:ACh_pore_alpha.txt
entry_date:3july03
refman_number:21022
endnote_number:
author:Miyazawa,A., Fujiyoshi,Y., Unwin,N.(2003) [Structure and gating mechanism of the acetylcholine receptor pore] {Nature, 423, 949-955}
remarks:Sequence is from PDB, chain A. There is additional 24 AA as signal sequence in Swiss-Prot. TMhelices=4.
pir_number:
Swiss_Prot_entry:ACHA_TORMA
Swiss_Prot_number:P02711
Swiss_Prot_gene:CHRNA1
Swiss_Prot_name:Acetylcholine receptor subunit alpha
PDB_title:Acetylcholine Receptor Protein, alpha Chain
PDB_Identifier:1OED
N_terminal:in
number_tmsegs:4
tm_segments:A.211,237;B.243,271;C.275,300;D.403,436
sequence:SEHETRLVANLLENYNKVIRPVEHHTHFVDITVGLQLIQLINVDEVNQIVETNVRLRQQWIDVRLRWNPADYGGIKKIRLPSDDVWLPDLVLYNNADGDFAIVHMTKLLLDYTGKIMWTPPAIFKSYCEIIVTHFPFDQQNCTMKLGIWTYDGTKVSISPESDRPDLSTFMESGEWVMKDYRGWKHWVYYTCCPDTPYLDITYHFIMQRIPLYFVVNVIIPCLLFSFLTVLVFYLPTDSGEKMTLSISVLLSLTVFLLVIVELIPSTSSAVPLIGKYMLFTMIFVISSIIVTVVVINTHHRSPSTHTMPQWVRKIFINTIPNVMFFSTMKRASKEKQENKIFADDIDISDISGKQVTGEVIFQTPLIKNPDVKSAIEGVKYIAEHMKSDEESSNAAEEWKYVAMVIDHILLCVFMLICIIGTVSVFAGRLIELSQEG*
>1D_helix;141
protein_name:AChR pore beta subunit (Torpedo marmorata)
file_name:ACh_pore_beta.txt
entry_date:3july03
refman_number:21022
endnote_number:
author:Miyazawa,A., Fujiyoshi,Y., Unwin,N.(2003) [Structure and gating mechanism of the acetylcholine receptor pore] {Nature, 423, 949-955}
remarks:Sequence is from PDB, chain B. There is additional 24 AA as signal sequence in Swiss-Prot. TMhelices=4.
pir_number:
Swiss_Prot_entry:Q6S3I0_TORMA
Swiss_Prot_number:Q6S3I0
Swiss_Prot_gene:none
Swiss_Prot_name:Acetylcholine receptor beta subunit
PDB_title:Acetylcholine Receptor Protein, beta Chain
PDB_Identifier:1OED
N_terminal:in
number_tmsegs:4
tm_segments:A.224,241;B.249,274;C.290,306;D.438,462
sequence:SVMEDTLLSVLFENYNPKVRPSQTVGDKVTVRVGLTLTSLLILNEKNEEMTTSVFLNLAWTDYRLQWDPAAYEGIKDLSIPSDDVWQPDIVLMNNNDGSFEITLHVNVLVQHTGAVSWHPSAIYRSSCTIKVMYFPFDWQNCTMVFKSYTYDTSEVILQHALDAKGEREVKEIMINQDAFTENGQWSIEHKPSRKNWRSDDPSYEDVTFYLIIQRKPLFYIVYTIVPCILISILAILVFYLPPDAGEKMSLSISALLALTVFLLLLADKVPETSLSVPIIISYLMFIMILVAFSVILSVVVLNLHHRSPNTHTMPNWIRQIFIETLPPFLWIQRPVTTPSPDSKPTIISRANDEYFIRKPAGDFVCPVDNARVAVQPERLFSEMKWHLNGLTQPVTLPQDLKEAVEAIKYIAEQLESASEFDDLKKDWQYVAMVADRLFLYIFITMCSIGTFSIFLDASHNVPPDNPFA*
>3D_other;143
protein_name:AChR pore delta subunit (Torpedo marmorata)
file_name:ACh_pore_delta.txt
entry_date:4dec03
refman_number:21022
endnote_number:
author:Miyazawa,A., Fujiyoshi,Y., Unwin,N.(2003) [Structure and gating mechanism of the acetylcholine receptor pore] {Nature, 423, 949-955}
remarks:Sequence is from PDB, chain C. Sequence in PDB has first 21 AA removed relative to Swiss-Prot. TMhelices=4.
pir_number:
Swiss_Prot_entry:Q6S3H8_TORMA
Swiss_Prot_number:Q6S3H8
Swiss_Prot_gene:none
Swiss_Prot_name:Acetylcholine receptor delta subunit
PDB_title:Acetylcholine Receptor Protein, delta Chain
PDB_Identifier:1OED
N_terminal:in
number_tmsegs:4
tm_segments:A.226,253;B.257,285;C.289,316;D.452,483
sequence:VNEEERLINDLLIVNKYNKHVRPVKHNNEVVNIALSLTLSNLISLKETDETLTTNVWMDHAWYDHRLTWNASEYSDISILRLRPELIWIPDIVLQNNNDGQYNVAYFCNVLVRPNGYVTWLPPAIFRSSCPINVLYFPFDWQNCSLKFTALNYNANEISMDLMTDTIDGKDYPIEWIIIDPEAFTENGEWEIIHKPAKKNIYGDKFPNGTNYQDVTFYLIIRRKPLFYVINFITPCVLISFLAALAFYLPAESGEKMSTAICVLLAQAVFLLLTSQRLPETALAVPLIGKYLMFIMSLVTGVVVNCGIVLNFHFRTPSTHVLSTRVKQIFLEKLPRILHMSRVDEIEQPDWQNDLKLRRSSSVGYISKAQEYFNIKSRSELMFEKQSERHGLVPRVTPRIGFGNNNENIAASDQLHDEIKSGIDSTNYIVKQIKEKNAYDEEVGNWNLVGQTIDRLSMFIITPVMVLGTIFIFVMGNFNRPPAKPFEGDPFDYSSDHPRCA
每个块都以 3 个给定选项中的任何一个开始。每个块中的行数是变化的。我想将文件分成 3 个部分(或 3 个单独的文件),这样:
part 1 contains all blocks starting with >3D_Helix
part 2 contains all blocks starting with >1D_helix
part 3 contains all blocks starting with >3d_other
我尝试了以下方法
prot_file = open(sys.argv[1], "r")
flag = False
for line in prot_file:
if line.startswith (">3D_other"):
flag == True
if flag == True:
print line
但它只打印第一行,即 3d_helix。我在网上找到的大多数提示根据每个块的大小将列表分成块(即已知大小固定在特定数字,比如 13)。但是在我的情况下,我不知道大小,因此无法使用它们。我想要一种有效的pythonic方法来划分文件,如解释的那样。