Example #1
0
 def __add__(self, other):
     if hasattr(other, "seq") and hasattr(other.seq, "watson"):
         other = _copy.deepcopy(other)
         for f in other.features:
             f.location = f.location + other.seq.ovhg
         answer = Dseqrecord(_SeqRecord.__add__(self, other))
         answer.n = min(self.n, other.n)
     else:
         answer = Dseqrecord(_SeqRecord.__add__(self, Dseqrecord(other)))
         answer.n = self.n
     return answer
Example #2
0
def test_lcs():
    from Bio.Seq import Seq
    from Bio.SeqRecord import SeqRecord as BSeqRecord
    from pydna.dseq import Dseq
    from pydna.dseqrecord import Dseqrecord
    from pydna.seqrecord import SeqRecord

    from pydna.seqfeature import SeqFeature
    from Bio.SeqFeature import FeatureLocation, ExactPosition

    s = SeqRecord(Seq("GGATCC"))

    expected = SeqFeature()
    expected.__dict__ = {
        "location": FeatureLocation(ExactPosition(0),
                                    ExactPosition(6),
                                    strand=1),
        "type": "read",
        "id": "<unknown id>",
        "qualifiers": {
            "label": ["sequence"],
            "ApEinfo_fwdcolor": ["#DAFFCF"],
            "ApEinfo_revcolor": ["#DFFDFF"],
        },
    }

    assert s.lcs("GGATCC", limit=4).__dict__ == expected.__dict__
    assert s.lcs(Seq("GGATCC"), limit=4).__dict__ == expected.__dict__
    assert (s.lcs(BSeqRecord(Seq("GGATCC"), name="sequence"),
                  limit=4).__dict__ == expected.__dict__)
    assert s.lcs(Dseq("GGATCC"), limit=4).__dict__ == expected.__dict__
    assert (s.lcs(Dseqrecord(Dseq("GGATCC"), name="sequence"),
                  limit=4).__dict__ == expected.__dict__)
    assert (s.lcs(Dseqrecord("GGATCC", name="sequence"),
                  limit=4).__dict__ == expected.__dict__)
Example #3
0
def test_format():
    from Bio.Seq import Seq
    from pydna.seqrecord import SeqRecord

    s = SeqRecord(Seq("GGATCC"))
    s.format("gb")
    s.format("genbank")
    s.format("fasta")
Example #4
0
def test_format():
    from Bio.Seq import Seq
    from pydna.seqrecord  import SeqRecord
    from Bio.Alphabet.IUPAC import IUPACAmbiguousDNA
    s = SeqRecord(Seq("GGATCC",alphabet=IUPACAmbiguousDNA()))
    s.format("gb")
    s.format("genbank")
    s.format("fasta")   
Example #5
0
    def __add__(self, other):
        if hasattr(other, "seq") and hasattr(other.seq, "watson"):
            other = _copy.deepcopy(other)
            other_five_prime = other.seq.five_prime_end()
            if other_five_prime[0] == "5'":
                # add other.seq.ovhg
                for f in other.features:
                    f.location = f.location + other.seq.ovhg
            elif other_five_prime[0] == "3'":
                # subtract other.seq.ovhg (sign change)
                for f in other.features:
                    f.location = f.location + (-other.seq.ovhg)

            answer = Dseqrecord(_SeqRecord.__add__(self, other))
            answer.n = min(self.n, other.n)
        else:
            answer = Dseqrecord(_SeqRecord.__add__(self, Dseqrecord(other)))
            answer.n = self.n
        return answer
Example #6
0
def test___hash__():
    from Bio.Seq import Seq
    from pydna.seqrecord import SeqRecord

    s = SeqRecord(Seq("GGATCC"))
    t = SeqRecord(Seq("GGATCC"))
    u = SeqRecord(Seq("GGATCc"))
    assert hash(s) == hash(t) != hash(u)

    assert s == t
    assert s != u
    assert s != "1"

    assert s < u
    assert not s > u

    with pytest.raises(TypeError):
        s > "3"
    with pytest.raises(TypeError):
        s < "3"
