Exemple #1
0
class TestCDSBiopythonConversion(unittest.TestCase):
    def setUp(self):
        self.cds = CDSFeature(FeatureLocation(0, 12, 1),
                              translation="A" * 4,
                              locus_tag="loctag",
                              gene="gene",
                              protein_id="prot_id")

    def convert(self):
        bio_features = self.cds.to_biopython()
        assert isinstance(bio_features, list)
        assert len(bio_features) == 1
        return bio_features[0]

    def test_basics(self):
        bio = self.convert()
        assert bio.location == self.cds.location
        assert bio.qualifiers["locus_tag"] == ["loctag"]
        assert bio.qualifiers["gene"] == ["gene"]
        assert bio.qualifiers["protein_id"] == ["prot_id"]
        assert bio.qualifiers["translation"] == ["A" * 4]

        regen = CDSFeature.from_biopython(bio)
        assert regen.location == self.cds.location
        assert regen.locus_tag == self.cds.locus_tag
        assert regen.gene == self.cds.gene
        assert regen.protein_id == self.cds.protein_id

    def test_without_genefunctions(self):
        bio = self.convert()
        assert "gene_functions" not in bio.qualifiers
        assert "gene_kind" not in bio.qualifiers

        regen = CDSFeature.from_biopython(bio)
        assert not regen.gene_functions

    def test_with_genefunctions(self):
        self.cds.gene_functions.add(GeneFunction.ADDITIONAL, "testtool",
                                    "dummy")
        bio = self.convert()
        assert "gene_functions" in bio.qualifiers
        assert bio.qualifiers["gene_kind"] == [str(
            self.cds.gene_function)] == ["biosynthetic-additional"]

        regen = CDSFeature.from_biopython(bio)
        assert regen.gene_function == self.cds.gene_function
        assert regen.gene_functions.get_by_tool(
            "testtool") == self.cds.gene_functions.get_by_tool("testtool")

    def test_without_secmet(self):
        assert not self.cds.sec_met
        bio = self.convert()
        assert "sec_met" not in bio.qualifiers  # for detecting legacy versions
        assert "sec_met_domain" not in bio.qualifiers

        regen = CDSFeature.from_biopython(bio)
        assert not regen.sec_met

    def test_with_secmet(self):
        domains = [
            SecMetQualifier.Domain("testA", 0.1, 1.1, 3, "test"),
            SecMetQualifier.Domain("testB", 5.1, 3.9, 5, "dummy")
        ]
        self.cds.sec_met = SecMetQualifier(domains)
        bio = self.convert()
        assert "sec_met" not in bio.qualifiers  # again, detecting leftover legacy versions
        assert len(bio.qualifiers["sec_met_domain"]) == 2
        assert bio.qualifiers["sec_met_domain"] == list(map(str, domains))

        regen = CDSFeature.from_biopython(bio)
        assert regen.sec_met
        assert len(regen.sec_met.domains) == len(domains)
        assert regen.sec_met.domains == domains

    def test_mixed_strand(self):
        bio = self.cds.to_biopython()[0]
        for location in [
                CompoundLocation([
                    FeatureLocation(1, 5, strand=-1),
                    FeatureLocation(8, 10, strand=1)
                ]),
                CompoundLocation([
                    FeatureLocation(1, 5, strand=1),
                    FeatureLocation(8, 10, strand=None)
                ])
        ]:
            bio.location = location
            with self.assertRaisesRegex(
                    ValueError, "compound locations with mixed strands"):
                CDSFeature.from_biopython(bio)
