Пример #1
0
 def test_get_max_distance_to_common_ancestor(self):
     self.assertEqual(
         taxonomy_util.Taxon(
             name='mycoplasma').get_max_distance_to_common_ancestor(), 8)
     self.assertEqual(
         taxonomy_util.Taxon(name='mycoplasma genitalium').
         get_max_distance_to_common_ancestor(), 9)
     self.assertEqual(
         taxonomy_util.Taxon(name='mycoplasma genitalium G37').
         get_max_distance_to_common_ancestor(), 10)
     self.assertEqual(
         taxonomy_util.Taxon(name='mycoplasma genitalium XXX').
         get_max_distance_to_common_ancestor(), 10)
     self.assertEqual(
         taxonomy_util.Taxon(name='mycoplasma genitalium XXX YYY').
         get_max_distance_to_common_ancestor(), 11)
Пример #2
0
    def test_Taxon_get_parent_taxa(self):
        parents = taxonomy_util.Taxon(
            name='mycoplasma genitalium').get_parent_taxa()
        self.assertEqual(parents[-1].name, 'Mycoplasma')
        self.assertEqual(parents[-2].name, 'Mycoplasmataceae')

        parents = taxonomy_util.Taxon(
            name='mycoplasma genitalium G37').get_parent_taxa()
        self.assertEqual(parents[-1].name, 'Mycoplasma genitalium')
        self.assertEqual(parents[-2].name, 'Mycoplasma')
        self.assertEqual(parents[-3].name, 'Mycoplasmataceae')

        parents = taxonomy_util.Taxon(
            name='mycoplasma genitalium XXX').get_parent_taxa()
        self.assertEqual(parents[-1].name, 'Mycoplasma genitalium')
        self.assertEqual(parents[-2].name, 'Mycoplasma')
        self.assertEqual(parents[-3].name, 'Mycoplasmataceae')
Пример #3
0
    def test_Taxon_init_from_id(self):
        taxon = taxonomy_util.Taxon(ncbi_id=2093)
        self.assertEqual(taxon.name, 'Mycoplasma')
        self.assertEqual(taxon.id_of_nearest_ncbi_taxon, 2093)
        self.assertEqual(taxon.distance_from_nearest_ncbi_taxon, 0)
        self.assertEqual(taxon.additional_name_beyond_nearest_ncbi_taxon, '')

        taxon = taxonomy_util.Taxon(ncbi_id=2097)
        self.assertEqual(taxon.name, 'Mycoplasma genitalium')
        self.assertEqual(taxon.id_of_nearest_ncbi_taxon, 2097)
        self.assertEqual(taxon.distance_from_nearest_ncbi_taxon, 0)
        self.assertEqual(taxon.additional_name_beyond_nearest_ncbi_taxon, '')

        taxon = taxonomy_util.Taxon(ncbi_id=243273)
        self.assertEqual(taxon.name, 'Mycoplasma genitalium G37')
        self.assertEqual(taxon.id_of_nearest_ncbi_taxon, 243273)
        self.assertEqual(taxon.distance_from_nearest_ncbi_taxon, 0)
        self.assertEqual(taxon.additional_name_beyond_nearest_ncbi_taxon, '')

        self.assertRaises(ValueError, taxonomy_util.Taxon, ncbi_id=-1)
Пример #4
0
def create_taxon(id_or_name):
    """ Create a taxon with NCBI id=:obj:`id_or_name` or name=:obj:`id_or_name`

    Args:
        id_or_name (:obj:`str`): NCBI id or name

    Returns:
        :obj:`taxonomy_util.Taxon`: taxon
    """
    ncbi_id = None
    name = None
    try:
        ncbi_id = float(id_or_name)
    except ValueError:
        name = id_or_name

    return taxonomy_util.Taxon(ncbi_id=ncbi_id, name=name)
Пример #5
0
    def test_Taxon_init_from_name(self):
        taxon = taxonomy_util.Taxon(name='mycoplasma')
        self.assertEqual(taxon.name, 'Mycoplasma')
        self.assertEqual(taxon.id_of_nearest_ncbi_taxon, 2093)
        self.assertEqual(taxon.distance_from_nearest_ncbi_taxon, 0)
        self.assertEqual(taxon.additional_name_beyond_nearest_ncbi_taxon, '')

        taxon = taxonomy_util.Taxon(name='mycoplasma genitalium')
        self.assertEqual(taxon.name, 'Mycoplasma genitalium')
        self.assertEqual(taxon.id_of_nearest_ncbi_taxon, 2097)
        self.assertEqual(taxon.distance_from_nearest_ncbi_taxon, 0)
        self.assertEqual(taxon.additional_name_beyond_nearest_ncbi_taxon, '')

        taxon = taxonomy_util.Taxon(name='mycoplasma genitalium G37')
        self.assertEqual(taxon.name, 'Mycoplasma genitalium G37')
        self.assertEqual(taxon.id_of_nearest_ncbi_taxon, 243273)
        self.assertEqual(taxon.distance_from_nearest_ncbi_taxon, 0)
        self.assertEqual(taxon.additional_name_beyond_nearest_ncbi_taxon, '')

        taxon = taxonomy_util.Taxon(name='mycoplasma genitalium XXX')
        self.assertEqual(taxon.name, 'Mycoplasma genitalium XXX')
        self.assertEqual(taxon.id_of_nearest_ncbi_taxon, 2097)
        self.assertEqual(taxon.distance_from_nearest_ncbi_taxon, 1)
        self.assertEqual(taxon.additional_name_beyond_nearest_ncbi_taxon,
                         ' XXX')

        taxon = taxonomy_util.Taxon(name='mycoplasma XXX')
        self.assertEqual(taxon.name, 'Mycoplasma XXX')
        self.assertEqual(taxon.id_of_nearest_ncbi_taxon, 2093)
        self.assertEqual(taxon.distance_from_nearest_ncbi_taxon, 1)
        self.assertEqual(taxon.additional_name_beyond_nearest_ncbi_taxon,
                         ' XXX')

        taxon = taxonomy_util.Taxon(name='mycoplasma XXX YYY')
        self.assertEqual(taxon.name, 'Mycoplasma XXX YYY')
        self.assertEqual(taxon.id_of_nearest_ncbi_taxon, 2093)
        self.assertEqual(taxon.distance_from_nearest_ncbi_taxon, 2)
        self.assertEqual(taxon.additional_name_beyond_nearest_ncbi_taxon,
                         ' XXX YYY')
