找出两个基因组序列中最长的子串 [英] Find the longest substring in two genomic sequence

查看:42
本文介绍了找出两个基因组序列中最长的子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个序列AAAAAAAAAGAAAAGAAGAAG, AAAGAAG.正确答案是 AAGAAG.

I have two sequences AAAAAAAAAGAAAAGAAGAAG, AAAGAAG. The correct answer is AAGAAG.

但我的代码给出了 AA.

But my code gives AA.

有时两个字符串的顺序为 AAAGAAG、AAAAAAAAAGAAAAGAAGAAG.

There will also be times when two strings will be in this order AAAGAAG, AAAAAAAAAGAAAAGAAGAAG.

这是我的代码

`def longestSubstringFinder(string1, string2):
    string1=string1.strip()
    string2=string2.strip()
    answer = ""
    len1=len(string1)
    len2=len(string2)
    if int(len1)>1 and int(len2)>1:
        for i in range(1,len1,1):
            match = ""
            for j in range(len2):
                if len1>len2:
                    if i+j<len1 and (string1[i+j]==string2[i+j]):
                        match=str(match)+str(string2[i+j])
                        print(match)
                    else:
                        if len(match)>len(answer):
                            answer=match
                            match=""
                elif len2>len1:
                    if i+j<len2 and (string1[i+j]==string2[i+j]):
                        match=str(match)+str(string2[i+j])
                        print(match)
                    else:
                        if len(match)>len(answer):
                            answer=match
                            match=""
    return(answer)`

推荐答案

获取两个字符串的所有子串,找到两组子串的交集,然后找到交集中最大的字符串

Get all the substrings of both strings, find the intersection of both sets of substrings and then find the largest string in the intersection

def get_all_substrings(input_string):
  length = len(input_string)
  return [input_string[i:j+1] for i in range(length) for j in range(i,length)]

strA = 'AAAAAAAAAGAAAAGAAGAAG'
strB = 'AAAGAAG'

intersection = set(get_all_substrings(strA)).intersection(set(get_all_substrings(strB)))
print(max(intersection, key=len))
>> 'AAAGAAG'

这篇关于找出两个基因组序列中最长的子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