Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Rosalind:重叠图_Python_String_Graph Theory - Fatal编程技术网

Python Rosalind:重叠图

Python Rosalind:重叠图,python,string,graph-theory,Python,String,Graph Theory,我在Rosalind上遇到了一个问题,我认为我已经正确地解决了,但是我被告知我的答案是错误的。问题可在此处找到: 这是基础图论,更具体地说,它处理的是返回重叠DNA字符串的邻接列表 “对于字符串和正整数k的集合,字符串的重叠图是一个有向图Ok,其中每个字符串由一个节点表示,当有一个长度为k的后缀s与长度为k的前缀t匹配时,字符串s通过有向边连接到字符串t,长度为s≠t、 我们要的是巧克力≠t以防止重叠图中出现有向循环(尽管可能存在有向循环) 给定:FASTA格式的DNA字符串集合,总长度最多为1

我在Rosalind上遇到了一个问题,我认为我已经正确地解决了,但是我被告知我的答案是错误的。问题可在此处找到:

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

“对于字符串和正整数k的集合,字符串的重叠图是一个有向图Ok,其中每个字符串由一个节点表示,当有一个长度为k的后缀s与长度为k的前缀t匹配时,字符串s通过有向边连接到字符串t,长度为s≠t、 我们要的是巧克力≠t以防止重叠图中出现有向循环(尽管可能存在有向循环)

给定:FASTA格式的DNA字符串集合,总长度最多为10 kbp

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

因此,如果你有:

罗莎琳德0498 AAAAA

罗莎琳德·欧2391 阿塔特

罗莎琳德2323 TTCCC

罗莎琳德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
listTitle=[]
listContent=[]
#SPLIT是已解析的DNA字符串列表
#在这里,我创建了两个新列表,一个(listTitle)包含标识特定字符串的四个数字,第二个(listContent)包含实际字符串(“>Rosalind_u2;”已被删除,因为它是我拆分文件的对象)
当我虽然p我不确定你的代码到底出了什么问题,但这里有一种可能被认为更“pythonic”的方法

我假设您已将数据读入将名称映射到DNA字符串的字典:

{'Rosalind_0442': 'AAATCCC',
 'Rosalind_0498': 'AAATAAA',
 'Rosalind_2323': 'TTTTCCC',
 'Rosalind_2391': 'AAATTTT',
 'Rosalind_5013': 'GGGTGGG'}
我们定义了一个简单的函数,用于检查字符串
s1
是否具有与字符串
s2
k
-前缀匹配的
k
-后缀:

def is_k_overlap(s1, s2, k):
    return s1[-k:] == s2[:k]
然后我们观察所有的DNA序列组合,找到匹配的。这通过
itertools实现。组合

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')]

我不确定你的代码到底出了什么问题,但这里有一种可能被认为更“pythonic”的方法

我假设您已将数据读入将名称映射到DNA字符串的字典:

{'Rosalind_0442': 'AAATCCC',
 'Rosalind_0498': 'AAATAAA',
 'Rosalind_2323': 'TTTTCCC',
 'Rosalind_2391': 'AAATTTT',
 'Rosalind_5013': 'GGGTGGG'}
我们定义了一个简单的函数,用于检查字符串
s1
是否具有与字符串
s2
k
-前缀匹配的
k
-后缀:

def is_k_overlap(s1, s2, k):
    return s1[-k:] == s2[:k]
然后我们观察所有的DNA序列组合,找到匹配的。这通过
itertools实现。组合

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')]