Пример #6
0
    def test_Taxon_get_ncbi_id(self):
        taxon = taxonomy_util.Taxon(ncbi_id=2093)
        self.assertEqual(taxon.get_ncbi_id(), 2093)

        taxon = taxonomy_util.Taxon(name='mycoplasma XXX')
        self.assertEqual(taxon.get_ncbi_id(), None)
Пример #7
0
    def test_get_distance_to_common_ancestor(self):
        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium')
        taxon_b = taxonomy_util.Taxon(name='mycoplasma pneumoniae')
        self.assertEqual(taxon_a.get_distance_to_common_ancestor(taxon_b), 1)

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium G37')
        taxon_b = taxonomy_util.Taxon(name='mycoplasma genitalium XXX')
        self.assertEqual(taxon_a.get_distance_to_common_ancestor(taxon_b), 1)

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium G37')
        taxon_b = taxonomy_util.Taxon(name='mycoplasma pneumoniae')
        self.assertEqual(taxon_a.get_distance_to_common_ancestor(taxon_b), 2)

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium G37')
        taxon_b = taxonomy_util.Taxon(name='mycoplasma')
        self.assertEqual(taxon_a.get_distance_to_common_ancestor(taxon_b), 2)

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium XXX')
        taxon_b = taxonomy_util.Taxon(name='mycoplasma')
        self.assertEqual(taxon_a.get_distance_to_common_ancestor(taxon_b), 2)

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium G37')
        taxon_b = taxonomy_util.Taxon(name='escherichia coli')
        self.assertEqual(taxon_a.get_distance_to_common_ancestor(taxon_b), 8)

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium XXX')
        taxon_b = taxonomy_util.Taxon(name='escherichia coli')
        self.assertEqual(taxon_a.get_distance_to_common_ancestor(taxon_b), 8)

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium')
        taxon_b = taxonomy_util.Taxon(name='escherichia coli')
        self.assertEqual(taxon_a.get_distance_to_common_ancestor(taxon_b), 7)
Пример #8
0
    def test_Taxon_get_common_ancestor(self):
        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium')
        taxon_b = taxonomy_util.Taxon(name='mycoplasma pneumoniae')
        self.assertEqual(
            taxon_a.get_common_ancestor(taxon_b).name, 'Mycoplasma')

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium G37')
        taxon_b = taxonomy_util.Taxon(name='mycoplasma genitalium XXX')
        self.assertEqual(
            taxon_a.get_common_ancestor(taxon_b).name, 'Mycoplasma genitalium')

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium G37')
        taxon_b = taxonomy_util.Taxon(name='mycoplasma pneumoniae')
        self.assertEqual(
            taxon_a.get_common_ancestor(taxon_b).name, 'Mycoplasma')

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium G37')
        taxon_b = taxonomy_util.Taxon(name='mycoplasma')
        self.assertEqual(
            taxon_a.get_common_ancestor(taxon_b).name, 'Mycoplasma')

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium XXX')
        taxon_b = taxonomy_util.Taxon(name='mycoplasma')
        self.assertEqual(
            taxon_a.get_common_ancestor(taxon_b).name, 'Mycoplasma')

        taxon_a = taxonomy_util.Taxon(name='mycoplasma genitalium G37')
        taxon_b = taxonomy_util.Taxon(name='escherichia coli')
        self.assertEqual(taxon_a.get_common_ancestor(taxon_b).name, 'Bacteria')
Пример #9
0
 def test_Taxon_get_rank(self):
     self.assertEqual(taxonomy_util.Taxon(ncbi_id=2093).get_rank(), 'genus')
     self.assertEqual(
         taxonomy_util.Taxon(ncbi_id=2097).get_rank(), 'species')
     self.assertEqual(taxonomy_util.Taxon(ncbi_id=243273).get_rank(), None)