def test_primer_design_four_fragments(): x = [primer_design(f) for f in frags] fourth = Dseqrecord("TAAAAATAAAATTGTTGACAGCAGAAGTGATATAGAAATTTGTTAATTATTA") y = assembly_fragments(x + [fourth], 20) z = Assembly(y, limit=20) result = z.assemble_linear()[0] assert result.seq == (frags[0] + frags[1] + frags[2] + fourth).seq
def test_primer_Design_multiple_products(): b = Dseqrecord( "agctactgactattaggggttaagctactgactattaggggtttctgatcatctgatctactatctgactgtactgatcta" ) from pydna import _PydnaWarning with pytest.warns(_PydnaWarning): a = primer_design(b)
def get_all(folder='/home/pandyr/Dropbox (MIT)/Research/Plasmids'): """ Gather all sequence files in fasta folder """ all_data = {} # initialize storage # Iterate through all files in fasta folder for file in os.listdir(folder): if file.endswith(".dna"): seq = get(folder + '/' + file) seqname = get_seqname(file) all_data[seqname] = Dseqrecord(seq, linear=False) return all_data
def test_editor_wo_features(monkeypatch): import subprocess from pydna import editor Popen = mock.MagicMock(name="subprocess.Popen") monkeypatch.setattr("subprocess.Popen", Popen) monkeypatch.setenv("pydna_ape", "path/to/ape") from pydna.dseqrecord import Dseqrecord argument = Dseqrecord("ggatcc") editor.ape(argument) assert Popen.called
def test_lcs(): from collections import OrderedDict 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").__dict__ == {'location': None, 'type': '', 'id': '<unknown id>', 'qualifiers': OrderedDict() } 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__ )
def test_editor_with_feature_wo_label_and_note(monkeypatch): import subprocess from pydna import editor Popen = mock.MagicMock(name="subprocess.Popen") monkeypatch.setattr("subprocess.Popen", Popen) monkeypatch.setenv("pydna_ape", "path/to/ape") from pydna.dseqrecord import Dseqrecord argument = Dseqrecord("ggatcc") argument.add_feature(2, 4) del argument.features[0].qualifiers["label"] editor.ape(argument) assert Popen.called
def infer_ref(line): contig=line[0] kmers=contig2kmer(contig) sp_case,cov_case,sp_control,cov_control,refpairs=contig_sp_cov.loc[contig].tolist() refpairs=refpairs.split(',') if len(refpairs)==1:return (refpairs[0],sp_case,cov_case,sp_control,cov_control) try: refseq=Assembly([Dseqrecord(i) for i in refpairs],limit=15).assemble_linear(max_nodes=3)[0].seq.watson if maxoverlap(contig,refseq)[2]<15:refseq=assemDNA(refpairs)#for sake of low complexity sequences except:refseq=assemDNA(refpairs) if maxoverlap(contig,refseq)[2]<15:refseq=str(Seq(refseq).reverse_complement()) return (refseq,sp_case,cov_case,sp_control,cov_control)
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)
def toDSEQ(self, graph, edges, nodes): complements = self.generateComplements(nodes) dna = [] offset = -10 for edge in edges: seq = Dseq(edges[edge], complements[edge[1]], ovhg=offset) x = Dseqrecord(seq) x.name = edge[0] + "_" + edge[1] x.seq = seq dna.append(x) return dna
def test_primer_design_same_first_and_third_Dseqrecord(): from pydna.dseqrecord import Dseqrecord x = [primer_design(f) for f in frags] y = assembly_fragments([frags[0], x[1], frags[0]], 20) z = Assembly(y, limit=20) result = z.assemble_circular()[0] assert result.cseguid() == (frags[0] + frags[1]).looped().cseguid() a = Dseqrecord("ccaaggacacaatcgagctccgatccgtactgtcgagaaacttgtatcc", name="a") b = Dseqrecord( "ctgtcgagaaacttgtatccctctaactagtatggatagccgtgtcttcactgtgctgcggctacccatcccaaggacacaatcgagctc", name="b") z = Assembly((a, b, a), limit=20) result = z.assemble_linear()[0] assert str( result.seq ) == "ccaaggacacaatcgagctccgatccgtactgtcgagaaacttgtatccctctaactagtatggatagccgtgtcttcactgtgctgcggctacccatcccaaggacacaatcgagctccgatccgtactgtcgagaaacttgtatcc" result = z.assemble_circular()[0] assert result.cseguid() == (frags[0] + frags[1]).looped().cseguid()
def infer_ref_unpair(line,unpair_reads_dict): contig,refseq=line[0],'' kmers=contig2kmer(contig) sp_case,cov_case,sp_control,cov_control,refpairs=contig_sp_cov.loc[contig].tolist() refpairs=refpairs.split(',') related_reads=unpair_reads_dict[contig] refseq='NA' if len(refpairs)>2:#indels should have no more than 2 paired refs.(head and tail) try: refseq=Assembly([Dseqrecord(i) for i in refpairs],limit=15).assemble_linear(max_nodes=3)[0].seq.watson except:refseq=assemDNA(refpairs) if len(related_reads)>lowdepth/2 and len(refseq)<len(contig):refseq=CAP(contig,related_reads) if maxoverlap(contig,refseq)[2]<15:refseq=str(Seq(refseq).reverse_complement()) return (refseq,sp_case,cov_case,sp_control,cov_control)
def test_primer_Design(): ''' test_primer_design''' a = Dseqrecord( "atgactgctaacccttccttggtgttgaacaagatcgacgacatttcgttcgaaacttacgatg") b = Dseqrecord( "ccaaacccaccaggtaccttatgtaagtacttcaagtcgccagaagacttcttggtcaagttgcc") c = Dseqrecord( "tgtactggtgctgaaccttgtatcaagttgggtgttgacgccattgccccaggtggtcgtttcgtt") frags = assembly_fragments([primer_design(r) for r in (a, b, c)]) asm = Assembly(frags) assert asm.assemble_linear()[0].seguid() == "1eNv3d_1PqDPP8qJZIVoA45Www8" frags = assembly_fragments([primer_design(r) for r in (a, b, c, a)]) a2 = pcr(frags[-1].forward_primer, frags[0].reverse_primer, a) asm = Assembly((a2, frags[1], frags[2])) assert asm.assemble_circular()[0].cseguid( ) == "V3Mi8zilejgyoH833UbjJOtDMbc"
def test_no_fwdprimer_anneal(): f0, r0 = parse_primers('''>ForwardPrimer gctact >ReversePrimer tgtggttactgactctatcttg''') t0 = Dseqrecord("gctactacacacgtactgactgcctccaagatagagtcagtaaccaca") f = f0 r = r0 t = t0 with pytest.raises(ValueError): pcr(f, r, t)
def test_string_arguments(): f0, r0 = parse_primers('''>ForwardPrimer gctactacacacgtactgactg >ReversePrimer tgtggttactgactctatcttg''') t0 = Dseqrecord("gctactacacacgtactgactgcctccaagatagagtcagtaaccaca") f = str(f0.seq) r = str(r0.seq) t = str(t0.seq) assert str(pcr( (f, r), t).seq) == "gctactacacacgtactgactgcctccaagatagagtcagtaaccaca"
def test_Primer_arguments(): f0, r0 = parse_primers('''>ForwardPrimer gctactacacacgtactgactg >ReversePrimer tgtggttactgactctatcttg''') t0 = Dseqrecord("gctactacacacgtactgactgcctccaagatagagtcagtaaccaca") f = f0 r = r0 t = t0 assert str(pcr( f, r, t).seq) == "gctactacacacgtactgactgcctccaagatagagtcagtaaccaca"
def test_Seq_arguments(): from Bio.Seq import Seq f0, r0 = parse_primers(""">ForwardPrimer gctactacacacgtactgactg >ReversePrimer tgtggttactgactctatcttg""") t0 = Dseqrecord("gctactacacacgtactgactgcctccaagatagagtcagtaaccaca") f = Seq(str(f0.seq)) r = Seq(str(r0.seq)) t = Seq(str(t0.seq)) assert str(pcr( f, r, t).seq) == "gctactacacacgtactgactgcctccaagatagagtcagtaaccaca"
def test_Dseq_arguments(): from pydna.dseq import Dseq f0, r0 = parse_primers('''>ForwardPrimer gctactacacacgtactgactg >ReversePrimer tgtggttactgactctatcttg''') t0 = Dseqrecord("gctactacacacgtactgactgcctccaagatagagtcagtaaccaca") f = Dseq(str(f0.seq)) r = Dseq(str(r0.seq)) t = Dseq(str(t0.seq)) assert str(pcr( f, r, t).seq) == "gctactacacacgtactgactgcctccaagatagagtcagtaaccaca"
def test_feature_label(): f0, r0 = parse_primers('''>ForwardPrimer gctactacacacgtactgactg >ReversePrimer tgtggttactgactctatcttg''') t0 = Dseqrecord("gctactacacacgtactgactgcctccaagatagagtcagtaaccaca") t0.add_feature() f = f0 r = r0 t = t0 assert str(pcr( f, r, t).seq) == "gctactacacacgtactgactgcctccaagatagagtcagtaaccaca"
def test_pcr_not_specific(): f0, r0 = parse_primers(""">ForwardPrimer gctactacacacgtactgactg >ReversePrimer tgtggttactgactctatcttg""") t0 = Dseqrecord( "gctactacacacgtactgactgtgctactacacacgtactgactgcctccaagatagagtcagtaaccaca" ) f = f0 r = r0 t = t0 with pytest.raises(ValueError): pcr(f, r, t)
def build_primers_for_part(self, part): """Build extraction primers and assign them into the part.""" part_seq_record = part.get_sequence() forward_primer = part.extraction_primers.forward_primer if part.has_primers else None reverse_primer = part.extraction_primers.reverse_primer if part.has_primers else None amplicon = primer_design(Dseqrecord(part_seq_record), fp=forward_primer, rp=reverse_primer, tm_func=self._perform_tm_func, limit=13) if not part.has_primers: part.set_extraction_primers( PrimerPair.from_sequences(amplicon.forward_primer.seq, amplicon.reverse_primer.seq)) return amplicon
def test_gel(): from pydna.gel import gel from pydna.ladders import PennStateLadder from pydna.dseqrecord import Dseqrecord mygel = gel([PennStateLadder, [Dseqrecord("A" * 2000)]]) from PIL import Image im = Image.open("pydna_gel.png") import numpy as np frame1 = np.asarray(mygel) frame2 = np.asarray(im) assert frame1.all() == frame2.all()
def test_feature_note(): f0, r0 = parse_primers('''>ForwardPrimer gctactacacacgtactgactg >ReversePrimer tgtggttactgactctatcttg''') t0 = Dseqrecord("gctactacacacgtactgactgcctccaagatagagtcagtaaccaca") t0.add_feature() del t0.features[0].qualifiers["label"] t0.features[0].qualifiers["note"] = ["note"] f = f0 r = r0 t = t0 assert str(pcr( f, r, t).seq) == "gctactacacacgtactgactgcctccaagatagagtcagtaaccaca" assert pcr(f, r, t).name == "note"
def test_set_primer_footprint(): f, r = parse_primers(""">ForwardPrimer gctactacacacgtactgactg >ReversePrimer tgtggttactgactctatcttg""") t = Dseqrecord("gctactacacacgtactgactgcctccaagatagagtcagtaaccaca") ampl = pcr((f, r), t) assert len(ampl.forward_primer.footprint) == 22 assert len(ampl.reverse_primer.footprint) == 22 ampl.set_forward_primer_footprint(15) ampl.set_reverse_primer_footprint(15) assert len(ampl.forward_primer.footprint) == 15 assert len(ampl.reverse_primer.footprint) == 15
def test_amplicon_dbd_low_gc(): from pydna.amplify import Anneal from pydna.dseqrecord import Dseqrecord from pydna.primer import Primer from textwrap import dedent template = Dseqrecord( "AAAATATTTTTATACATAATACAATTGTATATTCTTAAATAAAAAATACGTCATC") p1 = Primer("AAAATATTTTTATACAT") p2 = Primer("GATGACGTATTTTTTAT") ann = Anneal((p1, p2), template) prod = ann.products[0] assert repr(prod) == 'Amplicon(55)' fig = (r''' Pfu-Sso7d (rate 15s/kb) |55bp Three-step| 30 cycles | |Tm formula: Pydna tmbresluc 98.0°C |98.0°C | |SaltC 50mM __________|_____ 72.0°C |72.0°C|Primer1C 1.0µM 00min30s |10s \ 39.0°C ________|______|Primer2C 1.0µM | \______/ 0min 0s|10min |GC 14% | 10s | |4-12°C '''[1:]) fig = (r''' |98°C|98°C | |tmf:32.6 |____|_____ 72°C|72°C|tmr:39.6 |30s |10s \ 35.6°C _____|____|15s/kb | | \______/ 0: 0|5min|GC 14% | | 10s | |55bp '''[1:]) fig = dedent(fig) assert str(prod.dbd_program()) == fig
def test_amplicon_dbd(): from pydna.amplify import Anneal from pydna.dseqrecord import Dseqrecord from pydna.primer import Primer from textwrap import dedent template = Dseqrecord( "GCGTCCAGCGGCTGCCCGAGGCGCCAAGTGCCCGGGCCGAGCCCGCATCTGAGGCCGCCGCGGGC") p1 = Primer("GCGTCCAGCGGCTGCCCGAGG") p2 = Primer("GCCCGCGGCGGCCTCAGATGCGG") ann = Anneal((p1, p2), template) prod = ann.products[0] assert repr(prod) == 'Amplicon(65)' fig = (r''' Pfu-Sso7d (rate 15s/kb) Two-step| 30 cycles | |65bp 98.0°C |98.0C | |Tm formula: Pydna tmbresluc _____ __|_____ | |SaltC 50mM 00min30s|10s \ | |Primer1C 1.0µM | \ 72.0°C|72.0°C|Primer2C 1.0µM | \______|______|GC 81% | 0min 0s|10min |4-12°C '''[1:]) fig = (r''' |98°C|98°C | |tmf:71.6 |____|____ | |tmr:75.3 |30s |10s \ 72°C|72°C|15s/kb | | \____|____|GC 81% | | 0: 0|5min|65bp '''[1:]) fig = dedent(fig) assert str(prod.dbd_program()) == fig
def test_Amplicon_argument(): f0, r0 = parse_primers('''>ForwardPrimer gctactacacacgtactgactg >ReversePrimer tgtggttactgactctatcttg''') t0 = Dseqrecord("gctactacacacgtactgactgcctccaagatagagtcagtaaccaca") f = f0 r = r0 t = t0 ampl = pcr(f, r, t) assert str(ampl.seq) == "gctactacacacgtactgactgcctccaagatagagtcagtaaccaca" amplicon_from_amplicon = pcr(ampl) assert str(amplicon_from_amplicon.seq ) == "gctactacacacgtactgactgcctccaagatagagtcagtaaccaca"
try: p = sg.sysgenes[insertname] except KeyError: raise ValueError('No gene by that name in S.c.') pf, pr, comment = tp_dict[insertname] print(insertname, pf, pr, comment) fp = lp[int(pf)] rp = lp[int(pr)] gbref = p.promoter().description gblink = p.cds().id template = Dseqrecord(p.promoter()) templatesize = len(template) insertseguid = template.seguid() finalcseguidZ = (pYPKa.linearize(ZraI) + pcr(fp, rp, template)).looped().cseguid() finalcseguidE = (pYPKa.linearize(EcoRV) + pcr(fp, rp, template)).looped().cseguid() content = t.format(tp=insertname, gbref=gbref, gblink=gblink, templatesize=templatesize, insertseguid=insertseguid, finalcseguidZ=finalcseguidZ,
from pydna.design import primer_design from pydna.dseqrecord import Dseqrecord t = Dseqrecord("atgactgctaacccttccttggtgttgaacaagatcgacgacatttcgttcgaaacttacgatg") from pydna.primer import Primer pf = Primer("atgactgctaacccttccttggtg") print(pf.name) ampl = primer_design(t, fp=pf) print(ampl.forward_primer.name)
def test_primer_Design_given_fw_primer(): b = Dseqrecord( "agctactgactattaggggttattctgatcatctgatctactatctgactgtactgatcta") a = primer_design(b, fp=Primer("agctactgactattag")) assert str(a.reverse_primer.seq) == 'tagatcagtacagtca'
def test_primer_Design_given_rv_primer(): b = Dseqrecord( "agctactgactattaggggttattctgatcatctgatctactatctgactgtactgatcta") a = primer_design(b, rp=Primer("tagatcagtacagtca")) assert str(a.forward_primer.seq) == 'agctactgactattag' #g