【问题标题】:Maintaining overlapping annotations while removing dashes from string在从字符串中删除破折号的同时保持重叠注释
【发布时间】:2016-02-05 21:19:23
【问题描述】:

假设使用以下 Python 函数从字符串中删除破折号(“空白”),同时保持该字符串上的正确注释。输入变量instringannotations分别构成一个字符串和一个字典。

def DegapButMaintainAnno(instring, annotations):
    degapped_instring = ''
    degapped_annotations = {}
    gaps_cumulative = 0
    for range_name, index_list in annotations.items():
        gaps_within_range = 0
        for pos, char in enumerate(instring):
            if pos in index_list and char == '-':
                index_list.remove(pos)
                gaps_within_range += 1
            if pos in index_list and char != '-':
                degapped_instring += char
                index_list[index_list.index(pos)] = pos - gaps_within_range
        index_list = [i-gaps_cumulative for i in index_list]
        degapped_annotations[range_name] = index_list
        gaps_cumulative += gaps_within_range
    return (degapped_instring, degapped_annotations)

如果输入字典指定的范围没有重叠,则所述函数按预期工作:

>>> instr = "A--AT--T"
>>> annot = {"range1":[0,1,2,3,4], "range2":[5,6,7]}
>>> DegapButMaintainAnno(instr, annot)
Out: ('AATT', {'range1': [0, 1, 2], 'range2': [3]})

但是,一旦一个或多个范围重叠,代码就会失败:

>>> annot = {"range1":[0,1,2,3,4], "range2":[4,5,6,7]}
>>> DegapButMaintainAnno(instr, annot)
Out: ('AATTT', {'range1': [0, 1, 2], 'range2': [2, 3]}) # See additional 'T' in string

有人对如何纠正我的代码重叠范围有什么建议吗?

【问题讨论】:

  • 如果您解释了注释的含义,这将有很大帮助...比对算法进行逆向工程要好得多
  • @Pynchia 注解在生物信息学中很常见,指的是字符串的不同部分(或技术上的范围)表示应操作的不可分割子字符串。
  • 好的,但是您能解释一下使 range1 的注释从 [0,1,2,3,4] 变为 [0,1,2] 的基本原理吗?数字是多少?字符串中的索引?删除破折号后它们需要如何工作?
  • @Pynchia 您只需从子字符串中删除破折号,以便只保留字母(因此,在我的标题中“删除破折号”)。每个列表中的数字指的是每个字符串中的索引位置。
  • @Pynchia 上面的 Python 函数已经经过了一些审查和讨论,如 [stackoverflow.com/questions/34816513/….因此,对算法进行逆向工程可能没有用,而是为其添加功能。

标签: python string list loops


【解决方案1】:

我觉得你可能想多了。这是我的尝试:

from copy import copy

def rewriteGene(instr, annos):
    annotations = copy(annos)
    index = instr.find('-')
    while index > -1:
        for key, ls in annotations.items():
            if index in ls:
                ls.remove(index)
            annotations[key] = [e-1 if e > index else e for e in ls]
        instr = instr[:index] + instr[index+1:]
        index = instr.find('-')
    return instr, annotations

instr = "A--AT--T"
annos = {"range1":[0,1,2,3,4], "range2":[4,5,6,7]}

print rewriteGene(instr, annos)
# ('AATT', {'range2': [2, 3], 'range1': [0, 1, 2]})

它应该很容易阅读,但如果你想澄清任何事情,请告诉我。

【讨论】:

  • 很高兴再次见到你,陌生人。我记得你为我之前关于这个主题的问题做出了贡献。您的代码似乎确实解决了重叠注释的问题。例如,输入instr, annos = "AA----TT", {"gene1":[0,1,2,3,4], "gene2":[4,5,6,7]} 正确删除了注释中的重叠(因为只有间隙)并导致('AATT', {'gene1': [0, 1], 'gene2': [2, 3]})。感谢您的帮助!
猜你喜欢
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
相关资源
最近更新 更多