如何比较集群?

2024-10-01 09:28:58 发布

您现在位置:Python中文网/ 问答频道 /正文

希望这可以用python实现!我在同一个数据上使用了两个集群程序,现在两个都有了一个集群文件。我重新格式化了文件,使它们看起来像这样:

Cluster 0:
Brucellaceae(10)
    Brucella(10)
        abortus(1)
        canis(1)
        ceti(1)
        inopinata(1)
        melitensis(1)
        microti(1)
        neotomae(1)
        ovis(1)
        pinnipedialis(1)
        suis(1)
Cluster 1:
    Streptomycetaceae(28)
        Streptomyces(28)
            achromogenes(1)
            albaduncus(1)
            anthocyanicus(1)

etc.

这些文件包含细菌种类信息。所以我有了簇数(0),然后在它的正下方是布鲁氏菌科(brucellae)和这个家族中的细菌数量(10)。下面是在该科中发现的属(名称后接编号,布鲁氏菌属(10)),最后是每个属的物种(abortus(1)),等等。在

我的问题:我有两个这样格式化的文件,我想写一个程序来寻找两者之间的差异。唯一的问题是两个程序以不同的方式聚集,因此两个群集可能是相同的,即使实际的“簇号”不同(因此一个文件中的簇1的内容可能与另一个文件中的簇43相匹配,唯一不同的是实际的簇号)。所以我需要一些东西来忽略簇号,集中在簇的内容上。在

有什么方法可以比较这两个文件来检查它们的区别吗?有可能吗?任何想法都将不胜感激!在


Tags: 文件数据程序内容集群cluster细菌canis
3条回答

给予:

file1 = '''Cluster 0:
 giant(2)
  red(2)
   brick(1)
   apple(1)
Cluster 1:
 tiny(3)
  green(1)
   dot(1)
  blue(2)
   flower(1)
   candy(1)'''.split('\n')
file2 = '''Cluster 18:
 giant(2)
  red(2)
   brick(1)
   tomato(1)
Cluster 19:
 tiny(2)
  blue(2)
   flower(1)
   candy(1)'''.split('\n')

这是你需要的吗?在

^{pr2}$

要了解差异:

for desc, items in differences:
    print desc
    print 
    for item in items:
        print '\t' + item
    print

印刷品

common elements

    giant.red.brick
    tiny.blue.candy
    tiny.blue.flower

missing from file2

    tiny.green.dot
    giant.red.apple

missing from file1

    giant.red.tomato

我在评论中看到了很多不同的答案,为了帮助您,我将给您一个非常非常简单的脚本实现,您可以从中开始。在

请注意,这个并不能回答您的全部问题,而是在评论中为您指出一个方向。在

通常,如果你没有经验,我会建议你去读一读Python(无论如何我都会这么做,我会在答案的底部加上一些链接)

去玩好玩的东西吧!:)

class Cluster(object):
  '''
  This is a class that will contain your information about the Clusters.
  '''
  def __init__(self, number):
    '''
    This is what some languages call a constructor, but it's not.
    This method initializes the properties with values from the method call.
    '''
    self.cluster_number = number
    self.family_name = None
    self.bacteria_name = None
    self.bacteria = []

#This part below isn't a part of the class, this is the actual script.
with open('bacteria.txt', 'r') as file:
  cluster = None
  clusters = []
  for index, line in enumerate(file):
    if line.startswith('Cluster'):
      cluster = Cluster(index)
      clusters.append(cluster)
    else:
      if not cluster.family_name:
        cluster.family_name = line
      elif not cluster.bacteria_name:
        cluster.bacteria_name = line
      else:
        cluster.bacteria.append(line)

我在没有任何花哨的东西和python2.7.2的情况下,尽可能地编写了这篇愚蠢而过于简单的文章 您可以将这个文件复制到.py文件中,然后直接从命令行python bacteria.py运行它。在

希望这有点帮助,如果您有任何问题,请随时访问我们的Python聊天室!:)

你必须写一些代码来解析文件。如果你忽略了聚类,你应该能够根据缩进区分科、属和种。在

定义named tuple的最简单方法是:

import collections
Bacterium = collections.namedtuple('Bacterium', ['family', 'genera', 'species'])

在这个对象的实例中,您可以这样做:

^{pr2}$

解析器应该逐行读取文件,并设置族和属。如果它找到了一个物种,它应该在一个列表中添加一个细菌

with open('cluster0.txt', 'r') as infile:
    lines = infile.readlines()
family = None
genera = None
bacteria = []
for line in lines:
    # set family and genera.
    # if you detect a bacterium:
    bacteria.append(Bacterium(family, genera, species))

一旦您有了每个文件或集群中所有细菌的列表,您可以从所有细菌中进行选择,如下所示:

s = [b for b in bacteria if b.genera == 'Streptomycetaceae']

相关问题 更多 >