如何使用类似 Python 标头的 fasta 将字符串序列格式化为某个预定长度

问题描述 投票:0回答:1

我有一个名为

input.txt
的文本文件,看起来像这样。

A C H E C Q D S S C H H C R Q K L E D T S C H L E D V G K M
N T Y H C G E G I N N G P N A S C K F M L P C V V A E F E N H T
E T D W R C K L E A E H C D C K D A A V N H H F Y S L C K D V T E E W

注意上面的输入有3行氨基酸序列。

我有一个名为 input.txt 的文本文件,看起来像这样。

A C H E C Q D S S C H H C R Q K L E D T S C H L E D V G K M
N T Y H C G E G I N N G P N A S C K F M L P C V V A E F E N H T
E T D W R C K L E A E H C D C K D A A V N H H F Y S L C K D V T E E W

注意上面的输入有3行氨基酸序列。

我想把它转换成下面的格式。

<|endoftext|>
ACHECQDSSCHHCRQKLEDTSCHLEDVGKM
<|endoftext|>
NTYHCGEGINNGPNASCKFMLPCVVAEFEN
HT
<|endoftext|>
ETDWRCKLEAEHCDCKDAAVNHHFYSLCKD
VTEEW

氨基酸序列的每个开头都应该以这个字符串“<|endoftext|>”开头 每个新行不应超过 30 个氨基酸。

我有这段代码,但它不能完成工作:

def process_amino_acids(file_name):
    with open(file_name, "r") as file:
        data = file.read().replace("\n", "").replace(" ", "")

    output = "<|endoftext|>"
    for i, amino_acid in enumerate(data):
        if i % 30 == 0 and i != 0:
            output += "\n"
        output += amino_acid
    return output


def main():
    input_file = "data/input.txt"
    processed_amino_acids = process_amino_acids(input_file)

    with open("data/output.txt", "w") as output_file:
        output_file.write(processed_amino_acids)

    print("Formatted amino acid sequences are written to output.txt")


if __name__ == "__main__":
    main()

它给出的输出是这样的:

<|endoftext|>ACHECQDSSCHHCRQKLEDTSCHLEDVGKM
NTYHCGEGINNGPNASCKFMLPCVVAEFEN
HTETDWRCKLEAEHCDCKDAAVNHHFYSLC
KDVTEEW

如何使用 Python 正确地完成它?

python python-3.x string fasta
1个回答
3
投票

Python自带电池,使用

textwrap.wrap
:

from textwrap import wrap

with (open('input.txt') as f,
      open('output.txt', 'w') as out
     ):
    for line in f:
        line = line.replace(' ', '')
        out.write('<|endoftext|>\n')
        out.write('\n'.join(wrap(line, width=30)))
        out.write('\n')

输出文件:

<|endoftext|>
ACHECQDSSCHHCRQKLEDTSCHLEDVGKM
<|endoftext|>
NTYHCGEGINNGPNASCKFMLPCVVAEFEN
HT
<|endoftext|>
ETDWRCKLEAEHCDCKDAAVNHHFYSLCKD
VTEEW
© www.soinside.com 2019 - 2024. All rights reserved.