0

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

>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方法来划分文件,如解释的那样。

4

1 回答 1

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()

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

于 2013-03-19T01:28:51.493 回答