Exemple #2
0
class TestCDSBiopythonConversion(unittest.TestCase):
    def setUp(self):
        self.cds = CDSFeature(FeatureLocation(0, 12, 1),
                              translation="A" * 4,
                              locus_tag="loctag",
                              gene="gene",
                              protein_id="prot_id")

    def convert(self):
        bio_features = self.cds.to_biopython()
        assert isinstance(bio_features, list)
        assert len(bio_features) == 1
        return bio_features[0]

    def test_basics(self):
        bio = self.convert()
        assert bio.location == self.cds.location
        assert bio.qualifiers["locus_tag"] == ["loctag"]
        assert bio.qualifiers["gene"] == ["gene"]
        assert bio.qualifiers["protein_id"] == ["prot_id"]
        assert bio.qualifiers["translation"] == ["A" * 4]

        regen = CDSFeature.from_biopython(bio)
        assert regen.location == self.cds.location
        assert regen.locus_tag == self.cds.locus_tag
        assert regen.gene == self.cds.gene
        assert regen.protein_id == self.cds.protein_id

    def test_without_genefunctions(self):
        bio = self.convert()
        assert "gene_functions" not in bio.qualifiers
        assert "gene_kind" not in bio.qualifiers

        regen = CDSFeature.from_biopython(bio)
        assert not regen.gene_functions

    def test_with_genefunctions(self):
        self.cds.gene_functions.add(GeneFunction.ADDITIONAL, "testtool",
                                    "dummy")
        bio = self.convert()
        assert "gene_functions" in bio.qualifiers
        assert bio.qualifiers["gene_kind"] == [str(
            self.cds.gene_function)] == ["biosynthetic-additional"]

        regen = CDSFeature.from_biopython(bio)
        assert regen.gene_function == self.cds.gene_function
        assert regen.gene_functions.get_by_tool(
            "testtool") == self.cds.gene_functions.get_by_tool("testtool")

    def test_without_secmet(self):
        assert not self.cds.sec_met
        bio = self.convert()
        assert "sec_met" not in bio.qualifiers  # for detecting legacy versions
        assert "sec_met_domain" not in bio.qualifiers

        regen = CDSFeature.from_biopython(bio)
        assert not regen.sec_met

    def test_with_secmet(self):
        domains = [
            SecMetQualifier.Domain("testA", 0.1, 1.1, 3, "test"),
            SecMetQualifier.Domain("testB", 5.1, 3.9, 5, "dummy")
        ]
        self.cds.sec_met = SecMetQualifier(domains)
        bio = self.convert()
        assert "sec_met" not in bio.qualifiers  # again, detecting leftover legacy versions
        assert len(bio.qualifiers["sec_met_domain"]) == 2
        assert bio.qualifiers["sec_met_domain"] == list(map(str, domains))

        regen = CDSFeature.from_biopython(bio)
        assert regen.sec_met
        assert len(regen.sec_met.domains) == len(domains)
        assert regen.sec_met.domains == domains

    def test_mixed_strand(self):
        bio = self.cds.to_biopython()[0]
        for location in [
                CompoundLocation([
                    FeatureLocation(1, 5, strand=-1),
                    FeatureLocation(8, 10, strand=1)
                ]),
                CompoundLocation([
                    FeatureLocation(1, 5, strand=1),
                    FeatureLocation(8, 10, strand=None)
                ])
        ]:
            bio.location = location
            with self.assertRaisesRegex(
                    ValueError, "compound locations with mixed strands"):
                CDSFeature.from_biopython(bio)
        # compound locations starting with an invalid strand will be treated as per a non-compound wtih a bad strand

    def test_translation_outside_record(self):
        rec = DummyRecord(seq="A" * 10)
        for location in [
                FeatureLocation(0, AfterPosition(6), strand=1),
                FeatureLocation(BeforePosition(4), 10, strand=-1)
        ]:
            bio = SeqFeature(location, type="CDS")
            bio.qualifiers["translation"] = ["M" * 5]
            with self.assertRaisesRegex(SecmetInvalidInputError,
                                        "translation extends out of record"):
                CDSFeature.from_biopython(bio, record=rec)

    def test_invalid_translation_table(self):
        bio = self.cds.to_biopython()[0]
        bio.qualifiers["transl_table"] = ["11a"]
        with self.assertRaisesRegex(SecmetInvalidInputError,
                                    "invalid translation table"):
            CDSFeature.from_biopython(bio)