Example #7
0
def test___hash__():
    from Bio.Seq import Seq
    from pydna.seqrecord  import SeqRecord
    from Bio.Alphabet.IUPAC import IUPACAmbiguousDNA
    s = SeqRecord(Seq("GGATCC",alphabet=IUPACAmbiguousDNA()))
    t = SeqRecord(Seq("GGATCC",alphabet=IUPACAmbiguousDNA()))
    u = SeqRecord(Seq("GGATCc",alphabet=IUPACAmbiguousDNA()))
    assert hash(s) == hash(t) != hash(u)
    
    assert s==t
    assert s!=u                  
    assert s!="1"
    
    assert s<u
    assert not s>u
    
    with pytest.raises(TypeError):
        s>"3"
    with pytest.raises(TypeError):
        s<"3"
Example #8
0
def test_amplicon():
    
    from pydna.amplify    import Anneal
    from pydna.dseqrecord import Dseqrecord
    from pydna.primer     import Primer

    
    template = Dseqrecord("AAAtacactcaccgtctatcattatctactatcgactgtatcatctgatagcacTTT")
    
    p1 = Primer("CCCtacactcaccgtctatcattatc")
    p2 = Primer("GGGgtgctatcagatgatacagtcg")
    
    ann = Anneal((p1,p2),template)
    
    prod = ann.products[0]
    
    assert repr(prod) == 'Amplicon(57)'
    
    assert prod._repr_html_() == 'Amplicon(57)'
    
    from unittest.mock import MagicMock
    
    pp = MagicMock()
    
    prod._repr_pretty_(pp, None)
    
    #assert pp.text.assert_called_with('Amplicon(57)')
    
    
    fig='''    5tacactcaccgtctatcattatc...cgactgtatcatctgatagcac3
                               |||||||||||||||||||||| tm 55.9 (dbd) 60.5
                              3gctgacatagtagactatcgtgGGG5
 5CCCtacactcaccgtctatcattatc3
     ||||||||||||||||||||||| tm 54.6 (dbd) 58.8
    3atgtgagtggcagatagtaatag...gctgacatagtagactatcgtg5'''
    
    import textwrap
    
    assert prod.figure() == textwrap.dedent(fig)
    
    assert prod.program() == prod.taq_program()
    
    assert prod.pfu_sso7d_program() == prod.dbd_program()

    from pydna.amplicon import Amplicon

    from Bio.Seq import Seq
    from Bio.Alphabet.IUPAC import IUPACAmbiguousDNA
    from pydna.seqrecord import SeqRecord
    
    arg = SeqRecord(Seq("aaa", IUPACAmbiguousDNA()))
    
    x = Amplicon.from_SeqRecord(arg)
Example #9
0
def test_olaps():
    from Bio.Seq import Seq
    from Bio.SeqRecord import SeqRecord as BSeqRecord
    from pydna.dseq import Dseq
    from pydna.dseqrecord import Dseqrecord
    from pydna.seqrecord  import SeqRecord
    from Bio.Alphabet.IUPAC import IUPACAmbiguousDNA
    s = SeqRecord(Seq("GGATCC",alphabet=IUPACAmbiguousDNA()))
    assert "GGATCC" == str(s.olaps("GGATCC", limit = 4)[0].seq)
    assert "GGATCC" == str(s.olaps(Seq("GGATCC",alphabet=IUPACAmbiguousDNA()), limit = 4)[0].seq)
    assert "GGATCC" == str(s.olaps(BSeqRecord(Seq("GGATCC",alphabet=IUPACAmbiguousDNA())), limit = 4)[0].seq)
    assert "GGATCC" == str(s.olaps(Dseq("GGATCC",alphabet=IUPACAmbiguousDNA()), limit = 4)[0].seq)
    assert "GGATCC" == str(s.olaps(Dseqrecord(Dseq("GGATCC")), limit = 4)[0].seq)
    assert "GGATCC" == str(s.olaps(Dseqrecord("GGATCC"), limit = 4)[0].seq)
