Python:当每个块的大小未知时,以块的形式读取文本文件

时间:2013-03-19 01:15:13

标签: python

我必须处理一个巨大的文本文件,其中包含数据作为块除以空格。它如下:

>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方法来分割文件,如上所述。

1 个答案:

答案 0 :(得分:1)

这是我提出的解决方案:

#!/usr/bin/env python

INPUT_FILE = 'input.txt'
OUT_3D_HELIX = 'out_3dhelix.txt'
OUT_1D_HELIX = 'out_1dhelix.txt'
OUT_3D_OTHER = 'out_3dother.txt'

f_input = open(INPUT_FILE, 'r')
out_3dhelix = open(OUT_3D_HELIX, 'w')
out_1dhelix = open(OUT_1D_HELIX, 'w')
out_3dother = open(OUT_3D_OTHER, 'w')

dest_file = None
starting = True

try:
    for line in f_input:
        if starting:
            ## We are at a block start
            if line.startswith('>3D_helix;'):
                dest_file = out_3dhelix
            elif line.startswith('>1D_helix;'):
                dest_file = out_1dhelix
            elif line.startswith('>3D_other;'):
                dest_file = out_3dother
            else:
                continue   # Invalid line -- not a block beginning
            starting = False

        if not line.strip():  # Line is blank -- block end
            starting = True
            dest_file = None
            continue

        if dest_file is not None:  # And never should be, at this point..
            dest_file.write(line)

finally:
    ## Close files...
    f_input.close()
    out_3dhelix.close()
    out_1dhelix.close()
    out_3dother.close()

基本上,它逐行读取所有文件,检测“块启动器”,以便更改写入以下行的目标文件。