- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我有两个引物序列:
fiveprime = "GATTCGAAGTCCACTATC"threeprime = "TGAGTAGGACGGCACTATC"
What I need to do is when I have another sequence I need to check if it contains a part of one these primer sequences and if it does I need to remove the matching part leaving the non-matching part for further analysis.
For example my sequence: CACTATCAAAAAAAhas a part of the fiveprimer in it. I need to locate this common substring and then remove it leaving only the AAAAA
.
The problem I run in to is when you have a sequence like this CACTATCGAAG where GAAG is also in the primer, but not part of the primer sequence it's still removed. I tried to fix this by making sure that the found structure is at the left side of the primer and with the threeprimer on the right side so example:
With the CACTATCGAAG we have 2 common structures CACTATC and GAAGnow I can compare CACTATC with the end of the fiveprime GATTCGAAGTCCACTATC and tell it's part of the primer when it's a match and then remove it. So when we compare GAAG with the length of the last end of the fiveprimer it will give us this GATTCGAAGTCCACTATC which is not a match so GAAG can go on for further analysis.
For some reason my script bugs or doesn't work properly. Are there any other solutions to this problem or suggestions?
def longestSubstringFinder(string1, string2):
answer = ""
len1, len2 = len(string1), len(string2)
for i in range(len1):
match = ""
for j in range(len2):
if i + j < len1 and string1[i + j] == string2[j]:
match += string2[j]
else:
if len(match) > len(answer): answer = match
match = ""
return answer
def get_sequence(test, fiveprime, threeprime):
if test == fiveprime:
pass
elif test == threeprime:
pass
elif test in fiveprime:
pass
elif test in threeprime:
pass
# find out if there is a matching part between the primers and the found
# single stranded region, then calculates what part that is, and removes it
# from the single stranded region
else:
overlap = longestSubstringFinder(fiveprime, test)
l = len(overlap)
if fiveprime[-l:] == test[:l]:
check = test[l:]
else:
check = test
overlap2 = longestSubstringFinder(check, threeprime)
l = len(overlap2)
if threeprime[:l] == check[-l:]:
check2 = check[:-l]
structure.append(check2)
else:
structure.append(check)
return structure
最佳答案
我认为,如果您选择适当的数据结构来表示您要查找的数据,您的问题会更容易处理。我能想到的最好的一个是 trie .
这种结构的好处是,它允许您在给定初始字母序列的情况下表示所有可能的匹配,因此,如果您有序列 AABAB,它将允许从初始 A 遍历到 A 和 B,但不允许从 A 到 G 或 T 的遍历。这使得查找部分匹配变得高效,因为 trie 中的任何点都代表了那么多字母的匹配。
这个数据结构将类似于:
class Trie(object):
def __init__(self):
self.children = {}
def add_child(self, letter):
if letter in self.children:
return self.children[letter]
else:
child = Trie()
self.children[letter] = child
return child
def traverse(self, letter):
return self.children.get(letter, None)
然后您可以像这样填充:
root = Trie()
current_positions = []
for letter in letters:
current_positions = [
position.add_child(letter)
for position in current_positions
]
current_positions.append(root.add_child(letter))
一旦所有这些都设置完毕,您就应该能够遍历这个结构,直到遍历返回 null。这将指示当前最长的匹配。字母的初始化将每个字母视为匹配的潜在起点,您也应该如此。
然后您可以像这样搜索最长的子字符串匹配:
class TrieSearch(object):
def __init__(self, trie, starting_index):
self.trie = trie
self.starting_index = starting_index
self.ending_index = starting_index + 1
def update(self, letter):
""" This returns a boolean indicating
if the search can accept the letter """
self.trie = self.trie.traverse(letter)
if self.trie is not None:
self.ending_index = self.ending_index + 1
return True
return False
def get_match(self, letters):
return letters[self.starting_index:self.ending_index]
def find_matches(root, letters):
completed_matches = []
current_matches = []
for index, letter in enumerate(letters):
new_current = []
for current in current_matches:
if current.update(letter):
new_current.append(current)
else:
completed_matches.append(current)
new_search_trie = root.traverse(letter)
if new_search_trie is not None:
new_current.append(TrieSearch(new_search_trie, index))
current_matches = new_current
all_matches = completed_matches + current_matches
return [match.get_match(letters) for match in all_matches]
我已将所有这些放在 a gist 中当使用 thirdprime
和 Fiveprime
值初始化 trie 且输入数据为 CACTATCAAAAAAA
时,结果为:
['CACTATC', 'ACTATC', 'CTATC', 'TATC', 'ATC', 'TC', 'CA', 'AA', 'AA', 'AA', 'AA', 'AA', 'AA', 'A']
由于您无疑要处理大量字符串,因此您可能需要查看一些更有效的通用字符串替换算法。 Aho-Corasick algorithm是此处概述的 trie 方法的扩展。还有Knuth-Morris-Pratt algorithm它使用表而不是特里树。这两种算法都是线性复杂度算法,与您的 longestSubstringFinder
方法使用的二次方法相比,这将是一个重大改进。
关于python - 使用python查找并删除公共(public)子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27963222/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: When should [assembly: InternalsVisibleTo()] be used?
问题与微服务有关,当我有多个微服务提供将被订购和计费的功能/服务时。 我正在确定采用哪种方法, a) 每个可计费微服务有一个订单和一个计费服务,有各自的数据库。b) 跨所有微服务的通用订单管理和计费服
我正在尝试使用 gcloud图书馆。 (ns firengine.state (:import [com.google.cloud AuthCredentials] [com.goog
Java 允许定义以下一对类。 class Class1 { ... } public Class2 { public Class2(Class1 c1) { ... } } 如果因为 Class1
我正在尝试查找文件 1 和文件 2 中的共同行。如果公共(public)行存在,我想写入文件 2 中的行,否则打印文件 1 中的非公共(public)行。fin1 和 fin2 是这里的文件句柄。它读
好吧,这是一个满口的标题。不过,这让我明白了。这是我的代码的要点,在 jar 里: public class NetworkShared { public static class Login
我在使用 ltree 时遇到 PHP 问题来自 PostgreSQL .我在 SQL 中这样做: SELECT * FROM tabla t WHERE t.parent_path " for "OP
我知道如何为类/接口(interface)/包的子集生成 Javadoc。但是有没有办法只为公共(public)方法的一个子集生成 Javadoc? 我更喜欢能够将方法(Javadoc 标记或注释)标
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicates: c#: why have empty get set properties instead of usin
在我们的每个项目中,都有一个文件用于存储该项目中使用的各种SQL 语句。类的声明方式和字符串的声明方式有一些变化。 示例类声明: internal sealed class ClassName int
我根据 http://docs.jquery.com/Plugins/Authoring 定义了我的插件 (function( $ ){ var methods = { init : fu
我正在使用 Inno Setup 来构建我的安装程序,我有 C:\Users\Public文件夹硬编码在我的 [Files] 中放置一些文件的部分(Inno Setup 没有此文件夹的常量) 我的目标
我有一个 dataframe1 包含像 'ID', 'A', 'B', 'C', 'D', 'E', 'F', 'G' 这样的列. 现在,我创建了两个数据框, dataframe2 包含 'ID',
我有一个抽象类,不幸的是我无法更改它的定义,它基本上提供了一个抽象方法,有点像。 public abstract void do(Data someData, BaseInterface interf
我刚刚在重构时偶然发现了一段奇怪的代码。它看起来像是分解出两个 readString() 方法的共同部分的候选者,只是它似乎是不可能的(这对我来说是一个令人毛骨悚然的脑筋急转弯): private f
是否有解析为公用文件夹的属性?显然,我不想在目录结构中对“c:\users\public”进行硬编码,但我找不到预定义的 Property解决这个问题。是否有一种可接受的方式来指定要在此处安装和/或在
我试图将值从一个类传递到另一个类。 subPanel1 类读取全局变量,但当我通过调整监听器更新这些变量时,它不会更改值。我试图将 rc、gc 和 bc 变量从 subPanel2 类传递到 subP
我想使用具有自动属性的干净且编码较少的类。所有属性(property)都是公共(public)的。在同一类的方法中我也使用了该属性。因此,我认为这种方法是可混搭的,因为我将公共(public)属性用于
不久前,我在 Android 应用程序中创建了一个 SQLiteHelper 类。我不是 100% 确定原因,但表名和列名是嵌套公共(public)静态抽象类中的公共(public)静态最终字段。我记
这个问题已经有答案了: Cannot make a static reference to the non-static method (8 个回答) 已关闭 3 年前。 我正在为类(class)做一
我是一名优秀的程序员,十分优秀!