Example #10
0
def test_genbankfile():

    from pydna import genbankrecord

    gbr = genbankrecord.GenbankRecord("aaa")

    assert (
        gbr.hyperlink ==
        "<a href='https://www.ncbi.nlm.nih.gov/nuccore/accession?from=&to=&strand=1' target='_blank'>accession</a>"
    )

    assert repr(gbr) == "Gbank(accession)(-3)"

    assert (
        gbr._repr_html_() ==
        "<a href='https://www.ncbi.nlm.nih.gov/nuccore/accession?from=&to=&strand=1' target='_blank'>accession</a>"
    )

    from unittest.mock import MagicMock

    pp = MagicMock()

    gbr._repr_pretty_(pp, None)

    pp.text.assert_called_with("Gbank(accession)(-3)")

    gbr = genbankrecord.GenbankRecord("aaa", start=1, stop=2)

    assert (
        gbr.hyperlink ==
        "<a href='https://www.ncbi.nlm.nih.gov/nuccore/accession?from=1&to=2&strand=1' target='_blank'>accession 1-2</a>"
    )

    gbr_rc = gbr.rc()

    assert gbr_rc.strand == 2

    from Bio.Seq import Seq
    from pydna.seqrecord import SeqRecord

    arg = SeqRecord(Seq("aaa"))

    genbankrecord.GenbankRecord.from_SeqRecord(arg)
Example #11
0
 def __str__(self):
     return ("Dseqrecord\n"
             "circular: {}\n"
             "size: {}\n").format(self.circular,
                                  len(self)) + _SeqRecord.__str__(self)
Example #12
0
def test_stamp():
    from pydna.seqrecord import SeqRecord
    from pydna.readers import read
    from pydna.utils import eq

    from Bio.Seq import Seq
    from Bio.SeqRecord import SeqRecord as Srec

    a = SeqRecord("attt")
    assert a.stamp() == "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo"
    assert "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo" in a.definition
    assert a.stamp() == "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo"

    a = SeqRecord("attt")
    a.description = "something"
    assert a.stamp() == "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo"
    assert "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo" in a.definition
    assert a.stamp() == "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo"
    assert "something" in a.definition

    a = SeqRecord("attt")
    assert a.stamp() == "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo"
    a.seq = a.seq + "a"
    with pytest.raises(ValueError):
        a.stamp()

    a = SeqRecord(Seq("attt"))
    assert a.stamp() == "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo"
    assert "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo" in a.definition
    assert a.stamp() == "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo"

    a = SeqRecord(Seq("attt"))
    a.description = "something"
    assert a.stamp() == "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo"
    assert "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo" in a.definition
    assert a.stamp() == "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo"
    assert "something" in a.definition

    a = SeqRecord(Seq("attt"))
    assert a.stamp() == "SEGUID_ot6JPLeAeMmfztW1736Kc6DAqlo"
    a.seq = a.seq + "a"
    with pytest.raises(ValueError):
        a.stamp()
Example #13
0
def test_add_feature():
    from Bio.Seq import Seq
    from Bio.SeqRecord import SeqRecord as BSeqRecord
    from pydna.dseq import Dseq
    from pydna.dseqrecord import Dseqrecord
    from pydna.seqrecord import SeqRecord

    s = SeqRecord("tttGGATCCaaa")
    s.add_feature(3, 9)
    assert s.extract_feature(0).seq == SeqRecord("GGATCC").seq
    s = SeqRecord("tttGGATCCaaa")
    s.add_feature(seq="GGATCC")
    assert s.extract_feature(0).seq == SeqRecord("GGATCC").seq
    s = SeqRecord("tttGGATCCaaa")
    s.add_feature(seq=Seq("GGATCC"))
    assert s.extract_feature(0).seq == SeqRecord("GGATCC").seq
    s = SeqRecord("tttGGATCCaaa")
    s.add_feature(seq=Dseq("GGATCC"))
    assert s.extract_feature(0).seq == SeqRecord("GGATCC").seq
    s = SeqRecord("tttGGATCCaaa")
    s.add_feature(seq=SeqRecord("GGATCC"))
    assert s.extract_feature(0).seq == SeqRecord("GGATCC").seq
    s = SeqRecord("tttGGATCCaaa")
    s.add_feature(seq=BSeqRecord("GGATCC"))
    assert s.extract_feature(0).seq == SeqRecord("GGATCC").seq
    s = SeqRecord("tttGGATCCaaa")
    s.add_feature(seq=Dseqrecord("GGATCC"))
    assert s.extract_feature(0).seq == SeqRecord("GGATCC").seq
    s = SeqRecord("tttGGATCCaaa")
    with pytest.raises(TypeError):
        s.add_feature(seq=Dseqrecord("GGGGGG"))
    s = SeqRecord("tttATGaaaTAAggg")
    s.add_feature(3, 12)
    assert s.features[0].qualifiers["label"] == ["orf9"]

    from Bio.Seq import Seq

    from pydna.seqrecord import SeqRecord

    a = SeqRecord(Seq("atgtaa"))

    a.add_feature(2, 4)

    assert (
        a.list_features() ==
        "+-----+---------------+-----+-----+-----+-----+------+------+\n| Ft# | Label or Note | Dir | Sta | End | Len | type | orf? |\n+-----+---------------+-----+-----+-----+-----+------+------+\n|   0 | L:ft2         | --> | 2   | 4   |   2 | misc |  no  |\n+-----+---------------+-----+-----+-----+-----+------+------+"
    )
    a.features[0].qualifiers
    del a.features[0].qualifiers["label"]
    assert (
        a.list_features() ==
        "+-----+---------------+-----+-----+-----+-----+------+------+\n| Ft# | Label or Note | Dir | Sta | End | Len | type | orf? |\n+-----+---------------+-----+-----+-----+-----+------+------+\n|   0 | nd            | --> | 2   | 4   |   2 | misc |  no  |\n+-----+---------------+-----+-----+-----+-----+------+------+"
    )
    a.features[0].qualifiers["note"] = ["AwesomeFeature"]
    assert (
        a.list_features() ==
        "+-----+------------------+-----+-----+-----+-----+------+------+\n| Ft# | Label or Note    | Dir | Sta | End | Len | type | orf? |\n+-----+------------------+-----+-----+-----+-----+------+------+\n|   0 | N:AwesomeFeature | --> | 2   | 4   |   2 | misc |  no  |\n+-----+------------------+-----+-----+-----+-----+------+------+"
    )
