将第一个字典中的键与第二个字典中的值进行比较

Comparing key from first dictionary to values from second dictionary

拜托,我再次需要帮助。

我有一个包含很多信息的大数据库文件(姑且称之为db.csv)。

简化数据库文件说明:

我 运行 在我的基因序列上使用 arch61 -cluster_fast 以便对它们进行聚类。
我获得了一个名为 'clusters.uc' 的文件。我将它作为 csv 打开,然后我编写了一个代码来创建一个字典(假设 dict_1),将我的簇号作为键,将我的 gene_id(VFG...)作为值。
这是我制作然后存储在文件中的示例:dict_1

 0 ['VFG003386', 'VFG034084', 'VFG003381']  
 1 ['VFG000838', 'VFG000630', 'VFG035932', 'VFG000636']  
 2 ['VFG018349', 'VFG018485', 'VFG043567']  
 ...  
 14471 ['VFG015743', 'VFG002143']    

到目前为止一切顺利。然后使用 db.csv 我制作了另一个字典 (dict_2) gene_id (VFG...) 是键,VF_Accession (IA... 或 CVF.. 或 VF. ..) 是值,插图:dict_2

 VFG044259 IA027
 VFG044258 IA027
 VFG011941 CVF397
 VFG012016 CVF399
 ...  

最后我想要的是每个VF_Accession集群组的数量,插图:

IA027 [0,5,6,8]
CVF399 [15, 1025, 1562, 1712]
...   

所以我想因为我仍然是编码的初学者,所以我需要创建一个代码来比较来自 dict_1(VFG...) 的值和来自 dict_2(VFG. ..).如果它们匹配,将 VF_Accession 作为键,所有簇号作为值。由于 VF_Accession 是键,它们不能重复,所以我需要一个列表字典。我想我可以做到,因为我为 dict_1 做到了。但我的问题是,我无法找到一种方法来比较 dict_1 中的值和 dict_2 中的键,并为每个 VF_Accession 分配一个簇号。请帮助我。

警告:我没有做太多 Python 开发,所以可能有更好的方法来做到这一点。您可以先将您的 VFG... gene_ids 映射到它们的簇号,然后使用它来处理第二个字典:

from collections import defaultdict
import sys
import ast

# see 
vfg_cluster_map = defaultdict(list)

# map all of the vfg... keys to their cluster numbers first
with open(sys.argv[1], 'r') as dict_1:
    for line in dict_1:
        # split the line at the first space to separate the cluster number and gene ID list
        # e.g. after splitting the line "0 ['VFG003386', 'VFG034084', 'VFG003381']",
        # cluster_group_num holds "0", and vfg_list holds "['VFG003386', 'VFG034084', 'VFG003381']"
        cluster_group_num, vfg_list = line.strip().split(' ', 1)
        cluster_group_num = int(cluster_group_num)

        # convert "['VFG...', 'VFG...']" from a string to an actual list
        vfg_list = ast.literal_eval(vfg_list)
        for vfg in vfg_list:
            vfg_cluster_map[vfg].append(cluster_group_num)

# you now have a dictionary mapping gene IDs to the clusters they
# appear in, e.g 
# {'VFG003386': [0],
#  'VFG034084': [0],
#  ...}
# you can look in that dictionary to find the cluster numbers corresponding
# to your vfg... keys in dict_2 and add them to the list for that vf_accession
vf_accession_cluster_map = defaultdict(list)
with open(sys.argv[2], 'r') as dict_2:
    for line in dict_2:
        vfg, vf_accession = line.strip().split(' ')

        # add the list of cluster numbers corresponding to this vfg... to
        # the list of cluster numbers corresponding to this vf_accession 
        vf_accession_cluster_map[vf_accession].extend(vfg_cluster_map[vfg])

for vf_accession, cluster_list in vf_accession_cluster_map.items():
    print vf_accession + ' ' + str(cluster_list)

然后保存上面的脚本并像 python <script name> dict1_file dict2_file > output 一样调用它(或者您可以将字符串写入文件而不是打印它们和重定向)。

编辑:看完@BioGeek 的回答后,我应该注意到一次性处理所有内容比创建 dict_1 和 dict_2 文件并读入它们更有意义,将行解析回数字和列表等。如果您不需要先将字典写入文件,那么您只需将其他代码添加到脚本并直接使用字典。

首先,让我们为您的词典起一些比 dict_1dict_2 更好的名称,...这样可以更轻松地使用它们并记住它们包含的内容。

您首先创建了一个以簇号为键,以 gene_ids (VFG...) 为值的字典:

cluster_nr_to_gene_ids = {0: ['VFG003386', 'VFG034084', 'VFG003381', 'VFG044259'],
                          1: ['VFG000838', 'VFG000630', 'VFG035932', 'VFG000636'],
                          2: ['VFG018349', 'VFG018485', 'VFG043567', 'VFG012016'],
                          5: ['VFG011941'],
                          7949: ['VFG003386'],                              
                          14471: ['VFG015743', 'VFG002143', 'VFG012016']}

你还有另一个字典,其中 gene_ids 是键,VF_Accessions(IA...或 CVF.. 或 VF...)是值:

gene_id_to_vf_accession = {'VFG044259': 'IA027',
                           'VFG044258': 'IA027',
                           'VFG011941': 'CVF397',
                           'VFG012016': 'CVF399',
                           'VFG000676': 'VF0142',
                           'VFG002231': 'VF0369',
                           'VFG003386': 'CVF051'}

并且我们想要创建一个字典,其中每个 VF_Accession 键的值都是集群组的数量:vf_accession_to_cluster_groups.

我们还注意到一个 VF 登录号属于多个基因 ID(例如:VF 登录号 IA027 同时具有 VFG044259VFG044258 基因 ID。

所以我们使用defaultdict制作一个字典,以VF Accession为键,以基因ID列表为值

from collections import defaultdict
vf_accession_to_gene_ids = defaultdict(list)
for gene_id, vf_accession in gene_id_to_vf_accession.items():
    vf_accession_to_gene_ids[vf_accession].append(gene_id)

对于我上面发布的示例数据,vf_accession_to_gene_ids 现在看起来像:

defaultdict(<class 'list'>, {'VF0142': ['VFG000676'], 
                             'CVF051': ['VFG003386'], 
                             'IA027':  ['VFG044258', 'VFG044259'],
                             'CVF399': ['VFG012016'], 
                             'CVF397': ['VFG011941'], 
                             'VF0369': ['VFG002231']})

现在我们可以遍历每个 VF Accession 并查找其基因 ID 列表。然后,对于每个基因 ID,我们遍历每个簇并查看该基因 ID 是否存在:

vf_accession_to_cluster_groups = {}
for vf_accession in vf_accession_to_gene_ids:
    gene_ids = vf_accession_to_gene_ids[vf_accession]
    cluster_group = []
    for gene_id in gene_ids:
        for cluster_nr in cluster_nr_to_gene_ids:
            if gene_id in cluster_nr_to_gene_ids[cluster_nr]:
                cluster_group.append(cluster_nr)
    vf_accession_to_cluster_groups[vf_accession] = cluster_group

以上示例数据的最终结果现在是:

{'VF0142': [], 
 'CVF051': [0, 7949], 
 'IA027':  [0], 
 'CVF399': [2, 14471], 
 'CVF397': [5], 
 'VF0369': []}