gpt4 book ai didi

python - 寻找两个字符串之间最长的完美匹配

转载 作者:太空狗 更新时间:2023-10-29 21:27:59 26 4
gpt4 key购买 nike

我正在尝试编写一个脚本,当提供两个字符串时,该脚本将执行两个功能:

1。从pos[0]开始,找出两个字符串中相同的最长字符序列

Seq1 = 'ATCCTTAGC'
Seq2 = 'ATCCAGCAATTC'
^^^^ Match from pos[0] to pos[3]
Pos: 0:3
Length: 4
Seq: ATCC

2。找出同时存在于两个字符串中的最长字符串

Seq1 = 'TAGCTCCTTAGC' # Contains 'TCCTT'
Seq2 = 'GCAGCCATCCTTA' # Contains 'TCCTT'
^ No match at pos[0]
Pos1: 4:8
Pos2 7:11
Length: 5
Seq: TCCTT

为了完成问题 1,我有以下内容:

#!/usr/bin/python

upstream_seq = 'ATACATTGGCCTTGGCTTAGACTTAGATCTAGACCTGAAAATAACCTGCCGAAAAGACCCGCCCGACTGTTAATACTTTACGCGAGGCTCACCTTTTTGTTGTGCTCCC'
downstream_seq = 'ATACACGAAAAGCGTTCTTTTTTTGCCACTTTTTTTTTATGTTTCAAAACGGAAAATGTCGCCGTCGTCGGGAGAGTGCCTCCTCTTAGTTTATCAAATAAAGCTTTCG'

print("Upstream: %s\nDownstream: %s\n") % (upstream_seq, downstream_seq)

mh = 0
pos_count = 0
seq = ""
position =""
longest_hom=""
for i in range(len(upstream_seq)):
pos_count += 1
if upstream_seq[i] == downstream_seq[i]:
mh += 1
seq += upstream_seq[i]
position = pos_count
longest_hom = mh

else:
mh = 0
break

print("Pos: 0:%s\nLength: %s\nSeq: %s\n") % (position , longest_hom, seq)

Upstream:   ATACATTGGCCTTGGCTTAGACTTAGATCTAGACCTGAAAATAACCTGCCGAAAAGACCCGCCCGACTGTTAATACTTTACGCGAGGCTCACCTTTTTGTTGTGCTCCC
Downstream: ATACACGAAAAGCGTTCTTTTTTTGCCACTTTTTTTTTATGTTTCAAAACGGAAAATGTCGCCGTCGTCGGGAGAGTGCCTCCTCTTAGTTTATCAAATAAAGCTTTCG

Pos: 0:5
Length: 5
Seq: ATACA

我遇到问题 2。到目前为止,我已经考虑过使用 BioPython's pairwise2 在两个序列之间进行比对。 .然而,在这种情况下,我只想要完美匹配(没有间隙,没有扩展),我只想看到最长的序列,而不是我似乎得到的共识:

from Bio import pairwise2 as pw2

global_align = pw2.align.globalms(upstream_seq, downstream_seq, 3, -1, -.5, -.5)

print(global_align[0])

('ATACATT-G----GCC-TTGGCTTA-----G--ACTTAGATCTAG-----ACCTGAA----AATAACCTGCCGAAAA-GACC-CGCCCGACTGTTAATACTT-TACGCG-AG-GCT-CAC-C-T-TT--TTGT-TG----T---GCTCC--C-', 'ATACA--CGAAAAG-CGTT--CTT-TTTTTGCCACTT---T-T--TTTTTA--TG--TTTCAA-AA-C-G--GAAAATG---TCG--C--C-G----T-C--GT-CG-GGAGAG-TGC-CTCCTCTTAGTT-TAT-CAAATAAAGCT--TTCG', 151.0, 0, 153)

问题:如何找到同时存在于两个字符串中的最长字符?

最佳答案

这是问题 1 的较短代码:

upstream_seq = 'ATACATTGGCCTTGGCTTAGACTTAGATCTAGACCTGAAAATAACCTGCCGAAAAGACCCGCCCGACTGTTAATACTTTACGCGAGGCTCACCTTTTTGTTGTGCTCCC'
downstream_seq = 'ATACACGAAAAGCGTTCTTTTTTTGCCACTTTTTTTTTATGTTTCAAAACGGAAAATGTCGCCGTCGTCGGGAGAGTGCCTCCTCTTAGTTTATCAAATAAAGCTTTCG'

common_prefix = ''

for x,y in zip(upstream_seq, downstream_seq):
if x == y:
common_prefix += x
else:
break
print(common_prefix)
# ATACA

问题 2 的简单方法是简单地为每个字符串生成一组每个子字符串,计算它们的交集并按长度排序:

upstream_seq = 'ATACATTGGCCTTGGCTTAGACTTAGATCTAGACCTGAAAATAACCTGCCGAAAAGACCCGCCCGACTGTTAATACTTTACGCGAGGCTCACCTTTTTGTTGTGCTCCC'
downstream_seq = 'ATACACGAAAAGCGTTCTTTTTTTGCCACTTTTTTTTTATGTTTCAAAACGGAAAATGTCGCCGTCGTCGGGAGAGTGCCTCCTCTTAGTTTATCAAATAAAGCTTTCG'

def all_substrings(string):
n = len(string)
return {string[i:j+1] for i in range(n) for j in range(i,n)}

print(all_substrings('ABCA'))
# {'CA', 'BC', 'ABC', 'C', 'BCA', 'AB', 'A', 'B', 'ABCA'}
print(all_substrings(upstream_seq) & all_substrings(downstream_seq))
# {'AAAG', 'CA', 'A', 'AAC', 'TGTT', 'ACT', 'CTTAG', 'GCT', 'ATAC', 'AAAA', 'TTTA', 'AAT', 'GTGC', 'CTT', 'AAAAG', 'TTTG', 'CGAA', 'AA', 'CGAAAAG', 'GCC', 'ACA', 'TGCC', 'AAATAA', 'CTCC', 'TTTTT', 'CGCC', 'CAC', 'GAG', 'CTC', 'CGAAAA', 'ATC', 'TCA', 'GA', 'CGC', 'TGT', 'GT', 'GC', 'GAAA', 'ACTTT', 'AAG', 'TTTT', 'CT', 'AATA', 'TCC', 'CGAAA', 'GAA', 'GAAAAG', 'GTT', 'AG', 'TC', 'AAAAT', 'CC', 'TTT', 'AATAA', 'CTTTT', 'ACTT', 'TTA', 'CTTT', 'GCTT', 'GCCG', 'GTG', 'TACA', 'TT', 'GCG', 'TTTTTG', 'TAG', 'TTG', 'TTAG', 'AAATA', 'CTTTTT', 'AAAT', 'TAA', 'ACG', 'TG', 'GCCT', 'G', 'TAC', 'CCT', 'TCT', 'ATA', 'CTTA', 'CCG', 'CG', 'ATAA', 'GG', 'ATACA', 'AGA', 'TGC', 'C', 'T', 'AT', 'GAAAA', 'CGA', 'GAAAAT', 'TA', 'AC', 'AAA', 'TTTTG'}
print(max(all_substrings(upstream_seq) & all_substrings(downstream_seq), key=len))
# CGAAAAG

如果你想要一个更有效的方法,你应该使用 suffix tree .

如果你不想重新发明轮子,你可以简单地使用 difflib.SequenceMatcher.find_longest_match

关于python - 寻找两个字符串之间最长的完美匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46846177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com