多序列比对。将多行格式转换为单行格式?

时间:2015-05-08 13:32:37

标签: bioinformatics sequence-alignment

我有一个多序列比对文件,其中来自不同序列的线条散布在一起,如clustal和其他流行的多序列比对工具所输出的格式。它看起来像这样:

TGFb3_human_used_for_docking        ALDTNYCFRNLEENCCVRPLYIDFRQDLGWKWVHEPKGYYANFCSGPCPY
tr|B3KVH9|B3KVH9_HUMAN              ALDTNYCFRNLEENCCVRPLYIDFRQDLGWKWVHEPKGYYANFCSGPCPY
tr|G3UBH9|G3UBH9_LOXAF              ALDTNYCFRNLEENCCVRPLYIDFRQDLGWKWVHEPKGYYANFCSGPCPY
tr|G3WTJ4|G3WTJ4_SARHA              ALDTNYCFRNLEENCCVRPLYIDFRQDLGWKWVHEPKGYYANFCSGPCPY


TGFb3_human_used_for_docking        LRSADTTHST-
tr|B3KVH9|B3KVH9_HUMAN              LRSADTTHST-
tr|G3UBH9|G3UBH9_LOXAF              LRSTDTTHST-
tr|G3WTJ4|G3WTJ4_SARHA              LRSADTTHST-

每一行以序列标识符开头,然后是一系列字符(在本例中描述蛋白质的氨基酸序列)。每个序列被分成几行,因此您可以看到第一个序列(ID为TGFb3_human_used_for_docking)有两行。我想将其转换为每个序列都有一行的格式,如下所示:

TGFb3_human_used_for_docking        ALDTNYCFRNLEENCCVRPLYIDFRQDLGWKWVHEPKGYYANFCSGPCPYLRSADTTHST-
tr|B3KVH9|B3KVH9_HUMAN              ALDTNYCFRNLEENCCVRPLYIDFRQDLGWKWVHEPKGYYANFCSGPCPYLRSADTTHST-
tr|G3UBH9|G3UBH9_LOXAF              ALDTNYCFRNLEENCCVRPLYIDFRQDLGWKWVHEPKGYYANFCSGPCPYLRSTDTTHST-
tr|G3WTJ4|G3WTJ4_SARHA              ALDTNYCFRNLEENCCVRPLYIDFRQDLGWKWVHEPKGYYANFCSGPCPYLRSADTTHST-      

(在这个特定的例子中,序列几乎相同,但一般来说它们都不是!)

如何从多行多序列比对格式转换为单行?

1 个答案:

答案 0 :(得分:0)

看起来你需要编写某种类型的脚本来实现这一目标。这是我用Python编写的一个简单示例。它不会像你的例子那样漂亮地排列白色空间(如果你关心它,你将不得不陷入formatting),但它完成了其余的工作

#Create a dictionary to accumulate full sequences
full_sequences = {}

#Loop through original file (replace test.txt with your file name)
#and add each line to the appropriate dictionary entry
with open("test.txt") as infile:
    for line in infile:
        line = [element.strip() for element in line.split()]
        if len(line) < 2:
            continue
        full_sequences[line[0]] = full_sequences.get(line[0], "") + line[1]

#Now loop through the dictionary and write each entry as a single line
outstr = ""
with open("test.txt", "w") as outfile:
    for seq in full_sequences:
        outstr += seq + "\t\t" + full_sequences[seq] + "\n"

    outfile.write(outstr)