Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 根据序列与基因组的模式匹配创建生物体列表_Python_Design Patterns_Sequence_Matching_Genome - Fatal编程技术网

Python 根据序列与基因组的模式匹配创建生物体列表

Python 根据序列与基因组的模式匹配创建生物体列表,python,design-patterns,sequence,matching,genome,Python,Design Patterns,Sequence,Matching,Genome,我有一个有两列的数据框,第一列是生物体的名称,第二列是序列,是一个字母串。我正在尝试创建一个算法,看看一个有机体的序列是否在一个由字母组成的更大的基因组序列中。如果它在基因组中,我想把它的名字添加到一个列表中。例如,如果流感在下面的基因组中,我想把流感添加到一个列表中 dict_1={'organisms':['flu', 'cold', 'stomach bug'], 'seq_list':['HTIDIJEKODKDMRM', 'AGGTTTEFGFGEERDDTER', 'EGHDGGE

我有一个有两列的数据框,第一列是生物体的名称,第二列是序列,是一个字母串。我正在尝试创建一个算法,看看一个有机体的序列是否在一个由字母组成的更大的基因组序列中。如果它在基因组中,我想把它的名字添加到一个列表中。例如,如果流感在下面的基因组中,我想把流感添加到一个列表中

dict_1={'organisms':['flu', 'cold', 'stomach bug'], 'seq_list':['HTIDIJEKODKDMRM', 
'AGGTTTEFGFGEERDDTER', 'EGHDGGEDCGRDSGRDCFD']}
df=pd.DataFrame(dict_1)

     organisms             seq_list
0          flu      HTIDIJEKODKDMRM
1         cold  AGGTTTEFGFGEERDDTER
2  stomach bug  EGHDGGEDCGRDSGRDCFD

genome='TLTPSRDMEDHTIDIJEKODKDMRM'
如果有一个函数,其中p是生物体,t是基因组,那么第一个函数会找到匹配的索引。第二部分是我遇到麻烦的部分。我试图使用for循环来搜索df中的每个条目,但是如果我得到匹配项,我不确定如何引用df中的第一列来将名称添加到空列表中。谢谢你的帮助

def naive(p, t):
occurences = []
for i in range(len(t) - len(p) + 1):
    match = True
    for j in range(len(p)):
        if t[i+j] != p[j]:
            match = False
            break
    if match:
        occurences.append(i)
return occurences


Organisms_that_matched = []
for x in df:
   matches=naive(genome, x)
   if len(matches) > 0:
      #add name of organism to Organisms_that_matched list

我不确定您是否正在学习在列表中横切和应用自定义逻辑的不同方法,但您可以使用:


嗨,Rorra,我用不同的数据集尝试了您的解决方案,但出于某种原因,我的x值报告为非类型。如果我在数据框中只使用x执行for循环,那么x的值可以很好地打印出来。关于这个问题有什么建议吗?@juiser12更新问题,共享数据框架(如果是一个大文件,您可以使用github的gists),然后我可以查看itI,我能够解决我的问题谢谢。
import pandas as pd

dict_1 = {
    'organisms': ['flu', 'cold', 'stomach bug'],
    'seq_list':  ['HTIDIJEKODKDMRM', 'AGGTTTEFGFGEERDDTER', 'EGHDGGEDCGRDSGRDCFD']}
df = pd.DataFrame(dict_1)
genome = 'TLTPSRDMEDHTIDIJEKODKDMRM'

organisms_that_matched = [dict_1['organisms'][index] for index, x in enumerate(dict_1['seq_list']) if x in genome]

print(organisms_that_matched)