{"id":1020979,"date":"2024-12-27T13:17:46","date_gmt":"2024-12-27T05:17:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1020979.html"},"modified":"2024-12-27T13:17:50","modified_gmt":"2024-12-27T05:17:50","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%bafasta%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1020979.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fafasta\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25163321\/5cc3346e-4479-44b3-8318-9870744823ef.webp\" alt=\"python\u5982\u4f55\u8f93\u51fafasta\u683c\u5f0f\" \/><\/p>\n<p><p> Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8f93\u51faFASTA\u683c\u5f0f\u6587\u4ef6\uff0c<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u4f7f\u7528Biopython\u5e93\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528Biopython\u5e93\u662f\u6700\u4e3a\u63a8\u8350\u7684\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u63a5\u53e3\u6765\u5904\u7406FASTA\u683c\u5f0f\uff0c\u8fd8\u652f\u6301\u5176\u4ed6\u751f\u7269\u4fe1\u606f\u5b66\u683c\u5f0f\u3002Biopython\u5e93\u63d0\u4f9b\u4e86<code>SeqIO<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u548c\u5199\u5165FASTA\u6587\u4ef6\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Biopython\u5e93\u6765\u8f93\u51faFASTA\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>Biopython\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7b80\u4fbf\u7684\u4f7f\u7528\u65b9\u6cd5\u3002\u4f7f\u7528Biopython\u5e93\u8f93\u51faFASTA\u683c\u5f0f\u6587\u4ef6\u65f6\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Biopython\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<code>pip install biopython<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>SeqIO<\/code>\u6a21\u5757\u4e2d\u7684<code>write<\/code>\u51fd\u6570\u6765\u5c06\u5e8f\u5217\u5bf9\u8c61\u8f93\u51fa\u4e3aFASTA\u683c\u5f0f\u3002\u901a\u8fc7\u5b9a\u4e49\u5e8f\u5217\u7684\u6807\u8bc6\u7b26\u3001\u63cf\u8ff0\u548c\u5e8f\u5217\u672c\u8eab\uff0c\u53ef\u4ee5\u8f7b\u677e\u751f\u6210FASTA\u683c\u5f0f\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Biopython\u5e93\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8f93\u51faFASTA\u683c\u5f0f\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528BIOPYTHON\u5e93\u8f93\u51faFASTA\u683c\u5f0f<\/p>\n<\/p>\n<p><p>Biopython\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u751f\u7269\u4fe1\u606f\u5b66\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u64cd\u4f5c\u751f\u7269\u5e8f\u5217\u7684\u5de5\u5177\u3002\u4f7f\u7528Biopython\u5e93\u8f93\u51faFASTA\u683c\u5f0f\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Biopython\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fddBiopython\u5e93\u5df2\u7ecf\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Biopython\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install biopython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5b89\u88c5Biopython\u5e93\u7684\u6700\u65b0\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528SeqIO\u6a21\u5757\u8f93\u51faFASTA\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>Biopython\u4e2d\u7684<code>SeqIO<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u591a\u79cd\u751f\u7269\u4fe1\u606f\u5b66\u6587\u4ef6\u683c\u5f0f\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u7684\u652f\u6301\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>SeqIO<\/code>\u6a21\u5757\u8f93\u51faFASTA\u683c\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from Bio import SeqIO<\/p>\n<p>from Bio.Seq import Seq<\/p>\n<p>from Bio.SeqRecord import SeqRecord<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5e8f\u5217\u5bf9\u8c61<\/strong><\/h2>\n<p>seq = Seq(&quot;ATGCGTACGTAGCTAGCTAGCTGAC&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5e8f\u5217\u8bb0\u5f55\u5bf9\u8c61<\/strong><\/h2>\n<p>seq_record = SeqRecord(seq, id=&quot;example&quot;, description=&quot;Example Sequence&quot;)<\/p>\n<h2><strong>\u5c06\u5e8f\u5217\u8bb0\u5f55\u5199\u5165FASTA\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;output.fasta&quot;, &quot;w&quot;) as output_handle:<\/p>\n<p>    SeqIO.write(seq_record, output_handle, &quot;fasta&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>SeqIO<\/code>\u3001<code>Seq<\/code>\u548c<code>SeqRecord<\/code>\u6a21\u5757\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>Seq<\/code>\u5bf9\u8c61\u548c\u4e00\u4e2a<code>SeqRecord<\/code>\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>SeqIO.write<\/code>\u51fd\u6570\u5c06\u5e8f\u5217\u8bb0\u5f55\u5199\u5165FASTA\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u548c\u64cd\u4f5cFASTA\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>Biopython\u4e0d\u4ec5\u53ef\u4ee5\u5199\u5165FASTA\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u64cd\u4f5cFASTA\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u8bfb\u53d6FASTA\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from Bio import SeqIO<\/p>\n<h2><strong>\u8bfb\u53d6FASTA\u6587\u4ef6<\/strong><\/h2>\n<p>for record in SeqIO.parse(&quot;output.fasta&quot;, &quot;fasta&quot;):<\/p>\n<p>    print(record.id)<\/p>\n<p>    print(record.description)<\/p>\n<p>    print(record.seq)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>SeqIO.parse<\/code>\u51fd\u6570\u8bfb\u53d6FASTA\u6587\u4ef6\uff0c\u5e76\u8fed\u4ee3\u6bcf\u4e2a\u5e8f\u5217\u8bb0\u5f55\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u8bb0\u5f55\uff0c\u6211\u4eec\u6253\u5370\u51fa\u5b83\u7684\u6807\u8bc6\u7b26\u3001\u63cf\u8ff0\u548c\u5e8f\u5217\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u8f93\u51faFASTA\u683c\u5f0f<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u6765\u8f93\u51faFASTA\u683c\u5f0f\u6587\u4ef6\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982Biopython\u7075\u6d3b\uff0c\u4f46\u5bf9\u4e8e\u7b80\u5355\u7684\u5e94\u7528\u573a\u666f\u6765\u8bf4\u662f\u8db3\u591f\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u5e8f\u5217\u4fe1\u606f<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b9a\u4e49\u8981\u8f93\u51fa\u7684\u5e8f\u5217\u4fe1\u606f\uff0c\u5305\u62ec\u5e8f\u5217\u7684\u6807\u8bc6\u7b26\u3001\u63cf\u8ff0\u548c\u5e8f\u5217\u672c\u8eab\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sequence_id = &quot;example&quot;<\/p>\n<p>sequence_description = &quot;Example Sequence&quot;<\/p>\n<p>sequence_data = &quot;ATGCGTACGTAGCTAGCTAGCTGAC&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u683c\u5f0f\u5316\u8f93\u51fa\u4e3aFASTA\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u53ef\u4ee5\u5c06\u5e8f\u5217\u4fe1\u606f\u683c\u5f0f\u5316\u4e3aFASTA\u683c\u5f0f\u5e76\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;output.fasta&quot;, &quot;w&quot;) as output_handle:<\/p>\n<p>    output_handle.write(f&quot;&gt;{sequence_id} {sequence_description}\\n&quot;)<\/p>\n<p>    output_handle.write(f&quot;{sequence_data}\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u751f\u6210FASTA\u683c\u5f0f\u7684\u5e8f\u5217\u5934\uff0c\u7136\u540e\u5c06\u5e8f\u5217\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u8f93\u51faFASTA\u683c\u5f0f<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u7075\u6d3b\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8f93\u51faFASTA\u683c\u5f0f\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u573a\u666f\uff0c\u4f8b\u5982\u9700\u8981\u5bf9\u5e8f\u5217\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\u6216\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u8f93\u51faFASTA\u683c\u5f0f\u7684\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u5c06\u5e8f\u5217\u4fe1\u606f\u8f93\u51fa\u4e3aFASTA\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def write_fasta(sequence_id, sequence_description, sequence_data, file_path):<\/p>\n<p>    with open(file_path, &quot;w&quot;) as output_handle:<\/p>\n<p>        output_handle.write(f&quot;&gt;{sequence_id} {sequence_description}\\n&quot;)<\/p>\n<p>        output_handle.write(f&quot;{sequence_data}\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u7528\u51fd\u6570\u8f93\u51faFASTA\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5c06\u5e8f\u5217\u4fe1\u606f\u8f93\u51fa\u4e3aFASTA\u683c\u5f0f\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sequence_id = &quot;example&quot;<\/p>\n<p>sequence_description = &quot;Example Sequence&quot;<\/p>\n<p>sequence_data = &quot;ATGCGTACGTAGCTAGCTAGCTGAC&quot;<\/p>\n<p>write_fasta(sequence_id, sequence_description, sequence_data, &quot;output.fasta&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>write_fasta<\/code>\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u5e8f\u5217\u4fe1\u606f\u5199\u5165FASTA\u6587\u4ef6\u3002\u7136\u540e\uff0c\u6211\u4eec\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u8f93\u51faFASTA\u683c\u5f0f\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5904\u7406\u5927\u578b\u5e8f\u5217\u6570\u636e\u96c6<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u5e8f\u5217\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u5757\u5904\u7406\uff0c\u4ee5\u4fbf\u4e8e\u8282\u7701\u5185\u5b58\u548c\u63d0\u9ad8\u6548\u7387\u3002\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u5668\u6765\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u9010\u6b65\u751f\u6210\u5e8f\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_sequences(file_path):<\/p>\n<p>    # \u793a\u4f8b\uff1a\u4ece\u6587\u4ef6\u4e2d\u9010\u884c\u8bfb\u53d6\u5e8f\u5217\u6570\u636e<\/p>\n<p>    with open(file_path, &quot;r&quot;) as file_handle:<\/p>\n<p>        for line in file_handle:<\/p>\n<p>            # \u5047\u8bbe\u6bcf\u884c\u662f\u4e00\u4e2a\u5e8f\u5217<\/p>\n<p>            yield line.strip()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u751f\u6210\u5668\u8f93\u51faFASTA\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\uff0c\u9010\u6b65\u8f93\u51faFASTA\u683c\u5f0f\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def write_large_fasta(sequence_generator, file_path):<\/p>\n<p>    with open(file_path, &quot;w&quot;) as output_handle:<\/p>\n<p>        for i, sequence_data in enumerate(sequence_generator):<\/p>\n<p>            output_handle.write(f&quot;&gt;sequence_{i}\\n&quot;)<\/p>\n<p>            output_handle.write(f&quot;{sequence_data}\\n&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u751f\u6210\u5668\u51fd\u6570<\/strong><\/h2>\n<p>sequence_generator = generate_sequences(&quot;large_sequences.txt&quot;)<\/p>\n<p>write_large_fasta(sequence_generator, &quot;output_large.fasta&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>generate_sequences<\/code>\u751f\u6210\u5668\u51fd\u6570\uff0c\u7528\u4e8e\u9010\u6b65\u751f\u6210\u5e8f\u5217\u6570\u636e\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>write_large_fasta<\/code>\u51fd\u6570\uff0c\u5c06\u5e8f\u5217\u6570\u636e\u8f93\u51fa\u4e3aFASTA\u683c\u5f0f\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u9a8c\u8bc1\u548c\u4f18\u5316FASTA\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5728\u8f93\u51faFASTA\u683c\u5f0f\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u8f93\u51fa\u7ed3\u679c\u8fdb\u884c\u9a8c\u8bc1\u548c\u4f18\u5316\uff0c\u4ee5\u786e\u4fdd\u6587\u4ef6\u7684\u6b63\u786e\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9a8c\u8bc1FASTA\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u7f16\u5199\u51fd\u6570\uff0c\u9a8c\u8bc1FASTA\u6587\u4ef6\u7684\u683c\u5f0f\u662f\u5426\u6b63\u786e\u3002\u4f8b\u5982\uff0c\u68c0\u67e5\u6587\u4ef6\u7684\u884c\u6570\u662f\u5426\u4e3a\u5076\u6570\uff0c\u6216\u8005\u9a8c\u8bc1\u6bcf\u4e2a\u5e8f\u5217\u7684\u6807\u8bc6\u7b26\u662f\u5426\u552f\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def validate_fasta(file_path):<\/p>\n<p>    with open(file_path, &quot;r&quot;) as file_handle:<\/p>\n<p>        lines = file_handle.readlines()<\/p>\n<p>        if len(lines) % 2 != 0:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;FASTA file has an incorrect number of lines.&quot;)<\/p>\n<p>        for i in range(0, len(lines), 2):<\/p>\n<p>            if not lines[i].startswith(&quot;&gt;&quot;):<\/p>\n<p>                raise ValueError(f&quot;Line {i+1} does not start with &#39;&gt;&#39;.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u8f93\u51fa\u6548\u7387<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7f13\u51b2\u533a\u6216\u591a\u7ebf\u7a0b\u6280\u672f\u6765\u63d0\u9ad8\u8f93\u51fa\u6548\u7387\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u5e8f\u5217\u6570\u636e\u6279\u91cf\u5199\u5165\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u9010\u884c\u5199\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def write_fasta_with_buffer(sequence_generator, file_path, buffer_size=100):<\/p>\n<p>    buffer = []<\/p>\n<p>    with open(file_path, &quot;w&quot;) as output_handle:<\/p>\n<p>        for i, sequence_data in enumerate(sequence_generator):<\/p>\n<p>            buffer.append(f&quot;&gt;sequence_{i}\\n{sequence_data}\\n&quot;)<\/p>\n<p>            if len(buffer) &gt;= buffer_size:<\/p>\n<p>                output_handle.writelines(buffer)<\/p>\n<p>                buffer.clear()<\/p>\n<p>        if buffer:<\/p>\n<p>            output_handle.writelines(buffer)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>write_fasta_with_buffer<\/code>\u51fd\u6570\uff0c\u4f7f\u7528\u7f13\u51b2\u533a\u63d0\u9ad8\u6587\u4ef6\u5199\u5165\u6548\u7387\u3002\u6bcf\u5f53\u7f13\u51b2\u533a\u8fbe\u5230\u6307\u5b9a\u5927\u5c0f\u65f6\uff0c\u5c06\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\u6279\u91cf\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4f7f\u7528Python\u8f93\u51faFASTA\u683c\u5f0f\u6587\u4ef6\u3002\u5bf9\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0FASTA\u683c\u5f0f\u7684\u8f93\u51fa\u3002Biopython\u5e93\u662f\u5904\u7406\u751f\u7269\u4fe1\u606f\u5b66\u6587\u4ef6\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u800c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u548c\u7f13\u51b2\u533a\u6280\u672f\u6765\u63d0\u9ad8\u6548\u7387\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u8f93\u51fa\u7684FASTA\u6587\u4ef6\u683c\u5f0f\u6b63\u786e\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210fasta\u683c\u5f0f\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u751f\u6210fasta\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u7ed3\u5408\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5e8f\u5217\u4fe1\u606f\u7684\u5b57\u5178\u6216\u5217\u8868\uff0c\u7136\u540e\u6309\u7167fasta\u683c\u5f0f\uff08\u4ee5\u201c&gt;\u201d\u5f00\u5934\u7684\u63cf\u8ff0\u884c\u548c\u968f\u540e\u7684\u5e8f\u5217\uff09\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">sequences = {\n    &quot;seq1&quot;: &quot;ATGCCTAG&quot;,\n    &quot;seq2&quot;: &quot;GGTACCTA&quot;\n}\n\nwith open(&quot;output.fasta&quot;, &quot;w&quot;) as fasta_file:\n    for name, seq in sequences.items():\n        fasta_file.write(f&quot;&gt;{name}\\n{seq}\\n&quot;)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u5904\u7406fasta\u6587\u4ef6\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u7528\u4e8e\u5904\u7406fasta\u6587\u4ef6\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ecBiopython\u548cpandas\u3002Biopython\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u751f\u7269\u4fe1\u606f\u5b66\u5e93\uff0c\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u51fd\u6570\u6765\u8bfb\u53d6\u3001\u5199\u5165\u548c\u64cd\u4f5cfasta\u6587\u4ef6\u3002\u4f7f\u7528Biopython\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u5e8f\u5217\u5e76\u8fdb\u884c\u5206\u6790\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from Bio import SeqIO\n\nfor record in SeqIO.parse(&quot;input.fasta&quot;, &quot;fasta&quot;):\n    print(record.id)\n    print(record.seq)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1\u751f\u6210\u7684fasta\u6587\u4ef6\u662f\u5426\u7b26\u5408\u6807\u51c6\u683c\u5f0f\uff1f<\/strong><br \/>\u9a8c\u8bc1\u751f\u6210\u7684fasta\u6587\u4ef6\u662f\u5426\u7b26\u5408\u6807\u51c6\u683c\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u6587\u4ef6\u7684\u6bcf\u4e2a\u5e8f\u5217\u662f\u5426\u4ee5\u201c&gt;\u201d\u5f00\u5934\uff0c\u540e\u9762\u8ddf\u7740\u5e8f\u5217\u540d\u79f0\uff0c\u4ee5\u53ca\u7d27\u63a5\u7740\u7684\u884c\u4e2d\u662f\u5426\u53ea\u5305\u542b\u6838\u82f7\u9178\u6216\u6c28\u57fa\u9178\u5b57\u7b26\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6253\u5f00fasta\u6587\u4ef6\uff0c\u786e\u4fdd\u6ca1\u6709\u591a\u4f59\u7684\u7a7a\u884c\u548c\u65e0\u6548\u5b57\u7b26\u3002\u901a\u8fc7\u7f16\u5199\u7b80\u5355\u7684Python\u811a\u672c\u4e5f\u53ef\u4ee5\u81ea\u52a8\u5316\u6b64\u8fc7\u7a0b\uff0c\u68c0\u67e5\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\u662f\u5426\u7b26\u5408fasta\u683c\u5f0f\u7684\u8981\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8f93\u51faFASTA\u683c\u5f0f\u6587\u4ef6\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u4f7f\u7528Biopython\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1020995,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020979"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1020979"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020979\/revisions"}],"predecessor-version":[{"id":1020998,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020979\/revisions\/1020998"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1020995"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1020979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1020979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1020979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}