Example #14
0
def test_contig(monkeypatch):
    monkeypatch.setenv("pydna_cached_funcs", "")

    from pydna import contig
    from pydna.assembly import Assembly
    from pydna.dseqrecord import Dseqrecord

    a = Dseqrecord("acgatgctatactgCCCCCtgtgctgtgctcta", name="one")
    b = Dseqrecord("tgtgctgtgctctaTTTTTtattctggctgtatc", name="two")
    c = Dseqrecord("tattctggctgtatcGGGGGtacgatgctatactg", name="three")
    asm = Assembly((a, b, c), limit=14)

    cnt = asm.assemble_circular()[0]

    assert repr(cnt) == "Contig(o59)"

    assert cnt.detailed_figure() == str(
        "||||||||||||||\n"
        "acgatgctatactgCCCCCtgtgctgtgctcta\n"
        "                   TGTGCTGTGCTCTA\n"
        "                   tgtgctgtgctctaTTTTTtattctggctgtatc\n"
        "                                      TATTCTGGCTGTATC\n"
        "                                      tattctggctgtatcGGGGGtacgatgctatactg\n"
        "                                                           ACGATGCTATACTG\n"
    )

    from textwrap import indent

    fig = """ -|one|14
|      \\/
|      /\\
|      14|two|15
|             \\/
|             /\\
|             15|three|14
|                      \\/
|                      /\\
|                      14-
|                         |
 -------------------------"""

    cnt2 = asm.assemble_linear()[0]

    fig = ('one|14\n'
           '    \\/\n'
           '    /\\\n'
           '    14|two|15\n'
           '           \\/\n'
           '           /\\\n'
           '           15|three')

    assert fig == cnt2.figure()

    assert repr(cnt2) == 'Contig(-73)'

    #print(repr(cnt2._repr_html_()))

    assert cnt2._repr_html_(
    ) == '<pre>one|14\n    \\/\n    /\\\n    14|two|15\n           \\/\n           /\\\n           15|three</pre>'

    from unittest.mock import MagicMock

    pp = MagicMock()

    cnt2._repr_pretty_(pp, None)

    pp.text.assert_called_with('Contig(-73)')

    from Bio.Seq import Seq
    from Bio.Alphabet.IUPAC import IUPACAmbiguousDNA
    from pydna.seqrecord import SeqRecord

    arg = SeqRecord(Seq("aaa", IUPACAmbiguousDNA()))

    import networkx as nx

    x = contig.Contig.from_SeqRecord(arg, graph=nx.MultiDiGraph())