【问题标题】:Python - grouping defaultdict values by hamming distance in keysPython - 通过键中的汉明距离对 defaultdict 值进行分组
【发布时间】:2016-10-11 15:55:01
【问题描述】:

我有一个带有 ~700 个键的默认字典。密钥采用 A_B_STRING 等格式。我需要做的是用'_'分割键,如果A和B相同,比较每个键的'STRING'之间的距离。如果距离

输入文件为FASTA 格式,其中标题是键,值是序列(使用 defaultdict 是因为多个序列根据原始 fasta 文件的爆炸报告具有相同的标题)。

这是我目前所拥有的:

!/usr/bin/env python

import sys
from collections import defaultdict
import itertools

inp = sys.argv[1]                                                       # input fasta file; format '>header'\n'sequence'

with open(inp, 'r') as f:
        h = []
        s = []
        for line in f:
                if line.startswith(">"):
                        h.append(line.strip().split('>')[1])            # append headers to list
                else:
                        s.append(line.strip())                          # append sequences to list

seqs = dict(zip(h,s))                                                   # create dictionary of headers:sequence

print 'Total Sequences: ' + str(len(seqs))                              # Numb. total sequences in input file

groups = defaultdict(list)

for i in seqs:
        groups['_'.join(i.split('_')[1:])].append(seqs[i])                      # Create defaultdict with sequences in lists with identical headers

def hamming(str1, str2):
        """ Simple hamming distance calculator """
        if len(str1) == len(str2):
                diffs = 0
                for ch1, ch2 in zip(str1,str2):
                        if ch1 != ch2:
                                diffs += 1
                return diff

keys = [x for x in groups]

combos = list(itertools.combinations(keys,2))                           # Create tupled list with all comparison combinations

combined = defaultdict(list)                                            # Defaultdict in which to place groups

for i in combos:                                                        # Combo = (A1_B1_STRING2, A2_B2_STRING2)
        a1 = i[0].split('_')[0]
        a2 = i[1].split('_')[0]

        b1 = i[0].split('_')[1]                                         # Get A's, B's, C's
        b2 = i[1].split('_')[1]

        c1 = i[0].split('_')[2]
        c2 = i[1].split('_')[2]

        if a1 == a2 and b1 == b2:                                       # If A1 is equal to A2 and B1 is equal to B2
                d = hamming(c1, c2)                                     # Get distance of STRING1 vs STRING2
                if d <= 2:                                              # If distance is less than or equal to 2
                        combined[i[0]].append(groups[i[0]] + groups[i[1]])      # Add to defaultdict by combo 1 key

print len(combined)
for c in sorted(combined):
        print c, '\t', len(combined[c])

问题是这段代码没有按预期工作。打印组合默认字典中的键时;我清楚地看到有很多可以组合的。但是,组合的 defaultdict 的长度大约是原始大小的一半。

编辑

替代没有 itertools.combinations:

for a in keys:
        tocombine = []
        tocombine.append(a)
        tocheck = [x for x in keys if x != a]
        for b in tocheck:
                i = (a,b)                                               # Combo = (A1_B1_STRING2, A2_B2_STRING2)
                a1 = i[0].split('_')[0]
                a2 = i[1].split('_')[0]

                b1 = i[0].split('_')[1]                                         # Get A's, B's, C's
                b2 = i[1].split('_')[1]

                c1 = i[0].split('_')[2]
                c2 = i[1].split('_')[2]

                if a1 == a2 and b1 == b2:                                       # If A1 is equal to A2 and B1 is equal to B2
                        if len(c1) == len(c2):                                          # If length of STRING1 is equal to STRING2
                                d = hamming(c1, c2)                                     # Get distance of STRING1 vs STRING2
                                if d <= 2:
                                        tocombine.append(b)
        for n in range(len(tocombine[1:])):
                keys.remove(tocombine[n])
                combined[tocombine[0]].append(groups[tocombine[n]])

final = defaultdict(list)
for i in combined:
        final[i] = list(itertools.chain.from_iterable(combined[i]))

但是,使用这些方法,我仍然缺少一些与其他方法不匹配的部分。

【问题讨论】:

  • 您的汉明文档字符串中缺少一个 ",这会导致格式错误,您可以把它放在那里吗?我会为您编辑它,但堆栈溢出需要至少 6 个字符的编辑://
  • 改变了它。对我遇到的问题有什么想法吗?

标签: python combinations string-comparison defaultdict hamming-distance


【解决方案1】:

我认为您的代码存在一个问题,请考虑这种情况:

0: A_B_DATA1 
1: A_B_DATA2    
2: A_B_DATA3 

All the valid comparisons are:  
0 -> 1 * Combines under key 'A_B_DATA1' 
0 -> 2 * Combines under key 'A_B_DATA1'
1 -> 2 * Combines under key 'A_B_DATA2' **opps

我想你会希望所有这三个组合在一个键下。但是考虑一下这种情况:

0: A_B_DATA111
1: A_B_DATA122    
2: A_B_DATA223 

All the valid comparisons are:  
0 -> 1 * Combines under key 'A_B_DATA111' 
0 -> 2 * Combines under key 'A_B_DATA111'
1 -> 2 * Combines under key 'A_B_DATA122'

现在有点棘手,因为第 0 行距第 1 行的距离为 2,而第 1 行距第 2 行的距离为 2,但您可能不希望它们全部放在一起,因为第 0 行距第 2 行的距离为 3!

这是一个可行的解决方案示例,假设这是您希望输出的样子:

def unpack_key(key):
    data = key.split('_')
    return '_'.join(data[:2]), '_'.join(data[2:])

combined = defaultdict(list)
for key1 in groups:
    combined[key1] = []
    key1_ab, key1_string = unpack_key(key1)
    for key2 in groups:
        if key1 != key2:
            key2_ab, key2_string = unpack_key(key2)
            if key1_ab == key2_ab and len(key1_string) == len(key2_string):
               if hamming(key1_string, key2_string) <= 2:
                   combined[key1].append(key2)

在我们的第二个示例中,这将产生以下字典,如果这不是您要寻找的答案,您能否准确输入此示例的最终字典应该是什么?

A_B_DATA111: ['A_B_DATA122']
A_B_DATA122: ['A_B_DATA111', 'A_B_DATA223']
A_B_DATA223: ['A_B_DATA122']

请记住,这是一个 O(n^2) 算法,这意味着当您的密钥集变得更大时,它是不可扩展的。

【讨论】:

  • 我明白你对关键问题的意思。我想我想出了一个解决方案,不使用 itertoosl 组合,请参阅编辑
  • 你能告诉我你对上述场景的期望输出吗?
  • 输出应该是默认字典,其中键包含通过上述条件的值:所有值原始标题必须具有相同的 A 和 B,并且每个 STRING 应该相差
  • 我明白了,但我觉得有多种方法可以表示这个答案。你能告诉我输出应该是什么样子吗(结果字典的键/值是什么)。
  • 好的。所以假设键 A_B_DATA111 在其列表中有 10 个序列,A_B_DATA122 有 7 个,A_B_DATA223 有 4 个。在最终的字典中,A_B_DATA111 将是具有上述条件的其他键搜索的代表键。 A_B_DATA122 应属于 A_B_DATA111。 A_B_DATA223,将针对其他键进行搜索。
猜你喜欢
  • 1970-01-01
  • 2015-09-06
  • 2017-09-10
  • 2016-12-16
  • 2015-03-21
  • 2012-03-10
  • 2014-01-28
  • 1970-01-01
相关资源
最近更新 更多