0

我正在尝试解决 Rosalind 上的一个问题,在给定 1kb 最多 10 个序列的 FASTA 文件中,我需要提供共有序列和配置文件(所有序列在每个核苷酸上共有多少个碱基) . 在格式化我的回复的上下文中,我的代码适用于小序列(已验证)。

但是,当涉及到大序列时,我在格式化我的回复时遇到了问题。无论长度如何,我期望返回的是:

"consensus sequence"
"A: one line string of numbers without commas"
"C: one line string """" "
"G: one line string """" "
"T: one line string """" "

所有都相互对齐并在各自的行上对齐,或者至少有一些格式允许我将这种格式作为一个单元继续进行,以保持对齐的完整性。

但是当我为一个大序列运行我的代码时,我得到共识序列下面的每个单独的字符串被换行符打破,大概是因为字符串本身太长了。我一直在努力想办法规避这个问题,但我的搜索一直没有结果。我正在考虑一些迭代编写算法,它可以只编写上述期望的全部内容,但可以分块编写任何帮助将不胜感激。为了完整起见,我在下面附上了我的全部代码,并根据需要添加了块注释,尽管主要部分。

def cons(file):
#returns consensus sequence and profile of a FASTA file
    import os
    path = os.path.abspath(os.path.expanduser(file))

    with open(path,"r") as D:
        F=D.readlines()

#initialize list of sequences, list of all strings, and a temporary storage
#list, respectively
    SEQS=[]
    mystrings=[]
    temp_seq=[]

#get a list of strings from the file, stripping the newline character
    for x in F:
        mystrings.append(x.strip("\n"))

#if the string in question is a nucleotide sequence (without ">")
#i'll store that string into a temporary variable until I run into a string
#with a ">", in which case I'll join all the strings in my temporary
#sequence list and append to my list of sequences SEQS    
    for i in range(1,len(mystrings)):
        if ">" not in mystrings[i]:
            temp_seq.append(mystrings[i])
        else:
            SEQS.append(("").join(temp_seq))
            temp_seq=[]
    SEQS.append(("").join(temp_seq))

#set up list of nucleotide counts for A,C,G and T, in that order
    ACGT=      [[0 for i in range(0,len(SEQS[0]))],
                [0 for i in range(0,len(SEQS[0]))],
                [0 for i in range(0,len(SEQS[0]))],
                [0 for i in range(0,len(SEQS[0]))]]

#assumed to be equal length sequences. Counting amount of shared nucleotides
#in each column
    for i in range(0,len(SEQS[0])-1):
        for j in range(0, len(SEQS)):
            if SEQS[j][i]=="A":
                ACGT[0][i]+=1
            elif SEQS[j][i]=="C":
                ACGT[1][i]+=1
            elif SEQS[j][i]=="G":
                ACGT[2][i]+=1
            elif SEQS[j][i]=="T":
                ACGT[3][i]+=1

    ancstr=""
    TR_ACGT=list(zip(*ACGT))
    acgt=["A: ","C: ","G: ","T: "]
    for i in range(0,len(TR_ACGT)-1):
        comp=TR_ACGT[i]
        if comp.index(max(comp))==0:
            ancstr+=("A")
        elif comp.index(max(comp))==1:
            ancstr+=("C")
        elif comp.index(max(comp))==2:
            ancstr+=("G")
        elif comp.index(max(comp))==3:
            ancstr+=("T")

'''
writing to file... trying to get it to write as
consensus sequence
A: blah(1line)
C: blah(1line)
G: blah(1line)
T: blah(line)
which works for small sequences. but for larger sequences
python keeps adding newlines if the string in question is very long...
'''


    myfile="myconsensus.txt"
    writing_strings=[acgt[i]+' '.join(str(n) for n in ACGT[i] for i in      range(0,len(ACGT))) for i in range(0,len(acgt))]
    with open(myfile,'w') as D:
        D.writelines(ancstr)
        D.writelines("\n")
        for i in range(0,len(writing_strings)):
            D.writelines(writing_strings[i])
            D.writelines("\n")

缺点(“rosalind_cons.txt”)

4

1 回答 1

0

您的代码完全没问题,除了这一行:

writing_strings=[acgt[i]+' '.join(str(n) for n in ACGT[i] for i in      range(0,len(ACGT))) for i in range(0,len(acgt))]

您不小心复制了数据。尝试将其替换为:

writing_strings=[ACGT[i] + str(ACGT[i]) for i in range(0,len(ACGT))]

然后将其写入您的输出文件,如下所示:

D.write(writing_strings[i][1:-1])

这是从列表中删除括号的一种懒惰方式。

于 2016-08-06T16:45:25.873 回答