罗莎琳德:重叠图

2024-04-26 12:08:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我在罗莎琳德身上遇到了一个问题,我认为我已经正确地解决了,但是我被告知我的答案是不正确的。问题可以在这里找到:http://rosalind.info/problems/grph/

这是基本的图论,更具体地说,它处理的是返回重叠DNA串的邻接列表。在

对于一组串和一个正整数k,串的重叠图是一个有向图Ok,其中每个串由一个节点表示,当s的长度k后缀与长度k前缀t相匹配时,只要s≠t,字符串s就与字符串t相连重叠图中的循环(尽管可能存在有向循环)。在

给定:以FASTA格式的DNA字符串的集合,总长度不超过10kbp。在

Return:O3对应的邻接列表。您可以按任何顺序返回边。”

所以,如果你有:

Rosalind_0498 AAATAAA

Rosalind_2391 AAATTTT

Rosalind_2323 TTTTCCC

Rosalind_0442 AAATCCC

Rosalind_5013 GGGTGGG

您必须返回:

罗莎琳德0498罗莎琳德2391

罗莎琳德0498罗莎琳德0442

罗莎琳德2391罗莎琳2323

在解析了包含DNA字符串的FASTA文件之后,我的python代码如下:

        listTitle = []
        listContent = []

    #SPLIT is the parsed list of DNA strings

    #here i create two new lists, one (listTitle) containing the four numbers identifying a particular string, and the second (listContent) containing the actual strings ('>Rosalind_' has been removed, because it is what I split the file with)

        while i < len(SPLIT):
            curr = SPLIT[i]
            title = curr[0:4:1]
            listTitle.append(title)
            content = curr[4::1]
            listContent.append(content)
            i+=1

        start = []
        end = []

        #now I create two new lists, one containing the first three chars of the string and the second containing the last three chars, a particular string's index will be the same in both lists, as well as in the title list

        for item in listContent:
            start.append(item[0:3:1])
            end.append(item[len(item)-3:len(item):1])

        list = []

   #then I iterate through both lists, checking if the suffix and prefix are equal, but not originating from the same string, and append their titles to a last list

        p=0
        while p<len(end):
            iterator=0
            while iterator<len(start):
                if p!=iterator:
                    if end[p] == start[iterator]:
                        one=listTitle[p]
                        two=listTitle[iterator]
                        list.append(one)
                        list.append(two)
                iterator+=1
            p+=1

#finally I print the list in the format that they require for the answer

        listInc=0

        while listInc < len(list):
                print "Rosalind_"+list[listInc]+' '+"Rosalind_"+list[listInc+1]
                listInc+=2

我哪里出错了?很抱歉,代码有点乏味,我几乎没有接受过python方面的培训


Tags: the字符串lenitemonelistslistdna
1条回答
网友
1楼 · 发布于 2024-04-26 12:08:16

我不确定您的代码有什么问题,但是这里有一种方法可能被认为是更“python”的。在

我假设你已经把你的数据读入字典,把名字映射到DNA字符串:

{'Rosalind_0442': 'AAATCCC',
 'Rosalind_0498': 'AAATAAA',
 'Rosalind_2323': 'TTTTCCC',
 'Rosalind_2391': 'AAATTTT',
 'Rosalind_5013': 'GGGTGGG'}

{{cd2>一个匹配的字符串{cd2>检查一个 ^{pr2}$

然后我们观察所有的DNA序列组合,找出匹配的。这可以通过itertools.combinations来简化:

import itertools
def k_edges(data, k):
    edges = []
    for u,v in itertools.combinations(data, 2):
        u_dna, v_dna = data[u], data[v]

        if is_k_overlap(u_dna, v_dna, k):
            edges.append((u,v))

        if is_k_overlap(v_dna, u_dna, k):
            edges.append((v,u))

    return edges

例如,在上面的数据中,我们得到:

>>> k_edges(data, 3)
[('Rosalind_2391', 'Rosalind_2323'),
 ('Rosalind_0498', 'Rosalind_2391'),
 ('Rosalind_0498', 'Rosalind_0442')]

相关问题 更多 >