Beispiel #1
0
 def test_genbank(self):
     dummy_record = Record(Seq("A" * 100, generic_dna))
     clusters = [
         create_cluster(3, 20, "prodA"),
         create_cluster(25, 41, "prodB")
     ]
     for cluster in clusters:
         dummy_record.add_cluster(cluster)
     subregion = SubRegion(FeatureLocation(35, 71), "test", 0.7)
     dummy_record.add_subregion(subregion)
     supercluster = SuperCluster(SuperCluster.kinds.NEIGHBOURING, clusters)
     dummy_record.add_supercluster(supercluster)
     region = Region(superclusters=[supercluster], subregions=[subregion])
     dummy_record.add_region(region)
     with NamedTemporaryFile(suffix=".gbk") as output:
         region.write_to_genbank(output.name)
         bio = list(seqio.parse(output.name))
     assert len(bio) == 1
     rec = Record.from_biopython(bio[0], taxon="bacteria")
     assert len(rec.get_regions()) == 1
     new = rec.get_region(0)
     assert new.location.start == 3 - region.location.start
     assert new.location.end == 71 - region.location.start
     assert new.products == region.products
     assert new.probabilities == region.probabilities
Beispiel #2
0
class TestSuperCluster(unittest.TestCase):
    def setUp(self):
        self.record = Record(Seq("A" * 100))
        clusters = [create_cluster(0, 0, 10, 10)]
        for cluster in clusters:
            self.record.add_cluster(cluster)

    def test_kinds_attachment(self):
        assert SuperCluster.kinds == SuperClusterKind

    def test_record_linkage(self):
        cluster = SuperCluster(SuperCluster.kinds.INTERLEAVED,
                               self.record.get_clusters())
        with self.assertRaisesRegex(ValueError,
                                    "SuperCluster not contained in record"):
            cluster.get_supercluster_number()
        self.record.add_supercluster(cluster)
        assert cluster.get_supercluster_number() == 1

    def test_bad_kind(self):
        with self.assertRaisesRegex(TypeError, "should be SuperClusterKind"):
            SuperCluster("berf", self.record.get_clusters())

    def test_no_clusters(self):
        with self.assertRaisesRegex(ValueError,
                                    "cannot exist without at least one"):
            SuperCluster(SuperCluster.kinds.INTERLEAVED, [])

    def test_rules(self):
        cluster = SuperCluster(SuperCluster.kinds.INTERLEAVED,
                               self.record.get_clusters())
        assert cluster.detection_rules == [
            cluster.detection_rule for cluster in self.record.get_clusters()
        ]

    def test_smiles_and_polymer(self):
        cluster = SuperCluster(SuperCluster.kinds.INTERLEAVED,
                               self.record.get_clusters())
        assert cluster.smiles_structure is None
        assert cluster.polymer is None

    def test_conversion(self):
        kind = SuperClusterKind.INTERLEAVED
        original = SuperCluster(kind,
                                self.record.get_clusters(),
                                smiles="dummy smiles",
                                polymer="dummy polymer")
        self.record.add_supercluster(original)
        assert original.products == ["a"]
        assert len(original.clusters) == 1
        bios = original.to_biopython()
        assert len(bios) == 1
        bio = bios[0]
        assert bio.qualifiers["product"] == ["a"]
        assert bio.qualifiers["kind"] == [str(kind)]
        assert bio.qualifiers["candidate_cluster_number"] == [
            str(original.get_supercluster_number())
        ]
        assert bio.qualifiers["SMILES"] == ["dummy smiles"]
        assert bio.qualifiers["polymer"] == ["dummy polymer"]
        assert bio.qualifiers["contig_edge"] == ["True"]
        regenerated = SuperCluster.from_biopython(bio)
        assert isinstance(regenerated, TemporarySuperCluster)
        assert regenerated.products == original.products
        assert regenerated.location == original.location
        assert regenerated.smiles_structure == original.smiles_structure
        assert regenerated.polymer == original.polymer
        assert regenerated.clusters == [
            cluster.get_cluster_number()
            for cluster in self.record.get_clusters()
        ]
        assert regenerated.kind == original.kind

        real = regenerated.convert_to_real_feature(self.record)
        assert isinstance(real, SuperCluster)
        assert len(real.clusters) == len(self.record.get_clusters())
        for reference, record_cluster in zip(real.clusters,
                                             self.record.get_clusters()):
            assert reference is record_cluster

        # attempt a conversion with a record missing the cluster
        self.record.clear_clusters()
        with self.assertRaisesRegex(ValueError,
                                    "Not all referenced clusters are present"):
            regenerated.convert_to_real_feature(self.record)